python繪制立方體的方法
本文實(shí)例為大家分享了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個(gè)三維值代表長(zhǎng)方體的8個(gè)頂點(diǎn) 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 # 點(diǎn)的編號(hào)0-7,每個(gè)面由4個(gè)點(diǎn)組成 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()
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python如何處理matlab的mat數(shù)據(jù)
這篇文章主要介紹了python如何處理matlab的mat數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Python 如何創(chuàng)建一個(gè)簡(jiǎn)單的REST接口
這篇文章主要介紹了Python 如何創(chuàng)建一個(gè)簡(jiǎn)單的REST接口,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
使用Python來(lái)編寫(xiě)HTTP服務(wù)器的超級(jí)指南
這篇文章主要介紹了使用Python來(lái)編寫(xiě)HTTP服務(wù)器的超級(jí)指南,同時(shí)介紹了基于Python框架的web服務(wù)器的編寫(xiě)方法,譯文從理論到實(shí)現(xiàn)講得都很生動(dòng)詳細(xì),十分推薦!需要的朋友可以參考下2016-02-02
使用Python讀取二進(jìn)制文件的實(shí)例講解
今天小編就為大家分享一篇使用Python讀取二進(jìn)制文件的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python定時(shí)任務(wù)APScheduler的實(shí)例實(shí)例詳解
APScheduler 支持三種調(diào)度任務(wù):固定時(shí)間間隔,固定時(shí)間點(diǎn)(日期),Linux 下的 Crontab 命令。這篇文章主要介紹了Python定時(shí)任務(wù)APScheduler的使用,需要的朋友可以參考下2019-07-07
你眼中的Python大牛 應(yīng)該都有這份書(shū)單
現(xiàn)在學(xué)習(xí)Python途徑很多,但是想系統(tǒng)的學(xué)習(xí)Python的知識(shí)體系,還需要靠閱讀專(zhuān)業(yè)的書(shū)籍來(lái)不斷積累。你眼中的Python大牛,應(yīng)該都看過(guò)這些書(shū),趕快收藏一份2017-10-10
Python實(shí)現(xiàn)識(shí)別手寫(xiě)數(shù)字 Python圖片讀入與處理
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)識(shí)別手寫(xiě)數(shù)字,Python圖片的讀入與處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
python爬蟲(chóng)之異常捕獲及標(biāo)簽過(guò)濾詳解
今天帶大家了解python異常捕獲及標(biāo)簽過(guò)濾,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python爬蟲(chóng)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Windows系統(tǒng)下使用flup搭建Nginx和Python環(huán)境的方法
這篇文章主要介紹了Windows系統(tǒng)下使用flup搭建Nginx和Python環(huán)境的方法,文中使用到了flup這個(gè)Python的FastCGI工具,需要的朋友可以參考下2015-12-12

