ianmackenzie / elm-geometry / DelaunayTriangulation2d

This module provides functionality for working with Delaunay triangulations.

Delaunay triangulation

You can:

The current implementation is somewhat inefficient, but there are plans to speed it up in the future (without requiring any changes to the API).


type alias DelaunayTriangulation2d vertex units coordinates =
Geometry.Types.DelaunayTriangulation2d vertex units coordinates

A 2D Delaunay triangulation of a set of vertices.


type Error vertex
    = CoincidentVertices vertex vertex

An error type indicating that the two given vertices have the same position.


type alias Face vertex units coordinates =
{ vertices : ( vertex
, vertex
, vertex )
, triangle : Triangle2d units coordinates
, circumcircle : Circle2d units coordinates 
}

All the details about a particular face of a Delaunay triangulation:

Construction

Constructing a Delaunay triangulation from points/vertices is currently an O(n^2) operation but should be O(n log n) in the future.

empty : DelaunayTriangulation2d vertex units coordinates

An empty Delaunay triangulation with no vertices or faces.

fromPoints : Array (Point2d units coordinates) -> Result (Error (Point2d units coordinates)) (DelaunayTriangulation2d (Point2d units coordinates) units coordinates)

Construct a Delaunay triangulation from an array of points. The points must all be distinct; if any two points are equal, you will get an Err CoincidentVertices.

Note that if all points are collinear, then the resulting triangulation will be empty (have no faces).

fromVerticesBy : (vertex -> Point2d units coordinates) -> Array vertex -> Result (Error vertex) (DelaunayTriangulation2d vertex units coordinates)

Construct a Delaunay triangulation from an array of vertices of arbitrary type, by supplying a function that returns the position of each vertex as a Point2d. For example, if you had

types alias Vertex =
    { position = Point2d Meters WorldCoordinates
    , color = String
    }

and

vertices : Array Vertex
vertices =
    ...

then you would use

DelaunayTriangulation2d.fromVerticesBy .position
    vertices

The vertices must all be distinct; if any two have the same position, you will get an Err CoincidentVertices.

Note that if all vertices are collinear, then the resulting triangulation will be empty (have no faces).

Modification

Inserting a point into a Delaunay triangulation is currently an O(n) operation but should be O(log n) in the future.

insertPoint : Point2d units coordinates -> DelaunayTriangulation2d (Point2d units coordinates) units coordinates -> Result (Error (Point2d units coordinates)) (DelaunayTriangulation2d (Point2d units coordinates) units coordinates)

Add a new point into an existing Delaunay triangulation. It must not be equal to any existing point; if it is, you will get an Err CoincidentVertices.

insertVertexBy : (vertex -> Point2d units coordinates) -> vertex -> DelaunayTriangulation2d vertex units coordinates -> Result (Error vertex) (DelaunayTriangulation2d vertex units coordinates)

Add a new vertex into an existing Delaunay triangulation, by supplying a function to get the position of the vertex. The vertex must not have the same position as any existing vertex; if it is, you will get an Err CoincidentVertices.

Properties

vertices : DelaunayTriangulation2d vertex units coordinates -> Array vertex

Get the vertices of a Delaunay triangulation. If the triangulation was constructed by calling fromPoints or fromVerticesBy, then the returned vertex array will simply be the array that was passed in. If any vertices were added using insertPoint or insertVertexBy, then they will be appended to the end of the array. This is a simple accessor, so complexity is O(1).

triangles : DelaunayTriangulation2d vertex units coordinates -> List (Triangle2d units coordinates)

Get all triangles in a given Delaunay triangulation;

DelaunayTriangulation2d.triangles triangulation

is equivalent to

DelaunayTriangulation2d.faces triangulation
    |> List.map .triangle

but somewhat more efficient. Complexity: O(n).

circumcircles : DelaunayTriangulation2d vertex units coordinates -> List (Circle2d units coordinates)

Get all circumcircles in a given Delaunay triangulation;

DelaunayTriangulation2d.circumcircles triangulation

is equivalent to

DelaunayTriangulation2d.faces triangulation
    |> List.map .circumcircle

but somewhat more efficient. Complexity: O(n).

faces : DelaunayTriangulation2d vertex units coordinates -> List (Face vertex units coordinates)

Get a list of all Faces in a given Delaunay triangulation. Complexity: O(n).

toMesh : DelaunayTriangulation2d vertex units coordinates -> TriangularMesh vertex

Convert a Delaunay triangulation to a TriangularMesh. Complexity: O(n).