refactor: RcVec3f.Cross now corresponds to System.Numerics.Vector3.Normalize in the switching build

This commit is contained in:
ikpil 2023-10-22 12:44:50 +09:00
parent 3861b0a2c1
commit cb4a7b37d7
8 changed files with 44 additions and 41 deletions

View File

@ -259,16 +259,6 @@ namespace DotRecast.Core.Numerics
return right * left; return right * left;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RcVec3f Cross(RcVec3f v1, RcVec3f v2)
{
return new RcVec3f(
(v1.Y * v2.Z) - (v1.Z * v2.Y),
(v1.Z * v2.X) - (v1.X * v2.Z),
(v1.X * v2.Y) - (v1.Y * v2.X)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RcVec3f Lerp(RcVec3f v1, RcVec3f v2, float t) public static RcVec3f Lerp(RcVec3f v1, RcVec3f v2, float t)
{ {
@ -505,21 +495,14 @@ namespace DotRecast.Core.Numerics
e0.Z = i.Z - verts[j + 2]; e0.Z = i.Z - verts[j + 2];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Cross(float[] dest, float[] v1, float[] v2) public static RcVec3f Cross(RcVec3f v1, RcVec3f v2)
{ {
dest[0] = v1[1] * v2[2] - v1[2] * v2[1]; return new RcVec3f(
dest[1] = v1[2] * v2[0] - v1[0] * v2[2]; (v1.Y * v2.Z) - (v1.Z * v2.Y),
dest[2] = v1[0] * v2[1] - v1[1] * v2[0]; (v1.Z * v2.X) - (v1.X * v2.Z),
} (v1.X * v2.Y) - (v1.Y * v2.X)
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Cross(ref RcVec3f dest, RcVec3f v1, RcVec3f 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;
} }
/// Normalizes the vector. /// Normalizes the vector.

View File

@ -1,10 +1,9 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using DotRecast.Core.Numerics;
namespace DotRecast.Core.Numerics namespace DotRecast.Core.Numerics
{ {
public static class RcVecExtensions public static class RcVecUtils
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Get(this RcVec2f v, int i) public static float Get(this RcVec2f v, int i)
@ -56,5 +55,13 @@ namespace DotRecast.Core.Numerics
return @this.X * v[vi] + return @this.X * v[vi] +
@this.Z * v[vi + 2]; @this.Z * v[vi + 2];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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];
}
} }
} }

View File

@ -76,11 +76,11 @@ namespace DotRecast.Detour.Dynamic.Colliders
new RcVec3f(up.X, up.Y, up.Z), new RcVec3f(up.X, up.Y, up.Z),
RcVec3f.Zero RcVec3f.Zero
}; };
halfEdges[1] = RcVec3f.Normalize(halfEdges[1]); halfEdges[1] = RcVec3f.Normalize(halfEdges[1]);
RcVec3f.Cross(ref halfEdges[0], up, forward); halfEdges[0] = RcVec3f.Cross(up, forward);
halfEdges[0] = RcVec3f.Normalize(halfEdges[0]); halfEdges[0] = RcVec3f.Normalize(halfEdges[0]);
RcVec3f.Cross(ref halfEdges[2], halfEdges[0], up); halfEdges[2] = RcVec3f.Cross(halfEdges[0], up);
halfEdges[2] = RcVec3f.Normalize(halfEdges[2]); halfEdges[2] = RcVec3f.Normalize(halfEdges[2]);
halfEdges[0].X *= extent.X; halfEdges[0].X *= extent.X;
halfEdges[0].Y *= extent.X; halfEdges[0].Y *= extent.X;

View File

@ -1,6 +1,5 @@
using System; using System;
using DotRecast.Core.Numerics; using DotRecast.Core.Numerics;
using static DotRecast.Recast.Toolset.Gizmos.RcGizmoHelper; using static DotRecast.Recast.Toolset.Gizmos.RcGizmoHelper;
namespace DotRecast.Recast.Toolset.Gizmos namespace DotRecast.Recast.Toolset.Gizmos
@ -25,7 +24,7 @@ namespace DotRecast.Recast.Toolset.Gizmos
normals[1] = RcVec3f.Normalize(normals[1]); normals[1] = RcVec3f.Normalize(normals[1]);
normals[0] = GetSideVector(axis); normals[0] = GetSideVector(axis);
normals[2] = RcVec3f.Zero; normals[2] = RcVec3f.Zero;
RcVec3f.Cross(ref normals[2], normals[0], normals[1]); normals[2] = RcVec3f.Cross(normals[0], normals[1]);
normals[2] = RcVec3f.Normalize(normals[2]); normals[2] = RcVec3f.Normalize(normals[2]);
triangles = GenerateSphericalTriangles(); triangles = GenerateSphericalTriangles();
var trX = new RcVec3f(normals[0].X, normals[1].X, normals[2].X); var trX = new RcVec3f(normals[0].X, normals[1].X, normals[2].X);
@ -55,15 +54,14 @@ namespace DotRecast.Recast.Toolset.Gizmos
private RcVec3f GetSideVector(RcVec3f axis) private RcVec3f GetSideVector(RcVec3f axis)
{ {
RcVec3f side = new RcVec3f(1, 0, 0); var side = new RcVec3f(1, 0, 0);
if (axis.X > 0.8) if (axis.X > 0.8)
{ {
side = new RcVec3f(0, 0, 1); side = new RcVec3f(0, 0, 1);
} }
RcVec3f forward = new RcVec3f(); var forward = RcVec3f.Cross(side, axis);
RcVec3f.Cross(ref forward, side, axis); side = RcVec3f.Cross(axis, forward);
RcVec3f.Cross(ref side, axis, forward);
side = RcVec3f.Normalize(side); side = RcVec3f.Normalize(side);
return side; return side;
} }

