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 pyvista as pv
import numpy as np
from pyvista import _vtk as vtk

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

Out:

[(4.622235903444128, 4.622235903444128, 4.642235903444129),
 (-0.020000000000000018, -0.020000000000000018, 0.0),
 (0.0, 0.0, 1.0)]

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

Out:

[(3.9209773712593994, 3.9209773712593994, 3.9209773712593994),
 (-0.020000000000000018, -0.020000000000000018, -0.020000000000000018),
 (0.0, 0.0, 1.0)]

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

Gallery generated by Sphinx-Gallery