submodule: add rive-cpp to rive-tizen as submodule
[platform/core/uifw/rive-tizen.git] / submodule / src / math / aabb.cpp
1 #include "math/aabb.hpp"
2 #include <cmath>
3
4 using namespace rive;
5
6 AABB::AABB() : buffer{0} {}
7
8 AABB::AABB(const AABB& copy)
9 {
10         buffer[0] = copy.buffer[0];
11         buffer[1] = copy.buffer[1];
12         buffer[2] = copy.buffer[2];
13         buffer[3] = copy.buffer[3];
14 }
15
16 AABB::AABB(float minX, float minY, float maxX, float maxY) :
17     buffer{minX, minY, maxX, maxY}
18 {
19 }
20
21 void AABB::center(Vec2D& out, const AABB& a)
22 {
23         out[0] = (a[0] + a[2]) * 0.5f;
24         out[1] = (a[1] + a[3]) * 0.5f;
25 }
26
27 void AABB::size(Vec2D& out, const AABB& a)
28 {
29         out[0] = a[2] - a[0];
30         out[1] = a[3] - a[1];
31 }
32
33 void AABB::extents(Vec2D& out, const AABB& a)
34 {
35         out[0] = (a[2] - a[0]) * 0.5;
36         out[1] = (a[3] - a[1]) * 0.5;
37 }
38
39 void AABB::combine(AABB& out, const AABB& a, const AABB& b) {}
40
41 bool AABB::contains(const AABB& a, const AABB& b)
42 {
43         return a[0] <= b[0] && a[1] <= b[1] && b[2] <= a[2] && b[3] <= a[3];
44 }
45
46 bool AABB::isValid(const AABB& a)
47 {
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]);
52 }
53
54 bool AABB::testOverlap(const AABB& a, const AABB& b)
55 {
56         float d1x = b[0] - a[2];
57         float d1y = b[1] - a[3];
58
59         float d2x = a[0] - b[2];
60         float d2y = a[1] - b[3];
61
62         if (d1x > 0.0 || d1y > 0.0)
63         {
64                 return false;
65         }
66
67         if (d2x > 0.0 || d2y > 0.0)
68         {
69                 return false;
70         }
71
72         return true;
73 }
74
75 bool AABB::areIdentical(const AABB& a, const AABB& b)
76 {
77         return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
78 }
79
80 float AABB::width() const { return max[0] - min[0]; }
81
82 float AABB::height() const { return max[1] - min[1]; }
83
84 float AABB::perimeter() const
85 {
86         float wx = buffer[2] - buffer[0];
87         float wy = buffer[3] - buffer[1];
88         return 2.0 * (wx + wy);
89 }
90
91 void AABB::transform(AABB& out, const AABB& a, const Mat2D& matrix)
92 {
93         Vec2D p1(a[0], a[1]);
94         Vec2D p2(a[2], a[1]);
95         Vec2D p3(a[2], a[3]);
96         Vec2D p4(a[0], a[3]);
97
98         Vec2D::transform(p1, p1, matrix);
99         Vec2D::transform(p2, p2, matrix);
100         Vec2D::transform(p3, p3, matrix);
101         Vec2D::transform(p4, p4, matrix);
102
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])));
107 }