From 2af017c3eaada425845fa34da76f59d83c48332d Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Thu, 10 Jul 2008 14:01:38 +0000 Subject: [PATCH] Added serializable attribute. Added IEquatable interface and operator== overloads to Matrix4 and Vector* structs. --- Source/OpenTK/Math/Matrix4.cs | 58 ++++++++++++++++++- Source/OpenTK/Math/Quaternion.cs | 1 + Source/OpenTK/Math/Vector2.cs | 57 ++++++++++++++++++- Source/OpenTK/Math/Vector3.cs | 97 ++++++++++++++++++++++++++------ Source/OpenTK/Math/Vector4.cs | 61 +++++++++++++++++++- 5 files changed, 251 insertions(+), 23 deletions(-) diff --git a/Source/OpenTK/Math/Matrix4.cs b/Source/OpenTK/Math/Matrix4.cs index f5f36ae3..ca56a0fa 100644 --- a/Source/OpenTK/Math/Matrix4.cs +++ b/Source/OpenTK/Math/Matrix4.cs @@ -16,8 +16,9 @@ namespace OpenTK.Math /// /// Represents a 4x4 Matrix /// + [Serializable] [StructLayout(LayoutKind.Sequential)] - public struct Matrix4 + public struct Matrix4 : IEquatable { #region Fields @@ -152,9 +153,14 @@ namespace OpenTK.Math return Matrix4.Mult(left, right); } - public float get(int x, int y) + public static bool operator ==(Matrix4 left, Matrix4 right) { - throw new NotImplementedException(); + return left.Equals(right); + } + + public static bool operator !=(Matrix4 left, Matrix4 right) + { + return !left.Equals(right); } #endregion @@ -580,5 +586,51 @@ namespace OpenTK.Math } #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 Matrix4)) + return false; + + return this.Equals((Matrix4)obj); + } + + #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(Matrix4 other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1 && + Row2 == other.Row2 && + Row3 == other.Row3; + } + + #endregion } } diff --git a/Source/OpenTK/Math/Quaternion.cs b/Source/OpenTK/Math/Quaternion.cs index fcbb45da..4cb76afb 100644 --- a/Source/OpenTK/Math/Quaternion.cs +++ b/Source/OpenTK/Math/Quaternion.cs @@ -16,6 +16,7 @@ namespace OpenTK.Math /// /// Represents a Quaternion /// + [Serializable] [StructLayout(LayoutKind.Sequential)] public struct Quaternion { diff --git a/Source/OpenTK/Math/Vector2.cs b/Source/OpenTK/Math/Vector2.cs index b94d7b5c..59f888f1 100644 --- a/Source/OpenTK/Math/Vector2.cs +++ b/Source/OpenTK/Math/Vector2.cs @@ -19,8 +19,9 @@ namespace OpenTK.Math /// /// The Vector2 structure is suitable for interoperation with unmanaged code requiring two consecutive floats. /// + [Serializable] [StructLayout(LayoutKind.Sequential)] - public struct Vector2 + public struct Vector2 : IEquatable { #region Fields @@ -267,6 +268,16 @@ namespace OpenTK.Math return vec; } + public static bool operator ==(Vector2 left, Vector2 right) + { + return left.Equals(right); + } + + public static bool operator !=(Vector2 left, Vector2 right) + { + return !left.Equals(right); + } + [CLSCompliant(false)] unsafe public static explicit operator float*(Vector2 v) { @@ -615,5 +626,49 @@ namespace OpenTK.Math } #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 X.GetHashCode() ^ Y.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 Vector2)) + return false; + + return this.Equals((Vector2)obj); + } + + #endregion + + #region IEquatable Members + + /// Indicates whether the current vector is equal to another vector. + /// A vector to compare with this vector. + /// true if the current vector is equal to the vector parameter; otherwise, false. + public bool Equals(Vector2 other) + { + return + X == other.X && + Y == other.Y; + } + + #endregion } } diff --git a/Source/OpenTK/Math/Vector3.cs b/Source/OpenTK/Math/Vector3.cs index 9e4ca82a..47fedf3e 100644 --- a/Source/OpenTK/Math/Vector3.cs +++ b/Source/OpenTK/Math/Vector3.cs @@ -16,8 +16,9 @@ namespace OpenTK.Math /// /// Represents a three-dimensional vector. /// + [Serializable] [StructLayout(LayoutKind.Sequential)] - public struct Vector3 + public struct Vector3 : IEquatable { #region Fields @@ -97,6 +98,32 @@ namespace OpenTK.Math #region Functions + #region This property + + public float this[int index] + { + get + { + switch (index) + { + case 0: return X; + case 1: return Y; + case 2: return Z; + default: throw new ArgumentOutOfRangeException("index", index, "Should be 0, 1 or 2."); + } + /* + unsafe + { + fixed (float* ptr = &this.X) + return *(ptr + index); + } + + */ + } + } + + #endregion + #region public float Length /// @@ -257,23 +284,14 @@ namespace OpenTK.Math return vec; } - public float get(int index) + public static bool operator ==(Vector3 left, Vector3 right) { - switch (index) - { - case 0: return X; - case 1: return Y; - case 2: return Z; - default: throw new ArgumentOutOfRangeException("index", index, "Should be 0, 1 or 2."); - } - /* - unsafe - { - fixed (float* ptr = &this.X) - return *(ptr + index); - } + return left.Equals(right); + } - */ + public static bool operator !=(Vector3 left, Vector3 right) + { + return !left.Equals(right); } #endregion @@ -810,5 +828,50 @@ namespace OpenTK.Math } #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 X.GetHashCode() ^ Y.GetHashCode() ^ Z.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 Vector3)) + return false; + + return this.Equals((Vector3)obj); + } + + #endregion + + #region IEquatable Members + + /// Indicates whether the current vector is equal to another vector. + /// A vector to compare with this vector. + /// true if the current vector is equal to the vector parameter; otherwise, false. + public bool Equals(Vector3 other) + { + return + X == other.X && + Y == other.Y && + Z == other.Z; + } + + #endregion + } } diff --git a/Source/OpenTK/Math/Vector4.cs b/Source/OpenTK/Math/Vector4.cs index f1475a10..086236b3 100644 --- a/Source/OpenTK/Math/Vector4.cs +++ b/Source/OpenTK/Math/Vector4.cs @@ -16,8 +16,9 @@ namespace OpenTK.Math /// /// Represents a four-dimensional vector. /// + [Serializable] [StructLayout(LayoutKind.Sequential)] - public struct Vector4 + public struct Vector4 : IEquatable { #region Fields @@ -276,6 +277,16 @@ namespace OpenTK.Math return vec; } + public static bool operator ==(Vector4 left, Vector4 right) + { + return left.Equals(right); + } + + public static bool operator !=(Vector4 left, Vector4 right) + { + return !left.Equals(right); + } + [CLSCompliant(false)] unsafe public static explicit operator float*(Vector4 v) { @@ -682,5 +693,51 @@ namespace OpenTK.Math } #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 X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode() ^ W.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 Vector4)) + return false; + + return this.Equals((Vector4)obj); + } + + #endregion + + #region IEquatable Members + + /// Indicates whether the current vector is equal to another vector. + /// A vector to compare with this vector. + /// true if the current vector is equal to the vector parameter; otherwise, false. + public bool Equals(Vector4 other) + { + return + X == other.X && + Y == other.Y && + Z == other.Z && + W == other.W; + } + + #endregion + } }