-
Notifications
You must be signed in to change notification settings - Fork 40
Triangulations 3D
CGAL provides a number of 3D 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 = Point3d.RandomPoints(0, 10, new Box3f(-10, 10));
//Create the triangulation.
var tri = new Triangulation3<EIK>(points);
//Insert a point.
tri.Insert(new Point3d(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 Point3d[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 TriVertex3[tri.VertexCount];
tri.GetVertices(vertices, vertices.Length);
var cells = new TriCell3[tri.TetrahedronCount];
tri.GetCells(faces, cells.Length);
var vertex = vertices[0];
Console.WriteLine("Vertex index = " + vertex.Index);
Console.WriteLine("Vertex point = " + vertex.Point);
Console.WriteLine("Vertex cell index = " + vertex.CellIndex);
var cell = cells[0];
Console.WriteLine("Cell index = " + cell.Index);
Console.WriteLine("Cells first vertex index = " + cell.GetVertexIndex(0));
Console.WriteLine("Cells second vertex index = " + cell.GetVertexIndex(1));
Console.WriteLine("Cells third vertex index =" + cell.GetVertexIndex(2));
Console.WriteLine("Cells fourth vertex index =" + cell.GetVertexIndex(3));
Individual elements can be found with their index as follows.
if(tri.GetVertex(index, out TriVertex3 vertex))
{
//Vertex was found
}
The TriVertex3 and TriCell3 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
TriVertex3 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;
}
The triangle contains vertices and cells that are considered infinite. These surround the triangulation and help in its construction but you most likely want to be ignored. You can find out if a vertex or cell is infinite as follows.
TriVertex3 vertex;
tri.GetVertex(0, out vertex);
//A vertex is infinite if IsInfinite is true,
// its index is -1 and its point has a infinite component.
if(vertex.IsInfinite)
Console.WriteLine("Vertex is infinite");
if(vertex.Index == -1)
Console.WriteLine("Vertex is infinite");
if(!vertex.Point.IsFinite)
Console.WriteLine("Vertex is infinite");
TriCell3 cell;
tri.GetCell(0, out cell);
//A vertex is infinite if IsInfinite and is true its index is -1.
if(cell.IsInfinite)
Console.WriteLine("Cell is infinite");
if(cell.Index == -1)
Console.WriteLine("Cell is infinite");