diff --git a/CHANGELOG.md b/CHANGELOG.md index 74e52b7..3e227e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,8 @@ - X10D.Unity: Added `GameObject.SetLayerRecursively(int)` - X10D.Unity: Added `GameObject.SetParent(GameObject[, bool])` - X10D.Unity: Added `GameObject.SetParent(Transform[, bool])` +- X10D.Unity: Added `System.Numerics.Quaternion.ToUnityQuaternion()` +- X10D.Unity: Added `Quaternion.ToSystemQuaternion()` - X10D.Unity: Added `Random.NextColorArgb()` - X10D.Unity: Added `Random.NextColor32Argb()` - X10D.Unity: Added `Random.NextColorRgb()` @@ -54,6 +56,12 @@ - X10D.Unity: Added `Random.NextUnitVector3()` - X10D.Unity: Added `Transform.LookAt(GameObject[, Vector3])` - X10D.Unity: Added `Transform.SetParent(GameObject[, bool])` +- X10D.Unity: Added `System.Numerics.Vector2.ToUnityVector()` +- X10D.Unity: Added `System.Numerics.Vector3.ToUnityVector()` +- X10D.Unity: Added `System.Numerics.Vector4.ToUnityVector()` +- X10D.Unity: Added `Vector2.ToSystemVector()` +- X10D.Unity: Added `Vector3.ToSystemVector()` +- X10D.Unity: Added `Vector4.ToSystemVector()` - X10D.Unity: Added `Vector2.WithX()` - X10D.Unity: Added `Vector2.WithY()` - X10D.Unity: Added `Vector3.WithX()` diff --git a/X10D.Unity.Tests/Assets/Tests/Numerics/QuaternionTests.cs b/X10D.Unity.Tests/Assets/Tests/Numerics/QuaternionTests.cs new file mode 100644 index 0000000..a486a75 --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Numerics/QuaternionTests.cs @@ -0,0 +1,53 @@ +using System.Collections; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; +using X10D.Core; +using X10D.Unity.Numerics; +using Random = System.Random; + +namespace X10D.Unity.Tests.Numerics +{ + public class QuaternionTests + { + [UnityTest] + public IEnumerator ToSystemQuaternion_ShouldReturnQuaternion_WithEqualComponents() + { + var random = new Random(); + float x = random.NextSingle(); + float y = random.NextSingle(); + float z = random.NextSingle(); + float w = random.NextSingle(); + + var quaternion = new Quaternion(x, y, z, w); + var systemQuaternion = quaternion.ToSystemQuaternion(); + + Assert.AreEqual(quaternion.x, systemQuaternion.X, 1e-6f); + Assert.AreEqual(quaternion.y, systemQuaternion.Y, 1e-6f); + Assert.AreEqual(quaternion.z, systemQuaternion.Z, 1e-6f); + Assert.AreEqual(quaternion.w, systemQuaternion.W, 1e-6f); + + yield break; + } + + [UnityTest] + public IEnumerator ToUnityQuaternion_ShouldReturnQuaternion_WithEqualComponents() + { + var random = new Random(); + float x = random.NextSingle(); + float y = random.NextSingle(); + float z = random.NextSingle(); + float w = random.NextSingle(); + + var quaternion = new System.Numerics.Quaternion(x, y, z, w); + var unityQuaternion = quaternion.ToUnityQuaternion(); + + Assert.AreEqual(quaternion.X, unityQuaternion.x, 1e-6f); + Assert.AreEqual(quaternion.Y, unityQuaternion.y, 1e-6f); + Assert.AreEqual(quaternion.Z, unityQuaternion.z, 1e-6f); + Assert.AreEqual(quaternion.W, unityQuaternion.w, 1e-6f); + + yield break; + } + } +} diff --git a/X10D.Unity.Tests/Assets/Tests/Numerics/QuaternionTests.cs.meta b/X10D.Unity.Tests/Assets/Tests/Numerics/QuaternionTests.cs.meta new file mode 100644 index 0000000..a8117be --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Numerics/QuaternionTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a395fec41c5a4e9d9ffb05324e8159b0 +timeCreated: 1652124913 \ No newline at end of file diff --git a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2Tests.cs b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2Tests.cs index 45c2526..3fc09e2 100644 --- a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2Tests.cs +++ b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2Tests.cs @@ -2,12 +2,48 @@ using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; +using X10D.Core; using X10D.Unity.Numerics; +using Random = System.Random; namespace X10D.Unity.Tests.Numerics { public class Vector2Tests { + [UnityTest] + public IEnumerator ToSystemVector_ShouldReturnVector_WithEqualComponents() + { + var random = new Random(); + float x = random.NextSingle(); + float y = random.NextSingle(); + + var vector = new Vector2(x, y); + var systemVector = vector.ToSystemVector(); + + Assert.AreEqual(vector.magnitude, systemVector.Length(), 1e-6f); + Assert.AreEqual(vector.x, systemVector.X, 1e-6f); + Assert.AreEqual(vector.y, systemVector.Y, 1e-6f); + + yield break; + } + + [UnityTest] + public IEnumerator ToUnityVector_ShouldReturnVector_WithEqualComponents() + { + var random = new Random(); + float x = random.NextSingle(); + float y = random.NextSingle(); + + var vector = new System.Numerics.Vector2(x, y); + var unityVector = vector.ToUnityVector(); + + Assert.AreEqual(vector.Length(), unityVector.magnitude, 1e-6f); + Assert.AreEqual(vector.X, unityVector.x, 1e-6f); + Assert.AreEqual(vector.Y, unityVector.y, 1e-6f); + + yield break; + } + [UnityTest] public IEnumerator WithX_ShouldReturnVectorWithNewX_GivenVector() { diff --git a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector3Tests.cs b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector3Tests.cs index cf7ad8c..f14c7e3 100644 --- a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector3Tests.cs +++ b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector3Tests.cs @@ -2,12 +2,52 @@ using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; +using X10D.Core; using X10D.Unity.Numerics; +using Random = System.Random; namespace X10D.Unity.Tests.Numerics { public class Vector3Tests { + [UnityTest] + public IEnumerator ToSystemVector_ShouldReturnVector_WithEqualComponents() + { + var random = new Random(); + float x = random.NextSingle(); + float y = random.NextSingle(); + float z = random.NextSingle(); + + var vector = new Vector3(x, y, z); + var systemVector = vector.ToSystemVector(); + + Assert.AreEqual(vector.magnitude, systemVector.Length(), 1e-6f); + Assert.AreEqual(vector.x, systemVector.X, 1e-6f); + Assert.AreEqual(vector.y, systemVector.Y, 1e-6f); + Assert.AreEqual(vector.z, systemVector.Z, 1e-6f); + + yield break; + } + + [UnityTest] + public IEnumerator ToUnityVector_ShouldReturnVector_WithEqualComponents() + { + var random = new Random(); + float x = random.NextSingle(); + float y = random.NextSingle(); + float z = random.NextSingle(); + + var vector = new System.Numerics.Vector3(x, y, z); + var unityVector = vector.ToUnityVector(); + + Assert.AreEqual(vector.Length(), unityVector.magnitude, 1e-6f); + Assert.AreEqual(vector.X, unityVector.x, 1e-6f); + Assert.AreEqual(vector.Y, unityVector.y, 1e-6f); + Assert.AreEqual(vector.Z, unityVector.z, 1e-6f); + + yield break; + } + [UnityTest] public IEnumerator WithX_ShouldReturnVectorWithNewX_GivenVector() { diff --git a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector4Tests.cs b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector4Tests.cs index 2e79b3f..d400512 100644 --- a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector4Tests.cs +++ b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector4Tests.cs @@ -2,12 +2,56 @@ using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; +using X10D.Core; using X10D.Unity.Numerics; +using Random = System.Random; namespace X10D.Unity.Tests.Numerics { public class Vector4Tests { + [UnityTest] + public IEnumerator ToSystemVector_ShouldReturnVector_WithEqualComponents() + { + var random = new Random(); + float x = random.NextSingle(); + float y = random.NextSingle(); + float z = random.NextSingle(); + float w = random.NextSingle(); + + var vector = new Vector4(x, y, z, w); + var systemVector = vector.ToSystemVector(); + + Assert.AreEqual(vector.magnitude, systemVector.Length(), 1e-6f); + Assert.AreEqual(vector.x, systemVector.X, 1e-6f); + Assert.AreEqual(vector.y, systemVector.Y, 1e-6f); + Assert.AreEqual(vector.z, systemVector.Z, 1e-6f); + Assert.AreEqual(vector.w, systemVector.W, 1e-6f); + + yield break; + } + + [UnityTest] + public IEnumerator ToUnityVector_ShouldReturnVector_WithEqualComponents() + { + var random = new Random(); + float x = random.NextSingle(); + float y = random.NextSingle(); + float z = random.NextSingle(); + float w = random.NextSingle(); + + var vector = new System.Numerics.Vector4(x, y, z, w); + var unityVector = vector.ToUnityVector(); + + Assert.AreEqual(vector.Length(), unityVector.magnitude, 1e-6f); + Assert.AreEqual(vector.X, unityVector.x, 1e-6f); + Assert.AreEqual(vector.Y, unityVector.y, 1e-6f); + Assert.AreEqual(vector.Z, unityVector.z, 1e-6f); + Assert.AreEqual(vector.W, unityVector.w, 1e-6f); + + yield break; + } + [UnityTest] public IEnumerator WithW_ShouldReturnVectorWithNewW_GivenVector() { @@ -24,7 +68,7 @@ namespace X10D.Unity.Tests.Numerics Assert.AreEqual(new Vector4(1, 0, 0, 1), new Vector4(1, 0, 0, 0).WithW(1)); Assert.AreEqual(new Vector4(0, 1, 0, 1), new Vector4(0, 1, 0, 0).WithW(1)); Assert.AreEqual(new Vector4(0, 0, 1, 1), new Vector4(0, 0, 1, 0).WithW(1)); - + yield break; } @@ -44,7 +88,7 @@ namespace X10D.Unity.Tests.Numerics Assert.AreEqual(new Vector4(1, 0, 0, 0), new Vector4(1, 0, 0, 0).WithX(1)); Assert.AreEqual(new Vector4(1, 1, 0, 0), new Vector4(0, 1, 0, 0).WithX(1)); Assert.AreEqual(new Vector4(1, 0, 1, 0), new Vector4(0, 0, 1, 0).WithX(1)); - + yield break; } @@ -64,7 +108,7 @@ namespace X10D.Unity.Tests.Numerics Assert.AreEqual(new Vector4(1, 1, 0, 0), new Vector4(1, 0, 0, 0).WithY(1)); Assert.AreEqual(new Vector4(0, 1, 0, 0), new Vector4(0, 1, 0, 0).WithY(1)); Assert.AreEqual(new Vector4(0, 1, 1, 0), new Vector4(0, 0, 1, 0).WithY(1)); - + yield break; } @@ -84,7 +128,7 @@ namespace X10D.Unity.Tests.Numerics Assert.AreEqual(new Vector4(1, 0, 1, 0), new Vector4(1, 0, 0, 0).WithZ(1)); Assert.AreEqual(new Vector4(0, 1, 1, 0), new Vector4(0, 1, 0, 0).WithZ(1)); Assert.AreEqual(new Vector4(0, 0, 1, 0), new Vector4(0, 0, 1, 0).WithZ(1)); - + yield break; } } diff --git a/X10D.Unity/src/Numerics/QuaternionExtensions.cs b/X10D.Unity/src/Numerics/QuaternionExtensions.cs new file mode 100644 index 0000000..2c6355e --- /dev/null +++ b/X10D.Unity/src/Numerics/QuaternionExtensions.cs @@ -0,0 +1,35 @@ +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using UnityEngine; + +namespace X10D.Unity.Numerics; + +/// +/// Numeric-extensions for . +/// +public static class QuaternionExtensions +{ + /// + /// Converts the current quaternion to a . + /// + /// The quaternion to convert. + /// The converted quaternion. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static System.Numerics.Quaternion ToSystemQuaternion(this Quaternion quaternion) + { + return new System.Numerics.Quaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w); + } + + /// + /// Converts the current quaternion to a . + /// + /// The quaternion to convert. + /// The converted quaternion. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Quaternion ToUnityQuaternion(this System.Numerics.Quaternion quaternion) + { + return new Quaternion(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W); + } +} diff --git a/X10D.Unity/src/Numerics/Vector2Extensions.cs b/X10D.Unity/src/Numerics/Vector2Extensions.cs index 1f7b466..32ff372 100644 --- a/X10D.Unity/src/Numerics/Vector2Extensions.cs +++ b/X10D.Unity/src/Numerics/Vector2Extensions.cs @@ -9,6 +9,30 @@ namespace X10D.Unity.Numerics; /// public static class Vector2Extensions { + /// + /// Converts the current vector to a . + /// + /// The vector to convert. + /// The converted vector. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static System.Numerics.Vector2 ToSystemVector(this Vector2 vector) + { + return new System.Numerics.Vector2(vector.x, vector.y); + } + + /// + /// Converts the current vector to a . + /// + /// The vector to convert. + /// The converted vector. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 ToUnityVector(this System.Numerics.Vector2 vector) + { + return new Vector2(vector.X, vector.Y); + } + /// /// Returns a vector whose Y component is the same as the specified vector, and whose X component is a new value. /// diff --git a/X10D.Unity/src/Numerics/Vector3Extensions.cs b/X10D.Unity/src/Numerics/Vector3Extensions.cs index 52e0f56..1b725ce 100644 --- a/X10D.Unity/src/Numerics/Vector3Extensions.cs +++ b/X10D.Unity/src/Numerics/Vector3Extensions.cs @@ -9,6 +9,30 @@ namespace X10D.Unity.Numerics; /// public static class Vector3Extensions { + /// + /// Converts the current vector to a . + /// + /// The vector to convert. + /// The converted vector. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static System.Numerics.Vector3 ToSystemVector(this Vector3 vector) + { + return new System.Numerics.Vector3(vector.x, vector.y, vector.z); + } + + /// + /// Converts the current vector to a . + /// + /// The vector to convert. + /// The converted vector. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector3 ToUnityVector(this System.Numerics.Vector3 vector) + { + return new Vector3(vector.X, vector.Y, vector.Z); + } + /// /// Returns a vector whose Y and Z components are the same as the specified vector, and whose X component is a new value. /// diff --git a/X10D.Unity/src/Numerics/Vector4Extensions.cs b/X10D.Unity/src/Numerics/Vector4Extensions.cs index 0a21bca..ca2587f 100644 --- a/X10D.Unity/src/Numerics/Vector4Extensions.cs +++ b/X10D.Unity/src/Numerics/Vector4Extensions.cs @@ -9,6 +9,30 @@ namespace X10D.Unity.Numerics; /// public static class Vector4Extensions { + /// + /// Converts the current vector to a . + /// + /// The vector to convert. + /// The converted vector. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static System.Numerics.Vector4 ToSystemVector(this Vector4 vector) + { + return new System.Numerics.Vector4(vector.x, vector.y, vector.z, vector.w); + } + + /// + /// Converts the current vector to a . + /// + /// The vector to convert. + /// The converted vector. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector4 ToUnityVector(this System.Numerics.Vector4 vector) + { + return new Vector4(vector.X, vector.Y, vector.Z, vector.W); + } + /// /// Returns a vector whose Y, Z, and W components are the same as the specified vector, and whose X component is a new /// value.