Surface Boolean Logic

Use a surface inside a volume to set scalar values on an array in the volume.

Adopted from https://docs.pyvista.org/examples/01-filter/clipping-with-surface.html

import numpy as np
import pyvista as pv

Make a gridded volume

n = 51
xx = yy = zz = 1 - np.linspace(0, n, n) * 2 / (n - 1)
dataset = pv.RectilinearGrid(xx, yy, zz)

Define a surface within the volume

surface = pv.Cone(direction=(0, 0, -1), height=3.0, radius=1, resolution=50, capping=False)

Preview the problem

p = pv.Plotter()
p.add_mesh(surface, color="w", label="Surface")
p.add_mesh(dataset, color="gold", show_edges=True, opacity=0.75, label="To Clip")
p.add_legend()
p.show()
surface boolean

Compute an implicit distance inside the volume using this surface, then inject new data arrays

dataset.compute_implicit_distance(surface, inplace=True)
HeaderData Arrays
RectilinearGridInformation
N Cells125000
N Points132651
X Bounds-1.040e+00, 1.000e+00
Y Bounds-1.040e+00, 1.000e+00
Z Bounds-1.040e+00, 1.000e+00
Dimensions51, 51, 51
N Arrays1
NameFieldTypeN CompMinMax
implicit_distancePointsfloat641-7.623e-011.250e+00


Take note of the new implicit_distance scalar array. We will use this to fill in regions inside the surface with the value 3.0 and regions outside the surface with the value 2.0

dataset["my_array"] = np.zeros(dataset.n_points)
dataset["my_array"][dataset["implicit_distance"] >= 0] = 2.0
dataset["my_array"][dataset["implicit_distance"] < 0] = 3.0
dataset.plot(scalars="my_array", n_colors=2, clim=[1.5, 3.5])
surface boolean

Total running time of the script: (0 minutes 5.020 seconds)

Gallery generated by Sphinx-Gallery