DotRecastNetSim/test/DotRecast.Core.Test/Vector3Test.cs

190 lines
6.4 KiB
C#
Raw Permalink Normal View History

2024-09-05 16:57:28 +03:00
using System;
using System.Numerics;
using DotRecast.Core.Numerics;
using NUnit.Framework;
namespace DotRecast.Core.Test;
2024-02-04 07:24:09 +03:00
public class Vector3Test
{
[Test]
2023-10-23 16:43:53 +03:00
[Repeat(100000)]
public void TestVectorLength()
{
var v1 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle());
var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
Assert.That(v1.Length(), Is.EqualTo(v11.Length()));
Assert.That(v1.LengthSquared(), Is.EqualTo(v11.LengthSquared()));
}
[Test]
2023-10-23 16:43:53 +03:00
[Repeat(100000)]
public void TestVectorSubtract()
{
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.Subtract(v1, v2);
var v4 = v1 - v2;
Assert.That(v3, Is.EqualTo(v4));
var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
var v22 = new RcVec3f(v2.X, v2.Y, v2.Z);
var v33 = RcVec3f.Subtract(v11, v22);
var v44 = v11 - v22;
Assert.That(v33, Is.EqualTo(v44));
Assert.That(v3.X, Is.EqualTo(v33.X));
Assert.That(v3.Y, Is.EqualTo(v33.Y));
Assert.That(v3.Z, Is.EqualTo(v33.Z));
}
[Test]
2023-10-23 16:43:53 +03:00
[Repeat(100000)]
public void TestVectorAdd()
{
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.Add(v1, v2);
var v4 = v1 + v2;
Assert.That(v3, Is.EqualTo(v4));
var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
var v22 = new RcVec3f(v2.X, v2.Y, v2.Z);
var v33 = RcVec3f.Add(v11, v22);
var v44 = v11 + v22;
Assert.That(v33, Is.EqualTo(v44));
Assert.That(v3.X, Is.EqualTo(v33.X));
Assert.That(v3.Y, Is.EqualTo(v33.Y));
Assert.That(v3.Z, Is.EqualTo(v33.Z));
}
[Test]
2023-10-23 16:43:53 +03:00
[Repeat(100000)]
public void TestVectorNormalize()
{
var v1 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle());
var v2 = Vector3.Normalize(v1);
var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
var v22 = RcVec3f.Normalize(v11);
Assert.That(v2.X, Is.EqualTo(v22.X).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));
}
[Test]
2023-10-23 16:43:53 +03:00
[Repeat(100000)]
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));
}
2023-10-23 16:51:09 +03:00
[Test]
[Repeat(100000)]
public void TestVectorCopyTo()
{
var v1 = new Vector3(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle());
var array1 = new float[3];
2023-10-26 18:03:03 +03:00
var array2 = new float[3];
2023-10-23 16:51:09 +03:00
v1.CopyTo(array1);
2023-10-26 18:03:03 +03:00
v1.CopyTo(array2, 0);
2023-10-23 18:20:42 +03:00
2023-10-23 16:51:09 +03:00
var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
var array11 = new float[3];
2023-10-26 18:03:03 +03:00
var array22 = new float[3];
2023-10-23 16:51:09 +03:00
v11.CopyTo(array11);
2023-10-26 18:03:03 +03:00
v11.CopyTo(array22, 0);
2023-10-23 18:20:42 +03:00
2023-10-23 16:51:09 +03:00
Assert.That(array1, Is.EqualTo(array11));
2023-10-26 18:03:03 +03:00
Assert.That(array2, Is.EqualTo(array22));
2023-10-23 16:51:09 +03:00
}
2023-10-24 17:33:24 +03:00
[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);
2023-10-24 17:33:24 +03:00
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);
2023-10-24 17:33:24 +03:00
Assert.That(d3, Is.EqualTo(d33));
}
2023-10-23 18:20:42 +03:00
[Test]
[Repeat(100000)]
public void TestVectorDistance()
{
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 d3 = Vector3.Distance(v1, v2);
var d4 = Vector3.DistanceSquared(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.Distance(v11, v22);
var d44 = RcVec3f.DistanceSquared(v11, v22);
Assert.That(d3, Is.EqualTo(d33));
Assert.That(d4, Is.EqualTo(d44));
}
[Test]
[Repeat(100000)]
public void TestVectorMinMax()
{
2023-10-25 17:44:01 +03:00
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.Min(v1, v2);
var v4 = Vector3.Max(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.Min(v11, v22);
var v44 = RcVec3f.Max(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));
Assert.That(v4.X, Is.EqualTo(v44.X));
Assert.That(v4.Y, Is.EqualTo(v44.Y));
Assert.That(v4.Z, Is.EqualTo(v44.Z));
}
[Test]
[Repeat(100000)]
2023-10-26 18:03:03 +03:00
public void TestVectorLerp()
{
2023-10-26 18:03:03 +03:00
var amt = Random.Shared.NextSingle();
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.Lerp(v1, v2, amt);
var v11 = new RcVec3f(v1.X, v1.Y, v1.Z);
var v22 = new RcVec3f(v2.X, v2.Y, v2.Z);
var v33 = RcVec3f.Lerp(v11, v22, amt);
Assert.That(v3.X, Is.EqualTo(v33.X));
Assert.That(v3.Y, Is.EqualTo(v33.Y));
Assert.That(v3.Z, Is.EqualTo(v33.Z));
}
}