Simplify AABB
diff --git a/include/rive/math/aabb.hpp b/include/rive/math/aabb.hpp index 36bb3e5..16b8a1b 100644 --- a/include/rive/math/aabb.hpp +++ b/include/rive/math/aabb.hpp
@@ -22,15 +22,9 @@ class AABB { public: - union { - float buffer[4]; - struct { - float minX, minY, maxX, maxY; - }; - }; + float minX, minY, maxX, maxY; - public: - AABB() : buffer{0} {} + AABB() : minX(0), minY(0), maxX(0), maxY(0) {} AABB(const AABB& o) : minX(o.minX), minY(o.minY), maxX(o.maxX), maxY(o.maxY) {} AABB(float minX, float minY, float maxX, float maxY) : @@ -45,17 +39,6 @@ } bool operator!=(const AABB& o) const { return !(*this == o); } - inline const float* values() const { return buffer; } - - float& operator[](std::size_t idx) { return buffer[idx]; } - const float& operator[](std::size_t idx) const { return buffer[idx]; } - - static bool contains(const AABB& a, const AABB& b); - static bool isValid(const AABB& a); - static bool testOverlap(const AABB& a, const AABB& b); - static bool areIdentical(const AABB& a, const AABB& b); - static void transform(AABB& out, const AABB& a, const Mat2D& matrix); - float left() const { return minX; } float top() const { return minY; } float right() const { return maxX; }
diff --git a/src/math/aabb.cpp b/src/math/aabb.cpp index 62db4c4..028e213 100644 --- a/src/math/aabb.cpp +++ b/src/math/aabb.cpp
@@ -24,47 +24,6 @@ maxY = B; } -bool AABB::contains(const AABB& a, const AABB& b) { - return a[0] <= b[0] && a[1] <= b[1] && b[2] <= a[2] && b[3] <= a[3]; -} - -bool AABB::isValid(const AABB& a) { - float dx = a[2] - a[0]; - float dy = a[3] - a[1]; - return dx >= 0.0f && dy >= 0.0f && std::isfinite(a[0]) && std::isfinite(a[1]) && - std::isfinite(a[2]) && std::isfinite(a[3]); -} - -bool AABB::testOverlap(const AABB& a, const AABB& b) { - float d1x = b[0] - a[2]; - float d1y = b[1] - a[3]; - - float d2x = a[0] - b[2]; - float d2y = a[1] - b[3]; - - if (d1x > 0.0 || d1y > 0.0) { - return false; - } - - if (d2x > 0.0 || d2y > 0.0) { - return false; - } - - return true; -} - -void AABB::transform(AABB& out, const AABB& a, const Mat2D& matrix) { - const auto p1 = matrix * Vec2D(a[0], a[1]); - const auto p2 = matrix * Vec2D(a[2], a[1]); - const auto p3 = matrix * Vec2D(a[2], a[3]); - const auto p4 = matrix * Vec2D(a[0], a[3]); - - out[0] = std::fmin(p1[0], std::fmin(p2[0], std::fmin(p3[0], p4[0]))); - out[1] = std::fmin(p1[1], std::fmin(p2[1], std::fmin(p3[1], p4[1]))); - out[2] = std::fmax(p1[0], std::fmax(p2[0], std::fmax(p3[0], p4[0]))); - out[3] = std::fmax(p1[1], std::fmax(p2[1], std::fmax(p3[1], p4[1]))); -} - static inline float graphics_roundf(float x) { return std::floor(x + 0.5f); } static inline int graphics_round(float x) { return (int)graphics_roundf(x); }
diff --git a/src/renderer.cpp b/src/renderer.cpp index e41c250..d56657b 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -4,10 +4,10 @@ using namespace rive; Mat2D rive::computeAlignment(Fit fit, Alignment alignment, const AABB& frame, const AABB& content) { - float contentWidth = content[2] - content[0]; - float contentHeight = content[3] - content[1]; - float x = -content[0] - contentWidth / 2.0 - (alignment.x() * contentWidth / 2.0); - float y = -content[1] - contentHeight / 2.0 - (alignment.y() * contentHeight / 2.0); + float contentWidth = content.width(); + float contentHeight = content.height(); + float x = -content.left() - contentWidth / 2.0 - (alignment.x() * contentWidth / 2.0); + float y = -content.top() - contentHeight / 2.0 - (alignment.y() * contentHeight / 2.0); float scaleX = 1.0, scaleY = 1.0; @@ -52,8 +52,8 @@ } Mat2D translation; - translation[4] = frame[0] + frame.width() / 2.0 + (alignment.x() * frame.width() / 2.0); - translation[5] = frame[1] + frame.height() / 2.0 + (alignment.y() * frame.height() / 2.0); + translation[4] = frame.left() + frame.width() / 2.0 + (alignment.x() * frame.width() / 2.0); + translation[5] = frame.top() + frame.height() / 2.0 + (alignment.y() * frame.height() / 2.0); return translation * Mat2D::fromScale(scaleX, scaleY) * Mat2D::fromTranslate(x, y); }