Face Engine SDK  5.14.0
A face detection, recognition and tracking engine.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Rect.h
1 #pragma once
2 
3 #include <fsdk/Types/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 
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 
49  const Vector2<Type>& topLeft,
50  const Vector2<Type>& bottomRight) noexcept
51  {
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 
130  static float intersectionOverForeground(const BaseRect &rect1, const BaseRect &rect2) {
131  if (rect1.getArea() < rect2.getArea()) {
132  return intersectionOverFirstRect(rect1, rect2);
133  }
134  return intersectionOverFirstRect(rect2, rect1);
135  }
136 
141  template <typename OtherType>
142  BaseRect& operator = (const BaseRect<OtherType>& other) noexcept {
143  if(reinterpret_cast<const void*>(this) != reinterpret_cast<const void*>(&other)) {
144  x = static_cast<Type>(other.x);
145  y = static_cast<Type>(other.y);
146  width = static_cast<Type>(other.width);
147  height = static_cast<Type>(other.height);
148  }
149  return *this;
150  }
151 
156  template <typename OtherType>
157  bool operator == (const BaseRect<OtherType>& other) const noexcept {
158  return
159  x == other.x &&
160  y == other.y &&
161  width == other.width &&
162  height == other.height;
163  }
164 
169  template <typename OtherType>
170  bool operator != (const BaseRect<OtherType>& other) const noexcept {
171  return !(*this == other);
172  }
173 
178  BaseRect operator & (const BaseRect& other) const noexcept {
179  BaseRect newRect;
180  newRect.x = x > other.x ? x : other.x;
181  newRect.y = y > other.y ? y : other.y;
182  newRect.width = (x + width < other.x + other.width ? x + width : other.x + other.width) - newRect.x;
183  newRect.height = (y + height < other.y + other.height ? y + height : other.y + other.height) - newRect.y;
184  if (newRect.width <= 0 || newRect.height <= 0)
185  newRect = BaseRect();
186  return newRect;
187  }
188 
193  BaseRect operator &= (const BaseRect& other) noexcept {
194  if(this != &other)
195  *this = *this & other;
196 
197  return *this;
198  }
199 
204  BaseRect operator | (const BaseRect& other) const noexcept {
205  BaseRect newRect;
206  newRect.x = x < other.x ? x : other.x;
207  newRect.y = y < other.y ? y : other.y;
208  newRect.width = (x + width > other.x + other.width ? x + width : other.x + other.width) - newRect.x;
209  newRect.height = (y + height > other.y + other.height ? y + height : other.y + other.height) - newRect.y;
210 
211  return newRect;
212  }
213 
218  BaseRect operator |= (const BaseRect& other) noexcept {
219  if(this != &other)
220  *this = *this & other;
221 
222  return *this;
223  }
224 
229  BaseRect operator * (float scaleFactor) const noexcept {
230  BaseRect newRect;
231  newRect.x = static_cast<Type>(x * scaleFactor);
232  newRect.y = static_cast<Type>(y * scaleFactor);
233  newRect.width = static_cast<Type>(width * scaleFactor);
234  newRect.height = static_cast<Type>(height * scaleFactor);
235 
236  return newRect;
237  }
238 
243  BaseRect operator / (float scaleFactor) const noexcept {
244  BaseRect newRect;
245  newRect.x = static_cast<Type>(x / scaleFactor);
246  newRect.y = static_cast<Type>(y / scaleFactor);
247  newRect.width = static_cast<Type>(width / scaleFactor);
248  newRect.height = static_cast<Type>(height / scaleFactor);
249 
250  return newRect;
251  }
252 
254  BaseRect newRect = *this;
255  newRect.x += vec.x;
256  newRect.y += vec.y;
257 
258  return newRect;
259  }
260 
261  BaseRect operator - (Vector2<typename BaseRect::ElementType> vec) {
262  BaseRect newRect = *this;
263  newRect.x -= vec.x;
264  newRect.y -= vec.y;
265 
266  return newRect;
267  }
268 
272  Vector2<Type> size() const noexcept {
273  return Vector2<Type>(width, height);
274  }
275 
279  Vector2<Type> topLeft() const noexcept {
280  return Vector2<Type>(x, y);
281  }
282 
286  Vector2<Type> center() const noexcept {
287  return Vector2<Type>(
288  x + width / 2,
289  y + height / 2);
290  }
291 
295  Vector2<Type> bottomRight() const noexcept {
296  return Vector2<Type>(x + width, y + height);
297  }
298 
302  Type top() const noexcept { return y; }
303 
307  Type bottom() const noexcept { return y + height; }
308 
312  Type left() const noexcept { return x; }
313 
317  Type right() const noexcept { return x + width; }
318 
323  void set(const Vector2<Type>& topLeft,
324  const Vector2<Type>& bottomRight) noexcept {
325  x = topLeft.x;
326  y = topLeft.y;
327  width = bottomRight.x - x;
328  height = bottomRight.y - y;
329  }
330 
337  void adjust(Type dx, Type dy, Type dw, Type dh) noexcept {
338  x += dx;
339  y += dy;
340  width += dw;
341  height += dh;
342  }
343 
351  BaseRect adjusted(Type dx, Type dy, Type dw, Type dh) const noexcept {
352  BaseRect rect(*this);
353  rect.adjust(dx, dy, dw, dh);
354  return rect;
355  }
356 
360  Type getArea() const noexcept { return width * height; }
361 
366  bool inside(const BaseRect& other) const noexcept {
367  return
368  topLeft() >= other.topLeft() &&
369  bottomRight() <= other.bottomRight();
370  }
371 
372 
378  bool isValid() const noexcept {
379  return width > 0 && height > 0;
380  }
381  };
382 
388  template <typename Type>
389  BaseRect<Type> centerScale(const BaseRect<Type>& in, float scaleFactor) noexcept {
390  BaseRect<Type> out;
391  out.x = in.x - 0.5*(scaleFactor-1)*in.width;
392  out.y = in.y - 0.5*(scaleFactor-1)*in.height;
393  out.width = in.width * scaleFactor;
394  out.height = in.height * scaleFactor;
395  return out;
396  }
397 
398  using Rect = BaseRect<int>;
399  using FloatRect = BaseRect<float>;
400 }
void setBottom(Type b) noexcept
Set rect bottom.
Definition: Rect.h:91
Type height
Rectangle height.
Definition: Rect.h:16
BaseRect() noexcept
Initializes a default invalid rectangle.
Definition: Rect.h:20
Vector2< Type > size() const noexcept
Gets rect size (width, height).
Definition: Rect.h:272
bool inside(const BaseRect &other) const noexcept
Checks whether this rect is inside of another rect.
Definition: Rect.h:366
static BaseRect coords(Type x0, Type y0, Type x1, Type y1) noexcept
Create new Rect by coordinates.
Definition: Rect.h:101
static float intersectionOverForeground(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over foreground.
Definition: Rect.h:130
Vector2< Type > center() const noexcept
Gets rect center coordinates.
Definition: Rect.h:286
Type left() const noexcept
Gets rect left x coordinate.
Definition: Rect.h:312
BaseRect & operator=(const BaseRect< OtherType > &other) noexcept
Copies another rect.
Definition: Rect.h:142
Type width
Rectangle width.
Definition: Rect.h:15
Type getArea() const noexcept
Computes rect area (width x height).
Definition: Rect.h:360
void setLeft(Type l) noexcept
Set rect left.
Definition: Rect.h:69
T x
x coordinate.
Definition: Vector2.h:12
BaseRect(Type x_, Type y_, Type w_, Type h_) noexcept
Initializes a rectangle with given values.
Definition: Rect.h:33
bool isValid() const noexcept
Checks whether a rect is valid.
Definition: Rect.h:378
void adjust(Type dx, Type dy, Type dw, Type dh) noexcept
Adjusts the rect by given amounts.
Definition: Rect.h:337
BaseRect operator|=(const BaseRect &other) noexcept
Returns minimum area rectangle containing both rects.
Definition: Rect.h:218
BaseRect(const BaseRect &other) noexcept
Copies another rect.
Definition: Rect.h:58
BaseRect operator*(float scaleFactor) const noexcept
Multiplicates Rect scale by specified scale factor.
Definition: Rect.h:229
BaseRect(const Vector2< Type > &topLeft, const Vector2< Type > &bottomRight) noexcept
Initializes a rectangle with given values.
Definition: Rect.h:48
Vector2< Type > topLeft() const noexcept
Gets rect top-left corner coordinates.
Definition: Rect.h:279
bool operator!=(const BaseRect< OtherType > &other) const noexcept
Checks whether two rects are not equal.
Definition: Rect.h:170
Type bottom() const noexcept
Gets rect bottom y coordinate.
Definition: Rect.h:307
static float intersectionOverFirstRect(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over first rect.
Definition: Rect.h:110
BaseRect operator|(const BaseRect &other) const noexcept
Returns minimum area rectangle containing both rects.
Definition: Rect.h:204
void setRight(Type r) noexcept
Set rect right.
Definition: Rect.h:85
bool operator==(const BaseRect< OtherType > &other) const noexcept
Checks whether two rects are equal.
Definition: Rect.h:157
void setTop(Type t) noexcept
Set rect top.
Definition: Rect.h:77
static float intersectionOverUnion(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over union.
Definition: Rect.h:120
BaseRect< Type > centerScale(const BaseRect< Type > &in, float scaleFactor) noexcept
scale rect out of center
Definition: Rect.h:389
BaseRect operator/(float scaleFactor) const noexcept
Divides Rect scale by specified scale factor.
Definition: Rect.h:243
BaseRect adjusted(Type dx, Type dy, Type dw, Type dh) const noexcept
Copies and adjusts the rect by given amounts.
Definition: Rect.h:351
Vector2< Type > bottomRight() const noexcept
Gets rect bottom-right corner coordinates.
Definition: Rect.h:295
BaseRect operator&=(const BaseRect &other) noexcept
Returns rect that is intersection of rects.
Definition: Rect.h:193
BaseRect operator&(const BaseRect &other) const noexcept
Returns rect that is intersection of rects.
Definition: Rect.h:178
Rectangle.
Definition: Rect.h:10
Type top() const noexcept
Gets rect top y coordinate.
Definition: Rect.h:302
T y
y coordinate.
Definition: Vector2.h:13
void set(const Vector2< Type > &topLeft, const Vector2< Type > &bottomRight) noexcept
Sets rect corner coordinates.
Definition: Rect.h:323
Type right() const noexcept
Gets rect right x coordinate.
Definition: Rect.h:317
Type y
Upper left corner y-coordinate.
Definition: Rect.h:14
Generic 2D vector.
Definition: Vector2.h:11
Type x
Upper left corner x-coordinate.
Definition: Rect.h:13