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获取Linux下文件版本信息、公司名和产品名的方法
Oct 05 Python
利用Python如何生成hash值示例详解
Dec 20 Python
python多线程http压力测试脚本
Jun 25 Python
对Python中class和instance以及self的用法详解
Jun 26 Python
Pytorch加载部分预训练模型的参数实例
Aug 18 Python
Python3分析处理声音数据的例子
Aug 27 Python
django实现模板中的字符串文字和自动转义
Mar 31 Python
Python之Matplotlib文字与注释的使用方法
Jun 18 Python
对python中list的五种查找方法说明
Jul 13 Python
python进行二次方程式计算的实例讲解
Dec 06 Python
用python自动生成日历
Apr 24 Python
linux中nohup和后台运行进程查看及终止
Jun 24 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
Access数据库导入Mysql的方法之一
2006/10/09 PHP
phpphp图片采集后按原路径保存图片示例
2014/02/18 PHP
Yii2 批量插入、更新数据实例
2017/03/15 PHP
浅析PHP中的闭包和匿名函数
2017/12/25 PHP
PHP实现抽奖功能实例代码
2020/06/30 PHP
用JS剩余字数计算的代码
2008/07/03 Javascript
IE6、IE7中获取Button元素的值的bug说明
2011/08/28 Javascript
js常用代码段整理
2011/11/30 Javascript
最短的IE判断var ie=!-[1,]分析
2014/05/28 Javascript
JavaScript函数学习总结以及相关的编程习惯指南
2015/11/16 Javascript
javascript高级编程之函数表达式 递归和闭包函数
2015/11/29 Javascript
Javascript之BOM(window对象)详解
2016/05/25 Javascript
JavaScript 中使用 Generator的方法
2017/12/29 Javascript
Vue 页面状态保持页面间数据传输的一种方法(推荐)
2018/11/01 Javascript
jquery 键盘事件 keypress() keydown() keyup()用法总结
2019/10/23 jQuery
vue移动端使用canvas签名的实现
2020/01/15 Javascript
js+for循环实现字符串自动转义的代码(把后面的字符替换前面的字符)
2020/12/24 Javascript
[02:34]DOTA2亚洲邀请赛 BG战队出场宣传片
2015/03/09 DOTA
Python 基础教程之闭包的使用方法
2017/09/29 Python
Flask框架工厂函数用法实例分析
2019/05/25 Python
python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据示例
2019/11/28 Python
Python编程快速上手——疯狂填词程序实现方法分析
2020/02/29 Python
python获取响应某个字段值的3种实现方法
2020/04/30 Python
python爬取音频下载的示例代码
2020/10/19 Python
Python爬虫之Selenium实现关闭浏览器
2020/12/04 Python
Html5新增标签与样式及让元素水平垂直居中
2019/07/11 HTML / CSS
亚洲独特体验旅游专家:eOasia
2018/08/15 全球购物
Under Armour安德玛德国官网:美国高端运动科技品牌
2019/03/09 全球购物
Boom手表官网:瑞典手表品牌,设计你的手表
2019/03/11 全球购物
JavaScript获取当前url根目录(路径)
2014/02/19 面试题
物流仓储实习自我鉴定
2013/09/25 职场文书
法定代表人身份证明书
2014/09/10 职场文书
稽核岗位职责范本
2015/04/13 职场文书
刑事上诉状(量刑过重)
2015/05/23 职场文书
小学少先队工作总结2015
2015/05/26 职场文书
使用numpy nonzero 找出非0元素
2021/05/14 Python