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获得两个数组交集、并集、差集的方法
Mar 27 Python
浅谈python中的变量默认是什么类型
Sep 11 Python
python实现批量按比例缩放图片效果
Mar 30 Python
Python 实现数据结构-堆栈和队列的操作方法
Jul 17 Python
django 捕获异常和日志系统过程详解
Jul 18 Python
python进阶之自定义可迭代的类
Aug 20 Python
Win系统PyQt5安装和使用教程
Dec 25 Python
pytorch之添加BN的实现
Jan 06 Python
python 已知一个字符,在一个list中找出近似值或相似值实现模糊匹配
Feb 29 Python
Django 解决开发自定义抛出异常的问题
May 21 Python
用Python实现童年贪吃蛇小游戏功能的实例代码
Dec 07 Python
pycharm debug 断点调试心得分享
Apr 16 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实现文件上传操作和封装
2020/03/04 PHP
Extjs ajax同步请求时post方式参数发送方式
2009/08/05 Javascript
一些技巧性实用js代码小结
2009/10/14 Javascript
关于JavaScript中var声明变量作用域的推断
2010/12/16 Javascript
JS中window.open全屏命令解析及使用示例
2013/12/11 Javascript
table对象中的insertRow与deleteRow使用示例
2014/01/26 Javascript
控制台报错object is not a function的解决方法
2014/08/24 Javascript
Javascript添加监听与删除监听用法详解
2014/12/19 Javascript
简介AngularJS的视图功能应用
2015/06/17 Javascript
IE中document.createElement的iframe无法设置属性name的解决方法
2015/09/14 Javascript
JS实现选项卡实例详解
2015/11/17 Javascript
JS实现部分HTML固定页面顶部随屏滚动效果
2015/12/24 Javascript
Bootstrap布局之栅格系统详解
2016/06/13 Javascript
node.js中express中间件body-parser的介绍与用法详解
2017/05/23 Javascript
ES6与CommonJS中的模块处理的区别
2018/06/13 Javascript
Javascript Worker子线程代码实例
2020/02/20 Javascript
uniapp实现可滑动选项卡
2020/10/21 Javascript
小程序实现点击tab切换左右滑动
2020/11/16 Javascript
Python正则表达式匹配ip地址实例
2014/10/09 Python
python with提前退出遇到的坑与解决方案
2018/01/05 Python
使用Python抓取豆瓣影评数据的方法
2018/10/17 Python
python 重命名轴索引的方法
2018/11/10 Python
Python操作json的方法实例分析
2018/12/06 Python
python3 小数位的四舍五入(用两种方法解决round 遇5不进)
2019/04/11 Python
Python OpenCV利用笔记本摄像头实现人脸检测
2020/08/20 Python
python中的协程深入理解
2019/06/10 Python
详解Django 时间与时区设置问题
2019/07/23 Python
python 实现检验33品种数据是否是正态分布
2019/12/09 Python
python实现磁盘日志清理的示例
2020/11/05 Python
Html5 webview元素定位工具的实现
2020/08/07 HTML / CSS
英国建筑用品在线:Building Supplies Online(BSO)
2018/04/30 全球购物
2014年文秘工作总结
2014/11/25 职场文书
主持人开幕词
2015/01/29 职场文书
甲午大海战观后感
2015/06/02 职场文书
莫言诺贝尔获奖感言(全文)
2015/07/31 职场文书
SQLServer 错误: 15404,无法获取有关 Windows NT 组/用户 WIN-8IVSNAQS8T7\Administrator 的信息
2021/06/30 SQL Server