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 元组(Tuple)操作详解
Mar 11 Python
python实现查询苹果手机维修进度
Mar 16 Python
python实现自动更换ip的方法
May 05 Python
python中list列表的高级函数
May 17 Python
python快速建立超简单的web服务器的实现方法
Feb 17 Python
Python实现的FTP通信客户端与服务器端功能示例
Mar 28 Python
Python 70行代码实现简单算式计算器解析
Aug 30 Python
Python创建一个元素都为0的列表实例
Nov 28 Python
Python魔法方法 容器部方法详解
Jan 02 Python
PyCharm第一次安装及使用教程
Jan 08 Python
Python中url标签使用知识点总结
Jan 16 Python
Python 统计序列中元素的出现频度
Apr 26 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 Cookie的一个使用注意点
2008/11/08 PHP
php设计模式 Bridge (桥接模式)
2011/06/26 PHP
PHP实现使用优酷土豆视频地址获取swf播放器分享地址
2014/06/05 PHP
护卫神php套件 php版本升级方法(php5.5.24)
2015/05/10 PHP
实现WordPress主题侧边栏切换功能的PHP脚本详解
2015/12/14 PHP
PHP实现支持加盐的图片加密解密
2016/09/09 PHP
php版银联支付接口开发简明教程
2016/10/14 PHP
tp5框架基于ajax实现异步删除图片的方法示例
2020/02/10 PHP
jQuery DOM操作小结与实例
2010/01/07 Javascript
javascript event 事件解析
2011/01/31 Javascript
jquery中$.post()方法的简单实例
2014/02/04 Javascript
实现网页页面跳转的几种方法(meta标签、js实现、php实现)
2014/05/20 Javascript
用jquery的方法制作一个简单的导航栏
2014/06/23 Javascript
调试JavaScript中正则表达式中遇到的问题
2015/01/27 Javascript
JavaScript中setUTCFullYear()方法的使用简介
2015/06/12 Javascript
JavaScript中Math.SQRT2属性的使用详解
2015/06/14 Javascript
基于jQuery+PHP+Mysql实现在线拍照和在线浏览照片
2015/09/06 Javascript
jquery+css3实现会动的小圆圈效果
2016/01/27 Javascript
Listloading.js移动端上拉下拉刷新组件
2016/08/04 Javascript
微信js-sdk上传与下载图片接口用法示例
2016/10/12 Javascript
JavaScript触发onScroll事件的函数节流详解
2016/12/14 Javascript
vue如何实现observer和watcher源码解析
2017/03/09 Javascript
解决layui的form里的元素进行动态生成,验证失效的问题
2019/09/14 Javascript
JS检测浏览器开发者工具是否打开的方法详解
2020/10/02 Javascript
jquery实现拖拽小方块效果
2020/12/10 jQuery
深入讲解Python中的迭代器和生成器
2015/10/26 Python
Python+Opencv实现把图片、视频互转的示例
2020/12/17 Python
html5适合移动应用开发的12大特性
2014/03/19 HTML / CSS
Kneipp克奈圃美国官网:德国百年精油配方的传承
2018/02/07 全球购物
财务会计大学生自我评价
2014/04/09 职场文书
好的促销活动方案
2014/08/21 职场文书
会议接待欢迎词范文
2015/01/26 职场文书
材料采购员岗位职责
2015/04/03 职场文书
2016高校自主招生自荐信范文
2016/01/28 职场文书
Python使用sql语句对mysql数据库多条件模糊查询的思路详解
2021/04/12 Python
MYSQL 表的全面总结
2021/11/11 MySQL