CVB++ 15.0
rect.hpp
1#pragma once
2
3#include <cmath>
4
5#include "global.hpp"
6#include "size_2d.hpp"
7#include "point_2d.hpp"
8
9namespace Cvb
10{
11
12 CVB_BEGIN_INLINE_NS
13
15
22 template <class T, class ENABLE = void>
23 class Rect final
24 {
25
26#ifndef CVB_DOXYGEN
27 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
28 };
29
30 template <class T>
31 class Rect<T, typename EnableIfNumeric<T>::type> final
32 {
33
34#endif
35 public:
36 using ValueType = T;
37
39
45 Rect() noexcept
46 : left_(static_cast<T>(0))
47 , top_(static_cast<T>(0))
48 , right_(static_cast<T>(-1))
49 , bottom_(static_cast<T>(-1)) {};
50
52
59 Rect(T left, T top, T right, T bottom) noexcept
60 {
61 SetLeft(left);
62 SetTop(top);
63 SetRight(right);
64 SetBottom(bottom);
65 }
66
68
73 Rect(Point2D<T> location, Size2D<T> size) noexcept
74 {
75 SetLocation(location);
76 SetSize(size);
77 }
78
80
84 T Left() const noexcept
85 {
86 return left_;
87 }
88
90
94 void SetLeft(T left) noexcept
95 {
96 left_ = left;
97 }
98
100
104 T Top() const noexcept
105 {
106 return top_;
107 }
108
110
114 void SetTop(T top) noexcept
115 {
116 top_ = top;
117 }
118
120
124 T Right() const noexcept
125 {
126 return right_;
127 }
128
130
134 void SetRight(T right) noexcept
135 {
136 right_ = right;
137 }
138
140
144 T Bottom() const noexcept
145 {
146 return bottom_;
147 }
148
150
154 void SetBottom(T bottom) noexcept
155 {
156 bottom_ = bottom;
157 }
158
160
164 T Width() const noexcept
165 {
166 return right_ - left_ + static_cast<T>(1);
167 }
168
170
174 void SetWidth(T width) noexcept
175 {
176 right_ = left_ + width - static_cast<T>(1);
177 }
178
180
184 T Height() const noexcept
185 {
186 return bottom_ - top_ + static_cast<T>(1);
187 }
188
190
194 void SetHeight(T height) noexcept
195 {
196 bottom_ = top_ + height - static_cast<T>(1);
197 }
198
200
204 Size2D<T> Size() const noexcept
205 {
206 return Size2D<T>(Width(), Height());
207 }
208
210
214 void SetSize(Size2D<T> size) noexcept
215 {
216 SetWidth(size.Width());
217 SetHeight(size.Height());
218 }
219
221
225 Point2D<T> Location() const noexcept
226 {
227 return Point2D<T>(left_, top_);
228 }
229
231
235 void SetLocation(Point2D<T> location) noexcept
236 {
237 right_ = location.X() + Width();
238 bottom_ = location.Y() + Height();
239 left_ = location.X();
240 top_ = location.Y();
241 }
242
244
249 bool Contains(Point2D<T> point) const noexcept
250 {
251 return (point.X() >= Left()) && (point.Y() >= Top()) && (point.X() <= Right()) && (point.Y() <= Bottom());
252 }
253
256
264
266
271 bool operator==(const Rect<T> &rect) const noexcept
272 {
273 return (top_ == rect.top_ && left_ == rect.left_ && bottom_ == rect.bottom_ && right_ == rect.right_);
274 }
275
277
282 bool operator!=(const Rect<T> &rect) const noexcept
283 {
284 return !(*this == rect);
285 }
286
288 template <class C>
289 explicit operator Rect<C, typename std::enable_if<std::is_integral<C>::value>::type>() const noexcept
290 {
291 return Rect<C>(static_cast<C>(Left()), static_cast<C>(Top()), static_cast<C>(Right()), static_cast<C>(Bottom()));
292 }
293
294 template <class C>
295 explicit operator Rect<C, typename std::enable_if<!std::is_integral<C>::value>::type>() const noexcept
296 {
297 return Rect<C>(static_cast<C>(Left()), static_cast<C>(Top()), static_cast<C>(Right()), static_cast<C>(Bottom()));
298 }
299
300 private:
301 T left_;
302 T top_;
303 T right_;
304 T bottom_;
305 };
306
308
315 template <class T>
316 inline Rect<int> Round(const Rect<T> &rhs) noexcept
317 {
318 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
319 return Rect<int>(static_cast<int>(std::round(rhs.Left())), static_cast<int>(std::round(rhs.Top())),
320 static_cast<int>(std::round(rhs.Right())), static_cast<int>(std::round(rhs.Bottom())));
321 }
322
323 CVB_END_INLINE_NS
324
325} // namespace Cvb
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
Rectangle object.
Definition rect.hpp:24
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition rect.hpp:144
bool Contains(Point2D< T > point) const noexcept
Checks if this rectangle contains a point.
Definition rect.hpp:249
T Height() const noexcept
Gets the height of the rectangle.
Definition rect.hpp:184
void SetRight(T right) noexcept
Sets rightmost column of the rectangle (still inside the rectangle).
Definition rect.hpp:134
void SetWidth(T width) noexcept
Sets the width of the rectangle.
Definition rect.hpp:174
Rect() noexcept
Default rectangle in the integral type system.
Definition rect.hpp:45
void SetBottom(T bottom) noexcept
Sets bottom row of the rectangle (still inside the rectangle).
Definition rect.hpp:154
bool operator!=(const Rect< T > &rect) const noexcept
Compares to another rectangle.
Definition rect.hpp:282
Rect(T left, T top, T right, T bottom) noexcept
Constructor for a rectangle.
Definition rect.hpp:59
CoordinateSystemType CoordinateSystem() const noexcept
Indicates the coordinate system in which this object is being measured (when used as an area of inter...
Definition rect.hpp:260
Size2D< T > Size() const noexcept
Gets the size of the rectangle.
Definition rect.hpp:204
void SetLeft(T left) noexcept
Sets first column of the rectangle.
Definition rect.hpp:94
void SetLocation(Point2D< T > location) noexcept
Sets the location of the top left corner of the rectangle.
Definition rect.hpp:235
T Top() const noexcept
Gets first row of the rectangle.
Definition rect.hpp:104
bool operator==(const Rect< T > &rect) const noexcept
Compares to another rectangle.
Definition rect.hpp:271
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition rect.hpp:124
T Left() const noexcept
Gets first column of the rectangle.
Definition rect.hpp:84
void SetHeight(T height) noexcept
Sets the height of the rectangle.
Definition rect.hpp:194
void SetTop(T top) noexcept
Sets first row of the rectangle.
Definition rect.hpp:114
void SetSize(Size2D< T > size) noexcept
Gets the size of the rectangle.
Definition rect.hpp:214
Rect(Point2D< T > location, Size2D< T > size) noexcept
Constructor for a rectangle.
Definition rect.hpp:73
T Width() const noexcept
Gets the width of the rectangle.
Definition rect.hpp:164
Point2D< T > Location() const noexcept
Gets the location of the top left corner of the rectangle.
Definition rect.hpp:225
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
CoordinateSystemType
Enumeration of the different available coordinate systems that an Area of interest may be defined in.
Definition global.hpp:290
@ PixelCoordinates
Definition global.hpp:298
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition point_2d.hpp:371
T round(T... args)