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解析模块(ConfigParser)使用方法
Dec 10 Python
zbar解码二维码和条形码示例
Feb 07 Python
python采集博客中上传的QQ截图文件
Jul 18 Python
python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法
May 24 Python
python中利用xml.dom模块解析xml的方法教程
May 24 Python
Python实现的快速排序算法详解
Aug 01 Python
python入门前的第一课 python怎样入门
Mar 06 Python
如何用Python实现简单的Markdown转换器
Jul 16 Python
对Python正则匹配IP、Url、Mail的方法详解
Dec 25 Python
Python3实现统计单词表中每个字母出现频率的方法示例
Jan 28 Python
Python 存取npy格式数据实例
Jul 01 Python
用gpu训练好的神经网络,用tensorflow-cpu跑出错的原因及解决方案
Mar 03 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 HandlerSocket的使用
2011/05/02 PHP
php使用反射插入对象示例分享
2014/03/11 PHP
php图片水印添加、压缩、剪切的封装类实现
2020/04/18 PHP
PHP实现单例模式建立数据库连接的方法分析
2020/02/11 PHP
JS 页面内容搜索,类似于 Ctrl+F功能的实现代码
2007/08/13 Javascript
微博@符号的用户名提示效果。(想@到谁?)
2010/11/05 Javascript
JavaScript ECMA-262-3 深入解析.第三章.this
2011/09/28 Javascript
json的前台操作和后台操作实现代码
2012/01/20 Javascript
对frameset、frame、iframe的js操作示例代码
2013/08/16 Javascript
Node.js连接postgreSQL并进行数据操作
2016/12/18 Javascript
js中获取键盘按下键值event.keyCode、event.charCode和event.which的兼容性详解
2017/03/15 Javascript
vue页面切换到滚动页面显示顶部的实例
2018/03/13 Javascript
vue-cli项目中使用Mockjs详解
2018/05/14 Javascript
在vue中获取wangeditor的html和text的操作
2020/10/23 Javascript
[00:15]天涯墨客终极技能展示
2018/08/25 DOTA
玩转python爬虫之URLError异常处理
2016/02/17 Python
asyncio 的 coroutine对象 与 Future对象使用指南
2016/09/11 Python
python中import学习备忘笔记
2017/01/24 Python
Python 查看文件的编码格式方法
2017/12/21 Python
python中的单引号双引号区别知识点总结
2019/06/23 Python
Pytorch 中retain_graph的用法详解
2020/01/07 Python
python标准库os库的函数介绍
2020/02/12 Python
python数据处理——对pandas进行数据变频或插值实例
2020/04/22 Python
CSS3实现swap交换动画
2016/01/19 HTML / CSS
html5基础标签(html5视频标签 html5新标签用法)
2013/12/30 HTML / CSS
用HTML5制作数字时钟的教程
2015/05/11 HTML / CSS
html5 input输入实时检测以及延时优化
2018/07/18 HTML / CSS
德国传统玻璃制造商:Cristalica
2018/04/23 全球购物
婚礼主持词开场白
2014/03/13 职场文书
《大江保卫战》教学反思
2014/04/11 职场文书
三八节标语
2014/06/27 职场文书
支教个人总结
2015/03/04 职场文书
Python爬虫之用Xpath获取关键标签实现自动评论盖楼抽奖(二)
2021/06/07 Python
分享mysql的current_timestamp小坑及解决
2021/11/27 MySQL
SpringBoot 整合mongoDB并自定义连接池的示例代码
2022/02/28 MongoDB
日本官方排名前10的动漫,名侦探柯南上榜,第一是一部创造历史的动漫
2022/03/18 日漫