Skip to content

Commit

Permalink
[Math] System.Numerics conversions (#1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykafia authored Oct 9, 2021
1 parent a398412 commit 0fd8f8d
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sources/core/Stride.Core.Mathematics/Double2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,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
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 @@ -156,6 +156,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 @@ -175,6 +175,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 @@ -187,6 +187,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
28 changes: 28 additions & 0 deletions sources/core/Stride.Core.Mathematics/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#pragma warning disable SA1313 // Parameter names must begin with lower-case letter
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Stride.Core.Mathematics
Expand Down Expand Up @@ -445,6 +446,33 @@ 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)
{
return new(
v.M11, v.M12, v.M13, v.M14,
v.M21, v.M22, v.M23, v.M24,
v.M31, v.M32, v.M33, v.M34,
v.M41, v.M42, v.M43, v.M44
);
}
/// <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)
{
return new(
v.M11, v.M12, v.M13, v.M14,
v.M21, v.M22, v.M23, v.M24,
v.M31, v.M32, v.M33, v.M34,
v.M41, v.M42, v.M43, v.M44
);
}

/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
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 @@ -183,6 +183,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.As<System.Numerics.Vector2, Vector2>(ref 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.As<Vector2, System.Numerics.Vector2>(ref 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.As<System.Numerics.Vector3, Vector3>(ref 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.As<Vector3, System.Numerics.Vector3>(ref 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.As<System.Numerics.Vector4, Vector4>(ref 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.As<Vector4, System.Numerics.Vector4>(ref v);
}

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

0 comments on commit 0fd8f8d

Please sign in to comment.