python绘制立方体的方法

 更新时间:2018年07月02日 10:45:35   作者:genispan  
这篇文章主要为大家详细介绍了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()

效果图:

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

相关文章

  • Python命令行参数解析之argparse模块详解

    Python命令行参数解析之argparse模块详解

    这篇文章主要介绍了Python命令行参数解析之argparse模块详解,argparse 是 Python 的一个标准库,用于命令行参数的解析,这意味着我们无需在代码中手动为变量赋值,而是可以直接在命令行中向程序传递相应的参数,再由变量去读取这些参数,需要的朋友可以参考下
    2023-08-08
  • Python如何调用外部系统命令

    Python如何调用外部系统命令

    这篇文章主要介绍了Python如何调用外部系统命令,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Python实现中值滤波去噪方式

    Python实现中值滤波去噪方式

    今天小编就为大家分享一篇Python实现中值滤波去噪方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • Python编写条件分支代码方法

    Python编写条件分支代码方法

    这篇文章主要介绍了Python编写条件分支代码方法,编写条件分支代码是编码过程中不可或缺的一部分,更多详细介绍需要的小伙伴可以参考下面文章内容
    2022-05-05
  • pandas多层索引的创建和取值以及排序的实现

    pandas多层索引的创建和取值以及排序的实现

    这篇文章主要介绍了pandas多层索引的创建和取值以及排序的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 浅谈python 里面的单下划线与双下划线的区别

    浅谈python 里面的单下划线与双下划线的区别

    这篇文章主要介绍了浅谈python 里面的单下划线与双下划线的区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • Python基础之字典的详细使用教程

    Python基础之字典的详细使用教程

    字典作为Python的一个内置数据结构,和列表一样都是可变序列的,但是它是无序的,以键值对的方式存储数据。本文将详解一下Python中字典的使用,需要的可以参考一下
    2022-07-07
  • 亲手教你用Python打造一款摸鱼倒计时界面

    亲手教你用Python打造一款摸鱼倒计时界面

    前段时间在微博看到一段摸鱼人的倒计时模板,感觉很有意思,于是我用了一个小时的时间写一个页面出来,下面小编把实现过程分享给大家,对Python摸鱼倒计时界面感兴趣的朋友一起看看吧
    2021-12-12
  • python中的列表和元组实例详解

    python中的列表和元组实例详解

    在Python中序列是最基本的数据结构,它是一块用于存放多个值得连续内存空间,Python中内置了5个常用的序列结构,分别是列表、元组、集合、字典和字符串,本文将详细讲解python中的列表和元组,感兴趣的朋友跟随小编一起看看吧
    2022-11-11
  • Python中easy_install 和 pip 的安装及使用

    Python中easy_install 和 pip 的安装及使用

    本篇文章主要介绍了Python中easy_install 和 pip 的安装及使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06

最新评论