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
9
10
11namespace Cvb
12{
13
14CVB_BEGIN_INLINE_NS
15
17
24template< class T, class ENABLE = void>
25class Rect final
26{
27
28#ifndef CVB_DOXYGEN
29 static_assert (IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
30};
31
32
33template <class T>
34class Rect<T,
35 typename EnableIfNumeric<T>::type> final
36{
37
38#endif
39 public:
40
41 using ValueType = T;
42
43
45
51 Rect() noexcept
52 : left_(static_cast<T>(0))
53 , top_(static_cast<T>(0))
54 , right_(static_cast<T>(-1))
55 , bottom_(static_cast<T>(-1))
56 {};
57
58
59
61
68 Rect(T left, T top, T right, T bottom) noexcept
69 {
70 SetLeft(left);
71 SetTop(top);
72 SetRight(right);
73 SetBottom(bottom);
74 }
75
77
82 Rect(Point2D<T> location, Size2D<T> size) noexcept
83 {
84 SetLocation(location);
85 SetSize(size);
86 }
87
89
93 T Left() const noexcept
94 {
95 return left_;
96 }
97
99
103 void SetLeft(T left) noexcept
104 {
105 left_ = left;
106 }
107
109
113 T Top() const noexcept
114 {
115 return top_;
116 }
117
119
123 void SetTop(T top) noexcept
124 {
125 top_ = top;
126 }
127
129
133 T Right() const noexcept
134 {
135 return right_;
136 }
137
139
143 void SetRight(T right) noexcept
144 {
145 right_ = right;
146 }
147
149
153 T Bottom() const noexcept
154 {
155 return bottom_;
156 }
157
159
163 void SetBottom(T bottom) noexcept
164 {
165 bottom_ = bottom;
166 }
167
169
173 T Width() const noexcept
174 {
175 return right_ - left_ + static_cast<T>(1);
176 }
177
179
183 void SetWidth(T width) noexcept
184 {
185 right_ = left_ + width - static_cast<T>(1);
186 }
187
189
193 T Height() const noexcept
194 {
195 return bottom_ - top_ + static_cast<T>(1);
196 }
197
199
203 void SetHeight(T height) noexcept
204 {
205 bottom_ = top_ + height - static_cast<T>(1);
206 }
207
209
213 Size2D<T> Size() const noexcept
214 {
215 return Size2D<T>(Width(), Height());
216 }
217
219
223 void SetSize(Size2D<T> size) noexcept
224 {
225 SetWidth(size.Width());
226 SetHeight(size.Height());
227 }
228
230
234 Point2D<T> Location() const noexcept
235 {
236 return Point2D<T>(left_, top_);
237 }
238
240
244 void SetLocation(Point2D<T> location) noexcept
245 {
246 right_ = location.X() + Width();
247 bottom_ = location.Y() + Height();
248 left_ = location.X();
249 top_ = location.Y();
250 }
251
253
258 bool Contains(Point2D<T> point) const noexcept
259 {
260 return (point.X() >= Left()) && (point.Y() >= Top()) && (point.X() <= Right()) && (point.Y() <= Bottom());
261 }
262
263
265
270 {
272 }
273
275
280 bool operator==(const Rect<T>& rect) const noexcept
281 {
282 return (top_ == rect.top_
283 && left_ == rect.left_
284 && bottom_ == rect.bottom_
285 && right_ == rect.right_);
286 }
287
288
290
295 bool operator!=(const Rect<T>& rect) const noexcept
296 {
297 return !(*this == rect);
298 }
299
301 template<class C>
302 explicit operator Rect<C, typename std::enable_if<std::is_integral<C>::value>::type>() const noexcept
303 {
304 return Rect<C>(
305 static_cast<C>(Left()),
306 static_cast<C>(Top()),
307 static_cast<C>(Right()),
308 static_cast<C>(Bottom())
309 );
310 }
311
312 template<class C>
313 explicit operator Rect<C, typename std::enable_if<!std::is_integral<C>::value>::type>() const noexcept
314 {
315 return Rect<C>(
316 static_cast<C>(Left()),
317 static_cast<C>(Top()),
318 static_cast<C>(Right()),
319 static_cast<C>(Bottom())
320 );
321 }
322
323 private:
324
325 T left_;
326 T top_;
327 T right_;
328 T bottom_;
329};
330
331
333
340template <class T>
341inline Rect<int> Round(const Rect<T> & rhs) noexcept
342{
343 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
344 return Rect<int>(static_cast<int>(std::round(rhs.Left())),
345 static_cast<int>(std::round(rhs.Top())),
346 static_cast<int>(std::round(rhs.Right())),
347 static_cast<int>(std::round(rhs.Bottom())));
348}
349
350CVB_END_INLINE_NS
351
352}
Multi-purpose 2D vector class.
Definition: point_2d.hpp:20
Rectangle object.
Definition: rect.hpp:26
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition: rect.hpp:153
bool Contains(Point2D< T > point) const noexcept
Checks if this rectangle contains a point.
Definition: rect.hpp:258
T Height() const noexcept
Gets the height of the rectangle.
Definition: rect.hpp:193
void SetRight(T right) noexcept
Sets rightmost column of the rectangle (still inside the rectangle).
Definition: rect.hpp:143
void SetWidth(T width) noexcept
Sets the width of the rectangle.
Definition: rect.hpp:183
Rect() noexcept
Default rectangle in the integral type system.
Definition: rect.hpp:51
void SetBottom(T bottom) noexcept
Sets bottom row of the rectangle (still inside the rectangle).
Definition: rect.hpp:163
bool operator!=(const Rect< T > &rect) const noexcept
Compares to another rectangle.
Definition: rect.hpp:295
Rect(T left, T top, T right, T bottom) noexcept
Constructor for a rectangle.
Definition: rect.hpp:68
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:269
Size2D< T > Size() const noexcept
Gets the size of the rectangle.
Definition: rect.hpp:213
void SetLeft(T left) noexcept
Sets first column of the rectangle.
Definition: rect.hpp:103
void SetLocation(Point2D< T > location) noexcept
Sets the location of the top left corner of the rectangle.
Definition: rect.hpp:244
T Top() const noexcept
Gets first row of the rectangle.
Definition: rect.hpp:113
bool operator==(const Rect< T > &rect) const noexcept
Compares to another rectangle.
Definition: rect.hpp:280
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition: rect.hpp:133
T Left() const noexcept
Gets first column of the rectangle.
Definition: rect.hpp:93
void SetHeight(T height) noexcept
Sets the height of the rectangle.
Definition: rect.hpp:203
void SetTop(T top) noexcept
Sets first row of the rectangle.
Definition: rect.hpp:123
void SetSize(Size2D< T > size) noexcept
Gets the size of the rectangle.
Definition: rect.hpp:223
Rect(Point2D< T > location, Size2D< T > size) noexcept
Constructor for a rectangle.
Definition: rect.hpp:82
T Width() const noexcept
Gets the width of the rectangle.
Definition: rect.hpp:173
Point2D< T > Location() const noexcept
Gets the location of the top left corner of the rectangle.
Definition: rect.hpp:234
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition: size_2d.hpp:20
T Height() const noexcept
Gets the vertical component of the size.
Definition: size_2d.hpp:80
T Width() const noexcept
Gets the horizontal component of the size.
Definition: size_2d.hpp:60
Root namespace for the Image Manager interface.
Definition: c_barcode.h:15
CoordinateSystemType
Enumeration of the different available coordinate systems that an Area of interest may be defined in.
Definition: global.hpp:270
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition: point_2d.hpp:382