CVB++ 14.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
43
49 Rect() noexcept
50 : left_(static_cast<T>(0))
51 , top_(static_cast<T>(0))
52 , right_(static_cast<T>(-1))
53 , bottom_(static_cast<T>(-1))
54 {};
55
56
57
59
66 Rect(T left, T top, T right, T bottom) noexcept
67 {
68 SetLeft(left);
69 SetTop(top);
70 SetRight(right);
71 SetBottom(bottom);
72 }
73
75
80 Rect(Point2D<T> location, Size2D<T> size) noexcept
81 {
82 SetLocation(location);
83 SetSize(size);
84 }
85
87
91 T Left() const noexcept
92 {
93 return left_;
94 }
95
97
101 void SetLeft(T left) noexcept
102 {
103 left_ = left;
104 }
105
107
111 T Top() const noexcept
112 {
113 return top_;
114 }
115
117
121 void SetTop(T top) noexcept
122 {
123 top_ = top;
124 }
125
127
131 T Right() const noexcept
132 {
133 return right_;
134 }
135
137
141 void SetRight(T right) noexcept
142 {
143 right_ = right;
144 }
145
147
151 T Bottom() const noexcept
152 {
153 return bottom_;
154 }
155
157
161 void SetBottom(T bottom) noexcept
162 {
163 bottom_ = bottom;
164 }
165
167
171 T Width() const noexcept
172 {
173 return right_ - left_ + static_cast<T>(1);
174 }
175
177
181 void SetWidth(T width) noexcept
182 {
183 right_ = left_ + width - static_cast<T>(1);
184 }
185
187
191 T Height() const noexcept
192 {
193 return bottom_ - top_ + static_cast<T>(1);
194 }
195
197
201 void SetHeight(T height) noexcept
202 {
203 bottom_ = top_ + height - static_cast<T>(1);
204 }
205
207
211 Size2D<T> Size() const noexcept
212 {
213 return Size2D<T>(Width(), Height());
214 }
215
217
221 void SetSize(Size2D<T> size) noexcept
222 {
223 SetWidth(size.Width());
224 SetHeight(size.Height());
225 }
226
228
232 Point2D<T> Location() const noexcept
233 {
234 return Point2D<T>(left_, top_);
235 }
236
238
242 void SetLocation(Point2D<T> location) noexcept
243 {
244 right_ = location.X() + Width();
245 bottom_ = location.Y() + Height();
246 left_ = location.X();
247 top_ = location.Y();
248 }
249
251
256 bool Contains(Point2D<T> point) const noexcept
257 {
258 return (point.X() >= Left()) && (point.Y() >= Top()) && (point.X() <= Right()) && (point.Y() <= Bottom());
259 }
260
261
263
268 {
270 }
271
273
278 bool operator==(const Rect<T>& rect) const noexcept
279 {
280 return (top_ == rect.top_
281 && left_ == rect.left_
282 && bottom_ == rect.bottom_
283 && right_ == rect.right_);
284 }
285
286
288
293 bool operator!=(const Rect<T>& rect) const noexcept
294 {
295 return !(*this == rect);
296 }
297
299 template<class C>
300 explicit operator Rect<C, typename std::enable_if<std::is_integral<C>::value>::type>() const noexcept
301 {
302 return Rect<C>(
303 static_cast<C>(Left()),
304 static_cast<C>(Top()),
305 static_cast<C>(Right()),
306 static_cast<C>(Bottom())
307 );
308 }
309
310 template<class C>
311 explicit operator Rect<C, typename std::enable_if<!std::is_integral<C>::value>::type>() const noexcept
312 {
313 return Rect<C>(
314 static_cast<C>(Left()),
315 static_cast<C>(Top()),
316 static_cast<C>(Right()),
317 static_cast<C>(Bottom())
318 );
319 }
320
321 private:
322
323 T left_;
324 T top_;
325 T right_;
326 T bottom_;
327};
328
329
331
338template <class T>
339inline Rect<int> Round(const Rect<T> & rhs) noexcept
340{
341 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
342 return Rect<int>(static_cast<int>(std::round(rhs.Left())),
343 static_cast<int>(std::round(rhs.Top())),
344 static_cast<int>(std::round(rhs.Right())),
345 static_cast<int>(std::round(rhs.Bottom())));
346}
347
348CVB_END_INLINE_NS
349
350}
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:151
bool Contains(Point2D< T > point) const noexcept
Checks if this rectangle contains a point.
Definition: rect.hpp:256
T Height() const noexcept
Gets the height of the rectangle.
Definition: rect.hpp:191
void SetRight(T right) noexcept
Sets rightmost column of the rectangle (still inside the rectangle).
Definition: rect.hpp:141
void SetWidth(T width) noexcept
Sets the width of the rectangle.
Definition: rect.hpp:181
Rect() noexcept
Default rectangle in the integral type system.
Definition: rect.hpp:49
void SetBottom(T bottom) noexcept
Sets bottom row of the rectangle (still inside the rectangle).
Definition: rect.hpp:161
bool operator!=(const Rect< T > &rect) const noexcept
Compares to another rectangle.
Definition: rect.hpp:293
Rect(T left, T top, T right, T bottom) noexcept
Constructor for a rectangle.
Definition: rect.hpp:66
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:267
Size2D< T > Size() const noexcept
Gets the size of the rectangle.
Definition: rect.hpp:211
void SetLeft(T left) noexcept
Sets first column of the rectangle.
Definition: rect.hpp:101
void SetLocation(Point2D< T > location) noexcept
Sets the location of the top left corner of the rectangle.
Definition: rect.hpp:242
T Top() const noexcept
Gets first row of the rectangle.
Definition: rect.hpp:111
bool operator==(const Rect< T > &rect) const noexcept
Compares to another rectangle.
Definition: rect.hpp:278
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition: rect.hpp:131
T Left() const noexcept
Gets first column of the rectangle.
Definition: rect.hpp:91
void SetHeight(T height) noexcept
Sets the height of the rectangle.
Definition: rect.hpp:201
void SetTop(T top) noexcept
Sets first row of the rectangle.
Definition: rect.hpp:121
void SetSize(Size2D< T > size) noexcept
Gets the size of the rectangle.
Definition: rect.hpp:221
Rect(Point2D< T > location, Size2D< T > size) noexcept
Constructor for a rectangle.
Definition: rect.hpp:80
T Width() const noexcept
Gets the width of the rectangle.
Definition: rect.hpp:171
Point2D< T > Location() const noexcept
Gets the location of the top left corner of the rectangle.
Definition: rect.hpp:232
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:79
T Width() const noexcept
Gets the horizontal component of the size.
Definition: size_2d.hpp:59
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
CoordinateSystemType
Enumeration of the different available coordinate systems that an Area of interest may be defined in.
Definition: global.hpp:264
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition: point_2d.hpp:380