python绘制立方体的方法


Posted in Python onJuly 02, 2018

本文实例为大家分享了python绘制立方体的具体代码,供大家参考,具体内容如下

#!/usr/bin/env python
 
# This is (almost) a direct C++ to Python transliteration of
# <VTK-root>/Examples/DataManipulation/Cxx/Cube.cxx from the VTK
# source distribution, which "shows how to manually create vtkPolyData"
#
# A convenience function, mkVtkIdList(), has been added and one if/else
# so the example also works in version 6 or later.
#
# Lines like `obj->Delete()` have been transliterated as `del obj` to,
# preserve the resemblance to the original C++ example, although I
# doubt this achieves anything beyond what Python's garbage collection
# would do anyway.
 
import vtk
 
# Makes a vtkIdList from a Python iterable. I'm kinda surprised that
# this is necessary, since I assumed that this kind of thing would
# have been built into the wrapper and happen transparently, but it
# seems not.
 
 
def mkVtkIdList(it):
 vil = vtk.vtkIdList()
 for i in it:
  vil.InsertNextId(int(i))
 return vil
 
# 绘制通用方法
def myShow(cube):
 # Now we'll look at it.
 cubeMapper = vtk.vtkPolyDataMapper()
 if vtk.VTK_MAJOR_VERSION <= 5:
  cubeMapper.SetInput(cube)
 else:
  cubeMapper.SetInputData(cube)
 cubeMapper.SetScalarRange(0, 7)
 cubeActor = vtk.vtkActor()
 cubeActor.SetMapper(cubeMapper)
 
 # The usual rendering stuff.
 camera = vtk.vtkCamera()
 camera.SetPosition(1, 1, 1)
 camera.SetFocalPoint(0, 0, 0)
 
 renderer = vtk.vtkRenderer()
 renWin = vtk.vtkRenderWindow()
 renWin.AddRenderer(renderer)
 
 iren = vtk.vtkRenderWindowInteractor()
 iren.SetRenderWindow(renWin)
 
 renderer.AddActor(cubeActor)
 renderer.SetActiveCamera(camera)
 renderer.ResetCamera()
 renderer.SetBackground(0, 0, 0)
 
 renWin.SetSize(300, 300)
 
 # interact with data
 renWin.Render()
 iren.Start()
 del cubeMapper
 del cubeActor
 del camera
 del renderer
 del renWin
 del iren
 
