本文实例为大家分享了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()
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月20日
2024年11月20日
- 柏菲·珞叔作品集《金色大厅2》限量开盘母带ORMCD[低速原抓WAV+CUE]
- Gareth.T《sad songs(Explicit)》[320K/MP3][29.03MB]
- Gareth.T《sad songs(Explicit)》[FLAC/分轨][152.85MB]
- 证声音乐图书馆《海风摇曳·盛夏爵士曲》[320K/MP3][63.06MB]
- 龚玥《金装龚玥HQCD》头版限量[WAV分轨]
- 李小春《吻别》萨克斯演奏经典[原抓WAV+CUE]
- 齐秦《辉煌30年24K珍藏版》2CD[WAV+CUE]
- 证声音乐图书馆《海风摇曳·盛夏爵士曲》[FLAC/分轨][321.47MB]
- 群星 《世界经典汽车音乐》 [WAV分轨][1G]
- 冷漠.2011 《冷漠的爱DSD》[WAV+CUE][1.2G]
- 陈明《流金岁月精逊【中唱】【WAV+CUE】
- 群星《Jazz-Ladies1-2爵士女伶1-2》HQCD/2CD[原抓WAV+CUE]
- 群星《美女私房歌》(黑胶)[WAV分轨]
- 郑源.2009《试音天碟》24BIT-96KHZ[WAV+CUE][1.2G]
- 飞利浦试音碟 《环球群星监听录》SACD香港版[WAV+CUE][1.1G]