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中MySQLdb模块用法实例
Nov 10 Python
Python切换pip安装源的方法详解
Nov 18 Python
利用Python自带PIL库扩展图片大小给图片加文字描述的方法示例
Aug 08 Python
Django 跨域请求处理的示例代码
May 02 Python
python如何创建TCP服务端和客户端
Aug 26 Python
python安装numpy和pandas的方法步骤
May 27 Python
Pandas中Series和DataFrame的索引实现
Jun 27 Python
django创建最简单HTML页面跳转方法
Aug 16 Python
Python序列化与反序列化pickle用法实例
Nov 11 Python
python和pywin32实现窗口查找、遍历和点击的示例代码
Apr 01 Python
安装多个版本的TensorFlow的方法步骤
Apr 21 Python
Python 通过正则表达式快速获取电影的下载地址
Aug 17 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
php生成缩略图填充白边(等比缩略图方案)
2013/12/25 PHP
PHP实现返回JSON和XML的类分享
2015/01/28 PHP
PHP中Trait及其应用详解
2017/02/14 PHP
PHPUnit测试私有属性和方法功能示例
2018/06/12 PHP
jquery使用jquery.zclip插件复制对象的实例教程
2013/12/04 Javascript
用js模拟struts2的多action调用示例
2014/05/19 Javascript
js实现文字跟随鼠标移动而移动的方法
2015/02/28 Javascript
设置jQueryUI DatePicker默认语言为中文
2016/06/04 Javascript
JavaScript生成带有缩进的表格代码
2016/06/15 Javascript
原生JS实现网络彩票投注效果
2016/09/25 Javascript
纯js三维数组实现三级联动效果
2017/02/07 Javascript
解决vue router组件状态刷新消失的问题
2018/08/01 Javascript
koa上传excel文件并解析的实现方法
2018/08/09 Javascript
vue+springboot实现项目的CORS跨域请求
2018/09/05 Javascript
详解Vue一个案例引发「内容分发slot」的最全总结
2018/12/02 Javascript
Vue 简单实现前端权限控制的示例
2020/12/25 Vue.js
Python selenium文件上传方法汇总
2020/11/19 Python
python抓取文件夹的所有文件
2018/02/27 Python
Python3 replace()函数使用方法
2018/03/19 Python
python在openstreetmap地图上绘制路线图的实现
2019/07/11 Python
使用turtle绘制五角星、分形树
2019/10/06 Python
python如何操作mysql
2020/08/17 Python
pycharm 复制代码出现空格的解决方式
2021/01/15 Python
突袭HTML5之Javascript API扩展3—本地存储全新体验
2013/01/31 HTML / CSS
中国制造网:Made-in-China.com
2019/10/25 全球购物
车贷收入证明范本
2014/01/09 职场文书
县优秀教师事迹材料
2014/01/31 职场文书
怎么写好自荐书
2014/03/02 职场文书
2014年社区个人工作总结
2014/12/02 职场文书
材料员岗位职责
2015/02/10 职场文书
公务员个人总结
2015/02/12 职场文书
项目安全员岗位职责
2015/02/15 职场文书
新年晚会开场白
2015/05/29 职场文书
婚宴致辞
2015/07/28 职场文书
学习党章心得体会2016
2016/01/15 职场文书
浅谈 JavaScript 沙箱Sandbox
2021/11/02 Javascript