# Python code model-to-model distance

**URL:** <https://discourse.slicer.org/t/python-code-model-to-model-distance/15593>\
**Category:** Support\
**Created:** [January 19, 2021, 4:09pm UTC](https://discourse.slicer.org/t/python-code-model-to-model-distance/15593 "2021-01-19T16:09:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Judith](https://avatars.discourse-cdn.com/v4/letter/j/d9b06d/32.png) [@Judith](https://discourse.slicer.org/u/Judith)\
**Post date:** [January 19, 2021, 4:09pm UTC](https://discourse.slicer.org/t/python-code-model-to-model-distance/15593/1 "2021-01-19T16:09:35Z")

</div>

Hello everyone,  
Does anyone know where to find the underlying python code of the model-to-model distance module? I want to automate these parameters:

’ Source model: prostate  
Target model: tumor  
Vtkoutputfile: create new model as ‘whole\_dist\_pt’  
Distance: signed\_closest\_point  
Apply’

I have to do this for 6 models, so it would be really convenient if I can just copy the python script instead of manually clicking every time.

Many thanks in advance,

Kind regards,  
Judith

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [January 19, 2021, 11:40pm UTC](https://discourse.slicer.org/t/python-code-model-to-model-distance/15593/2 "2021-01-19T23:40:58Z")

</div>

The C++ source code for the model-to-model distance module is [here](https://github.com/NIRALUser/3DMetricTools/tree/master/ModelToModelDistance).

It uses the vtkDistancePolyDataFilter which also has a Python wrapper. I have a Python module that uses this filter [here](https://github.com/SlicerMorph/SlicerMorph/blob/4e7fcdf0a9e035cb66539ba140b8ba32a7413da8/MeshDistanceMeasurement/MeshDistanceMeasurement.py#L281) which may help as an example.

---

<div class="post-metadata">

**Author:** ![BraveDistribution](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bravedistribution/32/14564_2.png) [@BraveDistribution](https://discourse.slicer.org/u/BraveDistribution)\
**Post date:** [July 13, 2022, 4:40pm UTC](https://discourse.slicer.org/t/python-code-model-to-model-distance/15593/3 "2022-07-13T16:40:47Z")

</div>

Hi @smrolfe:

thanks for the script.

I am wondering why is rmse used instead of mean in the code?

```auto
#meanDistance = np.average(distanceArray)
meanDistance = self.rmse(distanceArray)

```

Do the indices of the meshes nodes have to correspond, when calculating distance?

Thanks.

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [July 13, 2022, 6:30pm UTC](https://discourse.slicer.org/t/python-code-model-to-model-distance/15593/4 "2022-07-13T18:30:48Z")

</div>

@BraveDistribution The mean error should not be used here since the script is using the signed distance option of the vtkDistancePolyDataFilter. The commented line is from a previous version, I will remove to avoid confusion.

In this implementation, the distance to the second model is computed for every point on the first model, so the mesh nodes are not required to correspond.

---

<div class="post-metadata">

**Author:** ![BraveDistribution](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bravedistribution/32/14564_2.png) [@BraveDistribution](https://discourse.slicer.org/u/BraveDistribution)\
**Post date:** [July 14, 2022, 12:55pm UTC](https://discourse.slicer.org/t/python-code-model-to-model-distance/15593/5 "2022-07-14T12:55:55Z")

</div>

Thanks.

What’s interesting is when using this code, I receive segfault:

```auto
    distanceFilter = vtk.vtkDistancePolyDataFilter()
    distanceFilter.SetInputData(0, input1)
    distanceFilter.SetInputData(1, input2)
    distanceFilter.SignedDistanceOff()
    distanceFilter.Update() # Calculate distance

```

at distanceFilter.Update() I receive:  
`Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)`

I know that’s probably out of scope of slicer, just wanted to mention it, since its closely related.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 14, 2022, 6:37pm UTC](https://discourse.slicer.org/t/python-code-model-to-model-distance/15593/6 "2022-07-14T18:37:14Z")

</div>

@BraveDistribution the thing to do is to create a small complete script in pure VTK and submit it along with the data that replicates the issue to the [VTK issue tracker](https://gitlab.kitware.com/vtk/vtk/-/issues).

---

<div class="post-metadata">

**Author:** ![griffinfi](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@griffinfi](https://discourse.slicer.org/u/griffinfi)\
**Post date:** [July 16, 2024, 7:29pm UTC](https://discourse.slicer.org/t/python-code-model-to-model-distance/15593/7 "2024-07-16T19:29:22Z")

</div>

Hi there,

I wanted to follow up on this as I do not know how to use C++ – do I copy and paste that code into Slicer? Or use the “wrapper” ?  
Thanks in advance.
