博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VTK计算网格模型上的最短路径
阅读量:6438 次
发布时间:2019-06-23

本文共 6271 字,大约阅读时间需要 20 分钟。

  Dijkstra algorithm to compute the graph geodesic.Takes as input a polygonal mesh and performs a single source shortest path calculation. Dijkstra's algorithm is used. 

  用鼠标右键拾取平面网格上的点,观察输出路径:

#!usrbinenv pythonimport vtkdef loadSTL(filenameSTL):    readerSTL = vtk.vtkSTLReader()    readerSTL.SetFileName(filenameSTL)    # 'update' the reader i.e. read the .stl file    readerSTL.Update()    polydata = readerSTL.GetOutput()        print "Number of Cells:",  polydata.GetNumberOfCells()    print "Number of Points:", polydata.GetNumberOfPoints()        # If there are no points in 'vtkPolyData' something went wrong    if polydata.GetNumberOfPoints() == 0:        raise ValueError("No point data could be loaded from " + filenameSTL)        return None            return polydata    # Customize vtkInteractorStyleTrackballCamera class MyInteractor(vtk.vtkInteractorStyleTrackballCamera):    def __init__(self,parent=None):        self.AddObserver("RightButtonPressEvent", self.RightButtonPressEvent)            def RightButtonPressEvent(self,obj,event):        clickPos = self.GetInteractor().GetEventPosition()        print "Picking pixel: " , clickPos                # Pick from this location        picker = self.GetInteractor().GetPicker()        picker.Pick(clickPos[0], clickPos[1], 0, self.GetDefaultRenderer())                # If CellId = -1, nothing was picked        if(picker.GetCellId() != -1):             print "Pick position is: " , picker.GetPickPosition()            print "Cell id is:",  picker.GetCellId()            print "Point id is:", picker.GetPointId()                        pathList.append(picker.GetPointId())            point_position = mesh.GetPoint(picker.GetPointId())                        # Create a sphere            sphereSource = vtk.vtkSphereSource()            sphereSource.SetCenter(point_position)            #sphereSource.SetRadius(0.2)            sphereSource.SetRadius(0.02)                        # Create a mapper and actor            sphereMapper = vtk.vtkPolyDataMapper()            sphereMapper.SetInputConnection(sphereSource.GetOutputPort())                        sphereActor = vtk.vtkActor()            sphereActor.SetMapper(sphereMapper)            sphereActor.GetProperty().SetColor(1.0, 0.0, 0.0)                self.GetDefaultRenderer().AddActor(sphereActor)                        # find the shortest path            if len(pathList) > 1:                dijkstra.SetStartVertex(pathList[-2])                dijkstra.SetEndVertex(pathList[-1])                dijkstra.Update()                                # Get the vertex ids (of the input polydata) on the shortest path                IdList = dijkstra.GetIdList()                            # store in pathList                for i in range(IdList.GetNumberOfIds()-1, 0, -1):                    pathList.insert(-1, IdList.GetId(i))                                    self.drawPath()        # Forward events        self.OnRightButtonDown()        return            def drawPath(self):        points = vtk.vtkPoints()        for i in range(0, len(pathList)):            points.InsertNextPoint(mesh.GetPoint(pathList[i]))         # draw intermediate points            # pointsPolydata = vtk.vtkPolyData()        # pointsPolydata.SetPoints(points)                              # vertexFilter  = vtk.vtkVertexGlyphFilter()        # vertexFilter.SetInputData(pointsPolydata)        # vertexFilter.Update()        # polydata = vtk.vtkPolyData()        # polydata.ShallowCopy(vertexFilter.GetOutput())                # mapper = vtk.vtkPolyDataMapper()        # mapper.SetInputData(polydata)        # polydataActor = vtk.vtkActor()        # polydataActor.SetMapper(mapper)        # polydataActor.GetProperty().SetPointSize(5)                # self.GetDefaultRenderer().AddActor(polydataActor)                        # draw path         polyLine = vtk.vtkPolyLine()        polyLine.GetPointIds().SetNumberOfIds(len(pathList))        for i in range(0, len(pathList)):            polyLine.GetPointIds().SetId(i,i)        #Create a cell array to store the lines in and add the lines to it        cells = vtk.vtkCellArray()        cells.InsertNextCell(polyLine)                # Create a polydata to store everything in            polyLine = vtk.vtkPolyData()        polyLine.SetPoints(points) # Add the points to the dataset        polyLine.SetLines(cells)   # Add the lines to the dataset                # Setup mapper        polyLineMapper = vtk.vtkPolyDataMapper()        polyLineMapper.SetInputData(polyLine)                # Create an actor to represent the polyline        polyLineActor = vtk.vtkActor()        polyLineActor.SetMapper(polyLineMapper)        polyLineActor.GetProperty().SetColor(0,0,1)        polyLineActor.GetProperty().SetLineWidth(2)                self.GetDefaultRenderer().AddActor(polyLineActor)                def CreateScene():    # Create a rendering window and renderer    renWin = vtk.vtkRenderWindow()    # Set window size    renWin.SetSize(600, 600)    ren = vtk.vtkRenderer()    # Set background color    ren.GradientBackgroundOn()    ren.SetBackground(.1, .1, .1)    ren.SetBackground2(0.8,0.8,0.8)        renWin.AddRenderer(ren)         # Create a renderwindowinteractor    iren = vtk.vtkRenderWindowInteractor()    iren.SetRenderWindow(renWin)        style = MyInteractor()    style.SetDefaultRenderer(ren)    iren.SetInteractorStyle(style)        # vtkCellPicker will shoot a ray into a 3D scene and return information about    # the first object that the ray hits.    cellPicker = vtk.vtkCellPicker()      iren.SetPicker(cellPicker)        # load STL file     global mesh    mesh = loadSTL("untitled.stl")        global dijkstra    global pathList    pathList = []    dijkstra = vtk.vtkDijkstraGraphGeodesicPath()    dijkstra.SetInputData(mesh)                mapper = vtk.vtkPolyDataMapper()     mapper.SetInputData(mesh)         # maps polygonal data to graphics primitives    actor = vtk.vtkLODActor()     actor.SetMapper(mapper)    actor.GetProperty().EdgeVisibilityOn()    actor.GetProperty().SetColor(0.0,0.9,0.9)    actor.GetProperty().SetLineWidth(0.3)    ren.AddActor(actor)    # Enable user interface interactor    iren.Initialize()    iren.Start()    if __name__ == "__main__":    CreateScene()
View Code

 

   立体网格:

 

 

 

参考:

转载地址:http://uhzwo.baihongyu.com/

你可能感兴趣的文章
熟悉常用的HDFS操作
查看>>
列表导航栏实例(04)——精美模板赏析
查看>>
Redis安装文档
查看>>
以ed结尾的单词
查看>>
Subversion的权限控制
查看>>
ScrollView嵌套ListView后,进入页面不从顶部开始显示的问题解决
查看>>
快速排序
查看>>
Linux x64 下 Matlab R2013a 300 kb 脚本文件调试的 CPU 占用过高问题的解决办法
查看>>
流式套接字(SOCK_STREAM),数据报套接字 (SOCK_DGRAM) 的比较
查看>>
HTML5 — 让拖放变的流行起来
查看>>
(转载)java list排序
查看>>
有效的字母异位词---简单
查看>>
第五周作业
查看>>
二、创建进程和线程
查看>>
linux 高性能服务排查方式
查看>>
OpenSSL 命令说明
查看>>
Bzoj4561 [JLoi2016]圆的异或并
查看>>
软工实践练习一——使用Git进行代码管理心得
查看>>
技巧:在Silverlight 2应用程序中切换用户控件
查看>>
2015年得规划
查看>>