Compare commits

...

5 Commits

Author SHA1 Message Date
wrenge 1315de063f Replaced arrays with spans in query. Replaced array allocation with buffer rent in query. 2024-11-26 20:05:00 +03:00
wrenge a6db4344e4 Queries change
(cherry picked from commit 2397f23fc3)
2024-11-26 20:00:22 +03:00
wrenge a7b9b772c4 Use rented array instead of allocating
(cherry picked from commit ff930712ee)
2024-11-26 19:59:47 +03:00
wrenge 7aeb31d369 Span access 2024-11-26 19:59:22 +03:00
wrenge 18b544845f Pass delegate 2024-11-26 19:54:21 +03:00
10 changed files with 78 additions and 34 deletions

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Buffers; using System.Buffers;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;

View File

@ -27,12 +27,12 @@ namespace DotRecast.Core.Collections
{ {
private bool _dirty; private bool _dirty;
private readonly List<T> _items; private readonly List<T> _items;
private readonly Comparer<T> _comparer; private readonly Comparison<T> _comparison;
public RcSortedQueue(Comparison<T> comp) public RcSortedQueue(Comparison<T> comp)
{ {
_items = new List<T>(); _items = new List<T>();
_comparer = Comparer<T>.Create((x, y) => comp.Invoke(x, y) * -1); _comparison = (x, y) => comp(x, y) * -1;
} }
public int Count() public int Count()
@ -55,7 +55,7 @@ namespace DotRecast.Core.Collections
{ {
if (_dirty) if (_dirty)
{ {
_items.Sort(_comparer); // reverse _items.Sort(_comparison); // reverse
_dirty = false; _dirty = false;
} }
} }

View File

@ -52,25 +52,12 @@ namespace DotRecast.Detour.Extras.Jumplink
RcVec3f halfExtents = new RcVec3f { X = cs, Y = heightRange, Z = cs }; RcVec3f halfExtents = new RcVec3f { X = cs, Y = heightRange, Z = cs };
float maxHeight = pt.Y + heightRange; float maxHeight = pt.Y + heightRange;
RcAtomicBoolean found = new RcAtomicBoolean(); var query = new DtHeightSamplePolyQuery(navMeshQuery, pt, pt.Y, maxHeight);
RcAtomicFloat minHeight = new RcAtomicFloat(pt.Y); navMeshQuery.QueryPolygons(pt, halfExtents, DtQueryNoOpFilter.Shared, ref query);
navMeshQuery.QueryPolygons(pt, halfExtents, DtQueryNoOpFilter.Shared, new DtCallbackPolyQuery((tile, poly, refs) => if (query.Found)
{ {
var status = navMeshQuery.GetPolyHeight(refs, pt, out var h); height = query.MinHeight;
if (status.Succeeded())
{
if (h > minHeight.Get() && h < maxHeight)
{
minHeight.Exchange(h);
found.Set(true);
}
}
}));
if (found.Get())
{
height = minHeight.Get();
return true; return true;
} }

View File

@ -2,7 +2,7 @@ using System;
namespace DotRecast.Detour namespace DotRecast.Detour
{ {
public class DtCallbackPolyQuery : IDtPolyQuery public struct DtCallbackPolyQuery : IDtPolyQuery
{ {
private readonly Action<DtMeshTile, DtPoly, long> _callback; private readonly Action<DtMeshTile, DtPoly, long> _callback;
@ -11,7 +11,7 @@ namespace DotRecast.Detour
_callback = callback; _callback = callback;
} }
public void Process(DtMeshTile tile, DtPoly[] poly, Span<long> refs, int count) public void Process(DtMeshTile tile, Span<DtPoly> poly, Span<long> refs, int count)
{ {
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
{ {

View File

@ -26,7 +26,7 @@ namespace DotRecast.Detour
return m_overflow; return m_overflow;
} }
public void Process(DtMeshTile tile, DtPoly[] poly, Span<long> refs, int count) public void Process(DtMeshTile tile, Span<DtPoly> poly, Span<long> refs, int count)
{ {
int numLeft = m_maxPolys - m_numCollected; int numLeft = m_maxPolys - m_numCollected;
int toCopy = count; int toCopy = count;

View File

@ -3,7 +3,7 @@ using DotRecast.Core.Numerics;
namespace DotRecast.Detour namespace DotRecast.Detour
{ {
public class DtFindNearestPolyQuery : IDtPolyQuery public struct DtFindNearestPolyQuery : IDtPolyQuery
{ {
private readonly DtNavMeshQuery _query; private readonly DtNavMeshQuery _query;
private readonly RcVec3f _center; private readonly RcVec3f _center;
@ -18,9 +18,12 @@ namespace DotRecast.Detour
_center = center; _center = center;
_nearestDistanceSqr = float.MaxValue; _nearestDistanceSqr = float.MaxValue;
_nearestPoint = center; _nearestPoint = center;
_nearestRef = default;
_overPoly = default;
} }
public void Process(DtMeshTile tile, DtPoly[] poly, Span<long> refs, int count) public void Process(DtMeshTile tile, Span<DtPoly> poly, Span<long> refs, int count)
{ {
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
{ {

View File

@ -0,0 +1,45 @@
using System;
using DotRecast.Core;
using DotRecast.Core.Numerics;
namespace DotRecast.Detour
{
public struct DtHeightSamplePolyQuery : IDtPolyQuery
{
private readonly DtNavMeshQuery _navMeshQuery;
private readonly RcVec3f _pt;
private readonly float _maxHeight;
public float MinHeight { get; private set; }
public bool Found { get; private set; }
public DtHeightSamplePolyQuery(DtNavMeshQuery navMeshQuery, RcVec3f pt, float minHeight, float maxHeight)
{
_navMeshQuery = navMeshQuery;
_pt = pt;
MinHeight = minHeight;
_maxHeight = maxHeight;
Found = default;
}
public void Process(DtMeshTile tile, Span<DtPoly> poly, Span<long> refs, int count)
{
for (int i = 0; i < count; i++)
{
ProcessSingle(refs[i]);
}
}
private void ProcessSingle(long refs)
{
var status = _navMeshQuery.GetPolyHeight(refs, _pt, out var h);
if (!status.Succeeded())
return;
if (!(h > MinHeight) || !(h < _maxHeight))
return;
MinHeight = h;
Found = true;
}
}
}

View File

@ -1365,6 +1365,11 @@ namespace DotRecast.Detour
} }
public int GetTilesAt(int x, int y, DtMeshTile[] tiles, int maxTiles) public int GetTilesAt(int x, int y, DtMeshTile[] tiles, int maxTiles)
{
return GetTilesAt(x, y, (Span<DtMeshTile>)tiles, maxTiles);
}
public int GetTilesAt(int x, int y, Span<DtMeshTile> tiles, int maxTiles)
{ {
int n = 0; int n = 0;

View File

@ -589,7 +589,7 @@ namespace DotRecast.Detour
// Get nearby polygons from proximity grid. // Get nearby polygons from proximity grid.
DtFindNearestPolyQuery query = new DtFindNearestPolyQuery(this, center); DtFindNearestPolyQuery query = new DtFindNearestPolyQuery(this, center);
DtStatus status = QueryPolygons(center, halfExtents, filter, query); DtStatus status = QueryPolygons(center, halfExtents, filter, ref query);
if (status.Failed()) if (status.Failed())
{ {
return status; return status;
@ -603,11 +603,13 @@ namespace DotRecast.Detour
} }
/// Queries polygons within a tile. /// Queries polygons within a tile.
protected void QueryPolygonsInTile(DtMeshTile tile, RcVec3f qmin, RcVec3f qmax, IDtQueryFilter filter, IDtPolyQuery query) protected void QueryPolygonsInTile<TQuery>(DtMeshTile tile, RcVec3f qmin, RcVec3f qmax, IDtQueryFilter filter, ref TQuery query)
where TQuery : IDtPolyQuery
{ {
const int batchSize = 32; const int batchSize = 32;
Span<long> polyRefs = stackalloc long[batchSize]; Span<long> polyRefs = stackalloc long[batchSize];
DtPoly[] polys = new DtPoly[batchSize]; using RcRentedArray<DtPoly> polysRent = RcRentedArray.Rent<DtPoly>(batchSize);
Span<DtPoly> polys = polysRent.AsSpan();
int n = 0; int n = 0;
if (tile.data.bvTree != null) if (tile.data.bvTree != null)
@ -758,7 +760,7 @@ namespace DotRecast.Detour
return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
DtCollectPolysQuery collector = new DtCollectPolysQuery(polys, maxPolys); DtCollectPolysQuery collector = new DtCollectPolysQuery(polys, maxPolys);
DtStatus status = QueryPolygons(center, halfExtents, filter, collector); DtStatus status = QueryPolygons(center, halfExtents, filter, ref collector);
if (status.Failed()) if (status.Failed())
return status; return status;
@ -780,7 +782,8 @@ namespace DotRecast.Detour
/// @param[in] halfExtents The search distance along each axis. [(x, y, z)] /// @param[in] halfExtents The search distance along each axis. [(x, y, z)]
/// @param[in] filter The polygon filter to apply to the query. /// @param[in] filter The polygon filter to apply to the query.
/// @param[in] query The query. Polygons found will be batched together and passed to this query. /// @param[in] query The query. Polygons found will be batched together and passed to this query.
public DtStatus QueryPolygons(RcVec3f center, RcVec3f halfExtents, IDtQueryFilter filter, IDtPolyQuery query) public DtStatus QueryPolygons<TQuery>(RcVec3f center, RcVec3f halfExtents, IDtQueryFilter filter, ref TQuery query)
where TQuery : IDtPolyQuery
{ {
if (!center.IsFinite() || !halfExtents.IsFinite() || null == filter) if (!center.IsFinite() || !halfExtents.IsFinite() || null == filter)
{ {
@ -796,7 +799,8 @@ namespace DotRecast.Detour
m_nav.CalcTileLoc(bmax, out var maxx, out var maxy); m_nav.CalcTileLoc(bmax, out var maxx, out var maxy);
const int MAX_NEIS = 32; const int MAX_NEIS = 32;
DtMeshTile[] neis = new DtMeshTile[MAX_NEIS]; using RcRentedArray<DtMeshTile> neisRent = RcRentedArray.Rent<DtMeshTile>(MAX_NEIS);
Span<DtMeshTile> neis = neisRent.AsSpan();
for (int y = miny; y <= maxy; ++y) for (int y = miny; y <= maxy; ++y)
{ {
@ -805,7 +809,7 @@ namespace DotRecast.Detour
int nneis = m_nav.GetTilesAt(x, y, neis, MAX_NEIS); int nneis = m_nav.GetTilesAt(x, y, neis, MAX_NEIS);
for (int j = 0; j < nneis; ++j) for (int j = 0; j < nneis; ++j)
{ {
QueryPolygonsInTile(neis[j], bmin, bmax, filter, query); QueryPolygonsInTile(neis[j], bmin, bmax, filter, ref query);
} }
} }
} }

View File

@ -9,6 +9,6 @@ namespace DotRecast.Detour
{ {
/// Called for each batch of unique polygons touched by the search area in dtNavMeshQuery::queryPolygons. /// Called for each batch of unique polygons touched by the search area in dtNavMeshQuery::queryPolygons.
/// This can be called multiple times for a single query. /// This can be called multiple times for a single query.
void Process(DtMeshTile tile, DtPoly[] poly, Span<long> refs, int count); void Process(DtMeshTile tile, Span<DtPoly> poly, Span<long> refs, int count);
} }
} }