我正在使用 vtk 试图确定两个多数据对象之间的最小距离。我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()