move RecastVectors -> Vector3f

This commit is contained in:
ikpil 2023-05-26 21:34:29 +09:00
parent 995c062a18
commit fa2b7f4ed5
21 changed files with 237 additions and 270 deletions

View File

@ -182,6 +182,7 @@ namespace DotRecast.Core
return x * v[vi] + z * v[vi + 2]; return x * v[vi] + z * v[vi + 2];
} }
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (!(obj is Vector3f)) if (!(obj is Vector3f))
@ -322,6 +323,19 @@ namespace DotRecast.Core
+ (v1.z * v2.z); + (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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float PerpXZ(Vector3f a, Vector3f b) public static float PerpXZ(Vector3f a, Vector3f b)
{ {
@ -471,6 +485,7 @@ namespace DotRecast.Core
return v.x * v.x + v.y * v.y + v.z * v.z; return v.x * v.x + v.y * v.y + v.z * v.z;
} }
/// Checks that the specified vector's components are all finite. /// Checks that the specified vector's components are all finite.
/// @param[in] v A point. [(x, y, z)] /// @param[in] v A point. [(x, y, z)]
/// @return True if all of the point's components are finite, i.e. not NaN /// @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); 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;
}
}
} }

View File

@ -74,11 +74,11 @@ namespace DotRecast.Detour.Dynamic.Colliders
Vector3f.Of(up.x, up.y, up.z), Vector3f.Of(up.x, up.y, up.z),
Vector3f.Zero Vector3f.Zero
}; };
RecastVectors.Normalize(ref halfEdges[1]); Vector3f.Normalize(ref halfEdges[1]);
RecastVectors.Cross(ref halfEdges[0], up, forward); Vector3f.Cross(ref halfEdges[0], up, forward);
RecastVectors.Normalize(ref halfEdges[0]); Vector3f.Normalize(ref halfEdges[0]);
RecastVectors.Cross(ref halfEdges[2], halfEdges[0], up); Vector3f.Cross(ref halfEdges[2], halfEdges[0], up);
RecastVectors.Normalize(ref halfEdges[2]); Vector3f.Normalize(ref halfEdges[2]);
halfEdges[0].x *= extent.x; halfEdges[0].x *= extent.x;
halfEdges[0].y *= extent.x; halfEdges[0].y *= extent.x;
halfEdges[0].z *= extent.x; halfEdges[0].z *= extent.x;

View File

@ -73,12 +73,12 @@ public class DemoInputGeomProvider : IInputGeomProvider
CalculateNormals(); CalculateNormals();
bmin = Vector3f.Zero; bmin = Vector3f.Zero;
bmax = Vector3f.Zero; bmax = Vector3f.Zero;
RecastVectors.Copy(ref bmin, vertices, 0); Vector3f.Copy(ref bmin, vertices, 0);
RecastVectors.Copy(ref bmax, vertices, 0); Vector3f.Copy(ref bmax, vertices, 0);
for (int i = 1; i < vertices.Length / 3; i++) for (int i = 1; i < vertices.Length / 3; i++)
{ {
RecastVectors.Min(ref bmin, vertices, i * 3); Vector3f.Min(ref bmin, vertices, i * 3);
RecastVectors.Max(ref bmax, vertices, i * 3); Vector3f.Max(ref bmax, vertices, i * 3);
} }
_mesh = new TriMesh(vertices, faces); _mesh = new TriMesh(vertices, faces);

View File

@ -56,9 +56,9 @@ public class BoxGizmo : IColliderGizmo
float[] vertices = new float[8 * 3]; float[] vertices = new float[8 * 3];
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
{ {
vertices[i * 3 + 0] = RecastVectors.Dot(VERTS[i], trX) + center.x; vertices[i * 3 + 0] = Vector3f.Dot(VERTS[i], trX) + center.x;
vertices[i * 3 + 1] = RecastVectors.Dot(VERTS[i], trY) + center.y; vertices[i * 3 + 1] = Vector3f.Dot(VERTS[i], trY) + center.y;
vertices[i * 3 + 2] = RecastVectors.Dot(VERTS[i], trZ) + center.z; vertices[i * 3 + 2] = Vector3f.Dot(VERTS[i], trZ) + center.z;
} }
debugDraw.Begin(DebugDrawPrimitives.TRIS); debugDraw.Begin(DebugDrawPrimitives.TRIS);

