From bb047c5a1d472b3c9cf0739b5a772f7f7b439a8d Mon Sep 17 00:00:00 2001 From: ikpil Date: Wed, 5 Jul 2023 00:03:37 +0900 Subject: [PATCH] reuse collection --- src/DotRecast.Detour.Crowd/DtPathCorridor.cs | 6 ++-- src/DotRecast.Detour/DtNavMeshQuery.cs | 18 +++++++---- .../Tools/TestNavmeshTool.cs | 13 +++++--- src/DotRecast.Recast.DemoTool/Sample.cs | 32 +++++++++++++++++++ .../Tools/TestNavmeshToolImpl.cs | 3 +- .../FindPolysAroundCircleTest.cs | 9 ++++-- .../MoveAlongSurfaceTest.cs | 4 ++- 7 files changed, 67 insertions(+), 18 deletions(-) diff --git a/src/DotRecast.Detour.Crowd/DtPathCorridor.cs b/src/DotRecast.Detour.Crowd/DtPathCorridor.cs index 0a8a02c..a172a57 100644 --- a/src/DotRecast.Detour.Crowd/DtPathCorridor.cs +++ b/src/DotRecast.Detour.Crowd/DtPathCorridor.cs @@ -303,7 +303,8 @@ namespace DotRecast.Detour.Crowd public bool MovePosition(RcVec3f npos, DtNavMeshQuery navquery, IDtQueryFilter filter) { // Move along navmesh and update new position. - var status = navquery.MoveAlongSurface(m_path[0], m_pos, npos, filter, out var result, out var visited); + var visited = new List(); + var status = navquery.MoveAlongSurface(m_path[0], m_pos, npos, filter, out var result, ref visited); if (status.Succeeded()) { m_path = PathUtils.MergeCorridorStartMoved(m_path, visited); @@ -342,7 +343,8 @@ namespace DotRecast.Detour.Crowd public bool MoveTargetPosition(RcVec3f npos, DtNavMeshQuery navquery, IDtQueryFilter filter) { // Move along navmesh and update new position. - var status = navquery.MoveAlongSurface(m_path[m_path.Count - 1], m_target, npos, filter, out var result, out var visited); + var visited = new List(); + var status = navquery.MoveAlongSurface(m_path[m_path.Count - 1], m_target, npos, filter, out var result, ref visited); if (status.Succeeded()) { m_path = PathUtils.MergeCorridorEndMoved(m_path, visited); diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index f0fdd6d..330b449 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -1798,10 +1798,12 @@ namespace DotRecast.Detour /// @returns The status flags for the query. public DtStatus MoveAlongSurface(long startRef, RcVec3f startPos, RcVec3f endPos, IDtQueryFilter filter, - out RcVec3f resultPos, out List visited) + out RcVec3f resultPos, ref List visited) { resultPos = RcVec3f.Zero; - visited = new List(); + + if (null != visited) + visited.Clear(); // Validate input if (!m_nav.IsValidPolyRef(startRef) || !RcVec3f.IsFinite(startPos) @@ -2452,12 +2454,14 @@ namespace DotRecast.Detour /// @param[in] maxResult The maximum number of polygons the result arrays can hold. /// @returns The status flags for the query. public DtStatus FindPolysAroundCircle(long startRef, RcVec3f centerPos, float radius, IDtQueryFilter filter, - out List resultRef, out List resultParent, out List resultCost) + ref List resultRef, ref List resultParent, ref List resultCost) { - // TODO : check performance - resultRef = new List(); - resultParent = new List(); - resultCost = new List(); + if (null != resultRef) + { + resultRef.Clear(); + resultParent.Clear(); + resultCost.Clear(); + } // Validate input if (!m_nav.IsValidPolyRef(startRef) || !RcVec3f.IsFinite(centerPos) || radius < 0 diff --git a/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs b/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs index d07c04f..7f351b4 100644 --- a/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs @@ -24,15 +24,15 @@ public class TestNavmeshTool : IRcTool private RcVec3f m_epos; private long m_startRef; private long m_endRef; - + private readonly DtQueryDefaultFilter m_filter; private readonly RcVec3f m_polyPickExt = RcVec3f.Of(2, 4, 2); - + // for hit private RcVec3f m_hitPos; private RcVec3f m_hitNormal; private bool m_hitResult; - + private float m_distanceToWall; private List m_straightPath; private List m_polys; @@ -281,7 +281,12 @@ public class TestNavmeshTool : IRcTool float dx = m_epos.x - m_spos.x; float dz = m_epos.z - m_spos.z; float dist = (float)Math.Sqrt(dx * dx + dz * dz); - var status = m_navQuery.FindPolysAroundCircle(m_startRef, m_spos, dist, m_filter, out var refs, out var parentRefs, out var costs); + + List refs = new(); + List parentRefs = new(); + List costs = new(); + + var status = m_navQuery.FindPolysAroundCircle(m_startRef, m_spos, dist, m_filter, ref refs, ref parentRefs, ref costs); if (status.Succeeded()) { m_polys = refs; diff --git a/src/DotRecast.Recast.DemoTool/Sample.cs b/src/DotRecast.Recast.DemoTool/Sample.cs index 334d2ef..8908a83 100644 --- a/src/DotRecast.Recast.DemoTool/Sample.cs +++ b/src/DotRecast.Recast.DemoTool/Sample.cs @@ -18,6 +18,7 @@ freely, subject to the following restrictions: 3. This notice may not be removed or altered from any source distribution. */ +using System; using System.Collections.Generic; using DotRecast.Detour; using DotRecast.Recast.DemoTool.Geom; @@ -90,7 +91,38 @@ namespace DotRecast.Recast.DemoTool _recastResults = recastResults; _navMesh = navMesh; SetQuery(navMesh); + _changed = true; + + // // by update + // _inputGeom.ClearConvexVolumes(); + // _inputGeom.RemoveOffMeshConnections(x => true); + // + // if (null != _navMesh && 0 < _navMesh.GetTileCount()) + // { + // for (int ti = 0; ti < _navMesh.GetTileCount(); ++ti) + // { + // var tile = _navMesh.GetTile(ti); + // for (int pi = 0; pi < tile.data.polys.Length; ++pi) + // { + // var polyType = tile.data.polys[pi].GetPolyType(); + // var polyArea= tile.data.polys[pi].GetArea(); + // + // if (0 != polyType) + // { + // int a = 3; + // } + // + // if (0 != polyArea) + // { + // int b = 3; + // } + // + // Console.WriteLine($"tileIdx({ti}) polyIdx({pi}) polyType({polyType} polyArea({polyArea})"); + // } + // + // } + // } } } } \ No newline at end of file diff --git a/src/DotRecast.Recast.DemoTool/Tools/TestNavmeshToolImpl.cs b/src/DotRecast.Recast.DemoTool/Tools/TestNavmeshToolImpl.cs index dd19156..a6c8d70 100644 --- a/src/DotRecast.Recast.DemoTool/Tools/TestNavmeshToolImpl.cs +++ b/src/DotRecast.Recast.DemoTool/Tools/TestNavmeshToolImpl.cs @@ -60,6 +60,7 @@ namespace DotRecast.Recast.DemoTool.Tools smoothPath.Clear(); smoothPath.Add(iterPos); + var visited = new List(); // Move towards target a small advancement at a time until target reached or // when ran out of memory to store the path. @@ -96,7 +97,7 @@ namespace DotRecast.Recast.DemoTool.Tools RcVec3f moveTgt = RcVec3f.Mad(iterPos, delta, len); // Move - navQuery.MoveAlongSurface(polys[0], iterPos, moveTgt, filter, out var result, out var visited); + navQuery.MoveAlongSurface(polys[0], iterPos, moveTgt, filter, out var result, ref visited); iterPos = result; diff --git a/test/DotRecast.Detour.Test/FindPolysAroundCircleTest.cs b/test/DotRecast.Detour.Test/FindPolysAroundCircleTest.cs index 226465c..db06c1c 100644 --- a/test/DotRecast.Detour.Test/FindPolysAroundCircleTest.cs +++ b/test/DotRecast.Detour.Test/FindPolysAroundCircleTest.cs @@ -16,8 +16,8 @@ freely, subject to the following restrictions: 3. This notice may not be removed or altered from any source distribution. */ +using System.Collections.Generic; using DotRecast.Core; - using NUnit.Framework; namespace DotRecast.Detour.Test; @@ -102,13 +102,16 @@ public class FindPolysAroundCircleTest : AbstractDetourTest public void TestFindPolysAroundCircle() { IDtQueryFilter filter = new DtQueryDefaultFilter(); + var refs = new List(); + var parentRefs = new List(); + var costs = new List(); for (int i = 0; i < startRefs.Length; i++) { long startRef = startRefs[i]; RcVec3f startPos = startPoss[i]; - var status = query.FindPolysAroundCircle(startRef, startPos, 7.5f, filter, out var refs, out var parentRefs, out var costs); + var status = query.FindPolysAroundCircle(startRef, startPos, 7.5f, filter, ref refs, ref parentRefs, ref costs); Assert.That(status.Succeeded(), Is.True); - + Assert.That(refs.Count, Is.EqualTo(REFS[i].Length)); for (int v = 0; v < REFS[i].Length; v++) { diff --git a/test/DotRecast.Detour.Test/MoveAlongSurfaceTest.cs b/test/DotRecast.Detour.Test/MoveAlongSurfaceTest.cs index 6f6d7c2..1b05e07 100644 --- a/test/DotRecast.Detour.Test/MoveAlongSurfaceTest.cs +++ b/test/DotRecast.Detour.Test/MoveAlongSurfaceTest.cs @@ -16,6 +16,7 @@ freely, subject to the following restrictions: 3. This notice may not be removed or altered from any source distribution. */ +using System.Collections.Generic; using DotRecast.Core; using NUnit.Framework; @@ -69,12 +70,13 @@ public class MoveAlongSurfaceTest : AbstractDetourTest public void TestMoveAlongSurface() { IDtQueryFilter filter = new DtQueryDefaultFilter(); + var visited = new List(); for (int i = 0; i < startRefs.Length; i++) { long startRef = startRefs[i]; RcVec3f startPos = startPoss[i]; RcVec3f endPos = endPoss[i]; - var status = query.MoveAlongSurface(startRef, startPos, endPos, filter, out var result, out var visited); + var status = query.MoveAlongSurface(startRef, startPos, endPos, filter, out var result, ref visited); Assert.That(status.Succeeded(), Is.True); for (int v = 0; v < 3; v++)