# Algorithm behind reference mesh

**URL:** <https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261>\
**Category:** Uncategorized\
**Created:** [August 21, 2024, 1:38pm UTC](https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261 "2024-08-21T13:38:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Apoorva](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Apoorva](https://shapeworks.discourse.group/u/Apoorva)\
**Post date:** [August 21, 2024, 1:38pm UTC](https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261/1 "2024-08-21T13:38:52Z")

</div>

I have tried using the Shapeworks Python library to develop the SSM. It seems that it tries to find the reference mesh using the function

ref\_index = sw.find\_reference\_mesh\_index(mesh\_list)

Can you please let me know how it finds the reference mesh? What algorithm goes behind it? And also how to find which mesh is the reference mesh in the Shapeworks software?

---

<div class="post-metadata">

**Author:** ![amorris](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amorris](https://shapeworks.discourse.group/u/amorris)\
**Post date:** [August 21, 2024, 7:37pm UTC](https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261/2 "2024-08-21T19:37:03Z")

</div>

This method performs pairwise ICP registrations between shapes and computes the average surface to surface distance. Then it picks the mesh that had the minimal distance to all other shapes.

It returns the index of this mesh, which should be the index into mesh\_list for the reference.

---

<div class="post-metadata">

**Author:** ![Apoorva](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Apoorva](https://shapeworks.discourse.group/u/Apoorva)\
**Post date:** [August 22, 2024, 9:59am UTC](https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261/3 "2024-08-22T09:59:46Z")

</div>

Thank you for your reply.  
Can you please let me know how we can get the reference geometry file name using Shapeworks software?

---

<div class="post-metadata">

**Author:** ![amorris](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amorris](https://shapeworks.discourse.group/u/amorris)\
**Post date:** [August 22, 2024, 4:46pm UTC](https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261/4 "2024-08-22T16:46:30Z")

</div>

```auto
ref_index = sw.find_reference_mesh_index(mesh_list)
mesh = mesh_list[ref_index]

```

`mesh` should contain the geometry of the reference mesh.

---

<div class="post-metadata">

**Author:** ![Apoorva](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Apoorva](https://shapeworks.discourse.group/u/Apoorva)\
**Post date:** [August 23, 2024, 8:38am UTC](https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261/5 "2024-08-23T08:38:44Z")

</div>

Thank you for your reply.

Is the same reference mesh used in the software to generate the final SSM meshes using the particle information?

---

<div class="post-metadata">

**Author:** ![amorris](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amorris](https://shapeworks.discourse.group/u/amorris)\
**Post date:** [August 26, 2024, 6:52am UTC](https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261/6 "2024-08-26T06:52:37Z")

</div>

No, the mesh used for the reference for mesh warping (reconstruction) is the median shape based on the particle system.

---

<div class="post-metadata">

**Author:** ![Apoorva](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Apoorva](https://shapeworks.discourse.group/u/Apoorva)\
**Post date:** [August 27, 2024, 9:44am UTC](https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261/7 "2024-08-27T09:44:56Z")

</div>

Is the same reference mesh (obtained from the median mesh) used to generate all mode meshes?

Also, how to get the median mesh based on the particle system?  
It is possible to get the median of all particles (shown as median\_particle) but how to get the median geometry using the below python code?

warper = MeshWarper()  
warper.generateWarp(reference\_mesh, median\_particle)  
median\_mesh = warper.buildMesh(median\_particle)

What is the reference\_mesh to get the median mesh?

---

<div class="post-metadata">

**Author:** ![amorris](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amorris](https://shapeworks.discourse.group/u/amorris)\
**Post date:** [August 28, 2024, 3:13pm UTC](https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261/8 "2024-08-28T15:13:11Z")

</div>

Yes, the same reference/template mesh is used to generate all reconstructions. In principle it can be any shape from the model, but we chose the median shape.

The Python API seems to be missing a method to query the median shape. I’ve created an issue to add it here:

> <https://github.com/SCIInstitute/ShapeWorks/issues/2320>
>
> The Python analysis API should have a method to query the median shape from a sh…ape model.

Briefly, it is identified by finding the shape that is closest to all other shapes (in the global space). This is done by adding up the particle-particle distance for each shape and finding the shape that has the minimum sum of particle-particle distances (L1 Norm).

I am not sure what the median position of each particle would define, but any set of particles can be used with the MeshWarper.

---

<div class="post-metadata">

**Author:** ![Apoorva](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Apoorva](https://shapeworks.discourse.group/u/Apoorva)\
**Post date:** [August 29, 2024, 1:18pm UTC](https://shapeworks.discourse.group/t/algorithm-behind-reference-mesh/261/9 "2024-08-29T13:18:28Z")

</div>

Thank you for your reply.

I hope the median shape API get updated soon.
