{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Plane Widget {#plane_widget_example}\n\nThe plane widget can be enabled and disabled by the\n`pyvista.Plotter.add_plane_widget`{.interpreted-text role=\"func\"} and\n`pyvista.Plotter.clear_plane_widgets`{.interpreted-text role=\"func\"}\nmethods respectively. As with all widgets, you must provide a custom\ncallback method to utilize that plane. Considering that planes are most\ncommonly used for clipping and slicing meshes, we have included two\nhelper methods for doing those tasks!\n\nLet\\'s use a plane to clip a mesh:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pyvista as pv\nfrom pyvista import examples\n\nvol = examples.download_brain()\n\npl = pv.Plotter()\npl.add_mesh_clip_plane(vol)\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "After interacting with the scene, the clipped mesh is available as:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl.plane_clipped_meshes"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And here is a screen capture of a user interacting with this\n\n![image](../../images/gifs/plane-clip.gif)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Or you could slice a mesh using the plane widget:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl = pv.Plotter()\npl.add_mesh_slice(vol)\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "After interacting with the scene, the slice is available as:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl.plane_sliced_meshes"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And here is a screen capture of a user interacting with this\n\n![image](../../images/gifs/plane-slice.gif)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Or you could leverage the plane widget for some custom task like\nglyphing a vector field along that plane. Note that we have to pass a\n`name` when calling `add_mesh` to ensure that there is only one set of\nglyphs plotted at a time.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pyvista as pv\nfrom pyvista import examples\n\nmesh = examples.download_carotid()\n\npl = pv.Plotter()\npl.add_mesh(mesh.contour(8).extract_largest(), opacity=0.5)\n\n\ndef my_plane_func(normal, origin) -> None:\n    slc = mesh.slice(normal=normal, origin=origin)\n    arrows = slc.glyph(orient=\"vectors\", scale=\"scalars\", factor=0.01)\n    pl.add_mesh(arrows, name=\"arrows\")\n\n\npl.add_plane_widget(my_plane_func)\npl.show_grid()\npl.add_axes()\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And here is a screen capture of a user interacting with this\n\n![image](../../images/gifs/plane-glyph.gif)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Further, a user can disable the arrow vector by setting the\n`normal_rotation` argument to `False`. For example, here we\nprogrammatically set the normal vector on which we want to translate the\nplane and we disable the arrow to prevent its rotation.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl = pv.Plotter()\npl.add_mesh_slice(vol, normal=(1, 1, 1), normal_rotation=False)\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The vector is also forcibly disabled anytime the `assign_to_axis`\nargument is set.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl = pv.Plotter()\npl.add_mesh_slice(vol, assign_to_axis=\"z\")\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Additionally, users can modify the interaction event that triggers the\ncallback functions handled by the different plane widget helpers through\nthe `interaction_event` keyword argument when available. For example, we\ncan have continuous slicing by using the `InteractionEvent` observer.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import vtk\n\npl = pv.Plotter()\npl.add_mesh_slice(vol, assign_to_axis=\"z\", interaction_event=vtk.vtkCommand.InteractionEvent)\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And here is a screen capture of a user interacting with this\ncontinuously via the `InteractionEvent` observer:\n\n![image](../../images/gifs/plane-slice-continuous.gif)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "```{=html}\n<center>\n  <a target=\"_blank\" href=\"https://colab.research.google.com/github/pyvista/pyvista-tutorial/blob/gh-pages/notebooks/tutorial/08_widgets/e_plane-widget.ipynb\">\n    <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/ width=\"150px\">\n  </a>\n</center>\n```\n"
      ]
    }
  ],
  "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.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}