From a50ba0b1b1b91b601acfa20f30030b9057b6807e Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 11 Nov 2023 12:33:32 +0900 Subject: [PATCH] refactor: changed float[6] to RcVec3f[2] in DtOffMeshConnection class --- .../Unity/Astar/OffMeshLinkCreator.cs | 7 ++--- src/DotRecast.Detour/DtNavMesh.cs | 11 +++----- src/DotRecast.Detour/DtNavMeshBuilder.cs | 6 +++- src/DotRecast.Detour/DtOffMeshConnection.cs | 4 ++- src/DotRecast.Detour/Io/DtMeshDataReader.cs | 6 ++-- src/DotRecast.Detour/Io/DtMeshDataWriter.cs | 6 ++-- .../Draw/RecastDebugDraw.cs | 28 +++++++++++-------- .../Io/MeshDataReaderWriterTest.cs | 2 +- .../NavMeshBuilderTest.cs | 7 +++-- 9 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/DotRecast.Detour.Extras/Unity/Astar/OffMeshLinkCreator.cs b/src/DotRecast.Detour.Extras/Unity/Astar/OffMeshLinkCreator.cs index 8623520..581aaf6 100644 --- a/src/DotRecast.Detour.Extras/Unity/Astar/OffMeshLinkCreator.cs +++ b/src/DotRecast.Detour.Extras/Unity/Astar/OffMeshLinkCreator.cs @@ -48,15 +48,14 @@ namespace DotRecast.Detour.Extras.Unity.Astar startTile.header.vertCount += 2; DtOffMeshConnection connection = new DtOffMeshConnection(); connection.poly = poly; - connection.pos = new float[] + connection.pos = new RcVec3f[] { - l.clamped1.X, l.clamped1.Y, l.clamped1.Z, - l.clamped2.X, l.clamped2.Y, l.clamped2.Z + l.clamped1, l.clamped2 }; connection.rad = 0.1f; connection.side = startTile == endTile ? 0xFF - : DtNavMeshBuilder.ClassifyOffMeshPoint(RcVecUtils.Create(connection.pos, 3), startTile.header.bmin, startTile.header.bmax); + : DtNavMeshBuilder.ClassifyOffMeshPoint(connection.pos[1], startTile.header.bmin, startTile.header.bmax); connection.userId = (int)l.linkID; if (startTile.offMeshCons == null) { diff --git a/src/DotRecast.Detour/DtNavMesh.cs b/src/DotRecast.Detour/DtNavMesh.cs index e601e42..3837ba7 100644 --- a/src/DotRecast.Detour/DtNavMesh.cs +++ b/src/DotRecast.Detour/DtNavMesh.cs @@ -855,10 +855,7 @@ namespace DotRecast.Detour }; // Find polygon to connect to. - RcVec3f p = new RcVec3f(); - p.X = targetCon.pos[3]; - p.Y = targetCon.pos[4]; - p.Z = targetCon.pos[5]; + RcVec3f p = targetCon.pos[1]; var refs = FindNearestPolyInTile(tile, p, ext, out var nearestPt); if (refs == 0) { @@ -1089,16 +1086,16 @@ namespace DotRecast.Detour }; // Find polygon to connect to. - var refs = FindNearestPolyInTile(tile, new RcVec3f(con.pos[0], con.pos[1], con.pos[2]), ext, out var nearestPt); + var refs = FindNearestPolyInTile(tile, con.pos[0], ext, out var nearestPt); if (refs == 0) { continue; } - float[] p = con.pos; // First vertex + RcVec3f[] p = con.pos; // First vertex // findNearestPoly may return too optimistic results, further check // to make sure. - if (RcMath.Sqr(nearestPt.X - p[0]) + RcMath.Sqr(nearestPt.Z - p[2]) > RcMath.Sqr(con.rad)) + if (RcMath.Sqr(nearestPt.X - p[0].X) + RcMath.Sqr(nearestPt.Z - p[0].Z) > RcMath.Sqr(con.rad)) { continue; } diff --git a/src/DotRecast.Detour/DtNavMeshBuilder.cs b/src/DotRecast.Detour/DtNavMeshBuilder.cs index 8a2df27..6cfb173 100644 --- a/src/DotRecast.Detour/DtNavMeshBuilder.cs +++ b/src/DotRecast.Detour/DtNavMeshBuilder.cs @@ -615,7 +615,11 @@ namespace DotRecast.Detour con.poly = (offMeshPolyBase + n); // Copy connection end-points. int endPts = i * 2 * 3; - Array.Copy(option.offMeshConVerts, endPts, con.pos, 0, 6); + for (int j = 0; j < 2; ++j) + { + con.pos[j] = RcVecUtils.Create(option.offMeshConVerts, endPts + (j * 3)); + } + con.rad = option.offMeshConRad[i]; con.flags = option.offMeshConDir[i] != 0 ? DtNavMesh.DT_OFFMESH_CON_BIDIR : 0; con.side = offMeshConClass[i * 2 + 1]; diff --git a/src/DotRecast.Detour/DtOffMeshConnection.cs b/src/DotRecast.Detour/DtOffMeshConnection.cs index 0ebecd0..244f005 100644 --- a/src/DotRecast.Detour/DtOffMeshConnection.cs +++ b/src/DotRecast.Detour/DtOffMeshConnection.cs @@ -18,6 +18,8 @@ freely, subject to the following restrictions: 3. This notice may not be removed or altered from any source distribution. */ +using DotRecast.Core.Numerics; + namespace DotRecast.Detour { /// Defines an navigation mesh off-mesh connection within a dtMeshTile object. @@ -25,7 +27,7 @@ namespace DotRecast.Detour public class DtOffMeshConnection { /// The endpoints of the connection. [(ax, ay, az, bx, by, bz)] - public float[] pos = new float[6]; + public RcVec3f[] pos = new RcVec3f[2]; /// The radius of the endpoints. [Limit: >= 0] public float rad; diff --git a/src/DotRecast.Detour/Io/DtMeshDataReader.cs b/src/DotRecast.Detour/Io/DtMeshDataReader.cs index 7823681..4d83ef2 100644 --- a/src/DotRecast.Detour/Io/DtMeshDataReader.cs +++ b/src/DotRecast.Detour/Io/DtMeshDataReader.cs @@ -237,9 +237,11 @@ namespace DotRecast.Detour.Io for (int i = 0; i < cons.Length; i++) { cons[i] = new DtOffMeshConnection(); - for (int j = 0; j < 6; j++) + for (int j = 0; j < 2; j++) { - cons[i].pos[j] = buf.GetFloat(); + cons[i].pos[j].X = buf.GetFloat(); + cons[i].pos[j].Y = buf.GetFloat(); + cons[i].pos[j].Z = buf.GetFloat(); } cons[i].rad = buf.GetFloat(); diff --git a/src/DotRecast.Detour/Io/DtMeshDataWriter.cs b/src/DotRecast.Detour/Io/DtMeshDataWriter.cs index 676ff44..fd9cf65 100644 --- a/src/DotRecast.Detour/Io/DtMeshDataWriter.cs +++ b/src/DotRecast.Detour/Io/DtMeshDataWriter.cs @@ -159,9 +159,11 @@ namespace DotRecast.Detour.Io { for (int i = 0; i < data.header.offMeshConCount; i++) { - for (int j = 0; j < 6; j++) + for (int j = 0; j < 2; j++) { - Write(stream, data.offMeshCons[i].pos[j], order); + Write(stream, data.offMeshCons[i].pos[j].X, order); + Write(stream, data.offMeshCons[i].pos[j].Y, order); + Write(stream, data.offMeshCons[i].pos[j].Z, order); } Write(stream, data.offMeshCons[i].rad, order); diff --git a/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs b/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs index 8c9fca9..db61456 100644 --- a/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs +++ b/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs @@ -213,25 +213,27 @@ public class RecastDebugDraw : DebugDraw // End points and their on-mesh locations. Vertex(va.X, va.Y, va.Z, col); - Vertex(con.pos[0], con.pos[1], con.pos[2], col); + Vertex(con.pos[0], col); col2 = startSet ? col : DuRGBA(220, 32, 16, 196); - AppendCircle(con.pos[0], con.pos[1] + 0.1f, con.pos[2], con.rad, col2); + AppendCircle(con.pos[0].X, con.pos[0].Y + 0.1f, con.pos[0].Z, con.rad, col2); Vertex(vb.X, vb.Y, vb.Z, col); - Vertex(con.pos[3], con.pos[4], con.pos[5], col); + Vertex(con.pos[1], col); col2 = endSet ? col : DuRGBA(220, 32, 16, 196); - AppendCircle(con.pos[3], con.pos[4] + 0.1f, con.pos[5], con.rad, col2); + AppendCircle(con.pos[1].X, con.pos[1].Y + 0.1f, con.pos[1].Z, con.rad, col2); // End point vertices. - Vertex(con.pos[0], con.pos[1], con.pos[2], DuRGBA(0, 48, 64, 196)); - Vertex(con.pos[0], con.pos[1] + 0.2f, con.pos[2], DuRGBA(0, 48, 64, 196)); + Vertex(con.pos[0], DuRGBA(0, 48, 64, 196)); + Vertex(con.pos[0].X, con.pos[0].Y + 0.2f, con.pos[0].Z, DuRGBA(0, 48, 64, 196)); - Vertex(con.pos[3], con.pos[4], con.pos[5], DuRGBA(0, 48, 64, 196)); - Vertex(con.pos[3], con.pos[4] + 0.2f, con.pos[5], DuRGBA(0, 48, 64, 196)); + Vertex(con.pos[1], DuRGBA(0, 48, 64, 196)); + Vertex(con.pos[1].X, con.pos[1].Y + 0.2f, con.pos[1].Z, DuRGBA(0, 48, 64, 196)); // Connection arc. - AppendArc(con.pos[0], con.pos[1], con.pos[2], con.pos[3], con.pos[4], con.pos[5], 0.25f, - (con.flags & 1) != 0 ? 0.6f : 0, 0.6f, col); + AppendArc( + con.pos[0].X, con.pos[0].Y, con.pos[0].Z, + con.pos[1].X, con.pos[1].Y, con.pos[1].Z, + 0.25f, (con.flags & 1) != 0 ? 0.6f : 0, 0.6f, col); } End(); @@ -1293,8 +1295,10 @@ public class RecastDebugDraw : DebugDraw Begin(DebugDrawPrimitives.LINES, 2.0f); // Connection arc. - AppendArc(con.pos[0], con.pos[1], con.pos[2], con.pos[3], con.pos[4], con.pos[5], 0.25f, - (con.flags & 1) != 0 ? 0.6f : 0.0f, 0.6f, c); + AppendArc( + con.pos[0].X, con.pos[0].Y, con.pos[0].Z, + con.pos[1].X, con.pos[1].Y, con.pos[1].Z, + 0.25f, (con.flags & 1) != 0 ? 0.6f : 0.0f, 0.6f, c); End(); } diff --git a/test/DotRecast.Detour.Test/Io/MeshDataReaderWriterTest.cs b/test/DotRecast.Detour.Test/Io/MeshDataReaderWriterTest.cs index 21a3c44..5a1edfa 100644 --- a/test/DotRecast.Detour.Test/Io/MeshDataReaderWriterTest.cs +++ b/test/DotRecast.Detour.Test/Io/MeshDataReaderWriterTest.cs @@ -131,7 +131,7 @@ public class MeshDataReaderWriterTest Assert.That(readData.offMeshCons[i].poly, Is.EqualTo(meshData.offMeshCons[i].poly)); Assert.That(readData.offMeshCons[i].side, Is.EqualTo(meshData.offMeshCons[i].side)); Assert.That(readData.offMeshCons[i].userId, Is.EqualTo(meshData.offMeshCons[i].userId)); - for (int j = 0; j < 6; j++) + for (int j = 0; j < 2; j++) { Assert.That(readData.offMeshCons[i].pos[j], Is.EqualTo(meshData.offMeshCons[i].pos[j])); } diff --git a/test/DotRecast.Detour.Test/NavMeshBuilderTest.cs b/test/DotRecast.Detour.Test/NavMeshBuilderTest.cs index 0a9393c..a1383f4 100644 --- a/test/DotRecast.Detour.Test/NavMeshBuilderTest.cs +++ b/test/DotRecast.Detour.Test/NavMeshBuilderTest.cs @@ -16,6 +16,7 @@ freely, subject to the following restrictions: 3. This notice may not be removed or altered from any source distribution. */ +using DotRecast.Core.Numerics; using NUnit.Framework; namespace DotRecast.Detour.Test; @@ -49,9 +50,9 @@ public class NavMeshBuilderTest Assert.That(nmd.bvTree[i], Is.Not.Null); } - for (int i = 0; i < 6; i++) + for (int i = 0; i < 2; i++) { - Assert.That(nmd.verts[223 * 3 + i], Is.EqualTo(nmd.offMeshCons[0].pos[i])); + Assert.That(RcVecUtils.Create(nmd.verts, 223 * 3 + (i * 3)), Is.EqualTo(nmd.offMeshCons[0].pos[i])); } Assert.That(nmd.offMeshCons[0].rad, Is.EqualTo(0.1f)); @@ -66,4 +67,4 @@ public class NavMeshBuilderTest Assert.That(nmd.polys[118].GetArea(), Is.EqualTo(2)); Assert.That(nmd.polys[118].GetPolyType(), Is.EqualTo(DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION)); } -} +} \ No newline at end of file