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爬虫常用的模块分析
Aug 29 Python
Python使用ftplib实现简易FTP客户端的方法
Jun 03 Python
python实现爬虫统计学校BBS男女比例之数据处理(三)
Dec 31 Python
利用pyinstaller或virtualenv将python程序打包详解
Mar 22 Python
pycharm安装图文教程
May 02 Python
Python入门_浅谈字符串的分片与索引、字符串的方法
May 16 Python
EM算法的python实现的方法步骤
Jan 02 Python
浅谈pyqt5中信号与槽的认识
Feb 17 Python
Python pickle模块实现对象序列化
Nov 22 Python
通过python实现windows桌面截图代码实例
Jan 17 Python
Python3实现监控新型冠状病毒肺炎疫情的示例代码
Feb 13 Python
使用Python提取文本中含有特定字符串的方法示例
Dec 09 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/05/10 PHP
php的hash算法介绍
2014/02/13 PHP
PHPer 需要了解的 5 个 Composer 小技巧
2014/08/18 PHP
PHP实现的增强性mhash函数
2015/05/27 PHP
php flush无效,IIS7下php实时输出的方法
2016/08/25 PHP
centos+php+coreseek+sphinx+mysql之一coreseek安装篇
2016/10/25 PHP
phpStudy 2016 使用教程详解(支持PHP7)
2017/10/18 PHP
php实现大文件断点续传下载实例代码
2019/10/01 PHP
学习YUI.Ext第七日-View&amp;JSONView Part Two-一个画室网站的案例
2007/03/10 Javascript
Jquery命名冲突解决的五种方案分享
2012/03/16 Javascript
JS解析json数据并将json字符串转化为数组的实现方法
2012/12/25 Javascript
extjs3 combobox取value和text案例详解
2013/02/06 Javascript
js给onclick赋值传参数的两种方法
2013/11/25 Javascript
js中substr,substring,indexOf,lastIndexOf的用法小结
2013/12/27 Javascript
jquery自适应布局的简单实例
2016/05/28 Javascript
修改js confirm alert 提示框文字的简单实例
2016/06/10 Javascript
JS实现可编辑的后台管理菜单功能【附demo源码下载】
2016/09/13 Javascript
详解Vue整合axios的实例代码
2017/06/21 Javascript
Vue中keep-alive 实现后退不刷新并保持滚动位置
2020/03/17 Javascript
[54:19]完美世界DOTA2联赛PWL S2 Magma vs PXG 第二场 11.28
2020/12/01 DOTA
Python使用pygame模块编写俄罗斯方块游戏的代码实例
2015/12/08 Python
Python3实现发送QQ邮件功能(附件)
2020/12/23 Python
Python标准库使用OrderedDict类的实例讲解
2019/02/14 Python
django中间键重定向实例方法
2019/11/10 Python
python异常处理和日志处理方式
2019/12/24 Python
Python字符串hashlib加密模块使用案例
2020/03/10 Python
在pycharm中关掉ipython console/PyDev操作
2020/06/09 Python
美国最受欢迎的度假租赁网站:VRBO
2016/08/02 全球购物
奥地利票务门户网站:oeticket.com
2019/12/31 全球购物
YSL圣罗兰美妆俄罗斯官网:Yves Saint Lauret RU
2020/09/23 全球购物
大学本科毕业生的自我鉴定范文
2013/11/19 职场文书
幼儿教师师德师风演讲稿
2014/08/22 职场文书
本科毕业论文指导教师评语
2014/12/30 职场文书
乔迁之喜答谢词
2015/01/05 职场文书
2015少先队大队辅导员工作总结
2015/07/24 职场文书
2019学生会干事辞职信
2019/06/27 职场文书