{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Label based on Distance on Line {#distance_labels_example}\n\nCreate a spline and generate labels along the spline based on distance\nalong a spline.\n\nThis is an extension of the `create_spline_example`{.interpreted-text\nrole=\"ref\"}.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nimport pyvista as pv"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Create a spline\n\nCreate a spline using `pyvista.Spline`{.interpreted-text role=\"func\"}.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Make points along a spline\ntheta = np.linspace(-1 * np.pi, 1 * np.pi, 100)\nz = np.linspace(2, -2, 100)\nr = z**2 + 1\nx = r * np.sin(theta)\ny = r * np.cos(theta)\npoints = np.column_stack((x, y, z))\n\n# Create a spline. This automatically computes arc_length, which is the\n# distance along the line.\nspline = pv.Spline(points, 1000)\nspline.point_data"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Determine the coordinates matching distance along a spline\n\nHere we write a simple function that gets the closest point matching a\ndistance along a spline and then generate labels for those points.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def get_point_along_spline(distance):\n    \"\"\"Return the closest point on the spline given a length along the spline.\"\"\"\n    idx = np.argmin(np.abs(spline.point_data['arc_length'] - distance))\n    return spline.points[idx]\n\n\n# distances along the spline we're interested in\ndists = [0, 4, 8, 11]\n\n# make labels\nlabels = []\nlabel_points = []\nfor dist in dists:\n    point = get_point_along_spline(dist)\n    labels.append(f'Dist {dist}: ({point[0]:.2f}, {point[1]:.2f}, {point[2]:.2f})')\n    label_points.append(point)\n\nlabels"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Plot with Labels\n\nPlot the spline with labeled points\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl = pv.Plotter()\npl.add_mesh(spline, scalars='arc_length', render_lines_as_tubes=True, line_width=10)\npl.add_point_labels(\n    label_points,\n    labels,\n    always_visible=True,\n    point_size=20,\n    render_points_as_spheres=True,\n)\npl.show_bounds()\npl.show_axes()\npl.camera_position = 'xz'\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
}