| 1 |
""" |
| 2 |
.. _boolean_operations_example: |
| 3 |
|
| 4 |
Boolean Operations |
| 5 |
~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Perform boolean operations with closed (manifold) surfaces. |
| 8 |
|
| 9 |
Boolean/topological operations (intersect, union, difference) methods |
| 10 |
are implemented for :class:`pyvista.PolyData` mesh types only and are |
| 11 |
accessible directly from any :class:`pyvista.PolyData` mesh. Check out |
| 12 |
:class:`pyvista.PolyDataFilters` and take a look at the following |
| 13 |
filters: |
| 14 |
|
| 15 |
* :func:`pyvista.PolyDataFilters.boolean_difference` |
| 16 |
* :func:`pyvista.PolyDataFilters.boolean_union` |
| 17 |
* :func:`pyvista.PolyDataFilters.boolean_intersection` |
| 18 |
|
| 19 |
Essentially, boolean union, difference, and intersection are all the |
| 20 |
same operation. Just different parts of the objects are kept at the |
| 21 |
end. |
| 22 |
|
| 23 |
The ``-`` operator can be used between any two :class:`pyvista.PolyData` |
| 24 |
meshes in PyVista to cut the first mesh by the second. These meshes |
| 25 |
must be all triangle meshes, which you can check with |
| 26 |
:attr:`pyvista.PolyData.is_all_triangles`. |
| 27 |
|
| 28 |
.. note:: |
| 29 |
For merging, the ``+`` operator can be used between any two meshes |
| 30 |
in PyVista which simply calls the ``.merge()`` filter to combine |
| 31 |
any two meshes. This is different from the operator ``|`` in PyVista |
| 32 |
which simply calls the ``boolean_union`` filter as it simply superimposes |
| 33 |
the two meshes without performing additional calculations on the result. |
| 34 |
The ``&`` operator in PyVista simply calls the ``boolean_intersection``. |
| 35 |
|
| 36 |
.. warning:: |
| 37 |
If your boolean operations don't react the way you think they |
| 38 |
should (i.e. the wrong parts disappear), one of your meshes |
| 39 |
probably has its normals pointing inward. Use |
| 40 |
:func:`pyvista.PolyDataFilters.plot_normals` to visualize the normals. |
| 41 |
|
| 42 |
|
| 43 |
""" |
| 44 |
|
| 45 |
# sphinx_gallery_thumbnail_number = 6 |
| 46 |
import pyvista as pv |
| 47 |
|
| 48 |
sphere_a = pv.Sphere() |
| 49 |
sphere_b = pv.Sphere(center=(0.5, 0, 0)) |
| 50 |
|
| 51 |
|
| 52 |
# %% |
| 53 |
# Boolean Union |
| 54 |
# +++++++++++++ |
| 55 |
# |
| 56 |
# Perform a boolean union of ``A`` and ``B`` using the |
| 57 |
# :func:`pyvista.PolyDataFilters.boolean_union` filter. |
| 58 |
# |
| 59 |
# The union of two manifold meshes ``A`` and ``B`` is the mesh |
| 60 |
# which is in ``A``, in ``B``, or in both ``A`` and ``B``. |
| 61 |
# |
| 62 |
# Order of operands does not matter for boolean union (the operation is |
| 63 |
# commutative). |
| 64 |
|
| 65 |
result = sphere_a | sphere_b |
| 66 |
pl = pv.Plotter() |
| 67 |
_ = pl.add_mesh(sphere_a, color='r', style='wireframe', line_width=3) |
| 68 |
_ = pl.add_mesh(sphere_b, color='b', style='wireframe', line_width=3) |
| 69 |
_ = pl.add_mesh(result, color='lightblue') |
| 70 |
pl.camera_position = 'xz' |
| 71 |
pl.show() |
| 72 |
|
| 73 |
|
| 74 |
# %% |
| 75 |
# Boolean Difference |
| 76 |
# ++++++++++++++++++ |
| 77 |
# |
| 78 |
# Perform a boolean difference of ``A`` and ``B`` using the |
| 79 |
# :func:`pyvista.PolyDataFilters.boolean_difference` filter or the |
| 80 |
# ``-`` operator since both meshes are :class:`pyvista.PolyData`. |
| 81 |
# |
| 82 |
# The difference of two manifold meshes ``A`` and ``B`` is the volume |
| 83 |
# of the mesh in ``A`` not belonging to ``B``. |
| 84 |
# |
| 85 |
# Order of operands matters for boolean difference. |
| 86 |
|
| 87 |
result = sphere_a - sphere_b |
| 88 |
pl = pv.Plotter() |
| 89 |
_ = pl.add_mesh(sphere_a, color='r', style='wireframe', line_width=3) |
| 90 |
_ = pl.add_mesh(sphere_b, color='b', style='wireframe', line_width=3) |
| 91 |
_ = pl.add_mesh(result, color='lightblue') |
| 92 |
pl.camera_position = 'xz' |
| 93 |
pl.show() |
| 94 |
|
| 95 |
|
| 96 |
# %% |
| 97 |
# Boolean Intersection |
| 98 |
# ++++++++++++++++++++ |
| 99 |
# |
| 100 |
# Perform a boolean intersection of ``A`` and ``B`` using the |
| 101 |
# :func:`pyvista.PolyDataFilters.boolean_intersection` filter. |
| 102 |
# |
| 103 |
# The intersection of two manifold meshes ``A`` and ``B`` is the mesh |
| 104 |
# which is the volume of ``A`` that is also in ``B``. |
| 105 |
# |
| 106 |
# Order of operands does not matter for boolean intersection (the |
| 107 |
# operation is commutative). |
| 108 |
|
| 109 |
result = sphere_a & sphere_b |
| 110 |
pl = pv.Plotter() |
| 111 |
_ = pl.add_mesh(sphere_a, color='r', style='wireframe', line_width=3) |
| 112 |
_ = pl.add_mesh(sphere_b, color='b', style='wireframe', line_width=3) |
| 113 |
_ = pl.add_mesh(result, color='lightblue') |
| 114 |
pl.camera_position = 'xz' |
| 115 |
pl.show() |
| 116 |
|
| 117 |
|
| 118 |
# %% |
| 119 |
# Behavior due to flipped faces |
| 120 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 121 |
# Note that these boolean filters behave differently depending on the |
| 122 |
# orientation of the faces. This is because the orientation determines |
| 123 |
# which parts are considered to be the "outside" or the "inside" portion |
| 124 |
# of the mesh. This example uses :meth:`~pyvista.PolyDataFilters.flip_faces` |
| 125 |
# to flip the faces. |
| 126 |
# |
| 127 |
# Boolean difference with both cube and sphere faces oriented |
| 128 |
# outward. This is the "normal" behavior. |
| 129 |
|
| 130 |
cube = pv.Cube().triangulate().subdivide(3) |
| 131 |
sphere = pv.Sphere(radius=0.6) |
| 132 |
result = cube.boolean_difference(sphere) |
| 133 |
result.plot(color='lightblue') |
| 134 |
|
| 135 |
|
| 136 |
# %% |
| 137 |
# Boolean difference with cube faces outward, sphere faces inward. |
| 138 |
|
| 139 |
cube = pv.Cube().triangulate().subdivide(3) |
| 140 |
sphere = pv.Sphere(radius=0.6).flip_faces() |
| 141 |
result = cube.boolean_difference(sphere) |
| 142 |
result.plot(color='lightblue') |
| 143 |
|
| 144 |
|
| 145 |
# %% |
| 146 |
# Boolean difference with cube faces inward, sphere faces outward. |
| 147 |
|
| 148 |
cube = pv.Cube().triangulate().subdivide(3).flip_faces() |
| 149 |
sphere = pv.Sphere(radius=0.6) |
| 150 |
result = cube.boolean_difference(sphere) |
| 151 |
result.plot(color='lightblue') |
| 152 |
|
| 153 |
|
| 154 |
# %% |
| 155 |
# Both cube and sphere faces inward. |
| 156 |
|
| 157 |
cube = pv.Cube().triangulate().subdivide(3).flip_faces() |
| 158 |
sphere = pv.Sphere(radius=0.6).flip_faces() |
| 159 |
result = cube.boolean_difference(sphere) |
| 160 |
result.plot(color='lightblue') |
| 161 |
# %% |
| 162 |
# .. tags:: filter |