pythonvtkgrasspyvista

How to cut vertices and faces connected to points lower than some value in pyvista?


So when one exports r.out.vtk from Grass GIS we get a bad surface with -99999 points instead of nulls:

enter image description here

I want to remove them, yet a simple clip is not enough:

pd = pv.read('./pid1.vtk')
pd = pd.clip((0,1,1), invert=False).extract_surface()
p.add_mesh(pd ) #add atoms to scene
p.show()

resulting in: enter image description here

So I wonder how to keep from it only top (> -999) points and connected vertices - in order to get only the top plane (it is curved\not flat actually) using pyvista?

link to example .vtk


Solution

  • There is an easy way to do this and there isn't...

    You could use pyvista's threshold filter with all_scalars=True as long as you have only one set of scalars:

    import pyvista as pv
    
    pd = pv.read('./pid1.vtk')
    pd = pd.threshold(-999, all_scalars=True)
    
    plotter = pv.Plotter()
    plotter.add_mesh(pd) #add atoms to scene
    
    plotter.show()
    

    Since all_scalars starts filtering based on every scalar array, this will only do what you'd expect if there are no other scalars. Furthermore, unfortunately there seems to be a bug in pyvista (expected to be fixed in version 0.32.0) which makes the use of this keyword impossible.

    What you can do in the meantime (if you don't want to use pyvista's main branch before the fix is released) is to threshold the data yourself using numpy:

    import pyvista as pv
    
    pd = pv.read('./pid1.vtk')
    
    scalars = pd.active_scalars
    keep_inds = (scalars > -999).nonzero()[0]
    pd = pd.extract_points(keep_inds, adjacent_cells=False)
    
    plotter = pv.Plotter()
    plotter.add_mesh(pd) #add atoms to scene
    
    plotter.show()
    

    The main point of both all_scalars (in threshold) and adjacent_cells (in extract_points) is to only keep cells where every point satisfies the condition.

    With both of the above I get the following figure using your data: fixed mesh: small flat surface without spurious jumps