Skip to content

Commit

Permalink
Minor cleanup and updates to DCEL entities.
Browse files Browse the repository at this point in the history
* Rename 'Boundary' property of HalfEdge class to 'Label'
* Add 'Label' property to Face class and initialize to generator label
* Update Voronoi tests.
  • Loading branch information
wo80 committed Jan 9, 2024
1 parent 3654aeb commit 826057a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 33 deletions.
16 changes: 13 additions & 3 deletions src/Triangle.Tests/Voronoi/BoundedVoronoiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ public class BoundedVoronoiTest
[Test]
public void TestBoundedVoronoi()
{
var p = Helper.SplitRectangle(-1, 1, 1, -1, 3);
int boundaryLabel = 3;

var p = Helper.SplitRectangle(-1, 1, 1, -1, boundaryLabel);

Assert.That(p.Regions.Count, Is.EqualTo(2));

int regionLabel1 = p.Regions[0].Label;
int regionLabel2 = p.Regions[1].Label;

var mesher = new GenericMesher();
var mesh = (Mesh)mesher.Triangulate(p);

var voronoi = new BoundedVoronoi(mesh);

// The "split rectangle" polygon has two region pointer set.
Assert.That(voronoi.Vertices.Count(v => v.Label == 1), Is.EqualTo(2));
Assert.That(voronoi.Vertices.Count(v => v.Label == 2), Is.EqualTo(2));
Assert.That(voronoi.Vertices.Count(v => v.Label == regionLabel1), Is.EqualTo(2));
Assert.That(voronoi.Vertices.Count(v => v.Label == regionLabel2), Is.EqualTo(2));

// The polygon has 6 boundary segments, so the Voronoi diagram
// should have 6 infinite edges (which are projected onto the
Expand All @@ -32,6 +39,9 @@ public void TestBoundedVoronoi()
// All Voronoi cells should have a generator vertex.
Assert.That(voronoi.Faces.All(f => f.Generator is not null));

// All Voronoi cells should have the same label as the dual vertex.
Assert.That(voronoi.Faces.All(f => f.Label == boundaryLabel));

// Check DCEL topology (all Voronoi cells should be closed).
Assert.That(voronoi.IsConsistent());
}
Expand Down
16 changes: 13 additions & 3 deletions src/Triangle.Tests/Voronoi/StandardVoronoiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ public class StandardVoronoiTest
[Test]
public void TestStandardVoronoi()
{
var p = Helper.SplitRectangle(-1, 1, 1, -1, 3);
int boundaryLabel = 3;

var p = Helper.SplitRectangle(-1, 1, 1, -1, boundaryLabel);

Assert.That(p.Regions.Count, Is.EqualTo(2));

int regionLabel1 = p.Regions[0].Label;
int regionLabel2 = p.Regions[1].Label;

var mesher = new GenericMesher();
var mesh = (Mesh)mesher.Triangulate(p);

var voronoi = new StandardVoronoi(mesh);

// The "split rectangle" polygon has two region pointer set.
Assert.That(voronoi.Vertices.Count(v => v.Label == 1), Is.EqualTo(2));
Assert.That(voronoi.Vertices.Count(v => v.Label == 2), Is.EqualTo(2));
Assert.That(voronoi.Vertices.Count(v => v.Label == regionLabel1), Is.EqualTo(2));
Assert.That(voronoi.Vertices.Count(v => v.Label == regionLabel2), Is.EqualTo(2));

// The polygon has 6 boundary segments, so the Voronoi diagram
// should have 6 infinite edges.
Expand All @@ -30,6 +37,9 @@ public void TestStandardVoronoi()
// All Voronoi cells should have a generator vertex.
Assert.That(voronoi.Faces.All(f => f.Generator is not null));

// All Voronoi cells should have the same label as the dual vertex.
Assert.That(voronoi.Faces.All(f => f.Label == boundaryLabel));

// Check DCEL topology (account for unbounded Voronoi cells).
Assert.That(voronoi.IsConsistent(false));
}
Expand Down
24 changes: 6 additions & 18 deletions src/Triangle/Topology/DCEL/DcelMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,22 @@ protected DcelMesh(bool initialize)
/// <summary>
/// Gets the vertices of the Voronoi diagram.
/// </summary>
public List<Vertex> Vertices
{
get { return vertices; }
}
public List<Vertex> Vertices => vertices;

