Compare commits

..

No commits in common. "1315de063fd410bb015eba5621c22cdb82399e05" and "088edcd655fd4b80a718f0ec765ce361c3c03174" have entirely different histories.

10 changed files with 34 additions and 78 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 Comparison<T> _comparison; private readonly Comparer<T> _comparer;
public RcSortedQueue(Comparison<T> comp) public RcSortedQueue(Comparison<T> comp)
{ {
_items = new List<T>(); _items = new List<T>();
_comparison = (x, y) => comp(x, y) * -1; _comparer = Comparer<T>.Create((x, y) => comp.Invoke(x, y) * -1);
} }
public int Count() public int Count()
@ -55,7 +55,7 @@ namespace DotRecast.Core.Collections
{ {
if (_dirty) if (_dirty)
{ {
_items.Sort(_comparison); // reverse _items.Sort(_comparer); // reverse
_dirty = false; _dirty = false;
} }
} }

View File

@ -52,12 +52,25 @@ 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;
var query = new DtHeightSamplePolyQuery(navMeshQuery, pt, pt.Y, maxHeight); RcAtomicBoolean found = new RcAtomicBoolean();
navMeshQuery.QueryPolygons(pt, halfExtents, DtQueryNoOpFilter.Shared, ref query); RcAtomicFloat minHeight = new RcAtomicFloat(pt.Y);
if (query.Found) navMeshQuery.QueryPolygons(pt, halfExtents, DtQueryNoOpFilter.Shared, new DtCallbackPolyQuery((tile, poly, refs) =>
{ {
height = query.MinHeight; var status = navMeshQuery.GetPolyHeight(refs, pt, out var h);
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 struct DtCallbackPolyQuery : IDtPolyQuery public class 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, Span<DtPoly> poly, Span<long> refs, int count) public void Process(DtMeshTile tile, 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, Span<DtPoly> poly, Span<long> refs, int count) public void Process(DtMeshTile tile, 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 struct DtFindNearestPolyQuery : IDtPolyQuery public class DtFindNearestPolyQuery : IDtPolyQuery
{ {
private readonly DtNavMeshQuery _query; private readonly DtNavMeshQuery _query;
private readonly RcVec3f _center; private readonly RcVec3f _center;
@ -18,12 +18,9 @@ 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, Span<DtPoly> poly, Span<long> refs, int count) public void Process(DtMeshTile tile, DtPoly[] poly, Span<long> refs, int count)
{ {
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
{ {

View File

@ -1,45 +0,0 @@
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,11 +1365,6 @@ 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, ref query); DtStatus status = QueryPolygons(center, halfExtents, filter, query);
if (status.Failed()) if (status.Failed())
{ {
return status; return status;
@ -603,13 +603,11 @@ namespace DotRecast.Detour
} }
/// Queries polygons within a tile. /// Queries polygons within a tile.
protected void QueryPolygonsInTile<TQuery>(DtMeshTile tile, RcVec3f qmin, RcVec3f qmax, IDtQueryFilter filter, ref TQuery query) protected void QueryPolygonsInTile(DtMeshTile tile, RcVec3f qmin, RcVec3f qmax, IDtQueryFilter filter, IDtPolyQuery query)
where TQuery : IDtPolyQuery
{ {
const int batchSize = 32; const int batchSize = 32;
Span<long> polyRefs = stackalloc long[batchSize]; Span<long> polyRefs = stackalloc long[batchSize];
using RcRentedArray<DtPoly> polysRent = RcRentedArray.Rent<DtPoly>(batchSize); DtPoly[] polys = new DtPoly[batchSize];
Span<DtPoly> polys = polysRent.AsSpan();
int n = 0; int n = 0;
if (tile.data.bvTree != null) if (tile.data.bvTree != null)
@ -760,7 +758,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, ref collector); DtStatus status = QueryPolygons(center, halfExtents, filter, collector);
if (status.Failed()) if (status.Failed())
return status; return status;
@ -782,8 +780,7 @@ 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<TQuery>(RcVec3f center, RcVec3f halfExtents, IDtQueryFilter filter, ref TQuery query) public DtStatus QueryPolygons(RcVec3f center, RcVec3f halfExtents, IDtQueryFilter filter, IDtPolyQuery query)
where TQuery : IDtPolyQuery
{ {
if (!center.IsFinite() || !halfExtents.IsFinite() || null == filter) if (!center.IsFinite() || !halfExtents.IsFinite() || null == filter)
{ {
@ -799,8 +796,7 @@ 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;
using RcRentedArray<DtMeshTile> neisRent = RcRentedArray.Rent<DtMeshTile>(MAX_NEIS); DtMeshTile[] neis = new DtMeshTile[MAX_NEIS];
Span<DtMeshTile> neis = neisRent.AsSpan();
for (int y = miny; y <= maxy; ++y) for (int y = miny; y <= maxy; ++y)
{ {
@ -809,7 +805,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, ref query); QueryPolygonsInTile(neis[j], bmin, bmax, filter, 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, Span<DtPoly> poly, Span<long> refs, int count); void Process(DtMeshTile tile, DtPoly[] poly, Span<long> refs, int count);
} }
} }