python绘制立方体的方法

本文实例为大家分享了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()

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Python3使用turtle绘制超立方体图形示例

    本文实例讲述了Python3使用turtle绘制超立方体图形.分享给大家供大家参考,具体如下: 利用Python3中turtle的绘制超立方体. 绘图思路: 1)求出边长100的超立方体的点坐标: 以竖直线为依据,将点分为上下两组: a为上边点列表,b为下边点列表: a = [[120.71, 50], [50, 120.71], [-50, 120.71], [-120.71, 50], [-50, -20.71], [50, -20.71], [20.71, 50],[-20.71, 50]

  • python使用turtle库绘制时钟

    Python函数库众多,而且在不断更新,所以学习这些函数库最有效的方法,就是阅读Python官方文档.同时借助Google和百度. 本文介绍的turtle库对应的官方文档地址 绘制动态钟表的基本思路如下(面向对象的编程): 使用5个turtle对象 1个turtle:绘制外表盘 3个turtle:模拟表针行为 1个turtle:输出表盘上文字 根据实时时间使用ontimer()函数更新表盘画面,显示效果如下: 相关函数的使用在程序中进行了详细的注释,代码如下: # -*- coding: utf

  • Python根据已知邻接矩阵绘制无向图操作示例

    本文实例讲述了Python根据已知邻接矩阵绘制无向图操作.分享给大家供大家参考,具体如下: 有六个点:[0,1,2,3,4,5,6],六个点之间的邻接矩阵如表格所示,根据邻接矩阵绘制出相对应的图 0 1 2 3 4 5 6 0 0 1 0 1 0 1 0 1 1 0 1 1 1 1 1 2 0 1 0 1 0 1 0 3 1 1 1 0 1 1 1 4 0 1 0 1 1 1 1 5 1 1 1 1 1 0 0 6 0 1 0 1 1 0 0 将点之间的联系构造成如下矩阵 N = [[0, 3,

  • python使用turtle库绘制树

    本文实例为大家分享了python使用turtle库绘制树的具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. """ import turtle, datetime def drawGap(): #绘制数码管间隔 turtle.penup() turtle.fd(5) def drawLine(draw): #绘制

  • python绘制直线的方法

    本文实例为大家分享了python绘制直线的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python import vtk # 绘制通用方法 def myshow(linepolydata): # Now we'll look at it. lineMapper = vtk.vtkPolyDataMapper() if vtk.VTK_MAJOR_VERSION <= 5: lineMapper.SetInput(linepolydata) else: lineMapper.

  • Python实现的绘制三维双螺旋线图形功能示例

    本文实例讲述了Python实现的绘制三维双螺旋线图形功能.分享给大家供大家参考,具体如下: 代码: # -*- coding:utf-8 -*- #! python3 #绘制三维双螺旋线 import numpy as np import matplotlib.pyplot as plt import mpl_toolkits.mplot3d t=list(range(100,200)) r=[i*np.cos(60+i*360*5) for i in t] theta=[i*np.sin(60

  • 基于python绘制科赫雪花

    什么是科赫曲线 科赫曲线是de Rham曲线的特例.给定线段AB,科赫曲线可以由以下步骤生成: 将线段分成三等份(AC,CD,DB) 以CD为底,向外(内外随意)画一个等边三角形DMC 将线段CD移去 分别对AC,CM,MD,DB重复1~3. 什么是科赫雪花 三段科赫曲线组成的图形 实现的效果 < #KocheDraw1 import turtle def koch(size,n): if n==1: turtle.fd(size) else: for i in [0,60,-120,60]:

  • Python实现绘制双柱状图并显示数值功能示例

    本文实例讲述了Python实现绘制双柱状图并显示数值功能.分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #! python3 import matplotlib.pyplot as plt import mpl_toolkits.mplot3d #定义函数来显示柱状上的数值 def autolabel(rects): for rect in rects: height = rect.get_height() plt.text(rect.get_x()+rect.

  • python绘制圆柱体的方法

    本文实例为大家分享了python绘制圆柱体示的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python import vtk # 参考的C++版本源码及解释 感谢原作者 # http://blog.csdn.net/www_doling_net/article/details/8536376 def main(): cylinder = vtk.vtkCylinderSource() cylinder.SetHeight(3.0) # 设置柱体的高 cylinder.Set

  • python使用turtle绘制分形树

    由于分形树具有对称性,自相似性,所以我们可以用递归来完成绘制.只要确定开始树枝长.每层树枝的减短长度和树枝分叉的角度,我们就可以把分形树画出来啦!! 代码如下: # -*- coding: utf-8 -*- ''' 绘制分形树 ''' import turtle as tl def draw_smalltree(tree_length,tree_angle): ''' 绘制分形树函数 ''' if tree_length >= 3: tl.forward(tree_length) #往前画 t

随机推荐