More work on matrices.
authorRobert Rouhani <robert.rouhani@gmail.com>
Sat, 19 Jan 2013 23:06:51 +0000 (15:06 -0800)
committerRobert Rouhani <robert.rouhani@gmail.com>
Sat, 19 Jan 2013 23:06:51 +0000 (15:06 -0800)
Fixed some static fields in the math classes that weren't also readonly.

12 files changed:
Source/OpenTK/Math/Matrix2.cs
Source/OpenTK/Math/Matrix2x3.cs
Source/OpenTK/Math/Matrix2x4.cs
Source/OpenTK/Math/Matrix3.cs
Source/OpenTK/Math/Matrix3x2.cs
Source/OpenTK/Math/Matrix3x4.cs
Source/OpenTK/Math/Matrix4.cs
Source/OpenTK/Math/Matrix4x2.cs
Source/OpenTK/Math/Matrix4x3.cs
Source/OpenTK/Math/Vector2d.cs
Source/OpenTK/Math/Vector4.cs
Source/OpenTK/Math/Vector4d.cs

index a3d46933c4e215c47a087bd18aa09d08fe93b93a..bfefb91496c8c0e1e22e8b2ccce581ad18eb399b 100644 (file)
@@ -44,6 +44,16 @@ namespace OpenTK
         /// </summary>
         public Vector2 Row1;
 
+        /// <summary>
+        /// The identity matrix.
+        /// </summary>
+        public static Matrix2 Identity = new Matrix2(Vector2.UnitX, Vector2.UnitY);
+
+        /// <summary>
+        /// The zero matrix.
+        /// </summary>
+        public static readonly Matrix2 Zero = new Matrix2(Vector2.Zero, Vector2.Zero);
+
         #endregion
 
         #region Constructors
@@ -280,6 +290,33 @@ namespace OpenTK
 
         #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 Matrix2 left, float right, out Matrix2 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;
+        }
+
+        /// <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 Matrix2 Mult(Matrix2 left, float right)
+        {
+            Matrix2 result;
+            Mult(ref left, right, out result);
+            return result;
+        }
+
         /// <summary>
         /// Multiplies two instances.
         /// </summary>
@@ -485,6 +522,28 @@ namespace OpenTK
 
         #region Operators
 
+        /// <summary>
+        /// Scalar multiplication.
+        /// </summary>
+        /// <param name="left">left-hand operand</param>
+        /// <param name="right">right-hand operand</param>
+        /// <returns>A new Matrix2 which holds the result of the multiplication</returns>
+        public static Matrix2 operator *(float left, Matrix2 right)
+        {
+            return Matrix2.Mult(right, left);
+        }
+
+        /// <summary>
+        /// Scalar multiplication.
+        /// </summary>
+        /// <param name="left">left-hand operand</param>
+        /// <param name="right">right-hand operand</param>
+        /// <returns>A new Matrix2 which holds the result of the multiplication</returns>
+        public static Matrix2 operator *(Matrix2 left, float right)
+        {
+            return Matrix2.Mult(left, right);
+        }
+
         /// <summary>
         /// Matrix multiplication
         /// </summary>
@@ -564,7 +623,7 @@ namespace OpenTK
 
         #endregion
 
-        #region Overloads
+        #region Overrides
 
         #region public override string ToString()
 
index 3546669df8cae2c1de1bdda50ab52a2b70c82d8d..b2aaae8e5812b7594ddda095eb1f49afc639919a 100644 (file)
@@ -44,6 +44,11 @@ namespace OpenTK
         /// </summary>
         public Vector3 Row1;
 
+        /// <summary>
+        /// The zero matrix.
+        /// </summary>
+        public static Matrix2x3 Zero = new Matrix2x3(Vector3.Zero, Vector3.Zero);
+
         #endregion
 
         #region Constructors
@@ -162,6 +167,15 @@ namespace OpenTK
             }
         }
 
