Python triangle library
The `triangle` library provides Python bindings to Jonathan Shewchuk's robust C library for 2D Delaunay triangulation and constrained Delaunay triangulation, as well as high-quality mesh generation. It processes inputs like sets of points, segments, and holes to generate triangulations and meshes. The current version is 20250106, with a release cadence that appears to be on an as-needed basis, indicated by its date-based versioning.
Common errors
-
KeyError: 'vertices'
cause The input dictionary passed to `triangle.triangulate` is missing the required 'vertices' key, or it's misspelled.fixEnsure your input dictionary explicitly includes a 'vertices' key pointing to your NumPy array of points, e.g., `{'vertices': points_array}`. -
ValueError: Input array is not 2-dimensional
cause The NumPy array provided for 'vertices', 'segments', or 'holes' is not shaped correctly (e.g., it's 1D, or has too many dimensions). Points should be N x 2, segments M x 2.fixVerify that your NumPy arrays have the correct dimensions. For example, a list of points `[[x1,y1],[x2,y2]]` should be `np.array([[x1,y1],[x2,y2]])`, resulting in a shape of (N, 2). -
triangle.triangulate() got an unexpected keyword argument 'mesh_quality'
cause Attempting to pass triangulation options as keyword arguments. The options must be passed as a single string.fixCombine all desired options into a single string argument, e.g., `triangle.triangulate(data, 'pq30a0.1')`. Do not use separate keyword arguments for options.
Warnings
- gotcha The `triangle.triangulate` function expects input data (vertices, segments, holes) in a specific dictionary format with keys like 'vertices', 'segments', and 'holes'. Misnaming or omitting these keys will lead to errors.
- gotcha The second argument to `triangle.triangulate` is a string of options (e.g., 'pq30a0.1') that directly mirror the command-line flags of the underlying C `triangle` library. Users often expect keyword arguments or separate parameters.
- gotcha The library returns its results (e.g., vertices, triangles, segments) in an output dictionary, which may contain new, refined points or segments compared to the input. The original C `triangle` library has an extensive output format.
- deprecated The versioning scheme is date-based (YYYYMMDD). While this indicates active development, it means that potentially breaking or significant API changes might occur between two 'newer' versions without a traditional major version increment (e.g., v1.x to v2.x).
Install
-
pip install triangle
Imports
- triangle
import triangle
Quickstart
import triangle
import numpy as np
# Define vertices for a square
points = np.array([
[0.0, 0.0],
[1.0, 0.0],
[1.0, 1.0],
[0.0, 1.0]
])
# Define segments (edges) connecting the vertices to form the boundary of the square
# Indices refer to the `points` array (0-indexed)
segments = np.array([
[0, 1],
[1, 2],
[2, 3],
[3, 0]
])
# Define a hole by a point within the region to be excluded from triangulation
holes = np.array([[0.5, 0.5]])
# Triangulate the region.
# The second argument is a string of options for the C library:
# 'p': Planar straight line graph (PSLG) triangulation
# 'q30': Quality mesh generation, minimum angle 30 degrees
# 'a0.01': Maximum triangle area attribute, 0.01
tri = triangle.triangulate({'vertices': points, 'segments': segments, 'holes': holes}, 'pq30a0.01')
print("--- Input ---")
print("Points:\n", points)
print("Segments:\n", segments)
print("Holes:\n", holes)
print("\n--- Output ---")
print("Vertices (may be refined):\n", tri['vertices'])
print("Triangles (indices into output vertices):\n", tri['triangles'])
print("Segments (may be refined):\n", tri['segments'])