# Cleaning Function Surface Toolbox does not handle 0 rea faces/duplicate edges

**URL:** <https://discourse.slicer.org/t/cleaning-function-surface-toolbox-does-not-handle-0-rea-faces-duplicate-edges/40124>\
**Category:** Support\
**Created:** [November 11, 2024, 3:51pm UTC](https://discourse.slicer.org/t/cleaning-function-surface-toolbox-does-not-handle-0-rea-faces-duplicate-edges/40124 "2024-11-11T15:51:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![evaherbst](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evaherbst/32/65595_2.png) [@evaherbst](https://discourse.slicer.org/u/evaherbst)\
**Post date:** [November 11, 2024, 3:51pm UTC](https://discourse.slicer.org/t/cleaning-function-surface-toolbox-does-not-handle-0-rea-faces-duplicate-edges/40124/1 "2024-11-11T15:51:45Z")

</div>

Hello,

I noticed that the clean function in the Surface Toolbox does not handle duplicate edges ie 0 area faces.  
Therefore it does not completely fix non manifold meshes.

Here you can see a mesh that I cleaned with the surface toolbox and checked in Blender. The highlighted edges are duplicated:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/2/527eccc6cddbc0af9503bd0088d01b4bf0a9cdbf.png)

If I move them you can see the issue:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/2/127e3456fbbef32bd05669029e2b0af98fad6fb2.png)

Would there be a way of adding a fix for this?  
Previously I have done a lot of semiautomatic mesh cleaning for FE meshes and 3D printing in Blender but would like to migrate everything to Slicer.

Also, I noticed a possible typo in the logic, should line 575 in [SurfaceToolbox.py](https://github.com/Slicer/SlicerSurfaceToolbox/blob/master/SurfaceToolbox/SurfaceToolbox.py) be

> SurfaceToolboxLogic.clean(inputModel, outputModel)

instead of

> SurfaceToolboxLogic.clean(outputModel, outputModel) ?

---

<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:** [November 11, 2024, 4:12pm UTC](https://discourse.slicer.org/t/cleaning-function-surface-toolbox-does-not-handle-0-rea-faces-duplicate-edges/40124/2 "2024-11-11T16:12:11Z")

</div>

Internally the code uses the vtk class with the default parameters.

> <https://github.com/Slicer/SlicerSurfaceToolbox/blob/master/SurfaceToolbox/SurfaceToolbox.py#L506-L513>

But there are lots of options that could address what you are seeing:

[https://vtk.org/doc/nightly/html/classvtkCleanPolyData.html](https://vtk.org/doc/nightly/html/classvtkCleanPolyData.html)

If you change to developer mode you can use the edit and reload options to change the settings and see what works. If you find settings that should be exposed they can be added to the module’s gui.

> [@evaherbst](#):
>
> possible typo

No, I think the code is correct. The code above what you referenced creates a new polydata if needed and the successive steps in the toolbox operate on that `outputModel`.

---

<div class="post-metadata">

**Author:** ![evaherbst](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evaherbst/32/65595_2.png) [@evaherbst](https://discourse.slicer.org/u/evaherbst)\
**Post date:** [November 11, 2024, 5:26pm UTC](https://discourse.slicer.org/t/cleaning-function-surface-toolbox-does-not-handle-0-rea-faces-duplicate-edges/40124/3 "2024-11-11T17:26:44Z")

</div>

Thanks @pieper

Great, I will look into that vtk class!  
I have not tried developer mode yet, that will be a good test case.

And ok, I was mistaken about the typo then, thanks for clarifying.

---

<div class="post-metadata">

**Author:** ![evaherbst](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evaherbst/32/65595_2.png) [@evaherbst](https://discourse.slicer.org/u/evaherbst)\
**Post date:** [November 12, 2024, 11:24am UTC](https://discourse.slicer.org/t/cleaning-function-surface-toolbox-does-not-handle-0-rea-faces-duplicate-edges/40124/4 "2024-11-12T11:24:58Z")

</div>

@pieper the developer mode works well.

I tried adding

> ```
> cleaner = vtk.vtkCleanPolyData()
> cleaner.ConvertLinesToPointsOn()
> cleaner.ConvertPolysToLinesOn()
> cleaner.ConvertStripsToPolysOn()
> cleaner.SetInputData(inputModel.GetPolyData())
> 
> ```

However, this does not fix the non manifold issues, since it only covers some, not all, mesh issues.

Would it be possible to incorporate the mesh cleaning functionality from the [3D print add-on in Blender](https://projects.blender.org/extensions/print3d_toolbox/src/branch/main/source/operators/cleanup.py)? It is written in Python and would enhance the Surface Toolbox cleaning function.

Here is the mesh that was giving me issues. I generated it by upsampling and remeshing and smoothing, all in Slicer. However, the non manifoldness remains (6 vertices, 2 edges, 2 faces): [O45\_003\_SlicerClean\_updatedNormals.stl - Google Drive](https://drive.google.com/file/d/1_dKk4dbaK7-Gzhky2Jt_IA5kuzzOR8jY/view?usp=sharing)

Thanks again!  
Eva

---

<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:** [November 12, 2024, 12:39pm UTC](https://discourse.slicer.org/t/cleaning-function-surface-toolbox-does-not-handle-0-rea-faces-duplicate-edges/40124/5 "2024-11-12T12:39:29Z")

</div>

There are other mesh cleanup options in vtk (see links below - I’m sure there are more if you search). They would be easy to add to the Surface Toolbox or as a separate module if there are lots of options.

But if the blender code works well you should also be able wrap and call a blender-python script as a subprocess. I looked a the code you linked and yes it’s in python, but it relies on the blender libraries to do the work. There is a [bpy](https://pypi.org/project/bpy/) package, but it didn’t install for me in slicer.

Maybe it’s possible to compile blender and bpy expose its python libraries in slicer’s python, and that could open interesting possibilities, but it would be easier as a start to just run blender in a separate process with its own python and pass meshes as files.

> **[Remove faces in contact with non-manifold edges](https://discourse.vtk.org/t/remove-faces-in-contact-with-non-manifold-edges/4116/14)**
>
> I don’t think vtkbool is accesible via pip. If you don’t want to build VTK then one option is to use VTK in the virtual Python environment that 3D Slicer provides. vtkbool is built as part of the Sandbox extension, so after installing 3D Slicer,...

> **[Remove non-manifold edges from decimated polydata](https://discourse.vtk.org/t/remove-non-manifold-edges-from-decimated-polydata/9002)**
>
> Hope this script is useful for someone… It’s done to be executed on Slicer but you can easily adapt it to pure vtk. guide\_dec = getNode('guide-dec') idFilter = vtk.vtkIdFilter() idFilter.SetInputData(guide\_dec.GetMesh());...

---

<div class="post-metadata">

**Author:** ![evaherbst](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evaherbst/32/65595_2.png) [@evaherbst](https://discourse.slicer.org/u/evaherbst)\
**Post date:** [November 12, 2024, 1:38pm UTC](https://discourse.slicer.org/t/cleaning-function-surface-toolbox-does-not-handle-0-rea-faces-duplicate-edges/40124/6 "2024-11-12T13:38:14Z")

</div>

Thank you @pieper !  
Seems like the codes in that forum post did not fully work for others but it is a great starting point.

Would be great to make it work with vtk but I will also think about running Blender separately.
