.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorial/04_filters/solutions/b_clipping.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 or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorial_04_filters_solutions_b_clipping.py: .. _clip_with_plane_box_example: Clipping with Planes & Boxes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Clip/cut any dataset using using planes or boxes. .. GENERATED FROM PYTHON SOURCE LINES 9-13 .. code-block:: Python import pyvista as pv from pyvista import examples .. GENERATED FROM PYTHON SOURCE LINES 15-20 Clip with Plane +++++++++++++++ Clip any dataset by a user defined plane using the :func:`pyvista.DataSetFilters.clip` filter .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: Python dataset = examples.download_bunny_coarse() dataset .. raw:: html
HeaderData Arrays
PolyDataInformation
N Cells1000
N Points872
N Strips0
X Bounds-1.316e-01, 1.802e-01
Y Bounds-1.205e-01, 1.877e-01
Z Bounds-1.430e-01, 9.851e-02
N Arrays1
NameFieldTypeN CompMinMax
NormalsPointsfloat323-9.998e-019.977e-01


.. GENERATED FROM PYTHON SOURCE LINES 24-26 .. code-block:: Python help(dataset.clip) .. rst-class:: sphx-glr-script-out .. code-block:: none Help on method clip in module pyvista.core.filters.data_set: clip(normal='x', origin=None, invert=True, value=0.0, inplace=False, return_clipped=False, progress_bar=False, crinkle=False) method of pyvista.core.pointset.PolyData instance Clip a dataset by a plane by specifying the origin and normal. If no parameters are given the clip will occur in the center of that dataset. Parameters ---------- normal : tuple(float) or str, default: 'x' Length 3 tuple for the normal vector direction. Can also be specified as a string conventional direction such as ``'x'`` for ``(1, 0, 0)`` or ``'-x'`` for ``(-1, 0, 0)``, etc. origin : sequence[float], optional The center ``(x, y, z)`` coordinate of the plane on which the clip occurs. The default is the center of the dataset. invert : bool, default: True Flag on whether to flip/invert the clip. value : float, default: 0.0 Set the clipping value along the normal direction. inplace : bool, default: False Updates mesh in-place. return_clipped : bool, default: False Return both unclipped and clipped parts of the dataset. progress_bar : bool, default: False Display a progress bar to indicate progress. crinkle : bool, default: False Crinkle the clip by extracting the entire cells along the clip. This adds the ``"cell_ids"`` array to the ``cell_data`` attribute that tracks the original cell IDs of the original dataset. Returns ------- pyvista.PolyData or tuple(pyvista.PolyData) Clipped mesh when ``return_clipped=False``, otherwise a tuple containing the unclipped and clipped datasets. Examples -------- Clip a cube along the +X direction. ``triangulate`` is used as the cube is initially composed of quadrilateral faces and subdivide only works on triangles. >>> import pyvista as pv >>> cube = pv.Cube().triangulate().subdivide(3) >>> clipped_cube = cube.clip() >>> clipped_cube.plot() Clip a cube in the +Z direction. This leaves half a cube below the XY plane. >>> import pyvista as pv >>> cube = pv.Cube().triangulate().subdivide(3) >>> clipped_cube = cube.clip('z') >>> clipped_cube.plot() See :ref:`clip_with_surface_example` for more examples using this filter. .. GENERATED FROM PYTHON SOURCE LINES 27-28 Perform a clip with a Y axis normal .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: Python clipped = dataset.clip('y', invert=False) clipped .. raw:: html
HeaderData Arrays
PolyDataInformation
N Cells427
N Points248
N Strips0
X Bounds-1.316e-01, 1.326e-01
Y Bounds3.360e-02, 1.877e-01
Z Bounds-1.430e-01, 8.721e-02
N Arrays1
NameFieldTypeN CompMinMax
NormalsPointsfloat323-9.927e-019.977e-01


