{"id":10310,"library":"triangle","title":"Python triangle library","description":"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.","status":"active","version":"20250106","language":"en","source_language":"en","source_url":"https://github.com/drufat/triangle","tags":["geometry","triangulation","Delaunay","mesh generation","constrained triangulation","numpy"],"install":[{"cmd":"pip install triangle","lang":"bash","label":"Install `triangle`"}],"dependencies":[{"reason":"Required for handling point and array data structures for input and output.","package":"numpy"}],"imports":[{"symbol":"triangle","correct":"import triangle"}],"quickstart":{"code":"import triangle\nimport numpy as np\n\n# Define vertices for a square\npoints = np.array([\n    [0.0, 0.0],\n    [1.0, 0.0],\n    [1.0, 1.0],\n    [0.0, 1.0]\n])\n\n# Define segments (edges) connecting the vertices to form the boundary of the square\n# Indices refer to the `points` array (0-indexed)\nsegments = np.array([\n    [0, 1],\n    [1, 2],\n    [2, 3],\n    [3, 0]\n])\n\n# Define a hole by a point within the region to be excluded from triangulation\nholes = np.array([[0.5, 0.5]])\n\n# Triangulate the region.\n# The second argument is a string of options for the C library:\n# 'p': Planar straight line graph (PSLG) triangulation\n# 'q30': Quality mesh generation, minimum angle 30 degrees\n# 'a0.01': Maximum triangle area attribute, 0.01\ntri = triangle.triangulate({'vertices': points, 'segments': segments, 'holes': holes}, 'pq30a0.01')\n\nprint(\"--- Input ---\")\nprint(\"Points:\\n\", points)\nprint(\"Segments:\\n\", segments)\nprint(\"Holes:\\n\", holes)\nprint(\"\\n--- Output ---\")\nprint(\"Vertices (may be refined):\\n\", tri['vertices'])\nprint(\"Triangles (indices into output vertices):\\n\", tri['triangles'])\nprint(\"Segments (may be refined):\\n\", tri['segments'])","lang":"python","description":"This quickstart demonstrates how to create a Delaunay triangulation of a square region with a central hole. It defines the boundary using vertices and segments, specifies the hole's location, and then uses `triangle.triangulate` with quality and area constraints to generate a mesh. The output includes refined vertices, the triangle connectivity, and potentially refined segments."},"warnings":[{"fix":"Always pass a dictionary with the correct keys, e.g., `{'vertices': np_array_of_points, 'segments': np_array_of_segments}`. Refer to the GitHub README for exact required keys.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consult the official documentation for the original C `triangle` library (or the Python `triangle` README) to understand the meaning and syntax of the option string. Common options like 'p' (PSLG), 'q' (quality), 'a' (area) are typically used.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always access the results by indexing the output dictionary, e.g., `tri['vertices']`, `tri['triangles']`. Be aware that the `vertices` and `segments` in the output might not be identical to your input if refinement was applied.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Before upgrading to a newer date-version, review the GitHub commit history or release notes for any changes that might impact your code. Pin specific versions in your `requirements.txt` to avoid unexpected updates.","message":"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).","severity":"deprecated","affected_versions":"All versions (by nature of scheme)"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure your input dictionary explicitly includes a 'vertices' key pointing to your NumPy array of points, e.g., `{'vertices': points_array}`.","cause":"The input dictionary passed to `triangle.triangulate` is missing the required 'vertices' key, or it's misspelled.","error":"KeyError: 'vertices'"},{"fix":"Verify 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).","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.","error":"ValueError: Input array is not 2-dimensional"},{"fix":"Combine all desired options into a single string argument, e.g., `triangle.triangulate(data, 'pq30a0.1')`. Do not use separate keyword arguments for options.","cause":"Attempting to pass triangulation options as keyword arguments. The options must be passed as a single string.","error":"triangle.triangulate() got an unexpected keyword argument 'mesh_quality'"}]}