python繪制立方體的方法
更新時間:2018年07月02日 10:45:35 作者:genispan
這篇文章主要為大家詳細介紹了python繪制立方體的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python繪制立方體的具體代碼,供大家參考,具體內(nèi)容如下
#!/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()
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
基于Python實現(xiàn)簡單的人臉識別系統(tǒng)
這篇文章主要介紹了如何通過Python實現(xiàn)一個簡單的人臉識別系統(tǒng),文中的示例代碼講解詳細,對我們學習Python有一定的幫助,感興趣的可以跟隨小編一起試一試2022-01-01Python開發(fā)網(wǎng)站目錄掃描器的實現(xiàn)
這篇文章主要介紹了Python開發(fā)網(wǎng)站目錄掃描器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-02-02Django中使用ModelForm生成HTML標簽的方法步驟
在 Django 中,使用 ModelForm 來生成 HTML 表單標簽是一種常見且高效的做法,本文主要介紹了Django中使用ModelForm生成HTML標簽的方法步驟,感興趣的可以了解一下2024-01-01Python 中pandas索引切片讀取數(shù)據(jù)缺失數(shù)據(jù)處理問題
pandas是一個Python軟件包,提供快速,靈活和富于表現(xiàn)力的數(shù)據(jù)結構,旨在使使用“關系”或“標記”數(shù)據(jù)既簡單又直觀。這篇文章主要介紹了pandas索引切片讀取數(shù)據(jù)缺失數(shù)據(jù)處理,需要的朋友可以參考下2019-10-10python中*args與**kwarsg及閉包和裝飾器的用法
這篇文章主要介紹了python中*args與**kwarsg及閉包和裝飾器的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07