diff --git a/src/DotRecast.Detour.Crowd/DtCrowd.cs b/src/DotRecast.Detour.Crowd/DtCrowd.cs index 6bb0b3d..7467c06 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowd.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowd.cs @@ -616,7 +616,7 @@ namespace DotRecast.Detour.Crowd // Partial path, constrain target position inside the // last polygon. var cr = _navQuery.ClosestPointOnPoly(reqPath[reqPath.Count - 1], ag.targetPos, out reqPos, out var _); - if (cr.IsFailed()) + if (cr.Failed()) { reqPath = new List(); } @@ -694,7 +694,7 @@ namespace DotRecast.Detour.Crowd // _telemetry.RecordPathWaitTime(ag.targetReplanTime); // Poll path queue. DtStatus status = ag.targetPathQueryResult.status; - if (status.IsFailed()) + if (status.Failed()) { // Path find failed, retry if the target location is still // valid. @@ -710,7 +710,7 @@ namespace DotRecast.Detour.Crowd ag.targetReplanTime = 0; } - else if (status.IsSuccess()) + else if (status.Succeeded()) { List path = ag.corridor.GetPath(); if (0 == path.Count) @@ -723,7 +723,7 @@ namespace DotRecast.Detour.Crowd bool valid = true; List res = ag.targetPathQueryResult.path; - if (status.IsFailed() || 0 == res.Count) + if (status.Failed() || 0 == res.Count) { valid = false; } @@ -780,7 +780,7 @@ namespace DotRecast.Detour.Crowd // Partial path, constrain target position inside // the last polygon. var cr = _navQuery.ClosestPointOnPoly(res[res.Count - 1], targetPos, out var nearest, out var _); - if (cr.IsSuccess()) + if (cr.Succeeded()) { targetPos = nearest; } diff --git a/src/DotRecast.Detour.Crowd/DtPathQueue.cs b/src/DotRecast.Detour.Crowd/DtPathQueue.cs index d9f2d0b..acb741c 100644 --- a/src/DotRecast.Detour.Crowd/DtPathQueue.cs +++ b/src/DotRecast.Detour.Crowd/DtPathQueue.cs @@ -56,21 +56,21 @@ namespace DotRecast.Detour.Crowd } // Handle query in progress. - if (q.result.status.IsInProgress()) + if (q.result.status.InProgress()) { Result res = q.navQuery.UpdateSlicedFindPath(iterCount); q.result.status = res.status; iterCount -= res.result; } - if (q.result.status.IsSuccess()) + if (q.result.status.Succeeded()) { Result> path = q.navQuery.FinalizeSlicedFindPath(); q.result.status = path.status; q.result.path = path.result; } - if (!(q.result.status.IsFailed() || q.result.status.IsSuccess())) + if (!(q.result.status.Failed() || q.result.status.Succeeded())) { queue.AddFirst(q); } diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index 862a719..6e75c6c 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -72,24 +72,24 @@ namespace DotRecast.Detour m_openList = new DtNodeQueue(); } - /** - * Returns random location on navmesh. Polygons are chosen weighted by area. The search runs in linear related to - * number of polygon. - * - * @param filter - * The polygon filter to apply to the query. - * @param frand - * Function returning a random number [0..1). - * @return Random location - */ - public Result FindRandomPoint(IDtQueryFilter filter, FRand frand) + /// Returns random location on navmesh. + /// Polygons are chosen weighted by area. The search runs in linear related to number of polygon. + /// @param[in] filter The polygon filter to apply to the query. + /// @param[in] frand Function returning a random number [0..1). + /// @param[out] randomRef The reference id of the random location. + /// @param[out] randomPt The random location. + /// @returns The status flags for the query. + public DtStatus FindRandomPoint(IDtQueryFilter filter, FRand frand, out long randomRef, out RcVec3f randomPt) { - // Randomly pick one tile. Assume that all tiles cover roughly the same area. + randomRef = 0; + randomPt = RcVec3f.Zero; + if (null == filter || null == frand) { - return Results.InvalidParam(); + return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; } + // Randomly pick one tile. Assume that all tiles cover roughly the same area. DtMeshTile tile = null; float tsum = 0.0f; for (int i = 0; i < m_nav.GetMaxTiles(); i++) @@ -112,7 +112,7 @@ namespace DotRecast.Detour if (tile == null) { - return Results.InvalidParam("Tile not found"); + return DtStatus.DT_FAILURE; } // Randomly pick one polygon weighted by polygon area. @@ -159,7 +159,7 @@ namespace DotRecast.Detour if (poly == null) { - return Results.InvalidParam("Poly not found"); + return DtStatus.DT_FAILURE; } // Randomly pick point on polygon. @@ -176,7 +176,11 @@ namespace DotRecast.Detour var pt = DetourCommon.RandomPointInConvexPoly(verts, poly.vertCount, areas, s, t); ClosestPointOnPoly(polyRef, pt, out var closest, out var _); - return Results.Success(new FindRandomPointResult(polyRef, closest)); + + randomRef = polyRef; + randomPt = closest; + + return DtStatus.DT_SUCCSESS; } /** @@ -198,7 +202,7 @@ namespace DotRecast.Detour public Result FindRandomPointAroundCircle(long startRef, RcVec3f centerPos, float maxRadius, IDtQueryFilter filter, FRand frand) { - return FindRandomPointAroundCircle(startRef, centerPos, maxRadius, filter, frand, IPolygonByCircleConstraint.Noop()); + return FindRandomPointAroundCircle(startRef, centerPos, maxRadius, filter, frand, NoOpPolygonByCircleConstraint.Noop); } /** @@ -219,7 +223,7 @@ namespace DotRecast.Detour public Result FindRandomPointWithinCircle(long startRef, RcVec3f centerPos, float maxRadius, IDtQueryFilter filter, FRand frand) { - return FindRandomPointAroundCircle(startRef, centerPos, maxRadius, filter, frand, IPolygonByCircleConstraint.Strict()); + return FindRandomPointAroundCircle(startRef, centerPos, maxRadius, filter, frand, StrictPolygonByCircleConstraint.Strict); } public Result FindRandomPointAroundCircle(long startRef, RcVec3f centerPos, float maxRadius, @@ -416,9 +420,9 @@ namespace DotRecast.Detour /// @returns The status flags for the query. public DtStatus ClosestPointOnPoly(long refs, RcVec3f pos, out RcVec3f closest, out bool posOverPoly) { - closest = RcVec3f.Zero; + closest = pos; posOverPoly = false; - + if (!m_nav.IsValidPolyRef(refs) || !RcVec3f.IsFinite(pos)) { return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; @@ -562,7 +566,7 @@ namespace DotRecast.Detour // Get nearby polygons from proximity grid. DtFindNearestPolyQuery query = new DtFindNearestPolyQuery(this, center); DtStatus status = QueryPolygons(center, halfExtents, filter, query); - if (status.IsFailed()) + if (status.Failed()) { return Results.Of(status, ""); } @@ -1083,7 +1087,7 @@ namespace DotRecast.Detour */ public virtual Result UpdateSlicedFindPath(int maxIter) { - if (!m_query.status.IsInProgress()) + if (!m_query.status.InProgress()) { return Results.Of(m_query.status, 0); } @@ -1322,7 +1326,7 @@ namespace DotRecast.Detour public virtual Result> FinalizeSlicedFindPath() { List path = new List(64); - if (m_query.status.IsFailed()) + if (m_query.status.Failed()) { // Reset query. m_query = new DtQueryData(); @@ -1367,7 +1371,7 @@ namespace DotRecast.Detour return Results.Failure(path); } - if (m_query.status.IsFailed()) + if (m_query.status.Failed()) { // Reset query. m_query = new DtQueryData(); @@ -1487,7 +1491,7 @@ namespace DotRecast.Detour { var pt = RcVec3f.Lerp(left, right, t); stat = AppendVertex(pt, 0, path[i + 1], straightPath, maxStraightPath); - if (!stat.IsInProgress()) + if (!stat.InProgress()) { return stat; } @@ -1549,7 +1553,7 @@ namespace DotRecast.Detour var closestEndPos = closestEndPosRes.result; // Add start point. DtStatus stat = AppendVertex(closestStartPos, DT_STRAIGHTPATH_START, path[0], straightPath, maxStraightPath); - if (!stat.IsInProgress()) + if (!stat.InProgress()) { return Results.Success(straightPath); } @@ -1639,7 +1643,7 @@ namespace DotRecast.Detour { stat = AppendPortals(apexIndex, leftIndex, portalLeft, path, straightPath, maxStraightPath, options); - if (!stat.IsInProgress()) + if (!stat.InProgress()) { return Results.Success(straightPath); } @@ -1662,7 +1666,7 @@ namespace DotRecast.Detour // Append or update vertex stat = AppendVertex(portalApex, flags, refs, straightPath, maxStraightPath); - if (!stat.IsInProgress()) + if (!stat.InProgress()) { return Results.Success(straightPath); } @@ -1696,7 +1700,7 @@ namespace DotRecast.Detour { stat = AppendPortals(apexIndex, rightIndex, portalRight, path, straightPath, maxStraightPath, options); - if (!stat.IsInProgress()) + if (!stat.InProgress()) { return Results.Success(straightPath); } @@ -1719,7 +1723,7 @@ namespace DotRecast.Detour // Append or update vertex stat = AppendVertex(portalApex, flags, refs, straightPath, maxStraightPath); - if (!stat.IsInProgress()) + if (!stat.InProgress()) { return Results.Success(straightPath); } @@ -1742,7 +1746,7 @@ namespace DotRecast.Detour { stat = AppendPortals(apexIndex, path.Count - 1, closestEndPos, path, straightPath, maxStraightPath, options); - if (!stat.IsInProgress()) + if (!stat.InProgress()) { return Results.Success(straightPath); } diff --git a/src/DotRecast.Detour/DtStatus.cs b/src/DotRecast.Detour/DtStatus.cs index 890c5cc..1872a6a 100644 --- a/src/DotRecast.Detour/DtStatus.cs +++ b/src/DotRecast.Detour/DtStatus.cs @@ -56,19 +56,19 @@ namespace DotRecast.Detour } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool IsSuccess() + public bool Succeeded() { return 0 != (Value & (DT_SUCCSESS.Value | DT_PARTIAL_RESULT.Value)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool IsFailed() + public bool Failed() { return 0 != (Value & (DT_FAILURE.Value | DT_INVALID_PARAM.Value)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool IsInProgress() + public bool InProgress() { return 0 != (Value & DT_IN_PROGRESS.Value); } diff --git a/src/DotRecast.Detour/IPolygonByCircleConstraint.cs b/src/DotRecast.Detour/IPolygonByCircleConstraint.cs index 9823539..03a2960 100644 --- a/src/DotRecast.Detour/IPolygonByCircleConstraint.cs +++ b/src/DotRecast.Detour/IPolygonByCircleConstraint.cs @@ -24,15 +24,5 @@ namespace DotRecast.Detour public interface IPolygonByCircleConstraint { float[] Aply(float[] polyVerts, RcVec3f circleCenter, float radius); - - public static IPolygonByCircleConstraint Noop() - { - return new NoOpPolygonByCircleConstraint(); - } - - public static IPolygonByCircleConstraint Strict() - { - return new StrictPolygonByCircleConstraint(); - } } } \ No newline at end of file diff --git a/src/DotRecast.Detour/LegacyNavMeshQuery.cs b/src/DotRecast.Detour/LegacyNavMeshQuery.cs index defd50d..153ce85 100644 --- a/src/DotRecast.Detour/LegacyNavMeshQuery.cs +++ b/src/DotRecast.Detour/LegacyNavMeshQuery.cs @@ -230,7 +230,7 @@ namespace DotRecast.Detour */ public override Result UpdateSlicedFindPath(int maxIter) { - if (!m_query.status.IsInProgress()) + if (!m_query.status.InProgress()) { return Results.Of(m_query.status, 0); } @@ -467,7 +467,7 @@ namespace DotRecast.Detour public override Result> FinalizeSlicedFindPath() { List path = new List(64); - if (m_query.status.IsFailed()) + if (m_query.status.Failed()) { // Reset query. m_query = new DtQueryData(); @@ -553,7 +553,7 @@ namespace DotRecast.Detour return Results.Failure(path); } - if (m_query.status.IsFailed()) + if (m_query.status.Failed()) { // Reset query. m_query = new DtQueryData(); diff --git a/src/DotRecast.Detour/NoOpPolygonByCircleConstraint.cs b/src/DotRecast.Detour/NoOpPolygonByCircleConstraint.cs index 8a1f576..5599b84 100644 --- a/src/DotRecast.Detour/NoOpPolygonByCircleConstraint.cs +++ b/src/DotRecast.Detour/NoOpPolygonByCircleConstraint.cs @@ -4,6 +4,12 @@ namespace DotRecast.Detour { public class NoOpPolygonByCircleConstraint : IPolygonByCircleConstraint { + public static readonly NoOpPolygonByCircleConstraint Noop = new NoOpPolygonByCircleConstraint(); + + private NoOpPolygonByCircleConstraint() + { + } + public float[] Aply(float[] polyVerts, RcVec3f circleCenter, float radius) { return polyVerts; diff --git a/src/DotRecast.Detour/QueryResults/FindRandomPointResult.cs b/src/DotRecast.Detour/QueryResults/FindRandomPointResult.cs index 451c34c..3e401e2 100644 --- a/src/DotRecast.Detour/QueryResults/FindRandomPointResult.cs +++ b/src/DotRecast.Detour/QueryResults/FindRandomPointResult.cs @@ -22,7 +22,7 @@ using DotRecast.Core; namespace DotRecast.Detour.QueryResults { -//TODO: (PP) Add comments + //TODO: (PP) Add comments public class FindRandomPointResult { private readonly long randomRef; diff --git a/src/DotRecast.Detour/QueryResults/Result.cs b/src/DotRecast.Detour/QueryResults/Result.cs index ba326ec..a9517c0 100644 --- a/src/DotRecast.Detour/QueryResults/Result.cs +++ b/src/DotRecast.Detour/QueryResults/Result.cs @@ -74,12 +74,12 @@ namespace DotRecast.Detour.QueryResults public bool Failed() { - return status.IsFailed(); + return status.Failed(); } public bool Succeeded() { - return status.IsSuccess(); + return status.Succeeded(); } } } \ No newline at end of file diff --git a/src/DotRecast.Detour/StrictPolygonByCircleConstraint.cs b/src/DotRecast.Detour/StrictPolygonByCircleConstraint.cs index 2f2ad47..cb09e99 100644 --- a/src/DotRecast.Detour/StrictPolygonByCircleConstraint.cs +++ b/src/DotRecast.Detour/StrictPolygonByCircleConstraint.cs @@ -9,7 +9,27 @@ namespace DotRecast.Detour public class StrictPolygonByCircleConstraint : IPolygonByCircleConstraint { private const int CIRCLE_SEGMENTS = 12; - private static float[] unitCircle; + private static readonly float[] UnitCircle = MakeUnitCircle(); + + public static readonly IPolygonByCircleConstraint Strict = new StrictPolygonByCircleConstraint(); + + private StrictPolygonByCircleConstraint() + { + } + + private static float[] MakeUnitCircle() + { + var temp = new float[CIRCLE_SEGMENTS * 3]; + for (int i = 0; i < CIRCLE_SEGMENTS; i++) + { + double a = i * Math.PI * 2 / CIRCLE_SEGMENTS; + temp[3 * i] = (float)Math.Cos(a); + temp[3 * i + 1] = 0; + temp[3 * i + 2] = (float)-Math.Sin(a); + } + + return temp; + } public float[] Aply(float[] verts, RcVec3f center, float radius) { @@ -41,26 +61,15 @@ namespace DotRecast.Detour return intersection; } + private float[] Circle(RcVec3f center, float radius) { - if (unitCircle == null) - { - unitCircle = new float[CIRCLE_SEGMENTS * 3]; - for (int i = 0; i < CIRCLE_SEGMENTS; i++) - { - double a = i * Math.PI * 2 / CIRCLE_SEGMENTS; - unitCircle[3 * i] = (float)Math.Cos(a); - unitCircle[3 * i + 1] = 0; - unitCircle[3 * i + 2] = (float)-Math.Sin(a); - } - } - float[] circle = new float[12 * 3]; for (int i = 0; i < CIRCLE_SEGMENTS * 3; i += 3) { - circle[i] = unitCircle[i] * radius + center.x; + circle[i] = UnitCircle[i] * radius + center.x; circle[i + 1] = center.y; - circle[i + 2] = unitCircle[i + 2] * radius + center.z; + circle[i + 2] = UnitCircle[i + 2] * radius + center.z; } return circle; diff --git a/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs b/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs index 7e8cca9..2f07e0b 100644 --- a/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs @@ -147,10 +147,10 @@ public class CrowdProfilingTool private RcVec3f? GetMobPosition(DtNavMeshQuery navquery, IDtQueryFilter filter) { - Result result = navquery.FindRandomPoint(filter, rnd); - if (result.Succeeded()) + var status = navquery.FindRandomPoint(filter, rnd, out var randomRef, out var randomPt); + if (status.Succeeded()) { - return result.result.GetRandomPt(); + return randomPt; } return null; @@ -182,13 +182,13 @@ public class CrowdProfilingTool float zoneSeparation = zoneRadius * zoneRadius * 16; for (int k = 0; k < 100; k++) { - Result result = navquery.FindRandomPoint(filter, rnd); - if (result.Succeeded()) + var status = navquery.FindRandomPoint(filter, rnd, out var randomRef, out var randomPt); + if (status.Succeeded()) { bool valid = true; - foreach (FindRandomPointResult zone in zones) + foreach (var zone in zones) { - if (RcVec3f.DistSqr(zone.GetRandomPt(), result.result.GetRandomPt()) < zoneSeparation) + if (RcVec3f.DistSqr(zone.GetRandomPt(), randomPt) < zoneSeparation) { valid = false; break; @@ -197,7 +197,7 @@ public class CrowdProfilingTool if (valid) { - zones.Add(result.result); + zones.Add(new FindRandomPointResult(randomRef, randomPt)); break; } } diff --git a/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs b/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs index 407ef41..a99c7c0 100644 --- a/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs @@ -335,7 +335,7 @@ public class TestNavmeshTool : ITool if (m_polys[m_polys.Count - 1] != m_endRef) { var result = m_navQuery.ClosestPointOnPoly(m_polys[m_polys.Count - 1], m_epos, out var closest, out var _); - if (result.IsSuccess()) + if (result.Succeeded()) { epos = closest; } @@ -489,8 +489,9 @@ public class TestNavmeshTool : ITool float dz = m_epos.z - m_spos.z; float dist = (float)Math.Sqrt(dx * dx + dz * dz); IPolygonByCircleConstraint constraint = constrainByCircle - ? IPolygonByCircleConstraint.Strict() - : IPolygonByCircleConstraint.Noop(); + ? StrictPolygonByCircleConstraint.Strict + : NoOpPolygonByCircleConstraint.Noop; + for (int i = 0; i < 200; i++) { Result result = m_navQuery.FindRandomPointAroundCircle(m_startRef, m_spos, dist, @@ -978,12 +979,12 @@ public class TestNavmeshTool : ITool if (m_toolMode == TestNavmeshToolMode.PATHFIND_SLICED) { DtNavMeshQuery m_navQuery = m_sample.GetNavMeshQuery(); - if (m_pathFindStatus.IsInProgress()) + if (m_pathFindStatus.InProgress()) { m_pathFindStatus = m_navQuery.UpdateSlicedFindPath(1).status; } - if (m_pathFindStatus.IsSuccess()) + if (m_pathFindStatus.Succeeded()) { m_polys = m_navQuery.FinalizeSlicedFindPath().result; m_straightPath = null; @@ -995,7 +996,7 @@ public class TestNavmeshTool : ITool if (m_polys[m_polys.Count - 1] != m_endRef) { var result = m_navQuery.ClosestPointOnPoly(m_polys[m_polys.Count - 1], m_epos, out var closest, out var _); - if (result.IsSuccess()) + if (result.Succeeded()) { epos = closest; } diff --git a/test/DotRecast.Detour.Extras.Test/Unity/Astar/UnityAStarPathfindingImporterTest.cs b/test/DotRecast.Detour.Extras.Test/Unity/Astar/UnityAStarPathfindingImporterTest.cs index 0316598..510b9d3 100644 --- a/test/DotRecast.Detour.Extras.Test/Unity/Astar/UnityAStarPathfindingImporterTest.cs +++ b/test/DotRecast.Detour.Extras.Test/Unity/Astar/UnityAStarPathfindingImporterTest.cs @@ -49,7 +49,7 @@ public class UnityAStarPathfindingImporterTest RcVec3f startPos = RcVec3f.Of(22.93f, -2.37f, -5.11f); RcVec3f endPos = RcVec3f.Of(16.81f, -2.37f, 25.52f); Result> path = FindPath(mesh, startPos, endPos); - Assert.That(path.status.IsSuccess(), Is.True); + Assert.That(path.status.Succeeded(), Is.True); Assert.That(path.result.Count, Is.EqualTo(15)); SaveMesh(mesh, "v4_1_16"); } diff --git a/test/DotRecast.Detour.Test/FindPathTest.cs b/test/DotRecast.Detour.Test/FindPathTest.cs index da0acca..f79ac6b 100644 --- a/test/DotRecast.Detour.Test/FindPathTest.cs +++ b/test/DotRecast.Detour.Test/FindPathTest.cs @@ -160,7 +160,7 @@ public class FindPathTest : AbstractDetourTest var endPos = endPoss[i]; query.InitSlicedFindPath(startRef, endRef, startPos, endPos, filter, DtNavMeshQuery.DT_FINDPATH_ANY_ANGLE); DtStatus status = DtStatus.DT_IN_PROGRESS; - while (status.IsInProgress()) + while (status.InProgress()) { Result res = query.UpdateSlicedFindPath(10); status = res.status; diff --git a/test/DotRecast.Detour.Test/PolygonByCircleConstraintTest.cs b/test/DotRecast.Detour.Test/PolygonByCircleConstraintTest.cs index 18a3cb7..f6b3b68 100644 --- a/test/DotRecast.Detour.Test/PolygonByCircleConstraintTest.cs +++ b/test/DotRecast.Detour.Test/PolygonByCircleConstraintTest.cs @@ -24,14 +24,14 @@ namespace DotRecast.Detour.Test; [Parallelizable] public class PolygonByCircleConstraintTest { - private readonly IPolygonByCircleConstraint constraint = new StrictPolygonByCircleConstraint(); + private readonly IPolygonByCircleConstraint _constraint = StrictPolygonByCircleConstraint.Strict; [Test] public void ShouldHandlePolygonFullyInsideCircle() { float[] polygon = { -2, 0, 2, 2, 0, 2, 2, 0, -2, -2, 0, -2 }; RcVec3f center = RcVec3f.Of(1, 0, 1); - float[] constrained = constraint.Aply(polygon, center, 6); + float[] constrained = _constraint.Aply(polygon, center, 6); Assert.That(constrained, Is.EqualTo(polygon)); } @@ -43,7 +43,7 @@ public class PolygonByCircleConstraintTest float[] polygon = { -2, 0, 2, 2, 0, 2, 2, 0, -2, -2, 0, -2 }; RcVec3f center = RcVec3f.Of(2, 0, 0); - float[] constrained = constraint.Aply(polygon, center, 3); + float[] constrained = _constraint.Aply(polygon, center, 3); Assert.That(constrained.Length, Is.EqualTo(expectedSize)); Assert.That(constrained, Is.SupersetOf(new[] { 2f, 0f, 2f, 2f, 0f, -2f })); } @@ -54,7 +54,7 @@ public class PolygonByCircleConstraintTest int expectedSize = 12 * 3; float[] polygon = { -4, 0, 0, -3, 0, 3, 2, 0, 3, 3, 0, -3, -2, 0, -4 }; RcVec3f center = RcVec3f.Of(-1, 0, -1); - float[] constrained = constraint.Aply(polygon, center, 2); + float[] constrained = _constraint.Aply(polygon, center, 2); Assert.That(constrained.Length, Is.EqualTo(expectedSize)); @@ -72,7 +72,7 @@ public class PolygonByCircleConstraintTest int expectedSize = 9 * 3; float[] polygon = { -4, 0, 0, -3, 0, 3, 2, 0, 3, 3, 0, -3, -2, 0, -4 }; RcVec3f center = RcVec3f.Of(-2, 0, -1); - float[] constrained = constraint.Aply(polygon, center, 3); + 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 })); @@ -84,7 +84,7 @@ public class PolygonByCircleConstraintTest int expectedSize = 7 * 3; float[] polygon = { -4, 0, 0, -3, 0, 3, 2, 0, 3, 3, 0, -3, -2, 0, -4 }; RcVec3f center = RcVec3f.Of(4, 0, 0); - float[] constrained = constraint.Aply(polygon, center, 4); + 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 })); diff --git a/test/DotRecast.Detour.Test/RandomPointTest.cs b/test/DotRecast.Detour.Test/RandomPointTest.cs index 2cb7182..14b0386 100644 --- a/test/DotRecast.Detour.Test/RandomPointTest.cs +++ b/test/DotRecast.Detour.Test/RandomPointTest.cs @@ -35,9 +35,9 @@ public class RandomPointTest : AbstractDetourTest IDtQueryFilter filter = new DtQueryDefaultFilter(); for (int i = 0; i < 1000; i++) { - Result point = query.FindRandomPoint(filter, f); - Assert.That(point.Succeeded(), Is.True); - Tuple tileAndPoly = navmesh.GetTileAndPolyByRef(point.result.GetRandomRef()).result; + var status = query.FindRandomPoint(filter, f, out var randomRef, out var randomPt); + Assert.That(status.Succeeded(), Is.True); + Tuple tileAndPoly = navmesh.GetTileAndPolyByRef(randomRef).result; float[] bmin = new float[2]; float[] bmax = new float[2]; for (int j = 0; j < tileAndPoly.Item2.vertCount; j++) @@ -49,10 +49,10 @@ public class RandomPointTest : AbstractDetourTest bmax[1] = j == 0 ? tileAndPoly.Item1.data.verts[v + 2] : Math.Max(bmax[1], tileAndPoly.Item1.data.verts[v + 2]); } - Assert.That(point.result.GetRandomPt().x >= bmin[0], Is.True); - Assert.That(point.result.GetRandomPt().x <= bmax[0], Is.True); - Assert.That(point.result.GetRandomPt().z >= bmin[1], Is.True); - Assert.That(point.result.GetRandomPt().z <= bmax[1], Is.True); + Assert.That(randomPt.x >= bmin[0], Is.True); + Assert.That(randomPt.x <= bmax[0], Is.True); + Assert.That(randomPt.z >= bmin[1], Is.True); + Assert.That(randomPt.z <= bmax[1], Is.True); } } @@ -61,14 +61,15 @@ public class RandomPointTest : AbstractDetourTest { FRand f = new FRand(1); IDtQueryFilter filter = new DtQueryDefaultFilter(); - FindRandomPointResult point = query.FindRandomPoint(filter, f).result; + query.FindRandomPoint(filter, f, out var randomRef, out var randomPt); for (int i = 0; i < 1000; i++) { - Result result = query.FindRandomPointAroundCircle(point.GetRandomRef(), point.GetRandomPt(), - 5f, filter, f); + Result result = query.FindRandomPointAroundCircle(randomRef, randomPt, 5f, filter, f); Assert.That(result.Failed(), Is.False); - point = result.result; - Tuple tileAndPoly = navmesh.GetTileAndPolyByRef(point.GetRandomRef()).result; + + randomRef = result.result.GetRandomRef(); + randomPt = result.result.GetRandomPt(); + Tuple tileAndPoly = navmesh.GetTileAndPolyByRef(randomRef).result; float[] bmin = new float[2]; float[] bmax = new float[2]; for (int j = 0; j < tileAndPoly.Item2.vertCount; j++) @@ -80,10 +81,10 @@ public class RandomPointTest : AbstractDetourTest bmax[1] = j == 0 ? tileAndPoly.Item1.data.verts[v + 2] : Math.Max(bmax[1], tileAndPoly.Item1.data.verts[v + 2]); } - Assert.That(point.GetRandomPt().x >= bmin[0], Is.True); - Assert.That(point.GetRandomPt().x <= bmax[0], Is.True); - Assert.That(point.GetRandomPt().z >= bmin[1], Is.True); - Assert.That(point.GetRandomPt().z <= bmax[1], Is.True); + Assert.That(randomPt.x >= bmin[0], Is.True); + Assert.That(randomPt.x <= bmax[0], Is.True); + Assert.That(randomPt.z >= bmin[1], Is.True); + Assert.That(randomPt.z <= bmax[1], Is.True); } } @@ -92,16 +93,18 @@ public class RandomPointTest : AbstractDetourTest { FRand f = new FRand(1); IDtQueryFilter filter = new DtQueryDefaultFilter(); - FindRandomPointResult point = query.FindRandomPoint(filter, f).result; + query.FindRandomPoint(filter, f, out var randomRef, out var randomPt); float radius = 5f; for (int i = 0; i < 1000; i++) { - Result result = query.FindRandomPointWithinCircle(point.GetRandomRef(), point.GetRandomPt(), - radius, filter, f); + Result result = query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f); Assert.That(result.Failed(), Is.False); - float distance = RcVec3f.Dist2D(point.GetRandomPt(), result.result.GetRandomPt()); + + float distance = RcVec3f.Dist2D(randomPt, result.result.GetRandomPt()); Assert.That(distance <= radius, Is.True); - point = result.result; + + randomRef = result.result.GetRandomRef(); + randomPt = result.result.GetRandomPt(); } } @@ -110,33 +113,34 @@ public class RandomPointTest : AbstractDetourTest { FRand f = new FRand(1); IDtQueryFilter filter = new DtQueryDefaultFilter(); - FindRandomPointResult point = query.FindRandomPoint(filter, f).result; + query.FindRandomPoint(filter, f, out var randomRef, out var randomPt); + float radius = 5f; // jvm warmup for (int i = 0; i < 1000; i++) { - query.FindRandomPointAroundCircle(point.GetRandomRef(), point.GetRandomPt(), radius, filter, f); + query.FindRandomPointAroundCircle(randomRef, randomPt, radius, filter, f); } for (int i = 0; i < 1000; i++) { - query.FindRandomPointWithinCircle(point.GetRandomRef(), point.GetRandomPt(), radius, filter, f); + query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f); } long t1 = RcFrequency.Ticks; for (int i = 0; i < 10000; i++) { - query.FindRandomPointAroundCircle(point.GetRandomRef(), point.GetRandomPt(), radius, filter, f); + query.FindRandomPointAroundCircle(randomRef, randomPt, radius, filter, f); } long t2 = RcFrequency.Ticks; for (int i = 0; i < 10000; i++) { - query.FindRandomPointWithinCircle(point.GetRandomRef(), point.GetRandomPt(), radius, filter, f); + query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f); } long t3 = RcFrequency.Ticks; Console.WriteLine("Random point around circle: " + (t2 - t1) / TimeSpan.TicksPerMillisecond + "ms"); Console.WriteLine("Random point within circle: " + (t3 - t2) / TimeSpan.TicksPerMillisecond + "ms"); } -} +} \ No newline at end of file