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函数式编程指南(一):函数式编程概述
Jun 24 Python
Python的Flask框架及Nginx实现静态文件访问限制功能
Jun 27 Python
Python常用算法学习基础教程
Apr 13 Python
apache部署python程序出现503错误的解决方法
Jul 24 Python
Python合并多个Excel数据的方法
Jul 16 Python
使用Python做定时任务及时了解互联网动态
May 15 Python
python3 自动识别usb连接状态,即对usb重连的判断方法
Jul 03 Python
python实现简易版学生成绩管理系统
Jun 22 Python
Python实现树莓派摄像头持续录像并传送到主机的步骤
Nov 30 Python
教你利用Selenium+python自动化来解决pip使用异常
May 20 Python
JAVA SpringMVC实现自定义拦截器
Mar 16 Python
全网非常详细的pytest配置文件
Jul 15 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 xml文件操作实现代码(二)
2009/03/20 PHP
php排序算法(冒泡排序,快速排序)
2012/10/09 PHP
php 读取文件头判断文件类型的实现代码
2013/08/05 PHP
win7安装php框架Yii的方法
2016/01/25 PHP
thinkphp3.x中session方法的用法分析
2016/05/20 PHP
PHP 序列化和反序列化函数实例详解
2020/07/18 PHP
PHP实现的redis主从数据库状态检测功能示例
2017/07/20 PHP
Javascript日期对象的dateAdd与dateDiff方法
2008/11/18 Javascript
jQuery性能优化28条建议你值得借鉴
2013/02/16 Javascript
jQuery异步加载数据并添加事件示例
2014/08/24 Javascript
理解Javascript文件动态加载
2016/01/29 Javascript
jQuery qrcode生成二维码的方法
2016/04/03 Javascript
微信小程序实战之上拉(分页加载)效果(2)
2017/04/17 Javascript
Angularjs在360兼容模式下取数据缓存问题的解决办法
2017/06/22 Javascript
基于js的变量提升和函数提升(详解)
2017/09/17 Javascript
jQuery实现表格隔行换色
2018/09/01 jQuery
vue.js+elementUI实现点击左右箭头切换头像功能(类似轮播图效果)
2019/09/05 Javascript
[16:04]DOTA2海涛带你玩炸弹 9月5日更新内容详解
2014/09/05 DOTA
使用python装饰器验证配置文件示例
2014/02/24 Python
Python学习笔记(二)基础语法
2014/06/06 Python
python实现图片变亮或者变暗的方法
2015/06/01 Python
Python编程中对super函数的正确理解和用法解析
2016/07/02 Python
Python 高级专用类方法的实例详解
2017/09/11 Python
Python Tkinter实现简易计算器功能
2018/01/30 Python
解决Mac安装scrapy失败的问题
2018/06/13 Python
python批量获取html内body内容的实例
2019/01/02 Python
pyqt5 实现工具栏文字图片同时显示
2019/06/13 Python
python3下pygame如何实现显示中文
2020/01/11 Python
无惧面试,带你搞懂python 装饰器
2020/08/17 Python
CSS3 icon font完全指南(CSS3 font 会取代icon图标)
2013/01/06 HTML / CSS
如何使用css3实现一个类在线直播的队列动画的示例代码
2020/06/17 HTML / CSS
Herschel Supply Co.美国:背包、手提袋及配件
2020/11/24 全球购物
学校光盘行动倡议书
2015/04/28 职场文书
2015年小学一年级班主任工作总结
2015/05/21 职场文书
详解CSS不受控制的position fixed
2021/05/25 HTML / CSS
世界无敌的ICOM IC-R9500宽频接收机
2022/03/25 无线电