/// <summary>Creates a rotation matrix from the given Quaternion rotation value.</summary> /// <param name="quaternion">The source Quaternion.</param> /// <returns>The rotation matrix.</returns> public static Matrix2X3 <T> CreateFromQuaternion <T>(Quaternion <T> quaternion) where T : unmanaged, IFormattable, IEquatable <T>, IComparable <T> { Matrix2X3 <T> result = Matrix2X3 <T> .Identity; T xx = Scalar.Multiply(quaternion.X, quaternion.X); T yy = Scalar.Multiply(quaternion.Y, quaternion.Y); T zz = Scalar.Multiply(quaternion.Z, quaternion.Z); T xy = Scalar.Multiply(quaternion.X, quaternion.Y); T wz = Scalar.Multiply(quaternion.Z, quaternion.W); T xz = Scalar.Multiply(quaternion.Z, quaternion.X); T wy = Scalar.Multiply(quaternion.Y, quaternion.W); T yz = Scalar.Multiply(quaternion.Y, quaternion.Z); T wx = Scalar.Multiply(quaternion.X, quaternion.W); result.M11 = Scalar.Subtract(Scalar <T> .One, Scalar.Multiply(Scalar <T> .Two, Scalar.Add(yy, zz))); result.M12 = Scalar.Multiply(Scalar <T> .Two, Scalar.Add(xy, wz)); result.M13 = Scalar.Multiply(Scalar <T> .Two, Scalar.Subtract(xz, wy)); result.M21 = Scalar.Multiply(Scalar <T> .Two, Scalar.Subtract(xy, wz)); result.M22 = Scalar.Subtract(Scalar <T> .One, Scalar.Multiply(Scalar <T> .Two, Scalar.Add(zz, xx))); result.M23 = Scalar.Multiply(Scalar <T> .Two, Scalar.Add(yz, wx)); return(result); }
/// <summary>Creates a matrix that rotates around an arbitrary vector.</summary> /// <param name="axis">The axis to rotate around.</param> /// <param name="angle">The angle to rotate around the given axis, in radians.</param> /// <returns>The rotation matrix.</returns> public static Matrix2X3 <T> CreateFromAxisAngle <T>(Vector3D <T> axis, T angle) where T : unmanaged, IFormattable, IEquatable <T>, IComparable <T> { // a: angle // x, y, z: unit vector for axis. // // Rotation matrix M can compute by using below equation. // // T T // M = uu + (cos a)( I-uu ) + (sin a)S // // Where: // // u = ( x, y, z ) // // [ 0 -z y ] // S = [ z 0 -x ] // [ -y x 0 ] // // [ 1 0 0 ] // I = [ 0 1 0 ] // [ 0 0 1 ] // // // [ xx+cosa*(1-xx) yx-cosa*yx-sina*z zx-cosa*xz+sina*y ] // M = [ xy-cosa*yx+sina*z yy+cosa(1-yy) yz-cosa*yz-sina*x ] // [ zx-cosa*zx-sina*y zy-cosa*zy+sina*x zz+cosa*(1-zz) ] // T x = axis.X, y = axis.Y, z = axis.Z; T sa = Scalar.Sin(angle), ca = Scalar.Cos(angle); T xx = Scalar.Multiply(x, x), yy = Scalar.Multiply(y, y), zz = Scalar.Multiply(z, z); T xy = Scalar.Multiply(x, y), xz = Scalar.Multiply(x, z), yz = Scalar.Multiply(y, z); Matrix2X3 <T> result = Matrix2X3 <T> .Identity; result.M11 = Scalar.Add(xx, Scalar.Multiply(ca, Scalar.Subtract(Scalar <T> .One, xx))); result.M12 = Scalar.Add(Scalar.Subtract(xy, Scalar.Multiply(ca, xy)), Scalar.Multiply(sa, z)); result.M13 = Scalar.Subtract(Scalar.Subtract(xz, Scalar.Multiply(ca, xz)), Scalar.Multiply(sa, y)); result.M21 = Scalar.Subtract(Scalar.Subtract(xy, Scalar.Multiply(ca, xy)), Scalar.Multiply(sa, z)); result.M22 = Scalar.Add(yy, Scalar.Multiply(ca, Scalar.Subtract(Scalar <T> .One, yy))); result.M23 = Scalar.Add(Scalar.Subtract(yz, Scalar.Multiply(ca, yz)), Scalar.Multiply(sa, x)); return(result); }
/// <summary>Transforms the given matrix by applying the given Quaternion rotation.</summary> /// <param name="value">The source matrix to transform.</param> /// <param name="rotation">The rotation to apply.</param> /// <returns>The transformed matrix.</returns> public static Matrix2X3 <T> Transform <T>(Matrix2X3 <T> value, Quaternion <T> rotation) where T : unmanaged, IFormattable, IEquatable <T>, IComparable <T> { // Compute rotation matrix. T x2 = Scalar.Add(rotation.X, rotation.X); T y2 = Scalar.Add(rotation.Y, rotation.Y); T z2 = Scalar.Add(rotation.Z, rotation.Z); T wx2 = Scalar.Multiply(rotation.W, x2); T wy2 = Scalar.Multiply(rotation.W, y2); T wz2 = Scalar.Multiply(rotation.W, z2); T xx2 = Scalar.Multiply(rotation.X, x2); T xy2 = Scalar.Multiply(rotation.X, y2); T xz2 = Scalar.Multiply(rotation.X, z2); T yy2 = Scalar.Multiply(rotation.Y, y2); T yz2 = Scalar.Multiply(rotation.Y, z2); T zz2 = Scalar.Multiply(rotation.Z, z2); T q11 = Scalar.Subtract(Scalar.Subtract(Scalar <T> .One, yy2), zz2); T q21 = Scalar.Subtract(xy2, wz2); T q31 = Scalar.Add(xz2, wy2); T q12 = Scalar.Add(xy2, wz2); T q22 = Scalar.Subtract(Scalar.Subtract(Scalar <T> .One, xx2), zz2); T q32 = Scalar.Subtract(yz2, wx2); T q13 = Scalar.Subtract(xz2, wy2); T q23 = Scalar.Add(yz2, wx2); T q33 = Scalar.Subtract(Scalar.Subtract(Scalar <T> .One, xx2), yy2); var q1 = new Vector3D <T>(q11, q12, q13); var q2 = new Vector3D <T>(q21, q22, q23); var q3 = new Vector3D <T>(q31, q32, q33); return(new(value.M11 * q1 + value.M12 * q2 + value.M13 * q3, value.M21 *q1 + value.M22 * q2 + value.M23 * q3)); }
public static Matrix2X3 <T> Multiply <T>(Matrix2X3 <T> value1, Matrix3X3 <T> value2) where T : unmanaged, IFormattable, IEquatable <T>, IComparable <T> => value1 * value2;
public static Matrix2X3 <T> Add <T>(Matrix2X3 <T> value1, Matrix2X3 <T> value2) where T : unmanaged, IFormattable, IEquatable <T>, IComparable <T> { return(value1 + value2); }
/// <summary>Linearly interpolates between the corresponding values of two matrices.</summary> /// <param name="matrix1">The first source matrix.</param> /// <param name="matrix2">The second source matrix.</param> /// <param name="amount">The relative weight of the second source matrix.</param> /// <returns>The interpolated matrix.</returns> public static unsafe Matrix2X3 <T> Lerp <T>(Matrix2X3 <T> matrix1, Matrix2X3 <T> matrix2, T amount) where T : unmanaged, IFormattable, IEquatable <T>, IComparable <T> { return(new(Vector3D.Lerp(matrix1.Row1, matrix2.Row2, amount), Vector3D.Lerp(matrix1.Row2, matrix2.Row2, amount))); }
public static Matrix2X3 <T> Subtract <T>(Matrix2X3 <T> value1, Matrix2X3 <T> value2) where T : unmanaged, IFormattable, IEquatable <T>, IComparable <T> => value1 - value2;
public static Matrix2X3 <T> Negate <T>(Matrix2X3 <T> value) where T : unmanaged, IFormattable, IEquatable <T>, IComparable <T> => - value;