{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Project points to a plane and Tessellate\n\nUsing pyvista and numpy, generate a 3D point cloud, project it to a\nplane, and tessellate it.\n\nThis demonstrates how to use\n`pyvista.UnstructuredGridFilters.delaunay_2d`{.interpreted-text\nrole=\"class\"} and a simple numpy function that projects points to a\nplane.\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": [
        "# Project Points\n\nCreate a point cloud and project it to a plane.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "num_points = 100\npoint_cloud = np.random.default_rng().random((num_points, 3))\n\n# Define a plane\norigin = [0, 0, 0]\nnormal = [0, 0, 1]\nplane = pv.Plane(center=origin, direction=normal)\n\n\ndef project_points_to_plane(points, plane_origin, plane_normal):\n    \"\"\"Project points to a plane.\"\"\"\n    vec = points - plane_origin\n    dist = np.dot(vec, plane_normal)\n    return points - np.outer(dist, plane_normal)\n\n\nprojected_points = project_points_to_plane(point_cloud, origin, normal)\n\n# Create a polydata object with projected points\npolydata = pv.PolyData(projected_points)\n\n# Mesh using delaunay_2d and pyvista\nmesh = polydata.delaunay_2d()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Visualize the Result\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Create a plane for visualization\nplane_vis = pv.Plane(\n    center=origin,\n    direction=normal,\n    i_size=2,\n    j_size=2,\n    i_resolution=10,\n    j_resolution=10,\n)\n\n# plot it\npl = pv.Plotter()\npl.add_mesh(mesh, show_edges=True, color='white', opacity=0.5, label='Tessellated mesh')\npl.add_mesh(\n    pv.PolyData(point_cloud),\n    color='red',\n    render_points_as_spheres=True,\n    point_size=10,\n    label='Points to project',\n)\npl.add_mesh(plane_vis, color='blue', opacity=0.1, label='Projection Plane')\npl.add_legend()\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
}