BLI: add comparison operators and hash functions for float3, etc.

This commit is contained in:
2020-07-08 14:40:34 +02:00
parent ff444da7c4
commit a8ff8b64dc
4 changed files with 64 additions and 0 deletions

View File

@@ -51,6 +51,25 @@ struct Color4f {
stream << "(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
return stream;
}
friend bool operator==(const Color4f &a, const Color4f &b)
{
return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
}
friend bool operator!=(const Color4f &a, const Color4f &b)
{
return !(a == b);
}
uint32_t hash() const
{
uint32_t x1 = *(uint32_t *)&r;
uint32_t x2 = *(uint32_t *)&g;
uint32_t x3 = *(uint32_t *)&b;
uint32_t x4 = *(uint32_t *)&a;
return (x1 * 1283591) ^ (x2 * 850177) ^ (x3 * 735391) ^ (x4 * 442319);
}
};
struct Color4b {
@@ -89,6 +108,16 @@ struct Color4b {
stream << "(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
return stream;
}
friend bool operator==(const Color4b &a, const Color4b &b)
{
return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
}
friend bool operator!=(const Color4b &a, const Color4b &b)
{
return !(a == b);
}
};
} // namespace blender

View File

@@ -79,6 +79,16 @@ struct float2 {
stream << "(" << v.x << ", " << v.y << ")";
return stream;
}
friend bool operator==(const float2 &a, const float2 &b)
{
return a.x == b.x && a.y == b.y;
}
friend bool operator!=(const float2 &a, const float2 &b)
{
return !(a == b);
}
};
} // namespace blender

View File

@@ -128,6 +128,16 @@ struct float3 {
return stream;
}
friend bool operator==(const float3 &a, const float3 &b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
friend bool operator!=(const float3 &a, const float3 &b)
{
return !(a == b);
}
float normalize_and_get_length()
{
return normalize_v3(*this);
@@ -178,6 +188,14 @@ struct float3 {
z = -z;
}
uint32_t hash() const
{
uint32_t x1 = *(uint32_t *)&x;
uint32_t x2 = *(uint32_t *)&y;
uint32_t x3 = *(uint32_t *)&z;
return (x1 * 435109) ^ (x2 * 380867) ^ (x3 * 1059217);
}
static float dot(const float3 &a, const float3 &b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;

View File

@@ -154,6 +154,13 @@ template<> struct DefaultHash<float> {
}
};
template<> struct DefaultHash<bool> {
uint32_t operator()(bool value) const
{
return (uint32_t)(value != false) * 1298191;
}
};
inline uint32_t hash_string(StringRef str)
{
uint32_t hash = 5381;