View File

@ -1,6 +1,6 @@
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Recast.Demo.Draw; using DotRecast.Recast.Demo.Draw;
using static DotRecast.Recast.RecastVectors;
using static DotRecast.Core.RcMath; using static DotRecast.Core.RcMath;
using static DotRecast.Recast.Demo.Tools.Gizmos.GizmoHelper; 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 axis = Vector3f.Of(end.x - start.x, end.y - start.y, end.z - start.z);
Vector3f[] normals = new Vector3f[3]; Vector3f[] normals = new Vector3f[3];
normals[1] = Vector3f.Of(end.x - start.x, end.y - start.y, end.z - start.z); 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[0] = GetSideVector(axis);
normals[2] = Vector3f.Zero; normals[2] = Vector3f.Zero;
Cross(ref normals[2], normals[0], normals[1]); Vector3f.Cross(ref normals[2], normals[0], normals[1]);
Normalize(ref normals[2]); Vector3f.Normalize(ref normals[2]);
triangles = GenerateSphericalTriangles(); triangles = GenerateSphericalTriangles();
var trX = Vector3f.Of(normals[0].x, normals[1].x, normals[2].x); 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); 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.x = vertices[i] - center[0];
v.y = vertices[i + 1] - center[1]; v.y = vertices[i + 1] - center[1];
v.z = vertices[i + 2] - center[2]; 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); 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(); Vector3f forward = new Vector3f();
Cross(ref forward, side, axis); Vector3f.Cross(ref forward, side, axis);
Cross(ref side, axis, forward); Vector3f.Cross(ref side, axis, forward);
Normalize(ref side); Vector3f.Normalize(ref side);
return side; return side;
} }

View File

@ -1,6 +1,6 @@
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Recast.Demo.Draw; using DotRecast.Recast.Demo.Draw;
using static DotRecast.Recast.RecastVectors;
using static DotRecast.Core.RcMath; using static DotRecast.Core.RcMath;
using static DotRecast.Recast.Demo.Tools.Gizmos.GizmoHelper; 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 axis = Vector3f.Of(end.x - start.x, end.y - start.y, end.z - start.z);
Vector3f[] normals = new Vector3f[3]; Vector3f[] normals = new Vector3f[3];
normals[1] = Vector3f.Of(end.x - start.x, end.y - start.y, end.z - start.z); 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[0] = GetSideVector(axis);
normals[2] = Vector3f.Zero; normals[2] = Vector3f.Zero;
Cross(ref normals[2], normals[0], normals[1]); Vector3f.Cross(ref normals[2], normals[0], normals[1]);
Normalize(ref normals[2]); Vector3f.Normalize(ref normals[2]);
triangles = GenerateCylindricalTriangles(); triangles = GenerateCylindricalTriangles();
Vector3f trX = Vector3f.Of(normals[0].x, normals[1].x, normals[2].x); 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); 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.x = vertices[i] - center.x;
v.y = vertices[i + 1] - center.y; v.y = vertices[i + 1] - center.y;
v.z = vertices[i + 2] - center.z; 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); 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(); Vector3f forward = new Vector3f();
Cross(ref forward, side, axis); Vector3f.Cross(ref forward, side, axis);
Cross(ref side, axis, forward); Vector3f.Cross(ref side, axis, forward);
Normalize(ref side); Vector3f.Normalize(ref side);
return side; return side;
} }

View File

@ -188,7 +188,7 @@ public class GizmoHelper
normal.x = e0.y * e1.z - e0.z * e1.y; normal.x = e0.y * e1.z - e0.z * e1.y;
normal.y = e0.z * e1.x - e0.x * e1.z; normal.y = e0.z * e1.x - e0.x * e1.z;
normal.z = e0.x * e1.y - e0.y * e1.x; 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); 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 col = DebugDraw.DuLerpCol(DebugDraw.DuRGBA(32, 32, 0, 160), DebugDraw.DuRGBA(220, 220, 0, 160),
(int)(127 * (1 + c))); (int)(127 * (1 + c)));

