Skip to content

Commit

Permalink
preparatory work to resolve the SOH issue during path merging.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed May 7, 2024
1 parent 19e358b commit cfdcc13
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/DotRecast.Detour.Crowd/DtCrowd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ private void UpdateMoveRequest(IList<DtCrowdAgent> agents, float dt)
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
{
Expand Down
4 changes: 2 additions & 2 deletions src/DotRecast.Detour.Crowd/DtPathCorridor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public int FindCorners(ref List<DtStraightPath> corners, int maxCorners, DtNavMe
{
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.
Expand Down Expand Up @@ -246,7 +246,7 @@ public bool OptimizePathTopology(DtNavMeshQuery navquery, IDtQueryFilter filter,
var res = new List<long>();
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)
{
Expand Down
34 changes: 18 additions & 16 deletions src/DotRecast.Detour/DtNavMeshQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1363,19 +1363,21 @@ public virtual DtStatus FinalizeSlicedFindPath(ref List<long> path)

/// 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<long> existing, ref List<long> path)
public virtual DtStatus FinalizeSlicedFindPathPartial(List<long> existing, int existingSize, ref List<long> 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;
}
Expand All @@ -1396,7 +1398,7 @@ public virtual DtStatus FinalizeSlicedFindPathPartial(List<long> existing, ref L
{
// 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)
Expand Down Expand Up @@ -1527,12 +1529,12 @@ protected DtStatus AppendPortals(int startIdx, int endIdx, RcVec3f endPos, List<
/// @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<long> path,
public virtual DtStatus FindStraightPath(RcVec3f startPos, RcVec3f endPos, List<long> path, int pathSize,
ref List<DtStraightPath> 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;
}
Expand All @@ -1546,7 +1548,7 @@ public virtual DtStatus FindStraightPath(RcVec3f startPos, RcVec3f endPos, List<
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;
Expand All @@ -1559,7 +1561,7 @@ public virtual DtStatus FindStraightPath(RcVec3f startPos, RcVec3f endPos, List<
return stat;
}

if (path.Count > 1)
if (pathSize > 1)
{
RcVec3f portalApex = closestStartPos;
RcVec3f portalLeft = portalApex;
Expand All @@ -1574,13 +1576,13 @@ public virtual DtStatus FindStraightPath(RcVec3f startPos, RcVec3f endPos, List<
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.

Expand Down Expand Up @@ -1633,7 +1635,7 @@ public virtual DtStatus FindStraightPath(RcVec3f startPos, RcVec3f endPos, List<
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;
}
Expand Down Expand Up @@ -1689,7 +1691,7 @@ public virtual DtStatus FindStraightPath(RcVec3f startPos, RcVec3f endPos, List<
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;
}
Expand Down Expand Up @@ -1743,7 +1745,7 @@ public virtual DtStatus FindStraightPath(RcVec3f startPos, RcVec3f endPos, List<
// 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;
Expand Down
4 changes: 2 additions & 2 deletions src/DotRecast.Detour/DtPathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class DtPathUtils

public static bool GetSteerTarget(DtNavMeshQuery navQuery, RcVec3f startPos, RcVec3f endPos,
float minTargetDist,
List<long> path,
List<long> path, int pathSize,
out RcVec3f steerPos, out int steerPosFlag, out long steerPosRef)
{
steerPos = RcVec3f.Zero;
Expand All @@ -40,7 +40,7 @@ public static bool GetSteerTarget(DtNavMeshQuery navQuery, RcVec3f startPos, RcV

// Find steer target.
var straightPath = new List<DtStraightPath>(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;
Expand Down
6 changes: 3 additions & 3 deletions src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public DtStatus FindFollowPath(DtNavMesh navMesh, DtNavMeshQuery navQuery, long
{
// 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;
}
Expand Down Expand Up @@ -200,7 +200,7 @@ public DtStatus FindStraightPath(DtNavMeshQuery navQuery, long startRef, long en
}
}

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;
}
Expand Down Expand Up @@ -245,7 +245,7 @@ public DtStatus UpdateSlicedFindPath(DtNavMeshQuery navQuery, int maxIter, long
}

straightPath = new List<DtStraightPath>(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;
Expand Down
6 changes: 4 additions & 2 deletions test/DotRecast.Detour.Crowd.Test/PathCorridorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ public void ShouldKeepOriginalPathInFindCornersWhenNothingCanBePruned()
It.IsAny<RcVec3f>(),
It.IsAny<RcVec3f>(),
It.IsAny<List<long>>(),
It.IsAny<int>(),
ref It.Ref<List<DtStraightPath>>.IsAny,
It.IsAny<int>(),
It.IsAny<int>())
)
.Callback((RcVec3f startPos, RcVec3f endPos, List<long> path,
.Callback((RcVec3f startPos, RcVec3f endPos, List<long> path, int pathSize,
ref List<DtStraightPath> refStraightPath, int maxStraightPath, int options) =>
{
refStraightPath = straightPath;
Expand Down Expand Up @@ -83,10 +84,11 @@ public void ShouldPrunePathInFindCorners()
It.IsAny<RcVec3f>(),
It.IsAny<RcVec3f>(),
It.IsAny<List<long>>(),
It.IsAny<int>(),
ref It.Ref<List<DtStraightPath>>.IsAny,
It.IsAny<int>(),
It.IsAny<int>())
).Callback((RcVec3f startPos, RcVec3f endPos, List<long> path,
).Callback((RcVec3f startPos, RcVec3f endPos, List<long> path, int pathSize,
ref List<DtStraightPath> refStraightPath, int maxStraightPath, int options) =>
{
refStraightPath = straightPath;
Expand Down
2 changes: 1 addition & 1 deletion test/DotRecast.Detour.Test/FindPathTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void TestFindPathStraight()
var endPos = endPoss[i];
var status = query.FindPath(startRef, endRef, startPos, endPos, filter, ref path, DtFindPathOption.NoOption);
var straightPath = new List<DtStraightPath>();
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++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void TestFindPath()
int options = 0;

var pathStr = new List<DtStraightPath>();
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));
}
}

0 comments on commit cfdcc13

Please sign in to comment.