Permalink
Browse files

Matrix3x2 implementation and partial Matrix4x2 implementation.

  • Loading branch information...
1 parent f10af59 commit df78210bc589697df927f954f7bc9360c9d09fc5 @Robmaister Robmaister committed Jan 20, 2013
Showing with 975 additions and 183 deletions.
  1. +12 −0 Source/OpenTK/Math/Matrix2x3.cs
  2. +652 −182 Source/OpenTK/Math/Matrix3x2.cs
  3. +311 −1 Source/OpenTK/Math/Matrix4x2.cs
View
12 Source/OpenTK/Math/Matrix2x3.cs
@@ -520,6 +520,18 @@ public static void Transpose(ref Matrix2x3 mat, out Matrix3x2 result)
result.Row2.Y = mat.Row1.Z;
}
+ /// <summary>
+ /// Calculate the transpose of the given matrix.
+ /// </summary>
+ /// <param name="mat">The matrix to transpose.</param>
+ /// <returns>The transpose of the given matrix.</returns>
+ public static Matrix3x2 Transpose(Matrix2x3 mat)
+ {
+ Matrix3x2 result;
+ Transpose(ref mat, out result);
+ return result;
+ }
+
#endregion
#endregion
View
834 Source/OpenTK/Math/Matrix3x2.cs
@@ -27,186 +27,656 @@
namespace OpenTK
{
- public struct Matrix3x2 : IEquatable<Matrix3x2>
- {
- #region Fields
-
- /// <summary>
- /// Top row of the matrix.
- /// </summary>
- public Vector2 Row0;
-
- /// <summary>
- /// Second row of the matrix.
- /// </summary>
- public Vector2 Row1;
-
- /// <summary>
- /// Bottom row of the matrix.
- /// </summary>
- public Vector2 Row2;
-
- /// <summary>
- /// The zero matrix.
- /// </summary>
- public static Matrix3x2 Zero = new Matrix3x2(Vector2.Zero, Vector2.Zero, Vector2.Zero);
-
- #endregion
-
- #region Constructors
-
- /// <summary>
- /// Constructs a new instance.
- /// </summary>
- /// <param name="row0">Top row of the matrix.</param>
- /// <param name="row1">Second row of the matrix.</param>
- /// <param name="row2">Bottom row of the matrix.</param>
- public Matrix3x2(Vector2 row0, Vector2 row1, Vector2 row2)
- {
- Row0 = row0;
- Row1 = row1;
- Row2 = row2;
- }
-
- /// <summary>
- /// Constructs a new instance
- /// </summary>
- /// <param name="m00">First item of the first row of the matrix.</param>
- /// <param name="m01">Second item of the first row of the matrix.</param>
- /// <param name="m10">First item of the second row of the matrix.</param>
- /// <param name="m11">Second item of the second row of the matrix.</param>
- /// <param name="m20">First item of the third row of the matrix.</param>
- /// <param name="m21">Second item of the third row of the matrix.</param>
- public Matrix3x2(
- float m00, float m01,
- float m10, float m11,
- float m20, float m21)
- {
- Row0 = new Vector2(m00, m01);
- Row1 = new Vector2(m10, m11);
- Row2 = new Vector2(m20, m21);
- }
-
- #endregion
-
- #region Public Members
-
- #region Properties
-
- public Vector3 Column0
- {
- get { return new Vector3(Row0.X, Row1.X, Row2.X); }
- set { Row0.X = value.X; Row1.X = value.Y; Row2.X = value.Z; }
- }
-
- public Vector3 Column1
- {
- get { return new Vector3(Row0.Y, Row1.Y, Row2.Y); }
- set { Row0.Y = value.X; Row1.Y = value.Y; Row2.Y = value.Z; }
- }
-
- public float M11 { get { return Row0.X; } set { Row0.X = value; } }
- public float M12 { get { return Row0.Y; } set { Row0.Y = 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 M31 { get { return Row2.X; } set { Row2.X = value; } }
- public float M32 { get { return Row2.Y; } set { Row2.Y = value; } }
-
- #endregion
-
- #region Indexers
-
- /// <summary>
- /// Gets or sets the value at a specified row and column.
- /// </summary>
- 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
- #endregion
-
- #region Static
- #endregion
-
- #region Operators
- #endregion
-
- #region Overrides
-
- #region public override string ToString()
-
- /// <summary>
- /// Returns a System.String that represents the current Matrix3d.
- /// </summary>
- /// <returns>The string representation of the matrix.</returns>
- public override string ToString()
- {
- return String.Format("{0}\n{1}\n{2}", Row0, Row1, Row2);
- }
-
- #endregion
-
- #region public override int GetHashCode()
-
- /// <summary>
- /// Returns the hashcode for this instance.
- /// </summary>
- /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
- public override int GetHashCode()
- {
- return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode();
- }
-
- #endregion
-
- #region public override bool Equals(object obj)
-
- /// <summary>
- /// Indicates whether this instance and a specified object are equal.
- /// </summary>
- /// <param name="obj">The object to compare to.</param>
- /// <returns>True if the instances are equal; false otherwise.</returns>
- public override bool Equals(object obj)
- {
- if (!(obj is Matrix3x2))
- return false;
-
- return this.Equals((Matrix3x2)obj);
- }
-
- #endregion
-
- #endregion
-
- #endregion
-
- #region IEquatable<Matrix3x2> Members
-
- public bool Equals(Matrix3x2 other)
- {
- return
- Row0 == other.Row0 &&
- Row1 == other.Row1 &&
- Row2 == other.Row2;
- }
-
- #endregion
- }
+ public struct Matrix3x2 : IEquatable<Matrix3x2>
+ {
+ #region Fields
+
+ /// <summary>
+ /// Top row of the matrix.
+ /// </summary>
+ public Vector2 Row0;
+
+ /// <summary>
+ /// Second row of the matrix.
+ /// </summary>
+ public Vector2 Row1;
+
+ /// <summary>
+ /// Bottom row of the matrix.
+ /// </summary>
+ public Vector2 Row2;
+
+ /// <summary>
+ /// The zero matrix.
+ /// </summary>
+ public static readonly Matrix3x2 Zero = new Matrix3x2(Vector2.Zero, Vector2.Zero, Vector2.Zero);
+
+ #endregion
+
+ #region Constructors
+
+ /// <summary>
+ /// Constructs a new instance.
+ /// </summary>
+ /// <param name="row0">Top row of the matrix.</param>
+ /// <param name="row1">Second row of the matrix.</param>
+ /// <param name="row2">Bottom row of the matrix.</param>
+ public Matrix3x2(Vector2 row0, Vector2 row1, Vector2 row2)
+ {
+ Row0 = row0;
+ Row1 = row1;
+ Row2 = row2;
+ }
+
+ /// <summary>
+ /// Constructs a new instance
+ /// </summary>
+ /// <param name="m00">First item of the first row of the matrix.</param>
+ /// <param name="m01">Second item of the first row of the matrix.</param>
+ /// <param name="m10">First item of the second row of the matrix.</param>
+ /// <param name="m11">Second item of the second row of the matrix.</param>
+ /// <param name="m20">First item of the third row of the matrix.</param>
+ /// <param name="m21">Second item of the third row of the matrix.</param>
+ public Matrix3x2(
+ float m00, float m01,
+ float m10, float m11,
+ float m20, float m21)
+ {
+ Row0 = new Vector2(m00, m01);
+ Row1 = new Vector2(m10, m11);
+ Row2 = new Vector2(m20, m21);
+ }
+
+ #endregion
+
+ #region Public Members
+
+ #region Properties
+
+ public Vector3 Column0
+ {
+ get { return new Vector3(Row0.X, Row1.X, Row2.X); }
+ set { Row0.X = value.X; Row1.X = value.Y; Row2.X = value.Z; }
+ }
+
+ public Vector3 Column1
+ {
+ get { return new Vector3(Row0.Y, Row1.Y, Row2.Y); }
+ set { Row0.Y = value.X; Row1.Y = value.Y; Row2.Y = value.Z; }
+ }
+
+ public float M11 { get { return Row0.X; } set { Row0.X = value; } }
+ public float M12 { get { return Row0.Y; } set { Row0.Y = 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 M31 { get { return Row2.X; } set { Row2.X = value; } }
+ public float M32 { get { return Row2.Y; } set { Row2.Y = value; } }
+
+ #endregion
+
+ #region Indexers
+
+ /// <summary>
+ /// Gets or sets the value at a specified row and column.
+ /// </summary>
+ 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
+ #endregion
+
+ #region Static
+
+ #region CreateRotation
+
+ /// <summary>
+ /// Builds a rotation matrix.
+ /// </summary>
+ /// <param name="angle">The counter-clockwise angle in radians.</param>
+ /// <param name="result">The resulting Matrix3x2 instance.</param>
+ public static void CreateRotation(float angle, out Matrix3x2 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;
+ }
+
+ /// <summary>
+ /// Builds a rotation matrix.
+ /// </summary>
+ /// <param name="angle">The counter-clockwise angle in radians.</param>
+ /// <returns>The resulting Matrix3x2 instance.</returns>
+ public static Matrix3x2 CreateRotation(float angle)
+ {
+ Matrix3x2 result;
+ CreateRotation(angle, out result);
+ return result;
+ }
+
+ #endregion
+
+ #region CreateScale
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="scale">Single scale factor for the x, y, and z axes.</param>
+ /// <param name="result">A scale matrix.</param>
+ public static void CreateScale(float scale, out Matrix3x2 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;
+ }
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="scale">Single scale factor for the x and y axes.</param>
+ /// <returns>A scale matrix.</returns>
+ public static Matrix3x2 CreateScale(float scale)
+ {
+ Matrix3x2 result;
+ CreateScale(scale, out result);
+ return result;
+ }
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="scale">Scale factors for the x and y axes.</param>
+ /// <param name="result">A scale matrix.</param>
+ public static void CreateScale(Vector2 scale, out Matrix3x2 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;
+ }
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="scale">Scale factors for the x and y axes.</param>
+ /// <returns>A scale matrix.</returns>
+ public static Matrix3x2 CreateScale(Vector2 scale)
+ {
+ Matrix3x2 result;
+ CreateScale(scale, out result);
+ return result;
+ }
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="x">Scale factor for the x axis.</param>
+ /// <param name="y">Scale factor for the y axis.</param>
+ /// <param name="result">A scale matrix.</param>
+ public static void CreateScale(float x, float y, out Matrix3x2 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;
+ }
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="x">Scale factor for the x axis.</param>
+ /// <param name="y">Scale factor for the y axis.</param>
+ /// <returns>A scale matrix.</returns>
+ public static Matrix3x2 CreateScale(float x, float y)
+ {
+ Matrix3x2 result;
+ CreateScale(x, y, out result);
+ return result;
+ }
+
+ #endregion
+
+ #region Multiply Functions
+
+ /// <summary>
+ /// Multiplies and instance by a scalar.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <param name="result">A new instance that is the result of the multiplication.</param>
+ public static void Mult(ref Matrix3x2 left, float right, out Matrix3x2 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;
+ }
+
+ /// <summary>
+ /// Multiplies and instance by a scalar.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <returns>A new instance that is the result of the multiplication.</returns>
+ public static Matrix3x2 Mult(Matrix3x2 left, float right)
+ {
+ Matrix3x2 result;
+ Mult(ref left, right, out result);
+ return result;
+ }
+
+ /// <summary>
+ /// Multiplies two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <param name="result">A new instance that is the result of the multiplication.</param>
+ public static void Mult(ref Matrix3x2 left, ref Matrix2 right, out Matrix3x2 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,
+ 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);
+ }
+
+ /// <summary>
+ /// Multiplies two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <returns>A new instance that is the result of the multiplication.</returns>
+ public static Matrix3x2 Mult(Matrix3x2 left, Matrix2 right)
+ {
+ Matrix3x2 result;
+ Mult(ref left, ref right, out result);
+ return result;
+ }
+
+ /// <summary>
+ /// Multiplies two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <param name="result">A new instance that is the result of the multiplication.</param>
+ public static void Mult(ref Matrix3x2 left, ref Matrix2x3 right, out Matrix3 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,
+ 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);
+ }
+
+ /// <summary>
+ /// Multiplies two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <returns>A new instance that is the result of the multiplication.</returns>
+ public static Matrix3 Mult(Matrix3x2 left, Matrix2x3 right)
+ {
+ Matrix3 result;
+ Mult(ref left, ref right, out result);
+ return result;
+ }
+
+ /// <summary>
+ /// Multiplies two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <param name="result">A new instance that is the result of the multiplication.</param>
+ public static void Mult(ref Matrix3x2 left, ref Matrix2x4 right, out Matrix3x4 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,
+ 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);
+ }
+
+ /// <summary>
+ /// Multiplies two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <returns>A new instance that is the result of the multiplication.</returns>
+ public static Matrix3x4 Mult(Matrix3x2 left, Matrix2x4 right)
+ {
+ Matrix3x4 result;
+ Mult(ref left, ref right, out result);
+ return result;
+ }
+
+ #endregion
+
+ #region Add
+
+ /// <summary>
+ /// Adds two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the addition.</param>
+ /// <param name="right">The right operand of the addition.</param>
+ /// <param name="result">A new instance that is the result of the addition.</param>
+ public static void Add(ref Matrix3x2 left, ref Matrix3x2 right, out Matrix3x2 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;
+ }
+
+ /// <summary>
+ /// Adds two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the addition.</param>
+ /// <param name="right">The right operand of the addition.</param>
+ /// <returns>A new instance that is the result of the addition.</returns>
+ public static Matrix3x2 Add(Matrix3x2 left, Matrix3x2 right)
+ {
+ Matrix3x2 result;
+ Add(ref left, ref right, out result);
+ return result;
+ }
+
+ #endregion
+
+ #region Subtract
+
+ /// <summary>
+ /// Subtracts two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the subtraction.</param>
+ /// <param name="right">The right operand of the subtraction.</param>
+ /// <param name="result">A new instance that is the result of the subtraction.</param>
+ public static void Subtract(ref Matrix3x2 left, ref Matrix3x2 right, out Matrix3x2 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;
+ }
+
+ /// <summary>
+ /// Subtracts two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the subtraction.</param>
+ /// <param name="right">The right operand of the subtraction.</param>
+ /// <returns>A new instance that is the result of the subtraction.</returns>
+ public static Matrix3x2 Subtract(Matrix3x2 left, Matrix3x2 right)
+ {
+ Matrix3x2 result;
+ Subtract(ref left, ref right, out result);
+ return result;
+ }
+
+ #endregion
+
+ #region Invert Functions
+
+ #endregion
+
+ #region Transpose
+
+ /// <summary>
+ /// Calculate the transpose of the given matrix.
+ /// </summary>
+ /// <param name="mat">The matrix to transpose.</param>
+ /// <param name="result">The transpose of the given matrix.</param>
+ public static void Transpose(ref Matrix3x2 mat, out Matrix2x3 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;
+ }
+
+ /// <summary>
+ /// Calculate the transpose of the given matrix.
+ /// </summary>
+ /// <param name="mat">The matrix to transpose.</param>
+ /// <returns>The transpose of the given matrix.</returns>
+ public static Matrix2x3 Transpose(Matrix3x2 mat)
+ {
+ Matrix2x3 result;
+ Transpose(ref mat, out result);
+ return result;
+ }
+
+ #endregion
+
+ #endregion
+
+ #region Operators
+
+ /// <summary>
+ /// Scalar multiplication.
+ /// </summary>
+ /// <param name="left">left-hand operand</param>
+ /// <param name="right">right-hand operand</param>
+ /// <returns>A new Matrix3x2 which holds the result of the multiplication</returns>
+ public static Matrix3x2 operator *(float left, Matrix3x2 right)
+ {
+ return Mult(right, left);
+ }
+
+ /// <summary>
+ /// Scalar multiplication.
+ /// </summary>
+ /// <param name="left">left-hand operand</param>
+ /// <param name="right">right-hand operand</param>
+ /// <returns>A new Matrix3x2 which holds the result of the multiplication</returns>
+ public static Matrix3x2 operator *(Matrix3x2 left, float right)
+ {
+ return Mult(left, right);
+ }
+
+ /// <summary>
+ /// Matrix multiplication
+ /// </summary>
+ /// <param name="left">left-hand operand</param>
+ /// <param name="right">right-hand operand</param>
+ /// <returns>A new Matrix3x2 which holds the result of the multiplication</returns>
+ public static Matrix3x2 operator *(Matrix3x2 left, Matrix2 right)
+ {
+ return Mult(left, right);
+ }
+
+ /// <summary>
+ /// Matrix multiplication
+ /// </summary>
+ /// <param name="left">left-hand operand</param>
+ /// <param name="right">right-hand operand</param>
+ /// <returns>A new Matrix3 which holds the result of the multiplication</returns>
+ public static Matrix3 operator *(Matrix3x2 left, Matrix2x3 right)
+ {
+ return Mult(left, right);
+ }
+
+ /// <summary>
+ /// Matrix multiplication
+ /// </summary>
+ /// <param name="left">left-hand operand</param>
+ /// <param name="right">right-hand operand</param>
+ /// <returns>A new Matrix3x4 which holds the result of the multiplication</returns>
+ public static Matrix3x4 operator *(Matrix3x2 left, Matrix2x4 right)
+ {
+ return Mult(left, right);
+ }
+
+ /// <summary>
+ /// Matrix addition
+ /// </summary>
+ /// <param name="left">left-hand operand</param>
+ /// <param name="right">right-hand operand</param>
+ /// <returns>A new Matrix3x2 which holds the result of the addition</returns>
+ public static Matrix3x2 operator +(Matrix3x2 left, Matrix3x2 right)
+ {
+ return Add(left, right);
+ }
+
+ /// <summary>
+ /// Matrix subtraction
+ /// </summary>
+ /// <param name="left">left-hand operand</param>
+ /// <param name="right">right-hand operand</param>
+ /// <returns>A new Matrix3x2 which holds the result of the subtraction</returns>
+ public static Matrix3x2 operator -(Matrix3x2 left, Matrix3x2 right)
+ {
+ return Subtract(left, right);
+ }
+
+ /// <summary>
+ /// Compares two instances for equality.
+ /// </summary>
+ /// <param name="left">The first instance.</param>
+ /// <param name="right">The second instance.</param>
+ /// <returns>True, if left equals right; false otherwise.</returns>
+ public static bool operator ==(Matrix3x2 left, Matrix3x2 right)
+ {
+ return left.Equals(right);
+ }
+
+ /// <summary>
+ /// Compares two instances for inequality.
+ /// </summary>
+ /// <param name="left">The first instance.</param>
+ /// <param name="right">The second instance.</param>
+ /// <returns>True, if left does not equal right; false otherwise.</returns>
+ public static bool operator !=(Matrix3x2 left, Matrix3x2 right)
+ {
+ return !left.Equals(right);
+ }
+
+ #endregion
+
+ #region Overrides
+
+ #region public override string ToString()
+
+ /// <summary>
+ /// Returns a System.String that represents the current Matrix3d.
+ /// </summary>
+ /// <returns>The string representation of the matrix.</returns>
+ public override string ToString()
+ {
+ return String.Format("{0}\n{1}\n{2}", Row0, Row1, Row2);
+ }
+
+ #endregion
+
+ #region public override int GetHashCode()
+
+ /// <summary>
+ /// Returns the hashcode for this instance.
+ /// </summary>
+ /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
+ public override int GetHashCode()
+ {
+ return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode();
+ }
+
+ #endregion
+
+ #region public override bool Equals(object obj)
+
+ /// <summary>
+ /// Indicates whether this instance and a specified object are equal.
+ /// </summary>
+ /// <param name="obj">The object to compare to.</param>
+ /// <returns>True if the instances are equal; false otherwise.</returns>
+ public override bool Equals(object obj)
+ {
+ if (!(obj is Matrix3x2))
+ return false;
+
+ return this.Equals((Matrix3x2)obj);
+ }
+
+ #endregion
+
+ #endregion
+
+ #endregion
+
+ #region IEquatable<Matrix3x2> Members
+
+ public bool Equals(Matrix3x2 other)
+ {
+ return
+ Row0 == other.Row0 &&
+ Row1 == other.Row1 &&
+ Row2 == other.Row2;
+ }
+
+ #endregion
+ }
}
View
312 Source/OpenTK/Math/Matrix4x2.cs
@@ -27,24 +27,49 @@
namespace OpenTK
{
+ /// <summary>
+ /// Represents a 4x2 matrix.
+ /// </summary>
public struct Matrix4x2 : IEquatable<Matrix4x2>
{
#region Fields
+ /// <summary>
+ /// Top row of the matrix.
+ /// </summary>
public Vector2 Row0;
+
+ /// <summary>
+ /// Second row of the matrix.
+ /// </summary>
public Vector2 Row1;
+
+ /// <summary>
+ /// Third row of the matrix.
+ /// </summary>
public Vector2 Row2;
+
+ /// <summary>
+ /// Bottom row of the matrix.
+ /// </summary>
public Vector2 Row3;
/// <summary>
/// The zero matrix.
/// </summary>
- public static Matrix4x2 Zero = new Matrix4x2(Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero);
+ public static readonly Matrix4x2 Zero = new Matrix4x2(Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero);
#endregion
#region Constructors
+ /// <summary>
+ /// Constructs a new instance.
+ /// </summary>
+ /// <param name="row0">Top row of the matrix.</param>
+ /// <param name="row1">Second row of the matrix.</param>
+ /// <param name="row2">Third row of the matrix.</param>
+ /// <param name="row3">Bottom row of the matrix.</param>
public Matrix4x2(Vector2 row0, Vector2 row1, Vector2 row2, Vector2 row3)
{
Row0 = row0;
@@ -53,6 +78,18 @@ public Matrix4x2(Vector2 row0, Vector2 row1, Vector2 row2, Vector2 row3)
Row3 = row3;
}
+
+ /// <summary>
+ /// Constructs a new instance
+ /// </summary>
+ /// <param name="m00">First item of the first row of the matrix.</param>
+ /// <param name="m01">Second item of the first row of the matrix.</param>
+ /// <param name="m10">First item of the second row of the matrix.</param>
+ /// <param name="m11">Second item of the second row of the matrix.</param>
+ /// <param name="m20">First item of the third row of the matrix.</param>
+ /// <param name="m21">Second item of the third row of the matrix.</param>
+ /// <param name="m30">First item of the fourth row of the matrix.</param>
+ /// <param name="m31">Second item of the fourth row of the matrix.</param>
public Matrix4x2(
float m00, float m01,
float m10, float m11,
@@ -121,6 +158,279 @@ public Vector4 Column1
#endregion
+ #region Static
+
+ #region CreateRotation
+
+ /// <summary>
+ /// Builds a rotation matrix.
+ /// </summary>
+ /// <param name="angle">The counter-clockwise angle in radians.</param>
+ /// <param name="result">The resulting Matrix3x2 instance.</param>
+ 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;
+ }
+
+ /// <summary>
+ /// Builds a rotation matrix.
+ /// </summary>
+ /// <param name="angle">The counter-clockwise angle in radians.</param>
+ /// <returns>The resulting Matrix3x2 instance.</returns>
+ public static Matrix4x2 CreateRotation(float angle)
+ {
+ Matrix4x2 result;
+ CreateRotation(angle, out result);
+ return result;
+ }
+
+ #endregion
+
+ #region CreateScale
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="scale">Single scale factor for the x, y, and z axes.</param>
+ /// <param name="result">A scale matrix.</param>
+ 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;
+ }
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="scale">Single scale factor for the x and y axes.</param>
+ /// <returns>A scale matrix.</returns>
+ public static Matrix4x2 CreateScale(float scale)
+ {
+ Matrix4x2 result;
+ CreateScale(scale, out result);
+ return result;
+ }
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="scale">Scale factors for the x and y axes.</param>
+ /// <param name="result">A scale matrix.</param>
+ 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;
+ }
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="scale">Scale factors for the x and y axes.</param>
+ /// <returns>A scale matrix.</returns>
+ public static Matrix4x2 CreateScale(Vector2 scale)
+ {
+ Matrix4x2 result;
+ CreateScale(scale, out result);
+ return result;
+ }
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="x">Scale factor for the x axis.</param>
+ /// <param name="y">Scale factor for the y axis.</param>
+ /// <param name="result">A scale matrix.</param>
+ 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;
+ }
+
+ /// <summary>
+ /// Creates a scale matrix.
+ /// </summary>
+ /// <param name="x">Scale factor for the x axis.</param>
+ /// <param name="y">Scale factor for the y axis.</param>
+ /// <returns>A scale matrix.</returns>
+ public static Matrix4x2 CreateScale(float x, float y)
+ {
+ Matrix4x2 result;
+ CreateScale(x, y, out result);
+ return result;
+ }
+
+ #endregion
+
+ #region Multiply Functions
+
+ /// <summary>
+ /// Multiplies and instance by a scalar.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <param name="result">A new instance that is the result of the multiplication.</param>
+ 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;
+ }
+
+ /// <summary>
+ /// Multiplies and instance by a scalar.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <returns>A new instance that is the result of the multiplication.</returns>
+ public static Matrix4x2 Mult(Matrix4x2 left, float right)
+ {
+ Matrix4x2 result;
+ Mult(ref left, right, out result);
+ return result;
+ }
+
+ /// <summary>
+ /// Multiplies two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <param name="result">A new instance that is the result of the multiplication.</param>
+ 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);
+ }
+
+ /// <summary>
+ /// Multiplies two instances.
+ /// </summary>
+ /// <param name="left">The left operand of the multiplication.</param>
+ /// <param name="right">The right operand of the multiplication.</param>
+ /// <returns>A new instance that is the result of the multiplication.</returns>
+ public static Matrix4x2 Mult(Matrix4x2 left, Matrix2 right)
+ {
+ Matrix4x2 result;
+ Mult(ref left, ref right, out result);
+ return result;
+ }
+
+ #endregion
+
+ #region Add
+
+ #endregion
+
+ #region Subtact
+
+ #endregion
+
+ #region Invert Functions
+
+ #endregion
+
+ #region Transpose
+
+ #endregion
+
+ #endregion
+
+ #region Operators
+
+ #endregion
+
+ #region Overrides
+
+ #region public override string ToString()
+
+ /// <summary>
+ /// Returns a System.String that represents the current Matrix3d.
+ /// </summary>
+ /// <returns>The string representation of the matrix.</returns>
+ public override string ToString()
+ {
+ return String.Format("{0}\n{1}\n{2}\n{3}", Row0, Row1, Row2, Row3);
+ }
+
+ #endregion
+
+ #region public override int GetHashCode()
+
+ /// <summary>
+ /// Returns the hashcode for this instance.
+ /// </summary>
+ /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
+ public override int GetHashCode()
+ {
+ return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode();
+ }
+
+ #endregion
+
+ #region public override bool Equals(object obj)
+
+ /// <summary>
+ /// Indicates whether this instance and a specified object are equal.
+ /// </summary>
+ /// <param name="obj">The object to compare to.</param>
+ /// <returns>True if the instances are equal; false otherwise.</returns>
+ public override bool Equals(object obj)
+ {
+ if (!(obj is Matrix4x2))
+ return false;
+
+ return this.Equals((Matrix4x2)obj);
+ }
+
+ #endregion
+
+ #endregion
+
#endregion
#region IEquatable<Matrix4x2> Members

0 comments on commit df78210

Please sign in to comment.