{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Contouring {#contouring_example}\n\nGenerate iso-lines or -surfaces for the scalars of a surface or volume.\n\n3D meshes can have 2D iso-surfaces of a scalar field extracted and 2D\nsurface meshes can have 1D iso-lines of a scalar field extracted.\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"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Iso-Lines\n\nLet\\'s extract 1D iso-lines of a scalar field from a 2D surface mesh.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mesh = examples.load_random_hills()\n\ncontours = mesh.contour()\n\npl = pv.Plotter()\npl.add_mesh(mesh, opacity=0.85)\npl.add_mesh(contours, color=\"white\", line_width=5)\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Iso-Surfaces\n\nLet\\'s extract 2D iso-surfaces of a scalar field from a 3D mesh.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mesh = examples.download_embryo()\n\ncontours = mesh.contour(np.linspace(50, 200, 5))\n\npl = pv.Plotter()\npl.add_mesh(mesh.outline(), color=\"k\")\npl.add_mesh(contours, opacity=0.25, clim=[0, 200])\npl.camera_position = [\n    (-130.99381142132086, 644.4868354828589, 163.80447435848686),\n    (125.21748748157661, 123.94368717158413, 108.83283586619626),\n    (0.2780372840777734, 0.03547871361794171, 0.9599148553609699),\n]\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Banded Contours\n\nCreate banded contours for surface meshes using\n`contour_banded() <pyvista.PolyDataFilters.contour_banded>`{.interpreted-text\nrole=\"func\"}.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mesh = examples.load_random_hills()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set number of contours and produce mesh and lines\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n_contours = 8\ncontours, edges = mesh.contour_banded(n_contours)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Also make normal vectors\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "arrows = mesh.glyph(scale=\"Normals\", orient=\"Normals\", tolerance=0.05)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Common display arguments\ndargs = dict(scalars='Elevation', n_colors=n_contours - 1, cmap='Set3')\n\npl = pv.Plotter()\npl.add_mesh(edges, line_width=5, render_lines_as_tubes=True, color='k')\npl.add_mesh(contours, **dargs)\npl.add_mesh(arrows, **dargs)\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Contours from a label map\n\nCreate labeled surfaces from 3D label maps (e.f. multi-label image\nsegmentation) using\n`contour_labeled() <pyvista.ImageDataFilters.contour_labeled>`{.interpreted-text\nrole=\"func\"}. Requires VTK version 9.3\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "if pv.vtk_version_info >= (9, 3):\n    label_map = pv.examples.download_frog_tissue()\n    mesh = label_map.contour_labeled()\n    mesh.plot(cmap=\"glasbey_warm\", cpos=\"yx\", show_scalar_bar=False)"
      ]
    }
  ],
  "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
}