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 ...
随机推荐
- jvm类加载器以及双亲委派
首先来了解几个概念: 类加载: 概念:虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验--转换解析--初始化,最终形成能被java虚拟机直接使用的java类型,就是jvm的类加载机制. ...
- 安卓在代码中设置TextView的drawableLeft、drawableRight、drawableTop、drawableBottom
Drawable rightDrawable = getResources().getDrawable(R.drawable.icon_new); //调用setCompoundDrawables时, ...
- 使用拷贝文件测试(BufferedInputStream,FileInputStream)
package com.demo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- BZOJ1218 [HNOI2003]激光炸弹 二维前缀和
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1218 题意概括 给出一个大的矩阵,求边长为r的正方形区域的最大sum. 题解 二维前缀和然后暴力就 ...
- [OpenCV-Python] OpenCV 中的图像处理 部分 IV (六)
部分 IVOpenCV 中的图像处理 OpenCV-Python 中文教程(搬运)目录 23 图像变换 23.1 傅里叶变换目标本小节我们将要学习: • 使用 OpenCV 对图像进行傅里叶变换 • ...
- 写一个java死锁的demo
package com.simon.study; /** * 线程死锁 一个线程要同时拥有两个对象的资源才能进行下一步操作: * @author: Simon * @date: 2017年7月29日 ...
- 计蒜客 无脑博士的试管们 【dfs】
题目链接:https://nanti.jisuanke.com/t/31 题目大意: 无脑博士有三个容量分别是A,B,C 升的试管,A,B,C 分别是三个从 1 到20 的整数,最初,A 和 B 试管 ...
- 【xxl-job】轻松实现分布式定时任务demo实例
[项目描述]前段时间专门独立了一个spring boot服务,用于做和第三方erp系统的对接工作.此服务的第一个需求工作就是可以通过不同的规则,设置不同的定时任务,从而获取erp系统的商品数据.所以, ...
- 数据库中,表一sum得出一个值,赋给表二的某个字段,为null
尝试使用了isnull(arg1,arg2)函数表示无效 最后运用了COALESCE(arg1,arg2,arg3,...) 该函数标识返回参数中第一个不为null的值. update a set ...
- C# EF 与 MySql 的那些坑
之前一直想用 mysql 和 ef .然后多次尝试也只能感叹 还是 sqlsever 是亲儿子. 今天在单位又尝试了一次,然后就成功了,记录一下遇到的问题. 首先是安装包和驱动?. 请保证 MySql ...