Skip to content

Commit

Permalink
refactor: RcVec3f.Cross now corresponds to System.Numerics.Vector3.No…
Browse files Browse the repository at this point in the history
…rmalize in the switching build
  • Loading branch information
ikpil committed Oct 22, 2023
1 parent 3861b0a commit cb4a7b3
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 41 deletions.
29 changes: 6 additions & 23 deletions src/DotRecast.Core/Numerics/RcVec3f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,6 @@ public override string ToString()
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)]
public static RcVec3f Lerp(RcVec3f v1, RcVec3f v2, float t)
{
Expand Down Expand Up @@ -505,21 +495,14 @@ public static void Sub(ref RcVec3f e0, RcVec3f i, float[] verts, int j)
e0.Z = i.Z - verts[j + 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];
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Cross(ref RcVec3f dest, RcVec3f v1, RcVec3f v2)
public static RcVec3f Cross(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;
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)
);
}

/// Normalizes the vector.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Runtime.CompilerServices;
using DotRecast.Core.Numerics;

namespace DotRecast.Core.Numerics
{
public static class RcVecExtensions
public static class RcVecUtils
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Get(this RcVec2f v, int i)
Expand Down Expand Up @@ -56,5 +55,13 @@ public static float Dot2D(this RcVec3f @this, float[] v, int vi)
return @this.X * v[vi] +
@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];
}
}
}
6 changes: 3 additions & 3 deletions src/DotRecast.Detour.Dynamic/Colliders/DtBoxCollider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ public static RcVec3f[] GetHalfEdges(RcVec3f up, RcVec3f forward, RcVec3f extent
new RcVec3f(up.X, up.Y, up.Z),
RcVec3f.Zero
};

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]);
RcVec3f.Cross(ref halfEdges[2], halfEdges[0], up);
halfEdges[2] = RcVec3f.Cross(halfEdges[0], up);
halfEdges[2] = RcVec3f.Normalize(halfEdges[2]);
halfEdges[0].X *= extent.X;
halfEdges[0].Y *= extent.X;
Expand Down
10 changes: 4 additions & 6 deletions src/DotRecast.Recast.Toolset/Gizmos/RcCapsuleGizmo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using DotRecast.Core.Numerics;

using static DotRecast.Recast.Toolset.Gizmos.RcGizmoHelper;

namespace DotRecast.Recast.Toolset.Gizmos
Expand All @@ -25,7 +24,7 @@ public RcCapsuleGizmo(RcVec3f start, RcVec3f end, float radius)
normals[1] = RcVec3f.Normalize(normals[1]);
normals[0] = GetSideVector(axis);
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]);
triangles = GenerateSphericalTriangles();
var trX = new RcVec3f(normals[0].X, normals[1].X, normals[2].X);
Expand Down Expand Up @@ -55,15 +54,14 @@ public RcCapsuleGizmo(RcVec3f start, RcVec3f end, float radius)

private RcVec3f GetSideVector(RcVec3f axis)
{
RcVec3f side = new RcVec3f(1, 0, 0);
var side = new RcVec3f(1, 0, 0);
if (axis.X > 0.8)
{
side = new RcVec3f(0, 0, 1);
}

RcVec3f forward = new RcVec3f();
RcVec3f.Cross(ref forward, side, axis);
RcVec3f.Cross(ref side, axis, forward);
var forward = RcVec3f.Cross(side, axis);
side = RcVec3f.Cross(axis, forward);
side = RcVec3f.Normalize(side);
return side;
}
Expand Down
8 changes: 3 additions & 5 deletions src/DotRecast.Recast.Toolset/Gizmos/RcCylinderGizmo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using DotRecast.Core.Numerics;

using static DotRecast.Recast.Toolset.Gizmos.RcGizmoHelper;


Expand All @@ -25,7 +24,7 @@ public RcCylinderGizmo(RcVec3f start, RcVec3f end, float radius)
normals[1] = RcVec3f.Normalize(normals[1]);
normals[0] = GetSideVector(axis);
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]);
triangles = GenerateCylindricalTriangles();
RcVec3f trX = new RcVec3f(normals[0].X, normals[1].X, normals[2].X);
Expand Down Expand Up @@ -67,9 +66,8 @@ private RcVec3f GetSideVector(RcVec3f axis)
side = new RcVec3f(0, 0, 1);
}

RcVec3f forward = new RcVec3f();
RcVec3f.Cross(ref forward, side, axis);
RcVec3f.Cross(ref side, axis, forward);
var forward = RcVec3f.Cross(side, axis);
side = RcVec3f.Cross(axis, forward);
side = RcVec3f.Normalize(side);
return side;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Recast/RcCommons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static void CalcTriNormal(float[] verts, int v0, int v1, int v2, ref RcVe
RcVec3f e1 = new RcVec3f();
RcVec3f.Sub(ref e0, verts, v1 * 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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Recast/RcFilledVolumeRasterization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static void RasterizeConvex(RcHeightfield hf, float[] vertices, int[] tri

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

Expand Down
17 changes: 17 additions & 0 deletions test/DotRecast.Core.Test/Vector3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,21 @@ public void TestVectorNormalize()
Assert.That(v2.Y, Is.EqualTo(v22.Y).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));
}
}

0 comments on commit cb4a7b3

Please sign in to comment.