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如何处理matlab的mat数据

    python如何处理matlab的mat数据

    这篇文章主要介绍了python如何处理matlab的mat数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Python 如何创建一个简单的REST接口

    Python 如何创建一个简单的REST接口

    这篇文章主要介绍了Python 如何创建一个简单的REST接口,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • 使用Python来编写HTTP服务器的超级指南

    使用Python来编写HTTP服务器的超级指南

    这篇文章主要介绍了使用Python来编写HTTP服务器的超级指南,同时介绍了基于Python框架的web服务器的编写方法,译文从理论到实现讲得都很生动详细,十分推荐!需要的朋友可以参考下
    2016-02-02
  • 使用Python读取二进制文件的实例讲解

    使用Python读取二进制文件的实例讲解

    今天小编就为大家分享一篇使用Python读取二进制文件的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • 强悍的Python读取大文件的解决方案

    强悍的Python读取大文件的解决方案

    今天小编就为大家分享一篇关于强悍的Python读取大文件的解决方案,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • Python定时任务APScheduler的实例实例详解

    Python定时任务APScheduler的实例实例详解

    APScheduler 支持三种调度任务:固定时间间隔,固定时间点(日期),Linux 下的 Crontab 命令。这篇文章主要介绍了Python定时任务APScheduler的使用,需要的朋友可以参考下
    2019-07-07
  • 你眼中的Python大牛 应该都有这份书单

    你眼中的Python大牛 应该都有这份书单

    现在学习Python途径很多,但是想系统的学习Python的知识体系,还需要靠阅读专业的书籍来不断积累。你眼中的Python大牛,应该都看过这些书,赶快收藏一份
    2017-10-10
  • Python实现识别手写数字 Python图片读入与处理

    Python实现识别手写数字 Python图片读入与处理

    这篇文章主要为大家详细介绍了Python实现识别手写数字,Python图片的读入与处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • python爬虫之异常捕获及标签过滤详解

    python爬虫之异常捕获及标签过滤详解

    今天带大家了解python异常捕获及标签过滤,文中有非常详细的代码示例,对正在学习python爬虫的小伙伴们很有帮助,需要的朋友可以参考下
    2021-05-05
  • Windows系统下使用flup搭建Nginx和Python环境的方法

    Windows系统下使用flup搭建Nginx和Python环境的方法

    这篇文章主要介绍了Windows系统下使用flup搭建Nginx和Python环境的方法,文中使用到了flup这个Python的FastCGI工具,需要的朋友可以参考下
    2015-12-12

最新评论