Clipping and cutting polydata shows incomplete contours

Hello everyone!
After my last question , to which I got a lot of useful suggestions, I managed to clip and cap a nested polydata.
The result is supposed to look like this:

The way I did this was to combine the inverted polydata from the inner element with the outer element using vtkPolyDataNormals (with FlipNormals on) and vtkAppendPolyData. Then I clipped the appended polydata with vtkClipPolyData and created a cap by using vtkCutter on the appended polydata and vtkContourTriangulator on the resulting contour.
My issue are these three things:

Inverted caps, in which the vtkContourTriangulator triangulates the outer contours and misses the actual inner part.

Situations in which the outer polydata is not really cut in such a way that there is a hole where the inverted inner polydata was appended, resulting in two caps being created on top of each other.

And finally the weird situation where both of these situations seem to cooccur and there are segments of the inner polydata missing, as if the vtkContourTriangulator skipped a triangle.

In this project I am using the Allen institute brain atlas for mice, and I am not entirely sure whether these issues occur due to problems with the model or mistakes in the way I am working with it.

I suspect that it has something to do with the way I cut the polydata, so that there are holes in the contour or the cap created by the triangulator.

How can I avoid these problems or fix them? Is there some step I am missing to properly cut the nested polydata?

Thanks in advance for any help you can give me with this!


Most computational geometry algorithms fail randomly if they get invalid or nearly-invalid input. Contour triangulator is not an exception, it does fail if you feed it with an arbitrarily complex contour set, such as one that is created from a noisy surface (that most likely contains a large number of errors, such as non-manifold edges, inverted triangles, small holes, etc.).

You can process the input mesh to ensure that it is perfect. Unfortunately no automatic solution exists for this, so you will need to experiment with different filters and probably also go back and regenerate the surface from the original segmentations.

Alternatively, you can use image-based methods (volume rendering), which does not suffer from instabilities like mesh-based methods (but may be computationally more intensive).

I already suspected that there are issues with the mesh. I tried to use vtkPolyDataNormals with Consistency, Splitting and AutoOrientNormals on, but this didn’t fix the issues.
I don’t think I can regenerate the surfaces, since these meshes came directly from the publicly available atlases done by the Allen brain institute.

For now I would like to avoid that, since this would also impact the way I would want to use this.

The most straight-forward way would likely be that I process the meshes that are available to me, but I don’t have any experience with that. What factors should I look at? And are there some filters you could recommend to me, that I could use to try to fix this?

You can use vtkCleanPolyData and use filters for finding and filling holes and non-manifold edges, but of course they will change the original mesh, so if you don’t have access to the original images then it is impossible to verify that all the changes are correct.

If I remember correctly, someone mentioned that Kitware is considering working on a filter that fixes some of the common mesh issues automatically. Maybe someone from Kitware can confirm this and give an estimation of when this might be available.

I don’t mind modifying the model to a small degree, since I can still look into the printed version of the atlas and check if the outlines match with the caps afterwards. The main point of this would be anyway to get a clean, nested surface which shows the general shape of the regions.

I spent some time looking into this, and this is what I found:
While there were some non-manifold edges (found with vtkFeatureEdges), I could remove them by using vtkTriangleFilter and the issues persisted, at the same spots and looking the same. So this excludes the non-manifold edges being a problem, I assume.

There were some holes, mostly seen with boundary edges:

While this hole could be fixed with vtkFillHolesFilter, others couldn’t, even with extremely high HoleSize, for example this one:

Fixing the small holes fixed 7/9 of the inverted caps and none of the weird missing triangles, so I left it in, with a low HoleSize.

The remaining inverted caps and the weird missing triangles I could fix by using vtkCleanPolyData as you suggested. But while this fixed the remaining problems in this model, at a tolerance of 0.3%, it wasn’t enough to fix the issues in other models, and the same problems persisted there: inverted caps and missing triangles. When I went above 0.5% tolerance, to try and fix the issues in the other models, I ended up with this:

