Skip to content

Commit

Permalink
Improve vectors comparison approach, and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cfnptr committed Feb 4, 2025
1 parent a3898b2 commit 75607bc
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 169 deletions.
37 changes: 25 additions & 12 deletions include/math/aabb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Aabb
*/
constexpr Aabb(const float3& min = float3(0.0f), const float3& max = float3(0.0f)) noexcept : min(min), max(max)
{
assert(min <= max);
assert((min <= max).areAllTrue());
}

/**
Expand All @@ -69,7 +69,7 @@ struct Aabb
*/
void setMin(const float3& min) noexcept
{
assert(min <= this->max);
assert((min <= this->max).areAllTrue());
this->min = min;
}
/**
Expand All @@ -79,7 +79,7 @@ struct Aabb
*/
void setMax(const float3& max) noexcept
{
assert(max >= this->min);
assert((max >= this->min).areAllTrue());
this->max = max;
}
/**
Expand All @@ -91,7 +91,7 @@ struct Aabb
*/
void set(const float3& min, const float3& max) noexcept
{
assert(min <= max);
assert((min <= max).areAllTrue());
this->min = min;
this->max = max;
}
Expand All @@ -104,7 +104,7 @@ struct Aabb
*/
void setSize(const float3& size, const float3& position = float3(0.0f)) noexcept
{
assert(size >= 0.0f);
assert((size >= 0.0f).areAllTrue());
auto extent = size * 0.5f;
min = position - extent;
max = position + extent;
Expand All @@ -127,7 +127,7 @@ struct Aabb
*/
void setExtent(const float3& extent, const float3& position = float3(0.0f)) noexcept
{
assert(extent >= 0.0f);
assert((extent >= 0.0f).areAllTrue());
min = position - extent;
max = position + extent;
}
Expand Down Expand Up @@ -186,10 +186,23 @@ struct Aabb
Aabb& operator-=(const float3& v) noexcept { min -= v; max -= v; return *this; }
constexpr bool operator==(const Aabb& v) const noexcept { return min == v.min && max == v.max; }
constexpr bool operator!=(const Aabb& v) const noexcept { return min != v.min || max != v.max; }
constexpr bool operator<(const Aabb& v) const noexcept { return min < v.min && max < v.max; }
constexpr bool operator>(const Aabb& v) const noexcept { return min > v.min && max > v.max; }
constexpr bool operator<=(const Aabb& v) const noexcept { return min <= v.min && max <= v.max; }
constexpr bool operator>=(const Aabb& v) const noexcept { return min >= v.min && max >= v.max; }

constexpr bool operator<(const Aabb& v) const noexcept
{
return (min < v.min).areAllTrue() && (max < v.max).areAllTrue();
}
constexpr bool operator>(const Aabb& v) const noexcept
{
return (min > v.min).areAllTrue() && (max > v.max).areAllTrue();
}
constexpr bool operator<=(const Aabb& v) const noexcept
{
return (min <= v.min).areAllTrue() && (max <= v.max).areAllTrue();
}
constexpr bool operator>=(const Aabb& v) const noexcept
{
return (min >= v.min).areAllTrue() && (max >= v.max).areAllTrue();
}

static const Aabb zero, one, two, half;
};
Expand All @@ -215,7 +228,7 @@ static bool isBinaryLess(const Aabb& a, const Aabb& b) noexcept { return memcmp(
*/
static constexpr bool isInside(const Aabb& aabb, const float3& point) noexcept
{
return point >= aabb.getMin() && point <= aabb.getMax();
return (aabb.getMin() <= point).areAllTrue() && (point <= aabb.getMax()).areAllTrue();
}
/**
* @brief Returns closest point inside AABB to the specified one.
Expand Down Expand Up @@ -301,7 +314,7 @@ static bool raycastI(const Aabb& aabb, const Ray& ray) noexcept
*/
static bool isIntersected(const Aabb& a, const Aabb& b) noexcept
{
return a.getMin() <= b.getMax() && a.getMax() >= b.getMin();
return (a.getMin() <= b.getMax()).areAllTrue() && (b.getMin() <= a.getMax()).areAllTrue();
}

/**
Expand Down
120 changes: 78 additions & 42 deletions include/math/vector/float.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ struct float2
float2& operator=(float n) noexcept { x = n; y = n; return *this; }
constexpr bool operator==(float2 v) const noexcept { return x == v.x && y == v.y; }
constexpr bool operator!=(float2 v) const noexcept { return x != v.x || y != v.y; }
constexpr bool operator<(float2 v) const noexcept { return x < v.x && y < v.y; }
constexpr bool operator>(float2 v) const noexcept { return x > v.x && y > v.y; }
constexpr bool operator<=(float2 v) const noexcept { return x <= v.x && y <= v.y; }
constexpr bool operator>=(float2 v) const noexcept { return x >= v.x && y >= v.y; }
constexpr uint2 operator<(float2 v) const noexcept { return uint2(x < v.x, y < v.y); }
constexpr uint2 operator>(float2 v) const noexcept { return uint2(x > v.x, y > v.y); }
constexpr uint2 operator<=(float2 v) const noexcept { return uint2(x <= v.x, y <= v.y); }
constexpr uint2 operator>=(float2 v) const noexcept { return uint2(x >= v.x, y >= v.y); }

static const float2 zero, one, minusOne, left, right, bottom, top;
};
Expand Down Expand Up @@ -255,10 +255,10 @@ struct float3
float3& operator=(float n) noexcept { x = n; y = n; z = n; return *this; }
constexpr bool operator==(const float3& v) const noexcept { return x == v.x && y == v.y && z == v.z; }
constexpr bool operator!=(const float3& v) const noexcept { return x != v.x || y != v.y || z != v.z; }
constexpr bool operator<(const float3& v) const noexcept { return x < v.x && y < v.y && z < v.z; }
constexpr bool operator>(const float3& v) const noexcept { return x > v.x && y > v.y && z > v.z; }
constexpr bool operator<=(const float3& v) const noexcept { return x <= v.x && y <= v.y && z <= v.z; }
constexpr bool operator>=(const float3& v) const noexcept { return x >= v.x && y >= v.y && z >= v.z; }
constexpr uint3 operator<(const float3& v) const noexcept { return uint3(x < v.x, y < v.y, z < v.z); }
constexpr uint3 operator>(const float3& v) const noexcept { return uint3(x > v.x, y > v.y, z > v.z); }
constexpr uint3 operator<=(const float3& v) const noexcept { return uint3(x <= v.x, y <= v.y, z <= v.z); }
constexpr uint3 operator>=(const float3& v) const noexcept { return uint3(x >= v.x, y >= v.y, z >= v.z); }

static const float3 zero, one, minusOne, left, right, bottom, top, back, front;
};
Expand Down Expand Up @@ -444,10 +444,10 @@ struct float4
float4& operator=(float n) noexcept { x = n; y = n; z = n; w = n; return *this; }
constexpr bool operator==(const float4& v) const noexcept { return x == v.x && y == v.y && z == v.z && w == v.w; }
constexpr bool operator!=(const float4& v) const noexcept { return x != v.x || y != v.y || z != v.z || w != v.w; }
constexpr bool operator<(const float4& v) const noexcept { return x < v.x && y < v.y && z < v.z && w < v.w; }
constexpr bool operator>(const float4& v) const noexcept { return x > v.x && y > v.y && z > v.z && w > v.w; }
constexpr bool operator<=(const float4& v) const noexcept { return x <= v.x && y <= v.y && z <= v.z && w <= v.w; }
constexpr bool operator>=(const float4& v) const noexcept { return x >= v.x && y >= v.y && z >= v.z && w >= v.w; }
constexpr uint4 operator<(const float4& v) const noexcept { return uint4(x < v.x, y < v.y, z < v.z, w < v.w); }
constexpr uint4 operator>(const float4& v) const noexcept { return uint4(x > v.x, y > v.y, z > v.z, w > v.w); }
constexpr uint4 operator<=(const float4& v) const noexcept { return uint4(x <= v.x, y <= v.y, z <= v.z, w <= v.w); }
constexpr uint4 operator>=(const float4& v) const noexcept { return uint4(x >= v.x, y >= v.y, z >= v.z, w >= v.w); }

static const float4 zero, one, minusOne;
};
Expand All @@ -463,21 +463,21 @@ static constexpr float2 operator*(float2 v, float n) noexcept { return float2(v.
static constexpr float2 operator/(float2 v, float n) noexcept { return float2(v.x / n, v.y / n); }
static constexpr bool operator==(float2 v, float n) noexcept { return v == float2(n); }
static constexpr bool operator!=(float2 v, float n) noexcept { return v != float2(n); }
static constexpr bool operator<(float2 v, float n) noexcept { return v < float2(n); }
static constexpr bool operator>(float2 v, float n) noexcept { return v > float2(n); }
static constexpr bool operator<=(float2 v, float n) noexcept { return v <= float2(n); }
static constexpr bool operator>=(float2 v, float n) noexcept { return v >= float2(n); }
static constexpr uint2 operator<(float2 v, float n) noexcept { return v < float2(n); }
static constexpr uint2 operator>(float2 v, float n) noexcept { return v > float2(n); }
static constexpr uint2 operator<=(float2 v, float n) noexcept { return v <= float2(n); }
static constexpr uint2 operator>=(float2 v, float n) noexcept { return v >= float2(n); }

static constexpr float2 operator+(float n, float2 v) noexcept { return float2(n + v.x, n + v.y); }
static constexpr float2 operator-(float n, float2 v) noexcept { return float2(n - v.x, n - v.y); }
static constexpr float2 operator*(float n, float2 v) noexcept { return float2(n * v.x, n * v.y); }
static constexpr float2 operator/(float n, float2 v) noexcept { return float2(n / v.x, n / v.y); }
static constexpr bool operator==(float n, float2 v) noexcept { return float2(n) == v; }
static constexpr bool operator!=(float n, float2 v) noexcept { return float2(n) != v; }
static constexpr bool operator<(float n, float2 v) noexcept { return float2(n) < v; }
static constexpr bool operator>(float n, float2 v) noexcept { return float2(n) > v; }
static constexpr bool operator<=(float n, float2 v) noexcept { return float2(n) <= v; }
static constexpr bool operator>=(float n, float2 v) noexcept { return float2(n) >= v; }
static constexpr uint2 operator<(float n, float2 v) noexcept { return float2(n) < v; }
static constexpr uint2 operator>(float n, float2 v) noexcept { return float2(n) > v; }
static constexpr uint2 operator<=(float n, float2 v) noexcept { return float2(n) <= v; }
static constexpr uint2 operator>=(float n, float2 v) noexcept { return float2(n) >= v; }

/**
* @brief Returns true if first vector binary representation is less than the second.
Expand All @@ -487,28 +487,40 @@ static constexpr bool operator>=(float n, float2 v) noexcept { return float2(n)
*/
static bool isBinaryLess(float2 a, float2 b) noexcept { return *((const int64*)&a) < *((const int64*)&b); }

/**
* @brief Selects between two vector components based on the control vector values.
*
* @param[in] c control vector (contains is true or false)
* @param[in] t contains components for true condition
* @param[in] f contains components for false condition
*/
static constexpr float2 select(uint2 c, float2 t, float2 f) noexcept
{
return float2(c.x ? t.x : f.x, c.y ? t.y : f.y);
}

//**********************************************************************************************************************
static constexpr float3 operator+(const float3& v, float n) noexcept { return float3(v.x + n, v.y + n, v.z + n); }
static constexpr float3 operator-(const float3& v, float n) noexcept { return float3(v.x - n, v.y - n, v.z - n); }
static constexpr float3 operator*(const float3& v, float n) noexcept { return float3(v.x * n, v.y * n, v.z * n); }
static constexpr float3 operator/(const float3& v, float n) noexcept { return float3(v.x / n, v.y / n, v.z / n); }
static constexpr bool operator==(const float3& v, float n) noexcept { return v == float3(n); }
static constexpr bool operator!=(const float3& v, float n) noexcept { return v != float3(n); }
static constexpr bool operator<(const float3& v, float n) noexcept { return v < float3(n); }
static constexpr bool operator>(const float3& v, float n) noexcept { return v > float3(n); }
static constexpr bool operator<=(const float3& v, float n) noexcept { return v <= float3(n); }
static constexpr bool operator>=(const float3& v, float n) noexcept { return v >= float3(n); }
static constexpr uint3 operator<(const float3& v, float n) noexcept { return v < float3(n); }
static constexpr uint3 operator>(const float3& v, float n) noexcept { return v > float3(n); }
static constexpr uint3 operator<=(const float3& v, float n) noexcept { return v <= float3(n); }
static constexpr uint3 operator>=(const float3& v, float n) noexcept { return v >= float3(n); }

static constexpr float3 operator+(float n, const float3& v) noexcept { return float3(n + v.x, n + v.y, n + v.z); }
static constexpr float3 operator-(float n, const float3& v) noexcept { return float3(n - v.x, n - v.y, n - v.z); }
static constexpr float3 operator*(float n, const float3& v) noexcept { return float3(n * v.x, n * v.y, n * v.z); }
static constexpr float3 operator/(float n, const float3& v) noexcept { return float3(n / v.x, n / v.y, n / v.z); }
static constexpr bool operator==(float n, const float3& v) noexcept { return float3(n) == v; }
static constexpr bool operator!=(float n, const float3& v) noexcept { return float3(n) != v; }
static constexpr bool operator<(float n, const float3& v) noexcept { return float3(n) < v; }
static constexpr bool operator>(float n, const float3& v) noexcept { return float3(n) > v; }
static constexpr bool operator<=(float n, const float3& v) noexcept { return float3(n) <= v; }
static constexpr bool operator>=(float n, const float3& v) noexcept { return float3(n) >= v; }
static constexpr uint3 operator<(float n, const float3& v) noexcept { return float3(n) < v; }
static constexpr uint3 operator>(float n, const float3& v) noexcept { return float3(n) > v; }
static constexpr uint3 operator<=(float n, const float3& v) noexcept { return float3(n) <= v; }
static constexpr uint3 operator>=(float n, const float3& v) noexcept { return float3(n) >= v; }

/**
* @brief Returns true if first vector binary representation is less than the second.
Expand All @@ -518,28 +530,40 @@ static constexpr bool operator>=(float n, const float3& v) noexcept { return flo
*/
static bool isBinaryLess(const float3& a, const float3& b) noexcept { return memcmp(&a, &b, sizeof(float) * 3) < 0; }

/**
* @brief Selects between two vector components based on the control vector values.
*
* @param[in] c control vector (contains is true or false)
* @param[in] t contains components for true condition
* @param[in] f contains components for false condition
*/
static constexpr float3 select(const uint3& c, const float3& t, const float3& f) noexcept
{
return float3(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z);
}

//**********************************************************************************************************************
static constexpr float4 operator+(const float4& v, float n) noexcept { return float4(v.x + n, v.y + n, v.z + n, v.w + n); }
static constexpr float4 operator-(const float4& v, float n) noexcept { return float4(v.x - n, v.y - n, v.z - n, v.w - n); }
static constexpr float4 operator*(const float4& v, float n) noexcept { return float4(v.x * n, v.y * n, v.z * n, v.w * n); }
static constexpr float4 operator/(const float4& v, float n) noexcept { return float4(v.x / n, v.y / n, v.z / n, v.w / n); }
static constexpr bool operator==(const float4& v, float n) noexcept { return v == float4(n); }
static constexpr bool operator!=(const float4& v, float n) noexcept { return v != float4(n); }
static constexpr bool operator<(const float4& v, float n) noexcept { return v < float4(n); }
static constexpr bool operator>(const float4& v, float n) noexcept { return v > float4(n); }
static constexpr bool operator<=(const float4& v, float n) noexcept { return v <= float4(n); }
static constexpr bool operator>=(const float4& v, float n) noexcept { return v >= float4(n); }
static constexpr uint4 operator<(const float4& v, float n) noexcept { return v < float4(n); }
static constexpr uint4 operator>(const float4& v, float n) noexcept { return v > float4(n); }
static constexpr uint4 operator<=(const float4& v, float n) noexcept { return v <= float4(n); }
static constexpr uint4 operator>=(const float4& v, float n) noexcept { return v >= float4(n); }

static constexpr float4 operator+(float n, const float4& v) noexcept { return float4(n + v.x, n + v.y, n + v.z, n + v.w); }
static constexpr float4 operator-(float n, const float4& v) noexcept { return float4(n - v.x, n - v.y, n - v.z, n - v.w); }
static constexpr float4 operator*(float n, const float4& v) noexcept { return float4(n * v.x, n * v.y, n * v.z, n * v.w); }
static constexpr float4 operator/(float n, const float4& v) noexcept { return float4(n / v.x, n / v.y, n / v.z, n / v.w); }
static constexpr bool operator==(float n, const float4& v) noexcept { return float4(n) == v; }
static constexpr bool operator!=(float n, const float4& v) noexcept { return float4(n) != v; }
static constexpr bool operator<(float n, const float4& v) noexcept { return float4(n) < v; }
static constexpr bool operator>(float n, const float4& v) noexcept { return float4(n) > v; }
static constexpr bool operator<=(float n, const float4& v) noexcept { return float4(n) <= v; }
static constexpr bool operator>=(float n, const float4& v) noexcept { return float4(n) >= v; }
static constexpr uint4 operator<(float n, const float4& v) noexcept { return float4(n) < v; }
static constexpr uint4 operator>(float n, const float4& v) noexcept { return float4(n) > v; }
static constexpr uint4 operator<=(float n, const float4& v) noexcept { return float4(n) <= v; }
static constexpr uint4 operator>=(float n, const float4& v) noexcept { return float4(n) >= v; }

/**
* @brief Returns true if first vector binary representation is less than the second.
Expand All @@ -549,6 +573,18 @@ static constexpr bool operator>=(float n, const float4& v) noexcept { return flo
*/
static bool isBinaryLess(const float4& a, const float4& b) noexcept { return memcmp(&a, &b, sizeof(float) * 4) < 0; }

/**
* @brief Selects between two vector components based on the control vector values.
*
* @param[in] c control vector (contains is true or false)
* @param[in] t contains components for true condition
* @param[in] f contains components for false condition
*/
static constexpr float4 select(const uint4& c, const float4& t, const float4& f) noexcept
{
return float4(c.x ? t.x : f.x, c.y ? t.y : f.y, c.z ? t.z : f.z, c.w ? t.w : f.w);
}

//**********************************************************************************************************************
// TODO: possibly add more specific math functions like remquo, sph_neumann or dFdx.

Expand Down Expand Up @@ -619,8 +655,8 @@ static float2 lerpDelta(float2 a, float2 b, float f, float dt) noexcept
}
static float2 gain(float2 x, float2 k) noexcept
{
auto a = float2(0.5f) * pow(2.0f * ((x < 0.5f) ? x : 1.0f - x), k);
return (x < 0.5f) ? a : 1.0f - a;
auto a = float2(0.5f) * pow(2.0f * select(x < 0.5f, x, 1.0f - x), k);
return select(x < 0.5f, a, 1.0f - a);
}

//**********************************************************************************************************************
Expand Down Expand Up @@ -705,8 +741,8 @@ static float3 lerpDelta(const float3& a, const float3& b, float f, float dt) noe
}
static float3 gain(const float3& x, const float3& k) noexcept
{
auto a = float3(0.5f) * pow(2.0f * ((x < 0.5f) ? x : 1.0f - x), k);
return (x < 0.5f) ? a : 1.0f - a;
auto a = float3(0.5f) * pow(2.0f * select(x < 0.5f, x, 1.0f - x), k);
return select(x < 0.5f, a, 1.0f - a);
}

//**********************************************************************************************************************
Expand Down Expand Up @@ -874,8 +910,8 @@ static float4 lerpDelta(const float4& a, const float4& b, float f, float dt) noe
}
static float4 gain(const float4& x, const float4& k) noexcept
{
auto a = float4(0.5f) * pow(2.0f * ((x < 0.5f) ? x : 1.0f - x), k);
return (x < 0.5f) ? a : 1.0f - a;
auto a = float4(0.5f) * pow(2.0f * select(x < 0.5f, x, 1.0f - x), k);
return select(x < 0.5f, a, 1.0f - a);
}

} // namespace math
Loading

0 comments on commit 75607bc

Please sign in to comment.