{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create Triangulated Surface {#triangulated_surface}\n\nCreate a surface from a set of points through a Delaunay triangulation.\nThis example uses\n`pyvista.PolyDataFilters.delaunay_2d`{.interpreted-text role=\"func\"}.\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": [ "# Simple Triangulations\n\nFirst, create some points for the surface.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define a simple Gaussian surface\nn = 20\nx = np.linspace(-200, 200, num=n) + np.random.default_rng().uniform(-5, 5, size=n)\ny = np.linspace(-200, 200, num=n) + np.random.default_rng().uniform(-5, 5, size=n)\nxx, yy = np.meshgrid(x, y)\nA, b = 100, 100\nzz = A * np.exp(-0.5 * ((xx / b) ** 2.0 + (yy / b) ** 2.0))\n\n# Get the points as a 2D NumPy array (N by 3)\npoints = np.c_[xx.reshape(-1), yy.reshape(-1), zz.reshape(-1)]\npoints[0:5, :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now use those points to create a point cloud PyVista data object. This\nwill be encompassed in a `pyvista.PolyData`{.interpreted-text\nrole=\"class\"} object.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# simply pass the numpy points to the PolyData constructor\ncloud = pv.PolyData(points)\ncloud.plot(point_size=15)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have a PyVista data structure of the points, we can perform\na triangulation to turn those boring discrete points into a connected\nsurface.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "surf = cloud.delaunay_2d()\nsurf.plot(show_edges=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Masked Triangulations\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(10, dtype=float)\nxx, yy, zz = np.meshgrid(x, x, [0])\npoints = np.column_stack((xx.ravel(order=\"F\"), yy.ravel(order=\"F\"), zz.ravel(order=\"F\")))\n# Perturb the points\npoints[:, 0] += np.random.default_rng().random(len(points)) * 0.3\npoints[:, 1] += np.random.default_rng().random(len(points)) * 0.3\n# Create the point cloud mesh to triangulate from the coordinates\ncloud = pv.PolyData(points)\ncloud" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the triangulation on these points\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "surf = cloud.delaunay_2d()\nsurf.plot(cpos=\"xy\", show_edges=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that some of the outer edges are unconstrained and the\ntriangulation added unwanted triangles. We can mitigate that with the\n`alpha` parameter.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "surf = cloud.delaunay_2d(alpha=1.0)\nsurf.plot(cpos=\"xy\", show_edges=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could also add a polygon to ignore during the triangulation via the\n`edge_source` parameter.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define a polygonal hole with a clockwise polygon\nids = [22, 23, 24, 25, 35, 45, 44, 43, 42, 32]\n\n# Create a polydata to store the boundary\npolygon = pv.PolyData()\n# Make sure it has the same points as the mesh being triangulated\npolygon.points = points\n# But only has faces in regions to ignore\npolygon.faces = np.insert(ids, 0, len(ids))\n\nsurf = cloud.delaunay_2d(alpha=1.0, edge_source=polygon)\n\np = pv.Plotter()\np.add_mesh(surf, show_edges=True)\np.add_mesh(polygon, color=\"red\", opacity=0.5)\np.show(cpos=\"xy\")" ] } ], "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 }