From fa2b7f4ed54ea368569320cbc7dd4d89bd33842c Mon Sep 17 00:00:00 2001 From: ikpil Date: Fri, 26 May 2023 21:34:29 +0900 Subject: [PATCH] move RecastVectors -> Vector3f --- src/DotRecast.Core/Vector3f.cs | 136 ++++++++++++++- .../Colliders/BoxCollider.cs | 10 +- .../Geom/DemoInputGeomProvider.cs | 8 +- .../Tools/Gizmos/BoxGizmo.cs | 6 +- .../Tools/Gizmos/CapsuleGizmo.cs | 16 +- .../Tools/Gizmos/CylinderGizmo.cs | 16 +- .../Tools/Gizmos/GizmoHelper.cs | 2 +- .../Geom/SimpleInputGeomProvider.cs | 8 +- .../Geom/SingleTrimeshInputGeomProvider.cs | 8 +- src/DotRecast.Recast/Recast.cs | 16 +- src/DotRecast.Recast/RecastArea.cs | 8 +- src/DotRecast.Recast/RecastBuilderConfig.cs | 4 +- src/DotRecast.Recast/RecastCompact.cs | 3 +- .../RecastFilledVolumeRasterization.cs | 36 ++-- src/DotRecast.Recast/RecastLayers.cs | 2 +- src/DotRecast.Recast/RecastMesh.cs | 4 +- src/DotRecast.Recast/RecastMeshDetail.cs | 32 ++-- src/DotRecast.Recast/RecastRasterization.cs | 28 +-- src/DotRecast.Recast/RecastVectors.cs | 160 ------------------ .../TestTiledNavMeshBuilder.cs | 2 +- .../AbstractTileCacheTest.cs | 2 +- 21 files changed, 237 insertions(+), 270 deletions(-) delete mode 100644 src/DotRecast.Recast/RecastVectors.cs diff --git a/src/DotRecast.Core/Vector3f.cs b/src/DotRecast.Core/Vector3f.cs index bf3fca0..14486e9 100644 --- a/src/DotRecast.Core/Vector3f.cs +++ b/src/DotRecast.Core/Vector3f.cs @@ -182,6 +182,7 @@ namespace DotRecast.Core return x * v[vi] + z * v[vi + 2]; } + public override bool Equals(object obj) { if (!(obj is Vector3f)) @@ -322,6 +323,19 @@ namespace DotRecast.Core + (v1.z * v2.z); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Dot(float[] v1, float[] v2) + { + return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Dot(float[] v1, Vector3f v2) + { + return v1[0] * v2.x + v1[1] * v2.y + v1[2] * v2.z; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float PerpXZ(Vector3f a, Vector3f b) { @@ -448,7 +462,7 @@ namespace DotRecast.Core float dz = verts[i + 2] - p.z; return dx * dx + dz * dz; } - + /// Derives the xz-plane 2D perp product of the two vectors. (uz*vx - ux*vz) /// @param[in] u The LHV vector [(x, y, z)] /// @param[in] v The RHV vector [(x, y, z)] @@ -461,7 +475,7 @@ namespace DotRecast.Core { return u.z * v.x - u.x * v.z; } - + /// Derives the square of the scalar length of the vector. (len * len) /// @param[in] v The vector. [(x, y, z)] /// @return The square of the scalar length of the vector. @@ -471,6 +485,7 @@ namespace DotRecast.Core return v.x * v.x + v.y * v.y + v.z * v.z; } + /// Checks that the specified vector's components are all finite. /// @param[in] v A point. [(x, y, z)] /// @return True if all of the point's components are finite, i.e. not NaN @@ -488,6 +503,121 @@ namespace DotRecast.Core { return float.IsFinite(v.x) && float.IsFinite(v.z); } + + public static void Min(ref Vector3f a, float[] b, int i) + { + a.x = Math.Min(a.x, b[i + 0]); + a.y = Math.Min(a.y, b[i + 1]); + a.z = Math.Min(a.z, b[i + 2]); + } + + public static void Min(ref Vector3f a, Vector3f b) + { + a.x = Math.Min(a.x, b.x); + a.y = Math.Min(a.y, b.y); + a.z = Math.Min(a.z, b.z); + } + + public static void Max(ref Vector3f a, float[] b, int i) + { + a.x = Math.Max(a.x, b[i + 0]); + a.y = Math.Max(a.y, b[i + 1]); + a.z = Math.Max(a.z, b[i + 2]); + } + + public static void Max(ref Vector3f a, Vector3f b) + { + a.x = Math.Max(a.x, b.x); + a.y = Math.Max(a.y, b.y); + a.z = Math.Max(a.z, b.z); + } + + public static void Copy(ref Vector3f @out, float[] @in, int i) + { + Copy(ref @out, 0, @in, i); + } + + public static void Copy(float[] @out, int n, float[] @in, int m) + { + @out[n] = @in[m]; + @out[n + 1] = @in[m + 1]; + @out[n + 2] = @in[m + 2]; + } + + public static void Copy(float[] @out, int n, Vector3f @in, int m) + { + @out[n] = @in[m]; + @out[n + 1] = @in[m + 1]; + @out[n + 2] = @in[m + 2]; + } + + public static void Copy(ref Vector3f @out, int n, float[] @in, int m) + { + @out[n] = @in[m]; + @out[n + 1] = @in[m + 1]; + @out[n + 2] = @in[m + 2]; + } + + public static void Add(ref Vector3f e0, Vector3f a, float[] verts, int i) + { + e0.x = a.x + verts[i]; + e0.y = a.y + verts[i + 1]; + e0.z = a.z + verts[i + 2]; + } + + + public static void Sub(ref Vector3f e0, float[] verts, int i, int j) + { + e0.x = verts[i] - verts[j]; + e0.y = verts[i + 1] - verts[j + 1]; + e0.z = verts[i + 2] - verts[j + 2]; + } + + + public static void Sub(ref Vector3f e0, Vector3f i, float[] verts, int j) + { + e0.x = i.x - verts[j]; + e0.y = i.y - verts[j + 1]; + e0.z = i.z - verts[j + 2]; + } + + + public static void Cross(float[] dest, float[] v1, float[] v2) + { + dest[0] = v1[1] * v2[2] - v1[2] * v2[1]; + dest[1] = v1[2] * v2[0] - v1[0] * v2[2]; + dest[2] = v1[0] * v2[1] - v1[1] * v2[0]; + } + + public static void Cross(float[] dest, Vector3f v1, Vector3f v2) + { + dest[0] = v1.y * v2.z - v1.z * v2.y; + dest[1] = v1.z * v2.x - v1.x * v2.z; + dest[2] = v1.x * v2.y - v1.y * v2.x; + } + + public static void Cross(ref Vector3f dest, Vector3f v1, Vector3f v2) + { + dest.x = v1.y * v2.z - v1.z * v2.y; + dest.y = v1.z * v2.x - v1.x * v2.z; + dest.z = v1.x * v2.y - v1.y * v2.x; + } + + + public static void Normalize(float[] v) + { + float d = (float)(1.0f / Math.Sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])); + v[0] *= d; + v[1] *= d; + v[2] *= d; + } + + public static void Normalize(ref Vector3f v) + { + float d = (float)(1.0f / Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z)); + v.x *= d; + v.y *= d; + v.z *= d; + } } - } \ No newline at end of file diff --git a/src/DotRecast.Detour.Dynamic/Colliders/BoxCollider.cs b/src/DotRecast.Detour.Dynamic/Colliders/BoxCollider.cs index 88fafef..8194e62 100644 --- a/src/DotRecast.Detour.Dynamic/Colliders/BoxCollider.cs +++ b/src/DotRecast.Detour.Dynamic/Colliders/BoxCollider.cs @@ -74,11 +74,11 @@ namespace DotRecast.Detour.Dynamic.Colliders Vector3f.Of(up.x, up.y, up.z), Vector3f.Zero }; - RecastVectors.Normalize(ref halfEdges[1]); - RecastVectors.Cross(ref halfEdges[0], up, forward); - RecastVectors.Normalize(ref halfEdges[0]); - RecastVectors.Cross(ref halfEdges[2], halfEdges[0], up); - RecastVectors.Normalize(ref halfEdges[2]); + Vector3f.Normalize(ref halfEdges[1]); + Vector3f.Cross(ref halfEdges[0], up, forward); + Vector3f.Normalize(ref halfEdges[0]); + Vector3f.Cross(ref halfEdges[2], halfEdges[0], up); + Vector3f.Normalize(ref halfEdges[2]); halfEdges[0].x *= extent.x; halfEdges[0].y *= extent.x; halfEdges[0].z *= extent.x; diff --git a/src/DotRecast.Recast.Demo/Geom/DemoInputGeomProvider.cs b/src/DotRecast.Recast.Demo/Geom/DemoInputGeomProvider.cs index fca6294..269b0cf 100644 --- a/src/DotRecast.Recast.Demo/Geom/DemoInputGeomProvider.cs +++ b/src/DotRecast.Recast.Demo/Geom/DemoInputGeomProvider.cs @@ -73,12 +73,12 @@ public class DemoInputGeomProvider : IInputGeomProvider CalculateNormals(); bmin = Vector3f.Zero; bmax = Vector3f.Zero; - RecastVectors.Copy(ref bmin, vertices, 0); - RecastVectors.Copy(ref bmax, vertices, 0); + Vector3f.Copy(ref bmin, vertices, 0); + Vector3f.Copy(ref bmax, vertices, 0); for (int i = 1; i < vertices.Length / 3; i++) { - RecastVectors.Min(ref bmin, vertices, i * 3); - RecastVectors.Max(ref bmax, vertices, i * 3); + Vector3f.Min(ref bmin, vertices, i * 3); + Vector3f.Max(ref bmax, vertices, i * 3); } _mesh = new TriMesh(vertices, faces); diff --git a/src/DotRecast.Recast.Demo/Tools/Gizmos/BoxGizmo.cs b/src/DotRecast.Recast.Demo/Tools/Gizmos/BoxGizmo.cs index 76f2180..6315d8a 100644 --- a/src/DotRecast.Recast.Demo/Tools/Gizmos/BoxGizmo.cs +++ b/src/DotRecast.Recast.Demo/Tools/Gizmos/BoxGizmo.cs @@ -56,9 +56,9 @@ public class BoxGizmo : IColliderGizmo float[] vertices = new float[8 * 3]; for (int i = 0; i < 8; i++) { - vertices[i * 3 + 0] = RecastVectors.Dot(VERTS[i], trX) + center.x; - vertices[i * 3 + 1] = RecastVectors.Dot(VERTS[i], trY) + center.y; - vertices[i * 3 + 2] = RecastVectors.Dot(VERTS[i], trZ) + center.z; + vertices[i * 3 + 0] = Vector3f.Dot(VERTS[i], trX) + center.x; + vertices[i * 3 + 1] = Vector3f.Dot(VERTS[i], trY) + center.y; + vertices[i * 3 + 2] = Vector3f.Dot(VERTS[i], trZ) + center.z; } debugDraw.Begin(DebugDrawPrimitives.TRIS); diff --git a/src/DotRecast.Recast.Demo/Tools/Gizmos/CapsuleGizmo.cs b/src/DotRecast.Recast.Demo/Tools/Gizmos/CapsuleGizmo.cs index 581f563..835cc3f 100644 --- a/src/DotRecast.Recast.Demo/Tools/Gizmos/CapsuleGizmo.cs +++ b/src/DotRecast.Recast.Demo/Tools/Gizmos/CapsuleGizmo.cs @@ -1,6 +1,6 @@ using DotRecast.Core; using DotRecast.Recast.Demo.Draw; -using static DotRecast.Recast.RecastVectors; + using static DotRecast.Core.RcMath; using static DotRecast.Recast.Demo.Tools.Gizmos.GizmoHelper; @@ -23,11 +23,11 @@ public class CapsuleGizmo : IColliderGizmo Vector3f axis = Vector3f.Of(end.x - start.x, end.y - start.y, end.z - start.z); Vector3f[] normals = new Vector3f[3]; normals[1] = Vector3f.Of(end.x - start.x, end.y - start.y, end.z - start.z); - Normalize(ref normals[1]); + Vector3f.Normalize(ref normals[1]); normals[0] = GetSideVector(axis); normals[2] = Vector3f.Zero; - Cross(ref normals[2], normals[0], normals[1]); - Normalize(ref normals[2]); + Vector3f.Cross(ref normals[2], normals[0], normals[1]); + Vector3f.Normalize(ref normals[2]); triangles = GenerateSphericalTriangles(); var trX = Vector3f.Of(normals[0].x, normals[1].x, normals[2].x); var trY = Vector3f.Of(normals[0].y, normals[1].y, normals[2].y); @@ -49,7 +49,7 @@ public class CapsuleGizmo : IColliderGizmo v.x = vertices[i] - center[0]; v.y = vertices[i + 1] - center[1]; v.z = vertices[i + 2] - center[2]; - Normalize(ref v); + Vector3f.Normalize(ref v); gradient[i / 3] = Clamp(0.57735026f * (v.x + v.y + v.z), -1, 1); } } @@ -63,9 +63,9 @@ public class CapsuleGizmo : IColliderGizmo } Vector3f forward = new Vector3f(); - Cross(ref forward, side, axis); - Cross(ref side, axis, forward); - Normalize(ref side); + Vector3f.Cross(ref forward, side, axis); + Vector3f.Cross(ref side, axis, forward); + Vector3f.Normalize(ref side); return side; } diff --git a/src/DotRecast.Recast.Demo/Tools/Gizmos/CylinderGizmo.cs b/src/DotRecast.Recast.Demo/Tools/Gizmos/CylinderGizmo.cs index e1e134b..288d0df 100644 --- a/src/DotRecast.Recast.Demo/Tools/Gizmos/CylinderGizmo.cs +++ b/src/DotRecast.Recast.Demo/Tools/Gizmos/CylinderGizmo.cs @@ -1,6 +1,6 @@ using DotRecast.Core; using DotRecast.Recast.Demo.Draw; -using static DotRecast.Recast.RecastVectors; + using static DotRecast.Core.RcMath; using static DotRecast.Recast.Demo.Tools.Gizmos.GizmoHelper; @@ -23,11 +23,11 @@ public class CylinderGizmo : IColliderGizmo Vector3f axis = Vector3f.Of(end.x - start.x, end.y - start.y, end.z - start.z); Vector3f[] normals = new Vector3f[3]; normals[1] = Vector3f.Of(end.x - start.x, end.y - start.y, end.z - start.z); - Normalize(ref normals[1]); + Vector3f.Normalize(ref normals[1]); normals[0] = GetSideVector(axis); normals[2] = Vector3f.Zero; - Cross(ref normals[2], normals[0], normals[1]); - Normalize(ref normals[2]); + Vector3f.Cross(ref normals[2], normals[0], normals[1]); + Vector3f.Normalize(ref normals[2]); triangles = GenerateCylindricalTriangles(); Vector3f trX = Vector3f.Of(normals[0].x, normals[1].x, normals[2].x); Vector3f trY = Vector3f.Of(normals[0].y, normals[1].y, normals[2].y); @@ -54,7 +54,7 @@ public class CylinderGizmo : IColliderGizmo v.x = vertices[i] - center.x; v.y = vertices[i + 1] - center.y; v.z = vertices[i + 2] - center.z; - Normalize(ref v); + Vector3f.Normalize(ref v); gradient[i / 3] = Clamp(0.57735026f * (v.x + v.y + v.z), -1, 1); } } @@ -69,9 +69,9 @@ public class CylinderGizmo : IColliderGizmo } Vector3f forward = new Vector3f(); - Cross(ref forward, side, axis); - Cross(ref side, axis, forward); - Normalize(ref side); + Vector3f.Cross(ref forward, side, axis); + Vector3f.Cross(ref side, axis, forward); + Vector3f.Normalize(ref side); return side; } diff --git a/src/DotRecast.Recast.Demo/Tools/Gizmos/GizmoHelper.cs b/src/DotRecast.Recast.Demo/Tools/Gizmos/GizmoHelper.cs index 162b773..1d2b069 100644 --- a/src/DotRecast.Recast.Demo/Tools/Gizmos/GizmoHelper.cs +++ b/src/DotRecast.Recast.Demo/Tools/Gizmos/GizmoHelper.cs @@ -188,7 +188,7 @@ public class GizmoHelper normal.x = e0.y * e1.z - e0.z * e1.y; normal.y = e0.z * e1.x - e0.x * e1.z; normal.z = e0.x * e1.y - e0.y * e1.x; - RecastVectors.Normalize(ref normal); + Vector3f.Normalize(ref normal); float c = Clamp(0.57735026f * (normal.x + normal.y + normal.z), -1, 1); int col = DebugDraw.DuLerpCol(DebugDraw.DuRGBA(32, 32, 0, 160), DebugDraw.DuRGBA(220, 220, 0, 160), (int)(127 * (1 + c))); diff --git a/src/DotRecast.Recast/Geom/SimpleInputGeomProvider.cs b/src/DotRecast.Recast/Geom/SimpleInputGeomProvider.cs index d9b7a4a..349a7e7 100644 --- a/src/DotRecast.Recast/Geom/SimpleInputGeomProvider.cs +++ b/src/DotRecast.Recast/Geom/SimpleInputGeomProvider.cs @@ -70,12 +70,12 @@ namespace DotRecast.Recast.Geom CalculateNormals(); bmin = Vector3f.Zero; bmax = Vector3f.Zero; - RecastVectors.Copy(ref bmin, vertices, 0); - RecastVectors.Copy(ref bmax, vertices, 0); + Vector3f.Copy(ref bmin, vertices, 0); + Vector3f.Copy(ref bmax, vertices, 0); for (int i = 1; i < vertices.Length / 3; i++) { - RecastVectors.Min(ref bmin, vertices, i * 3); - RecastVectors.Max(ref bmax, vertices, i * 3); + Vector3f.Min(ref bmin, vertices, i * 3); + Vector3f.Max(ref bmax, vertices, i * 3); } _mesh = new TriMesh(vertices, faces); diff --git a/src/DotRecast.Recast/Geom/SingleTrimeshInputGeomProvider.cs b/src/DotRecast.Recast/Geom/SingleTrimeshInputGeomProvider.cs index c8bf091..630cc4a 100644 --- a/src/DotRecast.Recast/Geom/SingleTrimeshInputGeomProvider.cs +++ b/src/DotRecast.Recast/Geom/SingleTrimeshInputGeomProvider.cs @@ -33,12 +33,12 @@ namespace DotRecast.Recast.Geom { bmin = Vector3f.Zero; bmax = Vector3f.Zero; - RecastVectors.Copy(ref bmin, vertices, 0); - RecastVectors.Copy(ref bmax, vertices, 0); + Vector3f.Copy(ref bmin, vertices, 0); + Vector3f.Copy(ref bmax, vertices, 0); for (int i = 1; i < vertices.Length / 3; i++) { - RecastVectors.Min(ref bmin, vertices, i * 3); - RecastVectors.Max(ref bmax, vertices, i * 3); + Vector3f.Min(ref bmin, vertices, i * 3); + Vector3f.Max(ref bmax, vertices, i * 3); } _mesh = new TriMesh(vertices, faces); diff --git a/src/DotRecast.Recast/Recast.cs b/src/DotRecast.Recast/Recast.cs index 247b6d1..fd1c209 100644 --- a/src/DotRecast.Recast/Recast.cs +++ b/src/DotRecast.Recast/Recast.cs @@ -101,20 +101,20 @@ namespace DotRecast.Recast { Vector3f e0 = new Vector3f(); Vector3f e1 = new Vector3f(); - RecastVectors.Sub(ref e0, verts, v1 * 3, v0 * 3); - RecastVectors.Sub(ref e1, verts, v2 * 3, v0 * 3); - RecastVectors.Cross(norm, e0, e1); - RecastVectors.Normalize(norm); + Vector3f.Sub(ref e0, verts, v1 * 3, v0 * 3); + Vector3f.Sub(ref e1, verts, v2 * 3, v0 * 3); + Vector3f.Cross(norm, e0, e1); + Vector3f.Normalize(norm); } static void CalcTriNormal(float[] verts, int v0, int v1, int v2, ref Vector3f norm) { Vector3f e0 = new Vector3f(); Vector3f e1 = new Vector3f(); - RecastVectors.Sub(ref e0, verts, v1 * 3, v0 * 3); - RecastVectors.Sub(ref e1, verts, v2 * 3, v0 * 3); - RecastVectors.Cross(ref norm, e0, e1); - RecastVectors.Normalize(ref norm); + Vector3f.Sub(ref e0, verts, v1 * 3, v0 * 3); + Vector3f.Sub(ref e1, verts, v2 * 3, v0 * 3); + Vector3f.Cross(ref norm, e0, e1); + Vector3f.Normalize(ref norm); } diff --git a/src/DotRecast.Recast/RecastArea.cs b/src/DotRecast.Recast/RecastArea.cs index a667b18..20a6b83 100644 --- a/src/DotRecast.Recast/RecastArea.cs +++ b/src/DotRecast.Recast/RecastArea.cs @@ -364,12 +364,12 @@ namespace DotRecast.Recast Vector3f bmin = new Vector3f(); Vector3f bmax = new Vector3f(); - RecastVectors.Copy(ref bmin, verts, 0); - RecastVectors.Copy(ref bmax, verts, 0); + Vector3f.Copy(ref bmin, verts, 0); + Vector3f.Copy(ref bmax, verts, 0); for (int i = 3; i < verts.Length; i += 3) { - RecastVectors.Min(ref bmin, verts, i); - RecastVectors.Max(ref bmax, verts, i); + Vector3f.Min(ref bmin, verts, i); + Vector3f.Max(ref bmax, verts, i); } bmin.y = hmin; diff --git a/src/DotRecast.Recast/RecastBuilderConfig.cs b/src/DotRecast.Recast/RecastBuilderConfig.cs index 55e1e5d..95092fb 100644 --- a/src/DotRecast.Recast/RecastBuilderConfig.cs +++ b/src/DotRecast.Recast/RecastBuilderConfig.cs @@ -22,8 +22,6 @@ using DotRecast.Core; namespace DotRecast.Recast { - using static RecastVectors; - public class RecastBuilderConfig { public readonly RecastConfig cfg; @@ -100,4 +98,4 @@ namespace DotRecast.Recast } } } -} +} \ No newline at end of file diff --git a/src/DotRecast.Recast/RecastCompact.cs b/src/DotRecast.Recast/RecastCompact.cs index 4300327..82be555 100644 --- a/src/DotRecast.Recast/RecastCompact.cs +++ b/src/DotRecast.Recast/RecastCompact.cs @@ -17,10 +17,9 @@ freely, subject to the following restrictions: */ using System; - using static DotRecast.Core.RcMath; using static DotRecast.Recast.RecastConstants; -using static DotRecast.Recast.RecastVectors; + namespace DotRecast.Recast { diff --git a/src/DotRecast.Recast/RecastFilledVolumeRasterization.cs b/src/DotRecast.Recast/RecastFilledVolumeRasterization.cs index d5d55bb..6cd022d 100644 --- a/src/DotRecast.Recast/RecastFilledVolumeRasterization.cs +++ b/src/DotRecast.Recast/RecastFilledVolumeRasterization.cs @@ -20,7 +20,7 @@ using System; using DotRecast.Core; using static DotRecast.Core.RcMath; using static DotRecast.Recast.RecastConstants; -using static DotRecast.Recast.RecastVectors; + namespace DotRecast.Recast { @@ -84,9 +84,9 @@ namespace DotRecast.Recast Vector3f.Of(halfEdges[1].x, halfEdges[1].y, halfEdges[1].z), Vector3f.Of(halfEdges[2].x, halfEdges[2].y, halfEdges[2].z), }; - Normalize(ref normals[0]); - Normalize(ref normals[1]); - Normalize(ref normals[2]); + Vector3f.Normalize(ref normals[0]); + Vector3f.Normalize(ref normals[1]); + Vector3f.Normalize(ref normals[2]); float[] vertices = new float[8 * 3]; float[] bounds = new float[] @@ -184,7 +184,7 @@ namespace DotRecast.Recast private static void Plane(float[][] planes, int p, float[] v1, float[] v2, float[] vertices, int vert) { - RecastVectors.Cross(planes[p], v1, v2); + Vector3f.Cross(planes[p], v1, v2); planes[p][3] = planes[p][0] * vertices[vert] + planes[p][1] * vertices[vert + 1] + planes[p][2] * vertices[vert + 2]; } @@ -306,14 +306,14 @@ namespace DotRecast.Recast { Vector3f[] rectangleOnStartPlane = new Vector3f[4]; Vector3f[] rectangleOnEndPlane = new Vector3f[4]; - float ds = Dot(axis, start); - float de = Dot(axis, end); + float ds = Vector3f.Dot(axis, start); + float de = Vector3f.Dot(axis, end); for (int i = 0; i < 4; i++) { float x = rectangle[(i + 1) & 2]; float z = rectangle[(i & 2) + 1]; Vector3f a = Vector3f.Of(x, rectangle[4], z); - float dotAxisA = Dot(axis, a); + float dotAxisA = Vector3f.Dot(axis, a); float t = (ds - dotAxisA) / axis.y; rectangleOnStartPlane[i].x = x; rectangleOnStartPlane[i].y = rectangle[4] + t; @@ -348,9 +348,9 @@ namespace DotRecast.Recast rectangleOnPlane[j].y - rectangleOnPlane[i].y, rectangleOnPlane[j].z - rectangleOnPlane[i].z ); - float dl = Dot(d, d); - float b = Dot(m, d) / dl; - float c = (Dot(m, m) - radiusSqr) / dl; + float dl = Vector3f.Dot(d, d); + float b = Vector3f.Dot(m, d) / dl; + float c = (Vector3f.Dot(m, m) - radiusSqr) / dl; float discr = b * b - c; if (discr > EPSILON) { @@ -429,10 +429,10 @@ namespace DotRecast.Recast Vector3f d = axis; Vector3f m = Vector3f.Of(point.x - start.x, point.y - start.y, point.z - start.z); // float[] n = { 0, 1, 0 }; - float md = Dot(m, d); + float md = Vector3f.Dot(m, d); // float nd = Dot(n, d); float nd = axis.y; - float dd = Dot(d, d); + float dd = Vector3f.Dot(d, d); // float nn = Dot(n, n); float nn = 1; @@ -440,7 +440,7 @@ namespace DotRecast.Recast float mn = m.y; // float a = dd * nn - nd * nd; float a = dd - nd * nd; - float k = Dot(m, m) - radiusSqr; + float k = Vector3f.Dot(m, m) - radiusSqr; float c = dd * k - md * md; if (Math.Abs(a) < EPSILON) { @@ -534,7 +534,7 @@ namespace DotRecast.Recast { if (Math.Abs(planes[j][1]) > EPSILON) { - float dotNormalPoint = Dot(planes[j], point); + float dotNormalPoint = Vector3f.Dot(planes[j], point); float t = (planes[j][3] - dotNormalPoint) / planes[j][1]; float y = point.y + t; bool valid = true; @@ -744,15 +744,15 @@ namespace DotRecast.Recast private static float? RayTriangleIntersection(Vector3f point, int plane, float[][] planes) { - float t = (planes[plane][3] - Dot(planes[plane], point)) / planes[plane][1]; + float t = (planes[plane][3] - Vector3f.Dot(planes[plane], point)) / planes[plane][1]; float[] s = { point.x, point.y + t, point.z }; - float u = Dot(s, planes[plane + 1]) - planes[plane + 1][3]; + float u = Vector3f.Dot(s, planes[plane + 1]) - planes[plane + 1][3]; if (u < 0.0f || u > 1.0f) { return null; } - float v = Dot(s, planes[plane + 2]) - planes[plane + 2][3]; + float v = Vector3f.Dot(s, planes[plane + 2]) - planes[plane + 2][3]; if (v < 0.0f) { return null; diff --git a/src/DotRecast.Recast/RecastLayers.cs b/src/DotRecast.Recast/RecastLayers.cs index 6706306..21474bc 100644 --- a/src/DotRecast.Recast/RecastLayers.cs +++ b/src/DotRecast.Recast/RecastLayers.cs @@ -26,7 +26,7 @@ namespace DotRecast.Recast { using static RecastCommon; using static RecastConstants; - using static RecastVectors; + using static RecastRegion; public class RecastLayers diff --git a/src/DotRecast.Recast/RecastMesh.cs b/src/DotRecast.Recast/RecastMesh.cs index 01c7290..7a01aae 100644 --- a/src/DotRecast.Recast/RecastMesh.cs +++ b/src/DotRecast.Recast/RecastMesh.cs @@ -1234,8 +1234,8 @@ namespace DotRecast.Recast int maxVertsPerMesh = 0; for (int i = 0; i < nmeshes; ++i) { - RecastVectors.Min(ref mesh.bmin, meshes[i].bmin); - RecastVectors.Max(ref mesh.bmax, meshes[i].bmax); + Vector3f.Min(ref mesh.bmin, meshes[i].bmin); + Vector3f.Max(ref mesh.bmax, meshes[i].bmax); maxVertsPerMesh = Math.Max(maxVertsPerMesh, meshes[i].nverts); maxVerts += meshes[i].nverts; maxPolys += meshes[i].npolys; diff --git a/src/DotRecast.Recast/RecastMeshDetail.cs b/src/DotRecast.Recast/RecastMeshDetail.cs index 48ab218..90682ef 100644 --- a/src/DotRecast.Recast/RecastMeshDetail.cs +++ b/src/DotRecast.Recast/RecastMeshDetail.cs @@ -161,8 +161,8 @@ namespace DotRecast.Recast Vector3f v1 = new Vector3f(); Vector3f v2 = new Vector3f(); Vector3f v3 = new Vector3f(); - RecastVectors.Sub(ref v2, verts, p2, p1); - RecastVectors.Sub(ref v3, verts, p3, p1); + Vector3f.Sub(ref v2, verts, p2, p1); + Vector3f.Sub(ref v3, verts, p3, p1); float cp = Vcross2(v1, v2, v3); if (Math.Abs(cp) > EPS) @@ -174,11 +174,11 @@ namespace DotRecast.Recast c.y = 0; c.z = (v1Sq * (v3.x - v2.x) + v2Sq * (v1.x - v3.x) + v3Sq * (v2.x - v1.x)) / (2 * cp); r.Exchange(Vdist2(c, v1)); - RecastVectors.Add(ref c, c, verts, p1); + Vector3f.Add(ref c, c, verts, p1); return true; } - RecastVectors.Copy(ref c, verts, p1); + Vector3f.Copy(ref c, verts, p1); r.Exchange(0f); return false; } @@ -188,9 +188,9 @@ namespace DotRecast.Recast Vector3f v0 = new Vector3f(); Vector3f v1 = new Vector3f(); Vector3f v2 = new Vector3f(); - RecastVectors.Sub(ref v0, verts, c, a); - RecastVectors.Sub(ref v1, verts, b, a); - RecastVectors.Sub(ref v2, p, verts, a); + Vector3f.Sub(ref v0, verts, c, a); + Vector3f.Sub(ref v1, verts, b, a); + Vector3f.Sub(ref v2, p, verts, a); float dot00 = Vdot2(v0, v0); float dot01 = Vdot2(v0, v1); @@ -845,7 +845,7 @@ namespace DotRecast.Recast for (int i = 0; i < nin; ++i) { - RecastVectors.Copy(verts, i * 3, @in, i * 3); + Vector3f.Copy(verts, i * 3, @in, i * 3); } tris.Clear(); @@ -964,7 +964,7 @@ namespace DotRecast.Recast { for (int k = nidx - 2; k > 0; --k) { - RecastVectors.Copy(verts, nverts * 3, edge, idx[k] * 3); + Vector3f.Copy(verts, nverts * 3, edge, idx[k] * 3); hull[nhull++] = nverts; nverts++; } @@ -973,7 +973,7 @@ namespace DotRecast.Recast { for (int k = 1; k < nidx - 1; ++k) { - RecastVectors.Copy(verts, nverts * 3, edge, idx[k] * 3); + Vector3f.Copy(verts, nverts * 3, edge, idx[k] * 3); hull[nhull++] = nverts; nverts++; } @@ -1005,12 +1005,12 @@ namespace DotRecast.Recast // Create sample locations in a grid. Vector3f bmin = new Vector3f(); Vector3f bmax = new Vector3f(); - RecastVectors.Copy(ref bmin, @in, 0); - RecastVectors.Copy(ref bmax, @in, 0); + Vector3f.Copy(ref bmin, @in, 0); + Vector3f.Copy(ref bmax, @in, 0); for (int i = 1; i < nin; ++i) { - RecastVectors.Min(ref bmin, @in, i * 3); - RecastVectors.Max(ref bmax, @in, i * 3); + Vector3f.Min(ref bmin, @in, i * 3); + Vector3f.Max(ref bmax, @in, i * 3); } int x0 = (int)Math.Floor(bmin.x / sampleDist); @@ -1091,7 +1091,7 @@ namespace DotRecast.Recast // Mark sample as added. samples[besti * 4 + 3] = 1; // Add the new sample point. - RecastVectors.Copy(verts, nverts * 3, bestpt, 0); + Vector3f.Copy(verts, nverts * 3, bestpt, 0); nverts++; // Create new triangulation. @@ -1666,7 +1666,7 @@ namespace DotRecast.Recast for (int k = 0; k < dm.nverts; ++k) { - RecastVectors.Copy(mesh.verts, mesh.nverts * 3, dm.verts, k * 3); + Vector3f.Copy(mesh.verts, mesh.nverts * 3, dm.verts, k * 3); mesh.nverts++; } diff --git a/src/DotRecast.Recast/RecastRasterization.cs b/src/DotRecast.Recast/RecastRasterization.cs index a7778f0..ae8c7fb 100644 --- a/src/DotRecast.Recast/RecastRasterization.cs +++ b/src/DotRecast.Recast/RecastRasterization.cs @@ -191,19 +191,19 @@ namespace DotRecast.Recast + (inVerts[inVertsOffset + i * 3 + 1] - inVerts[inVertsOffset + j * 3 + 1]) * s; inVerts[outVerts1 + m * 3 + 2] = inVerts[inVertsOffset + j * 3 + 2] + (inVerts[inVertsOffset + i * 3 + 2] - inVerts[inVertsOffset + j * 3 + 2]) * s; - RecastVectors.Copy(inVerts, outVerts2 + n * 3, inVerts, outVerts1 + m * 3); + Vector3f.Copy(inVerts, outVerts2 + n * 3, inVerts, outVerts1 + m * 3); m++; n++; // add the i'th point to the right polygon. Do NOT add points that are on the dividing line // since these were already added above if (d[i] > 0) { - RecastVectors.Copy(inVerts, outVerts1 + m * 3, inVerts, inVertsOffset + i * 3); + Vector3f.Copy(inVerts, outVerts1 + m * 3, inVerts, inVertsOffset + i * 3); m++; } else if (d[i] < 0) { - RecastVectors.Copy(inVerts, outVerts2 + n * 3, inVerts, inVertsOffset + i * 3); + Vector3f.Copy(inVerts, outVerts2 + n * 3, inVerts, inVertsOffset + i * 3); n++; } } @@ -212,13 +212,13 @@ namespace DotRecast.Recast // add the i'th point to the right polygon. Addition is done even for points on the dividing line if (d[i] >= 0) { - RecastVectors.Copy(inVerts, outVerts1 + m * 3, inVerts, inVertsOffset + i * 3); + Vector3f.Copy(inVerts, outVerts1 + m * 3, inVerts, inVertsOffset + i * 3); m++; if (d[i] != 0) continue; } - RecastVectors.Copy(inVerts, outVerts2 + n * 3, inVerts, inVertsOffset + i * 3); + Vector3f.Copy(inVerts, outVerts2 + n * 3, inVerts, inVertsOffset + i * 3); n++; } } @@ -263,12 +263,12 @@ namespace DotRecast.Recast float by = hfBBMax.y - hfBBMin.y; // Calculate the bounding box of the triangle. - RecastVectors.Copy(ref tmin, verts, v0 * 3); - RecastVectors.Copy(ref tmax, verts, v0 * 3); - RecastVectors.Min(ref tmin, verts, v1 * 3); - RecastVectors.Min(ref tmin, verts, v2 * 3); - RecastVectors.Max(ref tmax, verts, v1 * 3); - RecastVectors.Max(ref tmax, verts, v2 * 3); + Vector3f.Copy(ref tmin, verts, v0 * 3); + Vector3f.Copy(ref tmax, verts, v0 * 3); + Vector3f.Min(ref tmin, verts, v1 * 3); + Vector3f.Min(ref tmin, verts, v2 * 3); + Vector3f.Max(ref tmax, verts, v1 * 3); + Vector3f.Max(ref tmax, verts, v2 * 3); // If the triangle does not touch the bbox of the heightfield, skip the triagle. if (!OverlapBounds(hfBBMin, hfBBMax, tmin, tmax)) @@ -291,9 +291,9 @@ namespace DotRecast.Recast int p1 = inRow + 7 * 3; int p2 = p1 + 7 * 3; - RecastVectors.Copy(buf, 0, verts, v0 * 3); - RecastVectors.Copy(buf, 3, verts, v1 * 3); - RecastVectors.Copy(buf, 6, verts, v2 * 3); + Vector3f.Copy(buf, 0, verts, v0 * 3); + Vector3f.Copy(buf, 3, verts, v1 * 3); + Vector3f.Copy(buf, 6, verts, v2 * 3); int nvRow, nvIn = 3; for (int z = z0; z <= z1; ++z) diff --git a/src/DotRecast.Recast/RecastVectors.cs b/src/DotRecast.Recast/RecastVectors.cs deleted file mode 100644 index 532091f..0000000 --- a/src/DotRecast.Recast/RecastVectors.cs +++ /dev/null @@ -1,160 +0,0 @@ -/* -Copyright (c) 2009-2010 Mikko Mononen memon@inside.org -recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org -DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: -1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. -2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. -3. This notice may not be removed or altered from any source distribution. -*/ - -using System; -using DotRecast.Core; - -namespace DotRecast.Recast -{ - public static class RecastVectors - { - public static void Min(ref Vector3f a, float[] b, int i) - { - a.x = Math.Min(a.x, b[i + 0]); - a.y = Math.Min(a.y, b[i + 1]); - a.z = Math.Min(a.z, b[i + 2]); - } - - public static void Min(ref Vector3f a, Vector3f b) - { - a.x = Math.Min(a.x, b.x); - a.y = Math.Min(a.y, b.y); - a.z = Math.Min(a.z, b.z); - } - - public static void Max(ref Vector3f a, float[] b, int i) - { - a.x = Math.Max(a.x, b[i + 0]); - a.y = Math.Max(a.y, b[i + 1]); - a.z = Math.Max(a.z, b[i + 2]); - } - - public static void Max(ref Vector3f a, Vector3f b) - { - a.x = Math.Max(a.x, b.x); - a.y = Math.Max(a.y, b.y); - a.z = Math.Max(a.z, b.z); - } - - public static void Copy(ref Vector3f @out, float[] @in, int i) - { - Copy(ref @out, 0, @in, i); - } - - public static void Copy(float[] @out, int n, float[] @in, int m) - { - @out[n] = @in[m]; - @out[n + 1] = @in[m + 1]; - @out[n + 2] = @in[m + 2]; - } - - public static void Copy(float[] @out, int n, Vector3f @in, int m) - { - @out[n] = @in[m]; - @out[n + 1] = @in[m + 1]; - @out[n + 2] = @in[m + 2]; - } - - public static void Copy(ref Vector3f @out, int n, float[] @in, int m) - { - @out[n] = @in[m]; - @out[n + 1] = @in[m + 1]; - @out[n + 2] = @in[m + 2]; - } - - public static void Add(ref Vector3f e0, Vector3f a, float[] verts, int i) - { - e0.x = a.x + verts[i]; - e0.y = a.y + verts[i + 1]; - e0.z = a.z + verts[i + 2]; - } - - - public static void Sub(ref Vector3f e0, float[] verts, int i, int j) - { - e0.x = verts[i] - verts[j]; - e0.y = verts[i + 1] - verts[j + 1]; - e0.z = verts[i + 2] - verts[j + 2]; - } - - - public static void Sub(ref Vector3f e0, Vector3f i, float[] verts, int j) - { - e0.x = i.x - verts[j]; - e0.y = i.y - verts[j + 1]; - e0.z = i.z - verts[j + 2]; - } - - - public static void Cross(float[] dest, float[] v1, float[] v2) - { - dest[0] = v1[1] * v2[2] - v1[2] * v2[1]; - dest[1] = v1[2] * v2[0] - v1[0] * v2[2]; - dest[2] = v1[0] * v2[1] - v1[1] * v2[0]; - } - - public static void Cross(float[] dest, Vector3f v1, Vector3f v2) - { - dest[0] = v1.y * v2.z - v1.z * v2.y; - dest[1] = v1.z * v2.x - v1.x * v2.z; - dest[2] = v1.x * v2.y - v1.y * v2.x; - } - - public static void Cross(ref Vector3f dest, Vector3f v1, Vector3f v2) - { - dest.x = v1.y * v2.z - v1.z * v2.y; - dest.y = v1.z * v2.x - v1.x * v2.z; - dest.z = v1.x * v2.y - v1.y * v2.x; - } - - - public static void Normalize(float[] v) - { - float d = (float)(1.0f / Math.Sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])); - v[0] *= d; - v[1] *= d; - v[2] *= d; - } - - public static void Normalize(ref Vector3f v) - { - float d = (float)(1.0f / Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z)); - v.x *= d; - v.y *= d; - v.z *= d; - } - - public static float Dot(float[] v1, float[] v2) - { - return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]; - } - - public static float Dot(float[] v1, Vector3f v2) - { - return v1[0] * v2.x + v1[1] * v2.y + v1[2] * v2.z; - } - - public static float Dot(Vector3f v1, Vector3f v2) - { - return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; - } - - } -} diff --git a/test/DotRecast.Detour.Test/TestTiledNavMeshBuilder.cs b/test/DotRecast.Detour.Test/TestTiledNavMeshBuilder.cs index dae3bf4..f76ae6c 100644 --- a/test/DotRecast.Detour.Test/TestTiledNavMeshBuilder.cs +++ b/test/DotRecast.Detour.Test/TestTiledNavMeshBuilder.cs @@ -20,7 +20,7 @@ using System.Collections.Generic; using DotRecast.Core; using DotRecast.Recast; using DotRecast.Recast.Geom; -using static DotRecast.Recast.RecastVectors; + namespace DotRecast.Detour.Test; diff --git a/test/DotRecast.Detour.TileCache.Test/AbstractTileCacheTest.cs b/test/DotRecast.Detour.TileCache.Test/AbstractTileCacheTest.cs index 0d36c09..eea4abe 100644 --- a/test/DotRecast.Detour.TileCache.Test/AbstractTileCacheTest.cs +++ b/test/DotRecast.Detour.TileCache.Test/AbstractTileCacheTest.cs @@ -23,7 +23,7 @@ using DotRecast.Detour.TileCache.Io.Compress; using DotRecast.Recast.Geom; using NUnit.Framework; using static DotRecast.Core.RcMath; -using static DotRecast.Recast.RecastVectors; + namespace DotRecast.Detour.TileCache.Test;