/// <summary>
/// Gets the list of half-edges specify the Voronoi diagram topology.
/// </summary>
public List<HalfEdge> HalfEdges
{
get { return edges; }
}
public List<HalfEdge> HalfEdges => edges;

/// <summary>
/// Gets the faces of the Voronoi diagram.
/// </summary>
public List<Face> Faces
{
get { return faces; }
}
public List<Face> Faces => faces;

/// <summary>
/// Gets the collection of edges of the Voronoi diagram.
/// </summary>
public IEnumerable<IEdge> Edges
{
get { return EnumerateEdges(); }
}
public IEnumerable<IEdge> Edges => EnumerateEdges();

/// <summary>
/// Check if the DCEL is consistent.
Expand Down Expand Up @@ -234,7 +222,7 @@ public void ResolveBoundaryEdges()
var map = new Dictionary<int, HalfEdge>();

// TODO: parallel?
foreach (var edge in this.edges)
foreach (var edge in edges)
{
if (edge.twin == null)
{
Expand All @@ -252,7 +240,7 @@ public void ResolveBoundaryEdges()
edge.id = j++;
edge.next = map[edge.twin.origin.id];

this.edges.Add(edge);
edges.Add(edge);
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/Triangle/Topology/DCEL/Face.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace TriangleNet.Topology.DCEL
using TriangleNet.Geometry;

/// <summary>
/// A face of the DCEL datastructure.
/// A face of the DCEL data structure.
/// </summary>
public class Face
{
Expand All @@ -30,7 +30,7 @@ static Face()
#endregion

internal int id;
internal int mark;
internal int label;

// If the face is a Voronoi cell, this is the point that generates the cell.
internal Point generator;
Expand All @@ -53,6 +53,18 @@ public int ID
set { id = value; }
}

/// <summary>
/// Gets or sets a general-purpose label.
/// </summary>
/// <remarks>
/// For Voronoi diagrams, this will be the same as the <see cref="Generator"/> label.
/// </remarks>
public int Label
{
get { return label; }
set { label = value; }
}

/// <summary>
/// Gets or sets a half-edge connected to the face.
/// </summary>
Expand Down Expand Up @@ -95,6 +107,7 @@ public Face(Point generator, HalfEdge edge)
if (generator != null)
{
id = generator.ID;
label = generator.Label;
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/Triangle/Topology/DCEL/HalfEdge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace TriangleNet.Topology.DCEL
{
/// <summary>
/// A half-edge of the DCEL datastructure.
/// A half-edge of the DCEL data structure.
/// </summary>
public class HalfEdge
{
internal int id;
internal int mark;
internal int label;

internal Vertex origin;
internal Face face;
Expand All @@ -29,12 +29,15 @@ public int ID
}

/// <summary>
/// Gets or sets a boundary marker.
/// Gets or sets a general-purpose label.
/// </summary>
public int Boundary
/// <remarks>
/// Can be used to identify boundary segments.
/// </remarks>
public int Label
{
get { return mark; }
set { mark = value; }
get { return label; }
set { label = value; }
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Triangle/Voronoi/StandardVoronoi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public StandardVoronoi(Mesh mesh, Rectangle box)
/// Initializes a new instance of the <see cref="StandardVoronoi" /> class.
/// </summary>
/// <param name="mesh">The mesh.</param>
/// <param name="box">The bounding box used for clipping (not implemented.)</param>
/// <param name="box">The bounding box used for clipping (not implemented).</param>
/// <param name="factory"></param>
/// <param name="predicates"></param>
public StandardVoronoi(Mesh mesh, Rectangle box, IVoronoiFactory factory, IPredicates predicates)
Expand Down

0 comments on commit 826057a

Please sign in to comment.