From 36ad2e14989340c3a8364a00b5f18b37cf3f11f6 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 11 Jun 2023 13:41:37 +0900 Subject: [PATCH] remove FindNearestPolyResult ok --- src/DotRecast.Detour.Crowd/DtCrowd.cs | 16 +++--- src/DotRecast.Detour/DtNavMesh.cs | 20 +++---- .../QueryResults/FindNearestPolyResult.cs | 55 ------------------- .../Tools/CrowdProfilingTool.cs | 4 +- src/DotRecast.Recast.Demo/Tools/CrowdTool.cs | 2 +- .../AbstractCrowdTest.cs | 4 +- .../UnityAStarPathfindingImporterTest.cs | 6 +- .../FindNearestPolyTest.cs | 8 +-- 8 files changed, 29 insertions(+), 86 deletions(-) delete mode 100644 src/DotRecast.Detour/QueryResults/FindNearestPolyResult.cs diff --git a/src/DotRecast.Detour.Crowd/DtCrowd.cs b/src/DotRecast.Detour.Crowd/DtCrowd.cs index 482b9b4..0c7f4be 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowd.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowd.cs @@ -245,14 +245,14 @@ namespace DotRecast.Detour.Crowd UpdateAgentParameters(ag, option); // Find nearest position on navmesh and place the agent there. - var status = _navQuery.FindNearestPoly(pos, _ext, _filters[ag.option.queryFilterType], out var refs, out var nearest, out var _); + var status = _navQuery.FindNearestPoly(pos, _ext, _filters[ag.option.queryFilterType], out var refs, out var nearestPt, out var _); if (status.Failed()) { - nearest = pos; + nearestPt = pos; refs = 0; } - ag.corridor.Reset(refs, nearest); + ag.corridor.Reset(refs, nearestPt); ag.boundary.Reset(); ag.partial = false; @@ -262,7 +262,7 @@ namespace DotRecast.Detour.Crowd ag.dvel = RcVec3f.Zero; ag.nvel = RcVec3f.Zero; ag.vel = RcVec3f.Zero; - ag.npos = nearest; + ag.npos = nearestPt; ag.desiredSpeed = 0; @@ -465,8 +465,8 @@ namespace DotRecast.Detour.Crowd { // Current location is not valid, try to reposition. // TODO: this can snap agents, how to handle that? - _navQuery.FindNearestPoly(ag.npos, _ext, _filters[ag.option.queryFilterType], out agentRef, out var nearest, out var _); - agentPos = nearest; + _navQuery.FindNearestPoly(ag.npos, _ext, _filters[ag.option.queryFilterType], out agentRef, out var nearestPt, out var _); + agentPos = nearestPt; if (agentRef == 0) { @@ -505,8 +505,8 @@ namespace DotRecast.Detour.Crowd if (!_navQuery.IsValidPolyRef(ag.targetRef, _filters[ag.option.queryFilterType])) { // Current target is not valid, try to reposition. - _navQuery.FindNearestPoly(ag.targetPos, _ext, _filters[ag.option.queryFilterType], out ag.targetRef, out var nearest, out var _); - ag.targetPos = nearest; + _navQuery.FindNearestPoly(ag.targetPos, _ext, _filters[ag.option.queryFilterType], out ag.targetRef, out var nearestPt, out var _); + ag.targetPos = nearestPt; replan = true; } diff --git a/src/DotRecast.Detour/DtNavMesh.cs b/src/DotRecast.Detour/DtNavMesh.cs index 080bea1..dd34e76 100644 --- a/src/DotRecast.Detour/DtNavMesh.cs +++ b/src/DotRecast.Detour/DtNavMesh.cs @@ -840,14 +840,12 @@ namespace DotRecast.Detour p.x = targetCon.pos[3]; p.y = targetCon.pos[4]; p.z = targetCon.pos[5]; - FindNearestPolyResult nearest = FindNearestPolyInTile(tile, p, ext); - long refs = nearest.GetNearestRef(); + var refs = FindNearestPolyInTile(tile, p, ext, out var nearestPt); if (refs == 0) { continue; } - var nearestPt = nearest.GetNearestPos(); // findNearestPoly may return too optimistic results, further check // to make sure. @@ -1070,15 +1068,13 @@ namespace DotRecast.Detour }; // Find polygon to connect to. - FindNearestPolyResult nearestPoly = FindNearestPolyInTile(tile, RcVec3f.Of(con.pos), ext); - long refs = nearestPoly.GetNearestRef(); + var refs = FindNearestPolyInTile(tile, RcVec3f.Of(con.pos), ext, out var nearestPt); if (refs == 0) { continue; } float[] p = con.pos; // First vertex - RcVec3f nearestPt = nearestPoly.GetNearestPos(); // findNearestPoly may return too optimistic results, further check // to make sure. if (Sqr(nearestPt.x - p[0]) + Sqr(nearestPt.z - p[2]) > Sqr(con.rad)) @@ -1343,12 +1339,14 @@ namespace DotRecast.Detour closest = ClosestPointOnDetailEdges(tile, poly, pos, true); } - FindNearestPolyResult FindNearestPolyInTile(DtMeshTile tile, RcVec3f center, RcVec3f extents) + /// Find nearest polygon within a tile. + private long FindNearestPolyInTile(DtMeshTile tile, RcVec3f center, RcVec3f halfExtents, out RcVec3f nearestPt) { - RcVec3f nearestPt = new RcVec3f(); + nearestPt = RcVec3f.Zero; + bool overPoly = false; - RcVec3f bmin = center.Subtract(extents); - RcVec3f bmax = center.Add(extents); + RcVec3f bmin = center.Subtract(halfExtents); + RcVec3f bmax = center.Add(halfExtents); // Get nearby polygons from proximity grid. List polys = QueryPolygonsInTile(tile, bmin, bmax); @@ -1384,7 +1382,7 @@ namespace DotRecast.Detour } } - return new FindNearestPolyResult(nearest, nearestPt, overPoly); + return nearest; } DtMeshTile GetTileAt(int x, int y, int layer) diff --git a/src/DotRecast.Detour/QueryResults/FindNearestPolyResult.cs b/src/DotRecast.Detour/QueryResults/FindNearestPolyResult.cs deleted file mode 100644 index 3398dde..0000000 --- a/src/DotRecast.Detour/QueryResults/FindNearestPolyResult.cs +++ /dev/null @@ -1,55 +0,0 @@ -/* -Copyright (c) 2009-2010 Mikko Mononen memon@inside.org -recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org -DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com - -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. -*/ - -using DotRecast.Core; - -namespace DotRecast.Detour.QueryResults -{ - public class FindNearestPolyResult - { - private readonly long nearestRef; - private readonly RcVec3f nearestPos; - private readonly bool overPoly; - - public FindNearestPolyResult(long nearestRef, RcVec3f nearestPos, bool overPoly) - { - this.nearestRef = nearestRef; - this.nearestPos = nearestPos; - this.overPoly = overPoly; - } - - /** Returns the reference id of the nearest polygon. 0 if no polygon is found. */ - public long GetNearestRef() - { - return nearestRef; - } - - /** Returns the nearest point on the polygon. [opt] [(x, y, z)]. Unchanged if no polygon is found. */ - public RcVec3f GetNearestPos() - { - return nearestPos; - } - - public bool IsOverPoly() - { - return overPoly; - } - } -} \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs b/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs index 5e57f9f..c6bb13b 100644 --- a/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs @@ -279,7 +279,7 @@ public class CrowdProfilingTool private void MoveMob(DtNavMeshQuery navquery, IDtQueryFilter filter, DtCrowdAgent ag, CrowdAgentData crowAgentData) { // Move somewhere - var status = navquery.FindNearestPoly(ag.npos, crowd.GetQueryExtents(), filter, out var nearestRef, out var nearest, out var _); + var status = navquery.FindNearestPoly(ag.npos, crowd.GetQueryExtents(), filter, out var nearestRef, out var nearestPt, out var _); if (status.Succeeded()) { status = navquery.FindRandomPointAroundCircle(nearestRef, crowAgentData.home, zoneRadius * 2f, filter, rnd, @@ -294,7 +294,7 @@ public class CrowdProfilingTool private void MoveVillager(DtNavMeshQuery navquery, IDtQueryFilter filter, DtCrowdAgent ag, CrowdAgentData crowAgentData) { // Move somewhere close - var status = navquery.FindNearestPoly(ag.npos, crowd.GetQueryExtents(), filter, out var nearestRef, out var nearest, out var _); + var status = navquery.FindNearestPoly(ag.npos, crowd.GetQueryExtents(), filter, out var nearestRef, out var nearestPt, out var _); if (status.Succeeded()) { status = navquery.FindRandomPointAroundCircle(nearestRef, crowAgentData.home, zoneRadius * 0.2f, filter, rnd, diff --git a/src/DotRecast.Recast.Demo/Tools/CrowdTool.cs b/src/DotRecast.Recast.Demo/Tools/CrowdTool.cs index 1dfdf71..a4a903e 100644 --- a/src/DotRecast.Recast.Demo/Tools/CrowdTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/CrowdTool.cs @@ -158,7 +158,7 @@ public class CrowdTool : ITool { IDtQueryFilter filter = new DtQueryDefaultFilter(); RcVec3f halfExtents = crowd.GetQueryExtents(); - navquery.FindNearestPoly(p, halfExtents, filter, out var refs, out var nearest, out var _); + navquery.FindNearestPoly(p, halfExtents, filter, out var refs, out var nearestPt, out var _); if (refs != 0) { Result flags = nav.GetPolyFlags(refs); diff --git a/test/DotRecast.Detour.Crowd.Test/AbstractCrowdTest.cs b/test/DotRecast.Detour.Crowd.Test/AbstractCrowdTest.cs index 456cb24..8f1d4ff 100644 --- a/test/DotRecast.Detour.Crowd.Test/AbstractCrowdTest.cs +++ b/test/DotRecast.Detour.Crowd.Test/AbstractCrowdTest.cs @@ -143,10 +143,10 @@ public class AbstractCrowdTest } else { - query.FindNearestPoly(pos, ext, filter, out var refs, out var nearest, out var _); + query.FindNearestPoly(pos, ext, filter, out var nearestRef, out var nearestPt, out var _); foreach (DtCrowdAgent ag in crowd.GetActiveAgents()) { - crowd.RequestMoveTarget(ag, refs, nearest); + crowd.RequestMoveTarget(ag, nearestRef, nearestPt); } } } diff --git a/test/DotRecast.Detour.Extras.Test/Unity/Astar/UnityAStarPathfindingImporterTest.cs b/test/DotRecast.Detour.Extras.Test/Unity/Astar/UnityAStarPathfindingImporterTest.cs index 06c9027..3ae17aa 100644 --- a/test/DotRecast.Detour.Extras.Test/Unity/Astar/UnityAStarPathfindingImporterTest.cs +++ b/test/DotRecast.Detour.Extras.Test/Unity/Astar/UnityAStarPathfindingImporterTest.cs @@ -109,11 +109,11 @@ public class UnityAStarPathfindingImporterTest for (int i = 0; i < results.Length; i++) { RcVec3f position = positions[i]; - var status = query.FindNearestPoly(position, extents, filter, out var nearestRef, out var nearest, out var _); + var status = query.FindNearestPoly(position, extents, filter, out var nearestRef, out var nearestPt, out var _); Assert.That(status.Succeeded(), Is.True); - Assert.That(nearest, Is.Not.EqualTo(RcVec3f.Zero), "Nearest start position is null!"); + Assert.That(nearestPt, Is.Not.EqualTo(RcVec3f.Zero), "Nearest start position is null!"); - results[i] = new DtPolyPoint(nearestRef, nearest); + results[i] = new DtPolyPoint(nearestRef, nearestPt); } return results; diff --git a/test/DotRecast.Detour.Test/FindNearestPolyTest.cs b/test/DotRecast.Detour.Test/FindNearestPolyTest.cs index 4625d82..5e5be55 100644 --- a/test/DotRecast.Detour.Test/FindNearestPolyTest.cs +++ b/test/DotRecast.Detour.Test/FindNearestPolyTest.cs @@ -42,12 +42,12 @@ public class FindNearestPolyTest : AbstractDetourTest for (int i = 0; i < startRefs.Length; i++) { RcVec3f startPos = startPoss[i]; - var status = query.FindNearestPoly(startPos, extents, filter, out var nearestRef, out var nearest, out var _); + var status = query.FindNearestPoly(startPos, extents, filter, out var nearestRef, out var nearestPt, out var _); Assert.That(status.Succeeded(), Is.True); Assert.That(nearestRef, Is.EqualTo(POLY_REFS[i])); for (int v = 0; v < POLY_POS[i].Length; v++) { - Assert.That(nearest[v], Is.EqualTo(POLY_POS[i][v]).Within(0.001f)); + Assert.That(nearestPt[v], Is.EqualTo(POLY_POS[i][v]).Within(0.001f)); } } } @@ -61,12 +61,12 @@ public class FindNearestPolyTest : AbstractDetourTest for (int i = 0; i < startRefs.Length; i++) { RcVec3f startPos = startPoss[i]; - var status = query.FindNearestPoly(startPos, extents, filter, out var nearestRef, out var nearest, out var _); + var status = query.FindNearestPoly(startPos, extents, filter, out var nearestRef, out var nearestPt, out var _); Assert.That(status.Succeeded(), Is.True); Assert.That(nearestRef, Is.EqualTo(0L)); for (int v = 0; v < POLY_POS[i].Length; v++) { - Assert.That(nearest[v], Is.EqualTo(startPos[v]).Within(0.001f)); + Assert.That(nearestPt[v], Is.EqualTo(startPos[v]).Within(0.001f)); } } }