Add Polygon tests.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using NUnit.Framework;
|
||||
using TriangleNet.Geometry;
|
||||
|
||||
namespace TriangleNet.Tests.Geometry
|
||||
{
|
||||
public class PolygonTest
|
||||
{
|
||||
// The vertices that define the polygon contour (triangle shape).
|
||||
Vertex[] vertices = new Vertex[]
|
||||
{
|
||||
new Vertex(0d, 0d),
|
||||
new Vertex(2d ,0d),
|
||||
new Vertex(1d, 1.5)
|
||||
};
|
||||
|
||||
[Test]
|
||||
public void TestAddContour()
|
||||
{
|
||||
var contour = new Contour(vertices);
|
||||
|
||||
var p = new Polygon();
|
||||
|
||||
p.Add(contour);
|
||||
|
||||
Assert.AreEqual(3, p.Points.Count);
|
||||
Assert.AreEqual(3, p.Segments.Count);
|
||||
Assert.AreEqual(0, p.Holes.Count);
|
||||
Assert.AreEqual(0, p.Regions.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestAddContourAsHole()
|
||||
{
|
||||
var contour = new Contour(vertices);
|
||||
|
||||
var p = new Polygon();
|
||||
|
||||
p.Add(contour, true);
|
||||
|
||||
Assert.AreEqual(3, p.Points.Count);
|
||||
Assert.AreEqual(3, p.Segments.Count);
|
||||
Assert.AreEqual(1, p.Holes.Count);
|
||||
Assert.AreEqual(0, p.Regions.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestAddContourAsRegion()
|
||||
{
|
||||
var contour = new Contour(vertices, 1);
|
||||
|
||||
var p = new Polygon();
|
||||
|
||||
p.Add(contour, 1);
|
||||
|
||||
Assert.AreEqual(3, p.Points.Count);
|
||||
Assert.AreEqual(3, p.Segments.Count);
|
||||
Assert.AreEqual(0, p.Holes.Count);
|
||||
Assert.AreEqual(1, p.Regions.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestBounds()
|
||||
{
|
||||
var contour = new Contour(vertices);
|
||||
|
||||
var p = new Polygon();
|
||||
|
||||
p.Add(contour);
|
||||
|
||||
var bounds = p.Bounds();
|
||||
|
||||
Assert.AreEqual(2d, bounds.Width);
|
||||
Assert.AreEqual(1.5, bounds.Height);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,9 @@
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="nunit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.3.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -121,14 +121,15 @@ namespace TriangleNet.Tools
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the ratio of the largest and smallest segment length.
|
||||
/// Gets the minimum and maximum feature size (segment length) of the input polygon.
|
||||
/// </summary>
|
||||
/// <param name="poly">The polygon.</param>
|
||||
/// <param name="threshold">The ratio threshold.</param>
|
||||
/// <param name="threshold">The ratio threshold (smallest to largest segment length, default = 2e-10).</param>
|
||||
/// <remarks>
|
||||
/// This method will also report zero-length segments.
|
||||
/// This method will also report zero-length segments. The method does NOT detect
|
||||
/// free vertices lying close to segments.
|
||||
/// </remarks>
|
||||
public static double GetSegmentRatio(IPolygon poly, double threshold = 2e12)
|
||||
public static (double min, double max) GetSegmentRatio(IPolygon poly, double threshold = 2e-10)
|
||||
{
|
||||
var logger = Log.Instance;
|
||||
|
||||
@@ -156,15 +157,15 @@ namespace TriangleNet.Tools
|
||||
max = Math.Max(max, length);
|
||||
}
|
||||
|
||||
double ratio = max / min;
|
||||
double ratio = min / max;
|
||||
|
||||
if (ratio > threshold)
|
||||
if (ratio < threshold)
|
||||
{
|
||||
logger.Warning(string.Format("Polygon has large segment ratio {0:G2}.", ratio),
|
||||
logger.Warning(string.Format("Polygon has tiny segment ratio {0:G2}.", ratio),
|
||||
"PolygonValidator.GetSegmentRatio()");
|
||||
}
|
||||
|
||||
return ratio;
|
||||
return (min, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user