# Display the shortest surface to surface distance using python

**URL:** https://discourse.slicer.org/t/display-the-shortest-surface-to-surface-distance-using-python/42500
**Category:** Support
**Tags:** python
**Created:** [April 9, 2025, 12:14pm UTC](https://discourse.slicer.org/t/display-the-shortest-surface-to-surface-distance-using-python/42500 "2025-04-09T12:14:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Matteboo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matteboo/32/66548_2.png) [@Matteboo](https://discourse.slicer.org/u/Matteboo)
#### Post date: [April 9, 2025, 12:14pm UTC](https://discourse.slicer.org/t/display-the-shortest-surface-to-surface-distance-using-python/42500/1 "2025-04-09T12:14:49Z")

</div>

Hello,

I have two segmentations. I want to measure and display the shortest distance between the surfaces (just like the picture below)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/7/b78d91fd0c5a6b92421d68d77a6840c65ea4d9c1.png)

I can measure the distance between the two surfaces using `slicer.modules.modeltomodeldistance` but I can’t get the two points that achieve this smallest distance.

Does anyone knows how to get the points achieving the smallest distance given by `slicer.modules.modeltomodeldistance`

---

<div class="post-metadata">

### Author: ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)
#### Post date: [April 9, 2025, 7:42pm UTC](https://discourse.slicer.org/t/display-the-shortest-surface-to-surface-distance-using-python/42500/2 "2025-04-09T19:42:54Z")

</div>

I just discovered this module and AFAIU, the following _should_ solve your problem, at least if your models do not intersect.

If you use `absolute_closest_point`, you can get one point coordinate from the `Absolute` array of the output model (the one(s) with the minimum absolute distance). With this coordinate, the polydata of the `Target Model` can give the second coordinate using `FindPoint()` and `GetPoint()`.

---

<div class="post-metadata">

### Author: ![Matteboo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matteboo/32/66548_2.png) [@Matteboo](https://discourse.slicer.org/u/Matteboo)
#### Post date: [April 10, 2025, 1:24pm UTC](https://discourse.slicer.org/t/display-the-shortest-surface-to-surface-distance-using-python/42500/3 "2025-04-10T13:24:27Z")

</div>

I managed to get this using the following code

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/c/6ccf7bc0048114d5c9beee13f417ec1ad3055303.png)

```auto
    # Step 2: Compute distance from inside to outside
    distance_model = findModelToModelDistance(outside_model, inside_model) # Distance from outside to inside
    VTKFieldData = distance_model.GetMesh().GetAttributesAsFieldData(0)
    created_nodes.append(distance_model)

    # Step 3: Extract signed distances and find min
    signedVals = []
    for i in range(VTKFieldData.GetArray("Signed").GetNumberOfTuples()):
        val = VTKFieldData.GetArray("Signed").GetValue(i)
        signedVals.append(-val) # Negate to get correct direction

    min_index = np.argmin(signedVals)
    min_distance = signedVals[min_index]

    # Step 4: Get closest point on inside
    closest_inside_point = inside_model.GetPolyData().GetPoint(min_index)

    # Step 5: Get corresponding closest point on outside
    locator = vtk.vtkPointLocator()
    locator.SetDataSet(outside_model.GetPolyData())
    locator.BuildLocator()
    closest_outside_point_id = locator.FindClosestPoint(closest_inside_point)
    closest_outside_point = outside_model.GetPolyData().GetPoint(closest_outside_point_id)

def findModelToModelDistance(modelNode1,modelNode2):
    vtkOutput = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelNode")
    vtkOutput.SetName("m2md")

    parameters = {}
    parameters["vtkFile1"] = modelNode2
    parameters["vtkFile2"] = modelNode1
    parameters['distanceType'] = "signed_closest_point"
    parameters["vtkOutput"] = vtkOutput

    cliNode = slicer.cli.runSync(slicer.modules.modeltomodeldistance, None, parameters)
    return vtkOutput

```
