Kamujin's picture

Quaternion Patch

Here is a patch for the Quaternion class with a bug fix for ToAxisAngle and some overrides for equality comparison and get hash.

@@ -18,7 +18,7 @@
     /// </summary>
     [Serializable]
     [StructLayout(LayoutKind.Sequential)]
-    public struct Quaternion
+    public struct Quaternion : IEquatable<Quaternion>
     {
         #region Fields
 
@@ -85,6 +85,10 @@
 			{
 				axis = q.XYZ / den;
 			}
+			else
+			{
+				axis = Vector3.UnitX;
+			}
 		}
 
 		#endregion
@@ -172,6 +176,28 @@
 			return left;
 		}
 
+		/// <summary>
+		/// Compares two Quaternions for equality. 
+		/// </summary>
+		/// <param name="left">The first Quaternion to be used in the comparison.</param>
+		/// <param name="right">The second Quaternion to be used in the comparison.</param>
+		/// <returns>True if both Quaternions are equal. Otherwise it returns false.</returns>
+		public static bool operator ==(Quaternion left, Quaternion right)
+		{
+			return left.Equals(right);
+		}
+
+		/// <summary>
+		/// Compares two Quaternions for inequality. 
+		/// </summary>
+		/// <param name="left">The first Quaternion to be used in the comparison.</param>
+		/// <param name="right">The second Quaternion to be used in the comparison.</param>
+		/// <returns>True if both Quaternions are not equal. Otherwise it returns false.</returns>
+		public static bool operator !=(Quaternion left, Quaternion right)
+		{
+			return !left.Equals(right);
+		}
+		
 		[CLSCompliant(false)]
 		unsafe public static explicit operator float*(Quaternion q)
         {
@@ -455,6 +481,37 @@
             return String.Format("V: {0}, W: {1}", XYZ, W);
         }
 
+		/// <summary>
+		/// Compares this Quaternion instance to another Quaternion for equality. 
+		/// </summary>
+		/// <param name="other">The other Quaternion to be used in the comparison.</param>
+		/// <returns>True if both Quaternions are equal. Otherwise it returns false.</returns>
+		public bool Equals(Quaternion other)
+		{
+			return XYZ == other.XYZ
+			&& W == other.W;
+		}
+
+		/// <summary>
+		/// Compares this object instance to another object for equality. 
+		/// </summary>
+		/// <param name="other">The other object to be used in the comparison.</param>
+		/// <returns>True if both objects are Quaternions of equal value. Otherwise it returns false.</returns>
+		public override bool Equals (object o)
+		{
+			if (o is Quaternion == false) return false;
+			return this == (Quaternion)o;
+		}
+
+		/// <summary>
+		/// Provides the hash code for this object. 
+		/// </summary>
+		/// <returns>A hash code formed from the bitwise XOR of this objects members.</returns>
+		public override int GetHashCode ()
+		{
+			return XYZ.GetHashCode() ^ W.GetHashCode();
+		}
+
         #endregion
     }
 }

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Kamujin's picture

Fiddler
Any chance I can get a yes/no/this sucks on this patch? I'd like to at least know you've seen it.

the Fiddler's picture

Sorry, I've seen this. It's a yes (though it could use some comments), but I was looking for some way to verify quats work correctly (due to the other thread with the normalization bug).

Kamujin's picture

I'll add some comments and repost the patch.

Kamujin's picture

Hows this version?

the Fiddler's picture

Great, thanks!

Kamujin's picture

I just got the latest version from the SVN and it does not look like this patch was applied.

Can you double check you side?

the Fiddler's picture

Thanks for pointing this out, applied now.