+        #endregion
+
+        #region Instance
+        #endregion
+
+        #region Static
+
+
+
         #endregion
 
         #region Operators
index 5f9bfefbd31497dabe38974f685273023d22b74c..848091b17694f14457d93f930ce2936f28f889ec 100644 (file)
@@ -34,6 +34,11 @@ namespace OpenTK
                public Vector4 Row0;
                public Vector4 Row1;
 
+               /// <summary>
+               /// The zero matrix.
+               /// </summary>
+               public static Matrix2x4 Zero = new Matrix2x4(Vector4.Zero, Vector4.Zero);
+
                #endregion
 
                #region Constructors
index 52c41bb580ec3573b1778e00e7c652f9c15c5d0f..10423c37c057a3fd44aef19159d8ccf8bedbde64 100644 (file)
@@ -54,7 +54,12 @@ namespace OpenTK
         /// <summary>
         /// The identity matrix.
         /// </summary>
-        public static Matrix3 Identity = new Matrix3(Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ);
+        public static readonly Matrix3 Identity = new Matrix3(Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ);
+
+        /// <summary>
+        /// The zero matrix.
+        /// </summary>
+        public static readonly Matrix3 Zero = new Matrix3(Vector3.Zero, Vector3.Zero, Vector3.Zero);
         
         #endregion
         
index c9a1f760bf40a83f6205ac9af5a4efc703caa485..8eceb7bb1cc3c715ce832e39a31ad666364bdecb 100644 (file)
@@ -31,10 +31,26 @@ namespace OpenTK
        {
                #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
@@ -108,6 +124,62 @@ namespace OpenTK
 
                #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
index ef3c4f8acf15ff02d91db0f3bc58728b7d176431..7abb7d7f836a4beec705a03d0357a9066e00770e 100644 (file)
@@ -52,9 +52,9 @@ namespace OpenTK
                public Vector4 Row2;
 
                /// <summary>
-               /// The identity matrix
+               /// The zero matrix
                /// </summary>
-               public static Matrix3x4 Identity = new Matrix3x4(Vector4.UnitX, Vector4.UnitY, Vector4.UnitZ);
+               public static Matrix3x4 Zero = new Matrix3x4(Vector4.Zero, Vector4.Zero, Vector4.Zero);
 
                #endregion
 
@@ -355,11 +355,18 @@ namespace OpenTK
                        float cos = (float)System.Math.Cos(angle);
                        float sin = (float)System.Math.Sin(angle);
 
-                       result = Identity;
+                       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;
                }
 
                /// <summary>
@@ -384,11 +391,18 @@ namespace OpenTK
                        float cos = (float)System.Math.Cos(angle);
                        float sin = (float)System.Math.Sin(angle);
 
-                       result = Identity;
                        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;
                }
 
                /// <summary>
@@ -413,11 +427,18 @@ namespace OpenTK
                        float cos = (float)System.Math.Cos(angle);
                        float sin = (float)System.Math.Sin(angle);
 
-                       result = Identity;
                        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;
                }
 
                /// <summary>
@@ -445,9 +466,17 @@ namespace OpenTK
                /// <param name="result">The resulting Matrix4 instance.</param>
                public static void CreateTranslation(float x, float y, float z, out Matrix3x4 result)
                {
-                       result = Identity;
+                       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;
                }
 
@@ -458,9 +487,17 @@ namespace OpenTK
                /// <param name="result">The resulting Matrix4 instance.</param>
                public static void CreateTranslation(ref Vector3 vector, out Matrix3x4 result)
                {
-                       result = Identity;
+                       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;
                }
 
@@ -523,10 +560,19 @@ namespace OpenTK
                /// <returns>A scaling matrix</returns>
                public static Matrix3x4 CreateScale(float x, float y, float z)
                {
-                       Matrix3x4 result = Identity;
+                       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;
                }
 
