2013-04-11 49 views
1

我正在使用vtk試圖確定兩個polydata對象之間的最小距離。我正在使用vtkDistancePolyDataFilter來確定最小距離,但是如何確定定義此最小結果的每個對象上的關聯x,y,z點?繪製兩個vtkPolyData對象之間最接近的方法的矢量?

下面的最小示例根據距圓筒的距離給立方體着色;我希望在每個對象的最近點之間繪製一個vtkLinevtkDistancePolyDataFilter的結果似乎包括立方體對象的單元格,法線和距離,但我不知道如何輕鬆地確定哪些與最小距離相關,或者這與哪個點與圓柱體上的點有關。

編輯:我不確定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

這不能直接用VTK完成 - 需要物理/運動/碰撞庫。我目前正在研究如何讓Bullet與VTK合作。

相關問題