From 0da362d2a94c212554d13a69389aadc422a9c1be Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 16 Apr 2023 12:19:41 +0900 Subject: [PATCH] refactoring new float[2] -> struct Vector2f --- src/DotRecast.Core/Vector2f.cs | 2 + src/DotRecast.Detour/NavMesh.cs | 68 +++++++++++++++++---------------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/DotRecast.Core/Vector2f.cs b/src/DotRecast.Core/Vector2f.cs index 6328d0c..6a13434 100644 --- a/src/DotRecast.Core/Vector2f.cs +++ b/src/DotRecast.Core/Vector2f.cs @@ -7,6 +7,8 @@ namespace DotRecast.Core public float x; public float y; + public static readonly Vector2f Zero = new Vector2f { x = 0, y = 0 }; + public float Get(int idx) { if (0 == idx) diff --git a/src/DotRecast.Detour/NavMesh.cs b/src/DotRecast.Detour/NavMesh.cs index 92b64ea..c47f879 100644 --- a/src/DotRecast.Detour/NavMesh.cs +++ b/src/DotRecast.Detour/NavMesh.cs @@ -756,8 +756,7 @@ namespace DotRecast.Detour // Create new links int va = poly.verts[j] * 3; int vb = poly.verts[(j + 1) % nv] * 3; - IList> connectedPolys = findConnectingPolys(tile.data.verts, va, vb, target, - oppositeTile(dir)); + IList> connectedPolys = findConnectingPolys(tile.data.verts, va, vb, target, oppositeTile(dir)); foreach (Tuple connectedPoly in connectedPolys) { int idx = allocLink(tile); @@ -904,14 +903,14 @@ namespace DotRecast.Detour } List> result = new List>(); - float[] amin = new float[2]; - float[] amax = new float[2]; - calcSlabEndPoints(verts, va, vb, amin, amax, side); + Vector2f amin = Vector2f.Zero; + Vector2f amax = Vector2f.Zero; + calcSlabEndPoints(verts, va, vb, ref amin, ref amax, side); float apos = getSlabCoord(verts, va, side); // Remove links pointing to 'side' and compact the links array. - float[] bmin = new float[2]; - float[] bmax = new float[2]; + Vector2f bmin = Vector2f.Zero; + Vector2f bmax = Vector2f.Zero; int m = DT_EXT_LINK | side; long @base = getPolyRefBase(tile); @@ -937,7 +936,7 @@ namespace DotRecast.Detour } // Check if the segments touch. - calcSlabEndPoints(tile.data.verts, vc, vd, bmin, bmax, side); + calcSlabEndPoints(tile.data.verts, vc, vd, ref bmin, ref bmax, side); if (!overlapSlabs(amin, amax, bmin, bmax, 0.01f, tile.data.header.walkableClimb)) { @@ -945,7 +944,10 @@ namespace DotRecast.Detour } // Add return value. - result.Add(Tuple.Create(@base | i, Math.Max(amin[0], bmin[0]), Math.Min(amax[0], bmax[0]))); + long refs = @base | i; + float tmin = Math.Max(amin.x, bmin.x); + float tmax = Math.Min(amax.x, bmax.x); + result.Add(Tuple.Create(refs, tmin, tmax)); break; } } @@ -967,61 +969,61 @@ namespace DotRecast.Detour return 0; } - static void calcSlabEndPoints(float[] verts, int va, int vb, float[] bmin, float[] bmax, int side) + static void calcSlabEndPoints(float[] verts, int va, int vb, ref Vector2f bmin, ref Vector2f bmax, int side) { if (side == 0 || side == 4) { if (verts[va + 2] < verts[vb + 2]) { - bmin[0] = verts[va + 2]; - bmin[1] = verts[va + 1]; - bmax[0] = verts[vb + 2]; - bmax[1] = verts[vb + 1]; + bmin.x = verts[va + 2]; + bmin.y = verts[va + 1]; + bmax.x = verts[vb + 2]; + bmax.y = verts[vb + 1]; } else { - bmin[0] = verts[vb + 2]; - bmin[1] = verts[vb + 1]; - bmax[0] = verts[va + 2]; - bmax[1] = verts[va + 1]; + bmin.x = verts[vb + 2]; + bmin.y = verts[vb + 1]; + bmax.x = verts[va + 2]; + bmax.y = verts[va + 1]; } } else if (side == 2 || side == 6) { if (verts[va + 0] < verts[vb + 0]) { - bmin[0] = verts[va + 0]; - bmin[1] = verts[va + 1]; - bmax[0] = verts[vb + 0]; - bmax[1] = verts[vb + 1]; + bmin.x = verts[va + 0]; + bmin.y = verts[va + 1]; + bmax.x = verts[vb + 0]; + bmax.y = verts[vb + 1]; } else { - bmin[0] = verts[vb + 0]; - bmin[1] = verts[vb + 1]; - bmax[0] = verts[va + 0]; - bmax[1] = verts[va + 1]; + bmin.x = verts[vb + 0]; + bmin.y = verts[vb + 1]; + bmax.x = verts[va + 0]; + bmax.y = verts[va + 1]; } } } - bool overlapSlabs(float[] amin, float[] amax, float[] bmin, float[] bmax, float px, float py) + bool overlapSlabs(Vector2f amin, Vector2f amax, Vector2f bmin, Vector2f bmax, float px, float py) { // Check for horizontal overlap. // The segment is shrunken a little so that slabs which touch // at end points are not connected. - float minx = Math.Max(amin[0] + px, bmin[0] + px); - float maxx = Math.Min(amax[0] - px, bmax[0] - px); + float minx = Math.Max(amin.x + px, bmin.x + px); + float maxx = Math.Min(amax.x - px, bmax.x - px); if (minx > maxx) { return false; } // Check vertical overlap. - float ad = (amax[1] - amin[1]) / (amax[0] - amin[0]); - float ak = amin[1] - ad * amin[0]; - float bd = (bmax[1] - bmin[1]) / (bmax[0] - bmin[0]); - float bk = bmin[1] - bd * bmin[0]; + float ad = (amax.y - amin.y) / (amax.x - amin.x); + float ak = amin.y - ad * amin.x; + float bd = (bmax.y - bmin.y) / (bmax.x - bmin.x); + float bk = bmin.y - bd * bmin.x; float aminy = ad * minx + ak; float amaxy = ad * maxx + ak; float bminy = bd * minx + bk;