{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Moving Isovalue {#moving_isovalue_example}\n\nMake an animation of an isovalue through a volumetric dataset\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nimport pyvista as pv\nfrom pyvista import examples\n\nvol = examples.download_brain()\nvol"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now lets make an array of all of the isovalues for which we want to\nshow.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "values = np.linspace(5, 150, num=25)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now let\\'s create an initial isosurface that we can plot and move\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "surface = vol.contour(values[:1])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Precompute the surfaces\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "surfaces = [vol.contour([v]) for v in values]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set a single surface as the one being plotted that can be overwritten\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "surface = surfaces[0].copy()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "filename = \"isovalue.gif\"\n\nplotter = pv.Plotter(off_screen=True)\n# Open a movie file\nplotter.open_gif(filename)\n\n# Add initial mesh\nplotter.add_mesh(\n    surface,\n    opacity=0.5,\n    clim=vol.get_data_range(),\n    show_scalar_bar=False,\n)\n# Add outline for reference\nplotter.add_mesh(vol.outline_corners(), color='k')\n\nprint('Orient the view, then press \"q\" to close window and produce movie')\nplotter.camera_position = [\n    (392.9783280407326, 556.4341372317185, 235.51220650196404),\n    (88.69563012828344, 119.06774369173661, 72.61750326143748),\n    (-0.19275936948097383, -0.2218876327549124, 0.9558293278131397),\n]\n\n# initial render and do NOT close\nplotter.show(auto_close=False)\n\n# Run through each frame\nfor surf in surfaces:\n    surface.copy_from(surf)\n    plotter.write_frame()  # Write this frame\n# Run through backwards\nfor surf in surfaces[::-1]:\n    surface.copy_from(surf)\n    plotter.write_frame()  # Write this frame\n\n# Be sure to close the plotter when finished\nplotter.close()"
      ]
    }
  ],
  "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
}