Skip to content

Feature request: Add support for clip by scalar #874

@Aquan1412

Description

@Aquan1412

It would be nice to have access to clip-by-scalar in PyVista. The vtk function that is used in the background (vtkTableBasedClipDataSet) already offers this feature, so it's only a matter of making this functionality available in PyVista.

In my experience, clip-by-scalar produces much nicer looking surfaces than threshold, and is therefore more suitable for visualizing iso-surfaces/ iso-volumes.

For my personal use I already adapted the existing clip-function in PyVista and it works without problems (however, I only have unstructured meshes to test it, I don't know if there might be problems with other types of meshes).

This is the adapted function:

def clip_scalar(dataset, scalars=None, invert=True, value=0.0, inplace=False):
        """Clip a dataset by a scalar.

        Parameters
        ----------
        scalars : str, optional
            Name of scalars to clip on. Defaults to currently active scalars.

        invert : bool, optional
            Flag on whether to flip/invert the clip.

        value : float, optional
            Set the clipping value.
            The default value is 0.0.

        inplace : bool, optional
            Updates mesh in-place while returning nothing.

        """
        if isinstance(dataset, vtk.vtkPolyData):
            alg = vtk.vtkClipPolyData()
        # elif isinstance(dataset, vtk.vtkImageData):
        #     alg = vtk.vtkClipVolume()
        #     alg.SetMixed3DCellGeneration(True)
        else:
            alg = vtk.vtkTableBasedClipDataSet()
            
        alg.SetInputDataObject(dataset) # Use the grid as the data we desire to cut
        alg.SetValue(value)
        if scalars is None:
            field, scalars = dataset.active_scalars_info
        _, field = get_array(dataset, scalars, preference='point', info=True)
        alg.SetInputArrayToProcess(0, 0, 0, field.value, scalars) # args: (idx, port, connection, field, name)
        alg.SetInsideOut(invert) # invert the clip if needed
        alg.Update() # Perform the Cut
            
        # run the clip
        result = _get_output(alg)
        
        if inplace:
            dataset.overwrite(result)
        else:
            return result

It would be nice to have this as a part of PyVista, however, I don't really have experience with contributing code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions