CVB++ 14.1
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
40
45 RectLT() noexcept
46 : left_(static_cast<T>(0))
47 , top_(static_cast<T>(0))
48 , right_(static_cast<T>(0))
49 , bottom_(static_cast<T>(0))
50 {};
51
53
60 RectLT(T left, T top, T right, T bottom) noexcept
61 {
62 SetLeft(left);
63 SetTop(top);
64 SetRight(right);
65 SetBottom(bottom);
66 }
67
69
74 RectLT(Point2D<T> location, Size2D<T> size) noexcept
75 {
76 SetLocation(location);
77 SetSize(size);
78 }
79
81
85 T Left() const noexcept
86 {
87 return left_;
88 }
89
91
95 void SetLeft(T left) noexcept
96 {
97 left_ = left;
98 }
99
101
105 T Top() const noexcept
106 {
107 return top_;
108 }
109
111
115 void SetTop(T top) noexcept
116 {
117 top_ = top;
118 }
119
121
125 T Right() const noexcept
126 {
127 return right_;
128 }
129
131
135 void SetRight(T right) noexcept
136 {
137 right_ = right;
138 }
139
141
145 T Bottom() const noexcept
146 {
147 return bottom_;
148 }
149
151
155 void SetBottom(T bottom) noexcept
156 {
157 bottom_ = bottom;
158 }
159
161
165 T Width() const noexcept
166 {
167 return right_ - left_;
168 }
169
171
175 void SetWidth(T width) noexcept
176 {
177 right_ = left_ + width;
178 }
179
181
185 T Height() const noexcept
186 {
187 return bottom_ - top_;
188 }
189
191
195 void SetHeight(T height) noexcept
196 {
197 bottom_ = top_ + height;
198 }
199
201
205 Size2D<T> Size() const noexcept
206 {
207 return Size2D<T>(Width(), Height());
208 }
209
211
215 void SetSize(Size2D<T> size) noexcept
216 {
217 SetWidth(size.Width());
218 SetHeight(size.Height());
219 }
220
222
226 Point2D<T> Location() const noexcept
227 {
228 return Point2D<T>(left_, top_);
229 }
230
232
236 void SetLocation(Point2D<T> location) noexcept
237 {
238 right_ = location.X() + Width();
239 bottom_ = location.Y() + Height();
240 left_ = location.X();
241 top_ = location.Y();
242 }
243
244
246
250 bool IsValid() const noexcept
251 {
252 return Width() > static_cast<T>(0.0)
253 && Height() > static_cast<T>(0.0);
254 }
255
256
258
263 bool Contains(Point2D<T> point) const noexcept
264 {
265 return (point.X() >= Left()) && (point.Y() >= Top()) && (point.X() <= Right()) && (point.Y() <= Bottom());
266 }
267
268
270
275 {
277 }
278
280
285 bool operator==(const RectLT<T>& rect) const noexcept
286 {
287 return (top_ == rect.top_
288 && left_ == rect.left_
289 && bottom_ == rect.bottom_
290 && right_ == rect.right_);
291 }
292
293
295
300 bool operator!=(const RectLT<T>& rect) const noexcept
301 {
302 return !(*this == rect);
303 }
304
306 template<class C>
307 explicit operator RectLT<C>() const noexcept
308 {
309 return RectLT<C>(
310 static_cast<C>(Left()),
311 static_cast<C>(Top()),
312 static_cast<C>(Right()),
313 static_cast<C>(Bottom())
314 );
315 }
316
317 private:
318
319 T left_;
320 T top_;
321 T right_;
322 T bottom_;
323};
324
325
327
334template <class T>
335inline RectLT<int> Round(const RectLT<T> & rhs) noexcept
336{
337 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
338 return RectLT<int>(static_cast<int>(std::round(rhs.Left())),
339 static_cast<int>(std::round(rhs.Top())),
340 static_cast<int>(std::round(rhs.Right())),
341 static_cast<int>(std::round(rhs.Bottom())));
342}
343
344CVB_END_INLINE_NS
345
346}
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:145
bool Contains(Point2D< T > point) const noexcept
Checks if this rectangle contains a point.
Definition: rect_lt.hpp:263
RectLT(Point2D< T > location, Size2D< T > size) noexcept
Constructor for a rectangle.
Definition: rect_lt.hpp:74
T Height() const noexcept
Gets the height of the rectangle.
Definition: rect_lt.hpp:185
void SetRight(T right) noexcept
Sets rightmost point of the rectangle.
Definition: rect_lt.hpp:135
RectLT(T left, T top, T right, T bottom) noexcept
Constructor for a rectangle.
Definition: rect_lt.hpp:60
RectLT() noexcept
Default rectangle.
Definition: rect_lt.hpp:45
bool IsValid() const noexcept
Checks if the rectangle is valid.
Definition: rect_lt.hpp:250
void SetWidth(T width) noexcept
Sets the width of the rectangle.
Definition: rect_lt.hpp:175
void SetBottom(T bottom) noexcept
Sets bottom of the rectangle.
Definition: rect_lt.hpp:155
bool operator==(const RectLT< T > &rect) const noexcept
Compares to another rectangle.
Definition: rect_lt.hpp:285
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:274
Size2D< T > Size() const noexcept
Gets the size of the rectangle.
Definition: rect_lt.hpp:205
void SetLeft(T left) noexcept
Sets leftmost point of the rectangle.
Definition: rect_lt.hpp:95
void SetLocation(Point2D< T > location) noexcept
Sets the location of the top left corner of the rectangle.
Definition: rect_lt.hpp:236
T Top() const noexcept
Gets top of the rectangle.
Definition: rect_lt.hpp:105
T Right() const noexcept
Gets rightmost point of the rectangle.
Definition: rect_lt.hpp:125
T Left() const noexcept
Gets the leftmost point of the rectangle.
Definition: rect_lt.hpp:85
void SetHeight(T height) noexcept
Sets the height of the rectangle.
Definition: rect_lt.hpp:195
void SetTop(T top) noexcept
Sets top of the rectangle.
Definition: rect_lt.hpp:115
void SetSize(Size2D< T > size) noexcept
Gets the size of the rectangle.
Definition: rect_lt.hpp:215
T Width() const noexcept
Gets the width of the rectangle.
Definition: rect_lt.hpp:165
bool operator!=(const RectLT< T > &rect) const noexcept
Compares to another rectangle.
Definition: rect_lt.hpp:300
Point2D< T > Location() const noexcept
Gets the location of the top left corner of the rectangle.
Definition: rect_lt.hpp:226
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:270
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition: point_2d.hpp:380