Skip to content
Advertisement

Find cells from vertices in pyvista PolyData mesh

I have a PolyData mesh, and I am trying to color some cells that contain given vertices. However, I need an efficient way to to this.

For instance, this works, but it is way too slow for a large mesh:

JavaScript

This is what I get, which is fine…I just need to do it much faster!

enter image description here

Advertisement

Answer

If you have the point indices and want to extract every cell that contains any of these points you can use the extract_points() filter whose default adjacent_cells=True, include_cells=True parameters imply that extracting your points (all at once) will also extract every cell they are a part of.

Then all that’s left is using the cell scalars called 'vtkOriginalCellIds' and use those indices to change your scalars in the original mesh. I’ve cleaned up some style things in the code:

JavaScript

result showing points on a sphere's surface and differently coloured cells around them as islands

As you can see, with this approach you indeed get every cell that contains special points, leading to islands of cells around each point. This is what I’d expect based on your description. I suspect the reason why your original output doesn’t reflect this is because finding points by 3d point coordinates is prone to floating-point errors, which makes VTK miss most of the surrounding cells.

Advertisement