DotRecastNetSim/test/DotRecast.Detour.Test/PolygonByCircleConstraintTe...

92 lines
3.4 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
recast4j copyright (c) 2021 Piotr Piastucki piotr@jtilia.org
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
2023-03-29 18:59:00 +03:00
using DotRecast.Core;
2023-03-14 08:02:43 +03:00
using NUnit.Framework;
namespace DotRecast.Detour.Test;
2023-04-25 17:22:44 +03:00
[Parallelizable]
2023-03-16 19:48:49 +03:00
public class PolygonByCircleConstraintTest
{
2023-03-14 08:02:43 +03:00
private readonly PolygonByCircleConstraint constraint = new PolygonByCircleConstraint.StrictPolygonByCircleConstraint();
[Test]
2023-03-16 19:48:49 +03:00
public void shouldHandlePolygonFullyInsideCircle()
{
2023-03-14 08:02:43 +03:00
float[] polygon = { -2, 0, 2, 2, 0, 2, 2, 0, -2, -2, 0, -2 };
2023-03-29 18:59:00 +03:00
Vector3f center = Vector3f.Of(1, 0, 1);
2023-03-14 08:02:43 +03:00
float[] constrained = constraint.aply(polygon, center, 6);
Assert.That(constrained, Is.EqualTo(polygon));
}
[Test]
2023-03-16 19:48:49 +03:00
public void shouldHandleVerticalSegment()
{
2023-03-14 08:02:43 +03:00
int expectedSize = 21;
float[] polygon = { -2, 0, 2, 2, 0, 2, 2, 0, -2, -2, 0, -2 };
2023-03-29 18:59:00 +03:00
Vector3f center = Vector3f.Of(2, 0, 0);
2023-03-14 08:02:43 +03:00
float[] constrained = constraint.aply(polygon, center, 3);
Assert.That(constrained.Length, Is.EqualTo(expectedSize));
2023-03-16 19:48:49 +03:00
Assert.That(constrained, Is.SupersetOf(new[] { 2f, 0f, 2f, 2f, 0f, -2f }));
2023-03-14 08:02:43 +03:00
}
[Test]
2023-03-16 19:48:49 +03:00
public void shouldHandleCircleFullyInsidePolygon()
{
2023-03-14 08:02:43 +03:00
int expectedSize = 12 * 3;
float[] polygon = { -4, 0, 0, -3, 0, 3, 2, 0, 3, 3, 0, -3, -2, 0, -4 };
2023-03-29 18:59:00 +03:00
Vector3f center = Vector3f.Of(-1, 0, -1);
2023-03-14 08:02:43 +03:00
float[] constrained = constraint.aply(polygon, center, 2);
Assert.That(constrained.Length, Is.EqualTo(expectedSize));
2023-03-16 19:48:49 +03:00
for (int i = 0; i < expectedSize; i += 3)
{
2023-03-14 08:02:43 +03:00
float x = constrained[i] + 1;
float z = constrained[i + 2] + 1;
Assert.That(x * x + z * z, Is.EqualTo(4).Within(1e-4f));
}
}
[Test]
2023-03-16 19:48:49 +03:00
public void shouldHandleCircleInsidePolygon()
{
2023-03-14 08:02:43 +03:00
int expectedSize = 9 * 3;
float[] polygon = { -4, 0, 0, -3, 0, 3, 2, 0, 3, 3, 0, -3, -2, 0, -4 };
2023-03-29 18:59:00 +03:00
Vector3f center = Vector3f.Of(-2, 0, -1);
2023-03-14 08:02:43 +03:00
float[] constrained = constraint.aply(polygon, center, 3);
Assert.That(constrained.Length, Is.EqualTo(expectedSize));
Assert.That(constrained, Is.SupersetOf(new[] { -2f, 0f, -4f, -4f, 0f, 0f, -3.4641016f, 0.0f, 1.6076951f, -2.0f, 0.0f, 2.0f }));
}
[Test]
public void shouldHandleCircleOutsidePolygon()
{
int expectedSize = 7 * 3;
float[] polygon = { -4, 0, 0, -3, 0, 3, 2, 0, 3, 3, 0, -3, -2, 0, -4 };
2023-03-29 18:59:00 +03:00
Vector3f center = Vector3f.Of(4, 0, 0);
2023-03-14 08:02:43 +03:00
float[] constrained = constraint.aply(polygon, center, 4);
Assert.That(constrained.Length, Is.EqualTo(expectedSize));
Assert.That(constrained, Is.SupersetOf(new[] { 1.5358982f, 0f, 3f, 2f, 0f, 3f, 3f, 0f, -3f }));
2023-03-16 19:48:49 +03:00
}
}