Skip to content

Commit

Permalink
fix: SOH issue (#41)
Browse files Browse the repository at this point in the history
fix: SOH issue step2 (#41)

#41

fix: SOH issue step3 (#41)

- #41

fix : SOH issue (#41)

- #41 (comment)

fix: SOH issue (#41)

- #41 (comment)

fix: SOH issue(#41)

#41 (comment)

array benchmark

benchmark array test step2

test

ss

remove unused using

dd
  • Loading branch information
ikpil committed May 1, 2024
1 parent 1e0ef4f commit 26ebeff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/DotRecast.Detour.Crowd/DtCrowd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 3. This notice may not be removed or altered from any source distribution.
using System.Collections.Generic;
using System.Threading.Tasks;
using DotRecast.Core;
using DotRecast.Core.Buffers;
using DotRecast.Core.Collections;
using DotRecast.Core.Numerics;

Expand Down
1 change: 1 addition & 0 deletions src/DotRecast.Detour.Crowd/DtObstacleAvoidanceQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 3. This notice may not be removed or altered from any source distribution.
using System;
using System.Runtime.CompilerServices;
using DotRecast.Core;
using DotRecast.Core.Buffers;
using DotRecast.Core.Numerics;


Expand Down
7 changes: 4 additions & 3 deletions src/DotRecast.Detour/DtNavMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 3. This notice may not be removed or altered from any source distribution.
using System;
using System.Collections.Generic;
using DotRecast.Core;
using DotRecast.Core.Buffers;
using DotRecast.Core.Numerics;

namespace DotRecast.Detour
Expand Down Expand Up @@ -1246,14 +1247,14 @@ public bool GetPolyHeight(DtMeshTile tile, DtPoly poly, RcVec3f pos, out float h

int ip = poly.index;

float[] verts = new float[m_maxVertPerPoly * 3];
using var verts = RcRentedArray.Rent<float>(m_maxVertPerPoly * 3);
int nv = poly.vertCount;
for (int i = 0; i < nv; ++i)
{
RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts, i * 3, 3);
RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts.AsArray(), i * 3, 3);
}

if (!DtUtils.PointInPolygon(pos, verts, nv))
if (!DtUtils.PointInPolygon(pos, verts.AsArray(), nv))
{
return false;
}
Expand Down
47 changes: 25 additions & 22 deletions src/DotRecast.Detour/DtNavMeshQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 3. This notice may not be removed or altered from any source distribution.
using System;
using System.Collections.Generic;
using DotRecast.Core;
using DotRecast.Core.Buffers;
using DotRecast.Core.Collections;
using DotRecast.Core.Numerics;

Expand All @@ -41,9 +42,9 @@ public class DtNavMeshQuery
public DtNavMeshQuery(DtNavMesh nav)
{
m_nav = nav;
m_tinyNodePool = new DtNodePool();
m_nodePool = new DtNodePool();
m_openList = new DtNodeQueue();
m_tinyNodePool = new DtNodePool();
}

/// Returns random location on navmesh.
Expand Down Expand Up @@ -386,8 +387,8 @@ public DtStatus FindRandomPointAroundCircle(long startRef, RcVec3f centerPos, fl
float s = frand.Next();
float t = frand.Next();

float[] areas = new float[randomPolyVerts.Length / 3];
RcVec3f pt = DtUtils.RandomPointInConvexPoly(randomPolyVerts, randomPolyVerts.Length / 3, areas, s, t);
using var areas = RcRentedArray.Rent<float>(randomPolyVerts.Length / 3);
RcVec3f pt = DtUtils.RandomPointInConvexPoly(randomPolyVerts, randomPolyVerts.Length / 3, areas.AsArray(), s, t);
ClosestPointOnPoly(randomPolyRef, pt, out var closest, out var _);

randomRef = randomPolyRef;
Expand Down Expand Up @@ -457,16 +458,16 @@ public DtStatus ClosestPointOnPolyBoundary(long refs, RcVec3f pos, out RcVec3f c
}

// Collect vertices.
float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3];
float[] edged = new float[m_nav.GetMaxVertsPerPoly()];
float[] edget = new float[m_nav.GetMaxVertsPerPoly()];
using var verts = RcRentedArray.Rent<float>(m_nav.GetMaxVertsPerPoly() * 3);
using var edged = RcRentedArray.Rent<float>(m_nav.GetMaxVertsPerPoly());
using var edget = RcRentedArray.Rent<float>(m_nav.GetMaxVertsPerPoly());
int nv = poly.vertCount;
for (int i = 0; i < nv; ++i)
{
RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts, i * 3, 3);
RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts.AsArray(), i * 3, 3);
}

if (DtUtils.DistancePtPolyEdgesSqr(pos, verts, nv, edged, edget))
if (DtUtils.DistancePtPolyEdgesSqr(pos, verts.AsArray(), nv, edged.AsArray(), edget.AsArray()))
{
closest = pos;
}
Expand All @@ -486,7 +487,7 @@ public DtStatus ClosestPointOnPolyBoundary(long refs, RcVec3f pos, out RcVec3f c

int va = imin * 3;
int vb = ((imin + 1) % nv) * 3;
closest = RcVecUtils.Lerp(verts, va, vb, edget[imin]);
closest = RcVecUtils.Lerp(verts.AsArray(), va, vb, edget[imin]);
}

return DtStatus.DT_SUCCESS;
Expand Down Expand Up @@ -1793,7 +1794,9 @@ public DtStatus MoveAlongSurface(long startRef, RcVec3f startPos, RcVec3f endPos
resultPos = RcVec3f.Zero;

if (null != visited)
{
visited.Clear();
}

// Validate input
if (!m_nav.IsValidPolyRef(startRef) || !startPos.IsFinite()
Expand Down Expand Up @@ -1822,7 +1825,7 @@ public DtStatus MoveAlongSurface(long startRef, RcVec3f startPos, RcVec3f endPos
var searchPos = RcVec3f.Lerp(startPos, endPos, 0.5f);
float searchRadSqr = RcMath.Sqr(RcVec3f.Distance(startPos, endPos) / 2.0f + 0.001f);

float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3];
using var verts = RcRentedArray.Rent<float>(m_nav.GetMaxVertsPerPoly() * 3);

const int MAX_NEIS = 8;
Span<long> neis = stackalloc long[MAX_NEIS];
Expand All @@ -1842,11 +1845,11 @@ public DtStatus MoveAlongSurface(long startRef, RcVec3f startPos, RcVec3f endPos
int nverts = curPoly.vertCount;
for (int i = 0; i < nverts; ++i)
{
RcArrays.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts, i * 3, 3);
RcArrays.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts.AsArray(), i * 3, 3);
}

// If target is inside the poly, stop search.
if (DtUtils.PointInPolygon(endPos, verts, nverts))
if (DtUtils.PointInPolygon(endPos, verts.AsArray(), nverts))
{
bestNode = curNode;
bestPos = endPos;
Expand Down Expand Up @@ -1897,11 +1900,11 @@ public DtStatus MoveAlongSurface(long startRef, RcVec3f startPos, RcVec3f endPos
// Wall edge, calc distance.
int vj = j * 3;
int vi = i * 3;
var distSqr = DtUtils.DistancePtSegSqr2D(endPos, verts, vj, vi, out var tseg);
var distSqr = DtUtils.DistancePtSegSqr2D(endPos, verts.AsArray(), vj, vi, out var tseg);
if (distSqr < bestDist)
{
// Update nearest distance.
bestPos = RcVecUtils.Lerp(verts, vj, vi, tseg);
bestPos = RcVecUtils.Lerp(verts.AsArray(), vj, vi, tseg);
bestDist = distSqr;
bestNode = curNode;
}
Expand All @@ -1921,7 +1924,7 @@ public DtStatus MoveAlongSurface(long startRef, RcVec3f startPos, RcVec3f endPos
// TODO: Maybe should use GetPortalPoints(), but this one is way faster.
int vj = j * 3;
int vi = i * 3;
var distSqr = DtUtils.DistancePtSegSqr2D(searchPos, verts, vj, vi, out var _);
var distSqr = DtUtils.DistancePtSegSqr2D(searchPos, verts.AsArray(), vj, vi, out var _);
if (distSqr > searchRadSqr)
{
continue;
Expand Down Expand Up @@ -2245,7 +2248,7 @@ public DtStatus Raycast(long startRef, RcVec3f startPos, RcVec3f endPos,
hit.path.Clear();
hit.pathCost = 0;

RcVec3f[] verts = new RcVec3f[m_nav.GetMaxVertsPerPoly() + 1];
using var verts = RcRentedArray.Rent<RcVec3f>(m_nav.GetMaxVertsPerPoly() + 1);

RcVec3f curPos = RcVec3f.Zero;
RcVec3f lastPos = RcVec3f.Zero;
Expand Down Expand Up @@ -2279,7 +2282,7 @@ public DtStatus Raycast(long startRef, RcVec3f startPos, RcVec3f endPos,
nv++;
}

bool intersects = DtUtils.IntersectSegmentPoly2D(startPos, endPos, verts, nv, out var tmin, out var tmax, out var segMin, out var segMax);
bool intersects = DtUtils.IntersectSegmentPoly2D(startPos, endPos, verts.AsArray(), nv, out var tmin, out var tmax, out var segMin, out var segMax);
if (!intersects)
{
// Could not hit the polygon, keep the old t and report hit.
Expand Down Expand Up @@ -2874,8 +2877,8 @@ public DtStatus FindLocalNeighbourhood(long startRef, RcVec3f centerPos, float r

float radiusSqr = RcMath.Sqr(radius);

float[] pa = new float[m_nav.GetMaxVertsPerPoly() * 3];
float[] pb = new float[m_nav.GetMaxVertsPerPoly() * 3];
using var pa = RcRentedArray.Rent<float>(m_nav.GetMaxVertsPerPoly() * 3);
using var pb = RcRentedArray.Rent<float>(m_nav.GetMaxVertsPerPoly() * 3);

while (0 < stack.Count)
{
Expand Down Expand Up @@ -2946,7 +2949,7 @@ public DtStatus FindLocalNeighbourhood(long startRef, RcVec3f centerPos, float r
int npa = neighbourPoly.vertCount;
for (int k = 0; k < npa; ++k)
{
RcArrays.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa, k * 3, 3);
RcArrays.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa.AsArray(), k * 3, 3);
}

bool overlap = false;
Expand Down Expand Up @@ -2977,10 +2980,10 @@ public DtStatus FindLocalNeighbourhood(long startRef, RcVec3f centerPos, float r
int npb = pastPoly.vertCount;
for (int k = 0; k < npb; ++k)
{
RcArrays.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb, k * 3, 3);
RcArrays.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb.AsArray(), k * 3, 3);
}

if (DtUtils.OverlapPolyPoly2D(pa, npa, pb, npb))
if (DtUtils.OverlapPolyPoly2D(pa.AsArray(), npa, pb.AsArray(), npb))
{
overlap = true;
break;
Expand Down

0 comments on commit 26ebeff

Please sign in to comment.