Skip to content

Arrangements 2D

Justin edited this page Mar 14, 2022 · 3 revisions

CGAL provides a arrangement class that handles points, segments and faces on a 2D plane. Once an arrangement is constructed, the class can be used to obtain results of various queries on the arrangement, such as point location and intersection's.

Note - For intersection operations it is recommended to use the EEK kernel. Precision maybe a issue if the EIK kernel is used.

A arrangement can be created as follows.


//Create the arrangment object.
var arr = new Arrangement2<EEK>();

//Insert a point.
arr.InsertPoint(new Point2d(0,1));

//Insert a segment. Always check for intersection's with
// current added elements unless its know to not intersect. 
arr.InsertSegment(new Point2d(-5,-5), new Point2d(5,5), true);

//Inset a polygon. Again, check for intersections.
var box = PolygonFactory<EEK>.CreateBox(-5, 5);
arr.InsertPolygon(box, true);

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


var vertices = new ArrVertex2[arr.VertexCount];
arr.GetVertices(vertices, vertices.Length);

var faces = new ArrFace2[arr.FaceCount];
arr.GetFaces(faces, faces.Length);

var edges = new ArrHalfedge2[arr.HalfEdgeCount];
arr.GetHalfEdges(edges, edges.Length);

var face = faces[0];
Console.WriteLine("Face index = " + face.Index);
Console.WriteLine("Halfedge index = " + face.HalfEdgeIndex);

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

var edge = edges[0];
Console.WriteLine("Edge index = " + edge.Index);
Console.WriteLine("Edge next index = " + edge.NextIndex);
Console.WriteLine("Edge previous index = " + edge.PreviousIndex);

Individual elements can be found with their index as follows.


if (arr.GetFace(0, out ArrFace2 face))
{
    //Enmerate all vertices in face
    foreach(var vert in face.EnumerateVertices(arr))
    {
        //do something with vert
    }
}

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


//Create the triangulation.
var arr = new Arrangement2<EEK>(points);

int buildStamp = arr.BuildStamp;

//Get the first vertex
ArrVertex2 vertex;
arr.GetVertex(0, out vertex);

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

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

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