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([[-200.22729827, -203.85319251,    1.68676996],
       [-182.01790334, -203.85319251,    2.38891222],
       [-154.46585709, -203.85319251,    3.79763662],
       [-133.62257519, -203.85319251,    5.12748021],
       [-112.57721973, -203.85319251,    6.64378918]])

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.4390047221825, 645.1995476012571, 695.8227678878349), (-0.17345466489675232, -0.41291178582213206, 50.21030850075558), (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.4390047221825, 645.1995476012571, 695.8227678878349), (-0.17345466489675232, -0.41291178582213206, 50.21030850075558), (0.0, 0.0, 1.0)]

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

Gallery generated by Sphinx-Gallery