Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add numerics type conversions #2238

Merged
merged 16 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions sources/core/Stride.Core.Mathematics.Tests/TestMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
13 changes: 12 additions & 1 deletion sources/core/Stride.Core.Mathematics/Double2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -141,6 +140,18 @@ public double this[int index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator Double2(System.Numerics.Vector2 v) => new(v.X,v.Y);

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator System.Numerics.Vector2(Double2 v) => new((float)v.X, (float)v.Y);

/// <summary>
/// Calculates the length of the vector.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions sources/core/Stride.Core.Mathematics/Double3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ public double this[int index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator Double3(System.Numerics.Vector3 v) => new(v.X, v.Y, v.Z);

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator System.Numerics.Vector3(Double3 v) => new((float)v.X, (float)v.Y, (float)v.Z);


/// <summary>
/// Calculates the length of the vector.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions sources/core/Stride.Core.Mathematics/Double4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ public double this[int index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator Double4(System.Numerics.Vector4 v) => new(v.X, v.Y, v.Z, v.W);

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator System.Numerics.Vector4(Double4 v) => new((float)v.X, (float)v.Y,(float)v.Z,(float)v.W);

/// <summary>
/// Calculates the length of the vector.
/// </summary>
Expand Down
3 changes: 0 additions & 3 deletions sources/core/Stride.Core.Mathematics/Half.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
12 changes: 12 additions & 0 deletions sources/core/Stride.Core.Mathematics/Int2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ public int this[int index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator Int2(System.Numerics.Vector2 v) => new((int)v.X,(int)v.Y);

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator System.Numerics.Vector2(Int2 v) => new(v.X, v.Y);

/// <summary>
/// Calculates the length of the vector.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions sources/core/Stride.Core.Mathematics/Int3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ public int this[int index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator Int3(System.Numerics.Vector3 v) => new((int)v.X, (int)v.Y, (int)v.Z);

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator System.Numerics.Vector3(Int3 v) => new(v.X, v.Y, v.Z);

/// <summary>
/// Calculates the length of the vector.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions sources/core/Stride.Core.Mathematics/Int4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ public int this[int index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator Int4(System.Numerics.Vector4 v) => new((int)v.X, (int)v.Y, (int)v.Z, (int)v.W);

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator System.Numerics.Vector4(Int4 v) => new(v.X, v.Y, v.Z, v.W);

/// <summary>
/// Calculates the length of the vector.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions sources/core/Stride.Core.Mathematics/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,28 @@ public float this[int index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths matrix
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator Matrix(System.Numerics.Matrix4x4 v)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in fact a transpose followed by a cast. Better to write it like that, remove the code duplication. Better yet, while you're at it, cast it first, then transpose, so the transpose is optimized. Little effort to then go and replace the Transpose method doing it like that, too, with 2 casts - which should both be free.

{
//Transpose the matrix due to the different row/column major layout
v = System.Numerics.Matrix4x4.Transpose(v);
Matrix nm = Unsafe.As<System.Numerics.Matrix4x4, Matrix>(ref v);
return nm;
}
/// <summary>
/// Casts from Stride.Maths to System.Numerics matrix
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator System.Numerics.Matrix4x4(Matrix v)
{
System.Numerics.Matrix4x4 nm = Unsafe.As<Matrix, System.Numerics.Matrix4x4>(ref v);
//Transpose the matrix due to the different row/column major layout
return System.Numerics.Matrix4x4.Transpose(nm);
}

/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
Expand Down
19 changes: 18 additions & 1 deletion sources/core/Stride.Core.Mathematics/Quaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/
using System;
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static System.MathF;
Expand Down Expand Up @@ -1470,6 +1469,24 @@ public override readonly bool Equals(object value)
return value is Quaternion q && Equals(q);
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator Quaternion(System.Numerics.Quaternion v)
{
return Unsafe.BitCast<System.Numerics.Quaternion, Quaternion>(v);
}

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator System.Numerics.Quaternion(Quaternion v)
{
return Unsafe.BitCast<Quaternion, System.Numerics.Quaternion>(v);
}

#if SlimDX1xInterop
/// <summary>
/// Performs an implicit conversion from <see cref="Stride.Core.Mathematics.Quaternion"/> to <see cref="SlimDX.Quaternion"/>.
Expand Down
13 changes: 13 additions & 0 deletions sources/core/Stride.Core.Mathematics/UInt4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ public uint this[uint index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator UInt4(System.Numerics.Vector4 v) => new((uint)v.X, (uint)v.Y,(uint)v.Z,(uint)v.W);

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static explicit operator System.Numerics.Vector4(UInt4 v) => new(v.X, v.Y, v.Z, v.W);


/// <summary>
/// Creates an array containing the elements of the vector.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions sources/core/Stride.Core.Mathematics/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ public float this[int index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator Vector2(System.Numerics.Vector2 v)
{
return Unsafe.BitCast<System.Numerics.Vector2, Vector2>(v);
}

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator System.Numerics.Vector2(Vector2 v)
{
return Unsafe.BitCast<Vector2, System.Numerics.Vector2>(v);
}

/// <summary>
/// Calculates the length of the vector.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions sources/core/Stride.Core.Mathematics/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,24 @@ public float this[int index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator Vector3(System.Numerics.Vector3 v)
{
return Unsafe.BitCast<System.Numerics.Vector3, Vector3>(v);
}

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator System.Numerics.Vector3(Vector3 v)
{
return Unsafe.BitCast<Vector3, System.Numerics.Vector3>(v);
}

/// <summary>
/// Calculates the length of the vector.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions sources/core/Stride.Core.Mathematics/Vector4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ public float this[int index]
}
}

/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator Vector4(System.Numerics.Vector4 v)
{
return Unsafe.BitCast<System.Numerics.Vector4, Vector4>(v);
}

/// <summary>
/// Casts from Stride.Maths to System.Numerics vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator System.Numerics.Vector4(Vector4 v)
{
return Unsafe.BitCast<Vector4, System.Numerics.Vector4>(v);
}

/// <summary>
/// Calculates the length of the vector.
/// </summary>
Expand Down