# Model point count changing after transformation

**URL:** <https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736>\
**Category:** Support\
**Created:** [December 14, 2022, 8:46pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736 "2022-12-14T20:46:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 14, 2022, 8:46pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/1 "2022-12-14T20:46:28Z")

</div>

We have a visualization tool in SlicerMorph that applies a TPS transformation derived from PCA to a reference model. I have exported transformed models. I am trying to use the model to model distance to visualize a heatmap (using the reference model as the target), specifically with the “corresponding point to point option”.

This generates an error that says:

```auto
Model To Model Distance standard error:

Both input files must have the same number of points to use 'corresponding_point_to_point'
Source model has 172232 points
Target model has 172259 points

```

However, if I go to Models menu and check the point counts for these models, they are reported identically and as 173261. Any idea where/why the difference might be coming from?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 14, 2022, 9:40pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/2 "2022-12-14T21:40:12Z")

</div>

Geometric transformation does not change number of points. How did you apply the transform? I suspect that normals were computed for one or both of the surfaces before or after transforming, which can duplicate points (to allow proper rendering of sharp edges).

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 14, 2022, 9:52pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/3 "2022-12-14T21:52:43Z")

</div>

As I export the file, I chose apply transform. But the thing is for the transformed models, Models module report point numbers identical to the original model. That baffles me.

---

<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:** [December 14, 2022, 10:02pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/4 "2022-12-14T22:02:35Z")

</div>

It looks like there’s a [step that is cleaning and removing degenerate points](https://github.com/NIRALUser/3DMetricTools/blob/4b4e478f9e20ecef0f66a20eb6719bd4a8c4d949/ModelToModelDistance/ModelToModelDistance.cxx#L340) from the input and output models, maybe there’s an issue like this with the transformed model?

I tried one example with the GPA module and didn’t see this error.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 14, 2022, 10:04pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/5 "2022-12-14T22:04:00Z")

</div>

> [@smrolfe](#):
>
> I tried one example with the GPA module and didn’t see this error.

Can you try with [GitHub - SlicerMorph/Mouse\_Models](https://github.com/SlicerMorph/Mouse_Models) and skip the scaling at GPA?

---

<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:** [December 14, 2022, 10:07pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/6 "2022-12-14T22:07:57Z")

</div>

I’m getting the expected result for this dataset:

 ![Screen Shot 2022-12-14 at 2.22.29 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/f/6f4df08d317c36046206dcaf3fefc1f18e14ed11.jpeg)

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 14, 2022, 10:28pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/7 "2022-12-14T22:28:26Z")

</div>

That can’t be, I think you didn’t skip the scaling step. Make sure you are using the data from the repo linked (not the slicermorph sample data).

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 14, 2022, 10:36pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/8 "2022-12-14T22:36:35Z")

</div>

> [@smrolfe](#):
>
> It looks like there’s a [step that is cleaning and removing degenerate points](https://github.com/NIRALUser/3DMetricTools/blob/4b4e478f9e20ecef0f66a20eb6719bd4a8c4d949/ModelToModelDistance/ModelToModelDistance.cxx#L340) from the input and output models, maybe there’s an issue like this with the transformed model?

Points may become “degenerate” if their positions are changed, so it is normal that a geometric transform changes the number of points after such cleanup.

If the processing step is unavoidable then you can store `PedigreeIds` (point ID in the input of the filter) or `GlobalIds` (some global point ID) in point scalars and make sure that all the used filters preserve point data. ModelToModelDistance module could use these point data to match points between differently processed meshes.

---

<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:** [December 14, 2022, 10:42pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/9 "2022-12-14T22:42:11Z")

</div>

Yes, that was the SM sample data. I reran with the data from GitHub and am not seeing an error, but that could be the choice of reference model or transform type.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 14, 2022, 10:43pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/10 "2022-12-14T22:43:21Z")

</div>

I chose the FVB\_NJ, which should be the sample reported as the closest to the mean. You should see a really strong size effect overall.

---

<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:** [December 15, 2022, 9:14pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/11 "2022-12-15T21:14:53Z")

</div>

After testing, we found it’s the SlicerMorph extension that breaks the operation of the model to model distance module, but it only happens on Windows. Linux and Mac work as expected with exactly the same Slicer version and settings.

I don’t see any libraries or other dependencies that could result in this kind of OS specific difference. @lassoan do you have any ideas? Even importing a single module from the SlicerMorph module causes this difference

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 15, 2022, 9:25pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/12 "2022-12-15T21:25:35Z")

