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
9namespace Cvb
10{
11
12 CVB_BEGIN_INLINE_NS
13
15
20 template <class T, class ENABLE = void>
21 class RectLT final
22 {
23
24#ifndef CVB_DOXYGEN
25 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
26 };
27
28 template <class T>
29 class RectLT<T, typename EnableIfNumeric<T>::type> final
30 {
31
32#endif
33 public:
34 using ValueType = T;
35
37
42 RectLT() noexcept
43 : left_(static_cast<T>(0))
44 , top_(static_cast<T>(0))
45 , right_(static_cast<T>(0))
46 , bottom_(static_cast<T>(0)) {};
47
49
56 RectLT(T left, T top, T right, T bottom) noexcept
57 {
58 SetLeft(left);
59 SetTop(top);
60 SetRight(right);
61 SetBottom(bottom);
62 }
63
65
70 RectLT(Point2D<T> location, Size2D<T> size) noexcept
71 {
72 SetLocation(location);
73 SetSize(size);
74 }
75
77
81 T Left() const noexcept
82 {
83 return left_;
84 }
85
87
91 void SetLeft(T left) noexcept
92 {
93 left_ = left;
94 }
95
97
101 T Top() const noexcept
102 {
103 return top_;
104 }
105
107
111 void SetTop(T top) noexcept
112 {
113 top_ = top;
114 }
115
117
121 T Right() const noexcept
122 {
123 return right_;
124 }
125
127
131 void SetRight(T right) noexcept
132 {
133 right_ = right;
134 }
135
137
141 T Bottom() const noexcept
142 {
143 return bottom_;
144 }
145
147
151 void SetBottom(T bottom) noexcept
152 {
153 bottom_ = bottom;
154 }
155
157
161 T Width() const noexcept
162 {
163 return right_ - left_;
164 }
165
167
171 void SetWidth(T width) noexcept
172 {
173 right_ = left_ + width;
174 }
175
177
181 T Height() const noexcept
182 {
183 return bottom_ - top_;
184 }
185
187
191 void SetHeight(T height) noexcept
192 {
193 bottom_ = top_ + height;
194 }
195
197
201 Size2D<T> Size() const noexcept
202 {
203 return Size2D<T>(Width(), Height());
204 }
205
207
211 void SetSize(Size2D<T> size) noexcept
212 {
213 SetWidth(size.Width());
214 SetHeight(size.Height());
215 }
216
218
222 Point2D<T> Location() const noexcept
223 {
224 return Point2D<T>(left_, top_);
225 }
226
228
232 void SetLocation(Point2D<T> location) noexcept
233 {
234 right_ = location.X() + Width();
235 bottom_ = location.Y() + Height();
236 left_ = location.X();
237 top_ = location.Y();
238 }
239
241
245 bool IsValid() const noexcept
246 {
247 return Width() > static_cast<T>(0.0) && Height() > static_cast<T>(0.0);
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
263
271
273
278 bool operator==(const RectLT<T> &rect) const noexcept
279 {
280 return (top_ == rect.top_ && left_ == rect.left_ && bottom_ == rect.bottom_ && right_ == rect.right_);
281 }
282
284
289 bool operator!=(const RectLT<T> &rect) const noexcept
290 {
291 return !(*this == rect);
292 }
293
295 template <class C>
296 explicit operator RectLT<C>() const noexcept
297 {
298 return RectLT<C>(static_cast<C>(Left()), static_cast<C>(Top()), static_cast<C>(Right()),
299 static_cast<C>(Bottom()));
300 }
301
302 private:
303 T left_;
304 T top_;
305 T right_;
306 T bottom_;
307 };
308
310
317 template <class T>
318 inline RectLT<int> Round(const RectLT<T> &rhs) noexcept
319 {
320 static_assert(IsNumeric<T>::value, "CVB: Unsupported data type - must be numeric!");
321 return RectLT<int>(static_cast<int>(std::round(rhs.Left())), static_cast<int>(std::round(rhs.Top())),
322 static_cast<int>(std::round(rhs.Right())), static_cast<int>(std::round(rhs.Bottom())));
323 }
324
325 CVB_END_INLINE_NS
326
327} // namespace Cvb
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
Rectangle object.
Definition rect_lt.hpp:22
T Bottom() const noexcept
Gets bottom of the rectangle.
Definition rect_lt.hpp:141
bool Contains(Point2D< T > point) const noexcept
Checks if this rectangle contains a point.
Definition rect_lt.hpp:256
RectLT(Point2D< T > location, Size2D< T > size) noexcept
Constructor for a rectangle.
Definition rect_lt.hpp:70
T Height() const noexcept
Gets the height of the rectangle.
Definition rect_lt.hpp:181
void SetRight(T right) noexcept
Sets rightmost point of the rectangle.
Definition rect_lt.hpp:131
RectLT(T left, T top, T right, T bottom) noexcept
Constructor for a rectangle.
Definition rect_lt.hpp:56
RectLT() noexcept
Default rectangle.
Definition rect_lt.hpp:42
bool IsValid() const noexcept
Checks if the rectangle is valid.
Definition rect_lt.hpp:245
void SetWidth(T width) noexcept
Sets the width of the rectangle.
Definition rect_lt.hpp:171
void SetBottom(T bottom) noexcept
Sets bottom of the rectangle.
Definition rect_lt.hpp:151
bool operator==(const RectLT< T > &rect) const noexcept
Compares to another rectangle.
Definition rect_lt.hpp:278
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:267
Size2D< T > Size() const noexcept
Gets the size of the rectangle.
Definition rect_lt.hpp:201
void SetLeft(T left) noexcept
Sets leftmost point of the rectangle.
Definition rect_lt.hpp:91
void SetLocation(Point2D< T > location) noexcept
Sets the location of the top left corner of the rectangle.
Definition rect_lt.hpp:232
T Top() const noexcept
Gets top of the rectangle.
Definition rect_lt.hpp:101
T Right() const noexcept
Gets rightmost point of the rectangle.
Definition rect_lt.hpp:121
T Left() const noexcept
Gets the leftmost point of the rectangle.
Definition rect_lt.hpp:81
void SetHeight(T height) noexcept
Sets the height of the rectangle.
Definition rect_lt.hpp:191
void SetTop(T top) noexcept
Sets top of the rectangle.
Definition rect_lt.hpp:111
void SetSize(Size2D< T > size) noexcept
Gets the size of the rectangle.
Definition rect_lt.hpp:211
T Width() const noexcept
Gets the width of the rectangle.
Definition rect_lt.hpp:161
bool operator!=(const RectLT< T > &rect) const noexcept
Compares to another rectangle.
Definition rect_lt.hpp:289
Point2D< T > Location() const noexcept
Gets the location of the top left corner of the rectangle.
Definition rect_lt.hpp:222
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)