.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "pyvista/examples/surface-boolean.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_pyvista_examples_surface-boolean.py: 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 .. GENERATED FROM PYTHON SOURCE LINES 9-12 .. code-block:: default import numpy as np import pyvista as pv .. GENERATED FROM PYTHON SOURCE LINES 13-14 Make a gridded volume .. GENERATED FROM PYTHON SOURCE LINES 14-18 .. code-block:: default n = 51 xx = yy = zz = 1 - np.linspace(0, n, n) * 2 / (n - 1) dataset = pv.RectilinearGrid(xx, yy, zz) .. GENERATED FROM PYTHON SOURCE LINES 19-20 Define a surface within the volume .. GENERATED FROM PYTHON SOURCE LINES 20-22 .. code-block:: default surface = pv.Cone(direction=(0, 0, -1), height=3.0, radius=1, resolution=50, capping=False) .. GENERATED FROM PYTHON SOURCE LINES 23-24 Preview the problem .. GENERATED FROM PYTHON SOURCE LINES 24-30 .. code-block:: default 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() .. image-sg:: /pyvista/examples/images/sphx_glr_surface-boolean_001.png :alt: surface boolean :srcset: /pyvista/examples/images/sphx_glr_surface-boolean_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 31-33 Compute an implicit distance inside the volume using this surface, then inject new data arrays .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: default dataset.compute_implicit_distance(surface, inplace=True) .. raw:: html
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


.. GENERATED FROM PYTHON SOURCE LINES 38-41 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 .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: default 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]) .. image-sg:: /pyvista/examples/images/sphx_glr_surface-boolean_002.png :alt: surface boolean :srcset: /pyvista/examples/images/sphx_glr_surface-boolean_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.020 seconds) .. _sphx_glr_download_pyvista_examples_surface-boolean.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: surface-boolean.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: surface-boolean.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_