Rect.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 // libraries
4 #include <algorithm>
5 
9 
10 namespace dai {
11 
18 struct Rect {
19  // default constructor
20  Rect() = default;
21  Rect(float x, float y, float width, float height) : x(x), y(y), width(width), height(height) {}
22  Rect(const Rect& r) : x(r.x), y(r.y), width(r.width), height(r.height) {}
23  Rect(const Point2f& org, const Size2f& sz) : x(org.x), y(org.y), width(sz.width), height(sz.height) {}
24  Rect(const Point2f& pt1, const Point2f& pt2)
25  : x(std::min(pt1.x, pt2.x)), y(std::min(pt1.y, pt2.y)), width(std::max(pt1.x, pt2.x) - x), height(std::max(pt1.y, pt2.y) - y) {}
26  Rect& operator=(const Rect& r) = default;
27  Rect& operator=(Rect&& r) = default;
28 
32  Point2f topLeft() const {
33  return Point2f(x, y);
34  }
35 
39  Point2f bottomRight() const {
40  return Point2f(x + width, y + height);
41  }
42 
46  Size2f size() const {
47  return Size2f(width, height);
48  }
49 
53  float area() const {
54  return width * height;
55  }
56 
60  bool empty() const {
61  return width <= 0 || height <= 0;
62  }
63 
67  bool contains(const Point2f& pt) const {
68  return x <= pt.x && pt.x < x + width && y <= pt.y && pt.y < y + height;
69  }
70 
74  bool isNormalized() const {
75  if(x + width <= 1.f && y + height <= 1.f) return true;
76  return !(x == static_cast<int>(x) && y == static_cast<int>(y) && width == static_cast<int>(width) && height == static_cast<int>(height));
77  }
78 
84  Rect denormalize(int destWidth, int destHeight) const {
85  if(isNormalized()) {
86  return Rect(std::round(x * destWidth), std::round(y * destHeight), std::round(width * destWidth), std::round(height * destHeight));
87  }
88  return *this;
89  }
90 
96  Rect normalize(int srcWidth, int srcHeight) const {
97  if(isNormalized()) {
98  return *this;
99  }
100  return Rect(x / srcWidth, y / srcHeight, width / srcWidth, height / srcHeight);
101  }
102 
103  // order of declaration must be x, y, width, height for constructor initializer lists
104  float x = 0.0f; // x coordinate of the top-left corner
105  float y = 0.0f; // y coordinate of the top-left corner
106  float width = 0.0f; // width of the rectangle
107  float height = 0.0f; // height of the rectangle
108 };
109 DEPTHAI_SERIALIZE_EXT(Rect, x, y, width, height);
110 
111 } // namespace dai
dai::Rect::topLeft
Point2f topLeft() const
Definition: Rect.hpp:32
dai::Rect::denormalize
Rect denormalize(int destWidth, int destHeight) const
Definition: Rect.hpp:84
dai::Rect::x
float x
Definition: Rect.hpp:104
dai::Rect::normalize
Rect normalize(int srcWidth, int srcHeight) const
Definition: Rect.hpp:96
dai::DEPTHAI_SERIALIZE_EXT
DEPTHAI_SERIALIZE_EXT(CameraSensorConfig, width, height, minFps, maxFps, fov, type)
dai::Rect::empty
bool empty() const
Definition: Rect.hpp:60
dai::Rect::Rect
Rect()=default
dai::Point2f
Definition: Point2f.hpp:16
dai::Rect::size
Size2f size() const
Definition: Rect.hpp:46
dai::Rect::bottomRight
Point2f bottomRight() const
Definition: Rect.hpp:39
dai::Rect::Rect
Rect(const Point2f &org, const Size2f &sz)
Definition: Rect.hpp:23
dai::Rect::Rect
Rect(const Rect &r)
Definition: Rect.hpp:22
dai::Rect::isNormalized
bool isNormalized() const
Definition: Rect.hpp:74
dai::Size2f
Definition: Size2f.hpp:15
dai::Rect::contains
bool contains(const Point2f &pt) const
Definition: Rect.hpp:67
Serialization.hpp
Size2f.hpp
Point2f.hpp
std
Definition: Node.hpp:366
dai::Point2f::x
float x
Definition: Point2f.hpp:19
dai::Rect
Definition: Rect.hpp:18
dai::Rect::Rect
Rect(float x, float y, float width, float height)
Definition: Rect.hpp:21
dai::Rect::height
float height
Definition: Rect.hpp:107
dai::Rect::operator=
Rect & operator=(const Rect &r)=default
dai::Point2f::y
float y
Definition: Point2f.hpp:19
dai::Rect::Rect
Rect(const Point2f &pt1, const Point2f &pt2)
Definition: Rect.hpp:24
dai::Rect::width
float width
Definition: Rect.hpp:106
dai
Definition: CameraExposureOffset.hpp:6
dai::Rect::y
float y
Definition: Rect.hpp:105
dai::Rect::area
float area() const
Definition: Rect.hpp:53


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19