Banuba SDK
pixel_rect.hpp
1 #pragma once
2 
4 
5 #include <algorithm>
6 
7 namespace bnb
8 {
9  /**
10  * @addtogroup Types
11  * @{
12  */
13 
14  struct pixel_rect
15  {
16  int32_t x = 0, y = 0;
17  int32_t w = 0, h = 0;
18 
19  bool is_valid() const
20  {
21  return w > 0 && h > 0;
22  }
23 
24  void transpose()
25  {
26  std::swap(x, y);
27  std::swap(w, h);
28  }
29 
30  pixel_rect intersect(const pixel_rect& rect) const
31  {
32  auto newtlx = std::max(x, rect.x);
33  auto newtly = std::max(y, rect.y);
34  auto newbrx = std::min(x + w, rect.x + rect.w);
35  auto newbry = std::min(y + h, rect.y + rect.h);
36 
37  if (newbrx < newtlx || newbry < newtly)
38  return pixel_rect();
39 
40  return pixel_rect{newtlx, newtly, newbrx - newtlx, newbry - newtly};
41  }
42 
43  bool operator==(const pixel_rect& cmp) const noexcept
44  {
45  return w == cmp.w && h == cmp.h && x == cmp.x && y == cmp.y;
46  }
47  bool operator!=(const pixel_rect& cmp) const noexcept
48  {
49  return !(*this == cmp);
50  }
51 
52  pixel_rect(int32_t _x, int32_t _y, int32_t _w, int32_t _h)
53  : x(_x)
54  , y(_y)
55  , w(_w)
56  , h(_h)
57  {
58  }
59 
60  pixel_rect(int32_t _w, int32_t _h)
61  : x(0)
62  , y(0)
63  , w(_w)
64  , h(_h)
65  {
66  }
67 
68  pixel_rect()
69  : x(0)
70  , y(0)
71  , w(0)
72  , h(0)
73  {
74  }
75 
77  : x(rect.x)
78  , y(rect.y)
79  , w(rect.w)
80  , h(rect.h)
81  {
82  }
83 
84  interfaces::pixel_rect get_iface() const
85  {
86  return interfaces::pixel_rect{x, y, w, h};
87  }
88 
89  operator interfaces::pixel_rect() const
90  {
91  return interfaces::pixel_rect(x, y, w, h);
92  }
93  };
94 
95 } // namespace bnb
bnb::pixel_rect
Definition: pixel_rect.hpp:14
pixel_rect.hpp
bnb::interfaces::pixel_rect
Definition: pixel_rect.hpp:15