Skip to content

Commit

Permalink
Add Voronoi tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wo80 committed Jan 8, 2024
1 parent d652720 commit b35680e
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/Triangle.Tests/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,47 @@ static class Helper
public static Contour Rectangle(double left, double top,
double right, double bottom, int mark = 0)
{
var points = new List<Vertex>(4);

points.Add(new Vertex(left, top, mark));
points.Add(new Vertex(right, top, mark));
points.Add(new Vertex(right, bottom, mark));
points.Add(new Vertex(left, bottom, mark));
var points = new List<Vertex>(4)
{
new Vertex(left, top, mark),
new Vertex(right, top, mark),
new Vertex(right, bottom, mark),
new Vertex(left, bottom, mark)
};

return new Contour(points, mark, true);
}

public static Polygon SplitRectangle(double left, double top,
double right, double bottom, int mark = 0)
{
double midX = (right - left) / 2;
double midY = (top - bottom) / 2;

var midTop = new Vertex(left + midX, top, mark);
var midBottom = new Vertex(left + midX, bottom, mark);

var points = new List<Vertex>(4)
{
new Vertex(left, top, mark),
midTop,
new Vertex(right, top, mark),
new Vertex(right, bottom, mark),
midBottom,
new Vertex(left, bottom, mark)
};

var poly = new Polygon();

poly.Add(new Contour(points, mark, true));
poly.Add(new Segment(midTop, midBottom));

poly.Regions.Add(new RegionPointer(left + midX / 2, bottom + midY / 2, 1));
poly.Regions.Add(new RegionPointer(right - midX / 2, bottom + midY / 2, 2));

return poly;
}

public static Triangle CreateTriangle(int id, Vertex org, Vertex dest, Vertex apex)
{
var t = new Triangle() { id = id, hash = id };
Expand Down
33 changes: 33 additions & 0 deletions src/Triangle.Tests/Voronoi/BoundedVoronoiTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using NUnit.Framework;
using System;
using System.Linq;
using TriangleNet.Meshing;
using TriangleNet.Voronoi;

namespace TriangleNet.Tests.Voronoi
{
public class BoundedVoronoiTest
{
[Test]
public void TestBoundedVoronoi()
{
var p = Helper.SplitRectangle(-1, 1, 1, -1, 3);

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));

// The polygon has 6 boundary segments, so the Voronoi diagram
// should have 6 infinite edges (which are projected onto the
// boundary). Additionally, the 6 boundary vertices are part of
// the Voronoi diagram.
Assert.That(voronoi.Vertices.Count(v => v.Label == 0), Is.EqualTo(12));
Assert.That(voronoi.Vertices.Count(v => v.ID >= mesh.Triangles.Count), Is.EqualTo(12));
}
}
}
31 changes: 31 additions & 0 deletions src/Triangle.Tests/Voronoi/StandardVoronoiTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NUnit.Framework;
using System;
using System.Linq;
using TriangleNet.Meshing;
using TriangleNet.Voronoi;

namespace TriangleNet.Tests.Voronoi
{
public class StandardVoronoiTest
{
[Test]
public void TestStandardVoronoi()
{
var p = Helper.SplitRectangle(-1, 1, 1, -1, 3);

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));

// The polygon has 6 boundary segments, so the Voronoi diagram
// should have 6 infinite edges.
Assert.That(voronoi.Vertices.Count(v => v.Label == 0), Is.EqualTo(6));
Assert.That(voronoi.Vertices.Count(v => v.ID >= mesh.Triangles.Count), Is.EqualTo(6));
}
}
}

0 comments on commit b35680e

Please sign in to comment.