.. GENERATED FROM PYTHON SOURCE LINES 32-33 Plot the result. .. GENERATED FROM PYTHON SOURCE LINES 33-41 .. code-block:: Python p = pv.Plotter() p.add_mesh(dataset, style='wireframe', color='blue', label='Input') p.add_mesh(clipped, label='Clipped') p.add_legend() p.camera_position = [(0.24, 0.32, 0.7), (0.02, 0.03, -0.02), (-0.12, 0.93, -0.34)] p.show() .. image-sg:: /tutorial/04_filters/solutions/images/sphx_glr_b_clipping_001.png :alt: b clipping :srcset: /tutorial/04_filters/solutions/images/sphx_glr_b_clipping_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 42-49 Clip with Bounds ++++++++++++++++ Clip any dataset by a set of XYZ bounds using the :func:`pyvista.DataSetFilters.clip_box` filter. First, download an example dataset. .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python dataset = examples.download_office() .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python help(dataset.clip_box) .. rst-class:: sphx-glr-script-out .. code-block:: none Help on method clip_box in module pyvista.core.filters.data_set: clip_box(bounds=None, invert=True, factor=0.35, progress_bar=False, merge_points=True, crinkle=False) method of pyvista.core.pointset.StructuredGrid instance Clip a dataset by a bounding box defined by the bounds. If no bounds are given, a corner of the dataset bounds will be removed. Parameters ---------- bounds : sequence[float], optional Length 6 sequence of floats: ``(xmin, xmax, ymin, ymax, zmin, zmax)``. Length 3 sequence of floats: distances from the min coordinate of of the input mesh. Single float value: uniform distance from the min coordinate. Length 12 sequence of length 3 sequence of floats: a plane collection (normal, center, ...). :class:`pyvista.PolyData`: if a poly mesh is passed that represents a box with 6 faces that all form a standard box, then planes will be extracted from the box to define the clipping region. invert : bool, default: True Flag on whether to flip/invert the clip. factor : float, default: 0.35 If bounds are not given this is the factor along each axis to extract the default box. progress_bar : bool, default: False Display a progress bar to indicate progress. merge_points : bool, default: True If ``True``, coinciding points of independently defined mesh elements will be merged. crinkle : bool, default: False Crinkle the clip by extracting the entire cells along the clip. This adds the ``"cell_ids"`` array to the ``cell_data`` attribute that tracks the original cell IDs of the original dataset. Returns ------- pyvista.UnstructuredGrid Clipped dataset. Examples -------- Clip a corner of a cube. The bounds of a cube are normally ``[-0.5, 0.5, -0.5, 0.5, -0.5, 0.5]``, and this removes 1/8 of the cube's surface. >>> import pyvista as pv >>> cube = pv.Cube().triangulate().subdivide(3) >>> clipped_cube = cube.clip_box([0, 1, 0, 1, 0, 1]) >>> clipped_cube.plot() See :ref:`clip_with_plane_box_example` for more examples using this filter. .. GENERATED FROM PYTHON SOURCE LINES 55-57 Clip the dataset with a bounding box defined by the values in ``bounds`` ``(xmin, xmax, ymin, ymax, zmin, zmax)`` .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python bounds = [2, 4.5, 2, 4.5, 1, 3] clipped = dataset.clip_box(bounds) clipped .. raw:: html
HeaderData Arrays
UnstructuredGridInformation
N Cells38690
N Points9438
X Bounds1.000e-02, 4.500e+00
Y Bounds1.000e-02, 4.500e+00
Z Bounds1.000e-02, 2.500e+00
N Arrays2
NameFieldTypeN CompMinMax
scalarsPointsfloat321-3.870e+007.186e-01
vectorsPointsfloat323-6.729e-014.289e-01


