Skip to content

Commit 8bb3ca2

Browse files
authored
feat: Add numerics type conversions (#2238)
1 parent 154dabf commit 8bb3ca2

File tree

14 files changed

+218
-5
lines changed

14 files changed

+218
-5
lines changed

sources/core/Stride.Core.Mathematics.Tests/TestMatrix.cs

+38
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,43 @@ public void TestDecomposeXYZFromMatricesXYZ(float yawDegrees, float pitchDegrees
7575
var expectedQuat = Quaternion.RotationX(pitchRadians) * Quaternion.RotationY(yawRadians) * Quaternion.RotationZ(rollRadians);
7676
Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}");
7777
}
78+
79+
[Fact]
80+
public void TestNumericConversion()
81+
{
82+
System.Numerics.Matrix4x4 matrix = new System.Numerics.Matrix4x4(
83+
1, 2, 3, 4,
84+
5, 6, 7, 8,
85+
9, 10, 11, 12,
86+
13, 14, 15, 16);
87+
88+
Matrix baseStrideMatrix = new Matrix(
89+
1, 2, 3, 4,
90+
5, 6, 7, 8,
91+
9, 10, 11, 12,
92+
13, 14, 15, 16);
93+
94+
Matrix strideMatrix = matrix;
95+
Assert.Equal(baseStrideMatrix, strideMatrix);
96+
}
97+
98+
[Fact]
99+
public void TestStrideConversion()
100+
{
101+
Matrix matrix = new(
102+
1, 2, 3, 4,
103+
5, 6, 7, 8,
104+
9, 10, 11, 12,
105+
13, 14, 15, 16);
106+
107+
System.Numerics.Matrix4x4 baseNumericseMatrix = new(
108+
1, 2, 3, 4,
109+
5, 6, 7, 8,
110+
9, 10, 11, 12,
111+
13, 14, 15, 16);
112+
113+
System.Numerics.Matrix4x4 numericsMatrix = matrix;
114+
Assert.Equal(baseNumericseMatrix, numericsMatrix);
115+
}
78116
}
79117
}

sources/core/Stride.Core.Mathematics/Double2.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
33

44
using System;
5-
using System.ComponentModel;
65
using System.Globalization;
76
using System.Runtime.CompilerServices;
87
using System.Runtime.InteropServices;
@@ -141,6 +140,18 @@ public double this[int index]
141140
}
142141
}
143142

143+
/// <summary>
144+
/// Casts from System.Numerics to Stride.Maths vectors
145+
/// </summary>
146+
/// <param name="v">Value to cast</param>
147+
public static implicit operator Double2(System.Numerics.Vector2 v) => new(v.X,v.Y);
148+
149+
/// <summary>
150+
/// Casts from Stride.Maths to System.Numerics vectors
151+
/// </summary>
152+
/// <param name="v">Value to cast</param>
153+
public static explicit operator System.Numerics.Vector2(Double2 v) => new((float)v.X, (float)v.Y);
154+
144155
/// <summary>
145156
/// Calculates the length of the vector.
146157
/// </summary>

sources/core/Stride.Core.Mathematics/Double3.cs

+13
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ public double this[int index]
171171
}
172172
}
173173

174+
/// <summary>
175+
/// Casts from System.Numerics to Stride.Maths vectors
176+
/// </summary>
177+
/// <param name="v">Value to cast</param>
178+
public static implicit operator Double3(System.Numerics.Vector3 v) => new(v.X, v.Y, v.Z);
179+
180+
/// <summary>
181+
/// Casts from Stride.Maths to System.Numerics vectors
182+
/// </summary>
183+
/// <param name="v">Value to cast</param>
184+
public static explicit operator System.Numerics.Vector3(Double3 v) => new((float)v.X, (float)v.Y, (float)v.Z);
185+
186+
174187
/// <summary>
175188
/// Calculates the length of the vector.
176189
/// </summary>

sources/core/Stride.Core.Mathematics/Double4.cs

+12
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ public double this[int index]
204204
}
205205
}
206206

207+
/// <summary>
208+
/// Casts from System.Numerics to Stride.Maths vectors
209+
/// </summary>
210+
/// <param name="v">Value to cast</param>
211+
public static implicit operator Double4(System.Numerics.Vector4 v) => new(v.X, v.Y, v.Z, v.W);
212+
213+
/// <summary>
214+
/// Casts from Stride.Maths to System.Numerics vectors
215+
/// </summary>
216+
/// <param name="v">Value to cast</param>
217+
public static explicit operator System.Numerics.Vector4(Double4 v) => new((float)v.X, (float)v.Y,(float)v.Z,(float)v.W);
218+
207219
/// <summary>
208220
/// Calculates the length of the vector.
209221
/// </summary>