View File

@ -70,12 +70,12 @@ namespace DotRecast.Recast.Geom
CalculateNormals(); CalculateNormals();
bmin = Vector3f.Zero; bmin = Vector3f.Zero;
bmax = Vector3f.Zero; bmax = Vector3f.Zero;
RecastVectors.Copy(ref bmin, vertices, 0); Vector3f.Copy(ref bmin, vertices, 0);
RecastVectors.Copy(ref bmax, vertices, 0); Vector3f.Copy(ref bmax, vertices, 0);
for (int i = 1; i < vertices.Length / 3; i++) for (int i = 1; i < vertices.Length / 3; i++)
{ {
RecastVectors.Min(ref bmin, vertices, i * 3); Vector3f.Min(ref bmin, vertices, i * 3);
RecastVectors.Max(ref bmax, vertices, i * 3); Vector3f.Max(ref bmax, vertices, i * 3);
} }
_mesh = new TriMesh(vertices, faces); _mesh = new TriMesh(vertices, faces);

View File

@ -33,12 +33,12 @@ namespace DotRecast.Recast.Geom
{ {
bmin = Vector3f.Zero; bmin = Vector3f.Zero;
bmax = Vector3f.Zero; bmax = Vector3f.Zero;
RecastVectors.Copy(ref bmin, vertices, 0); Vector3f.Copy(ref bmin, vertices, 0);
RecastVectors.Copy(ref bmax, vertices, 0); Vector3f.Copy(ref bmax, vertices, 0);
for (int i = 1; i < vertices.Length / 3; i++) for (int i = 1; i < vertices.Length / 3; i++)
{ {
RecastVectors.Min(ref bmin, vertices, i * 3); Vector3f.Min(ref bmin, vertices, i * 3);
RecastVectors.Max(ref bmax, vertices, i * 3); Vector3f.Max(ref bmax, vertices, i * 3);
} }
_mesh = new TriMesh(vertices, faces); _mesh = new TriMesh(vertices, faces);

View File

@ -101,20 +101,20 @@ namespace DotRecast.Recast
{ {
Vector3f e0 = new Vector3f(); Vector3f e0 = new Vector3f();
Vector3f e1 = new Vector3f(); Vector3f e1 = new Vector3f();
RecastVectors.Sub(ref e0, verts, v1 * 3, v0 * 3); Vector3f.Sub(ref e0, verts, v1 * 3, v0 * 3);
RecastVectors.Sub(ref e1, verts, v2 * 3, v0 * 3); Vector3f.Sub(ref e1, verts, v2 * 3, v0 * 3);
RecastVectors.Cross(norm, e0, e1); Vector3f.Cross(norm, e0, e1);
RecastVectors.Normalize(norm); Vector3f.Normalize(norm);
} }
static void CalcTriNormal(float[] verts, int v0, int v1, int v2, ref Vector3f norm) static void CalcTriNormal(float[] verts, int v0, int v1, int v2, ref Vector3f norm)
{ {
Vector3f e0 = new Vector3f(); Vector3f e0 = new Vector3f();
Vector3f e1 = new Vector3f(); Vector3f e1 = new Vector3f();
RecastVectors.Sub(ref e0, verts, v1 * 3, v0 * 3); Vector3f.Sub(ref e0, verts, v1 * 3, v0 * 3);
RecastVectors.Sub(ref e1, verts, v2 * 3, v0 * 3); Vector3f.Sub(ref e1, verts, v2 * 3, v0 * 3);
RecastVectors.Cross(ref norm, e0, e1); Vector3f.Cross(ref norm, e0, e1);
RecastVectors.Normalize(ref norm); Vector3f.Normalize(ref norm);
} }

View File

@ -364,12 +364,12 @@ namespace DotRecast.Recast
Vector3f bmin = new Vector3f(); Vector3f bmin = new Vector3f();
Vector3f bmax = new Vector3f(); Vector3f bmax = new Vector3f();
RecastVectors.Copy(ref bmin, verts, 0); Vector3f.Copy(ref bmin, verts, 0);
RecastVectors.Copy(ref bmax, verts, 0); Vector3f.Copy(ref bmax, verts, 0);
for (int i = 3; i < verts.Length; i += 3) for (int i = 3; i < verts.Length; i += 3)
{ {
RecastVectors.Min(ref bmin, verts, i); Vector3f.Min(ref bmin, verts, i);
RecastVectors.Max(ref bmax, verts, i); Vector3f.Max(ref bmax, verts, i);
} }
bmin.y = hmin; bmin.y = hmin;

View File

@ -22,8 +22,6 @@ using DotRecast.Core;
namespace DotRecast.Recast namespace DotRecast.Recast
{ {
using static RecastVectors;
public class RecastBuilderConfig public class RecastBuilderConfig
{ {
public readonly RecastConfig cfg; public readonly RecastConfig cfg;

View File

@ -17,10 +17,9 @@ freely, subject to the following restrictions:
*/ */
using System; using System;
using static DotRecast.Core.RcMath; using static DotRecast.Core.RcMath;
using static DotRecast.Recast.RecastConstants; using static DotRecast.Recast.RecastConstants;
using static DotRecast.Recast.RecastVectors;
namespace DotRecast.Recast namespace DotRecast.Recast
{ {

View File

@ -20,7 +20,7 @@ using System;
using DotRecast.Core; using DotRecast.Core;
using static DotRecast.Core.RcMath; using static DotRecast.Core.RcMath;
using static DotRecast.Recast.RecastConstants; using static DotRecast.Recast.RecastConstants;
using static DotRecast.Recast.RecastVectors;
namespace DotRecast.Recast namespace DotRecast.Recast
{ {
@ -84,9 +84,9 @@ namespace DotRecast.Recast
Vector3f.Of(halfEdges[1].x, halfEdges[1].y, halfEdges[1].z), Vector3f.Of(halfEdges[1].x, halfEdges[1].y, halfEdges[1].z),
Vector3f.Of(halfEdges[2].x, halfEdges[2].y, halfEdges[2].z), Vector3f.Of(halfEdges[2].x, halfEdges[2].y, halfEdges[2].z),
}; };
Normalize(ref normals[0]); Vector3f.Normalize(ref normals[0]);
Normalize(ref normals[1]); Vector3f.Normalize(ref normals[1]);
Normalize(ref normals[2]); Vector3f.Normalize(ref normals[2]);
float[] vertices = new float[8 * 3]; float[] vertices = new float[8 * 3];
float[] bounds = new float[] 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) 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]; 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[] rectangleOnStartPlane = new Vector3f[4];
Vector3f[] rectangleOnEndPlane = new Vector3f[4]; Vector3f[] rectangleOnEndPlane = new Vector3f[4];
float ds = Dot(axis, start); float ds = Vector3f.Dot(axis, start);
float de = Dot(axis, end); float de = Vector3f.Dot(axis, end);
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
float x = rectangle[(i + 1) & 2]; float x = rectangle[(i + 1) & 2];
float z = rectangle[(i & 2) + 1]; float z = rectangle[(i & 2) + 1];
Vector3f a = Vector3f.Of(x, rectangle[4], z); 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; float t = (ds - dotAxisA) / axis.y;
rectangleOnStartPlane[i].x = x; rectangleOnStartPlane[i].x = x;
rectangleOnStartPlane[i].y = rectangle[4] + t; rectangleOnStartPlane[i].y = rectangle[4] + t;
@ -348,9 +348,9 @@ namespace DotRecast.Recast
rectangleOnPlane[j].y - rectangleOnPlane[i].y, rectangleOnPlane[j].y - rectangleOnPlane[i].y,
rectangleOnPlane[j].z - rectangleOnPlane[i].z rectangleOnPlane[j].z - rectangleOnPlane[i].z
); );
float dl = Dot(d, d); float dl = Vector3f.Dot(d, d);
float b = Dot(m, d) / dl; float b = Vector3f.Dot(m, d) / dl;
float c = (Dot(m, m) - radiusSqr) / dl; float c = (Vector3f.Dot(m, m) - radiusSqr) / dl;
float discr = b * b - c; float discr = b * b - c;
if (discr > EPSILON) if (discr > EPSILON)
{ {
@ -429,10 +429,10 @@ namespace DotRecast.Recast
Vector3f d = axis; Vector3f d = axis;
Vector3f m = Vector3f.Of(point.x - start.x, point.y - start.y, point.z - start.z); Vector3f m = Vector3f.Of(point.x - start.x, point.y - start.y, point.z - start.z);
// float[] n = { 0, 1, 0 }; // float[] n = { 0, 1, 0 };
float md = Dot(m, d); float md = Vector3f.Dot(m, d);
// float nd = Dot(n, d); // float nd = Dot(n, d);
float nd = axis.y; float nd = axis.y;
float dd = Dot(d, d); float dd = Vector3f.Dot(d, d);
// float nn = Dot(n, n); // float nn = Dot(n, n);
float nn = 1; float nn = 1;
@ -440,7 +440,7 @@ namespace DotRecast.Recast
float mn = m.y; float mn = m.y;
// float a = dd * nn - nd * nd; // float a = dd * nn - nd * nd;
float a = dd - 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; float c = dd * k - md * md;
if (Math.Abs(a) < EPSILON) if (Math.Abs(a) < EPSILON)
{ {
@ -534,7 +534,7 @@ namespace DotRecast.Recast
{ {
if (Math.Abs(planes[j][1]) > EPSILON) 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 t = (planes[j][3] - dotNormalPoint) / planes[j][1];
float y = point.y + t; float y = point.y + t;
bool valid = true; bool valid = true;
@ -744,15 +744,15 @@ namespace DotRecast.Recast
private static float? RayTriangleIntersection(Vector3f point, int plane, float[][] planes) 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[] 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) if (u < 0.0f || u > 1.0f)
{ {
return null; 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) if (v < 0.0f)
{ {
return null; return null;

View File

@ -26,7 +26,7 @@ namespace DotRecast.Recast
{ {
using static RecastCommon; using static RecastCommon;
using static RecastConstants; using static RecastConstants;
using static RecastVectors;
using static RecastRegion; using static RecastRegion;
public class RecastLayers public class RecastLayers

View File

@ -1234,8 +1234,8 @@ namespace DotRecast.Recast
int maxVertsPerMesh = 0; int maxVertsPerMesh = 0;
for (int i = 0; i < nmeshes; ++i) for (int i = 0; i < nmeshes; ++i)
{ {
RecastVectors.Min(ref mesh.bmin, meshes[i].bmin); Vector3f.Min(ref mesh.bmin, meshes[i].bmin);
RecastVectors.Max(ref mesh.bmax, meshes[i].bmax); Vector3f.Max(ref mesh.bmax, meshes[i].bmax);
maxVertsPerMesh = Math.Max(maxVertsPerMesh, meshes[i].nverts); maxVertsPerMesh = Math.Max(maxVertsPerMesh, meshes[i].nverts);
maxVerts += meshes[i].nverts; maxVerts += meshes[i].nverts;
maxPolys += meshes[i].npolys; maxPolys += meshes[i].npolys;

View File

@ -161,8 +161,8 @@ namespace DotRecast.Recast
Vector3f v1 = new Vector3f(); Vector3f v1 = new Vector3f();
Vector3f v2 = new Vector3f(); Vector3f v2 = new Vector3f();
Vector3f v3 = new Vector3f(); Vector3f v3 = new Vector3f();
RecastVectors.Sub(ref v2, verts, p2, p1); Vector3f.Sub(ref v2, verts, p2, p1);
RecastVectors.Sub(ref v3, verts, p3, p1); Vector3f.Sub(ref v3, verts, p3, p1);
float cp = Vcross2(v1, v2, v3); float cp = Vcross2(v1, v2, v3);
if (Math.Abs(cp) > EPS) if (Math.Abs(cp) > EPS)
@ -174,11 +174,11 @@ namespace DotRecast.Recast
c.y = 0; c.y = 0;
c.z = (v1Sq * (v3.x - v2.x) + v2Sq * (v1.x - v3.x) + v3Sq * (v2.x - v1.x)) / (2 * cp); c.z = (v1Sq * (v3.x - v2.x) + v2Sq * (v1.x - v3.x) + v3Sq * (v2.x - v1.x)) / (2 * cp);
r.Exchange(Vdist2(c, v1)); r.Exchange(Vdist2(c, v1));
RecastVectors.Add(ref c, c, verts, p1); Vector3f.Add(ref c, c, verts, p1);
return true; return true;
} }
RecastVectors.Copy(ref c, verts, p1); Vector3f.Copy(ref c, verts, p1);
r.Exchange(0f); r.Exchange(0f);
return false; return false;
} }
@ -188,9 +188,9 @@ namespace DotRecast.Recast
Vector3f v0 = new Vector3f(); Vector3f v0 = new Vector3f();
Vector3f v1 = new Vector3f(); Vector3f v1 = new Vector3f();
Vector3f v2 = new Vector3f(); Vector3f v2 = new Vector3f();
RecastVectors.Sub(ref v0, verts, c, a); Vector3f.Sub(ref v0, verts, c, a);
RecastVectors.Sub(ref v1, verts, b, a); Vector3f.Sub(ref v1, verts, b, a);
RecastVectors.Sub(ref v2, p, verts, a); Vector3f.Sub(ref v2, p, verts, a);
float dot00 = Vdot2(v0, v0); float dot00 = Vdot2(v0, v0);
float dot01 = Vdot2(v0, v1); float dot01 = Vdot2(v0, v1);
@ -845,7 +845,7 @@ namespace DotRecast.Recast
for (int i = 0; i < nin; ++i) 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(); tris.Clear();
@ -964,7 +964,7 @@ namespace DotRecast.Recast
{ {
for (int k = nidx - 2; k > 0; --k) 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; hull[nhull++] = nverts;
nverts++; nverts++;
} }
@ -973,7 +973,7 @@ namespace DotRecast.Recast
{ {
for (int k = 1; k < nidx - 1; ++k) 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; hull[nhull++] = nverts;
nverts++; nverts++;
} }
@ -1005,12 +1005,12 @@ namespace DotRecast.Recast
// Create sample locations in a grid. // Create sample locations in a grid.
Vector3f bmin = new Vector3f(); Vector3f bmin = new Vector3f();
Vector3f bmax = new Vector3f(); Vector3f bmax = new Vector3f();
RecastVectors.Copy(ref bmin, @in, 0); Vector3f.Copy(ref bmin, @in, 0);
RecastVectors.Copy(ref bmax, @in, 0); Vector3f.Copy(ref bmax, @in, 0);
for (int i = 1; i < nin; ++i) for (int i = 1; i < nin; ++i)
{ {
RecastVectors.Min(ref bmin, @in, i * 3); Vector3f.Min(ref bmin, @in, i * 3);
RecastVectors.Max(ref bmax, @in, i * 3); Vector3f.Max(ref bmax, @in, i * 3);
} }
int x0 = (int)Math.Floor(bmin.x / sampleDist); int x0 = (int)Math.Floor(bmin.x / sampleDist);
@ -1091,7 +1091,7 @@ namespace DotRecast.Recast
// Mark sample as added. // Mark sample as added.
samples[besti * 4 + 3] = 1; samples[besti * 4 + 3] = 1;
// Add the new sample point. // Add the new sample point.
RecastVectors.Copy(verts, nverts * 3, bestpt, 0); Vector3f.Copy(verts, nverts * 3, bestpt, 0);
nverts++; nverts++;
// Create new triangulation. // Create new triangulation.
@ -1666,7 +1666,7 @@ namespace DotRecast.Recast
for (int k = 0; k < dm.nverts; ++k) 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++; mesh.nverts++;
} }

View File

@ -191,19 +191,19 @@ namespace DotRecast.Recast
+ (inVerts[inVertsOffset + i * 3 + 1] - inVerts[inVertsOffset + j * 3 + 1]) * s; + (inVerts[inVertsOffset + i * 3 + 1] - inVerts[inVertsOffset + j * 3 + 1]) * s;
inVerts[outVerts1 + m * 3 + 2] = inVerts[inVertsOffset + j * 3 + 2] inVerts[outVerts1 + m * 3 + 2] = inVerts[inVertsOffset + j * 3 + 2]
+ (inVerts[inVertsOffset + i * 3 + 2] - inVerts[inVertsOffset + j * 3 + 2]) * s; + (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++; m++;
n++; n++;
// add the i'th point to the right polygon. Do NOT add points that are on the dividing line // 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 // since these were already added above
if (d[i] > 0) 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++; m++;
} }
else if (d[i] < 0) 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++; 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 // add the i'th point to the right polygon. Addition is done even for points on the dividing line
if (d[i] >= 0) 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++; m++;
if (d[i] != 0) if (d[i] != 0)
continue; continue;
} }
RecastVectors.Copy(inVerts, outVerts2 + n * 3, inVerts, inVertsOffset + i * 3); Vector3f.Copy(inVerts, outVerts2 + n * 3, inVerts, inVertsOffset + i * 3);
n++; n++;
} }
} }
@ -263,12 +263,12 @@ namespace DotRecast.Recast
float by = hfBBMax.y - hfBBMin.y; float by = hfBBMax.y - hfBBMin.y;
// Calculate the bounding box of the triangle. // Calculate the bounding box of the triangle.
RecastVectors.Copy(ref tmin, verts, v0 * 3); Vector3f.Copy(ref tmin, verts, v0 * 3);
RecastVectors.Copy(ref tmax, verts, v0 * 3); Vector3f.Copy(ref tmax, verts, v0 * 3);
RecastVectors.Min(ref tmin, verts, v1 * 3); Vector3f.Min(ref tmin, verts, v1 * 3);
RecastVectors.Min(ref tmin, verts, v2 * 3); Vector3f.Min(ref tmin, verts, v2 * 3);
RecastVectors.Max(ref tmax, verts, v1 * 3); Vector3f.Max(ref tmax, verts, v1 * 3);
RecastVectors.Max(ref tmax, verts, v2 * 3); Vector3f.Max(ref tmax, verts, v2 * 3);
// If the triangle does not touch the bbox of the heightfield, skip the triagle. // If the triangle does not touch the bbox of the heightfield, skip the triagle.
if (!OverlapBounds(hfBBMin, hfBBMax, tmin, tmax)) if (!OverlapBounds(hfBBMin, hfBBMax, tmin, tmax))
@ -291,9 +291,9 @@ namespace DotRecast.Recast
int p1 = inRow + 7 * 3; int p1 = inRow + 7 * 3;
int p2 = p1 + 7 * 3; int p2 = p1 + 7 * 3;
RecastVectors.Copy(buf, 0, verts, v0 * 3); Vector3f.Copy(buf, 0, verts, v0 * 3);
RecastVectors.Copy(buf, 3, verts, v1 * 3); Vector3f.Copy(buf, 3, verts, v1 * 3);
RecastVectors.Copy(buf, 6, verts, v2 * 3); Vector3f.Copy(buf, 6, verts, v2 * 3);
int nvRow, nvIn = 3; int nvRow, nvIn = 3;
for (int z = z0; z <= z1; ++z) for (int z = z0; z <= z1; ++z)

View File

@ -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;
}
}
}

View File

@ -20,7 +20,7 @@ using System.Collections.Generic;
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Recast; using DotRecast.Recast;
using DotRecast.Recast.Geom; using DotRecast.Recast.Geom;
using static DotRecast.Recast.RecastVectors;
namespace DotRecast.Detour.Test; namespace DotRecast.Detour.Test;

View File

@ -23,7 +23,7 @@ using DotRecast.Detour.TileCache.Io.Compress;
using DotRecast.Recast.Geom; using DotRecast.Recast.Geom;
using NUnit.Framework; using NUnit.Framework;
using static DotRecast.Core.RcMath; using static DotRecast.Core.RcMath;
using static DotRecast.Recast.RecastVectors;
namespace DotRecast.Detour.TileCache.Test; namespace DotRecast.Detour.TileCache.Test;