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

Simple Traingulations

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([[-199.69425524, -201.16940394,    1.80006712],
       [-176.23554167, -201.16940394,    2.79760984],
       [-158.07398616, -201.16940394,    3.78991836],
       [-132.67113278, -201.16940394,    5.48288958],
       [-117.96425843, -201.16940394,    6.59251832]])

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)
create tri surface

Out:

[(648.6117492707182, 647.5221116373073, 697.0317971729071),
 (2.1047299897085168, 1.0150923562975862, 50.524777891897415),
 (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)
create tri surface

Out:

[(648.6117492707182, 647.5221116373073, 697.0317971729071),
 (2.1047299897085168, 1.0150923562975862, 50.524777891897415),
 (0.0, 0.0, 1.0)]

Masked Triangulations

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
PolyDataInformation
N Cells100
N Points100
X Bounds9.242e-03, 9.282e+00
Y Bounds8.310e-03, 9.271e+00
Z Bounds0.000e+00, 0.000e+00
N Arrays0


Run the triangulation on these points

surf = cloud.delaunay_2d()
surf.plot(cpos="xy", show_edges=True)
create tri surface

Out:

[(4.645792454914505, 4.639644458980621, 25.3203365333097),
 (4.645792454914505, 4.639644458980621, 0.0),
 (0.0, 1.0, 0.0)]

Note that some of the outer edges are unconstrained and the triangulation added unwanted triangles. We cn mitigate that with the alpha parameter.

surf = cloud.delaunay_2d(alpha=1.0)
surf.plot(cpos="xy", show_edges=True)
create tri surface

Out:

[(4.645792454914505, 4.639644458980621, 25.3203365333097),
 (4.645792454914505, 4.639644458980621, 0.0),
 (0.0, 1.0, 0.0)]

We could also add a polygon to ignore during the triangulation via the edge_source parameter.

# Define a polygonal hole with a clockwise polygon
ids = [22, 23, 24, 25, 35, 45, 44, 43, 42, 32]

# Create a polydata to store the boundary
polygon = pv.PolyData()
# Make sure it has the same points as the mesh being triangulated
polygon.points = points
# But only has faces in regions to ignore
polygon.faces = np.array([len(ids),] + ids)

surf = cloud.delaunay_2d(alpha=1.0, edge_source=polygon)

p = pv.Plotter()
p.add_mesh(surf, show_edges=True)
p.add_mesh(polygon, color="red", opacity=0.5)
p.show(cpos="xy")
create tri surface

Out:

[(4.645792454914505, 4.639644458980621, 25.3203365333097),
 (4.645792454914505, 4.639644458980621, 0.0),
 (0.0, 1.0, 0.0)]

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

Gallery generated by Sphinx-Gallery