.. GENERATED FROM PYTHON SOURCE LINES 62-63 Plot the original dataset and the clipped one. .. GENERATED FROM PYTHON SOURCE LINES 63-70 .. code-block:: Python p = pv.Plotter() p.add_mesh(dataset, style='wireframe', color='blue', label='Input') p.add_mesh(clipped, label='Clipped') p.add_legend() p.show() .. image-sg:: /tutorial/04_filters/solutions/images/sphx_glr_b_clipping_002.png :alt: b clipping :srcset: /tutorial/04_filters/solutions/images/sphx_glr_b_clipping_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 71-76 Clip with Rotated Box +++++++++++++++++++++ Clip any dataset by an arbitrarily rotated solid box using the :func:`pyvista.DataSetFilters.clip_box` filter. .. GENERATED FROM PYTHON SOURCE LINES 76-87 .. code-block:: Python mesh = examples.load_airplane() # Use `pv.Box()` or `pv.Cube()` to create a region of interest roi = pv.Cube(center=(0.9e3, 0.2e3, mesh.center[2]), x_length=500, y_length=500, z_length=500) roi.rotate_z(33, inplace=True) p = pv.Plotter() p.add_mesh(roi, opacity=0.75, color="red") p.add_mesh(mesh, opacity=0.5) p.show() .. image-sg:: /tutorial/04_filters/solutions/images/sphx_glr_b_clipping_003.png :alt: b clipping :srcset: /tutorial/04_filters/solutions/images/sphx_glr_b_clipping_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-89 Run the box clipping algorithm with the defined box geometry. .. GENERATED FROM PYTHON SOURCE LINES 89-101 .. code-block:: Python extracted = mesh.clip_box(roi, invert=False) p = pv.Plotter(shape=(1, 2)) p.add_mesh(roi, opacity=0.75, color="red") p.add_mesh(mesh) p.subplot(0, 1) p.add_mesh(extracted) p.add_mesh(roi, opacity=0.75, color="red") p.link_views() p.view_isometric() p.show() .. image-sg:: /tutorial/04_filters/solutions/images/sphx_glr_b_clipping_004.png :alt: b clipping :srcset: /tutorial/04_filters/solutions/images/sphx_glr_b_clipping_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 102-112 Crinkled Clipping +++++++++++++++++ Crinkled clipping is useful if you don’t want the clip filter to truly clip cells on the boundary, but want to preserve the input cell structure and to pass the entire cell on through the boundary. This option is available for :func:`pyvista.DataSetFilters.clip`, :func:`pyvista.DataSetFilters.clip_box`, and :func:`pyvista.DataSetFilters.clip_sruface`, but not available when clipping by scalar in :func:`pyvista.DataSetFilters.clip_scalar`. .. GENERATED FROM PYTHON SOURCE LINES 112-116 .. code-block:: Python # Input mesh mesh = pv.Wavelet() .. GENERATED FROM PYTHON SOURCE LINES 117-118 Define clipping plane .. GENERATED FROM PYTHON SOURCE LINES 118-121 .. code-block:: Python normal = (1, 1, 1) plane = pv.Plane(i_size=30, j_size=30, direction=normal) .. GENERATED FROM PYTHON SOURCE LINES 122-123 Perform a standard clip .. GENERATED FROM PYTHON SOURCE LINES 123-125 .. code-block:: Python clipped = mesh.clip(normal=normal) .. GENERATED FROM PYTHON SOURCE LINES 126-127 Perform a crinkled clip to compare .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: Python crinkled = mesh.clip(normal=normal, crinkle=True) .. GENERATED FROM PYTHON SOURCE LINES 130-131 Plot comparison .. GENERATED FROM PYTHON SOURCE LINES 131-140 .. code-block:: Python p = pv.Plotter(shape=(1, 2)) p.add_mesh(clipped, show_edges=True) p.add_mesh(plane.extract_feature_edges(), color='r') p.subplot(0, 1) p.add_mesh(crinkled, show_edges=True) p.add_mesh(plane.extract_feature_edges(), color='r') p.link_views() p.show() .. image-sg:: /tutorial/04_filters/solutions/images/sphx_glr_b_clipping_005.png :alt: b clipping :srcset: /tutorial/04_filters/solutions/images/sphx_glr_b_clipping_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 141-148 .. raw:: html
Open In Colab
.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.342 seconds) .. _sphx_glr_download_tutorial_04_filters_solutions_b_clipping.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/pyvista/pyvista-tutorial/gh-pages?urlpath=lab/tree/notebooks/tutorial/04_filters/solutions/b_clipping.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: b_clipping.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: b_clipping.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_