View File

@ -1,6 +1,5 @@
using System; using System;
using DotRecast.Core.Numerics; using DotRecast.Core.Numerics;
using static DotRecast.Recast.Toolset.Gizmos.RcGizmoHelper; using static DotRecast.Recast.Toolset.Gizmos.RcGizmoHelper;
@ -25,7 +24,7 @@ namespace DotRecast.Recast.Toolset.Gizmos
normals[1] = RcVec3f.Normalize(normals[1]); normals[1] = RcVec3f.Normalize(normals[1]);
normals[0] = GetSideVector(axis); normals[0] = GetSideVector(axis);
normals[2] = RcVec3f.Zero; normals[2] = RcVec3f.Zero;
RcVec3f.Cross(ref normals[2], normals[0], normals[1]); normals[2] = RcVec3f.Cross(normals[0], normals[1]);
normals[2] = RcVec3f.Normalize(normals[2]); normals[2] = RcVec3f.Normalize(normals[2]);
triangles = GenerateCylindricalTriangles(); triangles = GenerateCylindricalTriangles();
RcVec3f trX = new RcVec3f(normals[0].X, normals[1].X, normals[2].X); RcVec3f trX = new RcVec3f(normals[0].X, normals[1].X, normals[2].X);
@ -67,9 +66,8 @@ namespace DotRecast.Recast.Toolset.Gizmos
side = new RcVec3f(0, 0, 1); side = new RcVec3f(0, 0, 1);
} }
RcVec3f forward = new RcVec3f(); var forward = RcVec3f.Cross(side, axis);
RcVec3f.Cross(ref forward, side, axis); side = RcVec3f.Cross(axis, forward);
RcVec3f.Cross(ref side, axis, forward);
side = RcVec3f.Normalize(side); side = RcVec3f.Normalize(side);
return side; return side;
} }

View File

@ -142,7 +142,7 @@ namespace DotRecast.Recast
RcVec3f e1 = new RcVec3f(); RcVec3f e1 = new RcVec3f();
RcVec3f.Sub(ref e0, verts, v1 * 3, v0 * 3); RcVec3f.Sub(ref e0, verts, v1 * 3, v0 * 3);
RcVec3f.Sub(ref e1, verts, v2 * 3, v0 * 3); RcVec3f.Sub(ref e1, verts, v2 * 3, v0 * 3);
RcVec3f.Cross(ref norm, e0, e1); norm = RcVec3f.Cross(e0, e1);
norm = RcVec3f.Normalize(norm); norm = RcVec3f.Normalize(norm);
} }

View File

@ -176,7 +176,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)
{ {
RcVec3f.Cross(planes[p], v1, v2); RcVecUtils.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];
} }

View File

@ -75,4 +75,21 @@ public class Vector3Tests
Assert.That(v2.Y, Is.EqualTo(v22.Y).Within(0.000001d)); Assert.That(v2.Y, Is.EqualTo(v22.Y).Within(0.000001d));
Assert.That(v2.Z, Is.EqualTo(v22.Z).Within(0.000001d)); Assert.That(v2.Z, Is.EqualTo(v22.Z).Within(0.000001d));
} }
[Test]
[Repeat(10000)]
public void TestVectorCross()
{
var v1 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle());
var v2 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle());
var v3 = Vector3.Cross(v1, v2);
var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
var v22 = new RcVec3f(v2.X, v2.Y, v2.Z);
var v33 = RcVec3f.Cross(v11, v22);
Assert.That(v3.X, Is.EqualTo(v33.X));
Assert.That(v3.Y, Is.EqualTo(v33.Y));
Assert.That(v3.Z, Is.EqualTo(v33.Z));
}
} }