Better hashcodes for Vectors and Vertices

This commit is contained in:
Latif Khalifa
2014-06-27 11:15:54 +02:00
committed by Justin Clark-Casey
parent 606834e9cf
commit 7e4bdad41a
3 changed files with 38 additions and 3 deletions

View File

@@ -80,7 +80,7 @@ namespace OpenMetaverse.Rendering
#region Structs
[StructLayout(LayoutKind.Explicit)]
public struct Vertex
public struct Vertex : IEquatable<Vertex>
{
[FieldOffset(0)]
public Vector3 Position;
@@ -93,6 +93,36 @@ namespace OpenMetaverse.Rendering
{
return String.Format("P: {0} N: {1} T: {2}", Position, Normal, TexCoord);
}
public override int GetHashCode()
{
int hash = Position.GetHashCode();
hash = hash * 31 + Normal.GetHashCode();
hash = hash * 31 + TexCoord.GetHashCode();
return hash;
}
public static bool operator ==(Vertex value1, Vertex value2)
{
return value1.Position == value2.Position
&& value1.Normal == value2.Normal
&& value1.TexCoord == value2.TexCoord;
}
public static bool operator !=(Vertex value1, Vertex value2)
{
return !(value1 == value2);
}
public override bool Equals(object obj)
{
return (obj is Vertex) ? this == (Vertex)obj : false;
}
public bool Equals(Vertex other)
{
return this == other;
}
}
public struct ProfileFace

View File

@@ -355,7 +355,9 @@ namespace OpenMetaverse
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode();
int hash = X.GetHashCode();
hash = hash * 31 + Y.GetHashCode();
return hash;
}
/// <summary>

View File

@@ -441,7 +441,10 @@ namespace OpenMetaverse
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode();
int hash = X.GetHashCode();
hash = hash * 31 + Y.GetHashCode();
hash = hash * 31 + Z.GetHashCode();
return hash;
}
/// <summary>