Skip to content

Triangulations 2D

Justin edited this page Mar 13, 2022 · 7 revisions

CGAL provides a number of 2D triangulations that can be constructed incrementally. The types of triangulations are as follows.

A triangulation can be constructed as follows.


//Create the points for the  triangulation.
var points = Point2d.RandomPoints(0, 10, new Box2f(-10, 10));

//Create the triangulation.
var tri = new Triangulation2<EIK>(points);

//Insert a point.
tri.Insert(new Point2d(1,1));

//Print some info about the triangulation.
tri.Print();

Elements of the triangulation can be recovered as follows.


//Get the points.
var points = new Point2d[tri.VertexCount];
tri.GetPoints(points, points.Length);

//Get the triangle indices where the index is a points index in trianglation.
var indices = new int[tri.TriangleCount];
tri.GetIndices(indices, indices.Length);

//Get the triangles a shapes.
var triangles = new Triangle2d[tri.TriangleCount];
tri.GetTriangles(triangles, triangles.Length);

The vertex and face objects can also be retrieved and contain more information.


var vertices = new TriVertex2[tri.VertexCount];
tri.GetVertices(vertices, vertices.Length);

var faces = new TriFace2[tri.TriangleCount];
tri.GetFaces(faces, faces.Length);

var face = faces[0];
Console.WriteLine("Face index = " +face.Index);
Console.WriteLine("Face first vertex index = " + face.GetVertexIndex(0));
Console.WriteLine("Face second vertex index = " + face.GetVertexIndex(1));
Console.WriteLine("Face third vertex index =" + face.GetVertexIndex(2));

var vertex = vertices[0];
Console.WriteLine("Vertex index = " + vertex.Index);
Console.WriteLine("Vertex point = " + vertex.Point);
Console.WriteLine("Vertex face index = " + vertex.FaceIndex);

Individual elements can be found with their index as follows.


if(tri.GetVertex(index, out TriVertex2 vertex))
{
    //Vertex was found
}

The TriVertex2 and TriFace2 structs only refer to elements by their index in the triangulation. These indices can change if the triangulation changes. The data then becomes invalid and will need to be retrieved again. You can check if the triangulation has changed by the build stamp.


//Create the triangulation.
var tri = new Triangulation2<EIK>(points);

int buildStamp = tri.BuildStamp;

//Get the first vertex
TriVertex2 vertex;
tri.GetVertex(0, out vertex);

//Insert a new point.
tri.Insert(new Point2d(1,1));

if(buildStamp != tri.BuildStamp)
{
    //Triangulation has changed so vertex data maybe invalid. Will need to retrieve again.

    tri.GetVertex(0, out vertex);
    buildStamp = tri.BuildStamp;
}

Below is a image a Delaunay triangulation and its Voronoi diagram.

triangulation;

Clone this wiki locally