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语言详解
VTKExamples/Cxx/Graphs/ShortestPath
VTKExamples/Cxx/PolyData/DijkstraGraphGeodesicPath
VTK: vtkDijkstraGraphGeodesicPath Class Reference
vtkDijkstraGraphGeodesicPath在曲面上寻找最短路径的应用
VTK计算网格模型上的最短路径相关推荐
- VTK修炼之道50:图形基本操作进阶_网格模型的特征边 与 封闭性检测
1.封闭性检测 由于受原始数据.重建方法的限制,得到的网格模型并不是封闭的.有时为了显示或者处理某些要求,需要网格必须是封闭的. 封闭性网格应该比较好理解,比如一个球形网格. 1.1网格模型边的分类 ...
- OBJ网格模型文件(上) - 学习随笔
很早的时候一直有个执念: 我要从0开始,编程写一个obj模型的加载器. 在学习3dsmax的时候,发现这种格式被很多软件所支持,而且导出的文件只含模型和材质,体积很小很干净,建好模型之后通常保存为自身 ...
- 点云/网格模型的体积计算
点云体积计算 有时用激光扫描设备扫描零件或者用无人机进行测量后会想知道它们的体积.比如下面的土堆: 如果扫描得到的数据是一系列三维点云,那么体积就比较难求,因为如何定义物体的边界比较困难.一种方法是提 ...
- VTK:网格上插值网格用法实战
VTK:网格上插值网格用法实战 程序输出 程序完整源代码 程序输出 程序完整源代码 #include <vtkActor.h> #include <vtkCamera.h> #
- ITK:计算网格上的测地距离
ITK:计算网格上的测地距离 内容提要 输出结果 输入 输出 C++实现代码 内容提要 从网格上提供的seed 顶点计算测地距离. 输出结果 输入 输出 C++实现代码 #include " ...
- three相机在模型上_基于 three.js 的 3D 粒子动效实现
作者:个推web前端开发工程师 梁神 一.背景 粒子特效是为模拟现实中的水.火.雾.气等效果由各种三维软件开发的制作模块,原理是将无数的单个粒子组合使其呈现出固定形态,借由控制器.脚本来控制其整体或单 ...
- 图形处理(十二)拉普拉斯网格优化、最小二乘网格模型光顺
看这篇博文前,请先参考我的另外一篇博文<图形处理(三)简单拉普拉斯网格变形-Siggraph 2004>学习拉普拉斯坐标的相关理论知识.这里要分享的paper,是通过拉普拉斯的方法实现三角 ...
- 桩筏有限元中的弹性板计算_采用PKPM系列JCCAD软件桩筏筏板有限元方法计算的模型参数 -...
*****采用PKPM系列JCCAD软件桩筏筏板有限元方法计算的模型参数****** 计算模型: 弹性地基梁板模型 (桩和土按WINKLER模型) 地基基础形式及参照规范: 天然地基(地基规范).常规 ...
- 【Visual C++】游戏开发五十一 浅墨DirectX教程十九 网格模型进阶之路
本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhmxy555/article/details/8770426 作者:毛星云(浅墨 ...
最新文章
- sql查两个时间内的数据_如何通过AWR的sql脚本查执行时间、消耗CPU、gets、I/0等的sql?...
- 029_jQuery Ajax简介
- PowerToys 又推新工具 Launcher,可以替代 Win+R
- 如何测量太阳距离地球的距离
- influxdb java api使用_java使用influxDB数据库的详细源码
- Hadoop MapReduce篇
- python如何安装pdfminer_Python3.8安装pdfminer
- 网关串口+EM310
- topjui中datagrid增删改查
- 卸载ncurses_linux 安装,卸载 监控服务
- ModifyStyle ModifyStyleEx修改自定义控件的问题
- java调用小冰,Java利用微软小冰API测颜值
- 第二章 2.群中的等价关系 -- 陪集,共轭,正规子群与商群
- win7下桌面IE快捷方式无法删除解决方法
- 移动互联网创业:美国不代表全球
- git is outside repository
- obd协议 混动车_OBD完整协议
- 等级测评——定级、等级划分及测评时间
- winRAR压缩、解压不常见的文件格式
- 强化学习实践六 :给Agent添加记忆功能