Skip to content

Commit

Permalink
refactor: TestVectorDot
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Oct 24, 2023
1 parent 617eaa1 commit ef8ef94
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
23 changes: 4 additions & 19 deletions src/DotRecast.Core/Numerics/RcVec3f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,28 +288,13 @@ public static float DistanceSquared(RcVec3f value1, RcVec3f value2)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Dot(RcVec3f v1, RcVec3f v2)
public static float Dot(RcVec3f vector1, RcVec3f vector2)
{
return (v1.X * v2.X)
+ (v1.Y * v2.Y)
+ (v1.Z * v2.Z);
return (vector1.X * vector2.X) +
(vector1.Y * vector2.Y) +
(vector1.Z * vector2.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, RcVec3f v2)
{
return v1[0] * v2.X + v1[1] * v2.Y + v1[2] * v2.Z;
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float PerpXZ(RcVec3f a, RcVec3f b)
{
Expand Down
18 changes: 17 additions & 1 deletion src/DotRecast.Core/Numerics/RcVecUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,29 @@ public static void Cross(float[] dest, float[] v1, float[] v2)
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 Copy(float[] @out, int n, float[] @in, int m)
{
@out[n + 0] = @in[m + 0];
@out[n + 1] = @in[m + 1];
@out[n + 2] = @in[m + 2];
}

[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, RcVec3f vector2)
{
return v1[0] * vector2.X +
v1[1] * vector2.Y +
v1[2] * vector2.Z;
}
}
}
8 changes: 4 additions & 4 deletions src/DotRecast.Recast/RcFilledVolumeRasterization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ private static float[] IntersectBox(float[] rectangle, float[] vertices, float[]
{
if (Math.Abs(planes[j][1]) > EPSILON)
{
float dotNormalPoint = RcVec3f.Dot(planes[j], point);
float dotNormalPoint = RcVecUtils.Dot(planes[j], point);
float t = (planes[j][3] - dotNormalPoint) / planes[j][1];
float y = point.Y + t;
bool valid = true;
Expand Down Expand Up @@ -729,15 +729,15 @@ private static bool ZSlabSegmentIntersection(float[] rectangle, float x, float y
private static bool RayTriangleIntersection(RcVec3f point, int plane, float[][] planes, out float y)
{
y = 0.0f;
float t = (planes[plane][3] - RcVec3f.Dot(planes[plane], point)) / planes[plane][1];
float t = (planes[plane][3] - RcVecUtils.Dot(planes[plane], point)) / planes[plane][1];
float[] s = { point.X, point.Y + t, point.Z };
float u = RcVec3f.Dot(s, planes[plane + 1]) - planes[plane + 1][3];
float u = RcVecUtils.Dot(s, planes[plane + 1]) - planes[plane + 1][3];
if (u < 0.0f || u > 1.0f)
{
return false;
}

float v = RcVec3f.Dot(s, planes[plane + 2]) - planes[plane + 2][3];
float v = RcVecUtils.Dot(s, planes[plane + 2]) - planes[plane + 2][3];
if (v < 0.0f)
{
return false;
Expand Down
15 changes: 15 additions & 0 deletions test/DotRecast.Core.Test/Vector3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ public void TestVectorCopyTo()
Assert.That(array1, Is.EqualTo(array11));
}

[Test]
[Repeat(100000)]
public void TestVectorDot()
{
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());
float d3 = Vector3.Dot(v1, v2);

var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
var v22 = new RcVec3f(v2.X, v2.Y, v2.Z);
var d33 = RcVec3f.Dot(v11, v22);

Assert.That(d3, Is.EqualTo(d33));
}

[Test]
[Repeat(100000)]
public void TestVectorDistance()
Expand Down

0 comments on commit ef8ef94

Please sign in to comment.