From 2f9ccbc3af0b930a3b8abb7fda1ff1cccfe29d02 Mon Sep 17 00:00:00 2001 From: Robert Rouhani Date: Sat, 26 Jan 2013 16:08:34 -0500 Subject: [PATCH] Converted tabs to spaces. --- Source/OpenTK/Math/Matrix2d.cs | 1426 ++++++++++++------------- Source/OpenTK/Math/Matrix2x3d.cs | 1346 ++++++++++++------------ Source/OpenTK/Math/Matrix2x4d.cs | 1420 ++++++++++++------------- Source/OpenTK/Math/Matrix3x2d.cs | 1370 ++++++++++++------------ Source/OpenTK/Math/Matrix3x4.cs | 1582 ++++++++++++++-------------- Source/OpenTK/Math/Matrix3x4d.cs | 1582 ++++++++++++++-------------- Source/OpenTK/Math/Matrix4d.cs | 54 +- Source/OpenTK/Math/Matrix4x2.cs | 1470 +++++++++++++------------- Source/OpenTK/Math/Matrix4x2d.cs | 1470 +++++++++++++------------- Source/OpenTK/Math/Matrix4x3.cs | 1680 +++++++++++++++--------------- Source/OpenTK/Math/Matrix4x3d.cs | 1670 ++++++++++++++--------------- 11 files changed, 7540 insertions(+), 7530 deletions(-) diff --git a/Source/OpenTK/Math/Matrix2d.cs b/Source/OpenTK/Math/Matrix2d.cs index beef7a28..aefef23d 100644 --- a/Source/OpenTK/Math/Matrix2d.cs +++ b/Source/OpenTK/Math/Matrix2d.cs @@ -27,717 +27,717 @@ using System.Runtime.InteropServices; namespace OpenTK { - /// - /// Represents a 2x2 matrix - /// - public struct Matrix2d : IEquatable - { - #region Fields - - /// - /// Top row of the matrix. - /// - public Vector2d Row0; - - /// - /// Bottom row of the matrix. - /// - public Vector2d Row1; - - /// - /// The identity matrix. - /// - public static readonly Matrix2d Identity = new Matrix2d(Vector2d.UnitX, Vector2d.UnitY); - - /// - /// The zero matrix. - /// - public static readonly Matrix2d Zero = new Matrix2d(Vector2d.Zero, Vector2d.Zero); - - #endregion - - #region Constructors - - /// - /// Constructs a new instance. - /// - /// Top row of the matrix. - /// Bottom row of the matrix. - public Matrix2d(Vector2d row0, Vector2d row1) - { - Row0 = row0; - Row1 = row1; - } - - /// - /// Constructs a new instance - /// - /// First item of the first row of the matrix. - /// Second item of the first row of the matrix. - /// First item of the second row of the matrix. - /// Second item of the second row of the matrix. - public Matrix2d( - double m00, double m01, - double m10, double m11) - { - Row0 = new Vector2d(m00, m01); - Row1 = new Vector2d(m10, m11); - } - - #endregion - - #region Public Members - - #region Properties - - /// - /// Gets the determinant of this matrix. - /// - public double Determinant - { - get - { - double m11 = Row0.X, m12 = Row0.Y, - m21 = Row1.X, m22 = Row1.Y; - - return m11 * m22 - m12 * m21; - } - } - - /// - /// Gets or sets the first column of this matrix. - /// - public Vector2d Column0 - { - get { return new Vector2d(Row0.X, Row1.X); } - set { Row0.X = value.X; Row1.X = value.Y; } - } - - /// - /// Gets or sets the second column of this matrix. - /// - public Vector2d Column1 - { - get { return new Vector2d(Row0.Y, Row1.Y); } - set { Row0.Y = value.X; Row1.Y = value.Y; } - } - - /// - /// Gets or sets the value at row 1, column 1 of this instance. - /// - public double M11 { get { return Row0.X; } set { Row0.X = value; } } - - /// - /// Gets or sets the value at row 1, column 2 of this instance. - /// - public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } - - /// - /// Gets or sets the value at row 2, column 1 of this instance. - /// - public double M21 { get { return Row1.X; } set { Row1.X = value; } } - - /// - /// Gets or sets the value at row 2, column 2 of this instance. - /// - public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } - - #endregion - - #region Indexers - - /// - /// Gets or sets the value at a specified row and column. - /// - public double this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } - - #endregion - - #region Instance - - #region public void Transpose() - - /// - /// Converts this instance to it's transpose. - /// - public void Transpose() - { - this = Matrix2d.Transpose(this); - } - - #endregion - - #region public void Invert() - - /// - /// Converts this instance into its inverse. - /// - public void Invert() - { - this = Matrix2d.Invert(this); - } - - #endregion - - #endregion - - #region Static - - #region CreateRotation - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix2d instance. - public static void CreateRotation(double angle, out Matrix2d result) - { - double cos = (double)System.Math.Cos(angle); - double sin = (double)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = sin; - result.Row1.X = -sin; - result.Row1.Y = cos; - } - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix2d instance. - public static Matrix2d CreateRotation(double angle) - { - Matrix2d result; - CreateRotation(angle, out result); - return result; - } - - #endregion - - #region CreateScale - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x, y, and z axes. - /// A scale matrix. - public static void CreateScale(double scale, out Matrix2d result) - { - result.Row0.X = scale; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = scale; - } - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x and y axes. - /// A scale matrix. - public static Matrix2d CreateScale(double scale) - { - Matrix2d result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static void CreateScale(Vector2d scale, out Matrix2d result) - { - result.Row0.X = scale.X; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = scale.Y; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static Matrix2d CreateScale(Vector2d scale) - { - Matrix2d result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static void CreateScale(double x, double y, out Matrix2d result) - { - result.Row0.X = x; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = y; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static Matrix2d CreateScale(double x, double y) - { - Matrix2d result; - CreateScale(x, y, out result); - return result; - } - - #endregion - - #region Multiply Functions - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2d left, double right, out Matrix2d result) - { - result.Row0.X = left.Row0.X * right; - result.Row0.Y = left.Row0.Y * right; - result.Row1.X = left.Row1.X * right; - result.Row1.Y = left.Row1.Y * right; - } - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2d Mult(Matrix2d left, double right) - { - Matrix2d result; - Mult(ref left, right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2d left, ref Matrix2d right, out Matrix2d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, - rM21 = right.Row1.X, rM22 = right.Row1.Y; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2d Mult(Matrix2d left, Matrix2d right) - { - Matrix2d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2d left, ref Matrix2x3d right, out Matrix2x3d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2x3d Mult(Matrix2d left, Matrix2x3d right) - { - Matrix2x3d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2d left, ref Matrix2x4d right, out Matrix2x4d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); - result.Row0.W = (lM11 * rM14) + (lM12 * rM24); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); - result.Row1.W = (lM21 * rM14) + (lM22 * rM24); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2x4d Mult(Matrix2d left, Matrix2x4d right) - { - Matrix2x4d result; - Mult(ref left, ref right, out result); - return result; - } - - #endregion - - #region Add - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static void Add(ref Matrix2d left, ref Matrix2d right, out Matrix2d result) - { - result.Row0.X = left.Row0.X + right.Row0.X; - result.Row0.Y = left.Row0.Y + right.Row0.Y; - result.Row1.X = left.Row1.X + right.Row1.X; - result.Row1.Y = left.Row1.Y + right.Row1.Y; - } - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static Matrix2d Add(Matrix2d left, Matrix2d right) - { - Matrix2d result; - Add(ref left, ref right, out result); - return result; - } - - #endregion - - #region Subtract - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static void Subtract(ref Matrix2d left, ref Matrix2d right, out Matrix2d result) - { - result.Row0.X = left.Row0.X - right.Row0.X; - result.Row0.Y = left.Row0.Y - right.Row0.Y; - result.Row1.X = left.Row1.X - right.Row1.X; - result.Row1.Y = left.Row1.Y - right.Row1.Y; - } - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static Matrix2d Subtract(Matrix2d left, Matrix2d right) - { - Matrix2d result; - Subtract(ref left, ref right, out result); - return result; - } - - #endregion - - #region Invert Functions - - /// - /// Calculate the inverse of the given matrix - /// - /// The matrix to invert - /// The inverse of the given matrix if it has one, or the input if it is singular - /// Thrown if the Matrix2d is singular. - public static void Invert(ref Matrix2d mat, out Matrix2d result) - { - double det = mat.Determinant; - - if (det == 0) - throw new InvalidOperationException("Matrix is singular and cannot be inverted."); - - double invDet = 1f / det; - - result.Row0.X = mat.Row1.Y * invDet; - result.Row0.Y = -mat.Row0.Y * invDet; - result.Row1.X = -mat.Row1.X * invDet; - result.Row1.Y = mat.Row0.X * invDet; - } - - /// - /// Calculate the inverse of the given matrix - /// - /// The matrix to invert - /// The inverse of the given matrix if it has one, or the input if it is singular - /// Thrown if the Matrix2d is singular. - public static Matrix2d Invert(Matrix2d mat) - { - Matrix2d result; - Invert(ref mat, out result); - return result; - } - - #endregion - - #region Transpose - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static void Transpose(ref Matrix2d mat, out Matrix2d result) - { - result.Row0.X = mat.Row0.X; - result.Row0.Y = mat.Row1.X; - result.Row1.X = mat.Row0.Y; - result.Row1.Y = mat.Row1.Y; - } - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static Matrix2d Transpose(Matrix2d mat) - { - Matrix2d result; - Transpose(ref mat, out result); - return result; - } - - #endregion - - #endregion - - #region Operators - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2d which holds the result of the multiplication - public static Matrix2d operator *(double left, Matrix2d right) - { - return Mult(right, left); - } - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2d which holds the result of the multiplication - public static Matrix2d operator *(Matrix2d left, double right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2d which holds the result of the multiplication - public static Matrix2d operator *(Matrix2d left, Matrix2d right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x3d which holds the result of the multiplication - public static Matrix2x3d operator *(Matrix2d left, Matrix2x3d right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x4d which holds the result of the multiplication - public static Matrix2x4d operator *(Matrix2d left, Matrix2x4d right) - { - return Mult(left, right); - } - - /// - /// Matrix addition - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2d which holds the result of the addition - public static Matrix2d operator +(Matrix2d left, Matrix2d right) - { - return Add(left, right); - } - - /// - /// Matrix subtraction - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2d which holds the result of the subtraction - public static Matrix2d operator -(Matrix2d left, Matrix2d right) - { - return Subtract(left, right); - } - - /// - /// Compares two instances for equality. - /// - /// The first instance. - /// The second instance. - /// True, if left equals right; false otherwise. - public static bool operator ==(Matrix2d left, Matrix2d right) - { - return left.Equals(right); - } - - /// - /// Compares two instances for inequality. - /// - /// The first instance. - /// The second instance. - /// True, if left does not equal right; false otherwise. - public static bool operator !=(Matrix2d left, Matrix2d right) - { - return !left.Equals(right); - } - - #endregion - - #region Overrides - - #region public override string ToString() - - /// - /// Returns a System.String that represents the current Matrix4. - /// - /// The string representation of the matrix. - public override string ToString() - { - return String.Format("{0}\n{1}", Row0, Row1); - } - - #endregion - - #region public override int GetHashCode() - - /// - /// Returns the hashcode for this instance. - /// - /// A System.Int32 containing the unique hashcode for this instance. - public override int GetHashCode() - { - return Row0.GetHashCode() ^ Row1.GetHashCode(); - } - - #endregion - - #region public override bool Equals(object obj) - - /// - /// Indicates whether this instance and a specified object are equal. - /// - /// The object to compare to. - /// True if the instances are equal; false otherwise. - public override bool Equals(object obj) - { - if (!(obj is Matrix2d)) - return false; - - return this.Equals((Matrix2d)obj); - } - - #endregion - - #endregion - - #endregion - - #region IEquatable Members - - /// Indicates whether the current matrix is equal to another matrix. - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(Matrix2d other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1; - } - - #endregion - } + /// + /// Represents a 2x2 matrix + /// + public struct Matrix2d : IEquatable + { + #region Fields + + /// + /// Top row of the matrix. + /// + public Vector2d Row0; + + /// + /// Bottom row of the matrix. + /// + public Vector2d Row1; + + /// + /// The identity matrix. + /// + public static readonly Matrix2d Identity = new Matrix2d(Vector2d.UnitX, Vector2d.UnitY); + + /// + /// The zero matrix. + /// + public static readonly Matrix2d Zero = new Matrix2d(Vector2d.Zero, Vector2d.Zero); + + #endregion + + #region Constructors + + /// + /// Constructs a new instance. + /// + /// Top row of the matrix. + /// Bottom row of the matrix. + public Matrix2d(Vector2d row0, Vector2d row1) + { + Row0 = row0; + Row1 = row1; + } + + /// + /// Constructs a new instance + /// + /// First item of the first row of the matrix. + /// Second item of the first row of the matrix. + /// First item of the second row of the matrix. + /// Second item of the second row of the matrix. + public Matrix2d( + double m00, double m01, + double m10, double m11) + { + Row0 = new Vector2d(m00, m01); + Row1 = new Vector2d(m10, m11); + } + + #endregion + + #region Public Members + + #region Properties + + /// + /// Gets the determinant of this matrix. + /// + public double Determinant + { + get + { + double m11 = Row0.X, m12 = Row0.Y, + m21 = Row1.X, m22 = Row1.Y; + + return m11 * m22 - m12 * m21; + } + } + + /// + /// Gets or sets the first column of this matrix. + /// + public Vector2d Column0 + { + get { return new Vector2d(Row0.X, Row1.X); } + set { Row0.X = value.X; Row1.X = value.Y; } + } + + /// + /// Gets or sets the second column of this matrix. + /// + public Vector2d Column1 + { + get { return new Vector2d(Row0.Y, Row1.Y); } + set { Row0.Y = value.X; Row1.Y = value.Y; } + } + + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public double M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public double M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + #endregion + + #region Indexers + + /// + /// Gets or sets the value at a specified row and column. + /// + public double this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } + + #endregion + + #region Instance + + #region public void Transpose() + + /// + /// Converts this instance to it's transpose. + /// + public void Transpose() + { + this = Matrix2d.Transpose(this); + } + + #endregion + + #region public void Invert() + + /// + /// Converts this instance into its inverse. + /// + public void Invert() + { + this = Matrix2d.Invert(this); + } + + #endregion + + #endregion + + #region Static + + #region CreateRotation + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix2d instance. + public static void CreateRotation(double angle, out Matrix2d result) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = sin; + result.Row1.X = -sin; + result.Row1.Y = cos; + } + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix2d instance. + public static Matrix2d CreateRotation(double angle) + { + Matrix2d result; + CreateRotation(angle, out result); + return result; + } + + #endregion + + #region CreateScale + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x, y, and z axes. + /// A scale matrix. + public static void CreateScale(double scale, out Matrix2d result) + { + result.Row0.X = scale; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = scale; + } + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x and y axes. + /// A scale matrix. + public static Matrix2d CreateScale(double scale) + { + Matrix2d result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static void CreateScale(Vector2d scale, out Matrix2d result) + { + result.Row0.X = scale.X; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = scale.Y; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static Matrix2d CreateScale(Vector2d scale) + { + Matrix2d result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static void CreateScale(double x, double y, out Matrix2d result) + { + result.Row0.X = x; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = y; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static Matrix2d CreateScale(double x, double y) + { + Matrix2d result; + CreateScale(x, y, out result); + return result; + } + + #endregion + + #region Multiply Functions + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2d left, double right, out Matrix2d result) + { + result.Row0.X = left.Row0.X * right; + result.Row0.Y = left.Row0.Y * right; + result.Row1.X = left.Row1.X * right; + result.Row1.Y = left.Row1.Y * right; + } + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2d Mult(Matrix2d left, double right) + { + Matrix2d result; + Mult(ref left, right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2d left, ref Matrix2d right, out Matrix2d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, + rM21 = right.Row1.X, rM22 = right.Row1.Y; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2d Mult(Matrix2d left, Matrix2d right) + { + Matrix2d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2d left, ref Matrix2x3d right, out Matrix2x3d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2x3d Mult(Matrix2d left, Matrix2x3d right) + { + Matrix2x3d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2d left, ref Matrix2x4d right, out Matrix2x4d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); + result.Row0.W = (lM11 * rM14) + (lM12 * rM24); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); + result.Row1.W = (lM21 * rM14) + (lM22 * rM24); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2x4d Mult(Matrix2d left, Matrix2x4d right) + { + Matrix2x4d result; + Mult(ref left, ref right, out result); + return result; + } + + #endregion + + #region Add + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static void Add(ref Matrix2d left, ref Matrix2d right, out Matrix2d result) + { + result.Row0.X = left.Row0.X + right.Row0.X; + result.Row0.Y = left.Row0.Y + right.Row0.Y; + result.Row1.X = left.Row1.X + right.Row1.X; + result.Row1.Y = left.Row1.Y + right.Row1.Y; + } + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static Matrix2d Add(Matrix2d left, Matrix2d right) + { + Matrix2d result; + Add(ref left, ref right, out result); + return result; + } + + #endregion + + #region Subtract + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static void Subtract(ref Matrix2d left, ref Matrix2d right, out Matrix2d result) + { + result.Row0.X = left.Row0.X - right.Row0.X; + result.Row0.Y = left.Row0.Y - right.Row0.Y; + result.Row1.X = left.Row1.X - right.Row1.X; + result.Row1.Y = left.Row1.Y - right.Row1.Y; + } + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static Matrix2d Subtract(Matrix2d left, Matrix2d right) + { + Matrix2d result; + Subtract(ref left, ref right, out result); + return result; + } + + #endregion + + #region Invert Functions + + /// + /// Calculate the inverse of the given matrix + /// + /// The matrix to invert + /// The inverse of the given matrix if it has one, or the input if it is singular + /// Thrown if the Matrix2d is singular. + public static void Invert(ref Matrix2d mat, out Matrix2d result) + { + double det = mat.Determinant; + + if (det == 0) + throw new InvalidOperationException("Matrix is singular and cannot be inverted."); + + double invDet = 1f / det; + + result.Row0.X = mat.Row1.Y * invDet; + result.Row0.Y = -mat.Row0.Y * invDet; + result.Row1.X = -mat.Row1.X * invDet; + result.Row1.Y = mat.Row0.X * invDet; + } + + /// + /// Calculate the inverse of the given matrix + /// + /// The matrix to invert + /// The inverse of the given matrix if it has one, or the input if it is singular + /// Thrown if the Matrix2d is singular. + public static Matrix2d Invert(Matrix2d mat) + { + Matrix2d result; + Invert(ref mat, out result); + return result; + } + + #endregion + + #region Transpose + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static void Transpose(ref Matrix2d mat, out Matrix2d result) + { + result.Row0.X = mat.Row0.X; + result.Row0.Y = mat.Row1.X; + result.Row1.X = mat.Row0.Y; + result.Row1.Y = mat.Row1.Y; + } + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static Matrix2d Transpose(Matrix2d mat) + { + Matrix2d result; + Transpose(ref mat, out result); + return result; + } + + #endregion + + #endregion + + #region Operators + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2d which holds the result of the multiplication + public static Matrix2d operator *(double left, Matrix2d right) + { + return Mult(right, left); + } + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2d which holds the result of the multiplication + public static Matrix2d operator *(Matrix2d left, double right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2d which holds the result of the multiplication + public static Matrix2d operator *(Matrix2d left, Matrix2d right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x3d which holds the result of the multiplication + public static Matrix2x3d operator *(Matrix2d left, Matrix2x3d right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x4d which holds the result of the multiplication + public static Matrix2x4d operator *(Matrix2d left, Matrix2x4d right) + { + return Mult(left, right); + } + + /// + /// Matrix addition + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2d which holds the result of the addition + public static Matrix2d operator +(Matrix2d left, Matrix2d right) + { + return Add(left, right); + } + + /// + /// Matrix subtraction + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2d which holds the result of the subtraction + public static Matrix2d operator -(Matrix2d left, Matrix2d right) + { + return Subtract(left, right); + } + + /// + /// Compares two instances for equality. + /// + /// The first instance. + /// The second instance. + /// True, if left equals right; false otherwise. + public static bool operator ==(Matrix2d left, Matrix2d right) + { + return left.Equals(right); + } + + /// + /// Compares two instances for inequality. + /// + /// The first instance. + /// The second instance. + /// True, if left does not equal right; false otherwise. + public static bool operator !=(Matrix2d left, Matrix2d right) + { + return !left.Equals(right); + } + + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix4. + /// + /// The string representation of the matrix. + public override string ToString() + { + return String.Format("{0}\n{1}", Row0, Row1); + } + + #endregion + + #region public override int GetHashCode() + + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode(); + } + + #endregion + + #region public override bool Equals(object obj) + + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare to. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Matrix2d)) + return false; + + return this.Equals((Matrix2d)obj); + } + + #endregion + + #endregion + + #endregion + + #region IEquatable Members + + /// Indicates whether the current matrix is equal to another matrix. + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix2d other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1; + } + + #endregion + } } diff --git a/Source/OpenTK/Math/Matrix2x3d.cs b/Source/OpenTK/Math/Matrix2x3d.cs index 0cc907b8..3e592d5a 100644 --- a/Source/OpenTK/Math/Matrix2x3d.cs +++ b/Source/OpenTK/Math/Matrix2x3d.cs @@ -27,677 +27,677 @@ using System.Collections.Generic; namespace OpenTK { - /// - /// Represents a 2x3 matrix. - /// - public struct Matrix2x3d : IEquatable - { - #region Fields - - /// - /// Top row of the matrix. - /// - public Vector3d Row0; - - /// - /// Bottom row of the matrix. - /// - public Vector3d Row1; - - /// - /// The zero matrix. - /// - public static readonly Matrix2x3d Zero = new Matrix2x3d(Vector3d.Zero, Vector3d.Zero); - - #endregion - - #region Constructors - - /// - /// Constructs a new instance. - /// - /// Top row of the matrix. - /// Bottom row of the matrix. - public Matrix2x3d(Vector3d row0, Vector3d row1) - { - Row0 = row0; - Row1 = row1; - } - - /// - /// Constructs a new instance - /// - /// First item of the first row of the matrix. - /// Second item of the first row of the matrix. - /// Third item of the first row of the matrix. - /// First item of the second row of the matrix. - /// Second item of the second row of the matrix. - /// Third item of the second row of the matrix. - public Matrix2x3d( - double m00, double m01, double m02, - double m10, double m11, double m12) - { - Row0 = new Vector3d(m00, m01, m02); - Row1 = new Vector3d(m10, m11, m12); - } - - #endregion - - #region Public Members - - #region Properties - - /// - /// Gets or sets the first column of this matrix. - /// - public Vector2d Column0 - { - get { return new Vector2d(Row0.X, Row1.X); } - set { Row0.X = value.X; Row1.X = value.Y; } - } - - /// - /// Gets or sets the second column of this matrix. - /// - public Vector2d Column1 - { - get { return new Vector2d(Row0.Y, Row1.Y); } - set { Row0.Y = value.X; Row1.Y = value.Y; } - } - - /// - /// Gets or sets the third column of this matrix. - /// - public Vector2d Column2 - { - get { return new Vector2d(Row0.Z, Row1.Z); } - set { Row0.Z = value.X; Row1.Z = value.Y; } - } - - /// - /// Gets or sets the value at row 1, column 1 of this instance. - /// - public double M11 { get { return Row0.X; } set { Row0.X = value; } } - - /// - /// Gets or sets the value at row 1, column 2 of this instance. - /// - public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } - - /// - /// Gets or sets the value at row 1, column 3 of this instance. - /// - public double M13 { get { return Row0.Z; } set { Row0.Z = value; } } - - /// - /// Gets or sets the value at row 2, column 1 of this instance. - /// - public double M21 { get { return Row1.X; } set { Row1.X = value; } } - - /// - /// Gets or sets the value at row 2, column 2 of this instance. - /// - public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } - - /// - /// Gets or sets the value at row 2, column 3 of this instance. - /// - public double M23 { get { return Row1.Z; } set { Row1.Z = value; } } - - #endregion - - #region Indexers - - /// - /// Gets or sets the value at a specified row and column. - /// - public double this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } - - #endregion - - #region Instance - #endregion - - #region Static - - #region CreateRotation - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix2x3d instance. - public static void CreateRotation(double angle, out Matrix2x3d result) - { - double cos = System.Math.Cos(angle); - double sin = System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = sin; - result.Row0.Z = 0; - result.Row1.X = -sin; - result.Row1.Y = cos; - result.Row1.Z = 0; - } - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix2x3d instance. - public static Matrix2x3d CreateRotation(double angle) - { - Matrix2x3d result; - CreateRotation(angle, out result); - return result; - } - - #endregion - - #region CreateScale - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x, y, and z axes. - /// A scale matrix. - public static void CreateScale(double scale, out Matrix2x3d result) - { - result.Row0.X = scale; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = scale; - result.Row1.Z = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x and y axes. - /// A scale matrix. - public static Matrix2x3d CreateScale(double scale) - { - Matrix2x3d result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static void CreateScale(Vector2d scale, out Matrix2x3d result) - { - result.Row0.X = scale.X; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = scale.Y; - result.Row1.Z = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static Matrix2x3d CreateScale(Vector2d scale) - { - Matrix2x3d result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static void CreateScale(double x, double y, out Matrix2x3d result) - { - result.Row0.X = x; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = y; - result.Row1.Z = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static Matrix2x3d CreateScale(double x, double y) - { - Matrix2x3d result; - CreateScale(x, y, out result); - return result; - } - - #endregion - - #region Multiply Functions - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2x3d left, double right, out Matrix2x3d result) - { - result.Row0.X = left.Row0.X * right; - result.Row0.Y = left.Row0.Y * right; - result.Row0.Z = left.Row0.Z * right; - result.Row1.X = left.Row1.X * right; - result.Row1.Y = left.Row1.Y * right; - result.Row1.Z = left.Row1.Z * right; - } - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2x3d Mult(Matrix2x3d left, double right) - { - Matrix2x3d result; - Mult(ref left, right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2x3d left, ref Matrix3x2 right, out Matrix2d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, - rM11 = right.Row0.X, rM12 = right.Row0.Y, - rM21 = right.Row1.X, rM22 = right.Row1.Y, - rM31 = right.Row2.X, rM32 = right.Row2.Y; - - result.Row0.X = ((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31); - result.Row0.Y = ((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rM32); - result.Row1.X = ((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31); - result.Row1.Y = ((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rM32); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2d Mult(Matrix2x3d left, Matrix3x2 right) - { - Matrix2d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2x3d left, ref Matrix3 right, out Matrix2x3d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, - rM31 = right.Row2.X, rm32 = right.Row2.Y, rM33 = right.Row2.Z; - - result.Row0.X = ((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31); - result.Row0.Y = ((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rm32); - result.Row0.Z = ((lM11 * rM13) + (lM12 * rM23)) + (lM13 * rM33); - result.Row1.X = ((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31); - result.Row1.Y = ((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rm32); - result.Row1.Z = ((lM21 * rM13) + (lM22 * rM23)) + (lM23 * rM33); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2x3d Mult(Matrix2x3d left, Matrix3 right) - { - Matrix2x3d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2x3d left, ref Matrix3x4 right, out Matrix2x4d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, - rM31 = right.Row2.X, rm32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W; - - result.Row0.X = ((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31); - result.Row0.Y = ((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rm32); - result.Row0.Z = ((lM11 * rM13) + (lM12 * rM23)) + (lM13 * rM33); - result.Row0.W = ((lM11 * rM14) + (lM12 * rM24)) + (lM13 * rM34); - result.Row1.X = ((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31); - result.Row1.Y = ((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rm32); - result.Row1.Z = ((lM21 * rM13) + (lM22 * rM23)) + (lM23 * rM33); - result.Row1.W = ((lM21 * rM14) + (lM22 * rM24)) + (lM23 * rM34); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2x4d Mult(Matrix2x3d left, Matrix3x4 right) - { - Matrix2x4d result; - Mult(ref left, ref right, out result); - return result; - } - - #endregion - - #region Add - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static void Add(ref Matrix2x3d left, ref Matrix2x3d right, out Matrix2x3d result) - { - result.Row0.X = left.Row0.X + right.Row0.X; - result.Row0.Y = left.Row0.Y + right.Row0.Y; - result.Row0.Z = left.Row0.Z + right.Row0.Z; - result.Row1.X = left.Row1.X + right.Row1.X; - result.Row1.Y = left.Row1.Y + right.Row1.Y; - result.Row1.Z = left.Row1.Z + right.Row1.Z; - } - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static Matrix2x3d Add(Matrix2x3d left, Matrix2x3d right) - { - Matrix2x3d result; - Add(ref left, ref right, out result); - return result; - } - - #endregion - - #region Subtract - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static void Subtract(ref Matrix2x3d left, ref Matrix2x3d right, out Matrix2x3d result) - { - result.Row0.X = left.Row0.X - right.Row0.X; - result.Row0.Y = left.Row0.Y - right.Row0.Y; - result.Row0.Z = left.Row0.Z - right.Row0.Z; - result.Row1.X = left.Row1.X - right.Row1.X; - result.Row1.Y = left.Row1.Y - right.Row1.Y; - result.Row1.Z = left.Row1.Z - right.Row1.Z; - } - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static Matrix2x3d Subtract(Matrix2x3d left, Matrix2x3d right) - { - Matrix2x3d result; - Subtract(ref left, ref right, out result); - return result; - } - - #endregion - - #region Transpose - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static void Transpose(ref Matrix2x3d mat, out Matrix3x2d result) - { - result.Row0.X = mat.Row0.X; - result.Row0.Y = mat.Row1.X; - result.Row1.X = mat.Row0.Y; - result.Row1.Y = mat.Row1.Y; - result.Row2.X = mat.Row0.Z; - result.Row2.Y = mat.Row1.Z; - } - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static Matrix3x2d Transpose(Matrix2x3d mat) - { - Matrix3x2d result; - Transpose(ref mat, out result); - return result; - } - - #endregion - - #endregion - - #region Operators - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x3d which holds the result of the multiplication - public static Matrix2x3d operator *(double left, Matrix2x3d right) - { - return Mult(right, left); - } - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x3d which holds the result of the multiplication - public static Matrix2x3d operator *(Matrix2x3d left, double right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2d which holds the result of the multiplication - public static Matrix2d operator *(Matrix2x3d left, Matrix3x2 right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x3d which holds the result of the multiplication - public static Matrix2x3d operator *(Matrix2x3d left, Matrix3 right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x4d which holds the result of the multiplication - public static Matrix2x4d operator *(Matrix2x3d left, Matrix3x4 right) - { - return Mult(left, right); - } - - /// - /// Matrix addition - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x3d which holds the result of the addition - public static Matrix2x3d operator +(Matrix2x3d left, Matrix2x3d right) - { - return Add(left, right); - } - - /// - /// Matrix subtraction - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x3d which holds the result of the subtraction - public static Matrix2x3d operator -(Matrix2x3d left, Matrix2x3d right) - { - return Subtract(left, right); - } - - /// - /// Compares two instances for equality. - /// - /// The first instance. - /// The second instance. - /// True, if left equals right; false otherwise. - public static bool operator ==(Matrix2x3d left, Matrix2x3d right) - { - return left.Equals(right); - } - - /// - /// Compares two instances for inequality. - /// - /// The first instance. - /// The second instance. - /// True, if left does not equal right; false otherwise. - public static bool operator !=(Matrix2x3d left, Matrix2x3d right) - { - return !left.Equals(right); - } - - #endregion - - #region Overrides - - #region public override string ToString() - - /// - /// Returns a System.String that represents the current Matrix2x3d. - /// - /// The string representation of the matrix. - public override string ToString() - { - return String.Format("{0}\n{1}", Row0, Row1); - } - - #endregion - - #region public override int GetHashCode() - - /// - /// Returns the hashcode for this instance. - /// - /// A System.Int32 containing the unique hashcode for this instance. - public override int GetHashCode() - { - return Row0.GetHashCode() ^ Row1.GetHashCode(); - } - - #endregion - - #region public override bool Equals(object obj) - - /// - /// Indicates whether this instance and a specified object are equal. - /// - /// The object to compare tresult. - /// True if the instances are equal; false otherwise. - public override bool Equals(object obj) - { - if (!(obj is Matrix2x3d)) - return false; - - return this.Equals((Matrix2x3d)obj); - } - - #endregion - - #endregion - - #endregion - - #region IEquatable Members - - /// - /// Indicates whether the current matrix is equal to another matrix. - /// - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(Matrix2x3d other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1; - } - - #endregion - } + /// + /// Represents a 2x3 matrix. + /// + public struct Matrix2x3d : IEquatable + { + #region Fields + + /// + /// Top row of the matrix. + /// + public Vector3d Row0; + + /// + /// Bottom row of the matrix. + /// + public Vector3d Row1; + + /// + /// The zero matrix. + /// + public static readonly Matrix2x3d Zero = new Matrix2x3d(Vector3d.Zero, Vector3d.Zero); + + #endregion + + #region Constructors + + /// + /// Constructs a new instance. + /// + /// Top row of the matrix. + /// Bottom row of the matrix. + public Matrix2x3d(Vector3d row0, Vector3d row1) + { + Row0 = row0; + Row1 = row1; + } + + /// + /// Constructs a new instance + /// + /// First item of the first row of the matrix. + /// Second item of the first row of the matrix. + /// Third item of the first row of the matrix. + /// First item of the second row of the matrix. + /// Second item of the second row of the matrix. + /// Third item of the second row of the matrix. + public Matrix2x3d( + double m00, double m01, double m02, + double m10, double m11, double m12) + { + Row0 = new Vector3d(m00, m01, m02); + Row1 = new Vector3d(m10, m11, m12); + } + + #endregion + + #region Public Members + + #region Properties + + /// + /// Gets or sets the first column of this matrix. + /// + public Vector2d Column0 + { + get { return new Vector2d(Row0.X, Row1.X); } + set { Row0.X = value.X; Row1.X = value.Y; } + } + + /// + /// Gets or sets the second column of this matrix. + /// + public Vector2d Column1 + { + get { return new Vector2d(Row0.Y, Row1.Y); } + set { Row0.Y = value.X; Row1.Y = value.Y; } + } + + /// + /// Gets or sets the third column of this matrix. + /// + public Vector2d Column2 + { + get { return new Vector2d(Row0.Z, Row1.Z); } + set { Row0.Z = value.X; Row1.Z = value.Y; } + } + + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public double M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 1, column 3 of this instance. + /// + public double M13 { get { return Row0.Z; } set { Row0.Z = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public double M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 3 of this instance. + /// + public double M23 { get { return Row1.Z; } set { Row1.Z = value; } } + + #endregion + + #region Indexers + + /// + /// Gets or sets the value at a specified row and column. + /// + public double this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } + + #endregion + + #region Instance + #endregion + + #region Static + + #region CreateRotation + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix2x3d instance. + public static void CreateRotation(double angle, out Matrix2x3d result) + { + double cos = System.Math.Cos(angle); + double sin = System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = sin; + result.Row0.Z = 0; + result.Row1.X = -sin; + result.Row1.Y = cos; + result.Row1.Z = 0; + } + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix2x3d instance. + public static Matrix2x3d CreateRotation(double angle) + { + Matrix2x3d result; + CreateRotation(angle, out result); + return result; + } + + #endregion + + #region CreateScale + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x, y, and z axes. + /// A scale matrix. + public static void CreateScale(double scale, out Matrix2x3d result) + { + result.Row0.X = scale; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = scale; + result.Row1.Z = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x and y axes. + /// A scale matrix. + public static Matrix2x3d CreateScale(double scale) + { + Matrix2x3d result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static void CreateScale(Vector2d scale, out Matrix2x3d result) + { + result.Row0.X = scale.X; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = scale.Y; + result.Row1.Z = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static Matrix2x3d CreateScale(Vector2d scale) + { + Matrix2x3d result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static void CreateScale(double x, double y, out Matrix2x3d result) + { + result.Row0.X = x; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = y; + result.Row1.Z = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static Matrix2x3d CreateScale(double x, double y) + { + Matrix2x3d result; + CreateScale(x, y, out result); + return result; + } + + #endregion + + #region Multiply Functions + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2x3d left, double right, out Matrix2x3d result) + { + result.Row0.X = left.Row0.X * right; + result.Row0.Y = left.Row0.Y * right; + result.Row0.Z = left.Row0.Z * right; + result.Row1.X = left.Row1.X * right; + result.Row1.Y = left.Row1.Y * right; + result.Row1.Z = left.Row1.Z * right; + } + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2x3d Mult(Matrix2x3d left, double right) + { + Matrix2x3d result; + Mult(ref left, right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2x3d left, ref Matrix3x2 right, out Matrix2d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, + rM11 = right.Row0.X, rM12 = right.Row0.Y, + rM21 = right.Row1.X, rM22 = right.Row1.Y, + rM31 = right.Row2.X, rM32 = right.Row2.Y; + + result.Row0.X = ((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31); + result.Row0.Y = ((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rM32); + result.Row1.X = ((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31); + result.Row1.Y = ((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rM32); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2d Mult(Matrix2x3d left, Matrix3x2 right) + { + Matrix2d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2x3d left, ref Matrix3 right, out Matrix2x3d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, + rM31 = right.Row2.X, rm32 = right.Row2.Y, rM33 = right.Row2.Z; + + result.Row0.X = ((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31); + result.Row0.Y = ((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rm32); + result.Row0.Z = ((lM11 * rM13) + (lM12 * rM23)) + (lM13 * rM33); + result.Row1.X = ((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31); + result.Row1.Y = ((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rm32); + result.Row1.Z = ((lM21 * rM13) + (lM22 * rM23)) + (lM23 * rM33); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2x3d Mult(Matrix2x3d left, Matrix3 right) + { + Matrix2x3d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2x3d left, ref Matrix3x4 right, out Matrix2x4d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, + rM31 = right.Row2.X, rm32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W; + + result.Row0.X = ((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31); + result.Row0.Y = ((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rm32); + result.Row0.Z = ((lM11 * rM13) + (lM12 * rM23)) + (lM13 * rM33); + result.Row0.W = ((lM11 * rM14) + (lM12 * rM24)) + (lM13 * rM34); + result.Row1.X = ((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31); + result.Row1.Y = ((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rm32); + result.Row1.Z = ((lM21 * rM13) + (lM22 * rM23)) + (lM23 * rM33); + result.Row1.W = ((lM21 * rM14) + (lM22 * rM24)) + (lM23 * rM34); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2x4d Mult(Matrix2x3d left, Matrix3x4 right) + { + Matrix2x4d result; + Mult(ref left, ref right, out result); + return result; + } + + #endregion + + #region Add + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static void Add(ref Matrix2x3d left, ref Matrix2x3d right, out Matrix2x3d result) + { + result.Row0.X = left.Row0.X + right.Row0.X; + result.Row0.Y = left.Row0.Y + right.Row0.Y; + result.Row0.Z = left.Row0.Z + right.Row0.Z; + result.Row1.X = left.Row1.X + right.Row1.X; + result.Row1.Y = left.Row1.Y + right.Row1.Y; + result.Row1.Z = left.Row1.Z + right.Row1.Z; + } + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static Matrix2x3d Add(Matrix2x3d left, Matrix2x3d right) + { + Matrix2x3d result; + Add(ref left, ref right, out result); + return result; + } + + #endregion + + #region Subtract + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static void Subtract(ref Matrix2x3d left, ref Matrix2x3d right, out Matrix2x3d result) + { + result.Row0.X = left.Row0.X - right.Row0.X; + result.Row0.Y = left.Row0.Y - right.Row0.Y; + result.Row0.Z = left.Row0.Z - right.Row0.Z; + result.Row1.X = left.Row1.X - right.Row1.X; + result.Row1.Y = left.Row1.Y - right.Row1.Y; + result.Row1.Z = left.Row1.Z - right.Row1.Z; + } + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static Matrix2x3d Subtract(Matrix2x3d left, Matrix2x3d right) + { + Matrix2x3d result; + Subtract(ref left, ref right, out result); + return result; + } + + #endregion + + #region Transpose + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static void Transpose(ref Matrix2x3d mat, out Matrix3x2d result) + { + result.Row0.X = mat.Row0.X; + result.Row0.Y = mat.Row1.X; + result.Row1.X = mat.Row0.Y; + result.Row1.Y = mat.Row1.Y; + result.Row2.X = mat.Row0.Z; + result.Row2.Y = mat.Row1.Z; + } + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static Matrix3x2d Transpose(Matrix2x3d mat) + { + Matrix3x2d result; + Transpose(ref mat, out result); + return result; + } + + #endregion + + #endregion + + #region Operators + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x3d which holds the result of the multiplication + public static Matrix2x3d operator *(double left, Matrix2x3d right) + { + return Mult(right, left); + } + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x3d which holds the result of the multiplication + public static Matrix2x3d operator *(Matrix2x3d left, double right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2d which holds the result of the multiplication + public static Matrix2d operator *(Matrix2x3d left, Matrix3x2 right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x3d which holds the result of the multiplication + public static Matrix2x3d operator *(Matrix2x3d left, Matrix3 right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x4d which holds the result of the multiplication + public static Matrix2x4d operator *(Matrix2x3d left, Matrix3x4 right) + { + return Mult(left, right); + } + + /// + /// Matrix addition + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x3d which holds the result of the addition + public static Matrix2x3d operator +(Matrix2x3d left, Matrix2x3d right) + { + return Add(left, right); + } + + /// + /// Matrix subtraction + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x3d which holds the result of the subtraction + public static Matrix2x3d operator -(Matrix2x3d left, Matrix2x3d right) + { + return Subtract(left, right); + } + + /// + /// Compares two instances for equality. + /// + /// The first instance. + /// The second instance. + /// True, if left equals right; false otherwise. + public static bool operator ==(Matrix2x3d left, Matrix2x3d right) + { + return left.Equals(right); + } + + /// + /// Compares two instances for inequality. + /// + /// The first instance. + /// The second instance. + /// True, if left does not equal right; false otherwise. + public static bool operator !=(Matrix2x3d left, Matrix2x3d right) + { + return !left.Equals(right); + } + + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix2x3d. + /// + /// The string representation of the matrix. + public override string ToString() + { + return String.Format("{0}\n{1}", Row0, Row1); + } + + #endregion + + #region public override int GetHashCode() + + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode(); + } + + #endregion + + #region public override bool Equals(object obj) + + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare tresult. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Matrix2x3d)) + return false; + + return this.Equals((Matrix2x3d)obj); + } + + #endregion + + #endregion + + #endregion + + #region IEquatable Members + + /// + /// Indicates whether the current matrix is equal to another matrix. + /// + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix2x3d other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1; + } + + #endregion + } } diff --git a/Source/OpenTK/Math/Matrix2x4d.cs b/Source/OpenTK/Math/Matrix2x4d.cs index 5b8521d7..4cfa066d 100644 --- a/Source/OpenTK/Math/Matrix2x4d.cs +++ b/Source/OpenTK/Math/Matrix2x4d.cs @@ -27,714 +27,714 @@ using System.Runtime.InteropServices; namespace OpenTK { - /// - /// Represents a 2x4 matrix. - /// - public struct Matrix2x4d : IEquatable - { - #region Fields - - /// - /// Top row of the matrix. - /// - public Vector4d Row0; - - /// - /// Bottom row of the matrix. - /// - public Vector4d Row1; - - /// - /// The zero matrix. - /// - public static readonly Matrix2x4d Zero = new Matrix2x4d(Vector4d.Zero, Vector4d.Zero); - - #endregion - - #region Constructors - - /// - /// Constructs a new instance. - /// - /// Top row of the matrix. - /// Bottom row of the matrix. - public Matrix2x4d(Vector4d row0, Vector4d row1) - { - Row0 = row0; - Row1 = row1; - } - - /// - /// Constructs a new instance - /// - /// First item of the first row of the matrix. - /// Second item of the first row of the matrix. - /// Third item of the first row of the matrix. - /// Fourth item of the first row of the matrix. - /// First item of the second row of the matrix. - /// Second item of the second row of the matrix. - /// Third item of the second row of the matrix. - /// Fourth item of the second row of the matrix. - public Matrix2x4d( - double m00, double m01, double m02, double m03, - double m10, double m11, double m12, double m13) - { - Row0 = new Vector4d(m00, m01, m02, m03); - Row1 = new Vector4d(m10, m11, m12, m13); - } - - #endregion - - #region Public Members - - #region Properties - - /// - /// Gets or sets the first column of the matrix. - /// - public Vector2d Column0 - { - get { return new Vector2d(Row0.X, Row1.X); } - set { Row0.X = value.X; Row1.X = value.Y; } - } - - /// - /// Gets or sets the second column of the matrix. - /// - public Vector2d Column1 - { - get { return new Vector2d(Row0.Y, Row1.Y); } - set { Row0.Y = value.X; Row1.Y = value.Y; } - } - - /// - /// Gets or sets the third column of the matrix. - /// - public Vector2d Column2 - { - get { return new Vector2d(Row0.Z, Row1.Z); } - set { Row0.Z = value.X; Row1.Z = value.Y; } - } - - /// - /// Gets or sets the fourth column of the matrix. - /// - public Vector2d Column3 - { - get { return new Vector2d(Row0.W, Row1.W); } - set { Row0.W = value.X; Row1.W = value.Y; } - } - - /// - /// Gets or sets the value at row 1, column 1 of this instance. - /// - public double M11 { get { return Row0.X; } set { Row0.X = value; } } - - /// - /// Gets or sets the value at row 1, column 2 of this instance. - /// - public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } - - /// - /// Gets or sets the value at row 1, column 3 of this instance. - /// - public double M13 { get { return Row0.Z; } set { Row0.Z = value; } } - - /// - /// Gets or sets the value at row 1, column 4 of this instance. - /// - public double M14 { get { return Row0.W; } set { Row0.W = value; } } - - /// - /// Gets or sets the value at row 2, column 1 of this instance. - /// - public double M21 { get { return Row1.X; } set { Row1.X = value; } } - - /// - /// Gets or sets the value at row 2, column 2 of this instance. - /// - public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } - - /// - /// Gets or sets the value at row 2, column 3 of this instance. - /// - public double M23 { get { return Row1.Z; } set { Row1.Z = value; } } - - /// - /// Gets or sets the value at row 2, column 4 of this instance. - /// - public double M24 { get { return Row1.W; } set { Row1.W = value; } } - - #endregion - - #region Indexers - - /// - /// Gets or sets the value at a specified row and column. - /// - public double this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } - - #endregion - - #region Static - - #region CreateRotation - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix2x4d instance. - public static void CreateRotation(double angle, out Matrix2x4d result) - { - double cos = System.Math.Cos(angle); - double sin = System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = sin; - result.Row0.Z = 0; - result.Row0.W = 0; - result.Row1.X = -sin; - result.Row1.Y = cos; - result.Row1.Z = 0; - result.Row1.W = 0; - } - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix2x3d instance. - public static Matrix2x4d CreateRotation(double angle) - { - Matrix2x4d result; - CreateRotation(angle, out result); - return result; - } - - #endregion - - #region CreateScale - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x, y, and z axes. - /// A scale matrix. - public static void CreateScale(double scale, out Matrix2x4d result) - { - result.Row0.X = scale; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = 0; - result.Row1.X = 0; - result.Row1.Y = scale; - result.Row1.Z = 0; - result.Row1.W = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x and y axes. - /// A scale matrix. - public static Matrix2x4d CreateScale(double scale) - { - Matrix2x4d result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static void CreateScale(Vector2d scale, out Matrix2x4d result) - { - result.Row0.X = scale.X; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = 0; - result.Row1.X = 0; - result.Row1.Y = scale.Y; - result.Row1.Z = 0; - result.Row1.W = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static Matrix2x4d CreateScale(Vector2d scale) - { - Matrix2x4d result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static void CreateScale(double x, double y, out Matrix2x4d result) - { - result.Row0.X = x; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = 0; - result.Row1.X = 0; - result.Row1.Y = y; - result.Row1.Z = 0; - result.Row1.W = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static Matrix2x4d CreateScale(double x, double y) - { - Matrix2x4d result; - CreateScale(x, y, out result); - return result; - } - - #endregion - - #region Multiply Functions - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2x4d left, double right, out Matrix2x4d result) - { - result.Row0.X = left.Row0.X * right; - result.Row0.Y = left.Row0.Y * right; - result.Row0.Z = left.Row0.Z * right; - result.Row0.W = left.Row0.W * right; - result.Row1.X = left.Row1.X * right; - result.Row1.Y = left.Row1.Y * right; - result.Row1.Z = left.Row1.Z * right; - result.Row1.W = left.Row1.W * right; - } - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2x4d Mult(Matrix2x4d left, double right) - { - Matrix2x4d result; - Mult(ref left, right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2x4d left, ref Matrix4x2 right, out Matrix2d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, - rM11 = right.Row0.X, rM12 = right.Row0.Y, - rM21 = right.Row1.X, rM22 = right.Row1.Y, - rM31 = right.Row2.X, rM32 = right.Row2.Y, - rM41 = right.Row3.X, rM42 = right.Row3.Y; - - result.Row0.X = (((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31)) + (lM14 * rM41); - result.Row0.Y = (((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rM32)) + (lM14 * rM42); - result.Row1.X = (((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31)) + (lM24 * rM41); - result.Row1.Y = (((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rM32)) + (lM24 * rM42); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2d Mult(Matrix2x4d left, Matrix4x2 right) - { - Matrix2d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2x4d left, ref Matrix4x3 right, out Matrix2x3d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, - rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, - rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z; - - result.Row0.X = (((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31)) + (lM14 * rM41); - result.Row0.Y = (((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rM32)) + (lM14 * rM42); - result.Row0.Z = (((lM11 * rM13) + (lM12 * rM23)) + (lM13 * rM33)) + (lM14 * rM43); - result.Row1.X = (((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31)) + (lM24 * rM41); - result.Row1.Y = (((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rM32)) + (lM24 * rM42); - result.Row1.Z = (((lM21 * rM13) + (lM22 * rM23)) + (lM23 * rM33)) + (lM24 * rM43); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2x3d Mult(Matrix2x4d left, Matrix4x3 right) - { - Matrix2x3d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix2x4d left, ref Matrix4 right, out Matrix2x4d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, - rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W, - rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z, rM44 = right.Row3.W; - - result.Row0.X = (((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31)) + (lM14 * rM41); - result.Row0.Y = (((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rM32)) + (lM14 * rM42); - result.Row0.Z = (((lM11 * rM13) + (lM12 * rM23)) + (lM13 * rM33)) + (lM14 * rM43); - result.Row0.W = (((lM11 * rM14) + (lM12 * rM24)) + (lM13 * rM34)) + (lM14 * rM44); - result.Row1.X = (((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31)) + (lM24 * rM41); - result.Row1.Y = (((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rM32)) + (lM24 * rM42); - result.Row1.Z = (((lM21 * rM13) + (lM22 * rM23)) + (lM23 * rM33)) + (lM24 * rM43); - result.Row1.W = (((lM21 * rM14) + (lM22 * rM24)) + (lM23 * rM34)) + (lM24 * rM44); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix2x4d Mult(Matrix2x4d left, Matrix4 right) - { - Matrix2x4d result; - Mult(ref left, ref right, out result); - return result; - } - - #endregion - - #region Add - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static void Add(ref Matrix2x4d left, ref Matrix2x4d right, out Matrix2x4d result) - { - result.Row0.X = left.Row0.X + right.Row0.X; - result.Row0.Y = left.Row0.Y + right.Row0.Y; - result.Row0.Z = left.Row0.Z + right.Row0.Z; - result.Row0.W = left.Row0.W + right.Row0.W; - result.Row1.X = left.Row1.X + right.Row1.X; - result.Row1.Y = left.Row1.Y + right.Row1.Y; - result.Row1.Z = left.Row1.Z + right.Row1.Z; - result.Row1.W = left.Row1.W + right.Row1.W; - } - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static Matrix2x4d Add(Matrix2x4d left, Matrix2x4d right) - { - Matrix2x4d result; - Add(ref left, ref right, out result); - return result; - } - - #endregion - - #region Subtract - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static void Subtract(ref Matrix2x4d left, ref Matrix2x4d right, out Matrix2x4d result) - { - result.Row0.X = left.Row0.X - right.Row0.X; - result.Row0.Y = left.Row0.Y - right.Row0.Y; - result.Row0.Z = left.Row0.Z - right.Row0.Z; - result.Row0.W = left.Row0.W - right.Row0.W; - result.Row1.X = left.Row1.X - right.Row1.X; - result.Row1.Y = left.Row1.Y - right.Row1.Y; - result.Row1.Z = left.Row1.Z - right.Row1.Z; - result.Row1.W = left.Row1.W - right.Row1.W; - } - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static Matrix2x4d Subtract(Matrix2x4d left, Matrix2x4d right) - { - Matrix2x4d result; - Subtract(ref left, ref right, out result); - return result; - } - - #endregion - - #region Transpose - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static void Transpose(ref Matrix2x4d mat, out Matrix4x2d result) - { - result.Row0.X = mat.Row0.X; - result.Row0.Y = mat.Row1.X; - result.Row1.X = mat.Row0.Y; - result.Row1.Y = mat.Row1.Y; - result.Row2.X = mat.Row0.Z; - result.Row2.Y = mat.Row1.Z; - result.Row3.X = mat.Row0.W; - result.Row3.Y = mat.Row1.W; - } - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static Matrix4x2d Transpose(Matrix2x4d mat) - { - Matrix4x2d result; - Transpose(ref mat, out result); - return result; - } - - #endregion - - #endregion - - #region Operators - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x4d which holds the result of the multiplication - public static Matrix2x4d operator *(double left, Matrix2x4d right) - { - return Mult(right, left); - } - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x4d which holds the result of the multiplication - public static Matrix2x4d operator *(Matrix2x4d left, double right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2d which holds the result of the multiplication - public static Matrix2d operator *(Matrix2x4d left, Matrix4x2 right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x3d which holds the result of the multiplication - public static Matrix2x3d operator *(Matrix2x4d left, Matrix4x3 right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x4d which holds the result of the multiplication - public static Matrix2x4d operator *(Matrix2x4d left, Matrix4 right) - { - return Mult(left, right); - } - - /// - /// Matrix addition - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2d which holds the result of the addition - public static Matrix2x4d operator +(Matrix2x4d left, Matrix2x4d right) - { - return Add(left, right); - } - - /// - /// Matrix subtraction - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2x4d which holds the result of the subtraction - public static Matrix2x4d operator -(Matrix2x4d left, Matrix2x4d right) - { - return Subtract(left, right); - } - - /// - /// Compares two instances for equality. - /// - /// The first instance. - /// The second instance. - /// True, if left equals right; false otherwise. - public static bool operator ==(Matrix2x4d left, Matrix2x4d right) - { - return left.Equals(right); - } - - /// - /// Compares two instances for inequality. - /// - /// The first instance. - /// The second instance. - /// True, if left does not equal right; false otherwise. - public static bool operator !=(Matrix2x4d left, Matrix2x4d right) - { - return !left.Equals(right); - } - - #endregion - - #region Overrides - - #region public override string ToString() - - /// - /// Returns a System.String that represents the current Matrix4. - /// - /// The string representation of the matrix. - public override string ToString() - { - return String.Format("{0}\n{1}", Row0, Row1); - } - - #endregion - - #region public override int GetHashCode() - - /// - /// Returns the hashcode for this instance. - /// - /// A System.Int32 containing the unique hashcode for this instance. - public override int GetHashCode() - { - return Row0.GetHashCode() ^ Row1.GetHashCode(); - } - - #endregion - - #region public override bool Equals(object obj) - - /// - /// Indicates whether this instance and a specified object are equal. - /// - /// The object to compare to. - /// True if the instances are equal; false otherwise. - public override bool Equals(object obj) - { - if (!(obj is Matrix2x4d)) - return false; - - return this.Equals((Matrix2x4d)obj); - } - - #endregion - - #endregion - - #endregion - - #region IEquatable Members - - /// - /// Indicates whether the current matrix is equal to another matrix. - /// - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(Matrix2x4d other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1; - } - - #endregion - } + /// + /// Represents a 2x4 matrix. + /// + public struct Matrix2x4d : IEquatable + { + #region Fields + + /// + /// Top row of the matrix. + /// + public Vector4d Row0; + + /// + /// Bottom row of the matrix. + /// + public Vector4d Row1; + + /// + /// The zero matrix. + /// + public static readonly Matrix2x4d Zero = new Matrix2x4d(Vector4d.Zero, Vector4d.Zero); + + #endregion + + #region Constructors + + /// + /// Constructs a new instance. + /// + /// Top row of the matrix. + /// Bottom row of the matrix. + public Matrix2x4d(Vector4d row0, Vector4d row1) + { + Row0 = row0; + Row1 = row1; + } + + /// + /// Constructs a new instance + /// + /// First item of the first row of the matrix. + /// Second item of the first row of the matrix. + /// Third item of the first row of the matrix. + /// Fourth item of the first row of the matrix. + /// First item of the second row of the matrix. + /// Second item of the second row of the matrix. + /// Third item of the second row of the matrix. + /// Fourth item of the second row of the matrix. + public Matrix2x4d( + double m00, double m01, double m02, double m03, + double m10, double m11, double m12, double m13) + { + Row0 = new Vector4d(m00, m01, m02, m03); + Row1 = new Vector4d(m10, m11, m12, m13); + } + + #endregion + + #region Public Members + + #region Properties + + /// + /// Gets or sets the first column of the matrix. + /// + public Vector2d Column0 + { + get { return new Vector2d(Row0.X, Row1.X); } + set { Row0.X = value.X; Row1.X = value.Y; } + } + + /// + /// Gets or sets the second column of the matrix. + /// + public Vector2d Column1 + { + get { return new Vector2d(Row0.Y, Row1.Y); } + set { Row0.Y = value.X; Row1.Y = value.Y; } + } + + /// + /// Gets or sets the third column of the matrix. + /// + public Vector2d Column2 + { + get { return new Vector2d(Row0.Z, Row1.Z); } + set { Row0.Z = value.X; Row1.Z = value.Y; } + } + + /// + /// Gets or sets the fourth column of the matrix. + /// + public Vector2d Column3 + { + get { return new Vector2d(Row0.W, Row1.W); } + set { Row0.W = value.X; Row1.W = value.Y; } + } + + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public double M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 1, column 3 of this instance. + /// + public double M13 { get { return Row0.Z; } set { Row0.Z = value; } } + + /// + /// Gets or sets the value at row 1, column 4 of this instance. + /// + public double M14 { get { return Row0.W; } set { Row0.W = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public double M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 3 of this instance. + /// + public double M23 { get { return Row1.Z; } set { Row1.Z = value; } } + + /// + /// Gets or sets the value at row 2, column 4 of this instance. + /// + public double M24 { get { return Row1.W; } set { Row1.W = value; } } + + #endregion + + #region Indexers + + /// + /// Gets or sets the value at a specified row and column. + /// + public double this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } + + #endregion + + #region Static + + #region CreateRotation + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix2x4d instance. + public static void CreateRotation(double angle, out Matrix2x4d result) + { + double cos = System.Math.Cos(angle); + double sin = System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = sin; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = -sin; + result.Row1.Y = cos; + result.Row1.Z = 0; + result.Row1.W = 0; + } + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix2x3d instance. + public static Matrix2x4d CreateRotation(double angle) + { + Matrix2x4d result; + CreateRotation(angle, out result); + return result; + } + + #endregion + + #region CreateScale + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x, y, and z axes. + /// A scale matrix. + public static void CreateScale(double scale, out Matrix2x4d result) + { + result.Row0.X = scale; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = 0; + result.Row1.Y = scale; + result.Row1.Z = 0; + result.Row1.W = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x and y axes. + /// A scale matrix. + public static Matrix2x4d CreateScale(double scale) + { + Matrix2x4d result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static void CreateScale(Vector2d scale, out Matrix2x4d result) + { + result.Row0.X = scale.X; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = 0; + result.Row1.Y = scale.Y; + result.Row1.Z = 0; + result.Row1.W = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static Matrix2x4d CreateScale(Vector2d scale) + { + Matrix2x4d result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static void CreateScale(double x, double y, out Matrix2x4d result) + { + result.Row0.X = x; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = 0; + result.Row1.Y = y; + result.Row1.Z = 0; + result.Row1.W = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static Matrix2x4d CreateScale(double x, double y) + { + Matrix2x4d result; + CreateScale(x, y, out result); + return result; + } + + #endregion + + #region Multiply Functions + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2x4d left, double right, out Matrix2x4d result) + { + result.Row0.X = left.Row0.X * right; + result.Row0.Y = left.Row0.Y * right; + result.Row0.Z = left.Row0.Z * right; + result.Row0.W = left.Row0.W * right; + result.Row1.X = left.Row1.X * right; + result.Row1.Y = left.Row1.Y * right; + result.Row1.Z = left.Row1.Z * right; + result.Row1.W = left.Row1.W * right; + } + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2x4d Mult(Matrix2x4d left, double right) + { + Matrix2x4d result; + Mult(ref left, right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2x4d left, ref Matrix4x2 right, out Matrix2d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, + rM11 = right.Row0.X, rM12 = right.Row0.Y, + rM21 = right.Row1.X, rM22 = right.Row1.Y, + rM31 = right.Row2.X, rM32 = right.Row2.Y, + rM41 = right.Row3.X, rM42 = right.Row3.Y; + + result.Row0.X = (((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31)) + (lM14 * rM41); + result.Row0.Y = (((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rM32)) + (lM14 * rM42); + result.Row1.X = (((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31)) + (lM24 * rM41); + result.Row1.Y = (((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rM32)) + (lM24 * rM42); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2d Mult(Matrix2x4d left, Matrix4x2 right) + { + Matrix2d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2x4d left, ref Matrix4x3 right, out Matrix2x3d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, + rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, + rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z; + + result.Row0.X = (((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31)) + (lM14 * rM41); + result.Row0.Y = (((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rM32)) + (lM14 * rM42); + result.Row0.Z = (((lM11 * rM13) + (lM12 * rM23)) + (lM13 * rM33)) + (lM14 * rM43); + result.Row1.X = (((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31)) + (lM24 * rM41); + result.Row1.Y = (((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rM32)) + (lM24 * rM42); + result.Row1.Z = (((lM21 * rM13) + (lM22 * rM23)) + (lM23 * rM33)) + (lM24 * rM43); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2x3d Mult(Matrix2x4d left, Matrix4x3 right) + { + Matrix2x3d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix2x4d left, ref Matrix4 right, out Matrix2x4d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, + rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W, + rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z, rM44 = right.Row3.W; + + result.Row0.X = (((lM11 * rM11) + (lM12 * rM21)) + (lM13 * rM31)) + (lM14 * rM41); + result.Row0.Y = (((lM11 * rM12) + (lM12 * rM22)) + (lM13 * rM32)) + (lM14 * rM42); + result.Row0.Z = (((lM11 * rM13) + (lM12 * rM23)) + (lM13 * rM33)) + (lM14 * rM43); + result.Row0.W = (((lM11 * rM14) + (lM12 * rM24)) + (lM13 * rM34)) + (lM14 * rM44); + result.Row1.X = (((lM21 * rM11) + (lM22 * rM21)) + (lM23 * rM31)) + (lM24 * rM41); + result.Row1.Y = (((lM21 * rM12) + (lM22 * rM22)) + (lM23 * rM32)) + (lM24 * rM42); + result.Row1.Z = (((lM21 * rM13) + (lM22 * rM23)) + (lM23 * rM33)) + (lM24 * rM43); + result.Row1.W = (((lM21 * rM14) + (lM22 * rM24)) + (lM23 * rM34)) + (lM24 * rM44); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix2x4d Mult(Matrix2x4d left, Matrix4 right) + { + Matrix2x4d result; + Mult(ref left, ref right, out result); + return result; + } + + #endregion + + #region Add + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static void Add(ref Matrix2x4d left, ref Matrix2x4d right, out Matrix2x4d result) + { + result.Row0.X = left.Row0.X + right.Row0.X; + result.Row0.Y = left.Row0.Y + right.Row0.Y; + result.Row0.Z = left.Row0.Z + right.Row0.Z; + result.Row0.W = left.Row0.W + right.Row0.W; + result.Row1.X = left.Row1.X + right.Row1.X; + result.Row1.Y = left.Row1.Y + right.Row1.Y; + result.Row1.Z = left.Row1.Z + right.Row1.Z; + result.Row1.W = left.Row1.W + right.Row1.W; + } + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static Matrix2x4d Add(Matrix2x4d left, Matrix2x4d right) + { + Matrix2x4d result; + Add(ref left, ref right, out result); + return result; + } + + #endregion + + #region Subtract + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static void Subtract(ref Matrix2x4d left, ref Matrix2x4d right, out Matrix2x4d result) + { + result.Row0.X = left.Row0.X - right.Row0.X; + result.Row0.Y = left.Row0.Y - right.Row0.Y; + result.Row0.Z = left.Row0.Z - right.Row0.Z; + result.Row0.W = left.Row0.W - right.Row0.W; + result.Row1.X = left.Row1.X - right.Row1.X; + result.Row1.Y = left.Row1.Y - right.Row1.Y; + result.Row1.Z = left.Row1.Z - right.Row1.Z; + result.Row1.W = left.Row1.W - right.Row1.W; + } + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static Matrix2x4d Subtract(Matrix2x4d left, Matrix2x4d right) + { + Matrix2x4d result; + Subtract(ref left, ref right, out result); + return result; + } + + #endregion + + #region Transpose + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static void Transpose(ref Matrix2x4d mat, out Matrix4x2d result) + { + result.Row0.X = mat.Row0.X; + result.Row0.Y = mat.Row1.X; + result.Row1.X = mat.Row0.Y; + result.Row1.Y = mat.Row1.Y; + result.Row2.X = mat.Row0.Z; + result.Row2.Y = mat.Row1.Z; + result.Row3.X = mat.Row0.W; + result.Row3.Y = mat.Row1.W; + } + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static Matrix4x2d Transpose(Matrix2x4d mat) + { + Matrix4x2d result; + Transpose(ref mat, out result); + return result; + } + + #endregion + + #endregion + + #region Operators + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x4d which holds the result of the multiplication + public static Matrix2x4d operator *(double left, Matrix2x4d right) + { + return Mult(right, left); + } + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x4d which holds the result of the multiplication + public static Matrix2x4d operator *(Matrix2x4d left, double right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2d which holds the result of the multiplication + public static Matrix2d operator *(Matrix2x4d left, Matrix4x2 right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x3d which holds the result of the multiplication + public static Matrix2x3d operator *(Matrix2x4d left, Matrix4x3 right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x4d which holds the result of the multiplication + public static Matrix2x4d operator *(Matrix2x4d left, Matrix4 right) + { + return Mult(left, right); + } + + /// + /// Matrix addition + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2d which holds the result of the addition + public static Matrix2x4d operator +(Matrix2x4d left, Matrix2x4d right) + { + return Add(left, right); + } + + /// + /// Matrix subtraction + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x4d which holds the result of the subtraction + public static Matrix2x4d operator -(Matrix2x4d left, Matrix2x4d right) + { + return Subtract(left, right); + } + + /// + /// Compares two instances for equality. + /// + /// The first instance. + /// The second instance. + /// True, if left equals right; false otherwise. + public static bool operator ==(Matrix2x4d left, Matrix2x4d right) + { + return left.Equals(right); + } + + /// + /// Compares two instances for inequality. + /// + /// The first instance. + /// The second instance. + /// True, if left does not equal right; false otherwise. + public static bool operator !=(Matrix2x4d left, Matrix2x4d right) + { + return !left.Equals(right); + } + + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix4. + /// + /// The string representation of the matrix. + public override string ToString() + { + return String.Format("{0}\n{1}", Row0, Row1); + } + + #endregion + + #region public override int GetHashCode() + + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode(); + } + + #endregion + + #region public override bool Equals(object obj) + + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare to. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Matrix2x4d)) + return false; + + return this.Equals((Matrix2x4d)obj); + } + + #endregion + + #endregion + + #endregion + + #region IEquatable Members + + /// + /// Indicates whether the current matrix is equal to another matrix. + /// + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix2x4d other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1; + } + + #endregion + } } diff --git a/Source/OpenTK/Math/Matrix3x2d.cs b/Source/OpenTK/Math/Matrix3x2d.cs index ac82a2ec..76c0a531 100644 --- a/Source/OpenTK/Math/Matrix3x2d.cs +++ b/Source/OpenTK/Math/Matrix3x2d.cs @@ -27,689 +27,689 @@ using System.Runtime.InteropServices; namespace OpenTK { - /// - /// Represents a 3x2 matrix. - /// - public struct Matrix3x2d : IEquatable - { - #region Fields - - /// - /// Top row of the matrix. - /// - public Vector2d Row0; - - /// - /// Second row of the matrix. - /// - public Vector2d Row1; - - /// - /// Bottom row of the matrix. - /// - public Vector2d Row2; - - /// - /// The zero matrix. - /// - public static readonly Matrix3x2d Zero = new Matrix3x2d(Vector2d.Zero, Vector2d.Zero, Vector2d.Zero); - - #endregion - - #region Constructors - - /// - /// Constructs a new instance. - /// - /// Top row of the matrix. - /// Second row of the matrix. - /// Bottom row of the matrix. - public Matrix3x2d(Vector2d row0, Vector2d row1, Vector2d row2) - { - Row0 = row0; - Row1 = row1; - Row2 = row2; - } - - /// - /// Constructs a new instance - /// - /// First item of the first row of the matrix. - /// Second item of the first row of the matrix. - /// First item of the second row of the matrix. - /// Second item of the second row of the matrix. - /// First item of the third row of the matrix. - /// Second item of the third row of the matrix. - public Matrix3x2d( - double m00, double m01, - double m10, double m11, - double m20, double m21) - { - Row0 = new Vector2d(m00, m01); - Row1 = new Vector2d(m10, m11); - Row2 = new Vector2d(m20, m21); - } - - #endregion - - #region Public Members - - #region Properties - - /// - /// Gets or sets the first column of this matrix. - /// - public Vector3d Column0 - { - get { return new Vector3d(Row0.X, Row1.X, Row2.X); } - set { Row0.X = value.X; Row1.X = value.Y; Row2.X = value.Z; } - } - - /// - /// Gets or sets the second column of this matrix. - /// - public Vector3d Column1 - { - get { return new Vector3d(Row0.Y, Row1.Y, Row2.Y); } - set { Row0.Y = value.X; Row1.Y = value.Y; Row2.Y = value.Z; } - } - - /// - /// Gets or sets the value at row 1, column 1 of this instance. - /// - public double M11 { get { return Row0.X; } set { Row0.X = value; } } - - /// - /// Gets or sets the value at row 1, column 2 of this instance. - /// - public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } - - /// - /// Gets or sets the value at row 2, column 1 of this instance. - /// - public double M21 { get { return Row1.X; } set { Row1.X = value; } } - - /// - /// Gets or sets the value at row 2, column 2 of this instance. - /// - public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } - - /// - /// Gets or sets the value at row 3, column 1 of this instance. - /// - public double M31 { get { return Row2.X; } set { Row2.X = value; } } - - /// - /// Gets or sets the value at row 3, column 2 of this instance. - /// - public double M32 { get { return Row2.Y; } set { Row2.Y = value; } } - - #endregion - - #region Indexers - - /// - /// Gets or sets the value at a specified row and column. - /// - public double this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - else if (rowIndex == 2) return Row2[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - else if (rowIndex == 2) Row2[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } - - #endregion - - #region Instance - #endregion - - #region Static - - #region CreateRotation - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix3x2d instance. - public static void CreateRotation(double angle, out Matrix3x2d result) - { - double cos = System.Math.Cos(angle); - double sin = System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = sin; - result.Row1.X = -sin; - result.Row1.Y = cos; - result.Row2.X = 0; - result.Row2.Y = 0; - } - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix3x2d instance. - public static Matrix3x2d CreateRotation(double angle) - { - Matrix3x2d result; - CreateRotation(angle, out result); - return result; - } - - #endregion - - #region CreateScale - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x, y, and z axes. - /// A scale matrix. - public static void CreateScale(double scale, out Matrix3x2d result) - { - result.Row0.X = scale; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = scale; - result.Row2.X = 0; - result.Row2.Y = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x and y axes. - /// A scale matrix. - public static Matrix3x2d CreateScale(double scale) - { - Matrix3x2d result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static void CreateScale(Vector2d scale, out Matrix3x2d result) - { - result.Row0.X = scale.X; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = scale.Y; - result.Row2.X = 0; - result.Row2.Y = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static Matrix3x2d CreateScale(Vector2d scale) - { - Matrix3x2d result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static void CreateScale(double x, double y, out Matrix3x2d result) - { - result.Row0.X = x; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = y; - result.Row2.X = 0; - result.Row2.Y = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static Matrix3x2d CreateScale(double x, double y) - { - Matrix3x2d result; - CreateScale(x, y, out result); - return result; - } - - #endregion - - #region Multiply Functions - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix3x2d left, double right, out Matrix3x2d result) - { - result.Row0.X = left.Row0.X * right; - result.Row0.Y = left.Row0.Y * right; - result.Row1.X = left.Row1.X * right; - result.Row1.Y = left.Row1.Y * right; - result.Row2.X = left.Row2.X * right; - result.Row2.Y = left.Row2.Y * right; - } - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix3x2d Mult(Matrix3x2d left, double right) - { - Matrix3x2d result; - Mult(ref left, right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix3x2d left, ref Matrix2d right, out Matrix3x2d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - lM31 = left.Row2.X, lM32 = left.Row2.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, - rM21 = right.Row1.X, rM22 = right.Row1.Y; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix3x2d Mult(Matrix3x2d left, Matrix2d right) - { - Matrix3x2d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix3x2d left, ref Matrix2x3d right, out Matrix3d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - lM31 = left.Row2.X, lM32 = left.Row2.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix3d Mult(Matrix3x2d left, Matrix2x3d right) - { - Matrix3d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix3x2d left, ref Matrix2x4d right, out Matrix3x4d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - lM31 = left.Row2.X, lM32 = left.Row2.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); - result.Row0.W = (lM11 * rM14) + (lM12 * rM24); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); - result.Row1.W = (lM21 * rM14) + (lM22 * rM24); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); - result.Row2.W = (lM31 * rM14) + (lM32 * rM24); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix3x4d Mult(Matrix3x2d left, Matrix2x4d right) - { - Matrix3x4d result; - Mult(ref left, ref right, out result); - return result; - } - - #endregion - - #region Add - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static void Add(ref Matrix3x2d left, ref Matrix3x2d right, out Matrix3x2d result) - { - result.Row0.X = left.Row0.X + right.Row0.X; - result.Row0.Y = left.Row0.Y + right.Row0.Y; - result.Row1.X = left.Row1.X + right.Row1.X; - result.Row1.Y = left.Row1.Y + right.Row1.Y; - result.Row2.X = left.Row2.X + right.Row2.X; - result.Row2.Y = left.Row2.Y + right.Row2.Y; - } - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static Matrix3x2d Add(Matrix3x2d left, Matrix3x2d right) - { - Matrix3x2d result; - Add(ref left, ref right, out result); - return result; - } - - #endregion - - #region Subtract - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static void Subtract(ref Matrix3x2d left, ref Matrix3x2d right, out Matrix3x2d result) - { - result.Row0.X = left.Row0.X - right.Row0.X; - result.Row0.Y = left.Row0.Y - right.Row0.Y; - result.Row1.X = left.Row1.X - right.Row1.X; - result.Row1.Y = left.Row1.Y - right.Row1.Y; - result.Row2.X = left.Row2.X - right.Row2.X; - result.Row2.Y = left.Row2.Y - right.Row2.Y; - } - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static Matrix3x2d Subtract(Matrix3x2d left, Matrix3x2d right) - { - Matrix3x2d result; - Subtract(ref left, ref right, out result); - return result; - } - - #endregion - - #region Transpose - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static void Transpose(ref Matrix3x2d mat, out Matrix2x3d result) - { - result.Row0.X = mat.Row0.X; - result.Row0.Y = mat.Row1.X; - result.Row0.Z = mat.Row2.X; - result.Row1.X = mat.Row0.Y; - result.Row1.Y = mat.Row1.Y; - result.Row1.Z = mat.Row2.Y; - } - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static Matrix2x3d Transpose(Matrix3x2d mat) - { - Matrix2x3d result; - Transpose(ref mat, out result); - return result; - } - - #endregion - - #endregion - - #region Operators - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix3x2d which holds the result of the multiplication - public static Matrix3x2d operator *(double left, Matrix3x2d right) - { - return Mult(right, left); - } - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix3x2d which holds the result of the multiplication - public static Matrix3x2d operator *(Matrix3x2d left, double right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix3x2d which holds the result of the multiplication - public static Matrix3x2d operator *(Matrix3x2d left, Matrix2d right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix3d which holds the result of the multiplication - public static Matrix3d operator *(Matrix3x2d left, Matrix2x3d right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix3x4 which holds the result of the multiplication - public static Matrix3x4d operator *(Matrix3x2d left, Matrix2x4d right) - { - return Mult(left, right); - } - - /// - /// Matrix addition - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix3x2d which holds the result of the addition - public static Matrix3x2d operator +(Matrix3x2d left, Matrix3x2d right) - { - return Add(left, right); - } - - /// - /// Matrix subtraction - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix3x2d which holds the result of the subtraction - public static Matrix3x2d operator -(Matrix3x2d left, Matrix3x2d right) - { - return Subtract(left, right); - } - - /// - /// Compares two instances for equality. - /// - /// The first instance. - /// The second instance. - /// True, if left equals right; false otherwise. - public static bool operator ==(Matrix3x2d left, Matrix3x2d right) - { - return left.Equals(right); - } - - /// - /// Compares two instances for inequality. - /// - /// The first instance. - /// The second instance. - /// True, if left does not equal right; false otherwise. - public static bool operator !=(Matrix3x2d left, Matrix3x2d right) - { - return !left.Equals(right); - } - - #endregion - - #region Overrides - - #region public override string ToString() - - /// - /// Returns a System.String that represents the current Matrix3d. - /// - /// The string representation of the matrix. - public override string ToString() - { - return String.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); - } - - #endregion - - #region public override int GetHashCode() - - /// - /// Returns the hashcode for this instance. - /// - /// A System.Int32 containing the unique hashcode for this instance. - public override int GetHashCode() - { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); - } - - #endregion - - #region public override bool Equals(object obj) - - /// - /// Indicates whether this instance and a specified object are equal. - /// - /// The object to compare to. - /// True if the instances are equal; false otherwise. - public override bool Equals(object obj) - { - if (!(obj is Matrix3x2d)) - return false; - - return this.Equals((Matrix3x2d)obj); - } - - #endregion - - #endregion - - #endregion - - #region IEquatable Members - - /// - /// Indicates whether the current matrix is equal to another matrix. - /// - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(Matrix3x2d other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1 && - Row2 == other.Row2; - } - - #endregion - } + /// + /// Represents a 3x2 matrix. + /// + public struct Matrix3x2d : IEquatable + { + #region Fields + + /// + /// Top row of the matrix. + /// + public Vector2d Row0; + + /// + /// Second row of the matrix. + /// + public Vector2d Row1; + + /// + /// Bottom row of the matrix. + /// + public Vector2d Row2; + + /// + /// The zero matrix. + /// + public static readonly Matrix3x2d Zero = new Matrix3x2d(Vector2d.Zero, Vector2d.Zero, Vector2d.Zero); + + #endregion + + #region Constructors + + /// + /// Constructs a new instance. + /// + /// Top row of the matrix. + /// Second row of the matrix. + /// Bottom row of the matrix. + public Matrix3x2d(Vector2d row0, Vector2d row1, Vector2d row2) + { + Row0 = row0; + Row1 = row1; + Row2 = row2; + } + + /// + /// Constructs a new instance + /// + /// First item of the first row of the matrix. + /// Second item of the first row of the matrix. + /// First item of the second row of the matrix. + /// Second item of the second row of the matrix. + /// First item of the third row of the matrix. + /// Second item of the third row of the matrix. + public Matrix3x2d( + double m00, double m01, + double m10, double m11, + double m20, double m21) + { + Row0 = new Vector2d(m00, m01); + Row1 = new Vector2d(m10, m11); + Row2 = new Vector2d(m20, m21); + } + + #endregion + + #region Public Members + + #region Properties + + /// + /// Gets or sets the first column of this matrix. + /// + public Vector3d Column0 + { + get { return new Vector3d(Row0.X, Row1.X, Row2.X); } + set { Row0.X = value.X; Row1.X = value.Y; Row2.X = value.Z; } + } + + /// + /// Gets or sets the second column of this matrix. + /// + public Vector3d Column1 + { + get { return new Vector3d(Row0.Y, Row1.Y, Row2.Y); } + set { Row0.Y = value.X; Row1.Y = value.Y; Row2.Y = value.Z; } + } + + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public double M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public double M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 3, column 1 of this instance. + /// + public double M31 { get { return Row2.X; } set { Row2.X = value; } } + + /// + /// Gets or sets the value at row 3, column 2 of this instance. + /// + public double M32 { get { return Row2.Y; } set { Row2.Y = value; } } + + #endregion + + #region Indexers + + /// + /// Gets or sets the value at a specified row and column. + /// + public double this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + else if (rowIndex == 2) return Row2[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + else if (rowIndex == 2) Row2[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } + + #endregion + + #region Instance + #endregion + + #region Static + + #region CreateRotation + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix3x2d instance. + public static void CreateRotation(double angle, out Matrix3x2d result) + { + double cos = System.Math.Cos(angle); + double sin = System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = sin; + result.Row1.X = -sin; + result.Row1.Y = cos; + result.Row2.X = 0; + result.Row2.Y = 0; + } + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix3x2d instance. + public static Matrix3x2d CreateRotation(double angle) + { + Matrix3x2d result; + CreateRotation(angle, out result); + return result; + } + + #endregion + + #region CreateScale + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x, y, and z axes. + /// A scale matrix. + public static void CreateScale(double scale, out Matrix3x2d result) + { + result.Row0.X = scale; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = scale; + result.Row2.X = 0; + result.Row2.Y = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x and y axes. + /// A scale matrix. + public static Matrix3x2d CreateScale(double scale) + { + Matrix3x2d result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static void CreateScale(Vector2d scale, out Matrix3x2d result) + { + result.Row0.X = scale.X; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = scale.Y; + result.Row2.X = 0; + result.Row2.Y = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static Matrix3x2d CreateScale(Vector2d scale) + { + Matrix3x2d result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static void CreateScale(double x, double y, out Matrix3x2d result) + { + result.Row0.X = x; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = y; + result.Row2.X = 0; + result.Row2.Y = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static Matrix3x2d CreateScale(double x, double y) + { + Matrix3x2d result; + CreateScale(x, y, out result); + return result; + } + + #endregion + + #region Multiply Functions + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix3x2d left, double right, out Matrix3x2d result) + { + result.Row0.X = left.Row0.X * right; + result.Row0.Y = left.Row0.Y * right; + result.Row1.X = left.Row1.X * right; + result.Row1.Y = left.Row1.Y * right; + result.Row2.X = left.Row2.X * right; + result.Row2.Y = left.Row2.Y * right; + } + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix3x2d Mult(Matrix3x2d left, double right) + { + Matrix3x2d result; + Mult(ref left, right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix3x2d left, ref Matrix2d right, out Matrix3x2d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + lM31 = left.Row2.X, lM32 = left.Row2.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, + rM21 = right.Row1.X, rM22 = right.Row1.Y; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix3x2d Mult(Matrix3x2d left, Matrix2d right) + { + Matrix3x2d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix3x2d left, ref Matrix2x3d right, out Matrix3d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + lM31 = left.Row2.X, lM32 = left.Row2.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix3d Mult(Matrix3x2d left, Matrix2x3d right) + { + Matrix3d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix3x2d left, ref Matrix2x4d right, out Matrix3x4d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + lM31 = left.Row2.X, lM32 = left.Row2.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); + result.Row0.W = (lM11 * rM14) + (lM12 * rM24); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); + result.Row1.W = (lM21 * rM14) + (lM22 * rM24); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); + result.Row2.W = (lM31 * rM14) + (lM32 * rM24); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix3x4d Mult(Matrix3x2d left, Matrix2x4d right) + { + Matrix3x4d result; + Mult(ref left, ref right, out result); + return result; + } + + #endregion + + #region Add + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static void Add(ref Matrix3x2d left, ref Matrix3x2d right, out Matrix3x2d result) + { + result.Row0.X = left.Row0.X + right.Row0.X; + result.Row0.Y = left.Row0.Y + right.Row0.Y; + result.Row1.X = left.Row1.X + right.Row1.X; + result.Row1.Y = left.Row1.Y + right.Row1.Y; + result.Row2.X = left.Row2.X + right.Row2.X; + result.Row2.Y = left.Row2.Y + right.Row2.Y; + } + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static Matrix3x2d Add(Matrix3x2d left, Matrix3x2d right) + { + Matrix3x2d result; + Add(ref left, ref right, out result); + return result; + } + + #endregion + + #region Subtract + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static void Subtract(ref Matrix3x2d left, ref Matrix3x2d right, out Matrix3x2d result) + { + result.Row0.X = left.Row0.X - right.Row0.X; + result.Row0.Y = left.Row0.Y - right.Row0.Y; + result.Row1.X = left.Row1.X - right.Row1.X; + result.Row1.Y = left.Row1.Y - right.Row1.Y; + result.Row2.X = left.Row2.X - right.Row2.X; + result.Row2.Y = left.Row2.Y - right.Row2.Y; + } + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static Matrix3x2d Subtract(Matrix3x2d left, Matrix3x2d right) + { + Matrix3x2d result; + Subtract(ref left, ref right, out result); + return result; + } + + #endregion + + #region Transpose + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static void Transpose(ref Matrix3x2d mat, out Matrix2x3d result) + { + result.Row0.X = mat.Row0.X; + result.Row0.Y = mat.Row1.X; + result.Row0.Z = mat.Row2.X; + result.Row1.X = mat.Row0.Y; + result.Row1.Y = mat.Row1.Y; + result.Row1.Z = mat.Row2.Y; + } + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static Matrix2x3d Transpose(Matrix3x2d mat) + { + Matrix2x3d result; + Transpose(ref mat, out result); + return result; + } + + #endregion + + #endregion + + #region Operators + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix3x2d which holds the result of the multiplication + public static Matrix3x2d operator *(double left, Matrix3x2d right) + { + return Mult(right, left); + } + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix3x2d which holds the result of the multiplication + public static Matrix3x2d operator *(Matrix3x2d left, double right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix3x2d which holds the result of the multiplication + public static Matrix3x2d operator *(Matrix3x2d left, Matrix2d right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix3d which holds the result of the multiplication + public static Matrix3d operator *(Matrix3x2d left, Matrix2x3d right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix3x4 which holds the result of the multiplication + public static Matrix3x4d operator *(Matrix3x2d left, Matrix2x4d right) + { + return Mult(left, right); + } + + /// + /// Matrix addition + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix3x2d which holds the result of the addition + public static Matrix3x2d operator +(Matrix3x2d left, Matrix3x2d right) + { + return Add(left, right); + } + + /// + /// Matrix subtraction + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix3x2d which holds the result of the subtraction + public static Matrix3x2d operator -(Matrix3x2d left, Matrix3x2d right) + { + return Subtract(left, right); + } + + /// + /// Compares two instances for equality. + /// + /// The first instance. + /// The second instance. + /// True, if left equals right; false otherwise. + public static bool operator ==(Matrix3x2d left, Matrix3x2d right) + { + return left.Equals(right); + } + + /// + /// Compares two instances for inequality. + /// + /// The first instance. + /// The second instance. + /// True, if left does not equal right; false otherwise. + public static bool operator !=(Matrix3x2d left, Matrix3x2d right) + { + return !left.Equals(right); + } + + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix3d. + /// + /// The string representation of the matrix. + public override string ToString() + { + return String.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); + } + + #endregion + + #region public override int GetHashCode() + + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + } + + #endregion + + #region public override bool Equals(object obj) + + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare to. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Matrix3x2d)) + return false; + + return this.Equals((Matrix3x2d)obj); + } + + #endregion + + #endregion + + #endregion + + #region IEquatable Members + + /// + /// Indicates whether the current matrix is equal to another matrix. + /// + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix3x2d other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1 && + Row2 == other.Row2; + } + + #endregion + } } diff --git a/Source/OpenTK/Math/Matrix3x4.cs b/Source/OpenTK/Math/Matrix3x4.cs index 418d7dba..1545debf 100644 --- a/Source/OpenTK/Math/Matrix3x4.cs +++ b/Source/OpenTK/Math/Matrix3x4.cs @@ -27,824 +27,824 @@ using System.Runtime.InteropServices; namespace OpenTK { - /// - /// Represents a 3x4 Matrix - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - public struct Matrix3x4 : IEquatable - { - #region Fields - - /// - /// Top row of the matrix - /// - public Vector4 Row0; - - /// - /// 2nd row of the matrix - /// - public Vector4 Row1; - - /// - /// Bottom row of the matrix - /// - public Vector4 Row2; - - /// - /// The zero matrix - /// - public static Matrix3x4 Zero = new Matrix3x4(Vector4.Zero, Vector4.Zero, Vector4.Zero); - - #endregion - - #region Constructors - - /// - /// Constructs a new instance. - /// - /// Top row of the matrix - /// Second row of the matrix - /// Bottom row of the matrix - public Matrix3x4(Vector4 row0, Vector4 row1, Vector4 row2) - { - Row0 = row0; - Row1 = row1; - Row2 = row2; - } - - /// - /// Constructs a new instance. - /// - /// First item of the first row of the matrix. - /// Second item of the first row of the matrix. - /// Third item of the first row of the matrix. - /// Fourth item of the first row of the matrix. - /// First item of the second row of the matrix. - /// Second item of the second row of the matrix. - /// Third item of the second row of the matrix. - /// Fourth item of the second row of the matrix. - /// First item of the third row of the matrix. - /// Second item of the third row of the matrix. - /// Third item of the third row of the matrix. - /// First item of the third row of the matrix. - public Matrix3x4( - float m00, float m01, float m02, float m03, - float m10, float m11, float m12, float m13, - float m20, float m21, float m22, float m23) - { - Row0 = new Vector4(m00, m01, m02, m03); - Row1 = new Vector4(m10, m11, m12, m13); - Row2 = new Vector4(m20, m21, m22, m23); - } - - #endregion - - #region Public Members - - #region Properties - - /// - /// Gets the first column of this matrix. - /// - public Vector3 Column0 - { - get { return new Vector3(Row0.X, Row1.X, Row2.X); } - } - - /// - /// Gets the second column of this matrix. - /// - public Vector3 Column1 - { - get { return new Vector3(Row0.Y, Row1.Y, Row2.Y); } - } - - /// - /// Gets the third column of this matrix. - /// - public Vector3 Column2 - { - get { return new Vector3(Row0.Z, Row1.Z, Row2.Z); } - } - - /// - /// Gets the fourth column of this matrix. - /// - public Vector3 Column3 - { - get { return new Vector3(Row0.W, Row1.W, Row2.W); } - } - - /// - /// Gets or sets the value at row 1, column 1 of this instance. - /// - public float M11 { get { return Row0.X; } set { Row0.X = value; } } - - /// - /// Gets or sets the value at row 1, column 2 of this instance. - /// - public float M12 { get { return Row0.Y; } set { Row0.Y = value; } } - - /// - /// Gets or sets the value at row 1, column 3 of this instance. - /// - public float M13 { get { return Row0.Z; } set { Row0.Z = value; } } - - /// - /// Gets or sets the value at row 1, column 4 of this instance. - /// - public float M14 { get { return Row0.W; } set { Row0.W = value; } } - - /// - /// Gets or sets the value at row 2, column 1 of this instance. - /// - public float M21 { get { return Row1.X; } set { Row1.X = value; } } - - /// - /// Gets or sets the value at row 2, column 2 of this instance. - /// - public float M22 { get { return Row1.Y; } set { Row1.Y = value; } } - - /// - /// Gets or sets the value at row 2, column 3 of this instance. - /// - public float M23 { get { return Row1.Z; } set { Row1.Z = value; } } - - /// - /// Gets or sets the value at row 2, column 4 of this instance. - /// - public float M24 { get { return Row1.W; } set { Row1.W = value; } } - - /// - /// Gets or sets the value at row 3, column 1 of this instance. - /// - public float M31 { get { return Row2.X; } set { Row2.X = value; } } - - /// - /// Gets or sets the value at row 3, column 2 of this instance. - /// - public float M32 { get { return Row2.Y; } set { Row2.Y = value; } } - - /// - /// Gets or sets the value at row 3, column 3 of this instance. - /// - public float M33 { get { return Row2.Z; } set { Row2.Z = value; } } - - /// - /// Gets or sets the value at row 3, column 4 of this instance. - /// - public float M34 { get { return Row2.W; } set { Row2.W = value; } } - - #endregion - - #region Indexers - - /// - /// Gets or sets the value at a specified row and column. - /// - public float this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - else if (rowIndex == 2) return Row2[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - else if (rowIndex == 2) Row2[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } - - #endregion - - #region Instance - - #region public void Invert() - - public void Invert() - { - this = Matrix3x4.Invert(this); - } - - #endregion - - #endregion - - #region Static - - #region CreateFromAxisAngle - - /// - /// Build a rotation matrix from the specified axis/angle rotation. - /// - /// The axis to rotate about. - /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). - /// A matrix instance. - public static void CreateFromAxisAngle(Vector3 axis, float angle, out Matrix3x4 result) - { - axis.Normalize(); - float axisX = axis.X, axisY = axis.Y, axisZ = axis.Z; - - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - float t = 1.0f - cos; - - float tXX = t * axisX * axisX, - tXY = t * axisX * axisY, - tXZ = t * axisX * axisZ, - tYY = t * axisY * axisY, - tYZ = t * axisY * axisZ, - tZZ = t * axisZ * axisZ; - - float sinX = sin * axisX, - sinY = sin * axisY, - sinZ = sin * axisZ; - - result.Row0.X = tXX + cos; - result.Row0.Y = tXY - sinZ; - result.Row0.Z = tXZ + sinY; - result.Row0.W = 0; - result.Row1.X = tXY + sinZ; - result.Row1.Y = tYY + cos; - result.Row1.Z = tYZ - sinX; - result.Row1.W = 0; - result.Row2.X = tXZ - sinY; - result.Row2.Y = tYZ + sinX; - result.Row2.Z = tZZ + cos; - result.Row2.W = 0; - } - - /// - /// Build a rotation matrix from the specified axis/angle rotation. - /// - /// The axis to rotate about. - /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). - /// A matrix instance. - public static Matrix3x4 CreateFromAxisAngle(Vector3 axis, float angle) - { - Matrix3x4 result; - CreateFromAxisAngle(axis, angle, out result); - return result; - } - - #endregion - - #region CreateFromQuaternion - - /// - /// Builds a rotation matrix from a quaternion. - /// - /// The quaternion to rotate by. - /// A matrix instance. - public static void CreateFromQuaternion(ref Quaternion q, out Matrix3x4 result) - { - float x = q.X, y = q.Y, z = q.Z, w = q.W, - tx = 2 * x, ty = 2 * y, tz = 2 * z, - txx = tx * x, tyy = ty * y, tzz = tz * z, - txy = tx * y, txz = tx * z, tyz = ty * z, - txw = tx * w, tyw = ty * w, tzw = tz * w; - - result.Row0.X = 1f - (tyy + tzz); - result.Row0.Y = txy + tzw; - result.Row0.Z = txz - tyw; - result.Row0.W = 0f; - result.Row1.X = txy - tzw; - result.Row1.Y = 1f - (txx + tzz); - result.Row1.Z = tyz + txw; - result.Row1.W = 0f; - result.Row2.X = txz + tyw; - result.Row2.Y = tyz - txw; - result.Row2.Z = 1f - (txx + tyy); - result.Row2.W = 0f; - - /*Vector3 axis; - float angle; - q.ToAxisAngle(out axis, out angle); - CreateFromAxisAngle(axis, angle, out result);*/ - } - - /// - /// Builds a rotation matrix from a quaternion. - /// - /// The quaternion to rotate by. - /// A matrix instance. - public static Matrix3x4 CreateFromQuaternion(Quaternion q) - { - Matrix3x4 result; - CreateFromQuaternion(ref q, out result); - return result; - } - - #endregion - - #region CreateRotation[XYZ] - - /// - /// Builds a rotation matrix for a rotation around the x-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static void CreateRotationX(float angle, out Matrix3x4 result) - { - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = 0; - result.Row1.X = 0; - result.Row1.Y = cos; - result.Row1.Z = sin; - result.Row1.W = 0; - result.Row2.X = 0; - result.Row2.Y = -sin; - result.Row2.Z = cos; - result.Row2.W = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the x-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static Matrix3x4 CreateRotationX(float angle) - { - Matrix3x4 result; - CreateRotationX(angle, out result); - return result; - } - - /// - /// Builds a rotation matrix for a rotation around the y-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static void CreateRotationY(float angle, out Matrix3x4 result) - { - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = 0; - result.Row0.Z = -sin; - result.Row0.W = 0; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row1.W = 0; - result.Row2.X = sin; - result.Row2.Y = 0; - result.Row2.Z = cos; - result.Row2.W = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the y-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static Matrix3x4 CreateRotationY(float angle) - { - Matrix3x4 result; - CreateRotationY(angle, out result); - return result; - } - - /// - /// Builds a rotation matrix for a rotation around the z-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static void CreateRotationZ(float angle, out Matrix3x4 result) - { - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = sin; - result.Row0.Z = 0; - result.Row0.W = 0; - result.Row1.X = -sin; - result.Row1.Y = cos; - result.Row1.Z = 0; - result.Row1.W = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row2.W = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the z-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static Matrix3x4 CreateRotationZ(float angle) - { - Matrix3x4 result; - CreateRotationZ(angle, out result); - return result; - } - - #endregion - - #region CreateTranslation - - /// - /// Creates a translation matrix. - /// - /// X translation. - /// Y translation. - /// Z translation. - /// The resulting Matrix4 instance. - public static void CreateTranslation(float x, float y, float z, out Matrix3x4 result) - { - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = x; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row1.W = y; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row2.W = z; - } - - /// - /// Creates a translation matrix. - /// - /// The translation vector. - /// The resulting Matrix4 instance. - public static void CreateTranslation(ref Vector3 vector, out Matrix3x4 result) - { - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = vector.X; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row1.W = vector.Y; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row2.W = vector.Z; - } - - /// - /// Creates a translation matrix. - /// - /// X translation. - /// Y translation. - /// Z translation. - /// The resulting Matrix4 instance. - public static Matrix3x4 CreateTranslation(float x, float y, float z) - { - Matrix3x4 result; - CreateTranslation(x, y, z, out result); - return result; - } - - /// - /// Creates a translation matrix. - /// - /// The translation vector. - /// The resulting Matrix4 instance. - public static Matrix3x4 CreateTranslation(Vector3 vector) - { - Matrix3x4 result; - CreateTranslation(vector.X, vector.Y, vector.Z, out result); - return result; - } - - #endregion - - #region CreateScale - - /// - /// Build a scaling matrix - /// - /// Single scale factor for x,y and z axes - /// A scaling matrix - public static Matrix3x4 CreateScale(float scale) - { - return CreateScale(scale, scale, scale); - } - - /// - /// Build a scaling matrix - /// - /// Scale factors for x,y and z axes - /// A scaling matrix - public static Matrix3x4 CreateScale(Vector3 scale) - { - return CreateScale(scale.X, scale.Y, scale.Z); - } - - /// - /// Build a scaling matrix - /// - /// Scale factor for x-axis - /// Scale factor for y-axis - /// Scale factor for z-axis - /// A scaling matrix - public static Matrix3x4 CreateScale(float x, float y, float z) - { - Matrix3x4 result; - result.Row0.X = x; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = 0; - result.Row1.X = 0; - result.Row1.Y = y; - result.Row1.Z = 0; - result.Row1.W = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = z; - result.Row2.W = 0; - return result; - } - - #endregion - - #region Multiply Functions - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication - public static Matrix3 Mult(Matrix3x4 left, Matrix4x3 right) - { - Matrix3 result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication - public static void Mult(ref Matrix3x4 left, ref Matrix4x3 right, out Matrix3 result) - { - float lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, - lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, lM34 = left.Row2.W, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, - rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, - rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31) + (lM14 * rM41); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32) + (lM14 * rM42); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33) + (lM14 * rM43); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31) + (lM24 * rM41); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32) + (lM24 * rM42); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33) + (lM24 * rM43); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31) + (lM34 * rM41); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32) + (lM34 * rM42); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33) + (lM34 * rM43); - } - - public static Matrix3x4 Mult(Matrix3x4 left, Matrix3x4 right) - { - Matrix3x4 result; - Mult(ref left, ref right, out result); - return result; - } - - public static void Mult(ref Matrix3x4 left, ref Matrix3x4 right, out Matrix3x4 result) - { - float lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, - lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, lM34 = left.Row2.W, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, - rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33); - result.Row0.W = (lM11 * rM14) + (lM12 * rM24) + (lM13 * rM34) + lM14; - result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33); - result.Row1.W = (lM21 * rM14) + (lM22 * rM24) + (lM23 * rM34) + lM24; - result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33); - result.Row2.W = (lM31 * rM14) + (lM32 * rM24) + (lM33 * rM34) + lM34; - - /*result.Row0 = (right.Row0 * lM11 + right.Row1 * lM12 + right.Row2 * lM13); - result.Row0.W += lM14; - - result.Row1 = (right.Row0 * lM21 + right.Row1 * lM22 + right.Row2 * lM23); - result.Row1.W += lM24; - - result.Row2 = (right.Row0 * lM31 + right.Row1 * lM32 + right.Row2 * lM33); - result.Row2.W += lM34;*/ - } - - public static Matrix3x4 Mult(Matrix3x4 left, float right) - { - Matrix3x4 result; - Mult(ref left, right, out result); - return result; - } - - public static void Mult(ref Matrix3x4 left, float right, out Matrix3x4 result) - { - result.Row0 = left.Row0 * right; - result.Row1 = left.Row1 * right; - result.Row2 = left.Row2 * right; - } - - #endregion - - #region Add Functions - - public static Matrix3x4 Add(Matrix3x4 left, Matrix3x4 right) - { - Matrix3x4 result; - Add(ref left, ref right, out result); - return result; - } - - public static void Add(ref Matrix3x4 left, ref Matrix3x4 right, out Matrix3x4 result) - { - result.Row0 = left.Row0 + right.Row0; - result.Row1 = left.Row1 + right.Row1; - result.Row2 = left.Row2 + right.Row2; - } - - #endregion - - #region Subtract Functions - - public static Matrix3x4 Subtract(Matrix3x4 left, Matrix3x4 right) - { - Matrix3x4 result; - Subtract(ref left, ref right, out result); - return result; - } - - public static void Subtract(ref Matrix3x4 left, ref Matrix3x4 right, out Matrix3x4 result) - { - result.Row0 = left.Row0 - right.Row0; - result.Row1 = left.Row1 - right.Row1; - result.Row2 = left.Row2 - right.Row2; - } - - #endregion - - #region Invert Functions - - public static Matrix3x4 Invert(Matrix3x4 mat) - { - Matrix3x4 result; - Invert(ref mat, out result); - return result; - } - - public static void Invert(ref Matrix3x4 mat, out Matrix3x4 result) - { - Matrix3 inverseRotation = new Matrix3(mat.Column0, mat.Column1, mat.Column2); - inverseRotation.Row0 /= inverseRotation.Row0.LengthSquared; - inverseRotation.Row1 /= inverseRotation.Row1.LengthSquared; - inverseRotation.Row2 /= inverseRotation.Row2.LengthSquared; - - Vector3 translation = new Vector3(mat.Row0.W, mat.Row1.W, mat.Row2.W); - - result.Row0 = new Vector4(inverseRotation.Row0, -Vector3.Dot(inverseRotation.Row0, translation)); - result.Row1 = new Vector4(inverseRotation.Row1, -Vector3.Dot(inverseRotation.Row1, translation)); - result.Row2 = new Vector4(inverseRotation.Row2, -Vector3.Dot(inverseRotation.Row2, translation)); - } + /// + /// Represents a 3x4 Matrix + /// + [Serializable] + [StructLayout(LayoutKind.Sequential)] + public struct Matrix3x4 : IEquatable + { + #region Fields + + /// + /// Top row of the matrix + /// + public Vector4 Row0; + + /// + /// 2nd row of the matrix + /// + public Vector4 Row1; + + /// + /// Bottom row of the matrix + /// + public Vector4 Row2; + + /// + /// The zero matrix + /// + public static Matrix3x4 Zero = new Matrix3x4(Vector4.Zero, Vector4.Zero, Vector4.Zero); + + #endregion + + #region Constructors + + /// + /// Constructs a new instance. + /// + /// Top row of the matrix + /// Second row of the matrix + /// Bottom row of the matrix + public Matrix3x4(Vector4 row0, Vector4 row1, Vector4 row2) + { + Row0 = row0; + Row1 = row1; + Row2 = row2; + } + + /// + /// Constructs a new instance. + /// + /// First item of the first row of the matrix. + /// Second item of the first row of the matrix. + /// Third item of the first row of the matrix. + /// Fourth item of the first row of the matrix. + /// First item of the second row of the matrix. + /// Second item of the second row of the matrix. + /// Third item of the second row of the matrix. + /// Fourth item of the second row of the matrix. + /// First item of the third row of the matrix. + /// Second item of the third row of the matrix. + /// Third item of the third row of the matrix. + /// First item of the third row of the matrix. + public Matrix3x4( + float m00, float m01, float m02, float m03, + float m10, float m11, float m12, float m13, + float m20, float m21, float m22, float m23) + { + Row0 = new Vector4(m00, m01, m02, m03); + Row1 = new Vector4(m10, m11, m12, m13); + Row2 = new Vector4(m20, m21, m22, m23); + } + + #endregion + + #region Public Members + + #region Properties + + /// + /// Gets the first column of this matrix. + /// + public Vector3 Column0 + { + get { return new Vector3(Row0.X, Row1.X, Row2.X); } + } + + /// + /// Gets the second column of this matrix. + /// + public Vector3 Column1 + { + get { return new Vector3(Row0.Y, Row1.Y, Row2.Y); } + } + + /// + /// Gets the third column of this matrix. + /// + public Vector3 Column2 + { + get { return new Vector3(Row0.Z, Row1.Z, Row2.Z); } + } + + /// + /// Gets the fourth column of this matrix. + /// + public Vector3 Column3 + { + get { return new Vector3(Row0.W, Row1.W, Row2.W); } + } + + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public float M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public float M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 1, column 3 of this instance. + /// + public float M13 { get { return Row0.Z; } set { Row0.Z = value; } } + + /// + /// Gets or sets the value at row 1, column 4 of this instance. + /// + public float M14 { get { return Row0.W; } set { Row0.W = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public float M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public float M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 3 of this instance. + /// + public float M23 { get { return Row1.Z; } set { Row1.Z = value; } } + + /// + /// Gets or sets the value at row 2, column 4 of this instance. + /// + public float M24 { get { return Row1.W; } set { Row1.W = value; } } + + /// + /// Gets or sets the value at row 3, column 1 of this instance. + /// + public float M31 { get { return Row2.X; } set { Row2.X = value; } } + + /// + /// Gets or sets the value at row 3, column 2 of this instance. + /// + public float M32 { get { return Row2.Y; } set { Row2.Y = value; } } + + /// + /// Gets or sets the value at row 3, column 3 of this instance. + /// + public float M33 { get { return Row2.Z; } set { Row2.Z = value; } } + + /// + /// Gets or sets the value at row 3, column 4 of this instance. + /// + public float M34 { get { return Row2.W; } set { Row2.W = value; } } + + #endregion + + #region Indexers + + /// + /// Gets or sets the value at a specified row and column. + /// + public float this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + else if (rowIndex == 2) return Row2[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + else if (rowIndex == 2) Row2[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } + + #endregion + + #region Instance + + #region public void Invert() + + public void Invert() + { + this = Matrix3x4.Invert(this); + } + + #endregion + + #endregion + + #region Static + + #region CreateFromAxisAngle + + /// + /// Build a rotation matrix from the specified axis/angle rotation. + /// + /// The axis to rotate about. + /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). + /// A matrix instance. + public static void CreateFromAxisAngle(Vector3 axis, float angle, out Matrix3x4 result) + { + axis.Normalize(); + float axisX = axis.X, axisY = axis.Y, axisZ = axis.Z; + + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + float t = 1.0f - cos; + + float tXX = t * axisX * axisX, + tXY = t * axisX * axisY, + tXZ = t * axisX * axisZ, + tYY = t * axisY * axisY, + tYZ = t * axisY * axisZ, + tZZ = t * axisZ * axisZ; + + float sinX = sin * axisX, + sinY = sin * axisY, + sinZ = sin * axisZ; + + result.Row0.X = tXX + cos; + result.Row0.Y = tXY - sinZ; + result.Row0.Z = tXZ + sinY; + result.Row0.W = 0; + result.Row1.X = tXY + sinZ; + result.Row1.Y = tYY + cos; + result.Row1.Z = tYZ - sinX; + result.Row1.W = 0; + result.Row2.X = tXZ - sinY; + result.Row2.Y = tYZ + sinX; + result.Row2.Z = tZZ + cos; + result.Row2.W = 0; + } + + /// + /// Build a rotation matrix from the specified axis/angle rotation. + /// + /// The axis to rotate about. + /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). + /// A matrix instance. + public static Matrix3x4 CreateFromAxisAngle(Vector3 axis, float angle) + { + Matrix3x4 result; + CreateFromAxisAngle(axis, angle, out result); + return result; + } + + #endregion + + #region CreateFromQuaternion + + /// + /// Builds a rotation matrix from a quaternion. + /// + /// The quaternion to rotate by. + /// A matrix instance. + public static void CreateFromQuaternion(ref Quaternion q, out Matrix3x4 result) + { + float x = q.X, y = q.Y, z = q.Z, w = q.W, + tx = 2 * x, ty = 2 * y, tz = 2 * z, + txx = tx * x, tyy = ty * y, tzz = tz * z, + txy = tx * y, txz = tx * z, tyz = ty * z, + txw = tx * w, tyw = ty * w, tzw = tz * w; + + result.Row0.X = 1f - (tyy + tzz); + result.Row0.Y = txy + tzw; + result.Row0.Z = txz - tyw; + result.Row0.W = 0f; + result.Row1.X = txy - tzw; + result.Row1.Y = 1f - (txx + tzz); + result.Row1.Z = tyz + txw; + result.Row1.W = 0f; + result.Row2.X = txz + tyw; + result.Row2.Y = tyz - txw; + result.Row2.Z = 1f - (txx + tyy); + result.Row2.W = 0f; + + /*Vector3 axis; + float angle; + q.ToAxisAngle(out axis, out angle); + CreateFromAxisAngle(axis, angle, out result);*/ + } + + /// + /// Builds a rotation matrix from a quaternion. + /// + /// The quaternion to rotate by. + /// A matrix instance. + public static Matrix3x4 CreateFromQuaternion(Quaternion q) + { + Matrix3x4 result; + CreateFromQuaternion(ref q, out result); + return result; + } + + #endregion + + #region CreateRotation[XYZ] + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationX(float angle, out Matrix3x4 result) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = 0; + result.Row1.Y = cos; + result.Row1.Z = sin; + result.Row1.W = 0; + result.Row2.X = 0; + result.Row2.Y = -sin; + result.Row2.Z = cos; + result.Row2.W = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix3x4 CreateRotationX(float angle) + { + Matrix3x4 result; + CreateRotationX(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationY(float angle, out Matrix3x4 result) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = 0; + result.Row0.Z = -sin; + result.Row0.W = 0; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row1.W = 0; + result.Row2.X = sin; + result.Row2.Y = 0; + result.Row2.Z = cos; + result.Row2.W = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix3x4 CreateRotationY(float angle) + { + Matrix3x4 result; + CreateRotationY(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationZ(float angle, out Matrix3x4 result) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = sin; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = -sin; + result.Row1.Y = cos; + result.Row1.Z = 0; + result.Row1.W = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row2.W = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix3x4 CreateRotationZ(float angle) + { + Matrix3x4 result; + CreateRotationZ(angle, out result); + return result; + } + + #endregion + + #region CreateTranslation + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4 instance. + public static void CreateTranslation(float x, float y, float z, out Matrix3x4 result) + { + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = x; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row1.W = y; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row2.W = z; + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4 instance. + public static void CreateTranslation(ref Vector3 vector, out Matrix3x4 result) + { + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = vector.X; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row1.W = vector.Y; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row2.W = vector.Z; + } + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4 instance. + public static Matrix3x4 CreateTranslation(float x, float y, float z) + { + Matrix3x4 result; + CreateTranslation(x, y, z, out result); + return result; + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4 instance. + public static Matrix3x4 CreateTranslation(Vector3 vector) + { + Matrix3x4 result; + CreateTranslation(vector.X, vector.Y, vector.Z, out result); + return result; + } + + #endregion + + #region CreateScale + + /// + /// Build a scaling matrix + /// + /// Single scale factor for x,y and z axes + /// A scaling matrix + public static Matrix3x4 CreateScale(float scale) + { + return CreateScale(scale, scale, scale); + } + + /// + /// Build a scaling matrix + /// + /// Scale factors for x,y and z axes + /// A scaling matrix + public static Matrix3x4 CreateScale(Vector3 scale) + { + return CreateScale(scale.X, scale.Y, scale.Z); + } + + /// + /// Build a scaling matrix + /// + /// Scale factor for x-axis + /// Scale factor for y-axis + /// Scale factor for z-axis + /// A scaling matrix + public static Matrix3x4 CreateScale(float x, float y, float z) + { + Matrix3x4 result; + result.Row0.X = x; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = 0; + result.Row1.Y = y; + result.Row1.Z = 0; + result.Row1.W = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = z; + result.Row2.W = 0; + return result; + } + + #endregion + + #region Multiply Functions + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication + public static Matrix3 Mult(Matrix3x4 left, Matrix4x3 right) + { + Matrix3 result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication + public static void Mult(ref Matrix3x4 left, ref Matrix4x3 right, out Matrix3 result) + { + float lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, + lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, lM34 = left.Row2.W, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, + rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, + rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31) + (lM14 * rM41); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32) + (lM14 * rM42); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33) + (lM14 * rM43); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31) + (lM24 * rM41); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32) + (lM24 * rM42); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33) + (lM24 * rM43); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31) + (lM34 * rM41); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32) + (lM34 * rM42); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33) + (lM34 * rM43); + } + + public static Matrix3x4 Mult(Matrix3x4 left, Matrix3x4 right) + { + Matrix3x4 result; + Mult(ref left, ref right, out result); + return result; + } + + public static void Mult(ref Matrix3x4 left, ref Matrix3x4 right, out Matrix3x4 result) + { + float lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, + lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, lM34 = left.Row2.W, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, + rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33); + result.Row0.W = (lM11 * rM14) + (lM12 * rM24) + (lM13 * rM34) + lM14; + result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33); + result.Row1.W = (lM21 * rM14) + (lM22 * rM24) + (lM23 * rM34) + lM24; + result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33); + result.Row2.W = (lM31 * rM14) + (lM32 * rM24) + (lM33 * rM34) + lM34; + + /*result.Row0 = (right.Row0 * lM11 + right.Row1 * lM12 + right.Row2 * lM13); + result.Row0.W += lM14; + + result.Row1 = (right.Row0 * lM21 + right.Row1 * lM22 + right.Row2 * lM23); + result.Row1.W += lM24; + + result.Row2 = (right.Row0 * lM31 + right.Row1 * lM32 + right.Row2 * lM33); + result.Row2.W += lM34;*/ + } + + public static Matrix3x4 Mult(Matrix3x4 left, float right) + { + Matrix3x4 result; + Mult(ref left, right, out result); + return result; + } + + public static void Mult(ref Matrix3x4 left, float right, out Matrix3x4 result) + { + result.Row0 = left.Row0 * right; + result.Row1 = left.Row1 * right; + result.Row2 = left.Row2 * right; + } + + #endregion + + #region Add Functions + + public static Matrix3x4 Add(Matrix3x4 left, Matrix3x4 right) + { + Matrix3x4 result; + Add(ref left, ref right, out result); + return result; + } + + public static void Add(ref Matrix3x4 left, ref Matrix3x4 right, out Matrix3x4 result) + { + result.Row0 = left.Row0 + right.Row0; + result.Row1 = left.Row1 + right.Row1; + result.Row2 = left.Row2 + right.Row2; + } + + #endregion + + #region Subtract Functions + + public static Matrix3x4 Subtract(Matrix3x4 left, Matrix3x4 right) + { + Matrix3x4 result; + Subtract(ref left, ref right, out result); + return result; + } + + public static void Subtract(ref Matrix3x4 left, ref Matrix3x4 right, out Matrix3x4 result) + { + result.Row0 = left.Row0 - right.Row0; + result.Row1 = left.Row1 - right.Row1; + result.Row2 = left.Row2 - right.Row2; + } + + #endregion + + #region Invert Functions + + public static Matrix3x4 Invert(Matrix3x4 mat) + { + Matrix3x4 result; + Invert(ref mat, out result); + return result; + } + + public static void Invert(ref Matrix3x4 mat, out Matrix3x4 result) + { + Matrix3 inverseRotation = new Matrix3(mat.Column0, mat.Column1, mat.Column2); + inverseRotation.Row0 /= inverseRotation.Row0.LengthSquared; + inverseRotation.Row1 /= inverseRotation.Row1.LengthSquared; + inverseRotation.Row2 /= inverseRotation.Row2.LengthSquared; + + Vector3 translation = new Vector3(mat.Row0.W, mat.Row1.W, mat.Row2.W); + + result.Row0 = new Vector4(inverseRotation.Row0, -Vector3.Dot(inverseRotation.Row0, translation)); + result.Row1 = new Vector4(inverseRotation.Row1, -Vector3.Dot(inverseRotation.Row1, translation)); + result.Row2 = new Vector4(inverseRotation.Row2, -Vector3.Dot(inverseRotation.Row2, translation)); + } - #endregion + #endregion - #region Transpose - - public static Matrix4x3 Transpose(Matrix3x4 mat) - { - return new Matrix4x3(mat.Column0, mat.Column1, mat.Column2, mat.Column3); - } - - public static void Transpose(ref Matrix3x4 mat, out Matrix4x3 result) - { - result.Row0 = mat.Column0; - result.Row1 = mat.Column1; - result.Row2 = mat.Column2; - result.Row3 = mat.Column3; - } + #region Transpose + + public static Matrix4x3 Transpose(Matrix3x4 mat) + { + return new Matrix4x3(mat.Column0, mat.Column1, mat.Column2, mat.Column3); + } + + public static void Transpose(ref Matrix3x4 mat, out Matrix4x3 result) + { + result.Row0 = mat.Column0; + result.Row1 = mat.Column1; + result.Row2 = mat.Column2; + result.Row3 = mat.Column3; + } - #endregion + #endregion - #endregion + #endregion - #region Operators + #region Operators - public static Matrix3 operator *(Matrix3x4 left, Matrix4x3 right) - { - return Matrix3x4.Mult(left, right); - } + public static Matrix3 operator *(Matrix3x4 left, Matrix4x3 right) + { + return Matrix3x4.Mult(left, right); + } - public static Matrix3x4 operator *(Matrix3x4 left, Matrix3x4 right) - { - return Matrix3x4.Mult(left, right); - } + public static Matrix3x4 operator *(Matrix3x4 left, Matrix3x4 right) + { + return Matrix3x4.Mult(left, right); + } - public static Matrix3x4 operator *(Matrix3x4 left, float right) - { - return Matrix3x4.Mult(left, right); - } + public static Matrix3x4 operator *(Matrix3x4 left, float right) + { + return Matrix3x4.Mult(left, right); + } - public static Matrix3x4 operator +(Matrix3x4 left, Matrix3x4 right) - { - return Matrix3x4.Add(left, right); - } + public static Matrix3x4 operator +(Matrix3x4 left, Matrix3x4 right) + { + return Matrix3x4.Add(left, right); + } - public static Matrix3x4 operator -(Matrix3x4 left, Matrix3x4 right) - { - return Matrix3x4.Subtract(left, right); - } + public static Matrix3x4 operator -(Matrix3x4 left, Matrix3x4 right) + { + return Matrix3x4.Subtract(left, right); + } - public static bool operator ==(Matrix3x4 left, Matrix3x4 right) - { - return left.Equals(right); - } + public static bool operator ==(Matrix3x4 left, Matrix3x4 right) + { + return left.Equals(right); + } - public static bool operator !=(Matrix3x4 left, Matrix3x4 right) - { - return !left.Equals(right); - } + public static bool operator !=(Matrix3x4 left, Matrix3x4 right) + { + return !left.Equals(right); + } - #endregion + #endregion - #region Overrides + #region Overrides - #region public override string ToString() + #region public override string ToString() - public override string ToString() - { - return string.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); - } + public override string ToString() + { + return string.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); + } - #endregion + #endregion - #region public override int GetHashCode() + #region public override int GetHashCode() - public override int GetHashCode() - { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); - } + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + } - #endregion + #endregion - #region public override bool Equals(object obj) + #region public override bool Equals(object obj) - public override bool Equals(object obj) - { - if (!(obj is Matrix3x4)) - return false; + public override bool Equals(object obj) + { + if (!(obj is Matrix3x4)) + return false; - return this.Equals((Matrix3x4)obj); - } + return this.Equals((Matrix3x4)obj); + } - #endregion + #endregion - #endregion + #endregion - #endregion + #endregion - #region IEquatable Members + #region IEquatable Members - /// - /// Indicates whether the current matrix is equal to another matrix. - /// - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(Matrix3x4 other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1 && - Row2 == other.Row2; - } + /// + /// Indicates whether the current matrix is equal to another matrix. + /// + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix3x4 other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1 && + Row2 == other.Row2; + } - #endregion - } + #endregion + } } diff --git a/Source/OpenTK/Math/Matrix3x4d.cs b/Source/OpenTK/Math/Matrix3x4d.cs index 76ce0ce2..668502fb 100644 --- a/Source/OpenTK/Math/Matrix3x4d.cs +++ b/Source/OpenTK/Math/Matrix3x4d.cs @@ -27,824 +27,824 @@ using System.Runtime.InteropServices; namespace OpenTK { - /// - /// Represents a 3x4 Matrix - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - public struct Matrix3x4d : IEquatable - { - #region Fields - - /// - /// Top row of the matrix - /// - public Vector4d Row0; - - /// - /// 2nd row of the matrix - /// - public Vector4d Row1; - - /// - /// Bottom row of the matrix - /// - public Vector4d Row2; - - /// - /// The zero matrix - /// - public static Matrix3x4d Zero = new Matrix3x4d(Vector4d.Zero, Vector4d.Zero, Vector4d.Zero); - - #endregion - - #region Constructors - - /// - /// Constructs a new instance. - /// - /// Top row of the matrix - /// Second row of the matrix - /// Bottom row of the matrix - public Matrix3x4d(Vector4d row0, Vector4d row1, Vector4d row2) - { - Row0 = row0; - Row1 = row1; - Row2 = row2; - } - - /// - /// Constructs a new instance. - /// - /// First item of the first row of the matrix. - /// Second item of the first row of the matrix. - /// Third item of the first row of the matrix. - /// Fourth item of the first row of the matrix. - /// First item of the second row of the matrix. - /// Second item of the second row of the matrix. - /// Third item of the second row of the matrix. - /// Fourth item of the second row of the matrix. - /// First item of the third row of the matrix. - /// Second item of the third row of the matrix. - /// Third item of the third row of the matrix. - /// First item of the third row of the matrix. - public Matrix3x4d( - double m00, double m01, double m02, double m03, - double m10, double m11, double m12, double m13, - double m20, double m21, double m22, double m23) - { - Row0 = new Vector4d(m00, m01, m02, m03); - Row1 = new Vector4d(m10, m11, m12, m13); - Row2 = new Vector4d(m20, m21, m22, m23); - } - - #endregion - - #region Public Members - - #region Properties - - /// - /// Gets the first column of this matrix. - /// - public Vector3d Column0 - { - get { return new Vector3d(Row0.X, Row1.X, Row2.X); } - } - - /// - /// Gets the second column of this matrix. - /// - public Vector3d Column1 - { - get { return new Vector3d(Row0.Y, Row1.Y, Row2.Y); } - } - - /// - /// Gets the third column of this matrix. - /// - public Vector3d Column2 - { - get { return new Vector3d(Row0.Z, Row1.Z, Row2.Z); } - } - - /// - /// Gets the fourth column of this matrix. - /// - public Vector3d Column3 - { - get { return new Vector3d(Row0.W, Row1.W, Row2.W); } - } - - /// - /// Gets or sets the value at row 1, column 1 of this instance. - /// - public double M11 { get { return Row0.X; } set { Row0.X = value; } } - - /// - /// Gets or sets the value at row 1, column 2 of this instance. - /// - public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } - - /// - /// Gets or sets the value at row 1, column 3 of this instance. - /// - public double M13 { get { return Row0.Z; } set { Row0.Z = value; } } - - /// - /// Gets or sets the value at row 1, column 4 of this instance. - /// - public double M14 { get { return Row0.W; } set { Row0.W = value; } } - - /// - /// Gets or sets the value at row 2, column 1 of this instance. - /// - public double M21 { get { return Row1.X; } set { Row1.X = value; } } - - /// - /// Gets or sets the value at row 2, column 2 of this instance. - /// - public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } - - /// - /// Gets or sets the value at row 2, column 3 of this instance. - /// - public double M23 { get { return Row1.Z; } set { Row1.Z = value; } } - - /// - /// Gets or sets the value at row 2, column 4 of this instance. - /// - public double M24 { get { return Row1.W; } set { Row1.W = value; } } - - /// - /// Gets or sets the value at row 3, column 1 of this instance. - /// - public double M31 { get { return Row2.X; } set { Row2.X = value; } } - - /// - /// Gets or sets the value at row 3, column 2 of this instance. - /// - public double M32 { get { return Row2.Y; } set { Row2.Y = value; } } - - /// - /// Gets or sets the value at row 3, column 3 of this instance. - /// - public double M33 { get { return Row2.Z; } set { Row2.Z = value; } } - - /// - /// Gets or sets the value at row 3, column 4 of this instance. - /// - public double M34 { get { return Row2.W; } set { Row2.W = value; } } - - #endregion - - #region Indexers - - /// - /// Gets or sets the value at a specified row and column. - /// - public double this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - else if (rowIndex == 2) return Row2[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - else if (rowIndex == 2) Row2[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } - - #endregion - - #region Instance - - #region public void Invert() - - public void Invert() - { - this = Matrix3x4d.Invert(this); - } - - #endregion - - #endregion - - #region Static - - #region CreateFromAxisAngle - - /// - /// Build a rotation matrix from the specified axis/angle rotation. - /// - /// The axis to rotate about. - /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). - /// A matrix instance. - public static void CreateFromAxisAngle(Vector3d axis, double angle, out Matrix3x4d result) - { - axis.Normalize(); - double axisX = axis.X, axisY = axis.Y, axisZ = axis.Z; - - double cos = (double)System.Math.Cos(angle); - double sin = (double)System.Math.Sin(angle); - double t = 1.0f - cos; - - double tXX = t * axisX * axisX, - tXY = t * axisX * axisY, - tXZ = t * axisX * axisZ, - tYY = t * axisY * axisY, - tYZ = t * axisY * axisZ, - tZZ = t * axisZ * axisZ; - - double sinX = sin * axisX, - sinY = sin * axisY, - sinZ = sin * axisZ; - - result.Row0.X = tXX + cos; - result.Row0.Y = tXY - sinZ; - result.Row0.Z = tXZ + sinY; - result.Row0.W = 0; - result.Row1.X = tXY + sinZ; - result.Row1.Y = tYY + cos; - result.Row1.Z = tYZ - sinX; - result.Row1.W = 0; - result.Row2.X = tXZ - sinY; - result.Row2.Y = tYZ + sinX; - result.Row2.Z = tZZ + cos; - result.Row2.W = 0; - } - - /// - /// Build a rotation matrix from the specified axis/angle rotation. - /// - /// The axis to rotate about. - /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). - /// A matrix instance. - public static Matrix3x4d CreateFromAxisAngle(Vector3d axis, double angle) - { - Matrix3x4d result; - CreateFromAxisAngle(axis, angle, out result); - return result; - } - - #endregion - - #region CreateFromQuaternion - - /// - /// Builds a rotation matrix from a quaternion. - /// - /// The quaternion to rotate by. - /// A matrix instance. - public static void CreateFromQuaternion(ref Quaternion q, out Matrix3x4d result) - { - double x = q.X, y = q.Y, z = q.Z, w = q.W, - tx = 2 * x, ty = 2 * y, tz = 2 * z, - txx = tx * x, tyy = ty * y, tzz = tz * z, - txy = tx * y, txz = tx * z, tyz = ty * z, - txw = tx * w, tyw = ty * w, tzw = tz * w; - - result.Row0.X = 1f - (tyy + tzz); - result.Row0.Y = txy + tzw; - result.Row0.Z = txz - tyw; - result.Row0.W = 0f; - result.Row1.X = txy - tzw; - result.Row1.Y = 1f - (txx + tzz); - result.Row1.Z = tyz + txw; - result.Row1.W = 0f; - result.Row2.X = txz + tyw; - result.Row2.Y = tyz - txw; - result.Row2.Z = 1f - (txx + tyy); - result.Row2.W = 0f; - - /*Vector3d axis; - double angle; - q.ToAxisAngle(out axis, out angle); - CreateFromAxisAngle(axis, angle, out result);*/ - } - - /// - /// Builds a rotation matrix from a quaternion. - /// - /// The quaternion to rotate by. - /// A matrix instance. - public static Matrix3x4d CreateFromQuaternion(Quaternion q) - { - Matrix3x4d result; - CreateFromQuaternion(ref q, out result); - return result; - } - - #endregion - - #region CreateRotation[XYZ] - - /// - /// Builds a rotation matrix for a rotation around the x-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static void CreateRotationX(double angle, out Matrix3x4d result) - { - double cos = (double)System.Math.Cos(angle); - double sin = (double)System.Math.Sin(angle); - - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = 0; - result.Row1.X = 0; - result.Row1.Y = cos; - result.Row1.Z = sin; - result.Row1.W = 0; - result.Row2.X = 0; - result.Row2.Y = -sin; - result.Row2.Z = cos; - result.Row2.W = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the x-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static Matrix3x4d CreateRotationX(double angle) - { - Matrix3x4d result; - CreateRotationX(angle, out result); - return result; - } - - /// - /// Builds a rotation matrix for a rotation around the y-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static void CreateRotationY(double angle, out Matrix3x4d result) - { - double cos = (double)System.Math.Cos(angle); - double sin = (double)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = 0; - result.Row0.Z = -sin; - result.Row0.W = 0; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row1.W = 0; - result.Row2.X = sin; - result.Row2.Y = 0; - result.Row2.Z = cos; - result.Row2.W = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the y-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static Matrix3x4d CreateRotationY(double angle) - { - Matrix3x4d result; - CreateRotationY(angle, out result); - return result; - } - - /// - /// Builds a rotation matrix for a rotation around the z-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static void CreateRotationZ(double angle, out Matrix3x4d result) - { - double cos = (double)System.Math.Cos(angle); - double sin = (double)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = sin; - result.Row0.Z = 0; - result.Row0.W = 0; - result.Row1.X = -sin; - result.Row1.Y = cos; - result.Row1.Z = 0; - result.Row1.W = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row2.W = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the z-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static Matrix3x4d CreateRotationZ(double angle) - { - Matrix3x4d result; - CreateRotationZ(angle, out result); - return result; - } - - #endregion - - #region CreateTranslation - - /// - /// Creates a translation matrix. - /// - /// X translation. - /// Y translation. - /// Z translation. - /// The resulting Matrix4 instance. - public static void CreateTranslation(double x, double y, double z, out Matrix3x4d result) - { - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = x; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row1.W = y; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row2.W = z; - } - - /// - /// Creates a translation matrix. - /// - /// The translation vector. - /// The resulting Matrix4 instance. - public static void CreateTranslation(ref Vector3d vector, out Matrix3x4d result) - { - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = vector.X; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row1.W = vector.Y; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row2.W = vector.Z; - } - - /// - /// Creates a translation matrix. - /// - /// X translation. - /// Y translation. - /// Z translation. - /// The resulting Matrix4 instance. - public static Matrix3x4d CreateTranslation(double x, double y, double z) - { - Matrix3x4d result; - CreateTranslation(x, y, z, out result); - return result; - } - - /// - /// Creates a translation matrix. - /// - /// The translation vector. - /// The resulting Matrix4 instance. - public static Matrix3x4d CreateTranslation(Vector3d vector) - { - Matrix3x4d result; - CreateTranslation(vector.X, vector.Y, vector.Z, out result); - return result; - } - - #endregion - - #region CreateScale - - /// - /// Build a scaling matrix - /// - /// Single scale factor for x,y and z axes - /// A scaling matrix - public static Matrix3x4d CreateScale(double scale) - { - return CreateScale(scale, scale, scale); - } - - /// - /// Build a scaling matrix - /// - /// Scale factors for x,y and z axes - /// A scaling matrix - public static Matrix3x4d CreateScale(Vector3d scale) - { - return CreateScale(scale.X, scale.Y, scale.Z); - } - - /// - /// Build a scaling matrix - /// - /// Scale factor for x-axis - /// Scale factor for y-axis - /// Scale factor for z-axis - /// A scaling matrix - public static Matrix3x4d CreateScale(double x, double y, double z) - { - Matrix3x4d result; - result.Row0.X = x; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row0.W = 0; - result.Row1.X = 0; - result.Row1.Y = y; - result.Row1.Z = 0; - result.Row1.W = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = z; - result.Row2.W = 0; - return result; - } - - #endregion - - #region Multiply Functions - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication - public static Matrix3d Mult(Matrix3x4d left, Matrix4x3d right) - { - Matrix3d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication - public static void Mult(ref Matrix3x4d left, ref Matrix4x3d right, out Matrix3d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, - lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, lM34 = left.Row2.W, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, - rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, - rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31) + (lM14 * rM41); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32) + (lM14 * rM42); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33) + (lM14 * rM43); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31) + (lM24 * rM41); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32) + (lM24 * rM42); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33) + (lM24 * rM43); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31) + (lM34 * rM41); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32) + (lM34 * rM42); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33) + (lM34 * rM43); - } - - public static Matrix3x4d Mult(Matrix3x4d left, Matrix3x4d right) - { - Matrix3x4d result; - Mult(ref left, ref right, out result); - return result; - } - - public static void Mult(ref Matrix3x4d left, ref Matrix3x4d right, out Matrix3x4d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, - lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, lM34 = left.Row2.W, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, - rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33); - result.Row0.W = (lM11 * rM14) + (lM12 * rM24) + (lM13 * rM34) + lM14; - result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33); - result.Row1.W = (lM21 * rM14) + (lM22 * rM24) + (lM23 * rM34) + lM24; - result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33); - result.Row2.W = (lM31 * rM14) + (lM32 * rM24) + (lM33 * rM34) + lM34; - - /*result.Row0 = (right.Row0 * lM11 + right.Row1 * lM12 + right.Row2 * lM13); - result.Row0.W += lM14; - - result.Row1 = (right.Row0 * lM21 + right.Row1 * lM22 + right.Row2 * lM23); - result.Row1.W += lM24; - - result.Row2 = (right.Row0 * lM31 + right.Row1 * lM32 + right.Row2 * lM33); - result.Row2.W += lM34;*/ - } - - public static Matrix3x4d Mult(Matrix3x4d left, double right) - { - Matrix3x4d result; - Mult(ref left, right, out result); - return result; - } - - public static void Mult(ref Matrix3x4d left, double right, out Matrix3x4d result) - { - result.Row0 = left.Row0 * right; - result.Row1 = left.Row1 * right; - result.Row2 = left.Row2 * right; - } - - #endregion - - #region Add Functions - - public static Matrix3x4d Add(Matrix3x4d left, Matrix3x4d right) - { - Matrix3x4d result; - Add(ref left, ref right, out result); - return result; - } - - public static void Add(ref Matrix3x4d left, ref Matrix3x4d right, out Matrix3x4d result) - { - result.Row0 = left.Row0 + right.Row0; - result.Row1 = left.Row1 + right.Row1; - result.Row2 = left.Row2 + right.Row2; - } - - #endregion - - #region Subtract Functions - - public static Matrix3x4d Subtract(Matrix3x4d left, Matrix3x4d right) - { - Matrix3x4d result; - Subtract(ref left, ref right, out result); - return result; - } - - public static void Subtract(ref Matrix3x4d left, ref Matrix3x4d right, out Matrix3x4d result) - { - result.Row0 = left.Row0 - right.Row0; - result.Row1 = left.Row1 - right.Row1; - result.Row2 = left.Row2 - right.Row2; - } - - #endregion - - #region Invert Functions - - public static Matrix3x4d Invert(Matrix3x4d mat) - { - Matrix3x4d result; - Invert(ref mat, out result); - return result; - } - - public static void Invert(ref Matrix3x4d mat, out Matrix3x4d result) - { - Matrix3d inverseRotation = new Matrix3d(mat.Column0, mat.Column1, mat.Column2); - inverseRotation.Row0 /= inverseRotation.Row0.LengthSquared; - inverseRotation.Row1 /= inverseRotation.Row1.LengthSquared; - inverseRotation.Row2 /= inverseRotation.Row2.LengthSquared; - - Vector3d translation = new Vector3d(mat.Row0.W, mat.Row1.W, mat.Row2.W); - - result.Row0 = new Vector4d(inverseRotation.Row0, -Vector3d.Dot(inverseRotation.Row0, translation)); - result.Row1 = new Vector4d(inverseRotation.Row1, -Vector3d.Dot(inverseRotation.Row1, translation)); - result.Row2 = new Vector4d(inverseRotation.Row2, -Vector3d.Dot(inverseRotation.Row2, translation)); - } + /// + /// Represents a 3x4 Matrix + /// + [Serializable] + [StructLayout(LayoutKind.Sequential)] + public struct Matrix3x4d : IEquatable + { + #region Fields + + /// + /// Top row of the matrix + /// + public Vector4d Row0; + + /// + /// 2nd row of the matrix + /// + public Vector4d Row1; + + /// + /// Bottom row of the matrix + /// + public Vector4d Row2; + + /// + /// The zero matrix + /// + public static Matrix3x4d Zero = new Matrix3x4d(Vector4d.Zero, Vector4d.Zero, Vector4d.Zero); + + #endregion + + #region Constructors + + /// + /// Constructs a new instance. + /// + /// Top row of the matrix + /// Second row of the matrix + /// Bottom row of the matrix + public Matrix3x4d(Vector4d row0, Vector4d row1, Vector4d row2) + { + Row0 = row0; + Row1 = row1; + Row2 = row2; + } + + /// + /// Constructs a new instance. + /// + /// First item of the first row of the matrix. + /// Second item of the first row of the matrix. + /// Third item of the first row of the matrix. + /// Fourth item of the first row of the matrix. + /// First item of the second row of the matrix. + /// Second item of the second row of the matrix. + /// Third item of the second row of the matrix. + /// Fourth item of the second row of the matrix. + /// First item of the third row of the matrix. + /// Second item of the third row of the matrix. + /// Third item of the third row of the matrix. + /// First item of the third row of the matrix. + public Matrix3x4d( + double m00, double m01, double m02, double m03, + double m10, double m11, double m12, double m13, + double m20, double m21, double m22, double m23) + { + Row0 = new Vector4d(m00, m01, m02, m03); + Row1 = new Vector4d(m10, m11, m12, m13); + Row2 = new Vector4d(m20, m21, m22, m23); + } + + #endregion + + #region Public Members + + #region Properties + + /// + /// Gets the first column of this matrix. + /// + public Vector3d Column0 + { + get { return new Vector3d(Row0.X, Row1.X, Row2.X); } + } + + /// + /// Gets the second column of this matrix. + /// + public Vector3d Column1 + { + get { return new Vector3d(Row0.Y, Row1.Y, Row2.Y); } + } + + /// + /// Gets the third column of this matrix. + /// + public Vector3d Column2 + { + get { return new Vector3d(Row0.Z, Row1.Z, Row2.Z); } + } + + /// + /// Gets the fourth column of this matrix. + /// + public Vector3d Column3 + { + get { return new Vector3d(Row0.W, Row1.W, Row2.W); } + } + + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public double M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 1, column 3 of this instance. + /// + public double M13 { get { return Row0.Z; } set { Row0.Z = value; } } + + /// + /// Gets or sets the value at row 1, column 4 of this instance. + /// + public double M14 { get { return Row0.W; } set { Row0.W = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public double M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 3 of this instance. + /// + public double M23 { get { return Row1.Z; } set { Row1.Z = value; } } + + /// + /// Gets or sets the value at row 2, column 4 of this instance. + /// + public double M24 { get { return Row1.W; } set { Row1.W = value; } } + + /// + /// Gets or sets the value at row 3, column 1 of this instance. + /// + public double M31 { get { return Row2.X; } set { Row2.X = value; } } + + /// + /// Gets or sets the value at row 3, column 2 of this instance. + /// + public double M32 { get { return Row2.Y; } set { Row2.Y = value; } } + + /// + /// Gets or sets the value at row 3, column 3 of this instance. + /// + public double M33 { get { return Row2.Z; } set { Row2.Z = value; } } + + /// + /// Gets or sets the value at row 3, column 4 of this instance. + /// + public double M34 { get { return Row2.W; } set { Row2.W = value; } } + + #endregion + + #region Indexers + + /// + /// Gets or sets the value at a specified row and column. + /// + public double this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + else if (rowIndex == 2) return Row2[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + else if (rowIndex == 2) Row2[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } + + #endregion + + #region Instance + + #region public void Invert() + + public void Invert() + { + this = Matrix3x4d.Invert(this); + } + + #endregion + + #endregion + + #region Static + + #region CreateFromAxisAngle + + /// + /// Build a rotation matrix from the specified axis/angle rotation. + /// + /// The axis to rotate about. + /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). + /// A matrix instance. + public static void CreateFromAxisAngle(Vector3d axis, double angle, out Matrix3x4d result) + { + axis.Normalize(); + double axisX = axis.X, axisY = axis.Y, axisZ = axis.Z; + + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + double t = 1.0f - cos; + + double tXX = t * axisX * axisX, + tXY = t * axisX * axisY, + tXZ = t * axisX * axisZ, + tYY = t * axisY * axisY, + tYZ = t * axisY * axisZ, + tZZ = t * axisZ * axisZ; + + double sinX = sin * axisX, + sinY = sin * axisY, + sinZ = sin * axisZ; + + result.Row0.X = tXX + cos; + result.Row0.Y = tXY - sinZ; + result.Row0.Z = tXZ + sinY; + result.Row0.W = 0; + result.Row1.X = tXY + sinZ; + result.Row1.Y = tYY + cos; + result.Row1.Z = tYZ - sinX; + result.Row1.W = 0; + result.Row2.X = tXZ - sinY; + result.Row2.Y = tYZ + sinX; + result.Row2.Z = tZZ + cos; + result.Row2.W = 0; + } + + /// + /// Build a rotation matrix from the specified axis/angle rotation. + /// + /// The axis to rotate about. + /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). + /// A matrix instance. + public static Matrix3x4d CreateFromAxisAngle(Vector3d axis, double angle) + { + Matrix3x4d result; + CreateFromAxisAngle(axis, angle, out result); + return result; + } + + #endregion + + #region CreateFromQuaternion + + /// + /// Builds a rotation matrix from a quaternion. + /// + /// The quaternion to rotate by. + /// A matrix instance. + public static void CreateFromQuaternion(ref Quaternion q, out Matrix3x4d result) + { + double x = q.X, y = q.Y, z = q.Z, w = q.W, + tx = 2 * x, ty = 2 * y, tz = 2 * z, + txx = tx * x, tyy = ty * y, tzz = tz * z, + txy = tx * y, txz = tx * z, tyz = ty * z, + txw = tx * w, tyw = ty * w, tzw = tz * w; + + result.Row0.X = 1f - (tyy + tzz); + result.Row0.Y = txy + tzw; + result.Row0.Z = txz - tyw; + result.Row0.W = 0f; + result.Row1.X = txy - tzw; + result.Row1.Y = 1f - (txx + tzz); + result.Row1.Z = tyz + txw; + result.Row1.W = 0f; + result.Row2.X = txz + tyw; + result.Row2.Y = tyz - txw; + result.Row2.Z = 1f - (txx + tyy); + result.Row2.W = 0f; + + /*Vector3d axis; + double angle; + q.ToAxisAngle(out axis, out angle); + CreateFromAxisAngle(axis, angle, out result);*/ + } + + /// + /// Builds a rotation matrix from a quaternion. + /// + /// The quaternion to rotate by. + /// A matrix instance. + public static Matrix3x4d CreateFromQuaternion(Quaternion q) + { + Matrix3x4d result; + CreateFromQuaternion(ref q, out result); + return result; + } + + #endregion + + #region CreateRotation[XYZ] + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationX(double angle, out Matrix3x4d result) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = 0; + result.Row1.Y = cos; + result.Row1.Z = sin; + result.Row1.W = 0; + result.Row2.X = 0; + result.Row2.Y = -sin; + result.Row2.Z = cos; + result.Row2.W = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix3x4d CreateRotationX(double angle) + { + Matrix3x4d result; + CreateRotationX(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationY(double angle, out Matrix3x4d result) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = 0; + result.Row0.Z = -sin; + result.Row0.W = 0; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row1.W = 0; + result.Row2.X = sin; + result.Row2.Y = 0; + result.Row2.Z = cos; + result.Row2.W = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix3x4d CreateRotationY(double angle) + { + Matrix3x4d result; + CreateRotationY(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationZ(double angle, out Matrix3x4d result) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = sin; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = -sin; + result.Row1.Y = cos; + result.Row1.Z = 0; + result.Row1.W = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row2.W = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix3x4d CreateRotationZ(double angle) + { + Matrix3x4d result; + CreateRotationZ(angle, out result); + return result; + } + + #endregion + + #region CreateTranslation + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4 instance. + public static void CreateTranslation(double x, double y, double z, out Matrix3x4d result) + { + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = x; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row1.W = y; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row2.W = z; + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4 instance. + public static void CreateTranslation(ref Vector3d vector, out Matrix3x4d result) + { + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = vector.X; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row1.W = vector.Y; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row2.W = vector.Z; + } + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4 instance. + public static Matrix3x4d CreateTranslation(double x, double y, double z) + { + Matrix3x4d result; + CreateTranslation(x, y, z, out result); + return result; + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4 instance. + public static Matrix3x4d CreateTranslation(Vector3d vector) + { + Matrix3x4d result; + CreateTranslation(vector.X, vector.Y, vector.Z, out result); + return result; + } + + #endregion + + #region CreateScale + + /// + /// Build a scaling matrix + /// + /// Single scale factor for x,y and z axes + /// A scaling matrix + public static Matrix3x4d CreateScale(double scale) + { + return CreateScale(scale, scale, scale); + } + + /// + /// Build a scaling matrix + /// + /// Scale factors for x,y and z axes + /// A scaling matrix + public static Matrix3x4d CreateScale(Vector3d scale) + { + return CreateScale(scale.X, scale.Y, scale.Z); + } + + /// + /// Build a scaling matrix + /// + /// Scale factor for x-axis + /// Scale factor for y-axis + /// Scale factor for z-axis + /// A scaling matrix + public static Matrix3x4d CreateScale(double x, double y, double z) + { + Matrix3x4d result; + result.Row0.X = x; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row0.W = 0; + result.Row1.X = 0; + result.Row1.Y = y; + result.Row1.Z = 0; + result.Row1.W = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = z; + result.Row2.W = 0; + return result; + } + + #endregion + + #region Multiply Functions + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication + public static Matrix3d Mult(Matrix3x4d left, Matrix4x3d right) + { + Matrix3d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication + public static void Mult(ref Matrix3x4d left, ref Matrix4x3d right, out Matrix3d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, + lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, lM34 = left.Row2.W, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, + rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, + rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31) + (lM14 * rM41); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32) + (lM14 * rM42); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33) + (lM14 * rM43); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31) + (lM24 * rM41); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32) + (lM24 * rM42); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33) + (lM24 * rM43); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31) + (lM34 * rM41); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32) + (lM34 * rM42); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33) + (lM34 * rM43); + } + + public static Matrix3x4d Mult(Matrix3x4d left, Matrix3x4d right) + { + Matrix3x4d result; + Mult(ref left, ref right, out result); + return result; + } + + public static void Mult(ref Matrix3x4d left, ref Matrix3x4d right, out Matrix3x4d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, lM24 = left.Row1.W, + lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, lM34 = left.Row2.W, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, + rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33); + result.Row0.W = (lM11 * rM14) + (lM12 * rM24) + (lM13 * rM34) + lM14; + result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33); + result.Row1.W = (lM21 * rM14) + (lM22 * rM24) + (lM23 * rM34) + lM24; + result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33); + result.Row2.W = (lM31 * rM14) + (lM32 * rM24) + (lM33 * rM34) + lM34; + + /*result.Row0 = (right.Row0 * lM11 + right.Row1 * lM12 + right.Row2 * lM13); + result.Row0.W += lM14; + + result.Row1 = (right.Row0 * lM21 + right.Row1 * lM22 + right.Row2 * lM23); + result.Row1.W += lM24; + + result.Row2 = (right.Row0 * lM31 + right.Row1 * lM32 + right.Row2 * lM33); + result.Row2.W += lM34;*/ + } + + public static Matrix3x4d Mult(Matrix3x4d left, double right) + { + Matrix3x4d result; + Mult(ref left, right, out result); + return result; + } + + public static void Mult(ref Matrix3x4d left, double right, out Matrix3x4d result) + { + result.Row0 = left.Row0 * right; + result.Row1 = left.Row1 * right; + result.Row2 = left.Row2 * right; + } + + #endregion + + #region Add Functions + + public static Matrix3x4d Add(Matrix3x4d left, Matrix3x4d right) + { + Matrix3x4d result; + Add(ref left, ref right, out result); + return result; + } + + public static void Add(ref Matrix3x4d left, ref Matrix3x4d right, out Matrix3x4d result) + { + result.Row0 = left.Row0 + right.Row0; + result.Row1 = left.Row1 + right.Row1; + result.Row2 = left.Row2 + right.Row2; + } + + #endregion + + #region Subtract Functions + + public static Matrix3x4d Subtract(Matrix3x4d left, Matrix3x4d right) + { + Matrix3x4d result; + Subtract(ref left, ref right, out result); + return result; + } + + public static void Subtract(ref Matrix3x4d left, ref Matrix3x4d right, out Matrix3x4d result) + { + result.Row0 = left.Row0 - right.Row0; + result.Row1 = left.Row1 - right.Row1; + result.Row2 = left.Row2 - right.Row2; + } + + #endregion + + #region Invert Functions + + public static Matrix3x4d Invert(Matrix3x4d mat) + { + Matrix3x4d result; + Invert(ref mat, out result); + return result; + } + + public static void Invert(ref Matrix3x4d mat, out Matrix3x4d result) + { + Matrix3d inverseRotation = new Matrix3d(mat.Column0, mat.Column1, mat.Column2); + inverseRotation.Row0 /= inverseRotation.Row0.LengthSquared; + inverseRotation.Row1 /= inverseRotation.Row1.LengthSquared; + inverseRotation.Row2 /= inverseRotation.Row2.LengthSquared; + + Vector3d translation = new Vector3d(mat.Row0.W, mat.Row1.W, mat.Row2.W); + + result.Row0 = new Vector4d(inverseRotation.Row0, -Vector3d.Dot(inverseRotation.Row0, translation)); + result.Row1 = new Vector4d(inverseRotation.Row1, -Vector3d.Dot(inverseRotation.Row1, translation)); + result.Row2 = new Vector4d(inverseRotation.Row2, -Vector3d.Dot(inverseRotation.Row2, translation)); + } - #endregion + #endregion - #region Transpose - - public static Matrix4x3d Transpose(Matrix3x4d mat) - { - return new Matrix4x3d(mat.Column0, mat.Column1, mat.Column2, mat.Column3); - } - - public static void Transpose(ref Matrix3x4d mat, out Matrix4x3d result) - { - result.Row0 = mat.Column0; - result.Row1 = mat.Column1; - result.Row2 = mat.Column2; - result.Row3 = mat.Column3; - } + #region Transpose + + public static Matrix4x3d Transpose(Matrix3x4d mat) + { + return new Matrix4x3d(mat.Column0, mat.Column1, mat.Column2, mat.Column3); + } + + public static void Transpose(ref Matrix3x4d mat, out Matrix4x3d result) + { + result.Row0 = mat.Column0; + result.Row1 = mat.Column1; + result.Row2 = mat.Column2; + result.Row3 = mat.Column3; + } - #endregion + #endregion - #endregion + #endregion - #region Operators + #region Operators - public static Matrix3d operator *(Matrix3x4d left, Matrix4x3d right) - { - return Matrix3x4d.Mult(left, right); - } + public static Matrix3d operator *(Matrix3x4d left, Matrix4x3d right) + { + return Matrix3x4d.Mult(left, right); + } - public static Matrix3x4d operator *(Matrix3x4d left, Matrix3x4d right) - { - return Matrix3x4d.Mult(left, right); - } + public static Matrix3x4d operator *(Matrix3x4d left, Matrix3x4d right) + { + return Matrix3x4d.Mult(left, right); + } - public static Matrix3x4d operator *(Matrix3x4d left, double right) - { - return Matrix3x4d.Mult(left, right); - } + public static Matrix3x4d operator *(Matrix3x4d left, double right) + { + return Matrix3x4d.Mult(left, right); + } - public static Matrix3x4d operator +(Matrix3x4d left, Matrix3x4d right) - { - return Matrix3x4d.Add(left, right); - } + public static Matrix3x4d operator +(Matrix3x4d left, Matrix3x4d right) + { + return Matrix3x4d.Add(left, right); + } - public static Matrix3x4d operator -(Matrix3x4d left, Matrix3x4d right) - { - return Matrix3x4d.Subtract(left, right); - } + public static Matrix3x4d operator -(Matrix3x4d left, Matrix3x4d right) + { + return Matrix3x4d.Subtract(left, right); + } - public static bool operator ==(Matrix3x4d left, Matrix3x4d right) - { - return left.Equals(right); - } + public static bool operator ==(Matrix3x4d left, Matrix3x4d right) + { + return left.Equals(right); + } - public static bool operator !=(Matrix3x4d left, Matrix3x4d right) - { - return !left.Equals(right); - } + public static bool operator !=(Matrix3x4d left, Matrix3x4d right) + { + return !left.Equals(right); + } - #endregion + #endregion - #region Overrides + #region Overrides - #region public override string ToString() + #region public override string ToString() - public override string ToString() - { - return string.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); - } + public override string ToString() + { + return string.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); + } - #endregion + #endregion - #region public override int GetHashCode() + #region public override int GetHashCode() - public override int GetHashCode() - { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); - } + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + } - #endregion + #endregion - #region public override bool Equals(object obj) + #region public override bool Equals(object obj) - public override bool Equals(object obj) - { - if (!(obj is Matrix3x4d)) - return false; + public override bool Equals(object obj) + { + if (!(obj is Matrix3x4d)) + return false; - return this.Equals((Matrix3x4d)obj); - } + return this.Equals((Matrix3x4d)obj); + } - #endregion + #endregion - #endregion + #endregion - #endregion + #endregion - #region IEquatable Members + #region IEquatable Members - /// - /// Indicates whether the current matrix is equal to another matrix. - /// - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(Matrix3x4d other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1 && - Row2 == other.Row2; - } + /// + /// Indicates whether the current matrix is equal to another matrix. + /// + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix3x4d other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1 && + Row2 == other.Row2; + } - #endregion - } + #endregion + } } diff --git a/Source/OpenTK/Math/Matrix4d.cs b/Source/OpenTK/Math/Matrix4d.cs index 9dfcb80a..1eb84137 100644 --- a/Source/OpenTK/Math/Matrix4d.cs +++ b/Source/OpenTK/Math/Matrix4d.cs @@ -245,38 +245,38 @@ namespace OpenTK #endregion - #region Indexers + #region Indexers - /// - /// Gets or sets the value at a specified row and column. - /// - public double this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - else if (rowIndex == 2) return Row2[columnIndex]; - else if (rowIndex == 3) return Row3[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - else if (rowIndex == 2) Row2[columnIndex] = value; - else if (rowIndex == 3) Row3[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } + /// + /// Gets or sets the value at a specified row and column. + /// + public double this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + else if (rowIndex == 2) return Row2[columnIndex]; + else if (rowIndex == 3) return Row3[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + else if (rowIndex == 2) Row2[columnIndex] = value; + else if (rowIndex == 3) Row3[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } - #endregion + #endregion - #region Instance + #region Instance - #region public void Invert() + #region public void Invert() - /// + /// /// Converts this instance into its inverse. /// public void Invert() diff --git a/Source/OpenTK/Math/Matrix4x2.cs b/Source/OpenTK/Math/Matrix4x2.cs index 4ad57d56..b55bc2d0 100644 --- a/Source/OpenTK/Math/Matrix4x2.cs +++ b/Source/OpenTK/Math/Matrix4x2.cs @@ -27,739 +27,739 @@ using System.Runtime.InteropServices; namespace OpenTK { - /// - /// Represents a 4x2 matrix. - /// - public struct Matrix4x2 : IEquatable - { - #region Fields - - /// - /// Top row of the matrix. - /// - public Vector2 Row0; - - /// - /// Second row of the matrix. - /// - public Vector2 Row1; - - /// - /// Third row of the matrix. - /// - public Vector2 Row2; - - /// - /// Bottom row of the matrix. - /// - public Vector2 Row3; - - /// - /// The zero matrix. - /// - public static readonly Matrix4x2 Zero = new Matrix4x2(Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero); - - #endregion - - #region Constructors - - /// - /// Constructs a new instance. - /// - /// Top row of the matrix. - /// Second row of the matrix. - /// Third row of the matrix. - /// Bottom row of the matrix. - public Matrix4x2(Vector2 row0, Vector2 row1, Vector2 row2, Vector2 row3) - { - Row0 = row0; - Row1 = row1; - Row2 = row2; - Row3 = row3; - } - - - /// - /// Constructs a new instance - /// - /// First item of the first row of the matrix. - /// Second item of the first row of the matrix. - /// First item of the second row of the matrix. - /// Second item of the second row of the matrix. - /// First item of the third row of the matrix. - /// Second item of the third row of the matrix. - /// First item of the fourth row of the matrix. - /// Second item of the fourth row of the matrix. - public Matrix4x2( - float m00, float m01, - float m10, float m11, - float m20, float m21, - float m30, float m31) - { - Row0 = new Vector2(m00, m01); - Row1 = new Vector2(m10, m11); - Row2 = new Vector2(m20, m21); - Row3 = new Vector2(m30, m31); - } - - #endregion - - #region Public Members - - #region Properties - - /// - /// Gets or sets the first column of this matrix. - /// - public Vector4 Column0 - { - get { return new Vector4(Row0.X, Row1.X, Row2.X, Row3.X); } - set { Row0.X = value.X; Row1.X = value.Y; Row2.X = value.Z; Row3.X = value.W; } - } - - /// - /// Gets or sets the second column of this matrix. - /// - public Vector4 Column1 - { - get { return new Vector4(Row0.Y, Row1.Y, Row2.Y, Row3.X); } - set { Row0.Y = value.X; Row1.Y = value.Y; Row2.Y = value.Z; Row3.Y = value.W; } - } - - /// - /// Gets or sets the value at row 1, column 1 of this instance. - /// - public float M11 { get { return Row0.X; } set { Row0.X = value; } } - - /// - /// Gets or sets the value at row 1, column 2 of this instance. - /// - public float M12 { get { return Row0.Y; } set { Row0.Y = value; } } - - /// - /// Gets or sets the value at row 2, column 1 of this instance. - /// - public float M21 { get { return Row1.X; } set { Row1.X = value; } } - - /// - /// Gets or sets the value at row 2, column 2 of this instance. - /// - public float M22 { get { return Row1.Y; } set { Row1.Y = value; } } - - /// - /// Gets or sets the value at row 3, column 1 of this instance. - /// - public float M31 { get { return Row2.X; } set { Row2.X = value; } } - - /// - /// Gets or sets the value at row 3, column 2 of this instance. - /// - public float M32 { get { return Row2.Y; } set { Row2.Y = value; } } - - /// - /// Gets or sets the value at row 4, column 1 of this instance. - /// - public float M41 { get { return Row3.X; } set { Row3.X = value; } } - - /// - /// Gets or sets the value at row 4, column 2 of this instance. - /// - public float M42 { get { return Row3.Y; } set { Row3.Y = value; } } - - #endregion - - #region Indexers - - /// - /// Gets or sets the value at a specified row and column. - /// - public float this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - else if (rowIndex == 2) return Row2[columnIndex]; - else if (rowIndex == 3) return Row3[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - else if (rowIndex == 2) Row2[columnIndex] = value; - else if (rowIndex == 3) Row3[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } - - #endregion - - #region Static - - #region CreateRotation - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix3x2 instance. - public static void CreateRotation(float angle, out Matrix4x2 result) - { - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = sin; - result.Row1.X = -sin; - result.Row1.Y = cos; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row3.X = 0; - result.Row3.Y = 0; - } - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix3x2 instance. - public static Matrix4x2 CreateRotation(float angle) - { - Matrix4x2 result; - CreateRotation(angle, out result); - return result; - } - - #endregion - - #region CreateScale - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x, y, and z axes. - /// A scale matrix. - public static void CreateScale(float scale, out Matrix4x2 result) - { - result.Row0.X = scale; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = scale; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row3.X = 0; - result.Row3.Y = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x and y axes. - /// A scale matrix. - public static Matrix4x2 CreateScale(float scale) - { - Matrix4x2 result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static void CreateScale(Vector2 scale, out Matrix4x2 result) - { - result.Row0.X = scale.X; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = scale.Y; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row3.X = 0; - result.Row3.Y = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static Matrix4x2 CreateScale(Vector2 scale) - { - Matrix4x2 result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static void CreateScale(float x, float y, out Matrix4x2 result) - { - result.Row0.X = x; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = y; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row3.X = 0; - result.Row3.Y = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static Matrix4x2 CreateScale(float x, float y) - { - Matrix4x2 result; - CreateScale(x, y, out result); - return result; - } - - #endregion - - #region Multiply Functions - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix4x2 left, float right, out Matrix4x2 result) - { - result.Row0.X = left.Row0.X * right; - result.Row0.Y = left.Row0.Y * right; - result.Row1.X = left.Row1.X * right; - result.Row1.Y = left.Row1.Y * right; - result.Row2.X = left.Row2.X * right; - result.Row2.Y = left.Row2.Y * right; - result.Row3.X = left.Row3.X * right; - result.Row3.Y = left.Row3.Y * right; - } - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix4x2 Mult(Matrix4x2 left, float right) - { - Matrix4x2 result; - Mult(ref left, right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix4x2 left, ref Matrix2 right, out Matrix4x2 result) - { - float lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - lM31 = left.Row2.X, lM32 = left.Row2.Y, - lM41 = left.Row3.X, lM42 = left.Row3.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, - rM21 = right.Row1.X, rM22 = right.Row1.Y; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); - result.Row3.X = (lM41 * rM11) + (lM42 * rM21); - result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix4x2 Mult(Matrix4x2 left, Matrix2 right) - { - Matrix4x2 result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix4x2 left, ref Matrix2x3 right, out Matrix4x3 result) - { - float lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - lM31 = left.Row2.X, lM32 = left.Row2.Y, - lM41 = left.Row3.X, lM42 = left.Row3.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); - result.Row3.X = (lM41 * rM11) + (lM42 * rM21); - result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); - result.Row3.Z = (lM41 * rM13) + (lM42 * rM23); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix4x3 Mult(Matrix4x2 left, Matrix2x3 right) - { - Matrix4x3 result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix4x2 left, ref Matrix2x4 right, out Matrix4 result) - { - float lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - lM31 = left.Row2.X, lM32 = left.Row2.Y, - lM41 = left.Row3.X, lM42 = left.Row3.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); - result.Row0.W = (lM11 * rM14) + (lM12 * rM24); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); - result.Row1.W = (lM21 * rM14) + (lM22 * rM24); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); - result.Row2.W = (lM31 * rM14) + (lM32 * rM24); - result.Row3.X = (lM41 * rM11) + (lM42 * rM21); - result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); - result.Row3.Z = (lM41 * rM13) + (lM42 * rM23); - result.Row3.W = (lM41 * rM14) + (lM42 * rM24); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix4 Mult(Matrix4x2 left, Matrix2x4 right) - { - Matrix4 result; - Mult(ref left, ref right, out result); - return result; - } - - #endregion - - #region Add - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static void Add(ref Matrix4x2 left, ref Matrix4x2 right, out Matrix4x2 result) - { - result.Row0.X = left.Row0.X + right.Row0.X; - result.Row0.Y = left.Row0.Y + right.Row0.Y; - result.Row1.X = left.Row1.X + right.Row1.X; - result.Row1.Y = left.Row1.Y + right.Row1.Y; - result.Row2.X = left.Row2.X + right.Row2.X; - result.Row2.Y = left.Row2.Y + right.Row2.Y; - result.Row3.X = left.Row3.X + right.Row3.X; - result.Row3.Y = left.Row3.Y + right.Row3.Y; - } - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static Matrix4x2 Add(Matrix4x2 left, Matrix4x2 right) - { - Matrix4x2 result; - Add(ref left, ref right, out result); - return result; - } - - #endregion - - #region Subtract - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static void Subtract(ref Matrix4x2 left, ref Matrix4x2 right, out Matrix4x2 result) - { - result.Row0.X = left.Row0.X - right.Row0.X; - result.Row0.Y = left.Row0.Y - right.Row0.Y; - result.Row1.X = left.Row1.X - right.Row1.X; - result.Row1.Y = left.Row1.Y - right.Row1.Y; - result.Row2.X = left.Row2.X - right.Row2.X; - result.Row2.Y = left.Row2.Y - right.Row2.Y; - result.Row3.X = left.Row3.X - right.Row3.X; - result.Row3.Y = left.Row3.Y - right.Row3.Y; - } - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static Matrix4x2 Subtract(Matrix4x2 left, Matrix4x2 right) - { - Matrix4x2 result; - Subtract(ref left, ref right, out result); - return result; - } - - #endregion - - #region Transpose - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static void Transpose(ref Matrix4x2 mat, out Matrix2x4 result) - { - result.Row0.X = mat.Row0.X; - result.Row0.Y = mat.Row1.X; - result.Row0.Z = mat.Row2.X; - result.Row0.W = mat.Row3.X; - result.Row1.X = mat.Row0.Y; - result.Row1.Y = mat.Row1.Y; - result.Row1.Z = mat.Row2.Y; - result.Row1.W = mat.Row3.Y; - } - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static Matrix2x4 Transpose(Matrix4x2 mat) - { - Matrix2x4 result; - Transpose(ref mat, out result); - return result; - } - - #endregion - - #endregion - - #region Operators - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4x2 which holds the result of the multiplication - public static Matrix4x2 operator *(float left, Matrix4x2 right) - { - return Mult(right, left); - } - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4x2 which holds the result of the multiplication - public static Matrix4x2 operator *(Matrix4x2 left, float right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2 which holds the result of the multiplication - public static Matrix4x2 operator *(Matrix4x2 left, Matrix2 right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4x3 which holds the result of the multiplication - public static Matrix4x3 operator *(Matrix4x2 left, Matrix2x3 right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4 which holds the result of the multiplication - public static Matrix4 operator *(Matrix4x2 left, Matrix2x4 right) - { - return Mult(left, right); - } - - /// - /// Matrix addition - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4x2 which holds the result of the addition - public static Matrix4x2 operator +(Matrix4x2 left, Matrix4x2 right) - { - return Add(left, right); - } - - /// - /// Matrix subtraction - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4x2 which holds the result of the subtraction - public static Matrix4x2 operator -(Matrix4x2 left, Matrix4x2 right) - { - return Subtract(left, right); - } - - /// - /// Compares two instances for equality. - /// - /// The first instance. - /// The second instance. - /// True, if left equals right; false otherwise. - public static bool operator ==(Matrix4x2 left, Matrix4x2 right) - { - return left.Equals(right); - } - - /// - /// Compares two instances for inequality. - /// - /// The first instance. - /// The second instance. - /// True, if left does not equal right; false otherwise. - public static bool operator !=(Matrix4x2 left, Matrix4x2 right) - { - return !left.Equals(right); - } - - #endregion - - #region Overrides - - #region public override string ToString() - - /// - /// Returns a System.String that represents the current Matrix3d. - /// - /// The string representation of the matrix. - public override string ToString() - { - return String.Format("{0}\n{1}\n{2}\n{3}", Row0, Row1, Row2, Row3); - } - - #endregion - - #region public override int GetHashCode() - - /// - /// Returns the hashcode for this instance. - /// - /// A System.Int32 containing the unique hashcode for this instance. - public override int GetHashCode() - { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode(); - } - - #endregion - - #region public override bool Equals(object obj) - - /// - /// Indicates whether this instance and a specified object are equal. - /// - /// The object to compare to. - /// True if the instances are equal; false otherwise. - public override bool Equals(object obj) - { - if (!(obj is Matrix4x2)) - return false; - - return this.Equals((Matrix4x2)obj); - } - - #endregion - - #endregion - - #endregion - - #region IEquatable Members - - /// - /// Indicates whether the current matrix is equal to another matrix. - /// - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(Matrix4x2 other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1 && - Row2 == other.Row2 && - Row3 == other.Row3; - } - - #endregion - } + /// + /// Represents a 4x2 matrix. + /// + public struct Matrix4x2 : IEquatable + { + #region Fields + + /// + /// Top row of the matrix. + /// + public Vector2 Row0; + + /// + /// Second row of the matrix. + /// + public Vector2 Row1; + + /// + /// Third row of the matrix. + /// + public Vector2 Row2; + + /// + /// Bottom row of the matrix. + /// + public Vector2 Row3; + + /// + /// The zero matrix. + /// + public static readonly Matrix4x2 Zero = new Matrix4x2(Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero); + + #endregion + + #region Constructors + + /// + /// Constructs a new instance. + /// + /// Top row of the matrix. + /// Second row of the matrix. + /// Third row of the matrix. + /// Bottom row of the matrix. + public Matrix4x2(Vector2 row0, Vector2 row1, Vector2 row2, Vector2 row3) + { + Row0 = row0; + Row1 = row1; + Row2 = row2; + Row3 = row3; + } + + + /// + /// Constructs a new instance + /// + /// First item of the first row of the matrix. + /// Second item of the first row of the matrix. + /// First item of the second row of the matrix. + /// Second item of the second row of the matrix. + /// First item of the third row of the matrix. + /// Second item of the third row of the matrix. + /// First item of the fourth row of the matrix. + /// Second item of the fourth row of the matrix. + public Matrix4x2( + float m00, float m01, + float m10, float m11, + float m20, float m21, + float m30, float m31) + { + Row0 = new Vector2(m00, m01); + Row1 = new Vector2(m10, m11); + Row2 = new Vector2(m20, m21); + Row3 = new Vector2(m30, m31); + } + + #endregion + + #region Public Members + + #region Properties + + /// + /// Gets or sets the first column of this matrix. + /// + public Vector4 Column0 + { + get { return new Vector4(Row0.X, Row1.X, Row2.X, Row3.X); } + set { Row0.X = value.X; Row1.X = value.Y; Row2.X = value.Z; Row3.X = value.W; } + } + + /// + /// Gets or sets the second column of this matrix. + /// + public Vector4 Column1 + { + get { return new Vector4(Row0.Y, Row1.Y, Row2.Y, Row3.X); } + set { Row0.Y = value.X; Row1.Y = value.Y; Row2.Y = value.Z; Row3.Y = value.W; } + } + + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public float M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public float M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public float M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public float M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 3, column 1 of this instance. + /// + public float M31 { get { return Row2.X; } set { Row2.X = value; } } + + /// + /// Gets or sets the value at row 3, column 2 of this instance. + /// + public float M32 { get { return Row2.Y; } set { Row2.Y = value; } } + + /// + /// Gets or sets the value at row 4, column 1 of this instance. + /// + public float M41 { get { return Row3.X; } set { Row3.X = value; } } + + /// + /// Gets or sets the value at row 4, column 2 of this instance. + /// + public float M42 { get { return Row3.Y; } set { Row3.Y = value; } } + + #endregion + + #region Indexers + + /// + /// Gets or sets the value at a specified row and column. + /// + public float this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + else if (rowIndex == 2) return Row2[columnIndex]; + else if (rowIndex == 3) return Row3[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + else if (rowIndex == 2) Row2[columnIndex] = value; + else if (rowIndex == 3) Row3[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } + + #endregion + + #region Static + + #region CreateRotation + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix3x2 instance. + public static void CreateRotation(float angle, out Matrix4x2 result) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = sin; + result.Row1.X = -sin; + result.Row1.Y = cos; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row3.X = 0; + result.Row3.Y = 0; + } + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix3x2 instance. + public static Matrix4x2 CreateRotation(float angle) + { + Matrix4x2 result; + CreateRotation(angle, out result); + return result; + } + + #endregion + + #region CreateScale + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x, y, and z axes. + /// A scale matrix. + public static void CreateScale(float scale, out Matrix4x2 result) + { + result.Row0.X = scale; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = scale; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row3.X = 0; + result.Row3.Y = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x and y axes. + /// A scale matrix. + public static Matrix4x2 CreateScale(float scale) + { + Matrix4x2 result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static void CreateScale(Vector2 scale, out Matrix4x2 result) + { + result.Row0.X = scale.X; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = scale.Y; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row3.X = 0; + result.Row3.Y = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static Matrix4x2 CreateScale(Vector2 scale) + { + Matrix4x2 result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static void CreateScale(float x, float y, out Matrix4x2 result) + { + result.Row0.X = x; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = y; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row3.X = 0; + result.Row3.Y = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static Matrix4x2 CreateScale(float x, float y) + { + Matrix4x2 result; + CreateScale(x, y, out result); + return result; + } + + #endregion + + #region Multiply Functions + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix4x2 left, float right, out Matrix4x2 result) + { + result.Row0.X = left.Row0.X * right; + result.Row0.Y = left.Row0.Y * right; + result.Row1.X = left.Row1.X * right; + result.Row1.Y = left.Row1.Y * right; + result.Row2.X = left.Row2.X * right; + result.Row2.Y = left.Row2.Y * right; + result.Row3.X = left.Row3.X * right; + result.Row3.Y = left.Row3.Y * right; + } + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix4x2 Mult(Matrix4x2 left, float right) + { + Matrix4x2 result; + Mult(ref left, right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix4x2 left, ref Matrix2 right, out Matrix4x2 result) + { + float lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + lM31 = left.Row2.X, lM32 = left.Row2.Y, + lM41 = left.Row3.X, lM42 = left.Row3.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, + rM21 = right.Row1.X, rM22 = right.Row1.Y; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); + result.Row3.X = (lM41 * rM11) + (lM42 * rM21); + result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix4x2 Mult(Matrix4x2 left, Matrix2 right) + { + Matrix4x2 result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix4x2 left, ref Matrix2x3 right, out Matrix4x3 result) + { + float lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + lM31 = left.Row2.X, lM32 = left.Row2.Y, + lM41 = left.Row3.X, lM42 = left.Row3.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); + result.Row3.X = (lM41 * rM11) + (lM42 * rM21); + result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); + result.Row3.Z = (lM41 * rM13) + (lM42 * rM23); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix4x3 Mult(Matrix4x2 left, Matrix2x3 right) + { + Matrix4x3 result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix4x2 left, ref Matrix2x4 right, out Matrix4 result) + { + float lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + lM31 = left.Row2.X, lM32 = left.Row2.Y, + lM41 = left.Row3.X, lM42 = left.Row3.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); + result.Row0.W = (lM11 * rM14) + (lM12 * rM24); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); + result.Row1.W = (lM21 * rM14) + (lM22 * rM24); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); + result.Row2.W = (lM31 * rM14) + (lM32 * rM24); + result.Row3.X = (lM41 * rM11) + (lM42 * rM21); + result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); + result.Row3.Z = (lM41 * rM13) + (lM42 * rM23); + result.Row3.W = (lM41 * rM14) + (lM42 * rM24); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix4 Mult(Matrix4x2 left, Matrix2x4 right) + { + Matrix4 result; + Mult(ref left, ref right, out result); + return result; + } + + #endregion + + #region Add + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static void Add(ref Matrix4x2 left, ref Matrix4x2 right, out Matrix4x2 result) + { + result.Row0.X = left.Row0.X + right.Row0.X; + result.Row0.Y = left.Row0.Y + right.Row0.Y; + result.Row1.X = left.Row1.X + right.Row1.X; + result.Row1.Y = left.Row1.Y + right.Row1.Y; + result.Row2.X = left.Row2.X + right.Row2.X; + result.Row2.Y = left.Row2.Y + right.Row2.Y; + result.Row3.X = left.Row3.X + right.Row3.X; + result.Row3.Y = left.Row3.Y + right.Row3.Y; + } + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static Matrix4x2 Add(Matrix4x2 left, Matrix4x2 right) + { + Matrix4x2 result; + Add(ref left, ref right, out result); + return result; + } + + #endregion + + #region Subtract + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static void Subtract(ref Matrix4x2 left, ref Matrix4x2 right, out Matrix4x2 result) + { + result.Row0.X = left.Row0.X - right.Row0.X; + result.Row0.Y = left.Row0.Y - right.Row0.Y; + result.Row1.X = left.Row1.X - right.Row1.X; + result.Row1.Y = left.Row1.Y - right.Row1.Y; + result.Row2.X = left.Row2.X - right.Row2.X; + result.Row2.Y = left.Row2.Y - right.Row2.Y; + result.Row3.X = left.Row3.X - right.Row3.X; + result.Row3.Y = left.Row3.Y - right.Row3.Y; + } + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static Matrix4x2 Subtract(Matrix4x2 left, Matrix4x2 right) + { + Matrix4x2 result; + Subtract(ref left, ref right, out result); + return result; + } + + #endregion + + #region Transpose + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static void Transpose(ref Matrix4x2 mat, out Matrix2x4 result) + { + result.Row0.X = mat.Row0.X; + result.Row0.Y = mat.Row1.X; + result.Row0.Z = mat.Row2.X; + result.Row0.W = mat.Row3.X; + result.Row1.X = mat.Row0.Y; + result.Row1.Y = mat.Row1.Y; + result.Row1.Z = mat.Row2.Y; + result.Row1.W = mat.Row3.Y; + } + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static Matrix2x4 Transpose(Matrix4x2 mat) + { + Matrix2x4 result; + Transpose(ref mat, out result); + return result; + } + + #endregion + + #endregion + + #region Operators + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4x2 which holds the result of the multiplication + public static Matrix4x2 operator *(float left, Matrix4x2 right) + { + return Mult(right, left); + } + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4x2 which holds the result of the multiplication + public static Matrix4x2 operator *(Matrix4x2 left, float right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2 which holds the result of the multiplication + public static Matrix4x2 operator *(Matrix4x2 left, Matrix2 right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4x3 which holds the result of the multiplication + public static Matrix4x3 operator *(Matrix4x2 left, Matrix2x3 right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4 which holds the result of the multiplication + public static Matrix4 operator *(Matrix4x2 left, Matrix2x4 right) + { + return Mult(left, right); + } + + /// + /// Matrix addition + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4x2 which holds the result of the addition + public static Matrix4x2 operator +(Matrix4x2 left, Matrix4x2 right) + { + return Add(left, right); + } + + /// + /// Matrix subtraction + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4x2 which holds the result of the subtraction + public static Matrix4x2 operator -(Matrix4x2 left, Matrix4x2 right) + { + return Subtract(left, right); + } + + /// + /// Compares two instances for equality. + /// + /// The first instance. + /// The second instance. + /// True, if left equals right; false otherwise. + public static bool operator ==(Matrix4x2 left, Matrix4x2 right) + { + return left.Equals(right); + } + + /// + /// Compares two instances for inequality. + /// + /// The first instance. + /// The second instance. + /// True, if left does not equal right; false otherwise. + public static bool operator !=(Matrix4x2 left, Matrix4x2 right) + { + return !left.Equals(right); + } + + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix3d. + /// + /// The string representation of the matrix. + public override string ToString() + { + return String.Format("{0}\n{1}\n{2}\n{3}", Row0, Row1, Row2, Row3); + } + + #endregion + + #region public override int GetHashCode() + + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode(); + } + + #endregion + + #region public override bool Equals(object obj) + + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare to. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Matrix4x2)) + return false; + + return this.Equals((Matrix4x2)obj); + } + + #endregion + + #endregion + + #endregion + + #region IEquatable Members + + /// + /// Indicates whether the current matrix is equal to another matrix. + /// + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix4x2 other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1 && + Row2 == other.Row2 && + Row3 == other.Row3; + } + + #endregion + } } diff --git a/Source/OpenTK/Math/Matrix4x2d.cs b/Source/OpenTK/Math/Matrix4x2d.cs index f6f32ff9..c75ff308 100644 --- a/Source/OpenTK/Math/Matrix4x2d.cs +++ b/Source/OpenTK/Math/Matrix4x2d.cs @@ -27,739 +27,739 @@ using System.Runtime.InteropServices; namespace OpenTK { - /// - /// Represents a 4x2 matrix. - /// - public struct Matrix4x2d : IEquatable - { - #region Fields - - /// - /// Top row of the matrix. - /// - public Vector2d Row0; - - /// - /// Second row of the matrix. - /// - public Vector2d Row1; - - /// - /// Third row of the matrix. - /// - public Vector2d Row2; - - /// - /// Bottom row of the matrix. - /// - public Vector2d Row3; - - /// - /// The zero matrix. - /// - public static readonly Matrix4x2d Zero = new Matrix4x2d(Vector2d.Zero, Vector2d.Zero, Vector2d.Zero, Vector2d.Zero); - - #endregion - - #region Constructors - - /// - /// Constructs a new instance. - /// - /// Top row of the matrix. - /// Second row of the matrix. - /// Third row of the matrix. - /// Bottom row of the matrix. - public Matrix4x2d(Vector2d row0, Vector2d row1, Vector2d row2, Vector2d row3) - { - Row0 = row0; - Row1 = row1; - Row2 = row2; - Row3 = row3; - } - - - /// - /// Constructs a new instance - /// - /// First item of the first row of the matrix. - /// Second item of the first row of the matrix. - /// First item of the second row of the matrix. - /// Second item of the second row of the matrix. - /// First item of the third row of the matrix. - /// Second item of the third row of the matrix. - /// First item of the fourth row of the matrix. - /// Second item of the fourth row of the matrix. - public Matrix4x2d( - double m00, double m01, - double m10, double m11, - double m20, double m21, - double m30, double m31) - { - Row0 = new Vector2d(m00, m01); - Row1 = new Vector2d(m10, m11); - Row2 = new Vector2d(m20, m21); - Row3 = new Vector2d(m30, m31); - } - - #endregion - - #region Public Members - - #region Properties - - /// - /// Gets or sets the first column of this matrix. - /// - public Vector4d Column0 - { - get { return new Vector4d(Row0.X, Row1.X, Row2.X, Row3.X); } - set { Row0.X = value.X; Row1.X = value.Y; Row2.X = value.Z; Row3.X = value.W; } - } - - /// - /// Gets or sets the second column of this matrix. - /// - public Vector4d Column1 - { - get { return new Vector4d(Row0.Y, Row1.Y, Row2.Y, Row3.X); } - set { Row0.Y = value.X; Row1.Y = value.Y; Row2.Y = value.Z; Row3.Y = value.W; } - } - - /// - /// Gets or sets the value at row 1, column 1 of this instance. - /// - public double M11 { get { return Row0.X; } set { Row0.X = value; } } - - /// - /// Gets or sets the value at row 1, column 2 of this instance. - /// - public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } - - /// - /// Gets or sets the value at row 2, column 1 of this instance. - /// - public double M21 { get { return Row1.X; } set { Row1.X = value; } } - - /// - /// Gets or sets the value at row 2, column 2 of this instance. - /// - public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } - - /// - /// Gets or sets the value at row 3, column 1 of this instance. - /// - public double M31 { get { return Row2.X; } set { Row2.X = value; } } - - /// - /// Gets or sets the value at row 3, column 2 of this instance. - /// - public double M32 { get { return Row2.Y; } set { Row2.Y = value; } } - - /// - /// Gets or sets the value at row 4, column 1 of this instance. - /// - public double M41 { get { return Row3.X; } set { Row3.X = value; } } - - /// - /// Gets or sets the value at row 4, column 2 of this instance. - /// - public double M42 { get { return Row3.Y; } set { Row3.Y = value; } } - - #endregion - - #region Indexers - - /// - /// Gets or sets the value at a specified row and column. - /// - public double this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - else if (rowIndex == 2) return Row2[columnIndex]; - else if (rowIndex == 3) return Row3[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - else if (rowIndex == 2) Row2[columnIndex] = value; - else if (rowIndex == 3) Row3[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } - - #endregion - - #region Static - - #region CreateRotation - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix3x2 instance. - public static void CreateRotation(double angle, out Matrix4x2d result) - { - double cos = (double)System.Math.Cos(angle); - double sin = (double)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = sin; - result.Row1.X = -sin; - result.Row1.Y = cos; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row3.X = 0; - result.Row3.Y = 0; - } - - /// - /// Builds a rotation matrix. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix3x2 instance. - public static Matrix4x2d CreateRotation(double angle) - { - Matrix4x2d result; - CreateRotation(angle, out result); - return result; - } - - #endregion - - #region CreateScale - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x, y, and z axes. - /// A scale matrix. - public static void CreateScale(double scale, out Matrix4x2d result) - { - result.Row0.X = scale; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = scale; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row3.X = 0; - result.Row3.Y = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Single scale factor for the x and y axes. - /// A scale matrix. - public static Matrix4x2d CreateScale(double scale) - { - Matrix4x2d result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static void CreateScale(Vector2d scale, out Matrix4x2d result) - { - result.Row0.X = scale.X; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = scale.Y; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row3.X = 0; - result.Row3.Y = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factors for the x and y axes. - /// A scale matrix. - public static Matrix4x2d CreateScale(Vector2d scale) - { - Matrix4x2d result; - CreateScale(scale, out result); - return result; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static void CreateScale(double x, double y, out Matrix4x2d result) - { - result.Row0.X = x; - result.Row0.Y = 0; - result.Row1.X = 0; - result.Row1.Y = y; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row3.X = 0; - result.Row3.Y = 0; - } - - /// - /// Creates a scale matrix. - /// - /// Scale factor for the x axis. - /// Scale factor for the y axis. - /// A scale matrix. - public static Matrix4x2d CreateScale(double x, double y) - { - Matrix4x2d result; - CreateScale(x, y, out result); - return result; - } - - #endregion - - #region Multiply Functions - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix4x2d left, double right, out Matrix4x2d result) - { - result.Row0.X = left.Row0.X * right; - result.Row0.Y = left.Row0.Y * right; - result.Row1.X = left.Row1.X * right; - result.Row1.Y = left.Row1.Y * right; - result.Row2.X = left.Row2.X * right; - result.Row2.Y = left.Row2.Y * right; - result.Row3.X = left.Row3.X * right; - result.Row3.Y = left.Row3.Y * right; - } - - /// - /// Multiplies and instance by a scalar. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix4x2d Mult(Matrix4x2d left, double right) - { - Matrix4x2d result; - Mult(ref left, right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix4x2d left, ref Matrix2d right, out Matrix4x2d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - lM31 = left.Row2.X, lM32 = left.Row2.Y, - lM41 = left.Row3.X, lM42 = left.Row3.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, - rM21 = right.Row1.X, rM22 = right.Row1.Y; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); - result.Row3.X = (lM41 * rM11) + (lM42 * rM21); - result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix4x2d Mult(Matrix4x2d left, Matrix2d right) - { - Matrix4x2d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix4x2d left, ref Matrix2x3d right, out Matrix4x3d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - lM31 = left.Row2.X, lM32 = left.Row2.Y, - lM41 = left.Row3.X, lM42 = left.Row3.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); - result.Row3.X = (lM41 * rM11) + (lM42 * rM21); - result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); - result.Row3.Z = (lM41 * rM13) + (lM42 * rM23); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix4x3d Mult(Matrix4x2d left, Matrix2x3d right) - { - Matrix4x3d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static void Mult(ref Matrix4x2d left, ref Matrix2x4d right, out Matrix4d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, - lM21 = left.Row1.X, lM22 = left.Row1.Y, - lM31 = left.Row2.X, lM32 = left.Row2.Y, - lM41 = left.Row3.X, lM42 = left.Row3.Y, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); - result.Row0.W = (lM11 * rM14) + (lM12 * rM24); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); - result.Row1.W = (lM21 * rM14) + (lM22 * rM24); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); - result.Row2.W = (lM31 * rM14) + (lM32 * rM24); - result.Row3.X = (lM41 * rM11) + (lM42 * rM21); - result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); - result.Row3.Z = (lM41 * rM13) + (lM42 * rM23); - result.Row3.W = (lM41 * rM14) + (lM42 * rM24); - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication. - public static Matrix4d Mult(Matrix4x2d left, Matrix2x4d right) - { - Matrix4d result; - Mult(ref left, ref right, out result); - return result; - } - - #endregion - - #region Add - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static void Add(ref Matrix4x2d left, ref Matrix4x2d right, out Matrix4x2d result) - { - result.Row0.X = left.Row0.X + right.Row0.X; - result.Row0.Y = left.Row0.Y + right.Row0.Y; - result.Row1.X = left.Row1.X + right.Row1.X; - result.Row1.Y = left.Row1.Y + right.Row1.Y; - result.Row2.X = left.Row2.X + right.Row2.X; - result.Row2.Y = left.Row2.Y + right.Row2.Y; - result.Row3.X = left.Row3.X + right.Row3.X; - result.Row3.Y = left.Row3.Y + right.Row3.Y; - } - - /// - /// Adds two instances. - /// - /// The left operand of the addition. - /// The right operand of the addition. - /// A new instance that is the result of the addition. - public static Matrix4x2d Add(Matrix4x2d left, Matrix4x2d right) - { - Matrix4x2d result; - Add(ref left, ref right, out result); - return result; - } - - #endregion - - #region Subtract - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static void Subtract(ref Matrix4x2d left, ref Matrix4x2d right, out Matrix4x2d result) - { - result.Row0.X = left.Row0.X - right.Row0.X; - result.Row0.Y = left.Row0.Y - right.Row0.Y; - result.Row1.X = left.Row1.X - right.Row1.X; - result.Row1.Y = left.Row1.Y - right.Row1.Y; - result.Row2.X = left.Row2.X - right.Row2.X; - result.Row2.Y = left.Row2.Y - right.Row2.Y; - result.Row3.X = left.Row3.X - right.Row3.X; - result.Row3.Y = left.Row3.Y - right.Row3.Y; - } - - /// - /// Subtracts two instances. - /// - /// The left operand of the subtraction. - /// The right operand of the subtraction. - /// A new instance that is the result of the subtraction. - public static Matrix4x2d Subtract(Matrix4x2d left, Matrix4x2d right) - { - Matrix4x2d result; - Subtract(ref left, ref right, out result); - return result; - } - - #endregion - - #region Transpose - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static void Transpose(ref Matrix4x2d mat, out Matrix2x4d result) - { - result.Row0.X = mat.Row0.X; - result.Row0.Y = mat.Row1.X; - result.Row0.Z = mat.Row2.X; - result.Row0.W = mat.Row3.X; - result.Row1.X = mat.Row0.Y; - result.Row1.Y = mat.Row1.Y; - result.Row1.Z = mat.Row2.Y; - result.Row1.W = mat.Row3.Y; - } - - /// - /// Calculate the transpose of the given matrix. - /// - /// The matrix to transpose. - /// The transpose of the given matrix. - public static Matrix2x4d Transpose(Matrix4x2d mat) - { - Matrix2x4d result; - Transpose(ref mat, out result); - return result; - } - - #endregion - - #endregion - - #region Operators - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4x2d which holds the result of the multiplication - public static Matrix4x2d operator *(double left, Matrix4x2d right) - { - return Mult(right, left); - } - - /// - /// Scalar multiplication. - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4x2d which holds the result of the multiplication - public static Matrix4x2d operator *(Matrix4x2d left, double right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix2d which holds the result of the multiplication - public static Matrix4x2d operator *(Matrix4x2d left, Matrix2d right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4x3d which holds the result of the multiplication - public static Matrix4x3d operator *(Matrix4x2d left, Matrix2x3d right) - { - return Mult(left, right); - } - - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4d which holds the result of the multiplication - public static Matrix4d operator *(Matrix4x2d left, Matrix2x4d right) - { - return Mult(left, right); - } - - /// - /// Matrix addition - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4x2d which holds the result of the addition - public static Matrix4x2d operator +(Matrix4x2d left, Matrix4x2d right) - { - return Add(left, right); - } - - /// - /// Matrix subtraction - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix4x2d which holds the result of the subtraction - public static Matrix4x2d operator -(Matrix4x2d left, Matrix4x2d right) - { - return Subtract(left, right); - } - - /// - /// Compares two instances for equality. - /// - /// The first instance. - /// The second instance. - /// True, if left equals right; false otherwise. - public static bool operator ==(Matrix4x2d left, Matrix4x2d right) - { - return left.Equals(right); - } - - /// - /// Compares two instances for inequality. - /// - /// The first instance. - /// The second instance. - /// True, if left does not equal right; false otherwise. - public static bool operator !=(Matrix4x2d left, Matrix4x2d right) - { - return !left.Equals(right); - } - - #endregion - - #region Overrides - - #region public override string ToString() - - /// - /// Returns a System.String that represents the current Matrix3d. - /// - /// The string representation of the matrix. - public override string ToString() - { - return String.Format("{0}\n{1}\n{2}\n{3}", Row0, Row1, Row2, Row3); - } - - #endregion - - #region public override int GetHashCode() - - /// - /// Returns the hashcode for this instance. - /// - /// A System.Int32 containing the unique hashcode for this instance. - public override int GetHashCode() - { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode(); - } - - #endregion - - #region public override bool Equals(object obj) - - /// - /// Indicates whether this instance and a specified object are equal. - /// - /// The object to compare to. - /// True if the instances are equal; false otherwise. - public override bool Equals(object obj) - { - if (!(obj is Matrix4x2d)) - return false; - - return this.Equals((Matrix4x2d)obj); - } - - #endregion - - #endregion - - #endregion - - #region IEquatable Members - - /// - /// Indicates whether the current matrix is equal to another matrix. - /// - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(Matrix4x2d other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1 && - Row2 == other.Row2 && - Row3 == other.Row3; - } - - #endregion - } + /// + /// Represents a 4x2 matrix. + /// + public struct Matrix4x2d : IEquatable + { + #region Fields + + /// + /// Top row of the matrix. + /// + public Vector2d Row0; + + /// + /// Second row of the matrix. + /// + public Vector2d Row1; + + /// + /// Third row of the matrix. + /// + public Vector2d Row2; + + /// + /// Bottom row of the matrix. + /// + public Vector2d Row3; + + /// + /// The zero matrix. + /// + public static readonly Matrix4x2d Zero = new Matrix4x2d(Vector2d.Zero, Vector2d.Zero, Vector2d.Zero, Vector2d.Zero); + + #endregion + + #region Constructors + + /// + /// Constructs a new instance. + /// + /// Top row of the matrix. + /// Second row of the matrix. + /// Third row of the matrix. + /// Bottom row of the matrix. + public Matrix4x2d(Vector2d row0, Vector2d row1, Vector2d row2, Vector2d row3) + { + Row0 = row0; + Row1 = row1; + Row2 = row2; + Row3 = row3; + } + + + /// + /// Constructs a new instance + /// + /// First item of the first row of the matrix. + /// Second item of the first row of the matrix. + /// First item of the second row of the matrix. + /// Second item of the second row of the matrix. + /// First item of the third row of the matrix. + /// Second item of the third row of the matrix. + /// First item of the fourth row of the matrix. + /// Second item of the fourth row of the matrix. + public Matrix4x2d( + double m00, double m01, + double m10, double m11, + double m20, double m21, + double m30, double m31) + { + Row0 = new Vector2d(m00, m01); + Row1 = new Vector2d(m10, m11); + Row2 = new Vector2d(m20, m21); + Row3 = new Vector2d(m30, m31); + } + + #endregion + + #region Public Members + + #region Properties + + /// + /// Gets or sets the first column of this matrix. + /// + public Vector4d Column0 + { + get { return new Vector4d(Row0.X, Row1.X, Row2.X, Row3.X); } + set { Row0.X = value.X; Row1.X = value.Y; Row2.X = value.Z; Row3.X = value.W; } + } + + /// + /// Gets or sets the second column of this matrix. + /// + public Vector4d Column1 + { + get { return new Vector4d(Row0.Y, Row1.Y, Row2.Y, Row3.X); } + set { Row0.Y = value.X; Row1.Y = value.Y; Row2.Y = value.Z; Row3.Y = value.W; } + } + + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public double M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public double M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 3, column 1 of this instance. + /// + public double M31 { get { return Row2.X; } set { Row2.X = value; } } + + /// + /// Gets or sets the value at row 3, column 2 of this instance. + /// + public double M32 { get { return Row2.Y; } set { Row2.Y = value; } } + + /// + /// Gets or sets the value at row 4, column 1 of this instance. + /// + public double M41 { get { return Row3.X; } set { Row3.X = value; } } + + /// + /// Gets or sets the value at row 4, column 2 of this instance. + /// + public double M42 { get { return Row3.Y; } set { Row3.Y = value; } } + + #endregion + + #region Indexers + + /// + /// Gets or sets the value at a specified row and column. + /// + public double this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + else if (rowIndex == 2) return Row2[columnIndex]; + else if (rowIndex == 3) return Row3[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + else if (rowIndex == 2) Row2[columnIndex] = value; + else if (rowIndex == 3) Row3[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } + + #endregion + + #region Static + + #region CreateRotation + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix3x2 instance. + public static void CreateRotation(double angle, out Matrix4x2d result) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = sin; + result.Row1.X = -sin; + result.Row1.Y = cos; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row3.X = 0; + result.Row3.Y = 0; + } + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix3x2 instance. + public static Matrix4x2d CreateRotation(double angle) + { + Matrix4x2d result; + CreateRotation(angle, out result); + return result; + } + + #endregion + + #region CreateScale + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x, y, and z axes. + /// A scale matrix. + public static void CreateScale(double scale, out Matrix4x2d result) + { + result.Row0.X = scale; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = scale; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row3.X = 0; + result.Row3.Y = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Single scale factor for the x and y axes. + /// A scale matrix. + public static Matrix4x2d CreateScale(double scale) + { + Matrix4x2d result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static void CreateScale(Vector2d scale, out Matrix4x2d result) + { + result.Row0.X = scale.X; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = scale.Y; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row3.X = 0; + result.Row3.Y = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factors for the x and y axes. + /// A scale matrix. + public static Matrix4x2d CreateScale(Vector2d scale) + { + Matrix4x2d result; + CreateScale(scale, out result); + return result; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static void CreateScale(double x, double y, out Matrix4x2d result) + { + result.Row0.X = x; + result.Row0.Y = 0; + result.Row1.X = 0; + result.Row1.Y = y; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row3.X = 0; + result.Row3.Y = 0; + } + + /// + /// Creates a scale matrix. + /// + /// Scale factor for the x axis. + /// Scale factor for the y axis. + /// A scale matrix. + public static Matrix4x2d CreateScale(double x, double y) + { + Matrix4x2d result; + CreateScale(x, y, out result); + return result; + } + + #endregion + + #region Multiply Functions + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix4x2d left, double right, out Matrix4x2d result) + { + result.Row0.X = left.Row0.X * right; + result.Row0.Y = left.Row0.Y * right; + result.Row1.X = left.Row1.X * right; + result.Row1.Y = left.Row1.Y * right; + result.Row2.X = left.Row2.X * right; + result.Row2.Y = left.Row2.Y * right; + result.Row3.X = left.Row3.X * right; + result.Row3.Y = left.Row3.Y * right; + } + + /// + /// Multiplies and instance by a scalar. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix4x2d Mult(Matrix4x2d left, double right) + { + Matrix4x2d result; + Mult(ref left, right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix4x2d left, ref Matrix2d right, out Matrix4x2d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + lM31 = left.Row2.X, lM32 = left.Row2.Y, + lM41 = left.Row3.X, lM42 = left.Row3.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, + rM21 = right.Row1.X, rM22 = right.Row1.Y; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); + result.Row3.X = (lM41 * rM11) + (lM42 * rM21); + result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix4x2d Mult(Matrix4x2d left, Matrix2d right) + { + Matrix4x2d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix4x2d left, ref Matrix2x3d right, out Matrix4x3d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + lM31 = left.Row2.X, lM32 = left.Row2.Y, + lM41 = left.Row3.X, lM42 = left.Row3.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); + result.Row3.X = (lM41 * rM11) + (lM42 * rM21); + result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); + result.Row3.Z = (lM41 * rM13) + (lM42 * rM23); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix4x3d Mult(Matrix4x2d left, Matrix2x3d right) + { + Matrix4x3d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static void Mult(ref Matrix4x2d left, ref Matrix2x4d right, out Matrix4d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, + lM21 = left.Row1.X, lM22 = left.Row1.Y, + lM31 = left.Row2.X, lM32 = left.Row2.Y, + lM41 = left.Row3.X, lM42 = left.Row3.Y, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23); + result.Row0.W = (lM11 * rM14) + (lM12 * rM24); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23); + result.Row1.W = (lM21 * rM14) + (lM22 * rM24); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23); + result.Row2.W = (lM31 * rM14) + (lM32 * rM24); + result.Row3.X = (lM41 * rM11) + (lM42 * rM21); + result.Row3.Y = (lM41 * rM12) + (lM42 * rM22); + result.Row3.Z = (lM41 * rM13) + (lM42 * rM23); + result.Row3.W = (lM41 * rM14) + (lM42 * rM24); + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication. + public static Matrix4d Mult(Matrix4x2d left, Matrix2x4d right) + { + Matrix4d result; + Mult(ref left, ref right, out result); + return result; + } + + #endregion + + #region Add + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static void Add(ref Matrix4x2d left, ref Matrix4x2d right, out Matrix4x2d result) + { + result.Row0.X = left.Row0.X + right.Row0.X; + result.Row0.Y = left.Row0.Y + right.Row0.Y; + result.Row1.X = left.Row1.X + right.Row1.X; + result.Row1.Y = left.Row1.Y + right.Row1.Y; + result.Row2.X = left.Row2.X + right.Row2.X; + result.Row2.Y = left.Row2.Y + right.Row2.Y; + result.Row3.X = left.Row3.X + right.Row3.X; + result.Row3.Y = left.Row3.Y + right.Row3.Y; + } + + /// + /// Adds two instances. + /// + /// The left operand of the addition. + /// The right operand of the addition. + /// A new instance that is the result of the addition. + public static Matrix4x2d Add(Matrix4x2d left, Matrix4x2d right) + { + Matrix4x2d result; + Add(ref left, ref right, out result); + return result; + } + + #endregion + + #region Subtract + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static void Subtract(ref Matrix4x2d left, ref Matrix4x2d right, out Matrix4x2d result) + { + result.Row0.X = left.Row0.X - right.Row0.X; + result.Row0.Y = left.Row0.Y - right.Row0.Y; + result.Row1.X = left.Row1.X - right.Row1.X; + result.Row1.Y = left.Row1.Y - right.Row1.Y; + result.Row2.X = left.Row2.X - right.Row2.X; + result.Row2.Y = left.Row2.Y - right.Row2.Y; + result.Row3.X = left.Row3.X - right.Row3.X; + result.Row3.Y = left.Row3.Y - right.Row3.Y; + } + + /// + /// Subtracts two instances. + /// + /// The left operand of the subtraction. + /// The right operand of the subtraction. + /// A new instance that is the result of the subtraction. + public static Matrix4x2d Subtract(Matrix4x2d left, Matrix4x2d right) + { + Matrix4x2d result; + Subtract(ref left, ref right, out result); + return result; + } + + #endregion + + #region Transpose + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static void Transpose(ref Matrix4x2d mat, out Matrix2x4d result) + { + result.Row0.X = mat.Row0.X; + result.Row0.Y = mat.Row1.X; + result.Row0.Z = mat.Row2.X; + result.Row0.W = mat.Row3.X; + result.Row1.X = mat.Row0.Y; + result.Row1.Y = mat.Row1.Y; + result.Row1.Z = mat.Row2.Y; + result.Row1.W = mat.Row3.Y; + } + + /// + /// Calculate the transpose of the given matrix. + /// + /// The matrix to transpose. + /// The transpose of the given matrix. + public static Matrix2x4d Transpose(Matrix4x2d mat) + { + Matrix2x4d result; + Transpose(ref mat, out result); + return result; + } + + #endregion + + #endregion + + #region Operators + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4x2d which holds the result of the multiplication + public static Matrix4x2d operator *(double left, Matrix4x2d right) + { + return Mult(right, left); + } + + /// + /// Scalar multiplication. + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4x2d which holds the result of the multiplication + public static Matrix4x2d operator *(Matrix4x2d left, double right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2d which holds the result of the multiplication + public static Matrix4x2d operator *(Matrix4x2d left, Matrix2d right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4x3d which holds the result of the multiplication + public static Matrix4x3d operator *(Matrix4x2d left, Matrix2x3d right) + { + return Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4d which holds the result of the multiplication + public static Matrix4d operator *(Matrix4x2d left, Matrix2x4d right) + { + return Mult(left, right); + } + + /// + /// Matrix addition + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4x2d which holds the result of the addition + public static Matrix4x2d operator +(Matrix4x2d left, Matrix4x2d right) + { + return Add(left, right); + } + + /// + /// Matrix subtraction + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix4x2d which holds the result of the subtraction + public static Matrix4x2d operator -(Matrix4x2d left, Matrix4x2d right) + { + return Subtract(left, right); + } + + /// + /// Compares two instances for equality. + /// + /// The first instance. + /// The second instance. + /// True, if left equals right; false otherwise. + public static bool operator ==(Matrix4x2d left, Matrix4x2d right) + { + return left.Equals(right); + } + + /// + /// Compares two instances for inequality. + /// + /// The first instance. + /// The second instance. + /// True, if left does not equal right; false otherwise. + public static bool operator !=(Matrix4x2d left, Matrix4x2d right) + { + return !left.Equals(right); + } + + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix3d. + /// + /// The string representation of the matrix. + public override string ToString() + { + return String.Format("{0}\n{1}\n{2}\n{3}", Row0, Row1, Row2, Row3); + } + + #endregion + + #region public override int GetHashCode() + + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode(); + } + + #endregion + + #region public override bool Equals(object obj) + + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare to. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Matrix4x2d)) + return false; + + return this.Equals((Matrix4x2d)obj); + } + + #endregion + + #endregion + + #endregion + + #region IEquatable Members + + /// + /// Indicates whether the current matrix is equal to another matrix. + /// + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix4x2d other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1 && + Row2 == other.Row2 && + Row3 == other.Row3; + } + + #endregion + } } diff --git a/Source/OpenTK/Math/Matrix4x3.cs b/Source/OpenTK/Math/Matrix4x3.cs index fde723db..4133e62e 100644 --- a/Source/OpenTK/Math/Matrix4x3.cs +++ b/Source/OpenTK/Math/Matrix4x3.cs @@ -27,842 +27,852 @@ using System.Runtime.InteropServices; namespace OpenTK { - /// - /// Represents a 3x4 matrix. - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - public struct Matrix4x3 : IEquatable - { - #region Fields - - /// - /// Top row of the matrix - /// - public Vector3 Row0; - - /// - /// 2nd row of the matrix - /// - public Vector3 Row1; - - /// - /// 3rd row of the matrix - /// - public Vector3 Row2; - - /// - /// Bottom row of the matrix - /// - public Vector3 Row3; - - /// - /// The zero matrix - /// - public static Matrix4x3 Zero = new Matrix4x3(Vector3.Zero, Vector3.Zero, Vector3.Zero, Vector3.Zero); - - #endregion - - #region Constructors - - /// - /// Constructs a new instance. - /// - /// Top row of the matrix - /// Second row of the matrix - /// Third row of the matrix - /// Bottom row of the matrix - public Matrix4x3(Vector3 row0, Vector3 row1, Vector3 row2, Vector3 row3) - { - Row0 = row0; - Row1 = row1; - Row2 = row2; - Row3 = row3; - } - - /// - /// Constructs a new instance. - /// - /// First item of the first row of the matrix. - /// Second item of the first row of the matrix. - /// Third item of the first row of the matrix. - /// First item of the second row of the matrix. - /// Second item of the second row of the matrix. - /// Third item of the second row of the matrix. - /// First item of the third row of the matrix. - /// Second item of the third row of the matrix. - /// Third item of the third row of the matrix. - /// First item of the fourth row of the matrix. - /// Second item of the fourth row of the matrix. - /// Third item of the fourth row of the matrix. - public Matrix4x3( - float m00, float m01, float m02, - float m10, float m11, float m12, - float m20, float m21, float m22, - float m30, float m31, float m32) - { - Row0 = new Vector3(m00, m01, m02); - Row1 = new Vector3(m10, m11, m12); - Row2 = new Vector3(m20, m21, m22); - Row3 = new Vector3(m30, m31, m32); - } - - #endregion - - #region Public Members - - #region Properties - - /// - /// Gets the first column of this matrix. - /// - public Vector4 Column0 - { - get { return new Vector4(Row0.X, Row1.X, Row2.X, Row3.X); } - } - - /// - /// Gets the second column of this matrix. - /// - public Vector4 Column1 - { - get { return new Vector4(Row0.Y, Row1.Y, Row2.Y, Row3.Y); } - } - - /// - /// Gets the third column of this matrix. - /// - public Vector4 Column2 - { - get { return new Vector4(Row0.Z, Row1.Z, Row2.Z, Row3.Z); } - } - - /// - /// Gets or sets the value at row 1, column 1 of this instance. - /// - public float M11 { get { return Row0.X; } set { Row0.X = value; } } - - /// - /// Gets or sets the value at row 1, column 2 of this instance. - /// - public float M12 { get { return Row0.Y; } set { Row0.Y = value; } } - - /// - /// Gets or sets the value at row 1, column 3 of this instance. - /// - public float M13 { get { return Row0.Z; } set { Row0.Z = value; } } - - /// - /// Gets or sets the value at row 2, column 1 of this instance. - /// - public float M21 { get { return Row1.X; } set { Row1.X = value; } } - - /// - /// Gets or sets the value at row 2, column 2 of this instance. - /// - public float M22 { get { return Row1.Y; } set { Row1.Y = value; } } - - /// - /// Gets or sets the value at row 2, column 3 of this instance. - /// - public float M23 { get { return Row1.Z; } set { Row1.Z = value; } } - - /// - /// Gets or sets the value at row 3, column 1 of this instance. - /// - public float M31 { get { return Row2.X; } set { Row2.X = value; } } - - /// - /// Gets or sets the value at row 3, column 2 of this instance. - /// - public float M32 { get { return Row2.Y; } set { Row2.Y = value; } } - - /// - /// Gets or sets the value at row 3, column 3 of this instance. - /// - public float M33 { get { return Row2.Z; } set { Row2.Z = value; } } - - /// - /// Gets or sets the value at row 4, column 1 of this instance. - /// - public float M41 { get { return Row3.X; } set { Row3.X = value; } } - - /// - /// Gets or sets the value at row 4, column 2 of this instance. - /// - public float M42 { get { return Row3.Y; } set { Row3.Y = value; } } - - /// - /// Gets or sets the value at row 4, column 3 of this instance. - /// - public float M43 { get { return Row3.Z; } set { Row3.Z = value; } } - - #endregion - - #region Indexers - - /// - /// Gets or sets the value at a specified row and column. - /// - public float this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - else if (rowIndex == 2) return Row2[columnIndex]; - else if (rowIndex == 3) return Row3[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - else if (rowIndex == 2) Row2[columnIndex] = value; - else if (rowIndex == 3) Row3[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } - - #endregion - - #region Instance - - #region public void Invert() - - public void Invert() - { - this = Matrix4x3.Invert(this); - } - - #endregion - - #endregion - - #region Static - - #region CreateFromAxisAngle - - /// - /// Build a rotation matrix from the specified axis/angle rotation. - /// - /// The axis to rotate about. - /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). - /// A matrix instance. - public static void CreateFromAxisAngle(Vector3 axis, float angle, out Matrix4x3 result) - { - axis.Normalize(); - float axisX = axis.X, axisY = axis.Y, axisZ = axis.Z; - - float cos = (float)System.Math.Cos(-angle); - float sin = (float)System.Math.Sin(-angle); - float t = 1.0f - cos; - - float tXX = t * axisX * axisX, - tXY = t * axisX * axisY, - tXZ = t * axisX * axisZ, - tYY = t * axisY * axisY, - tYZ = t * axisY * axisZ, - tZZ = t * axisZ * axisZ; - - float sinX = sin * axisX, - sinY = sin * axisY, - sinZ = sin * axisZ; - - result.Row0.X = tXX + cos; - result.Row0.Y = tXY - sinZ; - result.Row0.Z = tXZ + sinY; - result.Row1.X = tXY + sinZ; - result.Row1.Y = tYY + cos; - result.Row1.Z = tYZ - sinX; - result.Row2.X = tXZ - sinY; - result.Row2.Y = tYZ + sinX; - result.Row2.Z = tZZ + cos; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - } - - /// - /// Build a rotation matrix from the specified axis/angle rotation. - /// - /// The axis to rotate about. - /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). - /// A matrix instance. - public static Matrix4x3 CreateFromAxisAngle(Vector3 axis, float angle) - { - Matrix4x3 result; - CreateFromAxisAngle(axis, angle, out result); - return result; - } - - #endregion - - #region CreateFromQuaternion - - /// - /// Builds a rotation matrix from a quaternion. - /// - /// The quaternion to rotate by. - /// A matrix instance. - public static void CreateFromQuaternion(ref Quaternion q, out Matrix4x3 result) - { - float x = q.X, y = q.Y, z = q.Z, w = q.W, - tx = 2 * x, ty = 2 * y, tz = 2 * z, - txx = tx * x, tyy = ty * y, tzz = tz * z, - txy = tx * y, txz = tx * z, tyz = ty * z, - twx = w * tx, twy = w * ty, twz = w * tz; - - result.Row0.X = 1f - tyy - tzz; - result.Row0.Y = txy - twz; - result.Row0.Z = txz + twy; - result.Row1.X = txy + twz; - result.Row1.Y = 1f - txx - tzz; - result.Row1.Z = tyz - twx; - result.Row2.X = txz - twy; - result.Row2.Y = tyz + twx; - result.Row2.Z = 1f - txx - tyy; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - - /*Vector3 axis; - float angle; - q.ToAxisAngle(out axis, out angle); - CreateFromAxisAngle(axis, angle, out result);*/ - } - - /// - /// Builds a rotation matrix from a quaternion. - /// - /// The quaternion to rotate by. - /// A matrix instance. - public static Matrix4x3 CreateFromQuaternion(Quaternion q) - { - Matrix4x3 result; - CreateFromQuaternion(ref q, out result); - return result; - } - - #endregion - - #region CreateRotation[XYZ] - - /// - /// Builds a rotation matrix for a rotation around the x-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static void CreateRotationX(float angle, out Matrix4x3 result) - { - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = cos; - result.Row1.Z = sin; - result.Row2.X = 0; - result.Row2.Y = -sin; - result.Row2.Z = cos; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the x-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static Matrix4x3 CreateRotationX(float angle) - { - Matrix4x3 result; - CreateRotationX(angle, out result); - return result; - } - - /// - /// Builds a rotation matrix for a rotation around the y-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static void CreateRotationY(float angle, out Matrix4x3 result) - { - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = 0; - result.Row0.Z = -sin; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row2.X = sin; - result.Row2.Y = 0; - result.Row2.Z = cos; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the y-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static Matrix4x3 CreateRotationY(float angle) - { - Matrix4x3 result; - CreateRotationY(angle, out result); - return result; - } - - /// - /// Builds a rotation matrix for a rotation around the z-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static void CreateRotationZ(float angle, out Matrix4x3 result) - { - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = sin; - result.Row0.Z = 0; - result.Row1.X = -sin; - result.Row1.Y = cos; - result.Row1.Z = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the z-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4 instance. - public static Matrix4x3 CreateRotationZ(float angle) - { - Matrix4x3 result; - CreateRotationZ(angle, out result); - return result; - } - - #endregion - - #region CreateTranslation - - /// - /// Creates a translation matrix. - /// - /// X translation. - /// Y translation. - /// Z translation. - /// The resulting Matrix4 instance. - public static void CreateTranslation(float x, float y, float z, out Matrix4x3 result) - { - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row3.X = x; - result.Row3.Y = y; - result.Row3.Z = z; - } - - /// - /// Creates a translation matrix. - /// - /// The translation vector. - /// The resulting Matrix4 instance. - public static void CreateTranslation(ref Vector3 vector, out Matrix4x3 result) - { - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row3.X = vector.X; - result.Row3.Y = vector.Y; - result.Row3.Z = vector.Z; - } - - /// - /// Creates a translation matrix. - /// - /// X translation. - /// Y translation. - /// Z translation. - /// The resulting Matrix4 instance. - public static Matrix4x3 CreateTranslation(float x, float y, float z) - { - Matrix4x3 result; - CreateTranslation(x, y, z, out result); - return result; - } - - /// - /// Creates a translation matrix. - /// - /// The translation vector. - /// The resulting Matrix4 instance. - public static Matrix4x3 CreateTranslation(Vector3 vector) - { - Matrix4x3 result; - CreateTranslation(vector.X, vector.Y, vector.Z, out result); - return result; - } - - #endregion - - #region CreateScale - - /// - /// Build a scaling matrix - /// - /// Single scale factor for x,y and z axes - /// A scaling matrix - public static Matrix4x3 CreateScale(float scale) - { - return CreateScale(scale, scale, scale); - } - - /// - /// Build a scaling matrix - /// - /// Scale factors for x,y and z axes - /// A scaling matrix - public static Matrix4x3 CreateScale(Vector3 scale) - { - return CreateScale(scale.X, scale.Y, scale.Z); - } - - /// - /// Build a scaling matrix - /// - /// Scale factor for x-axis - /// Scale factor for y-axis - /// Scale factor for z-axis - /// A scaling matrix - public static Matrix4x3 CreateScale(float x, float y, float z) - { - Matrix4x3 result; - result.Row0.X = x; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = y; - result.Row1.Z = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = z; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - return result; - } - - #endregion - - #region Multiply Functions - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication - public static Matrix4 Mult(Matrix4x3 left, Matrix3x4 right) - { - Matrix4 result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication - public static void Mult(ref Matrix4x3 left, ref Matrix3x4 right, out Matrix4 result) - { - float lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, - lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, - lM41 = left.Row3.X, lM42 = left.Row3.Y, lM43 = left.Row3.Z, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, - rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33); - result.Row0.W = (lM11 * rM14) + (lM12 * rM24) + (lM13 * rM34); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33); - result.Row1.W = (lM21 * rM14) + (lM22 * rM24) + (lM23 * rM34); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33); - result.Row2.W = (lM31 * rM14) + (lM32 * rM24) + (lM33 * rM34); - result.Row3.X = (lM41 * rM11) + (lM42 * rM21) + (lM43 * rM31); - result.Row3.Y = (lM41 * rM12) + (lM42 * rM22) + (lM43 * rM32); - result.Row3.Z = (lM41 * rM13) + (lM42 * rM23) + (lM43 * rM33); - result.Row3.W = (lM41 * rM14) + (lM42 * rM24) + (lM43 * rM34); - } - - public static Matrix4x3 Mult(Matrix4x3 left, Matrix4x3 right) - { - Matrix4x3 result; - Mult(ref left, ref right, out result); - return result; - } - - public static void Mult(ref Matrix4x3 left, ref Matrix4x3 right, out Matrix4x3 result) - { - float lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, - lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, - lM41 = left.Row3.X, lM42 = left.Row3.Y, lM43 = left.Row3.Z, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, - rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, - rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31) + rM41; - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32) + rM42; - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33) + rM43; - result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31) + rM41; - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32) + rM42; - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33) + rM43; - result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31) + rM41; - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32) + rM42; - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33) + rM43; - result.Row3.X = (lM41 * rM11) + (lM42 * rM21) + (lM43 * rM31) + rM41; - result.Row3.Y = (lM41 * rM12) + (lM42 * rM22) + (lM43 * rM32) + rM42; - result.Row3.Z = (lM41 * rM13) + (lM42 * rM23) + (lM43 * rM33) + rM43; - } - - public static Matrix4x3 Mult(Matrix4x3 left, float right) - { - Matrix4x3 result; - Mult(ref left, right, out result); - return result; - } - - public static void Mult(ref Matrix4x3 left, float right, out Matrix4x3 result) - { - result.Row0 = left.Row0 * right; - result.Row1 = left.Row1 * right; - result.Row2 = left.Row2 * right; - result.Row3 = left.Row3 * right; - } - - #endregion - - #region Add Functions - - public static Matrix4x3 Add(Matrix4x3 left, Matrix4x3 right) - { - Matrix4x3 result; - Add(ref left, ref right, out result); - return result; - } - - public static void Add(ref Matrix4x3 left, ref Matrix4x3 right, out Matrix4x3 result) - { - result.Row0 = left.Row0 + right.Row0; - result.Row1 = left.Row1 + right.Row1; - result.Row2 = left.Row2 + right.Row2; - result.Row3 = left.Row3 + right.Row3; - } - - #endregion - - #region Subtract Functions - - public static Matrix4x3 Subtract(Matrix4x3 left, Matrix4x3 right) - { - Matrix4x3 result; - Subtract(ref left, ref right, out result); - return result; - } - - public static void Subtract(ref Matrix4x3 left, ref Matrix4x3 right, out Matrix4x3 result) - { - result.Row0 = left.Row0 - right.Row0; - result.Row1 = left.Row1 - right.Row1; - result.Row2 = left.Row2 - right.Row2; - result.Row3 = left.Row3 - right.Row3; - } - - #endregion - - #region Invert Functions - - public static Matrix4x3 Invert(Matrix4x3 mat) - { - Matrix4x3 result; - Invert(ref mat, out result); - return result; - } - - public static void Invert(ref Matrix4x3 mat, out Matrix4x3 result) - { - Matrix3 inverseRotation = new Matrix3(mat.Column0.Xyz, mat.Column1.Xyz, mat.Column2.Xyz); - inverseRotation.Row0 /= inverseRotation.Row0.LengthSquared; - inverseRotation.Row1 /= inverseRotation.Row1.LengthSquared; - inverseRotation.Row2 /= inverseRotation.Row2.LengthSquared; - - Vector3 translation = mat.Row3; - - result.Row0 = inverseRotation.Row0; - result.Row1 = inverseRotation.Row1; - result.Row2 = inverseRotation.Row2; - result.Row3 = new Vector3(-Vector3.Dot(inverseRotation.Row0, translation), -Vector3.Dot(inverseRotation.Row1, translation), -Vector3.Dot(inverseRotation.Row2, translation)); - } - - #endregion - - #region Transpose - - public static Matrix3x4 Transpose(Matrix4x3 mat) - { - return new Matrix3x4(mat.Column0, mat.Column1, mat.Column2); - } - - public static void Transpose(ref Matrix4x3 mat, out Matrix3x4 result) - { - result.Row0 = mat.Column0; - result.Row1 = mat.Column1; - result.Row2 = mat.Column2; - } - - #endregion - - #endregion - - #region Operators - - public static Matrix4 operator *(Matrix4x3 left, Matrix3x4 right) - { - return Matrix4x3.Mult(left, right); - } - - public static Matrix4x3 operator *(Matrix4x3 left, Matrix4x3 right) - { - return Matrix4x3.Mult(left, right); - } - - public static Matrix4x3 operator *(Matrix4x3 left, float right) - { - return Matrix4x3.Mult(left, right); - } - - public static Matrix4x3 operator +(Matrix4x3 left, Matrix4x3 right) - { - return Matrix4x3.Add(left, right); - } - - public static Matrix4x3 operator -(Matrix4x3 left, Matrix4x3 right) - { - return Matrix4x3.Subtract(left, right); - } - - public static bool operator ==(Matrix4x3 left, Matrix4x3 right) - { - return left.Equals(right); - } - - public static bool operator !=(Matrix4x3 left, Matrix4x3 right) - { - return !left.Equals(right); - } - - #endregion - - #region Overrides - - #region public override string ToString() - - /// - /// Returns a System.String that represents the current Matrix4x3. - /// - /// The string representation of the matrix. - public override string ToString() - { - return string.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); - } - - #endregion + /// + /// Represents a 3x4 matrix. + /// + [Serializable] + [StructLayout(LayoutKind.Sequential)] + public struct Matrix4x3 : IEquatable + { + #region Fields + + /// + /// Top row of the matrix + /// + public Vector3 Row0; + + /// + /// 2nd row of the matrix + /// + public Vector3 Row1; + + /// + /// 3rd row of the matrix + /// + public Vector3 Row2; + + /// + /// Bottom row of the matrix + /// + public Vector3 Row3; + + /// + /// The zero matrix + /// + public static readonly Matrix4x3 Zero = new Matrix4x3(Vector3.Zero, Vector3.Zero, Vector3.Zero, Vector3.Zero); + + #endregion + + #region Constructors + + /// + /// Constructs a new instance. + /// + /// Top row of the matrix + /// Second row of the matrix + /// Third row of the matrix + /// Bottom row of the matrix + public Matrix4x3(Vector3 row0, Vector3 row1, Vector3 row2, Vector3 row3) + { + Row0 = row0; + Row1 = row1; + Row2 = row2; + Row3 = row3; + } + + /// + /// Constructs a new instance. + /// + /// First item of the first row of the matrix. + /// Second item of the first row of the matrix. + /// Third item of the first row of the matrix. + /// First item of the second row of the matrix. + /// Second item of the second row of the matrix. + /// Third item of the second row of the matrix. + /// First item of the third row of the matrix. + /// Second item of the third row of the matrix. + /// Third item of the third row of the matrix. + /// First item of the fourth row of the matrix. + /// Second item of the fourth row of the matrix. + /// Third item of the fourth row of the matrix. + public Matrix4x3( + float m00, float m01, float m02, + float m10, float m11, float m12, + float m20, float m21, float m22, + float m30, float m31, float m32) + { + Row0 = new Vector3(m00, m01, m02); + Row1 = new Vector3(m10, m11, m12); + Row2 = new Vector3(m20, m21, m22); + Row3 = new Vector3(m30, m31, m32); + } + + #endregion + + #region Public Members + + #region Properties + + /// + /// Gets the first column of this matrix. + /// + public Vector4 Column0 + { + get { return new Vector4(Row0.X, Row1.X, Row2.X, Row3.X); } + } + + /// + /// Gets the second column of this matrix. + /// + public Vector4 Column1 + { + get { return new Vector4(Row0.Y, Row1.Y, Row2.Y, Row3.Y); } + } + + /// + /// Gets the third column of this matrix. + /// + public Vector4 Column2 + { + get { return new Vector4(Row0.Z, Row1.Z, Row2.Z, Row3.Z); } + } + + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public float M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public float M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 1, column 3 of this instance. + /// + public float M13 { get { return Row0.Z; } set { Row0.Z = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public float M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public float M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 3 of this instance. + /// + public float M23 { get { return Row1.Z; } set { Row1.Z = value; } } + + /// + /// Gets or sets the value at row 3, column 1 of this instance. + /// + public float M31 { get { return Row2.X; } set { Row2.X = value; } } + + /// + /// Gets or sets the value at row 3, column 2 of this instance. + /// + public float M32 { get { return Row2.Y; } set { Row2.Y = value; } } + + /// + /// Gets or sets the value at row 3, column 3 of this instance. + /// + public float M33 { get { return Row2.Z; } set { Row2.Z = value; } } + + /// + /// Gets or sets the value at row 4, column 1 of this instance. + /// + public float M41 { get { return Row3.X; } set { Row3.X = value; } } + + /// + /// Gets or sets the value at row 4, column 2 of this instance. + /// + public float M42 { get { return Row3.Y; } set { Row3.Y = value; } } + + /// + /// Gets or sets the value at row 4, column 3 of this instance. + /// + public float M43 { get { return Row3.Z; } set { Row3.Z = value; } } + + #endregion + + #region Indexers + + /// + /// Gets or sets the value at a specified row and column. + /// + public float this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + else if (rowIndex == 2) return Row2[columnIndex]; + else if (rowIndex == 3) return Row3[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + else if (rowIndex == 2) Row2[columnIndex] = value; + else if (rowIndex == 3) Row3[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } + + #endregion + + #region Instance + + #region public void Invert() + + /// + /// Converts this instance into it's inverse by inverting the upper-left 3x3 and replacing Row3. + /// + public void Invert() + { + this = Matrix4x3.Invert(this); + } + + #endregion + + #endregion + + #region Static + + #region CreateFromAxisAngle + + /// + /// Build a rotation matrix from the specified axis/angle rotation. + /// + /// The axis to rotate about. + /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). + /// A matrix instance. + public static void CreateFromAxisAngle(Vector3 axis, float angle, out Matrix4x3 result) + { + axis.Normalize(); + float axisX = axis.X, axisY = axis.Y, axisZ = axis.Z; + + float cos = (float)System.Math.Cos(-angle); + float sin = (float)System.Math.Sin(-angle); + float t = 1.0f - cos; + + float tXX = t * axisX * axisX, + tXY = t * axisX * axisY, + tXZ = t * axisX * axisZ, + tYY = t * axisY * axisY, + tYZ = t * axisY * axisZ, + tZZ = t * axisZ * axisZ; + + float sinX = sin * axisX, + sinY = sin * axisY, + sinZ = sin * axisZ; + + result.Row0.X = tXX + cos; + result.Row0.Y = tXY - sinZ; + result.Row0.Z = tXZ + sinY; + result.Row1.X = tXY + sinZ; + result.Row1.Y = tYY + cos; + result.Row1.Z = tYZ - sinX; + result.Row2.X = tXZ - sinY; + result.Row2.Y = tYZ + sinX; + result.Row2.Z = tZZ + cos; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + } + + /// + /// Build a rotation matrix from the specified axis/angle rotation. + /// + /// The axis to rotate about. + /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). + /// A matrix instance. + public static Matrix4x3 CreateFromAxisAngle(Vector3 axis, float angle) + { + Matrix4x3 result; + CreateFromAxisAngle(axis, angle, out result); + return result; + } + + #endregion + + #region CreateFromQuaternion + + /// + /// Builds a rotation matrix from a quaternion. + /// + /// The quaternion to rotate by. + /// A matrix instance. + public static void CreateFromQuaternion(ref Quaternion q, out Matrix4x3 result) + { + float x = q.X, y = q.Y, z = q.Z, w = q.W, + tx = 2 * x, ty = 2 * y, tz = 2 * z, + txx = tx * x, tyy = ty * y, tzz = tz * z, + txy = tx * y, txz = tx * z, tyz = ty * z, + twx = w * tx, twy = w * ty, twz = w * tz; + + result.Row0.X = 1f - tyy - tzz; + result.Row0.Y = txy - twz; + result.Row0.Z = txz + twy; + result.Row1.X = txy + twz; + result.Row1.Y = 1f - txx - tzz; + result.Row1.Z = tyz - twx; + result.Row2.X = txz - twy; + result.Row2.Y = tyz + twx; + result.Row2.Z = 1f - txx - tyy; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + + /*Vector3 axis; + float angle; + q.ToAxisAngle(out axis, out angle); + CreateFromAxisAngle(axis, angle, out result);*/ + } + + /// + /// Builds a rotation matrix from a quaternion. + /// + /// The quaternion to rotate by. + /// A matrix instance. + public static Matrix4x3 CreateFromQuaternion(Quaternion q) + { + Matrix4x3 result; + CreateFromQuaternion(ref q, out result); + return result; + } + + #endregion + + #region CreateRotation[XYZ] + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationX(float angle, out Matrix4x3 result) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = cos; + result.Row1.Z = sin; + result.Row2.X = 0; + result.Row2.Y = -sin; + result.Row2.Z = cos; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix4x3 CreateRotationX(float angle) + { + Matrix4x3 result; + CreateRotationX(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationY(float angle, out Matrix4x3 result) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = 0; + result.Row0.Z = -sin; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row2.X = sin; + result.Row2.Y = 0; + result.Row2.Z = cos; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix4x3 CreateRotationY(float angle) + { + Matrix4x3 result; + CreateRotationY(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static void CreateRotationZ(float angle, out Matrix4x3 result) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = sin; + result.Row0.Z = 0; + result.Row1.X = -sin; + result.Row1.Y = cos; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4 instance. + public static Matrix4x3 CreateRotationZ(float angle) + { + Matrix4x3 result; + CreateRotationZ(angle, out result); + return result; + } + + #endregion + + #region CreateTranslation + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4 instance. + public static void CreateTranslation(float x, float y, float z, out Matrix4x3 result) + { + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row3.X = x; + result.Row3.Y = y; + result.Row3.Z = z; + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4 instance. + public static void CreateTranslation(ref Vector3 vector, out Matrix4x3 result) + { + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row3.X = vector.X; + result.Row3.Y = vector.Y; + result.Row3.Z = vector.Z; + } + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4 instance. + public static Matrix4x3 CreateTranslation(float x, float y, float z) + { + Matrix4x3 result; + CreateTranslation(x, y, z, out result); + return result; + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4 instance. + public static Matrix4x3 CreateTranslation(Vector3 vector) + { + Matrix4x3 result; + CreateTranslation(vector.X, vector.Y, vector.Z, out result); + return result; + } + + #endregion + + #region CreateScale + + /// + /// Build a scaling matrix + /// + /// Single scale factor for x,y and z axes + /// A scaling matrix + public static Matrix4x3 CreateScale(float scale) + { + return CreateScale(scale, scale, scale); + } + + /// + /// Build a scaling matrix + /// + /// Scale factors for x,y and z axes + /// A scaling matrix + public static Matrix4x3 CreateScale(Vector3 scale) + { + return CreateScale(scale.X, scale.Y, scale.Z); + } + + /// + /// Build a scaling matrix + /// + /// Scale factor for x-axis + /// Scale factor for y-axis + /// Scale factor for z-axis + /// A scaling matrix + public static Matrix4x3 CreateScale(float x, float y, float z) + { + Matrix4x3 result; + result.Row0.X = x; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = y; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = z; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + return result; + } + + #endregion + + #region Multiply Functions + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication + public static Matrix4 Mult(Matrix4x3 left, Matrix3x4 right) + { + Matrix4 result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication + public static void Mult(ref Matrix4x3 left, ref Matrix3x4 right, out Matrix4 result) + { + float lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, + lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, + lM41 = left.Row3.X, lM42 = left.Row3.Y, lM43 = left.Row3.Z, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, + rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33); + result.Row0.W = (lM11 * rM14) + (lM12 * rM24) + (lM13 * rM34); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33); + result.Row1.W = (lM21 * rM14) + (lM22 * rM24) + (lM23 * rM34); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33); + result.Row2.W = (lM31 * rM14) + (lM32 * rM24) + (lM33 * rM34); + result.Row3.X = (lM41 * rM11) + (lM42 * rM21) + (lM43 * rM31); + result.Row3.Y = (lM41 * rM12) + (lM42 * rM22) + (lM43 * rM32); + result.Row3.Z = (lM41 * rM13) + (lM42 * rM23) + (lM43 * rM33); + result.Row3.W = (lM41 * rM14) + (lM42 * rM24) + (lM43 * rM34); + } + + public static Matrix4x3 Mult(Matrix4x3 left, Matrix4x3 right) + { + Matrix4x3 result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication + public static void Mult(ref Matrix4x3 left, ref Matrix4x3 right, out Matrix4x3 result) + { + float lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, + lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, + lM41 = left.Row3.X, lM42 = left.Row3.Y, lM43 = left.Row3.Z, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, + rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, + rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31) + rM41; + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32) + rM42; + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33) + rM43; + result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31) + rM41; + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32) + rM42; + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33) + rM43; + result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31) + rM41; + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32) + rM42; + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33) + rM43; + result.Row3.X = (lM41 * rM11) + (lM42 * rM21) + (lM43 * rM31) + rM41; + result.Row3.Y = (lM41 * rM12) + (lM42 * rM22) + (lM43 * rM32) + rM42; + result.Row3.Z = (lM41 * rM13) + (lM42 * rM23) + (lM43 * rM33) + rM43; + } + + public static Matrix4x3 Mult(Matrix4x3 left, float right) + { + Matrix4x3 result; + Mult(ref left, right, out result); + return result; + } + + + public static void Mult(ref Matrix4x3 left, float right, out Matrix4x3 result) + { + result.Row0 = left.Row0 * right; + result.Row1 = left.Row1 * right; + result.Row2 = left.Row2 * right; + result.Row3 = left.Row3 * right; + } + + #endregion + + #region Add Functions + + public static Matrix4x3 Add(Matrix4x3 left, Matrix4x3 right) + { + Matrix4x3 result; + Add(ref left, ref right, out result); + return result; + } + + public static void Add(ref Matrix4x3 left, ref Matrix4x3 right, out Matrix4x3 result) + { + result.Row0 = left.Row0 + right.Row0; + result.Row1 = left.Row1 + right.Row1; + result.Row2 = left.Row2 + right.Row2; + result.Row3 = left.Row3 + right.Row3; + } + + #endregion + + #region Subtract Functions + + public static Matrix4x3 Subtract(Matrix4x3 left, Matrix4x3 right) + { + Matrix4x3 result; + Subtract(ref left, ref right, out result); + return result; + } + + public static void Subtract(ref Matrix4x3 left, ref Matrix4x3 right, out Matrix4x3 result) + { + result.Row0 = left.Row0 - right.Row0; + result.Row1 = left.Row1 - right.Row1; + result.Row2 = left.Row2 - right.Row2; + result.Row3 = left.Row3 - right.Row3; + } + + #endregion + + #region Invert Functions + + public static Matrix4x3 Invert(Matrix4x3 mat) + { + Matrix4x3 result; + Invert(ref mat, out result); + return result; + } + + public static void Invert(ref Matrix4x3 mat, out Matrix4x3 result) + { + Matrix3 inverseRotation = new Matrix3(mat.Column0.Xyz, mat.Column1.Xyz, mat.Column2.Xyz); + inverseRotation.Row0 /= inverseRotation.Row0.LengthSquared; + inverseRotation.Row1 /= inverseRotation.Row1.LengthSquared; + inverseRotation.Row2 /= inverseRotation.Row2.LengthSquared; + + Vector3 translation = mat.Row3; + + result.Row0 = inverseRotation.Row0; + result.Row1 = inverseRotation.Row1; + result.Row2 = inverseRotation.Row2; + result.Row3 = new Vector3(-Vector3.Dot(inverseRotation.Row0, translation), -Vector3.Dot(inverseRotation.Row1, translation), -Vector3.Dot(inverseRotation.Row2, translation)); + } + + #endregion + + #region Transpose + + public static Matrix3x4 Transpose(Matrix4x3 mat) + { + return new Matrix3x4(mat.Column0, mat.Column1, mat.Column2); + } + + public static void Transpose(ref Matrix4x3 mat, out Matrix3x4 result) + { + result.Row0 = mat.Column0; + result.Row1 = mat.Column1; + result.Row2 = mat.Column2; + } + + #endregion + + #endregion + + #region Operators + + public static Matrix4 operator *(Matrix4x3 left, Matrix3x4 right) + { + return Matrix4x3.Mult(left, right); + } + + public static Matrix4x3 operator *(Matrix4x3 left, Matrix4x3 right) + { + return Matrix4x3.Mult(left, right); + } + + public static Matrix4x3 operator *(Matrix4x3 left, float right) + { + return Matrix4x3.Mult(left, right); + } + + public static Matrix4x3 operator +(Matrix4x3 left, Matrix4x3 right) + { + return Matrix4x3.Add(left, right); + } + + public static Matrix4x3 operator -(Matrix4x3 left, Matrix4x3 right) + { + return Matrix4x3.Subtract(left, right); + } + + public static bool operator ==(Matrix4x3 left, Matrix4x3 right) + { + return left.Equals(right); + } + + public static bool operator !=(Matrix4x3 left, Matrix4x3 right) + { + return !left.Equals(right); + } + + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix4x3. + /// + /// The string representation of the matrix. + public override string ToString() + { + return string.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); + } + + #endregion - #region public override int GetHashCode() - - /// - /// Returns the hashcode for this instance. - /// - /// A System.Int32 containing the unique hashcode for this instance. - public override int GetHashCode() - { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); - } - - #endregion - - #region public override bool Equals(object obj) - - /// - /// Indicates whether this instance and a specified object are equal. - /// - /// The object to compare tresult. - /// True if the instances are equal; false otherwise. - public override bool Equals(object obj) - { - if (!(obj is Matrix4x3)) - return false; + #region public override int GetHashCode() + + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + } + + #endregion + + #region public override bool Equals(object obj) + + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare tresult. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Matrix4x3)) + return false; - return this.Equals((Matrix4x3)obj); - } + return this.Equals((Matrix4x3)obj); + } - #endregion - - #endregion - - #endregion - - #region IEquatable Members - - /// Indicates whether the current matrix is equal to another matrix. - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(Matrix4x3 other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1 && - Row2 == other.Row2 && - Row3 == other.Row3; - } - - #endregion - } + #endregion + + #endregion + + #endregion + + #region IEquatable Members + + /// Indicates whether the current matrix is equal to another matrix. + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix4x3 other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1 && + Row2 == other.Row2 && + Row3 == other.Row3; + } + + #endregion + } } diff --git a/Source/OpenTK/Math/Matrix4x3d.cs b/Source/OpenTK/Math/Matrix4x3d.cs index 163b04ef..b6b24f3a 100644 --- a/Source/OpenTK/Math/Matrix4x3d.cs +++ b/Source/OpenTK/Math/Matrix4x3d.cs @@ -27,842 +27,842 @@ using System.Runtime.InteropServices; namespace OpenTK { - /// - /// Represents a 3x4 matrix. - /// - [Serializable] - [StructLayout(LayoutKind.Sequential)] - public struct Matrix4x3d : IEquatable - { - #region Fields - - /// - /// Top row of the matrix - /// - public Vector3d Row0; - - /// - /// 2nd row of the matrix - /// - public Vector3d Row1; - - /// - /// 3rd row of the matrix - /// - public Vector3d Row2; - - /// - /// Bottom row of the matrix - /// - public Vector3d Row3; - - /// - /// The zero matrix - /// - public static Matrix4x3d Zero = new Matrix4x3d(Vector3d.Zero, Vector3d.Zero, Vector3d.Zero, Vector3d.Zero); - - #endregion - - #region Constructors - - /// - /// Constructs a new instance. - /// - /// Top row of the matrix - /// Second row of the matrix - /// Third row of the matrix - /// Bottom row of the matrix - public Matrix4x3d(Vector3d row0, Vector3d row1, Vector3d row2, Vector3d row3) - { - Row0 = row0; - Row1 = row1; - Row2 = row2; - Row3 = row3; - } - - /// - /// Constructs a new instance. - /// - /// First item of the first row of the matrix. - /// Second item of the first row of the matrix. - /// Third item of the first row of the matrix. - /// First item of the second row of the matrix. - /// Second item of the second row of the matrix. - /// Third item of the second row of the matrix. - /// First item of the third row of the matrix. - /// Second item of the third row of the matrix. - /// Third item of the third row of the matrix. - /// First item of the fourth row of the matrix. - /// Second item of the fourth row of the matrix. - /// Third item of the fourth row of the matrix. - public Matrix4x3d( - double m00, double m01, double m02, - double m10, double m11, double m12, - double m20, double m21, double m22, - double m30, double m31, double m32) - { - Row0 = new Vector3d(m00, m01, m02); - Row1 = new Vector3d(m10, m11, m12); - Row2 = new Vector3d(m20, m21, m22); - Row3 = new Vector3d(m30, m31, m32); - } - - #endregion - - #region Public Members - - #region Properties - - /// - /// Gets the first column of this matrix. - /// - public Vector4d Column0 - { - get { return new Vector4d(Row0.X, Row1.X, Row2.X, Row3.X); } - } - - /// - /// Gets the second column of this matrix. - /// - public Vector4d Column1 - { - get { return new Vector4d(Row0.Y, Row1.Y, Row2.Y, Row3.Y); } - } - - /// - /// Gets the third column of this matrix. - /// - public Vector4d Column2 - { - get { return new Vector4d(Row0.Z, Row1.Z, Row2.Z, Row3.Z); } - } - - /// - /// Gets or sets the value at row 1, column 1 of this instance. - /// - public double M11 { get { return Row0.X; } set { Row0.X = value; } } - - /// - /// Gets or sets the value at row 1, column 2 of this instance. - /// - public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } - - /// - /// Gets or sets the value at row 1, column 3 of this instance. - /// - public double M13 { get { return Row0.Z; } set { Row0.Z = value; } } - - /// - /// Gets or sets the value at row 2, column 1 of this instance. - /// - public double M21 { get { return Row1.X; } set { Row1.X = value; } } - - /// - /// Gets or sets the value at row 2, column 2 of this instance. - /// - public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } - - /// - /// Gets or sets the value at row 2, column 3 of this instance. - /// - public double M23 { get { return Row1.Z; } set { Row1.Z = value; } } - - /// - /// Gets or sets the value at row 3, column 1 of this instance. - /// - public double M31 { get { return Row2.X; } set { Row2.X = value; } } - - /// - /// Gets or sets the value at row 3, column 2 of this instance. - /// - public double M32 { get { return Row2.Y; } set { Row2.Y = value; } } - - /// - /// Gets or sets the value at row 3, column 3 of this instance. - /// - public double M33 { get { return Row2.Z; } set { Row2.Z = value; } } - - /// - /// Gets or sets the value at row 4, column 1 of this instance. - /// - public double M41 { get { return Row3.X; } set { Row3.X = value; } } - - /// - /// Gets or sets the value at row 4, column 2 of this instance. - /// - public double M42 { get { return Row3.Y; } set { Row3.Y = value; } } - - /// - /// Gets or sets the value at row 4, column 3 of this instance. - /// - public double M43 { get { return Row3.Z; } set { Row3.Z = value; } } - - #endregion - - #region Indexers - - /// - /// Gets or sets the value at a specified row and column. - /// - public double this[int rowIndex, int columnIndex] - { - get - { - if (rowIndex == 0) return Row0[columnIndex]; - else if (rowIndex == 1) return Row1[columnIndex]; - else if (rowIndex == 2) return Row2[columnIndex]; - else if (rowIndex == 3) return Row3[columnIndex]; - throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - set - { - if (rowIndex == 0) Row0[columnIndex] = value; - else if (rowIndex == 1) Row1[columnIndex] = value; - else if (rowIndex == 2) Row2[columnIndex] = value; - else if (rowIndex == 3) Row3[columnIndex] = value; - throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); - } - } - - #endregion - - #region Instance - - #region public void Invert() - - public void Invert() - { - this = Matrix4x3d.Invert(this); - } - - #endregion - - #endregion - - #region Static - - #region CreateFromAxisAngle - - /// - /// Build a rotation matrix from the specified axis/angle rotation. - /// - /// The axis to rotate about. - /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). - /// A matrix instance. - public static void CreateFromAxisAngle(Vector3d axis, double angle, out Matrix4x3d result) - { - axis.Normalize(); - double axisX = axis.X, axisY = axis.Y, axisZ = axis.Z; - - double cos = (double)System.Math.Cos(-angle); - double sin = (double)System.Math.Sin(-angle); - double t = 1.0f - cos; - - double tXX = t * axisX * axisX, - tXY = t * axisX * axisY, - tXZ = t * axisX * axisZ, - tYY = t * axisY * axisY, - tYZ = t * axisY * axisZ, - tZZ = t * axisZ * axisZ; - - double sinX = sin * axisX, - sinY = sin * axisY, - sinZ = sin * axisZ; - - result.Row0.X = tXX + cos; - result.Row0.Y = tXY - sinZ; - result.Row0.Z = tXZ + sinY; - result.Row1.X = tXY + sinZ; - result.Row1.Y = tYY + cos; - result.Row1.Z = tYZ - sinX; - result.Row2.X = tXZ - sinY; - result.Row2.Y = tYZ + sinX; - result.Row2.Z = tZZ + cos; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - } - - /// - /// Build a rotation matrix from the specified axis/angle rotation. - /// - /// The axis to rotate about. - /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). - /// A matrix instance. - public static Matrix4x3d CreateFromAxisAngle(Vector3d axis, double angle) - { - Matrix4x3d result; - CreateFromAxisAngle(axis, angle, out result); - return result; - } - - #endregion - - #region CreateFromQuaternion - - /// - /// Builds a rotation matrix from a quaternion. - /// - /// The quaternion to rotate by. - /// A matrix instance. - public static void CreateFromQuaternion(ref Quaternion q, out Matrix4x3d result) - { - double x = q.X, y = q.Y, z = q.Z, w = q.W, - tx = 2 * x, ty = 2 * y, tz = 2 * z, - txx = tx * x, tyy = ty * y, tzz = tz * z, - txy = tx * y, txz = tx * z, tyz = ty * z, - twx = w * tx, twy = w * ty, twz = w * tz; - - result.Row0.X = 1f - tyy - tzz; - result.Row0.Y = txy - twz; - result.Row0.Z = txz + twy; - result.Row1.X = txy + twz; - result.Row1.Y = 1f - txx - tzz; - result.Row1.Z = tyz - twx; - result.Row2.X = txz - twy; - result.Row2.Y = tyz + twx; - result.Row2.Z = 1f - txx - tyy; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - - /*Vector3d axis; - double angle; - q.ToAxisAngle(out axis, out angle); - CreateFromAxisAngle(axis, angle, out result);*/ - } - - /// - /// Builds a rotation matrix from a quaternion. - /// - /// The quaternion to rotate by. - /// A matrix instance. - public static Matrix4x3d CreateFromQuaternion(Quaternion q) - { - Matrix4x3d result; - CreateFromQuaternion(ref q, out result); - return result; - } - - #endregion - - #region CreateRotation[XYZ] - - /// - /// Builds a rotation matrix for a rotation around the x-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4dinstance. - public static void CreateRotationX(double angle, out Matrix4x3d result) - { - double cos = (double)System.Math.Cos(angle); - double sin = (double)System.Math.Sin(angle); - - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = cos; - result.Row1.Z = sin; - result.Row2.X = 0; - result.Row2.Y = -sin; - result.Row2.Z = cos; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the x-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4dinstance. - public static Matrix4x3d CreateRotationX(double angle) - { - Matrix4x3d result; - CreateRotationX(angle, out result); - return result; - } - - /// - /// Builds a rotation matrix for a rotation around the y-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4dinstance. - public static void CreateRotationY(double angle, out Matrix4x3d result) - { - double cos = (double)System.Math.Cos(angle); - double sin = (double)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = 0; - result.Row0.Z = -sin; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row2.X = sin; - result.Row2.Y = 0; - result.Row2.Z = cos; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the y-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4dinstance. - public static Matrix4x3d CreateRotationY(double angle) - { - Matrix4x3d result; - CreateRotationY(angle, out result); - return result; - } - - /// - /// Builds a rotation matrix for a rotation around the z-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4dinstance. - public static void CreateRotationZ(double angle, out Matrix4x3d result) - { - double cos = (double)System.Math.Cos(angle); - double sin = (double)System.Math.Sin(angle); - - result.Row0.X = cos; - result.Row0.Y = sin; - result.Row0.Z = 0; - result.Row1.X = -sin; - result.Row1.Y = cos; - result.Row1.Z = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - } - - /// - /// Builds a rotation matrix for a rotation around the z-axis. - /// - /// The counter-clockwise angle in radians. - /// The resulting Matrix4dinstance. - public static Matrix4x3d CreateRotationZ(double angle) - { - Matrix4x3d result; - CreateRotationZ(angle, out result); - return result; - } - - #endregion - - #region CreateTranslation - - /// - /// Creates a translation matrix. - /// - /// X translation. - /// Y translation. - /// Z translation. - /// The resulting Matrix4dinstance. - public static void CreateTranslation(double x, double y, double z, out Matrix4x3d result) - { - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row3.X = x; - result.Row3.Y = y; - result.Row3.Z = z; - } - - /// - /// Creates a translation matrix. - /// - /// The translation vector. - /// The resulting Matrix4dinstance. - public static void CreateTranslation(ref Vector3d vector, out Matrix4x3d result) - { - result.Row0.X = 1; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = 1; - result.Row1.Z = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = 1; - result.Row3.X = vector.X; - result.Row3.Y = vector.Y; - result.Row3.Z = vector.Z; - } - - /// - /// Creates a translation matrix. - /// - /// X translation. - /// Y translation. - /// Z translation. - /// The resulting Matrix4dinstance. - public static Matrix4x3d CreateTranslation(double x, double y, double z) - { - Matrix4x3d result; - CreateTranslation(x, y, z, out result); - return result; - } - - /// - /// Creates a translation matrix. - /// - /// The translation vector. - /// The resulting Matrix4dinstance. - public static Matrix4x3d CreateTranslation(Vector3d vector) - { - Matrix4x3d result; - CreateTranslation(vector.X, vector.Y, vector.Z, out result); - return result; - } - - #endregion - - #region CreateScale - - /// - /// Build a scaling matrix - /// - /// Single scale factor for x,y and z axes - /// A scaling matrix - public static Matrix4x3d CreateScale(double scale) - { - return CreateScale(scale, scale, scale); - } - - /// - /// Build a scaling matrix - /// - /// Scale factors for x,y and z axes - /// A scaling matrix - public static Matrix4x3d CreateScale(Vector3d scale) - { - return CreateScale(scale.X, scale.Y, scale.Z); - } - - /// - /// Build a scaling matrix - /// - /// Scale factor for x-axis - /// Scale factor for y-axis - /// Scale factor for z-axis - /// A scaling matrix - public static Matrix4x3d CreateScale(double x, double y, double z) - { - Matrix4x3d result; - result.Row0.X = x; - result.Row0.Y = 0; - result.Row0.Z = 0; - result.Row1.X = 0; - result.Row1.Y = y; - result.Row1.Z = 0; - result.Row2.X = 0; - result.Row2.Y = 0; - result.Row2.Z = z; - result.Row3.X = 0; - result.Row3.Y = 0; - result.Row3.Z = 0; - return result; - } - - #endregion - - #region Multiply Functions - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication - public static Matrix4d Mult(Matrix4x3d left, Matrix3x4d right) - { - Matrix4d result; - Mult(ref left, ref right, out result); - return result; - } - - /// - /// Multiplies two instances. - /// - /// The left operand of the multiplication. - /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication - public static void Mult(ref Matrix4x3d left, ref Matrix3x4d right, out Matrix4d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, - lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, - lM41 = left.Row3.X, lM42 = left.Row3.Y, lM43 = left.Row3.Z, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, - rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31); - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32); - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33); - result.Row0.W = (lM11 * rM14) + (lM12 * rM24) + (lM13 * rM34); - result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31); - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32); - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33); - result.Row1.W = (lM21 * rM14) + (lM22 * rM24) + (lM23 * rM34); - result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31); - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32); - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33); - result.Row2.W = (lM31 * rM14) + (lM32 * rM24) + (lM33 * rM34); - result.Row3.X = (lM41 * rM11) + (lM42 * rM21) + (lM43 * rM31); - result.Row3.Y = (lM41 * rM12) + (lM42 * rM22) + (lM43 * rM32); - result.Row3.Z = (lM41 * rM13) + (lM42 * rM23) + (lM43 * rM33); - result.Row3.W = (lM41 * rM14) + (lM42 * rM24) + (lM43 * rM34); - } - - public static Matrix4x3d Mult(Matrix4x3d left, Matrix4x3d right) - { - Matrix4x3d result; - Mult(ref left, ref right, out result); - return result; - } - - public static void Mult(ref Matrix4x3d left, ref Matrix4x3d right, out Matrix4x3d result) - { - double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, - lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, - lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, - lM41 = left.Row3.X, lM42 = left.Row3.Y, lM43 = left.Row3.Z, - rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, - rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, - rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, - rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z; - - result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31) + rM41; - result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32) + rM42; - result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33) + rM43; - result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31) + rM41; - result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32) + rM42; - result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33) + rM43; - result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31) + rM41; - result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32) + rM42; - result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33) + rM43; - result.Row3.X = (lM41 * rM11) + (lM42 * rM21) + (lM43 * rM31) + rM41; - result.Row3.Y = (lM41 * rM12) + (lM42 * rM22) + (lM43 * rM32) + rM42; - result.Row3.Z = (lM41 * rM13) + (lM42 * rM23) + (lM43 * rM33) + rM43; - } - - public static Matrix4x3d Mult(Matrix4x3d left, double right) - { - Matrix4x3d result; - Mult(ref left, right, out result); - return result; - } - - public static void Mult(ref Matrix4x3d left, double right, out Matrix4x3d result) - { - result.Row0 = left.Row0 * right; - result.Row1 = left.Row1 * right; - result.Row2 = left.Row2 * right; - result.Row3 = left.Row3 * right; - } - - #endregion - - #region Add Functions - - public static Matrix4x3d Add(Matrix4x3d left, Matrix4x3d right) - { - Matrix4x3d result; - Add(ref left, ref right, out result); - return result; - } - - public static void Add(ref Matrix4x3d left, ref Matrix4x3d right, out Matrix4x3d result) - { - result.Row0 = left.Row0 + right.Row0; - result.Row1 = left.Row1 + right.Row1; - result.Row2 = left.Row2 + right.Row2; - result.Row3 = left.Row3 + right.Row3; - } - - #endregion - - #region Subtract Functions - - public static Matrix4x3d Subtract(Matrix4x3d left, Matrix4x3d right) - { - Matrix4x3d result; - Subtract(ref left, ref right, out result); - return result; - } - - public static void Subtract(ref Matrix4x3d left, ref Matrix4x3d right, out Matrix4x3d result) - { - result.Row0 = left.Row0 - right.Row0; - result.Row1 = left.Row1 - right.Row1; - result.Row2 = left.Row2 - right.Row2; - result.Row3 = left.Row3 - right.Row3; - } - - #endregion - - #region Invert Functions - - public static Matrix4x3d Invert(Matrix4x3d mat) - { - Matrix4x3d result; - Invert(ref mat, out result); - return result; - } - - public static void Invert(ref Matrix4x3d mat, out Matrix4x3d result) - { - Matrix3d inverseRotation = new Matrix3d(mat.Column0.Xyz, mat.Column1.Xyz, mat.Column2.Xyz); - inverseRotation.Row0 /= inverseRotation.Row0.LengthSquared; - inverseRotation.Row1 /= inverseRotation.Row1.LengthSquared; - inverseRotation.Row2 /= inverseRotation.Row2.LengthSquared; - - Vector3d translation = mat.Row3; - - result.Row0 = inverseRotation.Row0; - result.Row1 = inverseRotation.Row1; - result.Row2 = inverseRotation.Row2; - result.Row3 = new Vector3d(-Vector3d.Dot(inverseRotation.Row0, translation), -Vector3d.Dot(inverseRotation.Row1, translation), -Vector3d.Dot(inverseRotation.Row2, translation)); - } - - #endregion - - #region Transpose - - public static Matrix3x4d Transpose(Matrix4x3d mat) - { - return new Matrix3x4d(mat.Column0, mat.Column1, mat.Column2); - } - - public static void Transpose(ref Matrix4x3d mat, out Matrix3x4d result) - { - result.Row0 = mat.Column0; - result.Row1 = mat.Column1; - result.Row2 = mat.Column2; - } - - #endregion - - #endregion - - #region Operators - - public static Matrix4d operator *(Matrix4x3d left, Matrix3x4d right) - { - return Matrix4x3d.Mult(left, right); - } - - public static Matrix4x3d operator *(Matrix4x3d left, Matrix4x3d right) - { - return Matrix4x3d.Mult(left, right); - } - - public static Matrix4x3d operator *(Matrix4x3d left, double right) - { - return Matrix4x3d.Mult(left, right); - } - - public static Matrix4x3d operator +(Matrix4x3d left, Matrix4x3d right) - { - return Matrix4x3d.Add(left, right); - } - - public static Matrix4x3d operator -(Matrix4x3d left, Matrix4x3d right) - { - return Matrix4x3d.Subtract(left, right); - } - - public static bool operator ==(Matrix4x3d left, Matrix4x3d right) - { - return left.Equals(right); - } - - public static bool operator !=(Matrix4x3d left, Matrix4x3d right) - { - return !left.Equals(right); - } - - #endregion - - #region Overrides - - #region public override string ToString() - - /// - /// Returns a System.String that represents the current Matrix4x3d. - /// - /// The string representation of the matrix. - public override string ToString() - { - return string.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); - } - - #endregion + /// + /// Represents a 3x4 matrix. + /// + [Serializable] + [StructLayout(LayoutKind.Sequential)] + public struct Matrix4x3d : IEquatable + { + #region Fields + + /// + /// Top row of the matrix + /// + public Vector3d Row0; + + /// + /// 2nd row of the matrix + /// + public Vector3d Row1; + + /// + /// 3rd row of the matrix + /// + public Vector3d Row2; + + /// + /// Bottom row of the matrix + /// + public Vector3d Row3; + + /// + /// The zero matrix + /// + public static Matrix4x3d Zero = new Matrix4x3d(Vector3d.Zero, Vector3d.Zero, Vector3d.Zero, Vector3d.Zero); + + #endregion + + #region Constructors + + /// + /// Constructs a new instance. + /// + /// Top row of the matrix + /// Second row of the matrix + /// Third row of the matrix + /// Bottom row of the matrix + public Matrix4x3d(Vector3d row0, Vector3d row1, Vector3d row2, Vector3d row3) + { + Row0 = row0; + Row1 = row1; + Row2 = row2; + Row3 = row3; + } + + /// + /// Constructs a new instance. + /// + /// First item of the first row of the matrix. + /// Second item of the first row of the matrix. + /// Third item of the first row of the matrix. + /// First item of the second row of the matrix. + /// Second item of the second row of the matrix. + /// Third item of the second row of the matrix. + /// First item of the third row of the matrix. + /// Second item of the third row of the matrix. + /// Third item of the third row of the matrix. + /// First item of the fourth row of the matrix. + /// Second item of the fourth row of the matrix. + /// Third item of the fourth row of the matrix. + public Matrix4x3d( + double m00, double m01, double m02, + double m10, double m11, double m12, + double m20, double m21, double m22, + double m30, double m31, double m32) + { + Row0 = new Vector3d(m00, m01, m02); + Row1 = new Vector3d(m10, m11, m12); + Row2 = new Vector3d(m20, m21, m22); + Row3 = new Vector3d(m30, m31, m32); + } + + #endregion + + #region Public Members + + #region Properties + + /// + /// Gets the first column of this matrix. + /// + public Vector4d Column0 + { + get { return new Vector4d(Row0.X, Row1.X, Row2.X, Row3.X); } + } + + /// + /// Gets the second column of this matrix. + /// + public Vector4d Column1 + { + get { return new Vector4d(Row0.Y, Row1.Y, Row2.Y, Row3.Y); } + } + + /// + /// Gets the third column of this matrix. + /// + public Vector4d Column2 + { + get { return new Vector4d(Row0.Z, Row1.Z, Row2.Z, Row3.Z); } + } + + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public double M11 { get { return Row0.X; } set { Row0.X = value; } } + + /// + /// Gets or sets the value at row 1, column 2 of this instance. + /// + public double M12 { get { return Row0.Y; } set { Row0.Y = value; } } + + /// + /// Gets or sets the value at row 1, column 3 of this instance. + /// + public double M13 { get { return Row0.Z; } set { Row0.Z = value; } } + + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public double M21 { get { return Row1.X; } set { Row1.X = value; } } + + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public double M22 { get { return Row1.Y; } set { Row1.Y = value; } } + + /// + /// Gets or sets the value at row 2, column 3 of this instance. + /// + public double M23 { get { return Row1.Z; } set { Row1.Z = value; } } + + /// + /// Gets or sets the value at row 3, column 1 of this instance. + /// + public double M31 { get { return Row2.X; } set { Row2.X = value; } } + + /// + /// Gets or sets the value at row 3, column 2 of this instance. + /// + public double M32 { get { return Row2.Y; } set { Row2.Y = value; } } + + /// + /// Gets or sets the value at row 3, column 3 of this instance. + /// + public double M33 { get { return Row2.Z; } set { Row2.Z = value; } } + + /// + /// Gets or sets the value at row 4, column 1 of this instance. + /// + public double M41 { get { return Row3.X; } set { Row3.X = value; } } + + /// + /// Gets or sets the value at row 4, column 2 of this instance. + /// + public double M42 { get { return Row3.Y; } set { Row3.Y = value; } } + + /// + /// Gets or sets the value at row 4, column 3 of this instance. + /// + public double M43 { get { return Row3.Z; } set { Row3.Z = value; } } + + #endregion + + #region Indexers + + /// + /// Gets or sets the value at a specified row and column. + /// + public double this[int rowIndex, int columnIndex] + { + get + { + if (rowIndex == 0) return Row0[columnIndex]; + else if (rowIndex == 1) return Row1[columnIndex]; + else if (rowIndex == 2) return Row2[columnIndex]; + else if (rowIndex == 3) return Row3[columnIndex]; + throw new IndexOutOfRangeException("You tried to access this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + set + { + if (rowIndex == 0) Row0[columnIndex] = value; + else if (rowIndex == 1) Row1[columnIndex] = value; + else if (rowIndex == 2) Row2[columnIndex] = value; + else if (rowIndex == 3) Row3[columnIndex] = value; + throw new IndexOutOfRangeException("You tried to set this matrix at: (" + rowIndex + ", " + columnIndex + ")"); + } + } + + #endregion + + #region Instance + + #region public void Invert() + + public void Invert() + { + this = Matrix4x3d.Invert(this); + } + + #endregion + + #endregion + + #region Static + + #region CreateFromAxisAngle + + /// + /// Build a rotation matrix from the specified axis/angle rotation. + /// + /// The axis to rotate about. + /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). + /// A matrix instance. + public static void CreateFromAxisAngle(Vector3d axis, double angle, out Matrix4x3d result) + { + axis.Normalize(); + double axisX = axis.X, axisY = axis.Y, axisZ = axis.Z; + + double cos = (double)System.Math.Cos(-angle); + double sin = (double)System.Math.Sin(-angle); + double t = 1.0f - cos; + + double tXX = t * axisX * axisX, + tXY = t * axisX * axisY, + tXZ = t * axisX * axisZ, + tYY = t * axisY * axisY, + tYZ = t * axisY * axisZ, + tZZ = t * axisZ * axisZ; + + double sinX = sin * axisX, + sinY = sin * axisY, + sinZ = sin * axisZ; + + result.Row0.X = tXX + cos; + result.Row0.Y = tXY - sinZ; + result.Row0.Z = tXZ + sinY; + result.Row1.X = tXY + sinZ; + result.Row1.Y = tYY + cos; + result.Row1.Z = tYZ - sinX; + result.Row2.X = tXZ - sinY; + result.Row2.Y = tYZ + sinX; + result.Row2.Z = tZZ + cos; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + } + + /// + /// Build a rotation matrix from the specified axis/angle rotation. + /// + /// The axis to rotate about. + /// Angle in radians to rotate counter-clockwise (looking in the direction of the given axis). + /// A matrix instance. + public static Matrix4x3d CreateFromAxisAngle(Vector3d axis, double angle) + { + Matrix4x3d result; + CreateFromAxisAngle(axis, angle, out result); + return result; + } + + #endregion + + #region CreateFromQuaternion + + /// + /// Builds a rotation matrix from a quaternion. + /// + /// The quaternion to rotate by. + /// A matrix instance. + public static void CreateFromQuaternion(ref Quaternion q, out Matrix4x3d result) + { + double x = q.X, y = q.Y, z = q.Z, w = q.W, + tx = 2 * x, ty = 2 * y, tz = 2 * z, + txx = tx * x, tyy = ty * y, tzz = tz * z, + txy = tx * y, txz = tx * z, tyz = ty * z, + twx = w * tx, twy = w * ty, twz = w * tz; + + result.Row0.X = 1f - tyy - tzz; + result.Row0.Y = txy - twz; + result.Row0.Z = txz + twy; + result.Row1.X = txy + twz; + result.Row1.Y = 1f - txx - tzz; + result.Row1.Z = tyz - twx; + result.Row2.X = txz - twy; + result.Row2.Y = tyz + twx; + result.Row2.Z = 1f - txx - tyy; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + + /*Vector3d axis; + double angle; + q.ToAxisAngle(out axis, out angle); + CreateFromAxisAngle(axis, angle, out result);*/ + } + + /// + /// Builds a rotation matrix from a quaternion. + /// + /// The quaternion to rotate by. + /// A matrix instance. + public static Matrix4x3d CreateFromQuaternion(Quaternion q) + { + Matrix4x3d result; + CreateFromQuaternion(ref q, out result); + return result; + } + + #endregion + + #region CreateRotation[XYZ] + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4dinstance. + public static void CreateRotationX(double angle, out Matrix4x3d result) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = cos; + result.Row1.Z = sin; + result.Row2.X = 0; + result.Row2.Y = -sin; + result.Row2.Z = cos; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the x-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4dinstance. + public static Matrix4x3d CreateRotationX(double angle) + { + Matrix4x3d result; + CreateRotationX(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4dinstance. + public static void CreateRotationY(double angle, out Matrix4x3d result) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = 0; + result.Row0.Z = -sin; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row2.X = sin; + result.Row2.Y = 0; + result.Row2.Z = cos; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the y-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4dinstance. + public static Matrix4x3d CreateRotationY(double angle) + { + Matrix4x3d result; + CreateRotationY(angle, out result); + return result; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4dinstance. + public static void CreateRotationZ(double angle, out Matrix4x3d result) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + result.Row0.X = cos; + result.Row0.Y = sin; + result.Row0.Z = 0; + result.Row1.X = -sin; + result.Row1.Y = cos; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + } + + /// + /// Builds a rotation matrix for a rotation around the z-axis. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix4dinstance. + public static Matrix4x3d CreateRotationZ(double angle) + { + Matrix4x3d result; + CreateRotationZ(angle, out result); + return result; + } + + #endregion + + #region CreateTranslation + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4dinstance. + public static void CreateTranslation(double x, double y, double z, out Matrix4x3d result) + { + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row3.X = x; + result.Row3.Y = y; + result.Row3.Z = z; + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4dinstance. + public static void CreateTranslation(ref Vector3d vector, out Matrix4x3d result) + { + result.Row0.X = 1; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = 1; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = 1; + result.Row3.X = vector.X; + result.Row3.Y = vector.Y; + result.Row3.Z = vector.Z; + } + + /// + /// Creates a translation matrix. + /// + /// X translation. + /// Y translation. + /// Z translation. + /// The resulting Matrix4dinstance. + public static Matrix4x3d CreateTranslation(double x, double y, double z) + { + Matrix4x3d result; + CreateTranslation(x, y, z, out result); + return result; + } + + /// + /// Creates a translation matrix. + /// + /// The translation vector. + /// The resulting Matrix4dinstance. + public static Matrix4x3d CreateTranslation(Vector3d vector) + { + Matrix4x3d result; + CreateTranslation(vector.X, vector.Y, vector.Z, out result); + return result; + } + + #endregion + + #region CreateScale + + /// + /// Build a scaling matrix + /// + /// Single scale factor for x,y and z axes + /// A scaling matrix + public static Matrix4x3d CreateScale(double scale) + { + return CreateScale(scale, scale, scale); + } + + /// + /// Build a scaling matrix + /// + /// Scale factors for x,y and z axes + /// A scaling matrix + public static Matrix4x3d CreateScale(Vector3d scale) + { + return CreateScale(scale.X, scale.Y, scale.Z); + } + + /// + /// Build a scaling matrix + /// + /// Scale factor for x-axis + /// Scale factor for y-axis + /// Scale factor for z-axis + /// A scaling matrix + public static Matrix4x3d CreateScale(double x, double y, double z) + { + Matrix4x3d result; + result.Row0.X = x; + result.Row0.Y = 0; + result.Row0.Z = 0; + result.Row1.X = 0; + result.Row1.Y = y; + result.Row1.Z = 0; + result.Row2.X = 0; + result.Row2.Y = 0; + result.Row2.Z = z; + result.Row3.X = 0; + result.Row3.Y = 0; + result.Row3.Z = 0; + return result; + } + + #endregion + + #region Multiply Functions + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication + public static Matrix4d Mult(Matrix4x3d left, Matrix3x4d right) + { + Matrix4d result; + Mult(ref left, ref right, out result); + return result; + } + + /// + /// Multiplies two instances. + /// + /// The left operand of the multiplication. + /// The right operand of the multiplication. + /// A new instance that is the result of the multiplication + public static void Mult(ref Matrix4x3d left, ref Matrix3x4d right, out Matrix4d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, + lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, + lM41 = left.Row3.X, lM42 = left.Row3.Y, lM43 = left.Row3.Z, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, rM14 = right.Row0.W, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, rM24 = right.Row1.W, + rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, rM34 = right.Row2.W; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31); + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32); + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33); + result.Row0.W = (lM11 * rM14) + (lM12 * rM24) + (lM13 * rM34); + result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31); + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32); + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33); + result.Row1.W = (lM21 * rM14) + (lM22 * rM24) + (lM23 * rM34); + result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31); + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32); + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33); + result.Row2.W = (lM31 * rM14) + (lM32 * rM24) + (lM33 * rM34); + result.Row3.X = (lM41 * rM11) + (lM42 * rM21) + (lM43 * rM31); + result.Row3.Y = (lM41 * rM12) + (lM42 * rM22) + (lM43 * rM32); + result.Row3.Z = (lM41 * rM13) + (lM42 * rM23) + (lM43 * rM33); + result.Row3.W = (lM41 * rM14) + (lM42 * rM24) + (lM43 * rM34); + } + + public static Matrix4x3d Mult(Matrix4x3d left, Matrix4x3d right) + { + Matrix4x3d result; + Mult(ref left, ref right, out result); + return result; + } + + public static void Mult(ref Matrix4x3d left, ref Matrix4x3d right, out Matrix4x3d result) + { + double lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, + lM21 = left.Row1.X, lM22 = left.Row1.Y, lM23 = left.Row1.Z, + lM31 = left.Row2.X, lM32 = left.Row2.Y, lM33 = left.Row2.Z, + lM41 = left.Row3.X, lM42 = left.Row3.Y, lM43 = left.Row3.Z, + rM11 = right.Row0.X, rM12 = right.Row0.Y, rM13 = right.Row0.Z, + rM21 = right.Row1.X, rM22 = right.Row1.Y, rM23 = right.Row1.Z, + rM31 = right.Row2.X, rM32 = right.Row2.Y, rM33 = right.Row2.Z, + rM41 = right.Row3.X, rM42 = right.Row3.Y, rM43 = right.Row3.Z; + + result.Row0.X = (lM11 * rM11) + (lM12 * rM21) + (lM13 * rM31) + rM41; + result.Row0.Y = (lM11 * rM12) + (lM12 * rM22) + (lM13 * rM32) + rM42; + result.Row0.Z = (lM11 * rM13) + (lM12 * rM23) + (lM13 * rM33) + rM43; + result.Row1.X = (lM21 * rM11) + (lM22 * rM21) + (lM23 * rM31) + rM41; + result.Row1.Y = (lM21 * rM12) + (lM22 * rM22) + (lM23 * rM32) + rM42; + result.Row1.Z = (lM21 * rM13) + (lM22 * rM23) + (lM23 * rM33) + rM43; + result.Row2.X = (lM31 * rM11) + (lM32 * rM21) + (lM33 * rM31) + rM41; + result.Row2.Y = (lM31 * rM12) + (lM32 * rM22) + (lM33 * rM32) + rM42; + result.Row2.Z = (lM31 * rM13) + (lM32 * rM23) + (lM33 * rM33) + rM43; + result.Row3.X = (lM41 * rM11) + (lM42 * rM21) + (lM43 * rM31) + rM41; + result.Row3.Y = (lM41 * rM12) + (lM42 * rM22) + (lM43 * rM32) + rM42; + result.Row3.Z = (lM41 * rM13) + (lM42 * rM23) + (lM43 * rM33) + rM43; + } + + public static Matrix4x3d Mult(Matrix4x3d left, double right) + { + Matrix4x3d result; + Mult(ref left, right, out result); + return result; + } + + public static void Mult(ref Matrix4x3d left, double right, out Matrix4x3d result) + { + result.Row0 = left.Row0 * right; + result.Row1 = left.Row1 * right; + result.Row2 = left.Row2 * right; + result.Row3 = left.Row3 * right; + } + + #endregion + + #region Add Functions + + public static Matrix4x3d Add(Matrix4x3d left, Matrix4x3d right) + { + Matrix4x3d result; + Add(ref left, ref right, out result); + return result; + } + + public static void Add(ref Matrix4x3d left, ref Matrix4x3d right, out Matrix4x3d result) + { + result.Row0 = left.Row0 + right.Row0; + result.Row1 = left.Row1 + right.Row1; + result.Row2 = left.Row2 + right.Row2; + result.Row3 = left.Row3 + right.Row3; + } + + #endregion + + #region Subtract Functions + + public static Matrix4x3d Subtract(Matrix4x3d left, Matrix4x3d right) + { + Matrix4x3d result; + Subtract(ref left, ref right, out result); + return result; + } + + public static void Subtract(ref Matrix4x3d left, ref Matrix4x3d right, out Matrix4x3d result) + { + result.Row0 = left.Row0 - right.Row0; + result.Row1 = left.Row1 - right.Row1; + result.Row2 = left.Row2 - right.Row2; + result.Row3 = left.Row3 - right.Row3; + } + + #endregion + + #region Invert Functions + + public static Matrix4x3d Invert(Matrix4x3d mat) + { + Matrix4x3d result; + Invert(ref mat, out result); + return result; + } + + public static void Invert(ref Matrix4x3d mat, out Matrix4x3d result) + { + Matrix3d inverseRotation = new Matrix3d(mat.Column0.Xyz, mat.Column1.Xyz, mat.Column2.Xyz); + inverseRotation.Row0 /= inverseRotation.Row0.LengthSquared; + inverseRotation.Row1 /= inverseRotation.Row1.LengthSquared; + inverseRotation.Row2 /= inverseRotation.Row2.LengthSquared; + + Vector3d translation = mat.Row3; + + result.Row0 = inverseRotation.Row0; + result.Row1 = inverseRotation.Row1; + result.Row2 = inverseRotation.Row2; + result.Row3 = new Vector3d(-Vector3d.Dot(inverseRotation.Row0, translation), -Vector3d.Dot(inverseRotation.Row1, translation), -Vector3d.Dot(inverseRotation.Row2, translation)); + } + + #endregion + + #region Transpose + + public static Matrix3x4d Transpose(Matrix4x3d mat) + { + return new Matrix3x4d(mat.Column0, mat.Column1, mat.Column2); + } + + public static void Transpose(ref Matrix4x3d mat, out Matrix3x4d result) + { + result.Row0 = mat.Column0; + result.Row1 = mat.Column1; + result.Row2 = mat.Column2; + } + + #endregion + + #endregion + + #region Operators + + public static Matrix4d operator *(Matrix4x3d left, Matrix3x4d right) + { + return Matrix4x3d.Mult(left, right); + } + + public static Matrix4x3d operator *(Matrix4x3d left, Matrix4x3d right) + { + return Matrix4x3d.Mult(left, right); + } + + public static Matrix4x3d operator *(Matrix4x3d left, double right) + { + return Matrix4x3d.Mult(left, right); + } + + public static Matrix4x3d operator +(Matrix4x3d left, Matrix4x3d right) + { + return Matrix4x3d.Add(left, right); + } + + public static Matrix4x3d operator -(Matrix4x3d left, Matrix4x3d right) + { + return Matrix4x3d.Subtract(left, right); + } + + public static bool operator ==(Matrix4x3d left, Matrix4x3d right) + { + return left.Equals(right); + } + + public static bool operator !=(Matrix4x3d left, Matrix4x3d right) + { + return !left.Equals(right); + } + + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix4x3d. + /// + /// The string representation of the matrix. + public override string ToString() + { + return string.Format("{0}\n{1}\n{2}", Row0, Row1, Row2); + } + + #endregion - #region public override int GetHashCode() - - /// - /// Returns the hashcode for this instance. - /// - /// A System.Int32 containing the unique hashcode for this instance. - public override int GetHashCode() - { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); - } - - #endregion - - #region public override bool Equals(object obj) - - /// - /// Indicates whether this instance and a specified object are equal. - /// - /// The object to compare tresult. - /// True if the instances are equal; false otherwise. - public override bool Equals(object obj) - { - if (!(obj is Matrix4x3d)) - return false; + #region public override int GetHashCode() + + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + } + + #endregion + + #region public override bool Equals(object obj) + + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare tresult. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Matrix4x3d)) + return false; - return this.Equals((Matrix4x3d)obj); - } + return this.Equals((Matrix4x3d)obj); + } - #endregion - - #endregion - - #endregion - - #region IEquatable Members - - /// Indicates whether the current matrix is equal to another matrix. - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(Matrix4x3d other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1 && - Row2 == other.Row2 && - Row3 == other.Row3; - } - - #endregion - } + #endregion + + #endregion + + #endregion + + #region IEquatable Members + + /// Indicates whether the current matrix is equal to another matrix. + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix4x3d other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1 && + Row2 == other.Row2 && + Row3 == other.Row3; + } + + #endregion + } }