#region --- License --- /* Copyright (c) 2006 - 2008 The Open Toolkit library. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #endregion using System; 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; } } /// /// Gets or sets the values along the main diagonal of the matrix. /// public Vector2d Diagonal { get { return new Vector2d(Row0.X, Row1.Y); } set { Row0.X = value.X; Row1.Y = value.Y; } } /// /// Gets the trace of the matrix, the sum of the values along the diagonal. /// public double Trace { get { return Row0.X + Row1.Y; } } #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; else 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 } }