-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathVectorHash.cs
More file actions
80 lines (69 loc) · 2.46 KB
/
VectorHash.cs
File metadata and controls
80 lines (69 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using UnityEngine;
namespace UnityEngine.ProBuilder
{
/// <summary>
/// Fuzzy hashing functions for vector types. Exists as a shortcut to create hashcodes for Vector3 in the style of
/// IntVector3 without the overhead of casting.
/// </summary>
static class VectorHash
{
public const float FltCompareResolution = 1000000f;
static int HashFloat(float f)
{
ulong u = (ulong)(f * FltCompareResolution);
return (int)(u % int.MaxValue);
}
/// <summary>
/// Return the rounded hashcode for a vector2
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
public static int GetHashCode(Vector2 v)
{
// http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode/263416#263416
int hash = 27;
unchecked
{
hash = hash * 29 + HashFloat(v.x);
hash = hash * 29 + HashFloat(v.y);
}
return hash;
}
/// <summary>
/// Return the hashcode for a vector3 without first converting it to pb_IntVec3.
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
public static int GetHashCode(Vector3 v)
{
// http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode/263416#263416
int hash = 27;
unchecked
{
hash = hash * 29 + HashFloat(v.x);
hash = hash * 29 + HashFloat(v.y);
hash = hash * 29 + HashFloat(v.z);
}
return hash;
}
/// <summary>
/// Return the hashcode for a vector3 without first converting it to pb_IntVec3.
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
public static int GetHashCode(Vector4 v)
{
// http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode/263416#263416
int hash = 27;
unchecked
{
hash = hash * 29 + HashFloat(v.x);
hash = hash * 29 + HashFloat(v.y);
hash = hash * 29 + HashFloat(v.z);
hash = hash * 29 + HashFloat(v.w);
}
return hash;
}
}
}