Hello,
I have two model nodes: one that moves based on a transform handle and another that is fixed. I want to find the shortest distance between these two model nodes.
I’ve implemented this functionality with the code below, but I’d like to improve its speed (it seems that copying the nodes is taking a significant amount of time).
Could you provide some advice on optimizing this functionality?
def compute_closest_distance_between_models(event, caller, modelA, modelB):
polyDataA = modelA.GetPolyData()
transformNode = modelB.GetParentTransformNode()
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
nodeItem = shNode.GetItemByDataNode(modelB)
copyItem = slicer.modules.subjecthierarchy.logic().CloneSubjectHierarchyItem(shNode, nodeItem)
modelBCopy = shNode.GetItemDataNode(copyItem)
modelBCopy.SetAndObserveTransformNodeID(transformNode.GetID())
slicer.vtkSlicerTransformLogic().hardenTransform(modelBCopy)
polyDataB = modelBCopy.GetPolyData()
distanceFilter = vtk.vtkImplicitPolyDataDistance()
distanceFilter.SetInput(polyDataB)
closestDistance = float('inf')
for i in range(polyDataA.GetNumberOfPoints()):
point = polyDataA.GetPoint(i)
distance = distanceFilter.EvaluateFunction(point)
if distance < closestDistance:
closestDistance = distance
slicer.mrmlScene.RemoveNode(modelBCopy)
print(closestDistance)
return closestDistance