From 9e9a3f0dc246e2fcbbfdaf41105c9370a9d2e7da Mon Sep 17 00:00:00 2001 From: ikpil Date: Fri, 3 May 2024 00:19:12 +0900 Subject: [PATCH] SOH https://github.com/ikpil/DotRecast/issues/41 --- src/DotRecast.Core/Numerics/RcVecUtils.cs | 6 +++--- src/DotRecast.Detour/DtNavMeshQuery.cs | 20 ++++++++++---------- src/DotRecast.Detour/DtUtils.cs | 8 ++++---- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/DotRecast.Core/Numerics/RcVecUtils.cs b/src/DotRecast.Core/Numerics/RcVecUtils.cs index bbe9efd..68816b1 100644 --- a/src/DotRecast.Core/Numerics/RcVecUtils.cs +++ b/src/DotRecast.Core/Numerics/RcVecUtils.cs @@ -14,7 +14,7 @@ namespace DotRecast.Core.Numerics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static RcVec3f Create(float[] values, int n) + public static RcVec3f Create(Span values, int n) { return new RcVec3f(values[n + 0], values[n + 1], values[n + 2]); } @@ -63,7 +63,7 @@ namespace DotRecast.Core.Numerics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static float Dot2D(this RcVec3f @this, float[] v, int vi) + public static float Dot2D(this RcVec3f @this, Span v, int vi) { return @this.X * v[vi] + @this.Z * v[vi + 2]; @@ -262,7 +262,7 @@ namespace DotRecast.Core.Numerics /// @param[in] v2 The destination vector. /// @param[in] t The interpolation factor. [Limits: 0 <= value <= 1.0] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static RcVec3f Lerp(float[] verts, int v1, int v2, float t) + public static RcVec3f Lerp(Span verts, int v1, int v2, float t) { return new RcVec3f( verts[v1 + 0] + (verts[v2 + 0] - verts[v1 + 0]) * t, diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index 678c05a..20627b1 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -457,13 +457,13 @@ namespace DotRecast.Detour } // Collect vertices. - float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3]; - float[] edged = new float[m_nav.GetMaxVertsPerPoly()]; - float[] edget = new float[m_nav.GetMaxVertsPerPoly()]; + Span verts = stackalloc float[m_nav.GetMaxVertsPerPoly() * 3]; + Span edged = stackalloc float[m_nav.GetMaxVertsPerPoly()]; + Span edget = stackalloc 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); + RcSpans.Copy(tile.data.verts, poly.verts[i] * 3, verts, i * 3, 3); } if (DtUtils.DistancePtPolyEdgesSqr(pos, verts, nv, edged, edget)) @@ -1822,7 +1822,7 @@ namespace DotRecast.Detour 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]; + Span verts = stackalloc float[m_nav.GetMaxVertsPerPoly() * 3]; const int MAX_NEIS = 8; Span neis = stackalloc long[MAX_NEIS]; @@ -1842,7 +1842,7 @@ namespace DotRecast.Detour int nverts = curPoly.vertCount; for (int i = 0; i < nverts; ++i) { - RcArrays.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts, i * 3, 3); + RcSpans.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts, i * 3, 3); } // If target is inside the poly, stop search. @@ -2874,8 +2874,8 @@ namespace DotRecast.Detour float radiusSqr = RcMath.Sqr(radius); - float[] pa = new float[m_nav.GetMaxVertsPerPoly() * 3]; - float[] pb = new float[m_nav.GetMaxVertsPerPoly() * 3]; + Span pa = stackalloc float[m_nav.GetMaxVertsPerPoly() * 3]; + Span pb = stackalloc float[m_nav.GetMaxVertsPerPoly() * 3]; while (0 < stack.Count) { @@ -2946,7 +2946,7 @@ namespace DotRecast.Detour int npa = neighbourPoly.vertCount; for (int k = 0; k < npa; ++k) { - RcArrays.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa, k * 3, 3); + RcSpans.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa, k * 3, 3); } bool overlap = false; @@ -2977,7 +2977,7 @@ namespace DotRecast.Detour int npb = pastPoly.vertCount; for (int k = 0; k < npb; ++k) { - RcArrays.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb, k * 3, 3); + RcSpans.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb, k * 3, 3); } if (DtUtils.OverlapPolyPoly2D(pa, npa, pb, npb)) diff --git a/src/DotRecast.Detour/DtUtils.cs b/src/DotRecast.Detour/DtUtils.cs index b2cdc08..3cda106 100644 --- a/src/DotRecast.Detour/DtUtils.cs +++ b/src/DotRecast.Detour/DtUtils.cs @@ -97,7 +97,7 @@ namespace DotRecast.Detour /// @par /// /// All vertices are projected onto the xz-plane, so the y-values are ignored. - public static bool OverlapPolyPoly2D(float[] polya, int npolya, float[] polyb, int npolyb) + public static bool OverlapPolyPoly2D(Span polya, int npolya, Span polyb, int npolyb) { const float eps = 1e-4f; for (int i = 0, j = npolya - 1; i < npolya; j = i++) @@ -246,7 +246,7 @@ namespace DotRecast.Detour return false; } - public static RcVec2f ProjectPoly(RcVec3f axis, float[] poly, int npoly) + public static RcVec2f ProjectPoly(RcVec3f axis, Span poly, int npoly) { float rmin, rmax; rmin = rmax = axis.Dot2D(poly, 0); @@ -286,7 +286,7 @@ namespace DotRecast.Detour return c; } - public static bool DistancePtPolyEdgesSqr(RcVec3f pt, float[] verts, int nverts, float[] ed, float[] et) + public static bool DistancePtPolyEdgesSqr(RcVec3f pt, Span verts, int nverts, Span ed, Span et) { // TODO: Replace pnpoly with triArea2D tests? int i, j; @@ -307,7 +307,7 @@ namespace DotRecast.Detour return c; } - public static float DistancePtSegSqr2D(RcVec3f pt, float[] verts, int p, int q, out float t) + public static float DistancePtSegSqr2D(RcVec3f pt, Span verts, int p, int q, out float t) { var vp = RcVecUtils.Create(verts, p); var vq = RcVecUtils.Create(verts, q);