Create Triangulated SurfaceΒΆ

Create a surface from a set of points through a Delaunay triangulation.

# sphinx_gallery_thumbnail_number = 2
import pyvista as pv
import numpy as np

First, create some points for the surface.

# 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)]
print(points[0:5, :])

Out:

[[-197.89290336 -196.16615009    2.06067212]
 [-183.65316499 -196.16615009    2.70388063]
 [-156.22483319 -196.16615009    4.30941575]
 [-136.77527123 -196.16615009    5.73013175]
 [-117.35836105 -196.16615009    7.33357426]]

Now use those points to create a point cloud PyVista data object. This will be encompassed in a pyvista.PolyData object.

# simply pass the numpy points to the PolyData constructor
cloud = pv.PolyData(points)
cloud.plot(point_size=15)
../../_images/sphx_glr_create-tri-surface_001.png

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.

surf = cloud.delaunay_2d()
surf.plot(show_edges=True)
../../_images/sphx_glr_create-tri-surface_002.png

Total running time of the script: ( 0 minutes 1.709 seconds)

Gallery generated by Sphinx-Gallery