{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Clipping with Planes & Boxes {#clip_with_plane_box_example}\n\nClip/cut any dataset using planes or boxes.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pyvista as pv\nfrom pyvista import examples"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Clip with Plane\n\nClip any dataset by a user defined plane using the\n`pyvista.DataSetFilters.clip`{.interpreted-text role=\"func\"} filter\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dataset = examples.download_bunny_coarse()\nclipped = dataset.clip('y', invert=False)\n\np = pv.Plotter()\np.add_mesh(dataset, style='wireframe', color='blue', label='Input')\np.add_mesh(clipped, label='Clipped')\np.add_legend()\np.camera_position = [(0.24, 0.32, 0.7), (0.02, 0.03, -0.02), (-0.12, 0.93, -0.34)]\np.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Clip with Bounds\n\nClip any dataset by a set of XYZ bounds using the\n`pyvista.DataSetFilters.clip_box`{.interpreted-text role=\"func\"} filter.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dataset = examples.download_office()\n\nbounds = [2, 4.5, 2, 4.5, 1, 3]\nclipped = dataset.clip_box(bounds)\n\np = pv.Plotter()\np.add_mesh(dataset, style='wireframe', color='blue', label='Input')\np.add_mesh(clipped, label='Clipped')\np.add_legend()\np.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Clip with Rotated Box\n\nClip any dataset by an arbitrarily rotated solid box using the\n`pyvista.DataSetFilters.clip_box`{.interpreted-text role=\"func\"} filter.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mesh = examples.load_airplane()\n\n# Use `pv.Box()` or `pv.Cube()` to create a region of interest\nroi = pv.Cube(center=(0.9e3, 0.2e3, mesh.center[2]), x_length=500, y_length=500, z_length=500)\nroi.rotate_z(33, inplace=True)\n\np = pv.Plotter()\np.add_mesh(roi, opacity=0.75, color=\"red\")\np.add_mesh(mesh, opacity=0.5)\np.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run the box clipping algorithm\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "extracted = mesh.clip_box(roi, invert=False)\n\np = pv.Plotter(shape=(1, 2))\np.add_mesh(roi, opacity=0.75, color=\"red\")\np.add_mesh(mesh)\np.subplot(0, 1)\np.add_mesh(extracted)\np.add_mesh(roi, opacity=0.75, color=\"red\")\np.link_views()\np.view_isometric()\np.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Crinkled Clipping\n\nCrinkled clipping is useful if you don\\'t want the clip filter to truly\nclip cells on the boundary, but want to preserve the input cell\nstructure and to pass the entire cell on through the boundary.\n\nThis option is available for\n`pyvista.DataSetFilters.clip`{.interpreted-text role=\"func\"},\n`pyvista.DataSetFilters.clip_box`{.interpreted-text role=\"func\"}, and\n`pyvista.DataSetFilters.clip_surface`{.interpreted-text role=\"func\"},\nbut not available when clipping by scalar in\n`pyvista.DataSetFilters.clip_scalar`{.interpreted-text role=\"func\"}.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Input mesh\nmesh = pv.Wavelet()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define clipping plane\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "normal = (1, 1, 1)\nplane = pv.Plane(i_size=30, j_size=30, direction=normal)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Perform a standard clip\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "clipped = mesh.clip(normal=normal)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Perform a crinkled clip\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "crinkled = mesh.clip(normal=normal, crinkle=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot comparison\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "p = pv.Plotter(shape=(1, 2))\np.add_mesh(clipped, show_edges=True)\np.add_mesh(plane.extract_feature_edges(), color='r')\np.subplot(0, 1)\np.add_mesh(crinkled, show_edges=True)\np.add_mesh(plane.extract_feature_edges(), color='r')\np.link_views()\np.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}