/// <summary> /// Transposes a matrix. /// </summary> /// <param name="m">A <see cref="Matrix4F"/> instance.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the transposed matrix.</returns> public static Matrix4F Transpose(Matrix4F m) { Matrix4F t = new Matrix4F(m); t.Transpose(); return t; }
/// <summary> /// Adds two matrices. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="b">A <see cref="Matrix4F"/> instance.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the sum.</returns> public static Matrix4F operator+(Matrix4F a, Matrix4F b) { return(Matrix4F.Add(a, b)); }
/// <summary> /// Transforms a given vector by a matrix applying the positional matrix components but omitting the projection components. /// </summary> /// <param name="matrix">A <see cref="Matrix4F"/> instance.</param> /// <param name="vector">A <see cref="Vector3F"/> instance.</param> /// <returns>A new <see cref="Vector3F"/> instance containing the result.</returns> public static Vector3F Transform(Matrix4F matrix, Vector3F vector) { return new Vector3F( (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + matrix.M14, (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + matrix.M24, (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + matrix.M34); }
/// <summary> /// Transforms a given 3F vector by a matrix using perspective division. /// </summary> /// <remarks> /// Before the matrix multiplication the 3F vector is extended to 4F by setting its W component to 1. /// After the matrix multiplication the resulting 4D vector is transformed to 3D by dividing X, Y, and Z by W. /// (perspective division). /// </remarks> /// <param name="matrix">A <see cref="Matrix4F"/> instance.</param> /// <param name="vector">A <see cref="Vector3F"/> instance.</param> /// <returns>A new <see cref="Vector3D"/> instance containing the result.</returns> public static Vector3F TransformPD(Matrix4F matrix, Vector3F vector) { float w = (matrix.M41 * vector.X) + (matrix.M42 * vector.Y) + (matrix.M43 * vector.Z) + matrix.M44; return new Vector3F( ((matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + matrix.M14) / w, ((matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + matrix.M24) / w, ((matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + matrix.M34) / w); }
/// <summary> /// Subtracts a matrix from a matrix and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance to subtract from.</param> /// <param name="b">A <see cref="Matrix4F"/> instance to subtract.</param> /// <param name="result">A <see cref="Matrix4F"/> instance to hold the result.</param> /// <remarks>result[x][y] = a[x][y] - b[x][y]</remarks> public static void Subtract(Matrix4F a, Matrix4F b, ref Matrix4F result) { result.M11 = a.M11 - b.M11; result.M12 = a.M12 - b.M12; result.M13 = a.M13 - b.M13; result.M14 = a.M14 - b.M14; result.M21 = a.M21 - b.M21; result.M22 = a.M22 - b.M22; result.M23 = a.M23 - b.M23; result.M24 = a.M24 - b.M24; result.M31 = a.M31 - b.M31; result.M32 = a.M32 - b.M32; result.M33 = a.M33 - b.M33; result.M34 = a.M34 - b.M34; result.M41 = a.M41 - b.M41; result.M42 = a.M42 - b.M42; result.M43 = a.M43 - b.M43; result.M44 = a.M44 - b.M44; }
/// <summary> /// Transforms a given vector by a matrix. /// </summary> /// <param name="matrix">A <see cref="Matrix4F"/> instance.</param> /// <param name="vector">A <see cref="Vector4F"/> instance.</param> /// <returns>A new <see cref="Vector4F"/> instance containing the result.</returns> public static Vector4F Transform(Matrix4F matrix, Vector4F vector) { return new Vector4F( (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + (matrix.M14 * vector.W), (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + (matrix.M24 * vector.W), (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + (matrix.M34 * vector.W), (matrix.M41 * vector.X) + (matrix.M42 * vector.Y) + (matrix.M43 * vector.Z) + (matrix.M44 * vector.W)); }
/// <summary> /// Transform the vectors in the given array. /// </summary> /// <param name="vectors">An array of vectors to transform.</param> /// <param name="transformation">The transformation.</param> /// <remarks> /// This method changes the vector values in the <paramref name="vectors"/> array. /// </remarks> public static void TransformArray(Vector4FArrayList vectors, Matrix4F transformation) { for (int i = 0; i < vectors.Count; i++) { vectors[i] = transformation * vectors[i]; } }
/// <summary> /// Subtracts a matrix from a matrix. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance to subtract from.</param> /// <param name="b">A <see cref="Matrix4F"/> instance to subtract.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the difference.</returns> /// <remarks>result[x][y] = a[x][y] - b[x][y]</remarks> public static Matrix4F Subtract(Matrix4F a, Matrix4F b) { return new Matrix4F( a.M11 - b.M11, a.M12 - b.M12, a.M13 - b.M13, a.M14 - b.M14, a.M21 - b.M21, a.M22 - b.M22, a.M23 - b.M23, a.M24 - b.M24, a.M31 - b.M31, a.M32 - b.M32, a.M33 - b.M33, a.M34 - b.M34, a.M41 - b.M41, a.M42 - b.M42, a.M43 - b.M43, a.M44 - b.M44 ); }
/// <summary> /// Multiplies two matrices. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="b">A <see cref="Matrix4F"/> instance.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the result.</returns> public static Matrix4F operator*(Matrix4F a, Matrix4F b) { return(Matrix4F.Multiply(a, b)); }
/// <summary> /// Transforms a given vector by a matrix. /// </summary> /// <param name="matrix">A <see cref="Matrix4F"/> instance.</param> /// <param name="vector">A <see cref="Vector4F"/> instance.</param> /// <returns>A new <see cref="Vector4F"/> instance containing the result.</returns> public static Vector4F operator*(Matrix4F matrix, Vector4F vector) { return(Matrix4F.Transform(matrix, vector)); }
/// <summary> /// Subtracts a scalar from a matrix. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="s">A <see cref="Matrix4F"/> instance.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the difference.</returns> public static Matrix4F operator-(Matrix4F a, float s) { return(Matrix4F.Subtract(a, s)); }
/// <summary> /// Subtracts a matrix from a matrix. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="b">A <see cref="Matrix4F"/> instance.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the difference.</returns> public static Matrix4F operator-(Matrix4F a, Matrix4F b) { return(Matrix4F.Subtract(a, b)); }
/// <summary> /// Adds a matrix and a scalar. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="s">A scalar.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the sum.</returns> public static Matrix4F operator+(float s, Matrix4F a) { return(Matrix4F.Add(a, s)); }
/// <summary> /// Multiplies two matrices. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="b">A <see cref="Matrix4F"/> instance.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the result.</returns> public static Matrix4F Multiply(Matrix4F a, Matrix4F b) { return new Matrix4F( a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31 + a.M14 * b.M41, a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32 + a.M14 * b.M42, a.M11 * b.M13 + a.M12 * b.M23 + a.M13 * b.M33 + a.M14 * b.M43, a.M11 * b.M14 + a.M12 * b.M24 + a.M13 * b.M34 + a.M14 * b.M44, a.M21 * b.M11 + a.M22 * b.M21 + a.M23 * b.M31 + a.M24 * b.M41, a.M21 * b.M12 + a.M22 * b.M22 + a.M23 * b.M32 + a.M24 * b.M42, a.M21 * b.M13 + a.M22 * b.M23 + a.M23 * b.M33 + a.M24 * b.M43, a.M21 * b.M14 + a.M22 * b.M24 + a.M23 * b.M34 + a.M24 * b.M44, a.M31 * b.M11 + a.M32 * b.M21 + a.M33 * b.M31 + a.M34 * b.M41, a.M31 * b.M12 + a.M32 * b.M22 + a.M33 * b.M32 + a.M34 * b.M42, a.M31 * b.M13 + a.M32 * b.M23 + a.M33 * b.M33 + a.M34 * b.M43, a.M31 * b.M14 + a.M32 * b.M24 + a.M33 * b.M34 + a.M34 * b.M44, a.M41 * b.M11 + a.M42 * b.M21 + a.M43 * b.M31 + a.M44 * b.M41, a.M41 * b.M12 + a.M42 * b.M22 + a.M43 * b.M32 + a.M44 * b.M42, a.M41 * b.M13 + a.M42 * b.M23 + a.M43 * b.M33 + a.M44 * b.M43, a.M41 * b.M14 + a.M42 * b.M24 + a.M43 * b.M34 + a.M44 * b.M44 ); }
/// <summary> /// Transform the vectors in the given array. /// </summary> /// <param name="vectors">An array of vectors to transform.</param> /// <param name="transformation">The transformation.</param> /// <remarks> /// This method changes the vector values in the <paramref name="vectors"/> array. /// </remarks> public static void TransformArray(Vector3FArrayList vectors, Matrix4F transformation) { for (int i = 0; i < vectors.Count; i++) { vectors[i] = Matrix4F.Transform(transformation, vectors[i]); } }
/// <summary> /// Multiplies two matrices and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="b">A <see cref="Matrix4F"/> instance.</param> /// <param name="result">A <see cref="Matrix4F"/> instance to hold the result.</param> public static void Multiply(Matrix4F a, Matrix4F b, ref Matrix4F result) { result.M11 = a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31 + a.M14 * b.M41; result.M12 = a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32 + a.M14 * b.M42; result.M13 = a.M11 * b.M13 + a.M12 * b.M23 + a.M13 * b.M33 + a.M14 * b.M43; result.M14 = a.M11 * b.M14 + a.M12 * b.M24 + a.M13 * b.M34 + a.M14 * b.M44; result.M21 = a.M21 * b.M11 + a.M22 * b.M21 + a.M23 * b.M31 + a.M24 * b.M41; result.M22 = a.M21 * b.M12 + a.M22 * b.M22 + a.M23 * b.M32 + a.M24 * b.M42; result.M23 = a.M21 * b.M13 + a.M22 * b.M23 + a.M23 * b.M33 + a.M24 * b.M43; result.M24 = a.M21 * b.M14 + a.M22 * b.M24 + a.M23 * b.M34 + a.M24 * b.M44; result.M31 = a.M31 * b.M11 + a.M32 * b.M21 + a.M33 * b.M31 + a.M34 * b.M41; result.M32 = a.M31 * b.M12 + a.M32 * b.M22 + a.M33 * b.M32 + a.M34 * b.M42; result.M33 = a.M31 * b.M13 + a.M32 * b.M23 + a.M33 * b.M33 + a.M34 * b.M43; result.M34 = a.M31 * b.M14 + a.M32 * b.M24 + a.M33 * b.M34 + a.M34 * b.M44; result.M41 = a.M41 * b.M11 + a.M42 * b.M21 + a.M43 * b.M31 + a.M44 * b.M41; result.M42 = a.M41 * b.M12 + a.M42 * b.M22 + a.M43 * b.M32 + a.M44 * b.M42; result.M43 = a.M41 * b.M13 + a.M42 * b.M23 + a.M43 * b.M33 + a.M44 * b.M43; result.M44 = a.M41 * b.M14 + a.M42 * b.M24 + a.M43 * b.M34 + a.M44 * b.M44; }
/// <summary> /// Transform the vectors in the given array and put the result in another array. /// </summary> /// <param name="vectors">An array of vectors to transform.</param> /// <param name="transformation">The transformation.</param> /// <param name="result">An array of vectors to put the transformation results in (should be empty).</param> public static void TransformArray(Vector3FArrayList vectors, Matrix4F transformation, Vector3FArrayList result) { for (int i = 0; i < vectors.Count; i++) { result.Add(Matrix4F.Transform(transformation, vectors[i])); } }
/// <summary> /// Subtracts a scalar from a matrix. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="s">A scalar.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the difference.</returns> public static Matrix4F Subtract(Matrix4F a, float s) { return new Matrix4F( a.M11 - s, a.M12 - s, a.M13 - s, a.M14 - s, a.M21 - s, a.M22 - s, a.M23 - s, a.M24 - s, a.M31 - s, a.M32 - s, a.M33 - s, a.M34 - s, a.M41 - s, a.M42 - s, a.M43 - s, a.M44 - s ); }
/// <summary> /// Initializes a new instance of the <see cref="Matrix4F"/> class using a given matrix. /// </summary> public Matrix4F(Matrix4F m) { _m11 = m.M11; _m12 = m.M12; _m13 = m.M13; _m14 = m.M14; _m21 = m.M21; _m22 = m.M22; _m23 = m.M23; _m24 = m.M24; _m31 = m.M31; _m32 = m.M32; _m33 = m.M33; _m34 = m.M34; _m41 = m.M41; _m42 = m.M42; _m43 = m.M43; _m44 = m.M44; }
/// <summary> /// Subtracts a scalar from a matrix and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="s">A scalar.</param> /// <param name="result">A <see cref="Matrix4F"/> instance to hold the result.</param> public static void Subtract(Matrix4F a, float s, ref Matrix4F result) { result.M11 = a.M11 - s; result.M12 = a.M12 - s; result.M13 = a.M13 - s; result.M14 = a.M14 - s; result.M21 = a.M21 - s; result.M22 = a.M22 - s; result.M23 = a.M23 - s; result.M24 = a.M24 - s; result.M31 = a.M31 - s; result.M32 = a.M32 - s; result.M33 = a.M33 - s; result.M34 = a.M34 - s; result.M41 = a.M41 - s; result.M42 = a.M42 - s; result.M43 = a.M43 - s; result.M44 = a.M44 - s; }
/// <summary> /// Adds two matrices. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="b">A <see cref="Matrix4F"/> instance.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the sum.</returns> public static Matrix4F Add(Matrix4F a, Matrix4F b) { return new Matrix4F( a.M11 + b.M11, a.M12 + b.M12, a.M13 + b.M13, a.M14 + b.M14, a.M21 + b.M21, a.M22 + b.M22, a.M23 + b.M23, a.M24 + b.M24, a.M31 + b.M31, a.M32 + b.M32, a.M33 + b.M33, a.M34 + b.M34, a.M41 + b.M41, a.M42 + b.M42, a.M43 + b.M43, a.M44 + b.M44 ); }
/// <summary> /// Transforms a given vector by a matrix and put the result in a vector. /// </summary> /// <param name="matrix">A <see cref="Matrix4F"/> instance.</param> /// <param name="vector">A <see cref="Vector4F"/> instance.</param> /// <param name="result">A <see cref="Vector4F"/> instance to hold the result.</param> public static void Transform(Matrix4F matrix, Vector4F vector, ref Vector4F result) { result.X = (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + (matrix.M14 * vector.W); result.Y = (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + (matrix.M24 * vector.W); result.Z = (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + (matrix.M34 * vector.W); result.W = (matrix.M41 * vector.X) + (matrix.M42 * vector.Y) + (matrix.M43 * vector.Z) + (matrix.M44 * vector.W); }
/// <summary> /// Adds a matrix and a scalar. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="s">A scalar.</param> /// <returns>A new <see cref="Matrix4F"/> instance containing the sum.</returns> public static Matrix4F Add(Matrix4F a, float s) { return new Matrix4F( a.M11 + s, a.M12 + s, a.M13 + s, a.M14 + s, a.M21 + s, a.M22 + s, a.M23 + s, a.M24 + s, a.M31 + s, a.M32 + s, a.M33 + s, a.M34 + s, a.M41 + s, a.M42 + s, a.M43 + s, a.M44 + s ); }
/// <summary> /// Transforms a given vector by a matrix applying the positional matrix components but omitting the projection components and put the result in a vector. /// </summary> /// <param name="matrix">A <see cref="Matrix4F"/> instance.</param> /// <param name="vector">A <see cref="Vector3F"/> instance.</param> /// <param name="result">A <see cref="Vector3F"/> instance to hold the result.</param> public static void Transform(Matrix4F matrix, Vector3F vector, ref Vector3F result) { result.X = (matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + matrix.M14; result.Y = (matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + matrix.M24; result.Z = (matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + matrix.M34; }
/// <summary> /// Adds two matrices and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="b">A <see cref="Matrix4F"/> instance.</param> /// <param name="result">A <see cref="Matrix4F"/> instance to hold the result.</param> public static void Add(Matrix4F a, Matrix4F b, ref Matrix4F result) { result.M11 = a.M11 + b.M11; result.M12 = a.M12 + b.M12; result.M13 = a.M13 + b.M13; result.M14 = a.M14 + b.M14; result.M21 = a.M21 + b.M21; result.M22 = a.M22 + b.M22; result.M23 = a.M23 + b.M23; result.M24 = a.M24 + b.M24; result.M31 = a.M31 + b.M31; result.M32 = a.M32 + b.M32; result.M33 = a.M33 + b.M33; result.M34 = a.M34 + b.M34; result.M41 = a.M41 + b.M41; result.M42 = a.M42 + b.M42; result.M43 = a.M43 + b.M43; result.M44 = a.M44 + b.M44; }
/// <summary> /// Transforms a given vector by a matrix using perspective division and put the result in a vector. /// </summary> /// <remarks> /// Before the matrix multiplication the 3F vector is extended to 4F by setting its W component to 1. /// After the matrix multiplication the resulting 4F vector is transformed to 3F by dividing X, Y, and Z by W. /// (perspective division). /// </remarks> /// <param name="matrix">A <see cref="Matrix4F"/> instance.</param> /// <param name="vector">A <see cref="Vector3F"/> instance.</param> /// <param name="result">A <see cref="Vector3F"/> instance to hold the result.</param> public static void TransformPD(Matrix4F matrix, Vector3F vector, ref Vector3F result) { float w = (matrix.M41 * vector.X) + (matrix.M42 * vector.Y) + (matrix.M43 * vector.Z) + matrix.M44; result.X = ((matrix.M11 * vector.X) + (matrix.M12 * vector.Y) + (matrix.M13 * vector.Z) + matrix.M14) / w; result.Y = ((matrix.M21 * vector.X) + (matrix.M22 * vector.Y) + (matrix.M23 * vector.Z) + matrix.M24) / w; result.Z = ((matrix.M31 * vector.X) + (matrix.M32 * vector.Y) + (matrix.M33 * vector.Z) + matrix.M34) / w; }
/// <summary> /// Adds a matrix and a scalar and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="Matrix4F"/> instance.</param> /// <param name="s">A scalar.</param> /// <param name="result">A <see cref="Matrix4F"/> instance to hold the result.</param> public static void Add(Matrix4F a, float s, ref Matrix4F result) { result.M11 = a.M11 + s; result.M12 = a.M12 + s; result.M13 = a.M13 + s; result.M14 = a.M14 + s; result.M21 = a.M21 + s; result.M22 = a.M22 + s; result.M23 = a.M23 + s; result.M24 = a.M24 + s; result.M31 = a.M31 + s; result.M32 = a.M32 + s; result.M33 = a.M33 + s; result.M34 = a.M34 + s; result.M41 = a.M41 + s; result.M42 = a.M42 + s; result.M43 = a.M43 + s; result.M44 = a.M44 + s; }
/// <summary> /// Calculates the adjoint of the matrix. /// </summary> /// <returns>A <see cref="Matrix4F"/> instance containing the adjoint of the matrix.</returns> public Matrix4F Adjoint() { Matrix4F result = new Matrix4F(); for (int row = 0; row < 4; row++) { for (int col = 0; col < 4; col++) { if (((col+row) % 2) == 0) result[row, col] = Minor(col, row).Determinant(); else result[row, col] = -Minor(col, row).Determinant(); } } return result; }