index 081772836f6b308bfd51477065b7126450a729e7..858cf46215faac79df2f394f7549fd60d60e708c 100644 (file)
@@ -59,7 +59,12 @@ namespace OpenTK
         /// <summary>
         /// The identity matrix.
         /// </summary>
-        public static Matrix4 Identity = new Matrix4(Vector4.UnitX, Vector4.UnitY, Vector4.UnitZ, Vector4.UnitW);
+        public static readonly Matrix4 Identity = new Matrix4(Vector4.UnitX, Vector4.UnitY, Vector4.UnitZ, Vector4.UnitW);
+
+        /// <summary>
+        /// The zero matrix.
+        /// </summary>
+        public static readonly Matrix4 Zero = new Matrix4(Vector4.Zero, Vector4.Zero, Vector4.Zero, Vector4.Zero);
 
         #endregion
 
index ab08a7b7246cbf6961a5095006415544917a9123..209aaad2440da45b283716a1595b3d9e771d067a 100644 (file)
@@ -36,6 +36,11 @@ namespace OpenTK
                public Vector2 Row2;
                public Vector2 Row3;
 
+               /// <summary>
+               /// The zero matrix.
+               /// </summary>
+               public static Matrix4x2 Zero = new Matrix4x2(Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero);
+
                #endregion
 
                #region Constructors
index b43c44be4a36967af9b145f96177a1d7bc413bd4..a9a39e082e2be952c1ec8e82c1c4f723ee8dc459 100644 (file)
@@ -57,9 +57,9 @@ namespace OpenTK
                public Vector3 Row3;
 
                /// <summary>
-               /// The identity matrix
+               /// The zero matrix
                /// </summary>
-               public static Matrix4x3 Identity = new Matrix4x3(Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ, Vector3.Zero);
+               public static Matrix4x3 Zero = new Matrix4x3(Vector3.Zero, Vector3.Zero, Vector3.Zero, Vector3.Zero);
 
                #endregion
 
@@ -358,11 +358,18 @@ namespace OpenTK
                        float cos = (float)System.Math.Cos(angle);
                        float sin = (float)System.Math.Sin(angle);
 
-                       result = Identity;
+                       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;
                }
 
                /// <summary>
@@ -387,11 +394,18 @@ namespace OpenTK
                        float cos = (float)System.Math.Cos(angle);
                        float sin = (float)System.Math.Sin(angle);
 
-                       result = Identity;
                        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;
                }
 
                /// <summary>
@@ -416,11 +430,18 @@ namespace OpenTK
                        float cos = (float)System.Math.Cos(angle);
                        float sin = (float)System.Math.Sin(angle);
 
-                       result = Identity;
                        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;
                }
 
                /// <summary>