sources/core/Stride.Core.Mathematics/Half.cs

-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
// THE SOFTWARE.
2323

24-
using System;
25-
using System.ComponentModel;
2624
using System.Globalization;
2725
using System.Runtime.InteropServices;
28-
using Stride.Core.Serialization;
2926

3027
namespace Stride.Core.Mathematics
3128
{

sources/core/Stride.Core.Mathematics/Int2.cs

+12
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ public int this[int index]
157157
}
158158
}
159159

160+
/// <summary>
161+
/// Casts from System.Numerics to Stride.Maths vectors
162+
/// </summary>
163+
/// <param name="v">Value to cast</param>
164+
public static explicit operator Int2(System.Numerics.Vector2 v) => new((int)v.X,(int)v.Y);
165+
166+
/// <summary>
167+
/// Casts from Stride.Maths to System.Numerics vectors
168+
/// </summary>
169+
/// <param name="v">Value to cast</param>
170+
public static explicit operator System.Numerics.Vector2(Int2 v) => new(v.X, v.Y);
171+
160172
/// <summary>
161173
/// Calculates the length of the vector.
162174
/// </summary>

sources/core/Stride.Core.Mathematics/Int3.cs

+12
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ public int this[int index]
176176
}
177177
}
178178

179+
/// <summary>
180+
/// Casts from System.Numerics to Stride.Maths vectors
181+
/// </summary>
182+
/// <param name="v">Value to cast</param>
183+
public static explicit operator Int3(System.Numerics.Vector3 v) => new((int)v.X, (int)v.Y, (int)v.Z);
184+
185+
/// <summary>
186+
/// Casts from Stride.Maths to System.Numerics vectors
187+
/// </summary>
188+
/// <param name="v">Value to cast</param>
189+
public static explicit operator System.Numerics.Vector3(Int3 v) => new(v.X, v.Y, v.Z);
190+
179191
/// <summary>
180192
/// Calculates the length of the vector.
181193
/// </summary>

sources/core/Stride.Core.Mathematics/Int4.cs

+12
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ public int this[int index]
180180
}
181181
}
182182

183+
/// <summary>
184+
/// Casts from System.Numerics to Stride.Maths vectors
185+
/// </summary>
186+
/// <param name="v">Value to cast</param>
187+
public static explicit operator Int4(System.Numerics.Vector4 v) => new((int)v.X, (int)v.Y, (int)v.Z, (int)v.W);
188+
189+
/// <summary>
190+
/// Casts from Stride.Maths to System.Numerics vectors
191+
/// </summary>
192+
/// <param name="v">Value to cast</param>
193+
public static explicit operator System.Numerics.Vector4(Int4 v) => new(v.X, v.Y, v.Z, v.W);
194+
183195
/// <summary>
184196
/// Calculates the length of the vector.
185197
/// </summary>

sources/core/Stride.Core.Mathematics/Matrix.cs

+22
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,28 @@ public float this[int index]
453453
}
454454
}
455455

456+
/// <summary>
457+
/// Casts from System.Numerics to Stride.Maths matrix
458+
/// </summary>
459+
/// <param name="v">Value to cast</param>
460+
public static implicit operator Matrix(System.Numerics.Matrix4x4 v)
461+
{
462+
//Transpose the matrix due to the different row/column major layout
463+
v = System.Numerics.Matrix4x4.Transpose(v);
464+
Matrix nm = Unsafe.As<System.Numerics.Matrix4x4, Matrix>(ref v);
465+
return nm;
466+
}
467+
/// <summary>
468+
/// Casts from Stride.Maths to System.Numerics matrix
469+
/// </summary>
470+
/// <param name="v">Value to cast</param>
471+
public static implicit operator System.Numerics.Matrix4x4(Matrix v)
472+
{
473+
System.Numerics.Matrix4x4 nm = Unsafe.As<Matrix, System.Numerics.Matrix4x4>(ref v);
474+
//Transpose the matrix due to the different row/column major layout
475+
return System.Numerics.Matrix4x4.Transpose(nm);
476+
}
477+
456478
/// <summary>
457479
/// Gets or sets the component at the specified index.
458480
/// </summary>

sources/core/Stride.Core.Mathematics/Quaternion.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
*/
2929
using System;
3030
using System.Globalization;
31-
using System.Numerics;
3231
using System.Runtime.CompilerServices;
3332
using System.Runtime.InteropServices;
3433
using static System.MathF;
@@ -1470,6 +1469,24 @@ public override readonly bool Equals(object value)
14701469
return value is Quaternion q && Equals(q);
14711470
}
14721471

