我正在使用vtk尝试确定两个polydata对象之间的最小距离。我使用vtkDistancePolyDataFilter
来确定最小距离,但如何确定定义此最小结果的每个对象上的相关x,y,z点?
下面的最小例子根据到圆柱的距离为立方体着色;我想在每个vtkLine
对象的最近点之间绘制vtkPolyData
。 vtkDistancePolyDataFilter
的结果似乎包括立方体对象的单元格,法线和距离,但我不知道如何轻松地提取哪些与最小距离或这与点的关系如何相关联圆筒。
修改:我不确定vtkDistancePolyDataFilter
是否正确计算最小距离 - 它似乎只进行顶点 - 顶点交互。
import vtk
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
cube = vtk.vtkCubeSource()
cube.SetCenter (1.2, 0.3, 0.4);
transform = vtk.vtkTransform()
transform.RotateX(45)
transform.RotateY(45)
transform.Translate((1.2,0.2,0))
tf = vtk.vtkTransformPolyDataFilter()
tf.SetInput(cube.GetOutput())
tf.SetTransform(transform)
tf.Update()
cubeMapper = vtk.vtkPolyDataMapper()
cubeMapper.SetInput(tf.GetOutput())
cubeActor = vtk.vtkActor()
cubeActor.SetMapper(cubeMapper)
#Create Cylinder
cylinder = vtk.vtkCylinderSource()
cylinder.SetHeight(2)
cylinderMapper = vtk.vtkPolyDataMapper()
cylinderMapper.SetInput(cylinder.GetOutput())
cylinderActor = vtk.vtkActor()
cylinderActor.SetMapper(cylinderMapper)
#Determine distance btwn Cylinder and Cube
distfilt = vtk.vtkDistancePolyDataFilter()
distfilt.SetInputConnection(1, tf.GetOutputPort())
distfilt.SetInputConnection(0, cylinder.GetOutputPort())
distfilt.Update()
#Color the cube based on distance to cylinder
distmapper = vtk.vtkPolyDataMapper()
distmapper.SetInputConnection(distfilt.GetOutputPort(1))
distmapper.SetScalarRange( \
distfilt.GetOutput().GetPointData().GetScalars().GetRange()[0],
distfilt.GetOutput().GetPointData().GetScalars().GetRange()[1],)
distactor = vtk.vtkActor()
distactor.SetMapper(distmapper)
distarray = distfilt.GetOutput().GetPointData().GetScalars()
for i in range(24):
print distarray.GetValue(i)
# Minimum distance is the lowest of these results, but what are the
# Corresponding points on the cylinder and cube?
scalarBar = vtk.vtkScalarBarActor()
scalarBar.SetLookupTable(distmapper.GetLookupTable())
scalarBar.SetTitle("Distance")
scalarBar.SetNumberOfLabels(5)
ren.AddActor(cubeActor)
ren.AddActor(cylinderActor)
ren.AddActor(distactor)
ren.AddActor2D(scalarBar)
iren.Initialize()
renWin.Render()
iren.Start()
答案 0 :(得分:0)
这不能直接用VTK完成 - 需要物理/运动学/碰撞库。我目前正在研究如何让Bullet使用VTK。