diff --git a/Source/OpenTK/Math/Matrix2.cs b/Source/OpenTK/Math/Matrix2.cs index a3947fe4..a3d46933 100644 --- a/Source/OpenTK/Math/Matrix2.cs +++ b/Source/OpenTK/Math/Matrix2.cs @@ -27,6 +27,9 @@ using System.Runtime.InteropServices; namespace OpenTK { + /// + /// Represents a 2x2 matrix + /// public struct Matrix2 : IEquatable { #region Fields @@ -45,12 +48,24 @@ namespace OpenTK #region Constructors + /// + /// Constructs a new instance. + /// + /// Top row of the matrix. + /// Bottom row of the matrix. public Matrix2(Vector2 row0, Vector2 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 Matrix2( float m00, float m01, float m10, float m11) @@ -65,6 +80,9 @@ namespace OpenTK #region Properties + /// + /// Gets the determinant of this matrix. + /// public float Determinant { get @@ -76,21 +94,42 @@ namespace OpenTK } } + /// + /// Gets the first column of this matrix. + /// public Vector2 Column0 { get { return new Vector2(Row0.X, Row1.X); } set { Row0.X = value.X; Row1.X = value.Y; } } + /// + /// Gets the second column of this matrix. + /// public Vector2 Column1 { get { return new Vector2(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 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; } } #endregion @@ -118,10 +157,467 @@ namespace OpenTK #endregion + #region Instance + + + + #endregion + + #region Static + + #region CreateRotation + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix2 instance. + public static void CreateRotation(float angle, out Matrix2 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; + } + + /// + /// Builds a rotation matrix. + /// + /// The counter-clockwise angle in radians. + /// The resulting Matrix2 instance. + public static Matrix2 CreateRotation(float angle) + { + Matrix2 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 Matrix2 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 Matrix2 CreateScale(float scale) + { + Matrix2 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 Matrix2 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 Matrix2 CreateScale(Vector2 scale) + { + Matrix2 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 Matrix2 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 Matrix2 CreateScale(float x, float y) + { + Matrix2 result; + CreateScale(x, y, out result); + 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 void Mult(ref Matrix2 left, ref Matrix2 right, out Matrix2 result) + { + float 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 Matrix2 Mult(Matrix2 left, Matrix2 right) + { + Matrix2 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 Matrix2 left, ref Matrix2x3 right, out Matrix2x3 result) + { + float 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 Matrix2x3 Mult(Matrix2 left, Matrix2x3 right) + { + Matrix2x3 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 Matrix2 left, ref Matrix2x4 right, out Matrix2x4 result) + { + float 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 Matrix2x4 Mult(Matrix2 left, Matrix2x4 right) + { + Matrix2x4 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 Matrix2 left, ref Matrix2 right, out Matrix2 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 Matrix2 Add(Matrix2 left, Matrix2 right) + { + Matrix2 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 Matrix2 left, ref Matrix2 right, out Matrix2 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 Matrix2 Subtract(Matrix2 left, Matrix2 right) + { + Matrix2 result; + Subtract(ref left, ref right, out result); + return result; + } + + #endregion + + #region Invert Functions + + //TODO implement + + #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 Matrix2 mat, out Matrix2 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 Matrix2 Transpose(Matrix2 mat) + { + Matrix2 result; + Transpose(ref mat, out result); + return result; + } + + #endregion + + #endregion + + #region Operators + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2 which holds the result of the multiplication + public static Matrix2 operator *(Matrix2 left, Matrix2 right) + { + return Matrix2.Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x3 which holds the result of the multiplication + public static Matrix2x3 operator *(Matrix2 left, Matrix2x3 right) + { + return Matrix2.Mult(left, right); + } + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2x4 which holds the result of the multiplication + public static Matrix2x4 operator *(Matrix2 left, Matrix2x4 right) + { + return Matrix2.Mult(left, right); + } + + /// + /// Matrix addition + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2 which holds the result of the addition + public static Matrix2 operator +(Matrix2 left, Matrix2 right) + { + return Matrix2.Add(left, right); + } + + /// + /// Matrix subtraction + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix2 which holds the result of the subtraction + public static Matrix2 operator -(Matrix2 left, Matrix2 right) + { + return Matrix2.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 ==(Matrix2 left, Matrix2 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 !=(Matrix2 left, Matrix2 right) + { + return !left.Equals(right); + } + + #endregion + + #region Overloads + + #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 Matrix2)) + return false; + + return this.Equals((Matrix2)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(Matrix2 other) { return diff --git a/Source/OpenTK/Math/Matrix2x3.cs b/Source/OpenTK/Math/Matrix2x3.cs index 38d5be98..3546669d 100644 --- a/Source/OpenTK/Math/Matrix2x3.cs +++ b/Source/OpenTK/Math/Matrix2x3.cs @@ -27,98 +27,230 @@ using System.Collections.Generic; namespace OpenTK { - public struct Matrix2x3 : IEquatable - { - #region Fields + /// + /// Represents a 2x3 matrix. + /// + public struct Matrix2x3 : IEquatable + { + #region Fields - public Vector3 Row0; - public Vector3 Row1; + /// + /// Top row of the matrix. + /// + public Vector3 Row0; - #endregion + /// + /// Bottom row of the matrix. + /// + public Vector3 Row1; - #region Constructors + #endregion - public Matrix2x3(Vector3 row0, Vector3 row1) - { - Row0 = row0; - Row1 = row1; - } + #region Constructors - public Matrix2x3( - float m00, float m01, float m02, - float m10, float m11, float m12) - { - Row0 = new Vector3(m00, m01, m02); - Row1 = new Vector3(m10, m11, m12); - } + /// + /// Constructs a new instance. + /// + /// Top row of the matrix. + /// Bottom row of the matrix. + public Matrix2x3(Vector3 row0, Vector3 row1) + { + Row0 = row0; + Row1 = row1; + } - #endregion + /// + /// 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 Matrix2x3( + float m00, float m01, float m02, + float m10, float m11, float m12) + { + Row0 = new Vector3(m00, m01, m02); + Row1 = new Vector3(m10, m11, m12); + } - #region Public Members + #endregion - #region Properties + #region Public Members - public Vector2 Column0 - { - get { return new Vector2(Row0.X, Row1.X); } - set { Row0.X = value.X; Row1.X = value.Y; } - } + #region Properties - public Vector2 Column1 - { - get { return new Vector2(Row0.Y, Row1.Y); } - set { Row0.Y = value.X; Row1.Y = value.Y; } - } + /// + /// Gets the first column of this matrix. + /// + public Vector2 Column0 + { + get { return new Vector2(Row0.X, Row1.X); } + set { Row0.X = value.X; Row1.X = value.Y; } + } - public Vector2 Column2 - { - get { return new Vector2(Row0.Z, Row1.Z); } - set { Row0.Z = value.X; Row1.Z = value.Y; } - } + /// + /// Gets the second column of this matrix. + /// + public Vector2 Column1 + { + get { return new Vector2(Row0.Y, Row1.Y); } + set { Row0.Y = value.X; Row1.Y = value.Y; } + } - public float M11 { get { return Row0.X; } set { Row0.X = value; } } - public float M12 { get { return Row0.Y; } set { Row0.Y = value; } } - public float M13 { get { return Row0.Z; } set { Row0.Z = value; } } - public float M21 { get { return Row1.X; } set { Row1.X = value; } } - public float M22 { get { return Row1.Y; } set { Row1.Y = value; } } - public float M23 { get { return Row1.Z; } set { Row1.Z = value; } } + /// + /// Gets the third column of this matrix. + /// + public Vector2 Column2 + { + get { return new Vector2(Row0.Z, Row1.Z); } + set { Row0.Z = value.X; Row1.Z = value.Y; } + } - #endregion + /// + /// Gets or sets the value at row 1, column 1 of this instance. + /// + public float M11 { get { return Row0.X; } set { Row0.X = value; } } - #region Indexers + /// + /// 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 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]; - 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 + ")"); - } - } + /// + /// Gets or sets the value at row 1, column 3 of this instance. + /// + public float M13 { get { return Row0.Z; } set { Row0.Z = value; } } - #endregion + /// + /// Gets or sets the value at row 2, column 1 of this instance. + /// + public float M21 { get { return Row1.X; } set { Row1.X = value; } } - #endregion + /// + /// Gets or sets the value at row 2, column 2 of this instance. + /// + public float M22 { get { return Row1.Y; } set { Row1.Y = value; } } - #region IEquatable Members + /// + /// Gets or sets the value at row 2, column 3 of this instance. + /// + public float M23 { get { return Row1.Z; } set { Row1.Z = value; } } - public bool Equals(Matrix2x3 other) - { - return - Row0 == other.Row0 && - Row1 == other.Row1; - } + #endregion - #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]; + 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 Operators + + /// + /// Compares two instances for equality. + /// + /// The first instance. + /// The second instance. + /// True, if left equals right; false otherwise. + public static bool operator ==(Matrix2x3 left, Matrix2x3 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 !=(Matrix2x3 left, Matrix2x3 right) + { + return !left.Equals(right); + } + + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix2x3. + /// + /// 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 Matrix2x3)) + return false; + + return this.Equals((Matrix2x3)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(Matrix2x3 other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1; + } + + #endregion + } } diff --git a/Source/OpenTK/Math/Matrix3.cs b/Source/OpenTK/Math/Matrix3.cs index 1438bf3e..52c41bb5 100644 --- a/Source/OpenTK/Math/Matrix3.cs +++ b/Source/OpenTK/Math/Matrix3.cs @@ -112,9 +112,8 @@ namespace OpenTK m21 = Row1.X, m22 = Row1.Y, m23 = Row1.Z, m31 = Row2.X, m32 = Row2.Y, m33 = Row2.Z; - return - m11 * m22 * m33 + m12 * m23 * m31 + m13 * m21 * m32 - - m13 * m22 * m31 - m11 * m23 * m32 - m12 * m21 * m33; + return m11 * m22 * m33 + m12 * m23 * m31 + m13 * m21 * m32 + - m13 * m22 * m31 - m11 * m23 * m32 - m12 * m21 * m33; } } @@ -611,9 +610,15 @@ namespace OpenTK public static void Transpose(ref Matrix3 mat, out Matrix3 result) { - result.Row0 = mat.Column0; - result.Row1 = mat.Column1; - result.Row2 = mat.Column2; + 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; + result.Row2.X = mat.Row0.Z; + result.Row2.Y = mat.Row1.Z; + result.Row2.Z = mat.Row2.Z; } #endregion diff --git a/Source/OpenTK/Math/Matrix4.cs b/Source/OpenTK/Math/Matrix4.cs index 1e728f7b..08177283 100644 --- a/Source/OpenTK/Math/Matrix4.cs +++ b/Source/OpenTK/Math/Matrix4.cs @@ -118,7 +118,6 @@ namespace OpenTK #region Properties /// - /// The determinant of this matrix /// Gets the determinant of this matrix. /// public float Determinant @@ -1109,7 +1108,7 @@ namespace OpenTK /// /// The left operand of the multiplication. /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication + /// A new instance that is the result of the multiplication. public static Matrix4 Mult(Matrix4 left, Matrix4 right) { Matrix4 result; @@ -1122,7 +1121,7 @@ namespace OpenTK /// /// The left operand of the multiplication. /// The right operand of the multiplication. - /// A new instance that is the result of the multiplication + /// A new instance that is the result of the multiplication. public static void Mult(ref Matrix4 left, ref Matrix4 right, out Matrix4 result) { float lM11 = left.Row0.X, lM12 = left.Row0.Y, lM13 = left.Row0.Z, lM14 = left.Row0.W,