{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Triangle Strips {#strips_example}\n\nThis example shows how to build a simple\n`pyvista.PolyData`{.interpreted-text role=\"class\"} using triangle\nstrips.\n\nTriangle strips are a more efficient way of storing the connectivity of\nadjacent triangles.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nimport pyvista as pv\n\n# Create an array of points\npoints = np.array(\n    [\n        [1.0, 0.0, 0.0],\n        [0.0, 0.0, 0.0],\n        [1.0, 1.0, 0.0],\n        [0.0, 1.0, 0.0],\n        [1.0, 2.0, 0.0],\n        [0.0, 2.0, 0.0],\n        [1.0, 3.0, 0.0],\n        [0.0, 3.0, 0.0],\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Build the connectivity of the strips\n\nThe first element is the number of points in the strip next three\nelements is the initial triangle the rest of the points is where the\nstrip extends to.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "strips = np.array([8, 0, 1, 2, 3, 4, 5, 6, 7])\n\n\n# build the mesh\nmesh = pv.PolyData(points, strips=strips)\nmesh"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Plot the triangle strips\n\nPlot the `PolyData` and include the point labels using\n`add_point_labels() <pyvista.Plotter.add_point_labels>`{.interpreted-text\nrole=\"func\"} so we can see how the PolyData is constructed using\ntriangle strips.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl = pv.Plotter()\npl.add_mesh(mesh, show_edges=True)\npl.add_point_labels(mesh.points, range(mesh.n_points))\npl.camera_position = 'yx'\npl.camera.zoom(1.2)\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Convert strips to triangles\n\nYou can convert strips to triangle faces using `triangulate\n<pyvista.DataSetFilters.triangulate>`{.interpreted-text role=\"func\"}.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "trimesh = mesh.triangulate()\ntrimesh"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can use this new `pyvista.PolyData`{.interpreted-text role=\"class\"}\nto see how VTK represents triangle strips as individual faces.\n\nSee how the faces array is much larger (\\~3x more) even for this basic\nexample even despite representing the same data.\n\n::: note\n::: title\nNote\n:::\n\nThe faces array from VTK always contains padding (the number of points\nin the face) for each face in the face array. This is the initial `3` in\nthe following face array.\n:::\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "faces = trimesh.faces.reshape(-1, 4)\nfaces"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Convert triangles to strips\n\nConvert faces from a `pyvista.PolyData`{.interpreted-text role=\"class\"}\nto strips using `strip()\n<pyvista.PolyDataFilters.strip>`{.interpreted-text role=\"func\"}. Here,\nfor demonstration purposes we convert the triangulated mesh back to a\nstripped mesh.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "restripped = trimesh.strip()\nrestripped"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The output from the `strip` filter is, as expected, identical to the\noriginal `mesh`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "restripped == mesh"
      ]
    }
  ],
  "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
}