{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Subdivide Cells {#subdivide_example}\n\nIncrease the number of triangles in a single, connected triangular mesh.\n\nThe `pyvista.PolyDataFilters.subdivide`{.interpreted-text role=\"func\"}\nfilter utilizes three different subdivision algorithms to subdivide a\nmesh\\'s cells: [butterfly]{.title-ref}, [loop]{.title-ref}, or\n[linear]{.title-ref}.\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": [
        "First, let\\'s load a **triangulated** mesh to subdivide. We can use the\n`pyvista.DataSetFilters.triangulate`{.interpreted-text role=\"func\"}\nfilter to ensure the mesh we are using is purely triangles.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mesh = examples.download_bunny_coarse().triangulate()\n\ncpos = [\n    (-0.02788175062966399, 0.19293295656233056, 0.4334449972621349),\n    (-0.053260899930287015, 0.08881197167521734, -9.016948161029588e-05),\n    (-0.10170607813337212, 0.9686438023715356, -0.22668272496584665),\n]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, lets do a few subdivisions with the mesh and compare the results.\nBelow is a helper function to make a comparison plot of thee different\nsubdivisions.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def plot_subdivisions(mesh, a, b):\n    display_args = dict(show_edges=True, color=True)\n    p = pv.Plotter(shape=(3, 3))\n\n    for i in range(3):\n        p.subplot(i, 0)\n        p.add_mesh(mesh, **display_args)\n        p.add_text(\"Original Mesh\")\n\n    def row_plot(row, subfilter):\n        subs = [a, b]\n        for i in range(2):\n            p.subplot(row, i + 1)\n            p.add_mesh(mesh.subdivide(subs[i], subfilter=subfilter), **display_args)\n            p.add_text(f\"{subfilter} subdivision of {subs[i]}\")\n\n    row_plot(0, \"linear\")\n    row_plot(1, \"butterfly\")\n    row_plot(2, \"loop\")\n\n    p.link_views()\n    p.view_isometric()\n    return p"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run the subdivisions for 1 and 3 levels.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plotter = plot_subdivisions(mesh, 1, 3)\nplotter.camera_position = cpos\nplotter.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
}