ClipPolydata with points that are all around the polydata

Hi All,
I need to clip a polydata with some points. I used SelectPolydata but sometimes it doesn’t work because of the “can not follow edge” error (see code blew).

    loop = vtk.vtkSelectPolyData()
    loop.SetInputData(poly)
    loop.SetLoop(points)
    loop.GenerateSelectionScalarsOn()
    loop.SetSelectionModeToSmallestRegion()
    loop.InsideOutOff()
    loop.Update()
    clipper = vtk.vtkClipPolyData()
    clipper.SetInputData(loop.GetOutput())
    clipper.InsideOutOn()
    clipper.Update()

I use vtkImplicitSelectionLoop, but it doesn’t clip correctly because my points go around the polydata.see code:

    loop = vtk.vtkImplicitSelectionLoop()
    loop.SetLoop (points)

    clipper = vtk.vtkClipPolyData()
    clipper.SetInputData(self.entityManager.selected.part)
    clipper.SetClipFunction(loop)
    clipper.InsideOutOn()
    clipper.Update()
    clipped_poly = clipper.GetOutput()

and this picture :
Capture
How can I clip polydata with this points?

1 Like

vtkSelectPolyData can indeed get stuck (logs “Can’t follow edge” error) if control points are too far from each other or the mesh is too complex or have errors. I’ve found that by resampling the input curve and providing input points closer to each other often fixes the issue. According to the documentation of the class, the loop must not intersect itself.

thank you for your reply.
I increased the number of points so much that it looked like a line, but I still got the error.

If you provide approximately one control point for every few triangles in the mesh and the curve is not self-intersecting and path search still fails them you have to fix errors in the mesh. A small hole or non-manifold edge can throw the algorithm off.

For more robust geodesic path search, which also provides superior results, because it can cut across surface cells (not just connecting mesh points) you could try this method:

It has C++ implementation with MIT license, so you could turn it into a VTK filter without redeveloping anything.

1 Like

Hi, Mohammad,

You can try using vtkCleanPolyData (https://vtk.org/doc/nightly/html/classvtkCleanPolyData.html#details) to make sure your mesh forms a water tight polyhedron. There’s an example here: https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/CleanPolyData .

As for the geodesic path, you can try CGAL if you still get stuck: CGAL 5.2 - Triangulated Surface Mesh Shortest Paths: User Manual .

regards,

Paulo

1 Like

It is fantastic but I don’t understand too much about C++. how can I turn it into a VTK filter?
I use Python.

Hi Paulo, Thanks for your reply.
I already use vtkCleanPolyData but it wasn’t the solution.
As for the CGAL, it is C++ and I don’t know how I can use it in python. Is there any example for python?

Hello, Mohammad.

I didn’t notice the lack of C++'s semi-colons… Well, CGAL is heavily template-based. C++ templates are, well, templates to data types, not data types. Think C++ templates as recipes for the pre-processor to write code of real data types. Templates are great to minimize code writing. For example: instead of writing ListOfDouble, ListOfInteger, ListOfString, etc. classes, you just declare something like List<Type>. Then, when you write something like List<String>, the pre-processor writes a real ListOfString C++ class for you. Great, isn’t it? Well, that spells doom when it comes to Python bindings, which normally cannot make C++ classes on the fly. You may have guessed by now that putting up a working CGAL front-end for Python 3 will be quite an odyssey. It’s possible, but I think it won’t be worth the trouble. If you still want to delve into the world of Python-C++ bindings, here some resources I found after a bit of googling:

geometry - What happened to the python bindings for CGAL? - Stack Overflow (a bit of history of the complicated Python-CGAL binding with some alternative solutions)
http://cgal-python.gforge.inria.fr/CGAL_PYTHON_1.pdf ( nice slideshow teaching on how to use CGAL with Python 2).
http://cgal-python.gforge.inria.fr/ (the home page of a Python-CGAL binding. Rather old, as you may guess from the early 2000’s design of the page)

But I think you’re better off trying these: Geodesic Paths — PyVista 0.29.0 documentation and Geodesic Distance Computation on 3-D Meshes . I don’t know them by experience. I’ve just found them by googling around.

cheers,

Paulo

vtkSelectPolyData filter’s “Can’t follow edge” bug (and a few others) are fixed now see https://gitlab.kitware.com/vtk/vtk/-/merge_requests/8964

3 Likes

Hi Andras,
That’s perfect. how can I use it?
I use the updated version of VTK (vtk 9.1.0) but there isn’t Dijkstra method on it.