TeiaCareSDK  v0.1.0
TeiaCareSDK is a collection of reusable C++ components
Loading...
Searching...
No Matches
rectangle.hpp
1// Copyright 2024 TeiaCare
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <teiacare/sdk/geometry/line.hpp>
18#include <teiacare/sdk/geometry/point.hpp>
19#include <teiacare/sdk/geometry/size.hpp>
20#include <teiacare/sdk/math.hpp>
21
22#include <string>
23
24namespace tc::sdk
25{
26/*!
27 * \class rectangle
28 * \brief 2D rectangle specified by a position point (top-left corner) and a pair of width/height dimensions.
29 * \tparam T Underlying coordinates and dimensions type
30 *
31 */
32template <typename T>
34{
35public:
36 /*!
37 * \brief Default Constructor. Creates a default tc::sdk::rectangle instance.
38 */
39 constexpr rectangle() = default;
40
41 /*!
42 * \brief Copy Constructor. Copy a tc::sdk::rectangle instance into another one.
43 */
44 constexpr rectangle(const rectangle&) = default;
45
46 /*!
47 * \brief Move Constructor. Copy a tc::sdk::rectangle instance into another one.
48 */
49 constexpr rectangle(rectangle&&) = default;
50
51 /*!
52 * \brief Constructor. Creates tc::sdk::rectangle instance with given width and height dimensions.
53 * \param position the position of the rectangle (top-left corner).
54 * \param w the width of the rectangle.
55 * \param h the height of the rectangle.
56 */
57 constexpr explicit rectangle(tc::sdk::point<T> position, T w, T h) noexcept
58 : _position{position}
59 , _width{w}
60 , _height{h}
61 {
62 }
63
64 /*!
65 * \brief Constructor. Creates tc::sdk::rectangle instance with given width and height dimensions.
66 * \param x the x coordinate of the rectangle position (top-left corner).
67 * \param y the y coordinate of the rectangle position (top-left corner).
68 * \param w the width of the rectangle.
69 * \param h the height of the rectangle.
70 */
71 constexpr explicit rectangle(T x, T y, T w, T h) noexcept
72 : _position{x, y}
73 , _width{w}
74 , _height{h}
75 {
76 }
77
78 /*!
79 * \brief Assignment operator. Assign a tc::sdk::rectangle instance to another one.
80 */
81 rectangle& operator=(const rectangle&) = default;
82
83 /*!
84 * \brief Move assignment operator. Assign a tc::sdk::rectangle instance to another one.
85 */
87
88 /*!
89 * \brief Equality operator.
90 * \param other the rectangle to compare against.
91 * \return true if the two rectangles have the same dimensions and the same position.
92 */
93 constexpr inline bool operator==(const rectangle& other) const noexcept
94 {
95 return _position == other._position &&
96 _width == other._width &&
97 _height == other._height;
98 }
99
100 /*!
101 * \brief Inequality operator.
102 * \param other the rectangle to compare against.
103 * \return true if the two rectangles have the different dimensions or different position.
104 */
105 constexpr inline bool operator!=(const rectangle& other) const noexcept
106 {
107 return !operator==(other);
108 }
109
110 /*!
111 * \brief Check if the rectangle is the null.
112 * \return true if both width and height are zero or negative.
113 */
114 constexpr bool is_null() const noexcept
115 {
116 return _width <= 0 && _height <= 0;
117 }
118
119 /*!
120 * \brief Width dimension getter.
121 * \return Width dimension of the rectangle.
122 */
123 constexpr inline T width() const noexcept
124 {
125 return _width;
126 }
127
128 /*!
129 * \brief Height dimension getter.
130 * \return Height dimension of the rectangle.
131 */
132 constexpr inline T height() const noexcept
133 {
134 return _height;
135 }
136
137 /*!
138 * \brief Width coordinate setter. Set the width dimension of the rectangle.
139 */
140 inline void set_width(T width) noexcept
141 {
142 _width = width;
143 }
144
145 /*!
146 * \brief Height coordinate setter. Set the height dimension of the rectangle.
147 */
148 inline void set_height(T height) noexcept
149 {
150 _height = height;
151 }
152
153 /*!
154 * \brief Size setter.
155 * \return Set the size of the rectangle.
156 */
157 constexpr inline void set_size(T width, T height) noexcept
158 {
159 _width = width;
160 _height = height;
161 }
162
163 /*!
164 * \brief Set the rectangle position.
165 * \param position the new top left coordinate of the rectangle.
166 */
167 constexpr inline void set_position(tc::sdk::point<T> position) noexcept
168 {
169 _position = position;
170 }
171
172 /*!
173 * \brief Top left coordinate getter.
174 * \return Top left coordinate of the rectangle.
175 */
177 {
178 return tc::sdk::point<T>(_position);
179 }
180
181 /*!
182 * \brief Top right coordinate getter.
183 * \return Top right coordinate of the rectangle.
184 */
186 {
187 return tc::sdk::point<T>(_position.x() + _width, _position.y());
188 }
189
190 /*!
191 * \brief Bottom left coordinate getter.
192 * \return Bottom left coordinate of the rectangle.
193 */
195 {
196 return tc::sdk::point<T>(_position.x(), _position.y() + _height);
197 }
198
199 /*!
200 * \brief Bottom right coordinate getter.
201 * \return Bottom right coordinate of the rectangle.
202 */
204 {
205 return tc::sdk::point<T>(_position.x() + _width, _position.y() + _height);
206 }
207
208 /*!
209 * \brief Size getter.
210 * \return The size of the rectangle.
211 */
213 {
214 return tc::sdk::size<T>(_width, _height);
215 }
216
217 /*!
218 * \brief Area getter.
219 * \return The area of the rectangle.
220 */
221 constexpr inline T area() const noexcept
222 {
223 return _width * _height;
224 }
225
226 /*!
227 * \brief Center getter.
228 * \return The center coordinate of the rectangle.
229 */
231 {
232 return tc::sdk::point<double>(_position.x() + _width * 0.5, _position.y() + _height * 0.5);
233 }
234
235 /*!
236 * \brief Check if the given point is strictly contained within the current rectangle.
237 * \param point The point that is checked.
238 * \return true if the point is strictly contained in the current rectangle.
239 */
240 bool contains(tc::sdk::point<T> point) const noexcept
241 {
242 return point.x() > _position.x() &&
243 point.y() > _position.y() &&
244 point.x() < _position.x() + _width &&
245 point.y() < _position.y() + _height;
246 }
247
248 /*!
249 * \brief Check if the given rectangle is strictly contained within the current rectangle.
250 * \param other The rectangle that is checked.
251 * \return true if the other rectangle is strictly contained in the current rectangle.
252 */
254 {
255 return _position.x() < other._position.x() &&
256 _position.y() < other._position.y() &&
257 _position.x() + _width >= other._position.x() + other._width &&
258 _position.y() + _height >= other._position.y() + other._height;
259 }
260
261 /*!
262 * \brief Check if the given rectangle overlaps the current rectangle.
263 * \param other The rectangle to check the intersection with.
264 * \return true if this and the other rectangles are overlapped.
265 */
267 {
268 return _position.x() + _width > other._position.x() &&
269 _position.y() + _height > other._position.y() &&
270 _position.x() < other._position.x() + other._width &&
271 _position.y() < other._position.y() + other._height &&
272 !other.is_null() &&
273 !is_null();
274 }
275
276 /*!
277 * \brief Check if the given line pass through the current rectangle.
278 * \param line The line to check the intersection with.
279 * \return true if the given line intersects the current rectangle.
280 */
290
291 /** Returns the region that is the overlap between this and another rectangle.
292 If the two rectangles don't overlap, the rectangle returned will be empty.
293 */
295 {
296 if (is_null() || other.is_null())
297 return tc::sdk::rectangle<T>{};
298
299 auto intersection_x = tc::sdk::max(_position.x(), other._position.x());
300 auto intersection_y = tc::sdk::max(_position.y(), other._position.y());
301
302 auto intersection_width = tc::sdk::min(_position.x() + _width, other._position.x() + other._width) - intersection_x;
303 if (intersection_width < T())
304 return tc::sdk::rectangle<T>{};
305
306 auto intersection_height = tc::sdk::min(_position.y() + _height, other._position.y() + other._height) - intersection_y;
307 if (intersection_height < T())
308 return tc::sdk::rectangle<T>{};
309
312 }
313
314 /** Returns the smallest rectangle that contains both this one and the one passed-in.
315
316 If either this or the other rectangle are empty, they will not be counted as
317 part of the resulting region.
318 */
320 {
321 if (other.is_null())
322 return *this;
323
324 if (is_null())
325 return other;
326
327 auto union_x = tc::sdk::min(_position.x(), other._position.x());
328 auto union_y = tc::sdk::min(_position.y(), other._position.y());
329
330 auto union_width = tc::sdk::max(_position.x() + _width, other._position.x() + other._width) - union_x;
331 auto union_height = tc::sdk::max(_position.y() + _height, other._position.y() + other._height) - union_y;
332
335 }
336
337 /*!
338 * \brief Translate the rectangle by a given delta_x and delta_y offsets.
339 * \param delta_x the offset to be applied on the x direction
340 * \param delta_y the offset to be applied on the y direction
341 */
342 void translate(T delta_x, T delta_y) noexcept
343 {
344 _position.add_delta(delta_x, delta_y);
345 }
346
347 /*!
348 * \brief Create a copy of the rectangle translated by a given delta_x and delta_y offsets.
349 * \param delta_x the offset to be applied on the x direction
350 * \param delta_y the offset to be applied on the y direction
351 * \return a translated copy of the current rectangle
352 */
354 {
355 const auto translated_position = tc::sdk::point<T>(_position);
357 return tc::sdk::rectangle<T>(translated_position, _width, _height);
358 }
359
360 /*!
361 * \brief Get the rectangle string representation
362 * \return String representation of the current rectangle.
363 */
364 constexpr std::string to_string() const
365 {
366 return std::string(_position.to_string() + " : (" + std::to_string(_width) + "x" + std::to_string(_height) + ")");
367 }
368
369 /*!
370 * \brief Output stream operator.
371 * \param stream the output stream to write into.
372 * \param r the rectangle object to stream.
373 * \return reference to the output stream operator, with the rectangle string representation written into it.
374 */
375 friend std::ostream& operator<<(std::ostream& stream, const rectangle& r)
376 {
377 return stream << r.to_string();
378 }
379
380private:
381 tc::sdk::point<T> _position{};
382 T _width{};
383 T _height{};
384};
385
386}
Thread safe, blocking queue.
2D line specified by a pair of start/end points.
Definition line.hpp:32
constexpr tc::sdk::point< T > start() const noexcept
Start point getter.
Definition line.hpp:122
constexpr tc::sdk::point< T > end() const noexcept
End point getter.
Definition line.hpp:131
bool intersects(tc::sdk::line< T > other) const noexcept
Check if the given line segments intersects the current instance.
Definition line.hpp:157
2D point specified by a pair of (x,y) coordinates.
Definition point.hpp:30
constexpr T x() const noexcept
X coordinate getter.
Definition point.hpp:101
constexpr T y() const noexcept
Y coordinate getter.
Definition point.hpp:110
2D rectangle specified by a position point (top-left corner) and a pair of width/height dimensions.
Definition rectangle.hpp:34
constexpr tc::sdk::size< T > size() const noexcept
Size getter.
friend std::ostream & operator<<(std::ostream &stream, const rectangle &r)
Output stream operator.
constexpr tc::sdk::point< T > bottom_right() const noexcept
Bottom right coordinate getter.
constexpr rectangle(const rectangle &)=default
Copy Constructor. Copy a tc::sdk::rectangle instance into another one.
constexpr void set_size(T width, T height) noexcept
Size setter.
constexpr T height() const noexcept
Height dimension getter.
bool contains(tc::sdk::rectangle< T > other) const noexcept
Check if the given rectangle is strictly contained within the current rectangle.
tc::sdk::rectangle< T > get_union(tc::sdk::rectangle< T > other) const noexcept
constexpr T area() const noexcept
Area getter.
constexpr rectangle(T x, T y, T w, T h) noexcept
Constructor. Creates tc::sdk::rectangle instance with given width and height dimensions.
Definition rectangle.hpp:71
constexpr std::string to_string() const
Get the rectangle string representation.
constexpr bool is_null() const noexcept
Check if the rectangle is the null.
constexpr tc::sdk::point< T > bottom_left() const noexcept
Bottom left coordinate getter.
constexpr void set_position(tc::sdk::point< T > position) noexcept
Set the rectangle position.
tc::sdk::rectangle< T > translated(T delta_x, T delta_y) const noexcept
Create a copy of the rectangle translated by a given delta_x and delta_y offsets.
rectangle & operator=(const rectangle &)=default
Assignment operator. Assign a tc::sdk::rectangle instance to another one.
bool intersects(tc::sdk::line< T > line) const noexcept
Check if the given line pass through the current rectangle.
void set_width(T width) noexcept
Width coordinate setter. Set the width dimension of the rectangle.
bool contains(tc::sdk::point< T > point) const noexcept
Check if the given point is strictly contained within the current rectangle.
constexpr tc::sdk::point< double > center() const noexcept
Center getter.
constexpr bool operator==(const rectangle &other) const noexcept
Equality operator.
Definition rectangle.hpp:93
tc::sdk::rectangle< T > get_intersection(tc::sdk::rectangle< T > other) const noexcept
constexpr rectangle()=default
Default Constructor. Creates a default tc::sdk::rectangle instance.
constexpr rectangle(tc::sdk::point< T > position, T w, T h) noexcept
Constructor. Creates tc::sdk::rectangle instance with given width and height dimensions.
Definition rectangle.hpp:57
constexpr rectangle(rectangle &&)=default
Move Constructor. Copy a tc::sdk::rectangle instance into another one.
void translate(T delta_x, T delta_y) noexcept
Translate the rectangle by a given delta_x and delta_y offsets.
constexpr tc::sdk::point< T > top_right() const noexcept
Top right coordinate getter.
constexpr tc::sdk::point< T > top_left() const noexcept
Top left coordinate getter.
void set_height(T height) noexcept
Height coordinate setter. Set the height dimension of the rectangle.
bool intersects(tc::sdk::rectangle< T > other) const noexcept
Check if the given rectangle overlaps the current rectangle.
constexpr bool operator!=(const rectangle &other) const noexcept
Inequality operator.
constexpr T width() const noexcept
Width dimension getter.
rectangle & operator=(rectangle &&)=default
Move assignment operator. Assign a tc::sdk::rectangle instance to another one.