1 #include "math/aabb.hpp"
6 AABB::AABB() : buffer{0} {}
8 AABB::AABB(const AABB& copy)
10 buffer[0] = copy.buffer[0];
11 buffer[1] = copy.buffer[1];
12 buffer[2] = copy.buffer[2];
13 buffer[3] = copy.buffer[3];
16 AABB::AABB(float minX, float minY, float maxX, float maxY) :
17 buffer{minX, minY, maxX, maxY}
21 void AABB::center(Vec2D& out, const AABB& a)
23 out[0] = (a[0] + a[2]) * 0.5f;
24 out[1] = (a[1] + a[3]) * 0.5f;
27 void AABB::size(Vec2D& out, const AABB& a)
33 void AABB::extents(Vec2D& out, const AABB& a)
35 out[0] = (a[2] - a[0]) * 0.5;
36 out[1] = (a[3] - a[1]) * 0.5;
39 void AABB::combine(AABB& out, const AABB& a, const AABB& b) {}
41 bool AABB::contains(const AABB& a, const AABB& b)
43 return a[0] <= b[0] && a[1] <= b[1] && b[2] <= a[2] && b[3] <= a[3];
46 bool AABB::isValid(const AABB& a)
48 float dx = a[2] - a[0];
49 float dy = a[3] - a[1];
50 return dx >= 0.0f && dy >= 0.0f && std::isfinite(a[0]) &&
51 std::isfinite(a[1]) && std::isfinite(a[2]) && std::isfinite(a[3]);
54 bool AABB::testOverlap(const AABB& a, const AABB& b)
56 float d1x = b[0] - a[2];
57 float d1y = b[1] - a[3];
59 float d2x = a[0] - b[2];
60 float d2y = a[1] - b[3];
62 if (d1x > 0.0 || d1y > 0.0)
67 if (d2x > 0.0 || d2y > 0.0)
75 bool AABB::areIdentical(const AABB& a, const AABB& b)
77 return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
80 float AABB::width() const { return max[0] - min[0]; }
82 float AABB::height() const { return max[1] - min[1]; }
84 float AABB::perimeter() const
86 float wx = buffer[2] - buffer[0];
87 float wy = buffer[3] - buffer[1];
88 return 2.0 * (wx + wy);
91 void AABB::transform(AABB& out, const AABB& a, const Mat2D& matrix)
98 Vec2D::transform(p1, p1, matrix);
99 Vec2D::transform(p2, p2, matrix);
100 Vec2D::transform(p3, p3, matrix);
101 Vec2D::transform(p4, p4, matrix);
103 out[0] = std::fmin(p1[0], std::fmin(p2[0], std::fmin(p3[0], p4[0])));
104 out[1] = std::fmin(p1[1], std::fmin(p2[1], std::fmin(p3[1], p4[1])));
105 out[2] = std::fmax(p1[0], std::fmax(p2[0], std::fmax(p3[0], p4[0])));
106 out[3] = std::fmax(p1[1], std::fmax(p2[1], std::fmax(p3[1], p4[1])));