</div>

And removing the SlicerMorph extension fixes the issue. And the only effected functionality is the point-to-point distance calculations, others work fine regardless whether SlicerMorph is installed or not on Windows.

We do not have any idea how to debug this, so your input is most welcomed.

@lassoan @pieper @jcfr

---

<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:** [December 15, 2022, 9:46pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/13 "2022-12-15T21:46:21Z")

</div>

That’s really strange. It implies there’s some kind of environment change that causes a side effect, like a library path or variable setting. From a quick look it seems the ModelToModel distance is a pure C++ CLI so maybe it’s a windows system dll.

One way to debug could be to open the [depends](https://www.dependencywalker.com/) from Slicer with and without the SlicerMorph module and compare the trees when examining the CLI executable. Start it from python with something like `os.system(r"C:\Users\piepe\Downloads\depends22_x86\depends.exe")` then do File-\>Open and browse to the CLI executable.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 15, 2022, 9:46pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/14 "2022-12-15T21:46:32Z")

</div>

What file format do you use for saving meshes? Have you changed any default node settings related to saving meshes?

---

<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:** [December 15, 2022, 9:47pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/15 "2022-12-15T21:47:18Z")

</div>

Interesting point - the MorphPreferences do switch the storage node to ply by default.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 15, 2022, 9:58pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/16 "2022-12-15T21:58:02Z")

</div>

but it works without slicermorph. All data is already saved as ply.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 15, 2022, 10:08pm UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/17 "2022-12-15T22:08:03Z")

</div>

Check if you can see any difference when you save to file and load that file. There should be no difference when saving into VTK or VTP format, but there could be subtle differences when exporting/importing non-VTK file formats.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 16, 2022, 4:53am UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/18 "2022-12-16T04:53:21Z")

</div>

I saved the models under the PCA transform as VTK and OBJ and with both formats, and I am still seeing the same error in Model to Model Distance corresponding point to point calculation option if SlicerMorph is installed. Again, only windows build seems to be affected.

If SlicerMorph is uninstalled, calculation with any format (ply, obj, vtk) work fine. I don’t think the issue is coming from the export step.

[I uploaded all six model here](https://app.box.com/s/plth0mmkovmqrvizoa9j5t3y9dibx0j6) if anyone wants to try to.

I set PC1 as source model, and Mean as target (though order doesn’t seem to matter for error generation).

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 16, 2022, 5:12am UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/19 "2022-12-16T05:12:56Z")

</div>

@pieper  
I have uploaded the output from DW with and without SlicerMorph [here](https://app.box.com/s/plth0mmkovmqrvizoa9j5t3y9dibx0j6). I can’t tell much from it.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 16, 2022, 6:32am UTC](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736/20 "2022-12-16T06:32:58Z")

</div>

ModelToModelDistance module [runs the inputs through `vtkCleanPolyData`](https://github.com/NIRALUser/3DMetricTools/blob/master/ModelToModelDistance/ModelToModelDistance.cxx#L85), which of course changes the number of points (merging of coincident points is one of the most useful cleaning feature). You can see that when you apply Clean step in Surface toolbox module in Slicer then the number of points in the meshes are reduced to exactly 172245 and and 172232 points as expected. Therefore, **Model2ModelDistance extension must be fixed: cleaning must be skipped when `corresponding_point_to_point` mode is used.**

The only somewhat mysterious finding is that why SlicerMorph appear to change the behavior. By the way, I was not able to reproduce the difference between installing SlicerMorph or not on Windows: if I use the .vtk files in the shared folder as inputs then Model to model distance module fails if `corresponding_point_to_point` option is used because of point count mismatch - even on a computer where SlicerMorph has never been installed. **It may worth checking why on linux and macos model to model distance computation mode succeeds** when corresponding point option is used; because it should fail on all platforms.

[Next page](https://discourse.slicer.org/t/model-point-count-changing-after-transformation/26736.md?page=2)
