| Face Engine SDK 5.32.0 A face detection, recognition and tracking engine.

Rect.h
1 #pragma once
2
3 #include <fsdk/Types/Vector2.h>
4
5 namespace fsdk {
8 template <typename Type>
9 struct BaseRect {
10 typedef Type ElementType;
11
12 Type x;
13 Type y;
14 Type width;
15 Type height;
16
19 BaseRect() noexcept
20 : x(0)
21 , y(0)
22 , width(0)
23 , height(0) {
24 }
25
32 BaseRect(Type x_, Type y_, Type w_, Type h_) noexcept
33 : x(x_)
34 , y(y_)
35 , width(w_)
36 , height(h_) {
37 }
38
43 BaseRect(const Vector2<Type>& topLeft, const Vector2<Type>& bottomRight) noexcept {
44 set(topLeft, bottomRight);
45 }
46
50 BaseRect(const BaseRect& other) noexcept {
51 *this = other;
52 }
53
54 template <typename OtherType>
55 BaseRect(const BaseRect<OtherType>& other) noexcept {
56 *this = other;
57 }
58
61 void setLeft(Type l) noexcept {
62 const auto r = right();
63 x = l;
64 setRight(r);
65 }
66
69 void setTop(Type t) noexcept {
70 const auto b = bottom();
71 y = t;
72 setBottom(b);
73 }
74
77 void setRight(Type r) noexcept {
78 width = r - x;
79 }
80
83 void setBottom(Type b) noexcept {
84 height = b - y;
85 }
86
93 static BaseRect coords(Type x0, Type y0, Type x1, Type y1) noexcept {
94 return BaseRect(x0, y0, x1 - x0, y1 - y0);
95 }
96
102 static float intersectionOverFirstRect(const BaseRect& rect1, const BaseRect& rect2) {
103 const float inter = (rect1 & rect2).getArea();
104 return inter / rect1.getArea();
105 }
106
112 static float intersectionOverUnion(const BaseRect& rect1, const BaseRect& rect2) {
113 const float inter = (rect1 & rect2).getArea();
114 return inter / (rect1.getArea() + rect2.getArea() - inter);
115 }
116
122 static float intersectionOverForeground(const BaseRect& rect1, const BaseRect& rect2) {
123 if(rect1.getArea() < rect2.getArea()) {
124 return intersectionOverFirstRect(rect1, rect2);
125 }
126 return intersectionOverFirstRect(rect2, rect1);
127 }
128
133 template <typename OtherType>
134 BaseRect& operator=(const BaseRect<OtherType>& other) noexcept {
135 if(reinterpret_cast<const void*>(this) != reinterpret_cast<const void*>(&other)) {
136 x = static_cast<Type>(other.x);
137 y = static_cast<Type>(other.y);
138 width = static_cast<Type>(other.width);
139 height = static_cast<Type>(other.height);
140 }
141 return *this;
142 }
143
148 template <typename OtherType>
149 bool operator==(const BaseRect<OtherType>& other) const noexcept {
150 return x == other.x && y == other.y && width == other.width && height == other.height;
151 }
152
157 template <typename OtherType>
158 bool operator!=(const BaseRect<OtherType>& other) const noexcept {
159 return !(*this == other);
160 }
161
166 BaseRect operator&(const BaseRect& other) const noexcept {
167 BaseRect newRect;
168 newRect.x = x > other.x ? x : other.x;
169 newRect.y = y > other.y ? y : other.y;
170 newRect.width = (x + width < other.x + other.width ? x + width : other.x + other.width) - newRect.x;
171 newRect.height =
172 (y + height < other.y + other.height ? y + height : other.y + other.height) - newRect.y;
173 if(newRect.width <= 0 || newRect.height <= 0)
174 newRect = BaseRect();
175 return newRect;
176 }
177
182 BaseRect operator&=(const BaseRect& other) noexcept {
183 if(this != &other)
184 *this = *this & other;
185
186 return *this;
187 }
188
193 BaseRect operator|(const BaseRect& other) const noexcept {
194 BaseRect newRect;
195 newRect.x = x < other.x ? x : other.x;
196 newRect.y = y < other.y ? y : other.y;
197 newRect.width = (x + width > other.x + other.width ? x + width : other.x + other.width) - newRect.x;
198 newRect.height =
199 (y + height > other.y + other.height ? y + height : other.y + other.height) - newRect.y;
200
201 return newRect;
202 }
203
208 BaseRect operator|=(const BaseRect& other) noexcept {
209 if(this != &other)
210 *this = *this & other;
211
212 return *this;
213 }
214
219 BaseRect operator*(float scaleFactor) const noexcept {
220 BaseRect newRect;
221 newRect.x = static_cast<Type>(x * scaleFactor);
222 newRect.y = static_cast<Type>(y * scaleFactor);
223 newRect.width = static_cast<Type>(width * scaleFactor);
224 newRect.height = static_cast<Type>(height * scaleFactor);
225
226 return newRect;
227 }
228
233 BaseRect operator/(float scaleFactor) const noexcept {
234 BaseRect newRect;
235 newRect.x = static_cast<Type>(x / scaleFactor);
236 newRect.y = static_cast<Type>(y / scaleFactor);
237 newRect.width = static_cast<Type>(width / scaleFactor);
238 newRect.height = static_cast<Type>(height / scaleFactor);
239
240 return newRect;
241 }
242
243 BaseRect operator+(Vector2<typename BaseRect::ElementType> vec) {
244 BaseRect newRect = *this;
245 newRect.x += vec.x;
246 newRect.y += vec.y;
247
248 return newRect;
249 }
250
251 BaseRect operator-(Vector2<typename BaseRect::ElementType> vec) {
252 BaseRect newRect = *this;
253 newRect.x -= vec.x;
254 newRect.y -= vec.y;
255
256 return newRect;
257 }
258
262 Vector2<Type> size() const noexcept {
263 return Vector2<Type>(width, height);
264 }
265
269 Vector2<Type> topLeft() const noexcept {
270 return Vector2<Type>(x, y);
271 }
272
276 Vector2<Type> center() const noexcept {
277 return Vector2<Type>(x + width / 2, y + height / 2);
278 }
279
283 Vector2<Type> bottomRight() const noexcept {
284 return Vector2<Type>(x + width, y + height);
285 }
286
290 Type top() const noexcept {
291 return y;
292 }
293
297 Type bottom() const noexcept {
298 return y + height;
299 }
300
304 Type left() const noexcept {
305 return x;
306 }
307
311 Type right() const noexcept {
312 return x + width;
313 }
314
319 void set(const Vector2<Type>& topLeft, const Vector2<Type>& bottomRight) noexcept {
320 x = topLeft.x;
321 y = topLeft.y;
322 width = bottomRight.x - x;
323 height = bottomRight.y - y;
324 }
325
332 void adjust(Type dx, Type dy, Type dw, Type dh) noexcept {
333 x += dx;
334 y += dy;
335 width += dw;
336 height += dh;
337 }
338
346 BaseRect adjusted(Type dx, Type dy, Type dw, Type dh) const noexcept {
347 BaseRect rect(*this);
348 rect.adjust(dx, dy, dw, dh);
349 return rect;
350 }
351
355 Type getArea() const noexcept {
356 return width * height;
357 }
358
363 bool inside(const BaseRect& other) const noexcept {
364 return topLeft() >= other.topLeft() && bottomRight() <= other.bottomRight();
365 }
366
372 bool isValid() const noexcept {
373 return width > 0 && height > 0;
374 }
375 };
376
382 template <typename Type>
383 BaseRect<Type> centerScale(const BaseRect<Type>& in, float scaleFactor) noexcept {
384 BaseRect<Type> out;
385 out.x = in.x - 0.5 * (scaleFactor - 1) * in.width;
386 out.y = in.y - 0.5 * (scaleFactor - 1) * in.height;
387 out.width = in.width * scaleFactor;
388 out.height = in.height * scaleFactor;
389 return out;
390 }
391
392 using Rect = BaseRect<int>;
393 using FloatRect = BaseRect<float>;
394 } // namespace fsdk
fsdk
SDK namespace.
Definition: IAGSEstimator.h:8
fsdk::BaseRect::setBottom
void setBottom(Type b) noexcept
Set rect bottom.
Definition: Rect.h:83
fsdk::BaseRect::height
Type height
Rectangle height.
Definition: Rect.h:15
fsdk::BaseRect::BaseRect
BaseRect() noexcept
Initializes a default invalid rectangle.
Definition: Rect.h:19
fsdk::BaseRect::operator &=
BaseRect operator &=(const BaseRect &other) noexcept
Returns rect that is intersection of rects.
Definition: Rect.h:182
fsdk::BaseRect::size
Vector2< Type > size() const noexcept
Gets rect size (width, height).
Definition: Rect.h:262
fsdk::BaseRect::inside
bool inside(const BaseRect &other) const noexcept
Checks whether this rect is inside of another rect.
Definition: Rect.h:363
fsdk::BaseRect::coords
static BaseRect coords(Type x0, Type y0, Type x1, Type y1) noexcept
Create new Rect by coordinates.
Definition: Rect.h:93
fsdk::BaseRect::intersectionOverForeground
static float intersectionOverForeground(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over foreground.
Definition: Rect.h:122
fsdk::BaseRect::center
Vector2< Type > center() const noexcept
Gets rect center coordinates.
Definition: Rect.h:276
fsdk::BaseRect::left
Type left() const noexcept
Gets rect left x coordinate.
Definition: Rect.h:304
fsdk::BaseRect::operator=
BaseRect & operator=(const BaseRect< OtherType > &other) noexcept
Copies another rect.
Definition: Rect.h:134
fsdk::BaseRect::width
Type width
Rectangle width.
Definition: Rect.h:14
fsdk::BaseRect::getArea
Type getArea() const noexcept
Computes rect area (width x height).
Definition: Rect.h:355
fsdk::BaseRect::setLeft
void setLeft(Type l) noexcept
Set rect left.
Definition: Rect.h:61
fsdk::Vector2::x
T x
x coordinate.
Definition: Vector2.h:11
fsdk::BaseRect::BaseRect
BaseRect(Type x_, Type y_, Type w_, Type h_) noexcept
Initializes a rectangle with given values.
Definition: Rect.h:32
fsdk::BaseRect::isValid
bool isValid() const noexcept
Checks whether a rect is valid.
Definition: Rect.h:372
fsdk::BaseRect::adjust
void adjust(Type dx, Type dy, Type dw, Type dh) noexcept
Adjusts the rect by given amounts.
Definition: Rect.h:332
fsdk::BaseRect::operator|=
BaseRect operator|=(const BaseRect &other) noexcept
Returns minimum area rectangle containing both rects.
Definition: Rect.h:208
fsdk::BaseRect::BaseRect
BaseRect(const BaseRect &other) noexcept
Copies another rect.
Definition: Rect.h:50
fsdk::BaseRect::operator*
BaseRect operator*(float scaleFactor) const noexcept
Multiplicates Rect scale by specified scale factor.
Definition: Rect.h:219
fsdk::BaseRect::BaseRect
BaseRect(const Vector2< Type > &topLeft, const Vector2< Type > &bottomRight) noexcept
Initializes a rectangle with given values.
Definition: Rect.h:43
fsdk::BaseRect::topLeft
Vector2< Type > topLeft() const noexcept
Gets rect top-left corner coordinates.
Definition: Rect.h:269
fsdk::BaseRect::operator!=
bool operator!=(const BaseRect< OtherType > &other) const noexcept
Checks whether two rects are not equal.
Definition: Rect.h:158
fsdk::BaseRect::bottom
Type bottom() const noexcept
Gets rect bottom y coordinate.
Definition: Rect.h:297
fsdk::BaseRect::intersectionOverFirstRect
static float intersectionOverFirstRect(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over first rect.
Definition: Rect.h:102
fsdk::BaseRect::operator|
BaseRect operator|(const BaseRect &other) const noexcept
Returns minimum area rectangle containing both rects.
Definition: Rect.h:193
fsdk::BaseRect::operator &
BaseRect operator &(const BaseRect &other) const noexcept
Returns rect that is intersection of rects.
Definition: Rect.h:166
fsdk::BaseRect::setRight
void setRight(Type r) noexcept
Set rect right.
Definition: Rect.h:77
fsdk::BaseRect::operator==
bool operator==(const BaseRect< OtherType > &other) const noexcept
Checks whether two rects are equal.
Definition: Rect.h:149
fsdk::BaseRect::setTop
void setTop(Type t) noexcept
Set rect top.
Definition: Rect.h:69
fsdk::BaseRect::intersectionOverUnion
static float intersectionOverUnion(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over union.
Definition: Rect.h:112
fsdk::centerScale
BaseRect< Type > centerScale(const BaseRect< Type > &in, float scaleFactor) noexcept
scale rect out of center
Definition: Rect.h:383
fsdk::BaseRect::operator/
BaseRect operator/(float scaleFactor) const noexcept
Divides Rect scale by specified scale factor.
Definition: Rect.h:233
fsdk::BaseRect::adjusted
BaseRect adjusted(Type dx, Type dy, Type dw, Type dh) const noexcept
Copies and adjusts the rect by given amounts.
Definition: Rect.h:346
fsdk::BaseRect::bottomRight
Vector2< Type > bottomRight() const noexcept
Gets rect bottom-right corner coordinates.
Definition: Rect.h:283
fsdk::BaseRect
Rectangle.
Definition: Rect.h:9
fsdk::BaseRect::top
Type top() const noexcept
Gets rect top y coordinate.
Definition: Rect.h:290
fsdk::Vector2::y
T y
y coordinate.
Definition: Vector2.h:12
fsdk::BaseRect::right
Type right() const noexcept
Gets rect right x coordinate.
Definition: Rect.h:311
fsdk::BaseRect::y
Type y
Upper left corner y-coordinate.
Definition: Rect.h:13
fsdk::Vector2
Generic 2D vector.
Definition: Vector2.h:10
fsdk::BaseRect::x
Type x
Upper left corner x-coordinate.
Definition: Rect.h:12