日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

VTK计算网格模型上的最短路径

發布時間:2024/4/13 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VTK计算网格模型上的最短路径 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  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 wrongif polydata.GetNumberOfPoints() == 0:raise ValueError("No point data could be loaded from " + filenameSTL)return Nonereturn 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 locationpicker = self.GetInteractor().GetPicker()picker.Pick(clickPos[0], clickPos[1], 0, self.GetDefaultRenderer())# If CellId = -1, nothing was pickedif(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 spheresphereSource = vtk.vtkSphereSource()sphereSource.SetCenter(point_position)#sphereSource.SetRadius(0.2)sphereSource.SetRadius(0.02)# Create a mapper and actorsphereMapper = 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 pathif len(pathList) > 1:dijkstra.SetStartVertex(pathList[-2])dijkstra.SetEndVertex(pathList[-1])dijkstra.Update()# Get the vertex ids (of the input polydata) on the shortest pathIdList = dijkstra.GetIdList()# store in pathListfor i in range(IdList.GetNumberOfIds()-1, 0, -1):pathList.insert(-1, IdList.GetId(i))self.drawPath()# Forward events self.OnRightButtonDown()returndef 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 itcells = vtk.vtkCellArray()cells.InsertNextCell(polyLine)# Create a polydata to store everything in polyLine = vtk.vtkPolyData()polyLine.SetPoints(points) # Add the points to the datasetpolyLine.SetLines(cells) # Add the lines to the dataset# Setup mapperpolyLineMapper = vtk.vtkPolyDataMapper()polyLineMapper.SetInputData(polyLine)# Create an actor to represent the polylinepolyLineActor = 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 rendererrenWin = vtk.vtkRenderWindow()# Set window sizerenWin.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 renderwindowinteractoriren = 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 meshmesh = loadSTL("untitled.stl")global dijkstraglobal pathListpathList = []dijkstra = vtk.vtkDijkstraGraphGeodesicPath()dijkstra.SetInputData(mesh)mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(mesh) # maps polygonal data to graphics primitivesactor = 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

?

?  立體網格:

?

?

?

參考:

Dijkstra算法(一)之 C語言詳解

Python/GeometricObjects/PolyLine

VTKExamples/Cxx/Graphs/ShortestPath

VTKExamples/Cxx/PolyData/DijkstraGraphGeodesicPath

VTK: vtkDijkstraGraphGeodesicPath Class Reference

vtkDijkstraGraphGeodesicPath在曲面上尋找最短路徑的應用

總結

以上是生活随笔為你收集整理的VTK计算网格模型上的最短路径的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。