.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorial/02_mesh/exercises/d_create-tri-surface.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorial_02_mesh_exercises_d_create-tri-surface.py: .. _triangulated_surface_exercises: Create Triangulated Surface ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Create a surface from a set of points through a Delaunay triangulation. .. note:: We will use a filter from PyVista to perform our triangulation: `delaunay_2d `_. .. GENERATED FROM PYTHON SOURCE LINES 12-16 .. code-block:: Python import numpy as np import pyvista as pv .. GENERATED FROM PYTHON SOURCE LINES 17-21 Simple Triangulations +++++++++++++++++++++ First, create some points for the surface. .. GENERATED FROM PYTHON SOURCE LINES 21-34 .. code-block:: Python # Define a simple Gaussian surface n = 20 x = np.linspace(-200, 200, num=n) + np.random.uniform(-5, 5, size=n) y = np.linspace(-200, 200, num=n) + np.random.uniform(-5, 5, size=n) xx, yy = np.meshgrid(x, y) A, b = 100, 100 zz = A * np.exp(-0.5 * ((xx / b) ** 2.0 + (yy / b) ** 2.0)) # Get the points as a 2D NumPy array (N by 3) points = np.c_[xx.reshape(-1), yy.reshape(-1), zz.reshape(-1)] points[0:5, :] .. GENERATED FROM PYTHON SOURCE LINES 35-37 Now use those points to create a point cloud PyVista data object. This will be encompassed in a :class:`pyvista.PolyData` object. .. GENERATED FROM PYTHON SOURCE LINES 37-42 .. code-block:: Python # simply pass the numpy points to the PolyData constructor cloud = ... cloud.plot(point_size=15) .. GENERATED FROM PYTHON SOURCE LINES 43-46 Now that we have a PyVista data structure of the points, we can perform a triangulation to turn those boring discrete points into a connected surface. See :func:`pyvista.UnstructuredGridFilters.delaunay_2d`. .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: Python help(cloud.delaunay_2d) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Apply the ``delaunay_2d`` filter. .. GENERATED FROM PYTHON SOURCE LINES 50-57 .. code-block:: Python surf = ... # And plot it with edges shown surf.plot(show_edges=True) .. GENERATED FROM PYTHON SOURCE LINES 58-60 Clean Edges & Triangulations ++++++++++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 60-73 .. code-block:: Python # Create the points to triangulate x = np.arange(10, dtype=float) xx, yy, zz = np.meshgrid(x, x, [0]) points = np.column_stack((xx.ravel(order="F"), yy.ravel(order="F"), zz.ravel(order="F"))) # Perturb the points points[:, 0] += np.random.rand(len(points)) * 0.3 points[:, 1] += np.random.rand(len(points)) * 0.3 # Create the point cloud mesh to triangulate from the coordinates cloud = pv.PolyData(points) cloud .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python cloud.plot(cpos="xy") .. GENERATED FROM PYTHON SOURCE LINES 77-78 Run the triangulation on these points .. GENERATED FROM PYTHON SOURCE LINES 78-81 .. code-block:: Python surf = cloud.delaunay_2d() surf.plot(cpos="xy", show_edges=True) .. GENERATED FROM PYTHON SOURCE LINES 82-84 Note that some of the outer edges are unconstrained and the triangulation added unwanted triangles. We can mitigate that with the ``alpha`` parameter. .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: Python surf = cloud.delaunay_2d(alpha=...) surf.plot(cpos="xy", show_edges=True) .. GENERATED FROM PYTHON SOURCE LINES 88-95 .. raw:: html