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

Out:

array([[-196.55509598, -202.29893336,    1.87242049],
       [-183.17992314, -202.29893336,    2.41375361],
       [-153.09332513, -202.29893336,    4.00301951],
       [-133.71188937, -202.29893336,    5.2855876 ],
       [-118.89454586, -202.29893336,    6.37339065]])

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

Out:

[(645.3649372461371, 641.3006222135634, 692.2392946566033),
 (3.241823477858418, -0.8224915547152847, 50.11618088832464),
 (0.0, 0.0, 1.0)]

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

Out:

[(645.3649372461371, 641.3006222135634, 692.2392946566033),
 (3.241823477858418, -0.8224915547152847, 50.11618088832464),
 (0.0, 0.0, 1.0)]

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

Gallery generated by Sphinx-Gallery