@@ -448,7 +469,15 @@ namespace OpenTK
                /// <param name="result">The resulting Matrix4 instance.</param>
                public static void CreateTranslation(float x, float y, float z, out Matrix4x3 result)
                {
-                       result = Identity;
+                       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;
@@ -461,7 +490,15 @@ namespace OpenTK
                /// <param name="result">The resulting Matrix4 instance.</param>
                public static void CreateTranslation(ref Vector3 vector, out Matrix4x3 result)
                {
-                       result = Identity;
+                       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;
@@ -526,10 +563,19 @@ namespace OpenTK
                /// <returns>A scaling matrix</returns>
                public static Matrix4x3 CreateScale(float x, float y, float z)
                {
-                       Matrix4x3 result = Identity;
+                       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;
                }
 
index 9c81ca6928ad864bd9c3e067ce7dde87fe4a909a..82c8972285bb475ef1362ab1206950457b04f008 100644 (file)
@@ -44,17 +44,17 @@ namespace OpenTK
         /// <summary>
         /// Defines a unit-length Vector2d that points towards the X-axis.
         /// </summary>
-        public static Vector2d UnitX = new Vector2d(1, 0);
+        public static readonly Vector2d UnitX = new Vector2d(1, 0);
 
         /// <summary>
         /// Defines a unit-length Vector2d that points towards the Y-axis.
         /// </summary>
-        public static Vector2d UnitY = new Vector2d(0, 1);
+        public static readonly Vector2d UnitY = new Vector2d(0, 1);
 
         /// <summary>
         /// Defines a zero-length Vector2d.
         /// </summary>
-        public static Vector2d Zero = new Vector2d(0, 0);
+        public static readonly Vector2d Zero = new Vector2d(0, 0);
 
         /// <summary>
         /// Defines an instance with all components set to 1.
index ac15479a76343a5a1a47833ffccfda565fdea258..40b23bfb89ed926bed21d8c9b0b5f6a552bc5fe6 100644 (file)
@@ -60,27 +60,27 @@ namespace OpenTK
         /// <summary>
         /// Defines a unit-length Vector4 that points towards the X-axis.
         /// </summary>
-        public static Vector4 UnitX = new Vector4(1, 0, 0, 0);
+        public static readonly Vector4 UnitX = new Vector4(1, 0, 0, 0);
 
         /// <summary>
         /// Defines a unit-length Vector4 that points towards the Y-axis.
         /// </summary>
-        public static Vector4 UnitY = new Vector4(0, 1, 0, 0);
+        public static readonly Vector4 UnitY = new Vector4(0, 1, 0, 0);
 
         /// <summary>
         /// Defines a unit-length Vector4 that points towards the Z-axis.
         /// </summary>
-        public static Vector4 UnitZ = new Vector4(0, 0, 1, 0);
+        public static readonly Vector4 UnitZ = new Vector4(0, 0, 1, 0);
 
         /// <summary>
         /// Defines a unit-length Vector4 that points towards the W-axis.
         /// </summary>
-        public static Vector4 UnitW = new Vector4(0, 0, 0, 1);
+        public static readonly Vector4 UnitW = new Vector4(0, 0, 0, 1);
 
         /// <summary>
         /// Defines a zero-length Vector4.
         /// </summary>
-        public static Vector4 Zero = new Vector4(0, 0, 0, 0);
+        public static readonly Vector4 Zero = new Vector4(0, 0, 0, 0);
 
         /// <summary>
         /// Defines an instance with all components set to 1.
index 0932f738cb46b3d901f514e690d6991619c9afc8..0480460f63e40f0097bbe29869e4cf41165c1958 100644 (file)
@@ -58,27 +58,27 @@ namespace OpenTK
         /// <summary>
         /// Defines a unit-length Vector4d that points towards the X-axis.
         /// </summary>
-        public static Vector4d UnitX = new Vector4d(1, 0, 0, 0);
+        public static readonly Vector4d UnitX = new Vector4d(1, 0, 0, 0);
 
         /// <summary>
         /// Defines a unit-length Vector4d that points towards the Y-axis.
         /// </summary>
-        public static Vector4d UnitY = new Vector4d(0, 1, 0, 0);
+        public static readonly Vector4d UnitY = new Vector4d(0, 1, 0, 0);
 
         /// <summary>
         /// Defines a unit-length Vector4d that points towards the Z-axis.
         /// </summary>
-        public static Vector4d UnitZ = new Vector4d(0, 0, 1, 0);
+        public static readonly Vector4d UnitZ = new Vector4d(0, 0, 1, 0);
 
         /// <summary>
         /// Defines a unit-length Vector4d that points towards the W-axis.
         /// </summary>
-        public static Vector4d UnitW = new Vector4d(0, 0, 0, 1);
+        public static readonly Vector4d UnitW = new Vector4d(0, 0, 0, 1);
 
         /// <summary>
         /// Defines a zero-length Vector4d.
         /// </summary>
-        public static Vector4d Zero = new Vector4d(0, 0, 0, 0);
+        public static readonly Vector4d Zero = new Vector4d(0, 0, 0, 0);
 
         /// <summary>
         /// Defines an instance with all components set to 1.