def main():
 # x = array of 8 3-tuples of float representing the vertices of a cube:
 # 8个三维值代表长方体的8个顶点
 x = [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0),
   (0.0, 0.0, 1.0), (1.0, 0.0, 1.0), (1.0, 1.0, 1.0), (0.0, 1.0, 1.0)]
 
 # pts = array of 6 4-tuples of vtkIdType (int) representing the faces
 #  of the cube in terms of the above vertices
 # 点的编号0-7,每个面由4个点组成
 pts = [(0, 1, 2, 3), (4, 5, 6, 7), (0, 1, 5, 4),
   (1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]
 
 # We'll create the building blocks of polydata including data attributes.
 cube = vtk.vtkPolyData()
 points = vtk.vtkPoints()
 polys = vtk.vtkCellArray()
 scalars = vtk.vtkFloatArray()
 
 # Load the point, cell, and data attributes.
 for i in range(8):
  points.InsertPoint(i, x[i])
 for i in range(6):
  polys.InsertNextCell(mkVtkIdList(pts[i]))
 for i in range(8):
  scalars.InsertTuple1(i, i)
 
 # We now assign the pieces to the vtkPolyData.
 cube.SetPoints(points)
 del points
 cube.SetPolys(polys)
 del polys
 cube.GetPointData().SetScalars(scalars)
 del scalars
 
 myShow(cube)
 # Clean up
 del cube
 
main()

效果图:

python绘制立方体的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中random模块用法实例分析
May 19 Python
通过数据库向Django模型添加字段的示例
Jul 21 Python
Python网络编程中urllib2模块的用法总结
Jul 12 Python
Python使用爬虫抓取美女图片并保存到本地的方法【测试可用】
Aug 30 Python
对python3标准库httpclient的使用详解
Dec 18 Python
python接口自动化(十七)--Json 数据处理---一次爬坑记(详解)
Apr 18 Python
python地震数据可视化详解
Jun 18 Python
Django为窗体加上防机器人的验证码功能过程解析
Aug 14 Python
python web框架中实现原生分页
Sep 08 Python
wxPython之wx.DC绘制形状
Nov 19 Python
python3 中时间戳、时间、日期的转换和加减操作
Jul 14 Python
python之django路由和视图案例教程
Jul 26 Python
python numpy 一维数组转变为多维数组的实例
Jul 02 #Python
Python实现通过继承覆盖方法示例
Jul 02 #Python
Numpy中矩阵matrix读取一列的方法及数组和矩阵的相互转换实例
Jul 02 #Python
Python 中的range(),以及列表切片方法
Jul 02 #Python
python 统计数组中元素出现次数并进行排序的实例
Jul 02 #Python
分享vim python缩进等一些配置
Jul 02 #Python
实践Vim配置python开发环境
Jul 02 #Python
You might like
mysql From_unixtime及UNIX_TIMESTAMP及DATE_FORMAT日期函数
2010/03/21 PHP
ThinkPHP3.1新特性之多层MVC的支持
2014/06/19 PHP
PHP使用递归按层级查找数据的方法
2019/11/10 PHP
js 发个判断字符串是否为符合标准的函数
2009/04/27 Javascript
基于dom编程中 动态创建与删除元素的使用
2013/04/17 Javascript
jquery 追加tr和删除tr示例代码
2013/09/12 Javascript
9行javascript代码获取QQ群成员具体实现
2013/10/16 Javascript
jquery移动端TAB触屏切换实现效果
2020/12/22 Javascript
深入nodejs中流(stream)的理解
2017/03/27 NodeJs
bootstrap常用组件之头部导航实现代码
2017/04/20 Javascript
微信小程序 swiper组件构建轮播图的实例
2017/09/20 Javascript
angularjs实现分页和搜索功能
2018/01/03 Javascript
Vue项目中ESlint规范示例代码
2019/07/04 Javascript
VueX模块的具体使用(小白教程)
2020/06/05 Javascript
JS实现鼠标移动拖尾
2020/12/27 Javascript
python数据结构之图的实现方法
2015/07/08 Python
Python实现分割文件及合并文件的方法
2015/07/10 Python
python subprocess 杀掉全部派生的子进程方法
2017/01/16 Python
Python实现复杂对象转JSON的方法示例
2017/06/22 Python
python实现用户答题功能
2018/01/17 Python
Python实现的凯撒密码算法示例
2018/04/12 Python
Python中if elif else及缩进的使用简述
2018/05/31 Python
一文了解Python并发编程的工程实现方法
2019/05/31 Python
python代码实现逻辑回归logistic原理
2019/08/07 Python
Keras保存模型并载入模型继续训练的实现
2021/02/20 Python
Python解析m3u8拼接下载mp4视频文件的示例代码
2021/03/03 Python
CSS3属性box-shadow使用详细教程
2012/01/21 HTML / CSS
HTML5实现移动端点击翻牌功能
2020/10/23 HTML / CSS
阿迪达斯丹麦官网:adidas丹麦
2016/10/01 全球购物
Rhone官方网站:男士运动服装、健身服装和高级运动服
2019/05/01 全球购物
SmartBuyGlasses比利时:购买品牌太阳镜和眼镜
2019/08/09 全球购物
数据库测试通常都包括哪些方面
2015/11/30 面试题
开业庆典答谢词
2014/01/18 职场文书
《记承天寺夜游》教学反思
2014/02/16 职场文书
人民教师的自我评价分享
2014/02/21 职场文书
电子专业求职信
2014/06/19 职场文书