Added serializable attribute.

Added IEquatable interface and operator== overloads to Matrix4 and Vector* structs.
This commit is contained in:
the_fiddler 2008-07-10 14:01:38 +00:00
parent b477ad58de
commit 2af017c3ea
5 changed files with 251 additions and 23 deletions

View file

@ -16,8 +16,9 @@ namespace OpenTK.Math
/// <summary>
/// Represents a 4x4 Matrix
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Matrix4
public struct Matrix4 : IEquatable<Matrix4>
{
#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()
/// <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 Matrix4))
return false;
return this.Equals((Matrix4)obj);
}
#endregion
#region IEquatable<Matrix4> Members
/// <summary>Indicates whether the current matrix is equal to another matrix.</summary>
/// <param name="matrix">An matrix to compare with this matrix.</param>
/// <returns>true if the current matrix is equal to the matrix parameter; otherwise, false.</returns>
public bool Equals(Matrix4 other)
{
return
Row0 == other.Row0 &&
Row1 == other.Row1 &&
Row2 == other.Row2 &&
Row3 == other.Row3;
}
#endregion
}
}

View file

@ -16,6 +16,7 @@ namespace OpenTK.Math
/// <summary>
/// Represents a Quaternion
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Quaternion
{

View file

@ -19,8 +19,9 @@ namespace OpenTK.Math
/// <remarks>
/// The Vector2 structure is suitable for interoperation with unmanaged code requiring two consecutive floats.
/// </remarks>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Vector2
public struct Vector2 : IEquatable<Vector2>
{
#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()
/// <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 X.GetHashCode() ^ Y.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 Vector2))
return false;
return this.Equals((Vector2)obj);
}
#endregion
#region IEquatable<Vector2> Members
/// <summary>Indicates whether the current vector is equal to another vector.</summary>
/// <param name="vector">A vector to compare with this vector.</param>
/// <returns>true if the current vector is equal to the vector parameter; otherwise, false.</returns>
public bool Equals(Vector2 other)
{
return
X == other.X &&
Y == other.Y;
}
#endregion
}
}

View file

@ -16,8 +16,9 @@ namespace OpenTK.Math
/// <summary>
/// Represents a three-dimensional vector.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Vector3
public struct Vector3 : IEquatable<Vector3>
{
#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
/// <summary>
@ -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()
/// <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 X.GetHashCode() ^ Y.GetHashCode() ^ Z.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 Vector3))
return false;
return this.Equals((Vector3)obj);
}
#endregion
#region IEquatable<Vector3> Members
/// <summary>Indicates whether the current vector is equal to another vector.</summary>
/// <param name="vector">A vector to compare with this vector.</param>
/// <returns>true if the current vector is equal to the vector parameter; otherwise, false.</returns>
public bool Equals(Vector3 other)
{
return
X == other.X &&
Y == other.Y &&
Z == other.Z;
}
#endregion
}
}

View file

@ -16,8 +16,9 @@ namespace OpenTK.Math
/// <summary>
/// Represents a four-dimensional vector.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Vector4
public struct Vector4 : IEquatable<Vector4>
{
#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()
/// <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 X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode() ^ W.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 Vector4))
return false;
return this.Equals((Vector4)obj);
}
#endregion
#region IEquatable<Vector4> Members
/// <summary>Indicates whether the current vector is equal to another vector.</summary>
/// <param name="vector">A vector to compare with this vector.</param>
/// <returns>true if the current vector is equal to the vector parameter; otherwise, false.</returns>
public bool Equals(Vector4 other)
{
return
X == other.X &&
Y == other.Y &&
Z == other.Z &&
W == other.W;
}
#endregion
}
}