forked from rive-app/rive-cpp-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaabb.cpp
More file actions
82 lines (71 loc) · 1.7 KB
/
aabb.cpp
File metadata and controls
82 lines (71 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "rive/math/math_types.hpp"
#include "rive/math/aabb.hpp"
#include "rive/math/simd.hpp"
#include <algorithm>
#include <cmath>
using namespace rive;
AABB::AABB(Span<Vec2D> pts)
{
if (pts.size() == 0)
{
minX = minY = maxX = maxY = 0;
return;
}
float L = pts[0].x, R = L, T = pts[0].y, B = T;
for (size_t i = 1; i < pts.size(); ++i)
{
L = std::min(L, pts[i].x);
R = std::max(R, pts[i].x);
T = std::min(T, pts[i].y);
B = std::max(B, pts[i].y);
}
minX = L;
maxX = R;
minY = T;
maxY = B;
}
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); }
IAABB AABB::round() const
{
return {
graphics_round(left()),
graphics_round(top()),
graphics_round(right()),
graphics_round(bottom()),
};
}
IAABB AABB::roundOut() const
{
float4 bounds = simd::load4f(this);
bounds.xy = simd::floor(bounds.xy);
bounds.zw = simd::ceil(bounds.zw);
return math::bit_cast<IAABB>(simd::cast<int32_t>(bounds));
}
void AABB::expandTo(AABB& out, const Vec2D& point) { expandTo(out, point.x, point.y); }
void AABB::expandTo(AABB& out, float x, float y)
{
if (x < out.minX)
{
out.minX = x;
}
if (x > out.maxX)
{
out.maxX = x;
}
if (y < out.minY)
{
out.minY = y;
}
if (y > out.maxY)
{
out.maxY = y;
}
}
void AABB::join(AABB& out, const AABB& a, const AABB& b)
{
out.minX = std::min(a.minX, b.minX);
out.minY = std::min(a.minY, b.minY);
out.maxX = std::max(a.maxX, b.maxX);
out.maxY = std::max(a.maxY, b.maxY);
}