Note
Click here to download the full example code
Boolean OperationsΒΆ
Perform boolean operations with closed surfaces (intersect, cut, etc.).
Boolean/topological operations (intersect, cut, etc.) methods are implemented
for pyvista.PolyData
mesh types only and are accessible directly from
any pyvista.PolyData
mesh. Check out pyvista.PolyDataFilters
and take a look at the following filters:
For merging, the +
operator can be used between any two meshes in PyVista
which simply calls the .merge()
filter to combine any two meshes.
Similarly, the -
operator can be used between any two pyvista.PolyData
meshes in PyVista to cut the first mesh by the second.
# sphinx_gallery_thumbnail_number = 6
import pyvista as pv
import numpy as np
def make_cube():
x = np.linspace(-0.5, 0.5, 25)
grid = pv.StructuredGrid(*np.meshgrid(x, x, x))
return grid.extract_surface().triangulate()
# Create to example PolyData meshes for boolean operations
sphere = pv.Sphere(radius=0.65, center=(0, 0, 0))
cube = make_cube()
p = pv.Plotter()
p.add_mesh(sphere, color="yellow", opacity=0.5, show_edges=True)
p.add_mesh(cube, color="royalblue", opacity=0.5, show_edges=True)
p.show()

Out:
[(2.5043836950059895, 2.5043836950059895, 2.5043836950059895),
(0.0, 0.0, 0.0),
(0.0, 0.0, 1.0)]
Boolean AddΒΆ
Add all of the two meshes together using the
pyvista.PolyDataFilters.boolean_add()
filter or the +
operator.
Order of operations does not matter for boolean add as the entirety of both meshes are appended together.
add = sphere + cube
add.plot(opacity=0.5, color=True, show_edges=True)

Out:
[(2.5043836950059895, 2.5043836950059895, 2.5043836950059895),
(0.0, 0.0, 0.0),
(0.0, 0.0, 1.0)]
Boolean CutΒΆ
Perform a boolean cut of a
using b
with the
pyvista.PolyDataFilters.boolean_cut()
filter or the -
operator
since both meshes are pyvista.PolyData
.
Order of operations does not matter for boolean cut.
cut = cube - sphere
p = pv.Plotter()
p.add_mesh(cut, opacity=0.5, show_edges=True, color=True)
p.show()

Out:
[(2.5043836950059895, 2.5043836950059895, 2.5043836950059895),
(0.0, 0.0, 0.0),
(0.0, 0.0, 1.0)]
Boolean DifferenceΒΆ
Combine two meshes and retains only the volume in common between the meshes
using the pyvista.PolyDataFilters.boolean_difference()
method.
Note that the order of operations for a boolean difference will affect the results.
diff = sphere.boolean_difference(cube)
p = pv.Plotter()
p.add_mesh(diff, opacity=0.5, show_edges=True, color=True)
p.show()

Out:
[(1.9318516525781368, 1.9318516525781368, 1.9318516525781368),
(0.0, 0.0, 0.0),
(0.0, 0.0, 1.0)]
diff = cube.boolean_difference(sphere)
p = pv.Plotter()
p.add_mesh(diff, opacity=0.5, show_edges=True, color=True)
p.show()

Out:
[(2.5043836950059895, 2.5043836950059895, 2.5043836950059895),
(0.0, 0.0, 0.0),
(0.0, 0.0, 1.0)]
Boolean UnionΒΆ
Combine two meshes and attempts to create a manifold mesh using the
pyvista.PolyDataFilters.boolean_union()
method.
Order of operations does not matter for boolean union.
union = sphere.boolean_union(cube)
p = pv.Plotter()
p.add_mesh(union, opacity=0.5, show_edges=True, color=True)
p.show()

Out:
[(1.9318516525781368, 1.9318516525781368, 1.9318516525781368),
(0.0, 0.0, 0.0),
(0.0, 0.0, 1.0)]
Total running time of the script: ( 0 minutes 14.360 seconds)