-
-
Notifications
You must be signed in to change notification settings - Fork 978
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
Deprecate stride math for numerics alternatives #2579
base: master
Are you sure you want to change the base?
Changes from all commits
e040b3d
8e05644
1ccb8d7
9ab70d1
fee3181
47f0a01
1e8fb27
66e72c6
b483652
a4f00fd
9464603
ffd200c
3134b68
57ee4cf
7c75a1e
64c1ed8
bbe23d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here. |
||
|
||
/// <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> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System.Numerics; | ||
using System.Transactions; | ||
|
||
namespace Stride.Core.Mathematics; | ||
public static partial class MathUtil | ||
{ | ||
|
||
public static Matrix4x4 Orthonormalize(Matrix4x4 matrix) | ||
{ | ||
var result = matrix; | ||
|
||
var row1 = new System.Numerics.Vector4(matrix.M11, matrix.M12, matrix.M13, matrix.M14); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a bit unfortunate that var rows = MemoryMarshal.Cast<Matrix4x4, Vector4>(MemoryMarshal.CreateSpan(ref m, 1));
var row4 = rows[3]; // get the bounds check out of the way asap
var row3 = rows[2];
... Or, even faster:
Could also use Bitcast, but since it is by value and the sizes don't match, I'd have to try. And either way not entirely sure the above code will be optimal. Below you should do the reverse, since certainly 16 float assignments is a bad idea that you'd want to prevent everywhere you see them. |
||
var row2 = new System.Numerics.Vector4(matrix.M21, matrix.M22, matrix.M23, matrix.M24); | ||
var row3 = new System.Numerics.Vector4(matrix.M31, matrix.M32, matrix.M33, matrix.M34); | ||
var row4 = new System.Numerics.Vector4(matrix.M41, matrix.M42, matrix.M43, matrix.M44); | ||
|
||
row1 = System.Numerics.Vector4.Normalize(row1); | ||
|
||
row2 = row2 - System.Numerics.Vector4.Dot(row1, row2) * row1; | ||
row2 = System.Numerics.Vector4.Normalize(row2); | ||
|
||
row3 = row3 - System.Numerics.Vector4.Dot(row1, row3) * row1; | ||
row3 = row3 - System.Numerics.Vector4.Dot(row2, row3) * row2; | ||
row3 = System.Numerics.Vector4.Normalize(row3); | ||
|
||
row4 = row4 - System.Numerics.Vector4.Dot(row1, row4) * row1; | ||
row4 = row4 - System.Numerics.Vector4.Dot(row2, row4) * row2; | ||
row4 = row4 - System.Numerics.Vector4.Dot(row3, row4) * row3; | ||
row4 = System.Numerics.Vector4.Normalize(row4); | ||
|
||
result = new Matrix4x4(); | ||
result.M11 = row1.X; | ||
result.M12 = row1.Y; | ||
result.M13 = row1.Z; | ||
result.M14 = row1.W; | ||
result.M21 = row2.X; | ||
result.M22 = row2.Y; | ||
result.M23 = row2.Z; | ||
result.M24 = row2.W; | ||
result.M31 = row3.X; | ||
result.M32 = row3.Y; | ||
result.M33 = row3.Z; | ||
result.M34 = row3.W; | ||
result.M41 = row4.X; | ||
result.M42 = row4.Y; | ||
result.M43 = row4.Z; | ||
result.M44 = row4.W; | ||
|
||
return result; | ||
} | ||
|
||
public static Matrix4x4 Orthogonalize(Matrix4x4 matrix) | ||
{ | ||
var result = matrix; | ||
|
||
var row1 = new System.Numerics.Vector4(matrix.M11, matrix.M12, matrix.M13, matrix.M14); | ||
var row2 = new System.Numerics.Vector4(matrix.M21, matrix.M22, matrix.M23, matrix.M24); | ||
var row3 = new System.Numerics.Vector4(matrix.M31, matrix.M32, matrix.M33, matrix.M34); | ||
var row4 = new System.Numerics.Vector4(matrix.M41, matrix.M42, matrix.M43, matrix.M44); | ||
|
||
row2 = row2 - (System.Numerics.Vector4.Dot(row1, row2) / System.Numerics.Vector4.Dot(row1, row1)) * row1; | ||
|
||
row3 = row3 - (System.Numerics.Vector4.Dot(row1, row3) / System.Numerics.Vector4.Dot(row1, row1)) * row1; | ||
row3 = row3 - (System.Numerics.Vector4.Dot(row2, row3) / System.Numerics.Vector4.Dot(row2, row2)) * row2; | ||
|
||
row4 = row4 - (System.Numerics.Vector4.Dot(row1, row4) / System.Numerics.Vector4.Dot(row1, row1)) * row1; | ||
row4 = row4 - (System.Numerics.Vector4.Dot(row2, row4) / System.Numerics.Vector4.Dot(row2, row2)) * row2; | ||
row4 = row4 - (System.Numerics.Vector4.Dot(row3, row4) / System.Numerics.Vector4.Dot(row3, row3)) * row3; | ||
|
||
result.M21 = row2.X; | ||
result.M22 = row2.Y; | ||
result.M23 = row2.Z; | ||
result.M24 = row2.W; | ||
result.M31 = row3.X; | ||
result.M32 = row3.Y; | ||
result.M33 = row3.Z; | ||
result.M34 = row3.W; | ||
result.M41 = row4.X; | ||
result.M42 = row4.Y; | ||
result.M43 = row4.Z; | ||
result.M44 = row4.W; | ||
|
||
return result; | ||
} | ||
|
||
public static Matrix4x4 Invert(Matrix4x4 matrix) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally speaking, not sure whether it is a good idea to add public API for Before that decision is made, I would keep these internal. |
||
{ | ||
Matrix4x4.Invert(matrix, out var result); | ||
return result; | ||
} | ||
|
||
public static void Decompose(Matrix4x4 matrix, out System.Numerics.Vector3 scale, out System.Numerics.Quaternion rotation, out System.Numerics.Vector3 translation) | ||
{ | ||
Matrix4x4.Decompose(matrix, out scale, out rotation, out translation); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a space here?