CVB++ 15.0
rect_lt.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
22template< class T, class ENABLE = void>
23class RectLT final
24{
25
26#ifndef CVB_DOXYGEN
27 static_assert (IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
28};
29
30
31template <class T>
32class RectLT<T,
33 typename EnableIfNumeric<T>::type> final
34{
35
36#endif
37 public:
38
39 using ValueType = T;
40
42
47 RectLT() noexcept
48 : left_(static_cast<T>(0))
49 , top_(static_cast<T>(0))
50 , right_(static_cast<T>(0))
51 , bottom_(static_cast<T>(0))
52 {};
53
55
62 RectLT(T left, T top, T right, T bottom) noexcept
63 {
64 SetLeft(left);
65 SetTop(top);
66 SetRight(right);
67 SetBottom(bottom);
68 }
69
71
76 RectLT(Point2D<T> location, Size2D<T> size) noexcept
77 {
78 SetLocation(location);
79 SetSize(size);
80 }
81
83
87 T Left() const noexcept
88 {
89 return left_;
90 }
91
93
97 void SetLeft(T left) noexcept
98 {
99 left_ = left;
100 }
101
103
107 T Top() const noexcept
108 {
109 return top_;
110 }
111
113
117 void SetTop(T top) noexcept
118 {
119 top_ = top;
120 }
121
123
127 T Right() const noexcept
128 {
129 return right_;
130 }
131
133
137 void SetRight(T right) noexcept
138 {
139 right_ = right;
140 }
141
143
147 T Bottom() const noexcept
148 {
149 return bottom_;
150 }
151
153
157 void SetBottom(T bottom) noexcept
158 {
159 bottom_ = bottom;
160 }
161
163
167 T Width() const noexcept
168 {
169 return right_ - left_;
170 }
171
173
177 void SetWidth(T width) noexcept
178 {
179 right_ = left_ + width;
180 }
181
183
187 T Height() const noexcept
188 {
189 return bottom_ - top_;
190 }
191
193
197 void SetHeight(T height) noexcept
198 {
199 bottom_ = top_ + height;
200 }
201
203
207 Size2D<T> Size() const noexcept
208 {
209 return Size2D<T>(Width(), Height());
210 }
211
213
217 void SetSize(Size2D<T> size) noexcept
218 {
219 SetWidth(size.Width());
220 SetHeight(size.Height());
221 }
222
224
228 Point2D<T> Location() const noexcept
229 {
230 return Point2D<T>(left_, top_);
231 }
232
234
238 void SetLocation(Point2D<T> location) noexcept
239 {
240 right_ = location.X() + Width();
241 bottom_ = location.Y() + Height();
242 left_ = location.X();
243 top_ = location.Y();
244 }
245
246
248
252 bool IsValid() const noexcept
253 {
254 return Width() > static_cast<T>(0.0)
255 && Height() > static_cast<T>(0.0);
256 }
257
258
260
265 bool Contains(Point2D<T> point) const noexcept
266 {
267 return (point.X() >= Left()) && (point.Y() >= Top()) && (point.X() <= Right()) && (point.Y() <= Bottom());
268 }
269
270
272
277 {
279 }
280
282
287 bool operator==(const RectLT<T>& rect) const noexcept
288 {
289 return (top_ == rect.top_
290 && left_ == rect.left_
291 && bottom_ == rect.bottom_
292 && right_ == rect.right_);
293 }
294
295
297
302 bool operator!=(const RectLT<T>& rect) const noexcept
303 {
304 return !(*this == rect);
305 }
306
308 template<class C>
309 explicit operator RectLT<C>() const noexcept
310 {
311 return RectLT<C>(
312 static_cast<C>(Left()),
313 static_cast<C>(Top()),
314 static_cast<C>(Right()),
315 static_cast<C>(Bottom())
316 );
317 }
318
319 private:
320
321 T left_;
322 T top_;
323 T right_;
324 T bottom_;
325};
326
327
329
336template <class T>
337inline RectLT<int> Round(const RectLT<T> & rhs) noexcept
338{
339 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
340 return RectLT<int>(static_cast<int>(std::round(rhs.Left())),
341 static_cast<int>(std::round(rhs.Top())),
342 static_cast<int>(std::round(rhs.Right())),
343 static_cast<int>(std::round(rhs.Bottom())));
344}
345
346CVB_END_INLINE_NS
347
348}
Multi-purpose 2D vector class.
Definition: point_2d.hpp:20
Rectangle object.
Definition: rect_lt.hpp:24
T Bottom() const noexcept
Gets bottom of the rectangle.
Definition: rect_lt.hpp:147
bool Contains(Point2D< T > point) const noexcept
Checks if this rectangle contains a point.
Definition: rect_lt.hpp:265
RectLT(Point2D< T > location, Size2D< T > size) noexcept
Constructor for a rectangle.
Definition: rect_lt.hpp:76
T Height() const noexcept
Gets the height of the rectangle.
Definition: rect_lt.hpp:187
void SetRight(T right) noexcept
Sets rightmost point of the rectangle.
Definition: rect_lt.hpp:137
RectLT(T left, T top, T right, T bottom) noexcept
Constructor for a rectangle.
Definition: rect_lt.hpp:62
RectLT() noexcept
Default rectangle.
Definition: rect_lt.hpp:47
bool IsValid() const noexcept
Checks if the rectangle is valid.
Definition: rect_lt.hpp:252
void SetWidth(T width) noexcept
Sets the width of the rectangle.
Definition: rect_lt.hpp:177
void SetBottom(T bottom) noexcept
Sets bottom of the rectangle.
Definition: rect_lt.hpp:157
bool operator==(const RectLT< T > &rect) const noexcept
Compares to another rectangle.
Definition: rect_lt.hpp:287
CoordinateSystemType CoordinateSystem() const noexcept
Indicates the coordinate system in which this object is being measured (when used as an area of inter...
Definition: rect_lt.hpp:276
Size2D< T > Size() const noexcept
Gets the size of the rectangle.
Definition: rect_lt.hpp:207
void SetLeft(T left) noexcept
Sets leftmost point of the rectangle.
Definition: rect_lt.hpp:97
void SetLocation(Point2D< T > location) noexcept
Sets the location of the top left corner of the rectangle.
Definition: rect_lt.hpp:238
T Top() const noexcept
Gets top of the rectangle.
Definition: rect_lt.hpp:107
T Right() const noexcept
Gets rightmost point of the rectangle.
Definition: rect_lt.hpp:127
T Left() const noexcept
Gets the leftmost point of the rectangle.
Definition: rect_lt.hpp:87
void SetHeight(T height) noexcept
Sets the height of the rectangle.
Definition: rect_lt.hpp:197
void SetTop(T top) noexcept
Sets top of the rectangle.
Definition: rect_lt.hpp:117
void SetSize(Size2D< T > size) noexcept
Gets the size of the rectangle.
Definition: rect_lt.hpp:217
T Width() const noexcept
Gets the width of the rectangle.
Definition: rect_lt.hpp:167
bool operator!=(const RectLT< T > &rect) const noexcept
Compares to another rectangle.
Definition: rect_lt.hpp:302
Point2D< T > Location() const noexcept
Gets the location of the top left corner of the rectangle.
Definition: rect_lt.hpp:228
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