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 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()
立体网格:
参考:
VTKExamples/Cxx/Graphs/ShortestPath
VTKExamples/Cxx/PolyData/DijkstraGraphGeodesicPath
VTK: vtkDijkstraGraphGeodesicPath Class Reference
vtkDijkstraGraphGeodesicPath在曲面上寻找最短路径的应用
VTK计算网格模型上的最短路径的更多相关文章
- VTK拾取网格模型上的可见点
消隐与Z-Buffer 使用缓冲器记录物体表面在屏幕上投影所覆盖范围内的全部像素的深度值,依次访问屏幕范围内物体表面所覆盖的每一像素,用深度小(深度用z值表示,z值小表示离视点近)的像素点颜色替代深度 ...
- pcl曲面网格模型的三种显示方式
pcl网格模型有三种可选的显示模式,分别是面片模式(surface)显示,线框图模式(wireframe)显示,点模式(point)显示.默认为面片模式进行显示.设置函数分别为: void pcl:: ...
- 【小白的CFD之旅】19 来自计算网格的困惑
经过一年的忙碌,终于又到了寒假时间,小白又满状态复活了. 这一年小白学了很多的课程,但是一年下来,小白却感觉脑袋里没留下什么东西,貌似什么东西都在考完试的那一刹那全还回给老师了.这一年学习之余,小白仍 ...
- CSS3盒子模型(上)
CSS的盒子模型分为三个大模块: 盒子模型 . 浮动 . 定位,其余的都是细节.要求这三部分,只要是学前端的无论如何也要学的非常精通. 所谓盒子模型就是把HTML页面中的元素看作是一个矩形的盒子,也就 ...
- Linux内核(7) - 设备模型(上)
对于驱动开发来说,设备模型的理解是根本,毫不夸张得说,理解了设备模型,再去看那些五花八门的驱动程序,你会发现自己站在了另一个高度,从而有了一种俯视的感觉,就像凤姐俯视知音和故事会,韩峰同志俯视女下属. ...
- 在skyline中将井盖、雨水箅子等部件放到地面模型上
公司三维建模组遇到这样的一个问题,怎样将井盖.雨水盖子恰好放在做好的地面模型上.传统的方法是在skyline中逐个调整井盖的对地高度,就是调整为恰好能放在地面上.或者选择很粗糙的一个方法,在“高度”属 ...
- 不同材质怎么通过ZBrush赋予同一个模型上
ZBrush 作为最专业的数字雕刻与绘画软件,能够制作出高质量的3D模型,包括模型的颜色贴图和材质属性.不同材质可以改变照明在表面上的反应,以便模型表现出光泽.凹凸.反射.金属性或透明效果.ZBrus ...
- 使用k-means对3D网格模型进行分割
使用k-means对3D网格模型进行分割 由于一些原因,最近在做网格分割的相关工作.网格分割的方法有很多,如Easy mesh cutting.K-means.谱分割.基于SDF的分割等.根据对分割要 ...
- SciPy - 科学计算库(上)
SciPy - 科学计算库(上) 一.实验说明 SciPy 库建立在 Numpy 库之上,提供了大量科学算法,主要包括这些主题: 特殊函数 (scipy.special) 积分 (scipy.inte ...
随机推荐
- Java jvm 内存参数限制
nohup java -jar -Xms3g -Xmx3g jenkins.war > jenkins.log 2>&1 &
- JVM GC-----2、垃圾标记算法(一)
在上一篇文章中,我介绍了关于GC机制中,GC在确认垃圾对象后,是如何回收这些垃圾对象的几种算法.现在介绍下GC机制一般是如何定位(或者叫做标记)出这些垃圾对象的.我们先来问下自己,如何判介绍了断一个对 ...
- 阿里dubbo服务注册原理解析
阿里分布式服务框架 dubbo现在已成为了外面很多中小型甚至一些大型互联网公司作为服务治理的一个首选或者考虑方案,相信大家在日常工作中或多或少都已经用过或者接触过dubbo了.但是我搜了 ...
- Flutter开发环境(Window)配置及踩坑记录
Flutter 是 Google 用以帮助开发者在 iOS 和 Android 两个平台开发高质量原生 UI 的移动 SDK.Flutter 兼容现有的代码,免费且开源,在全球开发者中广泛被使用. F ...
- [译]javascript中的依赖注入
前言 在上文介绍过控制反转之后,本来打算写篇文章介绍下控制反转的常见模式-依赖注入.在翻看资料的时候,发现了一篇好文Dependency injection in JavaScript,就不自己折腾了 ...
- 从新安装SQLserver 过程中报错问题合集
1.安装SQL SERVER2008 到安装支持文件就闪退? 分析:这个是由于安装目录没有删除干净导致的,我遗漏了一个文件夹:microsoft Management console文件夹没有删除的原 ...
- CPU HQ 什么意思
CPU HQ 什么意思 High performance graphics, quad core 高性能图形,四核 参见:https://www.intel.cn/content/www/cn/zh/ ...
- Bzoj2673 3961: [WF2011]Chips Challenge 费用流
国际惯例题面:如果我们枚举放几个零件的话,第二个限制很容易解决,但是第一个怎么办?(好的,这么建图不可做)考虑我们枚举每行每列最多放几个零件t,然后计算零件总数sum.这样如果可行的话,则有t*B&l ...
- BZOJ.1005.[HNOI2008]明明的烦恼(Prufer 高精 排列组合)
题目链接 若点数确定那么ans = (n-2)!/[(d1-1)!(d2-1)!...(dn-1)!] 现在把那些不确定的点一起考虑(假设有m个),它们在Prufer序列中总出现数就是left=n-2 ...
- 潭州课堂25班:Ph201805201 MySQL第三课 (课堂笔记)
单表查询: select * from select sname from stu; 条件查询 select sname from stu where sid=2; select sname from ...