using System.Collections.Generic; using System.Linq; using Annulus.CSG; using Azimuth; namespace Annulus.UnitTests.CSG { public class PolygonSetTests : UnitTestSharp.TestFixture { public static PerforatedPolygon Solid(IList outline) { return new PerforatedPolygon(new SimplePolygon(outline)); } public static PerforatedPolygon Holed(IList outline, IList hole) { return new PerforatedPolygon(new SimplePolygon(outline), new[] { new SimplePolygon(hole) }); } public class UnionTests : UnitTestSharp.TestFixture { [UnitTestSharp.IgnoreTest] private void CheckPiece(IList expectedSilhouette, IList> expectedHoles, PerforatedPolygon piece) { PolygonUnionTests.Test(PolygonPolygon.UnionStatus.Success, PolygonPolygon.UnionStatus.Success, expectedSilhouette, piece.Silhouette.VertexList, expectedHoles, piece.Holes.Select(hole => hole.VertexList).ToList(), CheckEqual); } public void NoPolygons_GiveNoPieces() { CheckEqual(0, PolygonSet.Union(new PerforatedPolygon[0]).Count); } public void PolygonOverlappingTwoPieces_JoinsThemIntoOne() { var pieces = PolygonSet.Union(new[] { Solid(PolygonUnionTests.Box(0, 0, 1, 1)), Solid(PolygonUnionTests.Box(2, 0, 3, 1)), Solid(PolygonUnionTests.Box(0.5, 0, 2.5, 1)), }); CheckEqual(1, pieces.Count); CheckPiece(PolygonUnionTests.Box(0, 0, 3, 1), null, pieces[0]); } public void PolygonsMeetingAtOnlyACorner_StayTwoPieces() { var pieces = PolygonSet.Union(new[] { Solid(PolygonUnionTests.Box(0, 0, 1, 1)), Solid(PolygonUnionTests.Box(1, 1, 2, 2)), }); CheckEqual(2, pieces.Count); CheckPiece(PolygonUnionTests.Box(0, 0, 1, 1), null, pieces[0]); CheckPiece(PolygonUnionTests.Box(1, 1, 2, 2), null, pieces[1]); } public void PolygonInsideAHoleTouchingNothing_StaysItsOwnPiece() { var pieces = PolygonSet.Union(new[] { Holed(PolygonUnionTests.Box(0, 0, 3, 3), PolygonUnionTests.Hole(1, 1, 2, 2)), Solid(PolygonUnionTests.Box(1.25, 1.25, 1.75, 1.75)), }); CheckEqual(2, pieces.Count); CheckPiece(PolygonUnionTests.Box(0, 0, 3, 3), new[] { PolygonUnionTests.Hole(1, 1, 2, 2) }, pieces[0]); CheckPiece(PolygonUnionTests.Box(1.25, 1.25, 1.75, 1.75), null, pieces[1]); } public void Hole_SurvivesAMergeAwayFromIt() { var holed = Holed(PolygonUnionTests.Box(0, 0, 3, 3), PolygonUnionTests.Hole(1, 1, 2, 2)); var solid = Solid(PolygonUnionTests.Box(3, 0, 4, 3)); foreach (var polygons in new[] { new[] { holed, solid }, new[] { solid, holed } }) { var pieces = PolygonSet.Union(polygons); CheckEqual(1, pieces.Count); CheckPiece(PolygonUnionTests.Box(0, 0, 4, 3), new[] { PolygonUnionTests.Hole(1, 1, 2, 2) }, pieces[0]); } } public void PolygonWithNoArea_AddsNothing() { var sliver = new[] { new Vector(1, 0), new Vector(1 + 1e-12, 0.5), new Vector(1, 1) }; var pieces = PolygonSet.Union(new[] { Solid(PolygonUnionTests.Box(0, 0, 1, 1)), Solid(sliver) }); CheckEqual(1, pieces.Count); CheckPiece(PolygonUnionTests.Box(0, 0, 1, 1), null, pieces[0]); } } } }