1472+
/// <summary>
1473+
/// Casts from System.Numerics to Stride.Maths vectors
1474+
/// </summary>
1475+
/// <param name="v">Value to cast</param>
1476+
public static implicit operator Quaternion(System.Numerics.Quaternion v)
1477+
{
1478+
return Unsafe.BitCast<System.Numerics.Quaternion, Quaternion>(v);
1479+
}
1480+
1481+
/// <summary>
1482+
/// Casts from Stride.Maths to System.Numerics vectors
1483+
/// </summary>
1484+
/// <param name="v">Value to cast</param>
1485+
public static implicit operator System.Numerics.Quaternion(Quaternion v)
1486+
{
1487+
return Unsafe.BitCast<Quaternion, System.Numerics.Quaternion>(v);
1488+
}
1489+
14731490
#if SlimDX1xInterop
14741491
/// <summary>
14751492
/// Performs an implicit conversion from <see cref="Stride.Core.Mathematics.Quaternion"/> to <see cref="SlimDX.Quaternion"/>.

sources/core/Stride.Core.Mathematics/UInt4.cs

+13
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ public uint this[uint index]
184184
}
185185
}
186186

187+
/// <summary>
188+
/// Casts from System.Numerics to Stride.Maths vectors
189+
/// </summary>
190+
/// <param name="v">Value to cast</param>
191+
public static explicit operator UInt4(System.Numerics.Vector4 v) => new((uint)v.X, (uint)v.Y,(uint)v.Z,(uint)v.W);
192+
193+
/// <summary>
194+
/// Casts from Stride.Maths to System.Numerics vectors
195+
/// </summary>
196+
/// <param name="v">Value to cast</param>
197+
public static explicit operator System.Numerics.Vector4(UInt4 v) => new(v.X, v.Y, v.Z, v.W);
198+
199+
187200
/// <summary>
188201
/// Creates an array containing the elements of the vector.
189202
/// </summary>

sources/core/Stride.Core.Mathematics/Vector2.cs

+18
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ public float this[int index]
155155
}
156156
}
157157

158+
/// <summary>
159+
/// Casts from System.Numerics to Stride.Maths vectors
160+
/// </summary>
161+
/// <param name="v">Value to cast</param>
162+
public static implicit operator Vector2(System.Numerics.Vector2 v)
163+
{
164+
return Unsafe.BitCast<System.Numerics.Vector2, Vector2>(v);
165+
}
166+
167+
/// <summary>
168+
/// Casts from Stride.Maths to System.Numerics vectors
169+
/// </summary>
170+
/// <param name="v">Value to cast</param>
171+
public static implicit operator System.Numerics.Vector2(Vector2 v)
172+
{
173+
return Unsafe.BitCast<Vector2, System.Numerics.Vector2>(v);
174+
}
175+
158176
/// <summary>
159177
/// Calculates the length of the vector.
160178
/// </summary>

sources/core/Stride.Core.Mathematics/Vector3.cs

+18
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,24 @@ public float this[int index]
184184
}
185185
}
186186

187+
/// <summary>
188+
/// Casts from System.Numerics to Stride.Maths vectors
189+
/// </summary>
190+
/// <param name="v">Value to cast</param>
191+
public static implicit operator Vector3(System.Numerics.Vector3 v)
192+
{
193+
return Unsafe.BitCast<System.Numerics.Vector3, Vector3>(v);
194+
}
195+
196+
/// <summary>
197+
/// Casts from Stride.Maths to System.Numerics vectors
198+
/// </summary>
199+
/// <param name="v">Value to cast</param>
200+
public static implicit operator System.Numerics.Vector3(Vector3 v)
201+
{
202+
return Unsafe.BitCast<Vector3, System.Numerics.Vector3>(v);
203+
}
204+
187205
/// <summary>
188206
/// Calculates the length of the vector.
189207
/// </summary>

sources/core/Stride.Core.Mathematics/Vector4.cs

+18
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,24 @@ public float this[int index]
216216
}
217217
}
218218

219+
/// <summary>
220+
/// Casts from System.Numerics to Stride.Maths vectors
221+
/// </summary>
222+
/// <param name="v">Value to cast</param>
223+
public static implicit operator Vector4(System.Numerics.Vector4 v)
224+
{
225+
return Unsafe.BitCast<System.Numerics.Vector4, Vector4>(v);
226+
}
227+
228+
/// <summary>
229+
/// Casts from Stride.Maths to System.Numerics vectors
230+
/// </summary>
231+
/// <param name="v">Value to cast</param>
232+
public static implicit operator System.Numerics.Vector4(Vector4 v)
233+
{
234+
return Unsafe.BitCast<Vector4, System.Numerics.Vector4>(v);
235+
}
236+
219237
/// <summary>
220238
/// Calculates the length of the vector.
221239
/// </summary>

0 commit comments

Comments
 (0)