{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Plot Truss-like FEA Solution with Cylinders {#create_truss}\n\nPlot connections between points in 3D as cylinders, colored by scalars.\nLines are created in a `pyvista.PolyData`{.interpreted-text\nrole=\"class\"} and then rendered as cylinders.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nimport pyvista"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define the points and elements of the truss. Call them `nodes` here as\nit comes from finite element analysis.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nodes = [\n    [0.0, 0.0, 0.0],\n    [0.0, 1.0, 0.0],\n    [4.0, 3.0, 0.0],\n    [4.0, 0.0, 0.0],\n    [0.0, 1.0, 2.0],\n    [4.0, 1.0, 2.0],\n    [4.0, 3.0, 2.0],\n]\n\n\nedges = np.array(\n    [\n        [0, 4],\n        [1, 4],\n        [3, 4],\n        [5, 4],\n        [6, 4],\n        [3, 5],\n        [2, 5],\n        [5, 6],\n        [2, 6],\n    ],\n)\n\n# We must \"pad\" the edges to indicate to vtk how many points per edge\npadding = np.empty(edges.shape[0], int) * 2\npadding[:] = 2\nedges_w_padding = np.vstack((padding, edges.T)).T\nedges_w_padding"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the truss while rendering the lines as tubes.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mesh = pyvista.PolyData(nodes, edges_w_padding)\n\ncolors = range(edges.shape[0])\nmesh.plot(\n    scalars=colors,\n    render_lines_as_tubes=True,\n    style='wireframe',\n    line_width=10,\n    cmap='jet',\n    show_scalar_bar=False,\n    background='w',\n)"
      ]
    }
  ],
  "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
}