Cleaner also introduced some holes below 0.5%, but these could still be filled by vtkFillHolesFilter, while these dark spots couldn’t be fixed.
These dark triangles look like they are inverted and cause even more problems of the same type as before. Do you know if I can fix them somehow? Or is there a better way to do this? I tried using vtkPolyDataNormals with AutoOrientNormalsOn, but this only broke the model completely and the caps were all inverted.

Since this solutions seems insufficient to avoid the problems in the models, I thought about just focusing on the contours, which are used to generate the caps. I don’t care much about the holes in the models themselves, as the user will mostly look at the caps anyway, so I think it might be good enough to get the output from vtkCutter and fix any problems with the model there, before the cap is created by vtkContourTriangulator. Do you know if there is a way to fix the contours, for example by interpolating missing parts and closing them? How would you do this?

Thanks for the update. It is good to know that at least some errors could be fixed by existing filters. The mesh seems to be so low quality that it is unlikely that it can be fixed with reasonable effort.

Cutting a surface with a plane is trivial and you will always get perfect contours. So, if you don’t need to fill the contours or create caps then it is a trivial task.

Fixing errors in the contours is the same that you have been trying all along, except if you only start from the contours then you miss so much information that there is no chance you can create correct contours.

Do you have the labelmap-based segmentation that generated the meshes? If yes, then you can create the caps by cutting that labelmap - you can always cut a volume with a plane perfectly. If you don’t have the source segmentation then you can rasterize the meshes to a labelmap. If you find errors in the labelmap then you can fix that using segment editing tools (e.g., 3D Slicer’s Segment Editor).

I don’t have access to the segmentation they used to generate the meshes, but I found two other atlases with different resolutions. The one with the higher resolution seems to have less problems, but the same things show up as in the original atlas.

I understand your explanation that starting at a later point (the contours) with even less information has little chance of succes, when I want to maintain the original structure and create the correct contours.
That is why I had another look at the surface (before and I think I need to correct myself: When I checked once more the edges of the problematic cuts, I found that the errors seem to originate in non-manifold edges, which I overlooked in my original attempts.

For example in the caps with missing triangles, these seem to always touch a non-manifold edge, even though the boundaries still look alright, like this:

In the cases of fully inverted caps, there also seem to be problems with non-manifold edges

I missed this because there seems to be a combination of holes and non-manifold edges, which together cause the problems, and after I ran this through vtkTriangleFilter the (hole-based) issues persisted, which led me to believe that I should just remove it. When I check now after running the mesh through vtkCleanPolyData and afterwards vtkFillHolesFilter, I still see some non-manifold edges and the issues seem connected to them, but I haven’t managed to remove the faulty edges.

Do you know a way how to work around non-manifold edges, or even remove them?

Was there ever a followup solution to these issues? I’m currently running into this same problem after using vtk’s boolean operations. I’ve used vtkCleanPolyData, vtkTriangleFilter, vtkFillHolesFilter however the resulting meshes are never guaranteed to be watertight.

I don’t think it is impossible to develop a general, fully automatic method for fixing non-manifold surfaces. However, it could be possible to develop algorithms that can fix some mesh problems and create semi-automatic tools to fix the rest. If you have many non-trivial errors in your mesh then you probably need to step back and generate them differently.

Unfortunately, VTK’s built-in Boolean filters do not work (randomly give incorrect result for valid inputs), but vtkbool library is good.

Is this the vtkbool library extension you mentioned? GitHub - zippy84/vtkbool: A new boolean operations filter for VTK

Yes, that’s it. If you want to try it with a convenient GUI or in Python, then you can do it in 3D Slicer by installing Slicer’s “Sandbox” extension. It provides a simple module, Combine models:

It is used in Python scripting for example to automatically create these quite sophisticated surgical guides for bone reconstruction: