-
Notifications
You must be signed in to change notification settings - Fork 2
/
rect.h
196 lines (150 loc) · 5.26 KB
/
rect.h
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#ifndef GENERICS_RECT_H
#define GENERICS_RECT_H
#include "point.h"
namespace generics {
template< class coord_t, int DIMENSIONS, coord_t COORD_MIN, coord_t COORD_MAX >
struct Rect {
static constexpr coord_t MBRSIZE = 2 * DIMENSIONS;
coord_t * bounds; // array of length 2*DIMENSIONS with the semantics of a touple of bounds [a_min .. a_max]
inline Point< coord_t, DIMENSIONS > lowerBound() const {
Point< coord_t, DIMENSIONS > result;
for (int d = 0; d < DIMENSIONS; ++d)
result[d] = bounds[d * 2];
return result;
}
inline Point< coord_t, DIMENSIONS > upperBound() const {
Point< coord_t, DIMENSIONS > result;
for (int d = 0; d < DIMENSIONS; ++d)
result[d] = bounds[d * 2 + 1];
return result;
}
inline Point< coord_t, DIMENSIONS > center() const {
Point< coord_t, DIMENSIONS > result;
for (int d = 0; d < DIMENSIONS; ++d)
result[d] = (bounds[d * 2 + 1] + bounds[d * 2]) / 2;
return result;
}
inline bool overlapsRawRect(const coord_t * other) const {
for (int d = 0; d < DIMENSIONS; ++d) {
if (
(bounds[d * 2] > other[d * 2 + 1]) || // left(A) > right(B)
(bounds[d * 2 + 1] < other[d * 2]) // right(A) < left(B)
)
return false;
}
return true;
}
inline bool overlapsRawPoint(const coord_t * other) const {
for (int d = 0; d < DIMENSIONS; ++d) {
if (
(bounds[d * 2] > other[d]) || // left(A) > coord(B)
(bounds[d * 2 + 1] < other[d]) // right(A) < coord(B)
)
return false;
}
return true;
}
inline bool overlaps(const Rect & other) const {
return overlapsRawRect(other.bounds);
}
inline bool overlaps(const Point< coord_t, DIMENSIONS > & point) const {
return overlapsRawPoint(point.coords);
}
inline coord_t edgeLength(int dimension) const {
coord_t result = bounds[dimension * 2 + 1] + 1 - bounds[dimension * 2];
return result < 0 ? 0 : result;
}
inline coord_t area() const {
coord_t result = 1;
for (int d = 0; d < DIMENSIONS; ++d)
result *= edgeLength(d);
return result;
}
inline coord_t margin() const {
coord_t result = 0;
for (int d = 0; d < DIMENSIONS; ++d)
result += edgeLength(d);
return result;
}
inline void nullify() {
for (int d = 0 ; d < DIMENSIONS; ++d) {
bounds[d * 2] = COORD_MAX;
bounds[d * 2 + 1] = COORD_MIN;
}
}
inline bool isNull() {
for (int d = 0 ; d < DIMENSIONS; ++d) {
if (bounds[d * 2 + 1] < bounds[d * 2])
return true;
}
return false;
}
inline void enlarge(const Rect & other) {
for (int d = 0; d < DIMENSIONS; ++d) {
bounds[d * 2] = other.bounds[d * 2] < bounds[d * 2] ? other.bounds[d * 2] : bounds[d * 2]; // min(left(a), left(b))
bounds[d * 2 + 1] = other.bounds[d * 2 + 1] > bounds[d * 2 + 1] ? other.bounds[d * 2 + 1] : bounds[d * 2 + 1]; // max(right(a), right(b))
}
}
inline void enlarge(const Point< coord_t, DIMENSIONS > & other) {
for (int d = 0; d < DIMENSIONS; ++d) {
bounds[d * 2] = other[d] < bounds[d * 2] ? other[d] : bounds[d * 2]; // min(left(a), coord)
bounds[d * 2 + 1] = other[d] > bounds[d * 2 + 1] ? other[d] : bounds[d * 2 + 1]; // max(right(a), coord)
}
}
inline void reduce(const Rect & other) {
for (int d = 0; d < DIMENSIONS; ++d) {
bounds[d * 2] = other.bounds[d * 2] > bounds[d * 2] ? other.bounds[d * 2] : bounds[d * 2]; // max(left(a), left(b))
bounds[d * 2 + 1] = other.bounds[d * 2 + 1] < bounds[d * 2 + 1] ? other.bounds[d * 2 + 1] : bounds[d * 2 + 1]; // min(right(a), right(b))
}
}
inline static void shift(coord_t * rbounds, const Point< coord_t, DIMENSIONS > & vector) {
for (int d = 0; d < DIMENSIONS; ++d) {
rbounds[d * 2] += vector[d];
rbounds[d * 2 + 1] += vector[d];
}
}
inline static void shiftInv(coord_t * rbounds, const Point< coord_t, DIMENSIONS > & vector) {
for (int d = 0; d < DIMENSIONS; ++d) {
rbounds[d * 2] -= vector[d];
rbounds[d * 2 + 1] -= vector[d];
}
}
inline void shift(const Point< coord_t, DIMENSIONS > & vector) { shift(bounds, vector); }
inline void shiftInv(const Point< coord_t, DIMENSIONS > & vector) { shift(bounds, vector); }
inline void swap(Rect & other) {
coord_t * h = bounds;
bounds = other.bounds;
other.bounds = h;
}
/** WARNING
* Size of raw MUST be equal to MBRSIZE otherwise behavior is undefined
*/
inline void takeDataFrom(coord_t raw[]) {
delete[] bounds;
bounds = raw;
}
Rect() : bounds(new coord_t[MBRSIZE]) {}
Rect(const Rect & other) : bounds(new coord_t[MBRSIZE]) {
for (int i = 0 ; i < MBRSIZE; ++i) bounds[i] = other.bounds[i];
}
explicit Rect(const coord_t * raw) : bounds(new coord_t[MBRSIZE]) {
for (int i = 0 ; i < MBRSIZE; ++i) bounds[i] = raw[i];
}
explicit Rect(const Point< coord_t, DIMENSIONS > & lowerBound, const Point< coord_t, DIMENSIONS > & upperBound) : bounds(new coord_t[MBRSIZE]) {
for (int i = 0 ; i < DIMENSIONS; ++i) {
bounds[i * 2] = lowerBound[i];
bounds[i * 2 + 1] = upperBound[i];
}
}
~Rect() {
delete[] bounds;
}
Rect & operator=(const Rect & other) {
for (int i = 0 ; i < MBRSIZE; ++i) bounds[i] = other.bounds[i];
return *this;
}
inline coord_t & operator[](int index) { return bounds[index]; }
inline const coord_t & operator[](int index) const { return bounds[index]; }
};
}
#endif