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 thread 并发且顺序运行示例
Apr 09 Python
Python实现抓取百度搜索结果页的网站标题信息
Jan 22 Python
用Python中的__slots__缓存资源以节省内存开销的方法
Apr 02 Python
Python中pip安装非PyPI官网第三方库的方法
Jun 02 Python
python采用django框架实现支付宝即时到帐接口
May 17 Python
python中OrderedDict的使用方法详解
May 05 Python
Python中矩阵库Numpy基本操作详解
Nov 21 Python
Python 删除连续出现的指定字符的实例
Jun 29 Python
Django使用Jinja2模板引擎的示例代码
Aug 09 Python
django实现用户注册实例讲解
Oct 30 Python
Django封装交互接口代码
Jul 12 Python
Python还能这么玩之用Python做个小游戏的外挂
Jun 04 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
PHP4中session登录页面的应用
2008/07/25 PHP
ajax取消挂起请求的处理方法
2013/03/18 PHP
PHP编程基本语法快速入门手册
2016/01/07 PHP
PHP实现字符串的全排列详解
2019/04/24 PHP
escape、encodeURI、encodeURIComponent等方法的区别比较
2006/12/27 Javascript
让IE6支持min-width和max-width的方法
2010/06/25 Javascript
js中eval详解
2012/03/30 Javascript
jQuery实现仿QQ头像闪烁效果的文字闪动提示代码
2015/11/03 Javascript
基于html5和nodejs相结合实现websocket即使通讯
2015/11/19 NodeJs
JS防止网页被嵌入iframe框架的方法分析
2016/09/13 Javascript
js判断是否为空和typeof的用法(详解)
2016/10/07 Javascript
web前端vue之CSS过渡效果示例
2018/01/10 Javascript
jQuery实现监听下拉框选中内容发生改变操作示例
2018/07/13 jQuery
electron实现qq快捷登录的方法示例
2018/10/22 Javascript
JS桶排序的简单理解与实现方法示例
2019/11/25 Javascript
谈谈node.js中的模块系统
2020/09/01 Javascript
vant-ui组件调用Dialog弹窗异步关闭操作
2020/11/04 Javascript
Python ORM框架SQLAlchemy学习笔记之数据添加和事务回滚介绍
2014/06/10 Python
详解Django框架中用户的登录和退出的实现
2015/07/23 Python
简单实现python聊天程序
2018/04/01 Python
Pytorch入门之mnist分类实例
2018/04/14 Python
python scipy求解非线性方程的方法(fsolve/root)
2018/11/12 Python
Python 使用office365邮箱的示例
2020/10/29 Python
纯CSS和jQuery实现的在页面顶部显示的进度条效果2例(仿手机浏览器进度条效果)
2014/04/16 HTML / CSS
美国家庭鞋店:Shoe Sensation
2019/09/27 全球购物
PHP数据运算类型都有哪些
2013/11/05 面试题
大学生两会精神学习心得体会
2014/03/10 职场文书
航空学院求职信
2014/06/11 职场文书
法学求职信
2014/06/22 职场文书
另类冲刺标语
2014/06/24 职场文书
关于成绩下滑的自我检讨书
2014/09/20 职场文书
个人作风建设总结
2014/10/23 职场文书
培训通知
2015/04/17 职场文书
2016抗战胜利71周年红领巾广播稿
2015/12/18 职场文书
物业管理交接协议书
2016/03/24 职场文书
anaconda python3.8安装后降级
2021/06/11 Python