{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Extract Cells Inside Surface\n\nExtract the cells in a mesh that exist inside or outside a closed\nsurface of another mesh\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pyvista as pv\nfrom pyvista import examples\n\nmesh = examples.download_cow()\n\ncpos = [(13.0, 7.6, -13.85), (0.44, -0.4, -0.37), (-0.28, 0.9, 0.3)]\n\ndargs = dict(show_edges=True)\n# Rotate the mesh to have a second mesh\nrot = mesh.rotate_y(90, inplace=False)\n\np = pv.Plotter()\np.add_mesh(mesh, color=\"Crimson\", **dargs)\np.add_mesh(rot, color=\"mintcream\", opacity=0.35, **dargs)\np.camera_position = cpos\np.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Mark points inside with 1 and outside with a 0\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "select = mesh.select_enclosed_points(rot)\n\nselect"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Extract two meshes, one completely inside and one completely outside the\nenclosing surface.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "inside = select.threshold(0.5)\noutside = select.threshold(0.5, invert=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "display the results\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "p = pv.Plotter()\np.add_mesh(outside, color=\"Crimson\", **dargs)\np.add_mesh(inside, color=\"green\", **dargs)\np.add_mesh(rot, color=\"mintcream\", opacity=0.35, **dargs)\n\np.camera_position = cpos\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
}