{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import subprocess\nimport sys\n\nif \"google.colab\" in sys.modules:\n subprocess.run(\"apt-get update\", shell=True, check=True)\n subprocess.run(\"apt-get install -qq xvfb libgl1-mesa-glx\", shell=True, check=True)\n subprocess.run(\"pip install pyvista[all] -qq\", shell=True, check=True)\n\n import pyvista as pv\n\n # Seems that only static plotting is supported by colab at the moment\n pv.global_theme.jupyter_backend = \"static\"\n pv.global_theme.notebook = True\n pv.start_xvfb()\nelse:\n %matplotlib inline\n from pyvista import set_plot_theme\n\n set_plot_theme(\"document\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clipping with Planes & Boxes\n============================\n\nClip/cut any dataset using 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===============\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()\ndataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "help(dataset.clip)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perform a clip with a Y axis normal\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "clipped = ...\nclipped" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot the result.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = 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================\n\nClip any dataset by a set of XYZ bounds using the\n`pyvista.DataSetFilters.clip_box`{.interpreted-text role=\"func\"} filter.\n\nFirst, download an example dataset.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dataset = examples.download_office()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "help(dataset.clip_box)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clip the dataset with a bounding box defined by the values in `bounds`\n`(xmin, xmax, ymin, ymax, zmin, zmax)`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "bounds = [2, 4.5, 2, 4.5, 1, 3]\nclipped = ...\nclipped" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot the original dataset and the clipped one.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = 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=====================\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 with the defined box geometry.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "extracted = ...\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=================\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_sruface`{.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 to compare\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "crinkled = mesh.clip(..., normal=normal)" ] }, { "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()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{=html}\n
\n \n \"Open\n \n
\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.10.14" } }, "nbformat": 4, "nbformat_minor": 0 }