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常用方法和技巧
May 18 Python
Python处理XML格式数据的方法详解
Mar 21 Python
Python处理PDF及生成多层PDF实例代码
Apr 24 Python
python读取中文txt文本的方法
Apr 12 Python
详谈python在windows中的文件路径问题
Apr 28 Python
Python快速查找list中相同部分的方法
Jun 27 Python
基于python3 的百度图片下载器的实现代码
Nov 05 Python
Python实现屏幕录制功能的代码
Mar 02 Python
基于python检查SSL证书到期情况代码实例
Apr 04 Python
使用anaconda安装pytorch的实现步骤
Sep 03 Python
详解Python中第三方库Faker
Sep 25 Python
Python基础详解之描述符
Apr 28 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
BBS(php &amp; mysql)完整版(六)
2006/10/09 PHP
PHP中的session永不过期的解决思路及实现方法分享
2011/04/20 PHP
PHP连接sql server 2005环境配置及问题解决
2014/08/08 PHP
php使用str_replace实现输入框回车替换br的方法
2014/11/24 PHP
Yii实现显示静态页的方法
2016/04/25 PHP
PHP数据对象PDO操作技巧小结
2016/09/27 PHP
PHP内部实现打乱字符串顺序函数str_shuffle的方法
2019/02/14 PHP
ThinkPHP5.1框架数据库链接和增删改查操作示例
2019/08/03 PHP
jQuery中filter(),not(),split()使用方法
2010/07/06 Javascript
Javascript 正则表达式实现为数字添加千位分隔符
2015/03/10 Javascript
AngularJS 所有版本下载地址
2016/09/14 Javascript
webpack+vue.js实现组件化详解
2016/10/12 Javascript
jQuery动态添加与删除tr行实例代码
2016/10/18 Javascript
js 作用域和变量详解
2017/02/16 Javascript
JS实现简易日历效果
2021/01/25 Javascript
[03:02]生活中的Dendi之野外度假篇
2016/08/09 DOTA
[57:53]Secret vs Pain 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
python网络编程学习笔记(二):socket建立网络客户端
2014/06/09 Python
Python性能优化技巧
2015/03/09 Python
Django中传递参数到URLconf的视图函数中的方法
2015/07/18 Python
Python进行数据提取的方法总结
2016/08/22 Python
python3个性签名设计实现代码
2018/06/19 Python
python redis存入字典序列化存储教程
2020/07/16 Python
Pycharm导入anaconda环境的教程图解
2020/07/31 Python
css3的transition属性详解
2014/12/15 HTML / CSS
HTML5 textarea高度自适应的两种方案
2020/04/08 HTML / CSS
用Python写一个for循环的例子
2016/07/19 面试题
心得体会范文
2014/01/04 职场文书
培训楼经理岗位责任制
2014/02/10 职场文书
《小小雨点》教学反思
2014/02/18 职场文书
中学语文教学反思
2016/02/16 职场文书
公文写作:教你写“建议书”
2019/05/07 职场文书
深入理解CSS 中 transform matrix矩阵变换问题
2021/08/30 HTML / CSS
一小时迅速入门Mybatis之bind与多数据源支持 Java API
2021/09/15 Javascript
Win11如何修改dns?Win11修改dns图文教程
2022/01/18 数码科技
python​格式化字符串
2022/04/20 Python