ianmackenzie / elm-geometry-prerelease / DelaunayTriangulation2d

This module provides functionality for working with Delaunay triangulations. 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 =
Geometry.Types.DelaunayTriangulation2d vertex

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 =
{ vertices : ( vertex
, vertex
, vertex )
, triangle : Triangle2d
, circumcircle : Circle2d 
}

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

An empty Delaunay triangulation with no vertices or faces.

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

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) -> Array vertex -> Result (Error vertex) (DelaunayTriangulation2d vertex)

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
    , 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 -> DelaunayTriangulation2d Point2d -> Result (Error Point2d) (DelaunayTriangulation2d Point2d)

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) -> vertex -> DelaunayTriangulation2d vertex -> Result (Error vertex) (DelaunayTriangulation2d vertex)

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 -> 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 -> List Triangle2d

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 -> List Circle2d

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 -> List (Face vertex)

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

toMesh : DelaunayTriangulation2d vertex -> TriangularMesh vertex

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