{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Unstructured Grid with Polyhedra {#polyhedron_example}\n\nThis example shows how to build a simple\n`pyvista.UnstructuredGrid`{.interpreted-text role=\"class\"} using\npolyhedra. We will be using VTK types to determine which type of cells\nwe are building. A list of cell types is given in\n`pyvista.CellType`{.interpreted-text role=\"class\"}.\n\nFirst, we import the required libraries.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pyvista as pv"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Define Points\n\nWe will mix several cells in one grid for this example. Here we create\nthe points that will define each cell.\n\n::: note\n::: title\nNote\n:::\n\nIt is not necessary that each cell has an isolated set of points. This\nhas been done here to create isolated cells for this example.\n:::\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "quad_points = [\n    [0.0, 0.0, 0.0],  # 0\n    [0.0, 0.01, 0.0],  # 1\n    [0.01, 0.01, 0.0],  # 2\n    [0.01, 0.0, 0.0],  # 3\n]\npolygon_points = [\n    [0.02, 0.0, 0.0],  # 4\n    [0.02, 0.01, 0.0],  # 5\n    [0.03, 0.01, 0.0],  # 6\n    [0.035, 0.005, 0.0],  # 7\n    [0.03, 0.0, 0.0],  # 8\n]\nhexa_points = [\n    [0.0, 0.0, 0.02],  # 9\n    [0.0, 0.01, 0.02],  # 10\n    [0.01, 0.01, 0.02],  # 11\n    [0.01, 0.0, 0.02],  # 12\n    [0.0, 0.0, 0.03],  # 13\n    [0.0, 0.01, 0.03],  # 14\n    [0.01, 0.01, 0.03],  # 15\n    [0.01, 0.0, 0.03],  # 16\n]\npolyhedron_points = [\n    [0.02, 0.0, 0.02],  # 17\n    [0.02, 0.01, 0.02],  # 18\n    [0.03, 0.01, 0.02],  # 19\n    [0.035, 0.005, 0.02],  # 20\n    [0.03, 0.0, 0.02],  # 21\n    [0.02, 0.0, 0.03],  # 22\n    [0.02, 0.01, 0.03],  # 23\n    [0.03, 0.01, 0.03],  # 24\n    [0.035, 0.005, 0.03],  # 25\n    [0.03, 0.0, 0.03],  # 26\n]\npoints = quad_points + polygon_points + hexa_points + polyhedron_points"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Cell connectivity\n\nConnectivity describes the indices of the points to compose each cell.\nThe first item in each cell\\'s connectivity is the number of items the\ncell will have. For example, a quad cell is composed of points\n`[0, 1, 2, 3]` and totaling 4 points, therefore `[4, 0, 1, 2, 3]`\ndescribes its connectivity.\n\n::: note\n::: title\nNote\n:::\n\nThis example uses lists for simplicity, but internally PyVista converts\nthese lists to a `numpy.ndarray`{.interpreted-text role=\"class\"} with\n`dtype=pyvista.ID_TYPE` and passes it to VTK.\n:::\n\nThe same approach can be applied to all the other cell types.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "quad = [4, 0, 1, 2, 3]\npolygon = [5, 4, 5, 6, 7, 8]\nhexa = [8, 9, 10, 11, 12, 13, 14, 15, 16]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Polyhedron connectivity array\n\nThe connectivity array of polyhedra is defined differently from the rest\nof the cell types. For polyhedra, we need to set the faces with the\nfollowing format:\n\n`[NItems, NFaces, Face0NPoints, Face0Point0, Face0Point1..., Face0PointN-1, Face1NPoints, ...]`\n\nWhere:\n\n-   `NItems` refers to the total number of items in the list needed to\n    describe the polyhedron.\n-   `NFaces` is the number of faces the polyhedron will have.\n-   `Face0NPoints` is the number of points the first face will have.\n-   `Face0Point0...Face0PointN-1` are each of the points that describe\n    `face0`.\n\nIn `polyhedron_connectivity`, the first item is `NFaces`. `NItems` is\nadded to `polyhedron`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "polyhedron_connectivity = [\n    # NItems will go here\n    7,  # number of faces\n    5,  # number of points in face0\n    17,  # point index 0\n    18,  # point index 1\n    19,  # point index 2\n    20,  # point index 3\n    21,  # point index 4\n    4,  # number of points in face1\n    17,  # point index ...\n    18,\n    23,\n    22,\n    4,\n    17,\n    21,\n    26,\n    22,\n    4,\n    21,\n    26,\n    25,\n    20,\n    4,\n    20,\n    25,\n    24,\n    19,\n    4,\n    19,\n    24,\n    23,\n    18,\n    5,\n    22,\n    23,\n    24,\n    25,\n    26,\n]\n\n# note how we retroactively add NItems\npolyhedron = [len(polyhedron_connectivity), *polyhedron_connectivity]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Cells array\n\nNow we build the input cells array for the\n`pyvista.UnstructuredGrid`{.interpreted-text role=\"class\"}. Here, we\njoin all cells in a flat list. Internally, the `NItems` previously\ndescribed is used to determine which nodes belong to which cells.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "cells = quad + polygon + hexa + polyhedron"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Cell types\n\nWe need to specify the cell types for each of the cells we define in the\ncells array.\n\nThe number of items in this list must match the number of cells in the\nconnectivity array.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "celltypes = [pv.CellType.QUAD, pv.CellType.POLYGON, pv.CellType.HEXAHEDRON, pv.CellType.POLYHEDRON]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Create the grid\n\nTo create the grid, we use the cells array we built, the cell types, and\nthe points that describe the faces.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "grid = pv.UnstructuredGrid(cells, celltypes, points)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Plot the mesh\n\nFinally, we can plot the grid we\\'ve created. Label each cell at its\ncell center for clarity.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl = pv.Plotter()\npl.show_axes()\npl.add_mesh(grid, show_edges=True, line_width=5)\npl.add_point_labels(\n    grid.cell_centers().points,\n    ['QUAD', 'POLYGON', 'HEXAHEDRON', 'POLYHEDRON'],\n    always_visible=True,\n    font_size=20,\n)\npl.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
}