python绘制立方体的方法
作者:genispan 时间:2022-09-26 10:34:14
本文实例为大家分享了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()
效果图:
来源:https://blog.csdn.net/genispan/article/details/79583193
标签:python,立方体
0
投稿
猜你喜欢
MySQL 删除大表的性能问题解决方案
2024-01-18 22:20:31
MySQL数据库分组查询group by语句详解
2024-01-28 05:50:31
pandas dataframe drop函数介绍
2023-07-11 17:19:17
一文教你利用Python制作一个生日提醒
2022-12-12 09:20:43
php实现XSS安全过滤的方法
2024-05-05 09:16:15
JS在IE和FireFox之间常用函数的区别小结
2024-04-16 09:54:54
关于python3的ThreadPoolExecutor线程池大小设置
2023-12-05 13:37:57
Python实现获取照片的地理定位信息
2023-06-30 01:39:54
Python语法概念基础详解
2023-07-07 01:36:24
python threading模块操作多线程介绍
2021-03-16 20:02:13
用javascript给表格加滚动条
2008-10-15 10:03:00
10分钟搞定让你困惑的 Jenkins 环境变量过程详解
2023-09-23 13:56:03
Python序列对象与String类型内置方法详解
2023-09-22 13:25:18
他们是如何不让我的Teleport和Webzip工作的?
2010-07-14 21:06:00
使用bootstrap-paginator.js 分页来进行ajax 异步分页请求示例
2024-05-21 10:13:15
利用WSH获取计算机硬件信息、DNS信息等
2008-05-05 13:04:00
你还在 Select * 吗?
2024-01-24 22:10:50
centos7.3 安装mysql5.7.18的详细教程
2024-01-12 18:57:00
VUE+elementui面包屑实现动态路由详解
2024-05-02 17:11:47
MySQL的一些安全注意点
2008-12-24 16:29:00