Distance Calculation Between Two Model Nodes

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

You could do this same loop in C++ and it would be faster. But if you have a labelmap version of the model (i.e. it comes from a segment or is easy to convert to one) you could make a distance transform volume from that. Then you should also be able to get the point coordinates of the other model as a numpy array, convert them to IJK, extract the distances from the volume with a vectorized indexing operation and then find the min of the resulting array.

Thank you very much. Converting to a segment and then performing the calculations seems like a very good approach.