From 8bb3ca2f53f058b3c988a18e80d8aa3610037ef3 Mon Sep 17 00:00:00 2001 From: Doprez <73259914+Doprez@users.noreply.github.com> Date: Fri, 27 Dec 2024 16:09:01 -0700 Subject: [PATCH] feat: Add numerics type conversions (#2238) --- .../TestMatrix.cs | 38 +++++++++++++++++++ .../core/Stride.Core.Mathematics/Double2.cs | 13 ++++++- .../core/Stride.Core.Mathematics/Double3.cs | 13 +++++++ .../core/Stride.Core.Mathematics/Double4.cs | 12 ++++++ sources/core/Stride.Core.Mathematics/Half.cs | 3 -- sources/core/Stride.Core.Mathematics/Int2.cs | 12 ++++++ sources/core/Stride.Core.Mathematics/Int3.cs | 12 ++++++ sources/core/Stride.Core.Mathematics/Int4.cs | 12 ++++++ .../core/Stride.Core.Mathematics/Matrix.cs | 22 +++++++++++ .../Stride.Core.Mathematics/Quaternion.cs | 19 +++++++++- sources/core/Stride.Core.Mathematics/UInt4.cs | 13 +++++++ .../core/Stride.Core.Mathematics/Vector2.cs | 18 +++++++++ .../core/Stride.Core.Mathematics/Vector3.cs | 18 +++++++++ .../core/Stride.Core.Mathematics/Vector4.cs | 18 +++++++++ 14 files changed, 218 insertions(+), 5 deletions(-) diff --git a/sources/core/Stride.Core.Mathematics.Tests/TestMatrix.cs b/sources/core/Stride.Core.Mathematics.Tests/TestMatrix.cs index a1fcea459f..96dd9392dc 100644 --- a/sources/core/Stride.Core.Mathematics.Tests/TestMatrix.cs +++ b/sources/core/Stride.Core.Mathematics.Tests/TestMatrix.cs @@ -75,5 +75,43 @@ public void TestDecomposeXYZFromMatricesXYZ(float yawDegrees, float pitchDegrees var expectedQuat = Quaternion.RotationX(pitchRadians) * Quaternion.RotationY(yawRadians) * Quaternion.RotationZ(rollRadians); Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}"); } + + [Fact] + public void TestNumericConversion() + { + System.Numerics.Matrix4x4 matrix = new System.Numerics.Matrix4x4( + 1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16); + + Matrix baseStrideMatrix = new Matrix( + 1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16); + + Matrix strideMatrix = matrix; + Assert.Equal(baseStrideMatrix, strideMatrix); + } + + [Fact] + public void TestStrideConversion() + { + Matrix matrix = new( + 1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16); + + System.Numerics.Matrix4x4 baseNumericseMatrix = new( + 1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16); + + System.Numerics.Matrix4x4 numericsMatrix = matrix; + Assert.Equal(baseNumericseMatrix, numericsMatrix); + } } } diff --git a/sources/core/Stride.Core.Mathematics/Double2.cs b/sources/core/Stride.Core.Mathematics/Double2.cs index 0d5031618e..4c6afefd22 100644 --- a/sources/core/Stride.Core.Mathematics/Double2.cs +++ b/sources/core/Stride.Core.Mathematics/Double2.cs @@ -2,7 +2,6 @@ // Distributed under the MIT license. See the LICENSE.md file in the project root for more information. using System; -using System.ComponentModel; using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -141,6 +140,18 @@ public double this[int index] } } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static implicit operator Double2(System.Numerics.Vector2 v) => new(v.X,v.Y); + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static explicit operator System.Numerics.Vector2(Double2 v) => new((float)v.X, (float)v.Y); + /// /// Calculates the length of the vector. /// diff --git a/sources/core/Stride.Core.Mathematics/Double3.cs b/sources/core/Stride.Core.Mathematics/Double3.cs index f7e11603b1..c490813007 100644 --- a/sources/core/Stride.Core.Mathematics/Double3.cs +++ b/sources/core/Stride.Core.Mathematics/Double3.cs @@ -171,6 +171,19 @@ public double this[int index] } } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static implicit operator Double3(System.Numerics.Vector3 v) => new(v.X, v.Y, v.Z); + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static explicit operator System.Numerics.Vector3(Double3 v) => new((float)v.X, (float)v.Y, (float)v.Z); + + /// /// Calculates the length of the vector. /// diff --git a/sources/core/Stride.Core.Mathematics/Double4.cs b/sources/core/Stride.Core.Mathematics/Double4.cs index 827eff522a..6ec1897a32 100644 --- a/sources/core/Stride.Core.Mathematics/Double4.cs +++ b/sources/core/Stride.Core.Mathematics/Double4.cs @@ -204,6 +204,18 @@ public double this[int index] } } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static implicit operator Double4(System.Numerics.Vector4 v) => new(v.X, v.Y, v.Z, v.W); + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static explicit operator System.Numerics.Vector4(Double4 v) => new((float)v.X, (float)v.Y,(float)v.Z,(float)v.W); + /// /// Calculates the length of the vector. /// diff --git a/sources/core/Stride.Core.Mathematics/Half.cs b/sources/core/Stride.Core.Mathematics/Half.cs index 7fa5221ea2..5513a548f9 100644 --- a/sources/core/Stride.Core.Mathematics/Half.cs +++ b/sources/core/Stride.Core.Mathematics/Half.cs @@ -21,11 +21,8 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; -using System.ComponentModel; using System.Globalization; using System.Runtime.InteropServices; -using Stride.Core.Serialization; namespace Stride.Core.Mathematics { diff --git a/sources/core/Stride.Core.Mathematics/Int2.cs b/sources/core/Stride.Core.Mathematics/Int2.cs index 397e980a72..9c3898ac5c 100644 --- a/sources/core/Stride.Core.Mathematics/Int2.cs +++ b/sources/core/Stride.Core.Mathematics/Int2.cs @@ -157,6 +157,18 @@ public int this[int index] } } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static explicit operator Int2(System.Numerics.Vector2 v) => new((int)v.X,(int)v.Y); + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static explicit operator System.Numerics.Vector2(Int2 v) => new(v.X, v.Y); + /// /// Calculates the length of the vector. /// diff --git a/sources/core/Stride.Core.Mathematics/Int3.cs b/sources/core/Stride.Core.Mathematics/Int3.cs index 252a73c741..fa004ba90a 100644 --- a/sources/core/Stride.Core.Mathematics/Int3.cs +++ b/sources/core/Stride.Core.Mathematics/Int3.cs @@ -176,6 +176,18 @@ public int this[int index] } } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static explicit operator Int3(System.Numerics.Vector3 v) => new((int)v.X, (int)v.Y, (int)v.Z); + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static explicit operator System.Numerics.Vector3(Int3 v) => new(v.X, v.Y, v.Z); + /// /// Calculates the length of the vector. /// diff --git a/sources/core/Stride.Core.Mathematics/Int4.cs b/sources/core/Stride.Core.Mathematics/Int4.cs index d946bf49a6..df540a358f 100644 --- a/sources/core/Stride.Core.Mathematics/Int4.cs +++ b/sources/core/Stride.Core.Mathematics/Int4.cs @@ -180,6 +180,18 @@ public int this[int index] } } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static explicit operator Int4(System.Numerics.Vector4 v) => new((int)v.X, (int)v.Y, (int)v.Z, (int)v.W); + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static explicit operator System.Numerics.Vector4(Int4 v) => new(v.X, v.Y, v.Z, v.W); + /// /// Calculates the length of the vector. /// diff --git a/sources/core/Stride.Core.Mathematics/Matrix.cs b/sources/core/Stride.Core.Mathematics/Matrix.cs index bf5b95d75a..734262520c 100644 --- a/sources/core/Stride.Core.Mathematics/Matrix.cs +++ b/sources/core/Stride.Core.Mathematics/Matrix.cs @@ -453,6 +453,28 @@ public float this[int index] } } + /// + /// Casts from System.Numerics to Stride.Maths matrix + /// + /// Value to cast + public static implicit operator Matrix(System.Numerics.Matrix4x4 v) + { + //Transpose the matrix due to the different row/column major layout + v = System.Numerics.Matrix4x4.Transpose(v); + Matrix nm = Unsafe.As(ref v); + return nm; + } + /// + /// Casts from Stride.Maths to System.Numerics matrix + /// + /// Value to cast + public static implicit operator System.Numerics.Matrix4x4(Matrix v) + { + System.Numerics.Matrix4x4 nm = Unsafe.As(ref v); + //Transpose the matrix due to the different row/column major layout + return System.Numerics.Matrix4x4.Transpose(nm); + } + /// /// Gets or sets the component at the specified index. /// diff --git a/sources/core/Stride.Core.Mathematics/Quaternion.cs b/sources/core/Stride.Core.Mathematics/Quaternion.cs index c078372102..71cb118d5f 100644 --- a/sources/core/Stride.Core.Mathematics/Quaternion.cs +++ b/sources/core/Stride.Core.Mathematics/Quaternion.cs @@ -28,7 +28,6 @@ */ using System; using System.Globalization; -using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using static System.MathF; @@ -1470,6 +1469,24 @@ public override readonly bool Equals(object value) return value is Quaternion q && Equals(q); } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static implicit operator Quaternion(System.Numerics.Quaternion v) + { + return Unsafe.BitCast(v); + } + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static implicit operator System.Numerics.Quaternion(Quaternion v) + { + return Unsafe.BitCast(v); + } + #if SlimDX1xInterop /// /// Performs an implicit conversion from to . diff --git a/sources/core/Stride.Core.Mathematics/UInt4.cs b/sources/core/Stride.Core.Mathematics/UInt4.cs index c8a098c97f..1aa5233f9d 100644 --- a/sources/core/Stride.Core.Mathematics/UInt4.cs +++ b/sources/core/Stride.Core.Mathematics/UInt4.cs @@ -184,6 +184,19 @@ public uint this[uint index] } } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static explicit operator UInt4(System.Numerics.Vector4 v) => new((uint)v.X, (uint)v.Y,(uint)v.Z,(uint)v.W); + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static explicit operator System.Numerics.Vector4(UInt4 v) => new(v.X, v.Y, v.Z, v.W); + + /// /// Creates an array containing the elements of the vector. /// diff --git a/sources/core/Stride.Core.Mathematics/Vector2.cs b/sources/core/Stride.Core.Mathematics/Vector2.cs index c42834e207..d007357c05 100644 --- a/sources/core/Stride.Core.Mathematics/Vector2.cs +++ b/sources/core/Stride.Core.Mathematics/Vector2.cs @@ -155,6 +155,24 @@ public float this[int index] } } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static implicit operator Vector2(System.Numerics.Vector2 v) + { + return Unsafe.BitCast(v); + } + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static implicit operator System.Numerics.Vector2(Vector2 v) + { + return Unsafe.BitCast(v); + } + /// /// Calculates the length of the vector. /// diff --git a/sources/core/Stride.Core.Mathematics/Vector3.cs b/sources/core/Stride.Core.Mathematics/Vector3.cs index 92e3b14083..fe7a25f74b 100644 --- a/sources/core/Stride.Core.Mathematics/Vector3.cs +++ b/sources/core/Stride.Core.Mathematics/Vector3.cs @@ -184,6 +184,24 @@ public float this[int index] } } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static implicit operator Vector3(System.Numerics.Vector3 v) + { + return Unsafe.BitCast(v); + } + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static implicit operator System.Numerics.Vector3(Vector3 v) + { + return Unsafe.BitCast(v); + } + /// /// Calculates the length of the vector. /// diff --git a/sources/core/Stride.Core.Mathematics/Vector4.cs b/sources/core/Stride.Core.Mathematics/Vector4.cs index 82397450ca..ca327769ac 100644 --- a/sources/core/Stride.Core.Mathematics/Vector4.cs +++ b/sources/core/Stride.Core.Mathematics/Vector4.cs @@ -216,6 +216,24 @@ public float this[int index] } } + /// + /// Casts from System.Numerics to Stride.Maths vectors + /// + /// Value to cast + public static implicit operator Vector4(System.Numerics.Vector4 v) + { + return Unsafe.BitCast(v); + } + + /// + /// Casts from Stride.Maths to System.Numerics vectors + /// + /// Value to cast + public static implicit operator System.Numerics.Vector4(Vector4 v) + { + return Unsafe.BitCast(v); + } + /// /// Calculates the length of the vector. ///