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

Rect.h
1 #pragma once
2
3 #include "Vector2.h"
4
5 namespace fsdk
6 {
9 template <typename Type>
10 struct BaseRect {
11 typedef Type ElementType;
12
13 Type x;
14 Type y;
15 Type width;
16 Type height;
17
20 BaseRect() noexcept
21 : x(0)
22 , y(0)
23 , width(0)
24 , height(0)
25 {}
26
33 BaseRect(
34 Type x_,
35 Type y_,
36 Type w_,
37 Type h_) noexcept
38 : x(x_)
39 , y(y_)
40 , width(w_)
41 , height(h_)
42 {}
43
48 BaseRect(
49 const Vector2<Type>& topLeft,
50 const Vector2<Type>& bottomRight) noexcept
51 {
52 set(topLeft, bottomRight);
53 }
54
58 BaseRect(const BaseRect& other) noexcept {
59 *this = other;
60 }
61
62 template <typename OtherType>
63 BaseRect(const BaseRect<OtherType>& other) noexcept {
64 *this = other;
65 }
66
69 void setLeft(Type l) noexcept {
70 const auto r = right();
71 x = l;
72 setRight(r);
73 }
74
77 void setTop(Type t) noexcept {
78 const auto b = bottom();
79 y = t;
80 setBottom(b);
81 }
82
85 void setRight(Type r) noexcept {
86 width = r - x;
87 }
88
91 void setBottom(Type b) noexcept {
92 height = b - y;
93 }
94
101 static BaseRect coords(Type x0, Type y0, Type x1, Type y1) noexcept {
102 return BaseRect(x0, y0, x1-x0, y1-y0);
103 }
104
110 static float intersectionOverFirstRect(const BaseRect &rect1, const BaseRect &rect2) {
111 const float inter = (rect1 & rect2).getArea();
112 return inter / rect1.getArea();
113 }
114
120 static float intersectionOverUnion(const BaseRect& rect1, const BaseRect& rect2) {
121 const float inter = (rect1 & rect2).getArea();
122 return inter / (rect1.getArea() + rect2.getArea() - inter);
123 }
124
129 template <typename OtherType>
130 BaseRect& operator = (const BaseRect<OtherType>& other) noexcept {
131 if(reinterpret_cast<const void*>(this) != reinterpret_cast<const void*>(&other)) {
132 x = static_cast<Type>(other.x);
133 y = static_cast<Type>(other.y);
134 width = static_cast<Type>(other.width);
135 height = static_cast<Type>(other.height);
136 }
137 return *this;
138 }
139
144 template <typename OtherType>
145 bool operator == (const BaseRect<OtherType>& other) const noexcept {
146 return
147 x == other.x &&
148 y == other.y &&
149 width == other.width &&
150 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 = (y + height < other.y + other.height ? y + height : other.y + other.height) - newRect.y;
172 if (newRect.width <= 0 || newRect.height <= 0)
173 newRect = BaseRect();
174 return newRect;
175 }
176
181 BaseRect operator &= (const BaseRect& other) noexcept {
182 if(this != &other)
183 *this = *this & other;
184
185 return *this;
186 }
187
192 BaseRect operator | (const BaseRect& other) const noexcept {
193 BaseRect newRect;
194 newRect.x = x < other.x ? x : other.x;
195 newRect.y = y < other.y ? y : other.y;
196 newRect.width = (x + width > other.x + other.width ? x + width : other.x + other.width) - newRect.x;
197 newRect.height = (y + height > other.y + other.height ? y + height : other.y + other.height) - newRect.y;
198
199 return newRect;
200 }
201
206 BaseRect operator |= (const BaseRect& other) noexcept {
207 if(this != &other)
208 *this = *this & other;
209
210 return *this;
211 }
212
217 BaseRect operator * (float scaleFactor) const noexcept {
218 BaseRect newRect;
219 newRect.x = static_cast<Type>(x * scaleFactor);
220 newRect.y = static_cast<Type>(y * scaleFactor);
221 newRect.width = static_cast<Type>(width * scaleFactor);
222 newRect.height = static_cast<Type>(height * scaleFactor);
223
224 return newRect;
225 }
226
231 BaseRect operator / (float scaleFactor) const noexcept {
232 BaseRect newRect;
233 newRect.x = static_cast<Type>(x / scaleFactor);
234 newRect.y = static_cast<Type>(y / scaleFactor);
235 newRect.width = static_cast<Type>(width / scaleFactor);
236 newRect.height = static_cast<Type>(height / scaleFactor);
237
238 return newRect;
239 }
240
241 BaseRect operator + (Vector2<typename BaseRect::ElementType> vec) {
242 BaseRect newRect = *this;
243 newRect.x += vec.x;
244 newRect.y += vec.y;
245
246 return newRect;
247 }
248
249 BaseRect operator - (Vector2<typename BaseRect::ElementType> vec) {
250 BaseRect newRect = *this;
251 newRect.x -= vec.x;
252 newRect.y -= vec.y;
253
254 return newRect;
255 }
256
260 Vector2<Type> size() const noexcept {
261 return Vector2<Type>(width, height);
262 }
263
267 Vector2<Type> topLeft() const noexcept {
268 return Vector2<Type>(x, y);
269 }
270
274 Vector2<Type> center() const noexcept {
275 return Vector2<Type>(
276 x + width / 2,
277 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 { return y; }
291
295 Type bottom() const noexcept { return y + height; }
296
300 Type left() const noexcept { return x; }
301
305 Type right() const noexcept { return x + width; }
306
311 void set(const Vector2<Type>& topLeft,
312 const Vector2<Type>& bottomRight) noexcept {
313 x = topLeft.x;
314 y = topLeft.y;
315 width = bottomRight.x - x;
316 height = bottomRight.y - y;
317 }
318
325 void adjust(Type dx, Type dy, Type dw, Type dh) noexcept {
326 x += dx;
327 y += dy;
328 width += dw;
329 height += dh;
330 }
331
339 BaseRect adjusted(Type dx, Type dy, Type dw, Type dh) const noexcept {
340 BaseRect rect(*this);
341 rect.adjust(dx, dy, dw, dh);
342 return rect;
343 }
344
348 Type getArea() const noexcept { return width * height; }
349
354 bool inside(const BaseRect& other) const noexcept {
355 return
356 topLeft() >= other.topLeft() &&
357 bottomRight() <= other.bottomRight();
358 }
359
360
366 bool isValid() const noexcept {
367 return width > 0 && height > 0;
368 }
369 };
370
376 template <typename Type>
377 BaseRect<Type> centerScale(const BaseRect<Type>& in, float scaleFactor) noexcept {
378 BaseRect<Type> out;
379 out.x = in.x - 0.5*(scaleFactor-1)*in.width;
380 out.y = in.y - 0.5*(scaleFactor-1)*in.height;
381 out.width = in.width * scaleFactor;
382 out.height = in.height * scaleFactor;
383 return out;
384 }
385
386 using Rect = BaseRect<int>;
387 using FloatRect = BaseRect<float>;
388 }
fsdk::BaseRect::intersectionOverUnion
static float intersectionOverUnion(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over union.
Definition: Rect.h:120
fsdk::BaseRect::BaseRect
BaseRect(const BaseRect &other) noexcept
Copies another rect.
Definition: Rect.h:58
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:339
fsdk::BaseRect::operator*
BaseRect operator*(float scaleFactor) const noexcept
Multiplicates Rect scale by specified scale factor.
Definition: Rect.h:217
fsdk::BaseRect::operator=
BaseRect & operator=(const BaseRect< OtherType > &other) noexcept
Copies another rect.
Definition: Rect.h:130
fsdk::BaseRect::operator&=
BaseRect operator&=(const BaseRect &other) noexcept
Returns rect that is intersection of rects.
Definition: Rect.h:181
fsdk::BaseRect::size
Vector2< Type > size() const noexcept
Gets rect size (width, height).
Definition: Rect.h:260
fsdk::BaseRect::width
Type width
Rectangle width.
Definition: Rect.h:15
fsdk::BaseRect::left
Type left() const noexcept
Gets rect left x coordinate.
Definition: Rect.h:300
fsdk::BaseRect::y
Type y
Upper left corner y-coordinate.
Definition: Rect.h:14
fsdk::BaseRect::setTop
void setTop(Type t) noexcept
Set rect top.
Definition: Rect.h:77
fsdk::Vector2
Generic 2D vector.
Definition: Vector2.h:11
fsdk::centerScale
BaseRect< Type > centerScale(const BaseRect< Type > &in, float scaleFactor) noexcept
scale rect out of center
Definition: Rect.h:377
fsdk::BaseRect::operator|=
BaseRect operator|=(const BaseRect &other) noexcept
Returns minimum area rectangle containing both rects.
Definition: Rect.h:206
fsdk::BaseRect::operator==
bool operator==(const BaseRect< OtherType > &other) const noexcept
Checks whether two rects are equal.
Definition: Rect.h:145
fsdk::BaseRect::BaseRect
BaseRect(const Vector2< Type > &topLeft, const Vector2< Type > &bottomRight) noexcept
Initializes a rectangle with given values.
Definition: Rect.h:48
fsdk
SDK namespace.
Definition: IAGSEstimator.h:8
fsdk::BaseRect
Rectangle.
Definition: Rect.h:10
fsdk::BaseRect::bottomRight
Vector2< Type > bottomRight() const noexcept
Gets rect bottom-right corner coordinates.
Definition: Rect.h:283
fsdk::BaseRect::operator|
BaseRect operator|(const BaseRect &other) const noexcept
Returns minimum area rectangle containing both rects.
Definition: Rect.h:192
fsdk::BaseRect::coords
static BaseRect coords(Type x0, Type y0, Type x1, Type y1) noexcept
Create new Rect by coordinates.
Definition: Rect.h:101
fsdk::BaseRect::right
Type right() const noexcept
Gets rect right x coordinate.
Definition: Rect.h:305
fsdk::Vector2::x
T x
x coordinate.
Definition: Vector2.h:16
fsdk::BaseRect::setBottom
void setBottom(Type b) noexcept
Set rect bottom.
Definition: Rect.h:91
fsdk::BaseRect::top
Type top() const noexcept
Gets rect top y coordinate.
Definition: Rect.h:290
fsdk::BaseRect::BaseRect
BaseRect() noexcept
Initializes a default invalid rectangle.
Definition: Rect.h:20
fsdk::BaseRect::inside
bool inside(const BaseRect &other) const noexcept
Checks whether this rect is inside of another rect.
Definition: Rect.h:354
fsdk::Vector2::y
T y
y coordinate.
Definition: Vector2.h:17
fsdk::BaseRect::BaseRect
BaseRect(Type x_, Type y_, Type w_, Type h_) noexcept
Initializes a rectangle with given values.
Definition: Rect.h:33
fsdk::BaseRect::isValid
bool isValid() const noexcept
Checks whether a rect is valid.
Definition: Rect.h:366
fsdk::BaseRect::x
Type x
Upper left corner x-coordinate.
Definition: Rect.h:13
fsdk::BaseRect::center
Vector2< Type > center() const noexcept
Gets rect center coordinates.
Definition: Rect.h:274
fsdk::BaseRect::adjust
void adjust(Type dx, Type dy, Type dw, Type dh) noexcept
Adjusts the rect by given amounts.
Definition: Rect.h:325
fsdk::BaseRect::operator&
BaseRect operator&(const BaseRect &other) const noexcept
Returns rect that is intersection of rects.
Definition: Rect.h:166
fsdk::BaseRect::getArea
Type getArea() const noexcept
Computes rect area (width x height).
Definition: Rect.h:348
fsdk::BaseRect::topLeft
Vector2< Type > topLeft() const noexcept
Gets rect top-left corner coordinates.
Definition: Rect.h:267
fsdk::BaseRect::operator/
BaseRect operator/(float scaleFactor) const noexcept
Divides Rect scale by specified scale factor.
Definition: Rect.h:231
fsdk::BaseRect::bottom
Type bottom() const noexcept
Gets rect bottom y coordinate.
Definition: Rect.h:295
fsdk::BaseRect::intersectionOverFirstRect
static float intersectionOverFirstRect(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over first rect.
Definition: Rect.h:110
fsdk::BaseRect::setRight
void setRight(Type r) noexcept
Set rect right.
Definition: Rect.h:85
fsdk::BaseRect::setLeft
void setLeft(Type l) noexcept
Set rect left.
Definition: Rect.h:69
fsdk::BaseRect::height
Type height
Rectangle height.
Definition: Rect.h:16
fsdk::BaseRect::set
void set(const Vector2< Type > &topLeft, const Vector2< Type > &bottomRight) noexcept
Sets rect corner coordinates.
Definition: Rect.h:311
fsdk::BaseRect::operator!=
bool operator!=(const BaseRect< OtherType > &other) const noexcept
Checks whether two rects are not equal.
Definition: Rect.h:158