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 相关文章推荐
使用wxPython获取系统剪贴板中的数据的教程
May 06 Python
简述Python中的进程、线程、协程
Mar 18 Python
Python 爬虫之超链接 url中含有中文出错及解决办法
Aug 03 Python
Python 在字符串中加入变量的实例讲解
May 02 Python
python操作xlsx文件的包openpyxl实例
May 03 Python
TensorFlow 合并/连接数组的方法
Jul 27 Python
一篇文章彻底搞懂Python中可迭代(Iterable)、迭代器(Iterator)与生成器(Generator)的概念
May 13 Python
python文件写入write()的操作
May 14 Python
python获取引用对象的个数方式
Dec 20 Python
Python3变量与基本数据类型用法实例分析
Feb 14 Python
使用pytorch实现论文中的unet网络
Jun 24 Python
5款实用的python 工具推荐
Oct 13 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
thinkPHP框架可添加js事件的分页类customPage.class.php完整实例
2017/03/16 PHP
深入浅析PHP的session反序列化漏洞问题
2017/06/15 PHP
JavaScript 指导方针
2007/04/05 Javascript
JavaScript 实现类的多种方法实例
2013/05/01 Javascript
JS验证日期的格式YYYY-mm-dd 具体实现
2013/06/29 Javascript
基于bootstrap3和jquery的分页插件
2015/07/31 Javascript
在微信、支付宝、百度钱包实现点击返回按钮关闭当前页面和窗口的方法
2016/08/05 Javascript
微信小程序 教程之注册页面
2016/10/17 Javascript
javascript中href和replace的比较(详解)
2016/11/25 Javascript
JavaScript 限制文本框不可输入英文单双引号的方法
2016/12/20 Javascript
JS基于正则实现数字千分位用逗号分隔的方法
2017/06/16 Javascript
详解require.js配置路径的用法和css的引入
2017/09/06 Javascript
jQuery自动或手动图片切换效果
2017/10/11 jQuery
AngularJS日期格式化常见操作实例分析
2018/05/17 Javascript
nodejs中express入门和基础知识点学习
2018/09/13 NodeJs
详解angular2.x创建项目入门指令
2018/10/11 Javascript
Vue中Axios从远程/后台读取数据
2019/01/21 Javascript
记录vue做微信自定义分享的一些问题
2019/09/12 Javascript
JS实现排行榜文字向上滚动轮播效果
2019/11/26 Javascript
深入理解redux之compose的具体应用
2020/01/12 Javascript
jQuery实现中奖播报功能(让文本滚动起来) 简单设置数值即可
2020/03/20 jQuery
python中的yield使用方法
2014/02/11 Python
Python使用chardet判断字符编码
2015/05/09 Python
python+webdriver自动化环境搭建步骤详解
2019/06/03 Python
对Python 字典元素进行删除的方法
2020/07/31 Python
python字典key不能是可以是啥类型
2020/08/04 Python
python 删除系统中的文件(按时间,大小,扩展名)
2020/11/19 Python
HTML5的结构和语义(5):交互
2008/10/17 HTML / CSS
澳大利亚便宜隐形眼镜购买网站:QUICKLENS Australia
2018/10/06 全球购物
为什么要使用servlet
2016/01/17 面试题
工业设计专业个人求职信范文
2013/12/28 职场文书
小学生2014国庆节演讲稿:祖国在我心中
2014/09/21 职场文书
工程部经理岗位职责
2015/02/02 职场文书
2015年度优秀员工获奖感言
2015/07/31 职场文书
2016年校园社会综合治理宣传月活动总结
2016/03/16 职场文书
JS轻量级函数式编程实现XDM二
2022/06/16 Javascript