From cfdcc1336c42a72d9483792b45ddeb7dba5af305 Mon Sep 17 00:00:00 2001 From: ikpil Date: Wed, 8 May 2024 00:25:32 +0900 Subject: [PATCH] preparatory work to resolve the SOH issue during path merging. --- src/DotRecast.Detour.Crowd/DtCrowd.cs | 2 +- src/DotRecast.Detour.Crowd/DtPathCorridor.cs | 4 +-- src/DotRecast.Detour/DtNavMeshQuery.cs | 34 ++++++++++--------- src/DotRecast.Detour/DtPathUtils.cs | 4 +-- .../Tools/RcTestNavMeshTool.cs | 6 ++-- .../PathCorridorTest.cs | 6 ++-- test/DotRecast.Detour.Test/FindPathTest.cs | 2 +- .../TileCacheFindPathTest.cs | 2 +- 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/DotRecast.Detour.Crowd/DtCrowd.cs b/src/DotRecast.Detour.Crowd/DtCrowd.cs index 892c4b3..2bbb1c1 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowd.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowd.cs @@ -589,7 +589,7 @@ namespace DotRecast.Detour.Crowd if (ag.targetReplan) // && npath > 10) { // Try to use existing steady path during replan if possible. - status = _navQuery.FinalizeSlicedFindPathPartial(path, ref reqPath); + status = _navQuery.FinalizeSlicedFindPathPartial(path, path.Count, ref reqPath); } else { diff --git a/src/DotRecast.Detour.Crowd/DtPathCorridor.cs b/src/DotRecast.Detour.Crowd/DtPathCorridor.cs index e7d56e8..67f9d27 100644 --- a/src/DotRecast.Detour.Crowd/DtPathCorridor.cs +++ b/src/DotRecast.Detour.Crowd/DtPathCorridor.cs @@ -137,7 +137,7 @@ namespace DotRecast.Detour.Crowd { const float MIN_TARGET_DIST = 0.01f; - var result = navquery.FindStraightPath(m_pos, m_target, m_path, ref corners, maxCorners, 0); + var result = navquery.FindStraightPath(m_pos, m_target, m_path, m_npath, ref corners, maxCorners, 0); if (result.Succeeded()) { // Prune points in the beginning of the path which are too close. @@ -246,7 +246,7 @@ namespace DotRecast.Detour.Crowd var res = new List(); navquery.InitSlicedFindPath(m_path[0], m_path[^1], m_pos, m_target, filter, 0); navquery.UpdateSlicedFindPath(maxIterations, out var _); - var status = navquery.FinalizeSlicedFindPathPartial(m_path, ref res); + var status = navquery.FinalizeSlicedFindPathPartial(m_path, m_npath, ref res); if (status.Succeeded() && res.Count > 0) { diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index a7b72a8..b3a6ab0 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -1363,19 +1363,21 @@ namespace DotRecast.Detour /// Finalizes and returns the results of an incomplete sliced path query, returning the path to the furthest /// polygon on the existing path that was visited during the search. - /// @param[in] existing An array of polygon references for the existing path. - /// @param[in] existingSize The number of polygon in the @p existing array. - /// @param[out] path An ordered list of polygon references representing the path. (Start to end.) - /// [(polyRef) * @p pathCount] + /// @param[in] existing An array of polygon references for the existing path. + /// @param[in] existingSize The number of polygon in the @p existing array. + /// @param[out] path An ordered list of polygon references representing the path. (Start to end.) + /// [(polyRef) * @p pathCount] + /// @param[out] pathCount The number of polygons returned in the @p path array. + /// @param[in] maxPath The max number of polygons the @p path array can hold. [Limit: >= 1] /// @returns The status flags for the query. - public virtual DtStatus FinalizeSlicedFindPathPartial(List existing, ref List path) + public virtual DtStatus FinalizeSlicedFindPathPartial(List existing, int existingSize, ref List path) { if (null == path) return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; path.Clear(); - if (null == existing || existing.Count <= 0) + if (null == existing || existingSize <= 0) { return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; } @@ -1396,7 +1398,7 @@ namespace DotRecast.Detour { // Find furthest existing node that was visited. DtNode node = null; - for (int i = existing.Count - 1; i >= 0; --i) + for (int i = existingSize - 1; i >= 0; --i) { node = m_nodePool.FindNode(existing[i]); if (node != null) @@ -1527,12 +1529,12 @@ namespace DotRecast.Detour /// @param[in] maxStraightPath The maximum number of points the straight path arrays can hold. [Limit: > 0] /// @param[in] options Query options. (see: #dtStraightPathOptions) /// @returns The status flags for the query. - public virtual DtStatus FindStraightPath(RcVec3f startPos, RcVec3f endPos, List path, + public virtual DtStatus FindStraightPath(RcVec3f startPos, RcVec3f endPos, List path, int pathSize, ref List straightPath, int maxStraightPath, int options) { if (!startPos.IsFinite() || !endPos.IsFinite() || null == straightPath - || null == path || 0 == path.Count || path[0] == 0 || maxStraightPath <= 0) + || null == path || pathSize <= 0 || path[0] == 0 || maxStraightPath <= 0) { return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; } @@ -1546,7 +1548,7 @@ namespace DotRecast.Detour return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; } - var closestEndPosRes = ClosestPointOnPolyBoundary(path[path.Count - 1], endPos, out var closestEndPos); + var closestEndPosRes = ClosestPointOnPolyBoundary(path[pathSize - 1], endPos, out var closestEndPos); if (closestEndPosRes.Failed()) { return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; @@ -1559,7 +1561,7 @@ namespace DotRecast.Detour return stat; } - if (path.Count > 1) + if (pathSize > 1) { RcVec3f portalApex = closestStartPos; RcVec3f portalLeft = portalApex; @@ -1574,13 +1576,13 @@ namespace DotRecast.Detour long leftPolyRef = path[0]; long rightPolyRef = path[0]; - for (int i = 0; i < path.Count; ++i) + for (int i = 0; i < pathSize; ++i) { RcVec3f left; RcVec3f right; int toType; - if (i + 1 < path.Count) + if (i + 1 < pathSize) { int fromType; // // fromType is ignored. @@ -1633,7 +1635,7 @@ namespace DotRecast.Detour if (DtUtils.VEqual(portalApex, portalRight) || DtUtils.TriArea2D(portalApex, portalLeft, right) > 0.0f) { portalRight = right; - rightPolyRef = (i + 1 < path.Count) ? path[i + 1] : 0; + rightPolyRef = (i + 1 < pathSize) ? path[i + 1] : 0; rightPolyType = toType; rightIndex = i; } @@ -1689,7 +1691,7 @@ namespace DotRecast.Detour if (DtUtils.VEqual(portalApex, portalLeft) || DtUtils.TriArea2D(portalApex, portalRight, left) < 0.0f) { portalLeft = left; - leftPolyRef = (i + 1 < path.Count) ? path[i + 1] : 0; + leftPolyRef = (i + 1 < pathSize) ? path[i + 1] : 0; leftPolyType = toType; leftIndex = i; } @@ -1743,7 +1745,7 @@ namespace DotRecast.Detour // Append portals along the current straight path segment. if ((options & (DtStraightPathOptions.DT_STRAIGHTPATH_AREA_CROSSINGS | DtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) { - stat = AppendPortals(apexIndex, path.Count - 1, closestEndPos, path, ref straightPath, maxStraightPath, options); + stat = AppendPortals(apexIndex, pathSize - 1, closestEndPos, path, ref straightPath, maxStraightPath, options); if (!stat.InProgress()) { return stat; diff --git a/src/DotRecast.Detour/DtPathUtils.cs b/src/DotRecast.Detour/DtPathUtils.cs index 998754a..72d7043 100644 --- a/src/DotRecast.Detour/DtPathUtils.cs +++ b/src/DotRecast.Detour/DtPathUtils.cs @@ -31,7 +31,7 @@ namespace DotRecast.Detour public static bool GetSteerTarget(DtNavMeshQuery navQuery, RcVec3f startPos, RcVec3f endPos, float minTargetDist, - List path, + List path, int pathSize, out RcVec3f steerPos, out int steerPosFlag, out long steerPosRef) { steerPos = RcVec3f.Zero; @@ -40,7 +40,7 @@ namespace DotRecast.Detour // Find steer target. var straightPath = new List(MAX_STEER_POINTS); - var result = navQuery.FindStraightPath(startPos, endPos, path, ref straightPath, MAX_STEER_POINTS, 0); + var result = navQuery.FindStraightPath(startPos, endPos, path, pathSize, ref straightPath, MAX_STEER_POINTS, 0); if (result.Failed()) { return false; diff --git a/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs b/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs index 11cbbf1..197d6a5 100644 --- a/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs +++ b/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs @@ -65,7 +65,7 @@ namespace DotRecast.Recast.Toolset.Tools { // Find location to steer towards. if (!DtPathUtils.GetSteerTarget(navQuery, iterPos, targetPos, SLOP, - pathIterPolys, out var steerPos, out var steerPosFlag, out var steerPosRef)) + pathIterPolys, pathIterPolyCount, out var steerPos, out var steerPosFlag, out var steerPosRef)) { break; } @@ -200,7 +200,7 @@ namespace DotRecast.Recast.Toolset.Tools } } - navQuery.FindStraightPath(startPt, epos, polys, ref straightPath, MAX_POLYS, straightPathOptions); + navQuery.FindStraightPath(startPt, epos, polys, polys.Count, ref straightPath, MAX_POLYS, straightPathOptions); return DtStatus.DT_SUCCESS; } @@ -245,7 +245,7 @@ namespace DotRecast.Recast.Toolset.Tools } straightPath = new List(MAX_POLYS); - navQuery.FindStraightPath(startPos, epos, path, ref straightPath, MAX_POLYS, DtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS); + navQuery.FindStraightPath(startPos, epos, path, path.Count, ref straightPath, MAX_POLYS, DtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS); } return DtStatus.DT_SUCCESS; diff --git a/test/DotRecast.Detour.Crowd.Test/PathCorridorTest.cs b/test/DotRecast.Detour.Crowd.Test/PathCorridorTest.cs index fdc10c1..3cb9169 100644 --- a/test/DotRecast.Detour.Crowd.Test/PathCorridorTest.cs +++ b/test/DotRecast.Detour.Crowd.Test/PathCorridorTest.cs @@ -51,11 +51,12 @@ public class PathCorridorTest It.IsAny(), It.IsAny(), It.IsAny>(), + It.IsAny(), ref It.Ref>.IsAny, It.IsAny(), It.IsAny()) ) - .Callback((RcVec3f startPos, RcVec3f endPos, List path, + .Callback((RcVec3f startPos, RcVec3f endPos, List path, int pathSize, ref List refStraightPath, int maxStraightPath, int options) => { refStraightPath = straightPath; @@ -83,10 +84,11 @@ public class PathCorridorTest It.IsAny(), It.IsAny(), It.IsAny>(), + It.IsAny(), ref It.Ref>.IsAny, It.IsAny(), It.IsAny()) - ).Callback((RcVec3f startPos, RcVec3f endPos, List path, + ).Callback((RcVec3f startPos, RcVec3f endPos, List path, int pathSize, ref List refStraightPath, int maxStraightPath, int options) => { refStraightPath = straightPath; diff --git a/test/DotRecast.Detour.Test/FindPathTest.cs b/test/DotRecast.Detour.Test/FindPathTest.cs index 4ce825a..32ad91a 100644 --- a/test/DotRecast.Detour.Test/FindPathTest.cs +++ b/test/DotRecast.Detour.Test/FindPathTest.cs @@ -193,7 +193,7 @@ public class FindPathTest : AbstractDetourTest var endPos = endPoss[i]; var status = query.FindPath(startRef, endRef, startPos, endPos, filter, ref path, DtFindPathOption.NoOption); var straightPath = new List(); - query.FindStraightPath(startPos, endPos, path, ref straightPath, int.MaxValue, 0); + query.FindStraightPath(startPos, endPos, path, path.Count, ref straightPath, int.MaxValue, 0); Assert.That(straightPath.Count, Is.EqualTo(STRAIGHT_PATHS[i].Length)); for (int j = 0; j < STRAIGHT_PATHS[i].Length; j++) { diff --git a/test/DotRecast.Detour.TileCache.Test/TileCacheFindPathTest.cs b/test/DotRecast.Detour.TileCache.Test/TileCacheFindPathTest.cs index f1735bd..2139074 100644 --- a/test/DotRecast.Detour.TileCache.Test/TileCacheFindPathTest.cs +++ b/test/DotRecast.Detour.TileCache.Test/TileCacheFindPathTest.cs @@ -60,7 +60,7 @@ public class TileCacheFindPathTest : AbstractTileCacheTest int options = 0; var pathStr = new List(); - query.FindStraightPath(startPos, endPos, path, ref pathStr, maxStraightPath, options); + query.FindStraightPath(startPos, endPos, path, path.Count, ref pathStr, maxStraightPath, options); Assert.That(pathStr.Count, Is.EqualTo(8)); } } \ No newline at end of file