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 python

import vtk

def 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()

  立体网格:

参考:

Dijkstra算法(一)之 C语言详解

VTKExamples/Cxx/Graphs/ShortestPath

VTKExamples/Cxx/PolyData/DijkstraGraphGeodesicPath

VTK: vtkDijkstraGraphGeodesicPath Class Reference

vtkDijkstraGraphGeodesicPath在曲面上寻找最短路径的应用

VTK计算网格模型上的最短路径的更多相关文章

  1. VTK拾取网格模型上的可见点

    消隐与Z-Buffer 使用缓冲器记录物体表面在屏幕上投影所覆盖范围内的全部像素的深度值,依次访问屏幕范围内物体表面所覆盖的每一像素,用深度小(深度用z值表示,z值小表示离视点近)的像素点颜色替代深度 ...

  2. pcl曲面网格模型的三种显示方式

    pcl网格模型有三种可选的显示模式,分别是面片模式(surface)显示,线框图模式(wireframe)显示,点模式(point)显示.默认为面片模式进行显示.设置函数分别为: void pcl:: ...

  3. 【小白的CFD之旅】19 来自计算网格的困惑

    经过一年的忙碌,终于又到了寒假时间,小白又满状态复活了. 这一年小白学了很多的课程,但是一年下来,小白却感觉脑袋里没留下什么东西,貌似什么东西都在考完试的那一刹那全还回给老师了.这一年学习之余,小白仍 ...

  4. CSS3盒子模型(上)

    CSS的盒子模型分为三个大模块: 盒子模型 . 浮动 . 定位,其余的都是细节.要求这三部分,只要是学前端的无论如何也要学的非常精通. 所谓盒子模型就是把HTML页面中的元素看作是一个矩形的盒子,也就 ...

  5. Linux内核(7) - 设备模型(上)

    对于驱动开发来说,设备模型的理解是根本,毫不夸张得说,理解了设备模型,再去看那些五花八门的驱动程序,你会发现自己站在了另一个高度,从而有了一种俯视的感觉,就像凤姐俯视知音和故事会,韩峰同志俯视女下属. ...

  6. 在skyline中将井盖、雨水箅子等部件放到地面模型上

    公司三维建模组遇到这样的一个问题,怎样将井盖.雨水盖子恰好放在做好的地面模型上.传统的方法是在skyline中逐个调整井盖的对地高度,就是调整为恰好能放在地面上.或者选择很粗糙的一个方法,在“高度”属 ...

  7. 不同材质怎么通过ZBrush赋予同一个模型上

    ZBrush 作为最专业的数字雕刻与绘画软件,能够制作出高质量的3D模型,包括模型的颜色贴图和材质属性.不同材质可以改变照明在表面上的反应,以便模型表现出光泽.凹凸.反射.金属性或透明效果.ZBrus ...

  8. 使用k-means对3D网格模型进行分割

    使用k-means对3D网格模型进行分割 由于一些原因,最近在做网格分割的相关工作.网格分割的方法有很多,如Easy mesh cutting.K-means.谱分割.基于SDF的分割等.根据对分割要 ...

  9. SciPy - 科学计算库(上)

    SciPy - 科学计算库(上) 一.实验说明 SciPy 库建立在 Numpy 库之上,提供了大量科学算法,主要包括这些主题: 特殊函数 (scipy.special) 积分 (scipy.inte ...

随机推荐

  1. 扩展方法 C#

    “扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.” 定义和调用扩展方法 定义一个静态类以包含扩展方法. 该类必须对客户端代码可见. 有关可访问性规则 ...

  2. javafx实现模态/模式窗口

    import javafx.stage.*; import javafx.scene.*; import javafx.scene.paint.Color; import javafx.scene.c ...

  3. 【C++ Primer | 19】运行类型识别

    运行类型识别 一.使用RTTI dynamic_cast运算符的调用形式如下所示: dynamic_cast<type*>(e) //e是指针 dynamic_cast<type&a ...

  4. Linux下apache支持PHP配置

    https://www.cnblogs.com/qiuxiao/p/6815350.html https://www.cnblogs.com/polestar/p/6086552.html

  5. MockMvc 对 Spring Boot 进行单元测试

    import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.ann ...

  6. Python os.path.basename

    一.获取对应路径下文件的名字 >>> os.path.basename("/etc/sysconfig/selinux") 'selinux' >>& ...

  7. TotoriseGit安装

    1.前提 前提是有安装过git. 2.下载 3.安装 二:参考的文档 1.不错的文档 https://www.cnblogs.com/xinlj/p/5978730.html http://blog. ...

  8. POJ 3279 Fliptile (二进制枚举)

    <题目链接> <转载于 >>> > 题目大意: 给定一个M*N矩阵,有些是黑色(1表示)否则白色(0表示),每翻转一个(i,j),会使得它和它周围4个格变为另 ...

  9. python 中的可变对象与不可变对象

    近日辞职待工,没有实际的项目与大家分享.暂写写在实际运用python中遇到的关于可变对象和不可变对象的坑. 首先我们需要明确一个概念,在python中一且皆对象.我们一般定义一个变量a=0,其实质a是 ...

  10. 推荐一个spring cloud 学习路线,绝对合理化

    最近没有时间所有没用给大家更新spring cloud 系列学习,在这先给大家奉献上我学习spring cloud 的路线 当然第一步先学习springboot然后: spring cloud eur ...