Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
Rect.h
1// trax track library
2// AD 2014
3//
4// "Dream On"
5//
6// Aerosmith
7//
8// Copyright (c) 2025 Trend Redaktions- und Verlagsgesellschaft mbH
9// Copyright (c) 2019 Marc-Michael Horstmann
10//
11// Permission is hereby granted to any person obtaining a copy of this software
12// and associated source code (the "Software"), to use, view, and study the
13// Software for personal or internal business purposes, subject to the following
14// conditions:
15//
16// 1. Redistribution, modification, sublicensing, or commercial use of the
17// Software is NOT permitted without prior written consent from the copyright
18// holder.
19//
20// 2. The Software is provided "AS IS", without warranty of any kind, express
21// or implied.
22//
23// 3. All copies of the Software must retain this license notice.
24//
25// For further information, please contact: horstmann.marc@trendverlag.de
26
27#pragma once
28
29#include "common/Interval.h"
30
31
32namespace spat{
33
34 template<typename> struct Position2D;
35 template<typename> struct Vector2D;
36
37
39 template<typename Valtype>
40 struct Rect
41 {
42 typedef Valtype value_type;
43
46
55 Rect() noexcept = default;
56 Rect( Valtype width, Valtype height ) noexcept;
57 Rect( const common::Interval<Valtype>& width, const common::Interval<Valtype>& height ) noexcept;
58 explicit Rect( const Vector2D<Valtype>& diagonal ) noexcept;
59 Rect( const Position2D<Valtype>& lefttop, const Position2D<Valtype>& rightbottom ) noexcept;
60 Rect( Valtype left, Valtype top, Valtype right, Valtype bottom ) noexcept;
62
63
71 Rect& Init() noexcept;
72
73 Rect& Init( Valtype left, Valtype top, Valtype width, Valtype height ) noexcept;
75
76
78 bool Normal() const noexcept;
79
80
82 Rect& Normalize() noexcept;
83
84
86 Valtype Left() const noexcept;
87
88
90 Valtype Top() const noexcept;
91
92
94 Valtype Right() const noexcept;
95
96
98 Valtype Bottom() const noexcept;
99
100
101 Rect& Left( Valtype val ) noexcept;
102 Rect& Top( Valtype val ) noexcept;
103 Rect& Right( Valtype val ) noexcept;
104 Rect& Bottom( Valtype val ) noexcept;
105
106 Position2D<Valtype> LeftTop () const noexcept;
107 Position2D<Valtype> LeftBottom () const noexcept;
108 Position2D<Valtype> RightBottom () const noexcept;
109 Position2D<Valtype> RightTop () const noexcept;
110 Position2D<Valtype> Center () const noexcept;
111
112
114 Valtype Width() const noexcept;
115
116
118 Valtype Height() const noexcept;
119
120
122 auto Area() const noexcept -> decltype(Valtype{}*Valtype{});
123
124
126 Valtype Diagonal() const noexcept;
127
128
130 bool IsArea() const noexcept;
131
132
134 Rect& Union( const Rect& r1, const Rect& r2 ) noexcept;
135
136
138 Rect& Union( const Rect& rect ) noexcept;
139
140
145 Rect& Intersection( const Rect& r1, const Rect& r2 ) noexcept;
146
147
152 Rect& Intersection( const Rect& rect ) noexcept;
153
154
159 bool Includes( Valtype x, Valtype y ) const noexcept;
160
161
166 bool Includes( const Position2D<Valtype>& pt ) const noexcept;
167
168
173 Rect& Expand( Valtype x, Valtype y ) noexcept;
174
175
180 Rect& Expand( const Position2D<Valtype>& pt ) noexcept;
181
182
186 Rect& Inflate( Valtype dx, Valtype dy ) noexcept;
187
188
192 Rect& Deflate( Valtype dx, Valtype dy ) noexcept;
193
194
201 Rect& Move( Valtype dx, Valtype dy ) noexcept;
202
203 Rect& Move( const Vector2D<Valtype>& offset ) noexcept;
205 };
206
207
210 template<typename Valtype>
211 void operator+=( Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept;
212 template<typename Valtype>
213 Rect<Valtype> operator+( const Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept;
214 template<typename Valtype> constexpr
215 bool operator==( const Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept;
216 template<typename Valtype> constexpr
217 bool operator!=( const Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept;
219
220
221
223 template<typename Valtype>
224 bool Intersect( const Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept;
225
227//inlines:
228template<typename Valtype> inline
229Rect<Valtype>::Rect( Valtype width, Valtype height ) noexcept
230 : m_Width {-width/2,width/2},
231 m_Height{-height/2,height/2}
232{}
233
234template<typename Valtype> inline
236 : m_Width {width},
237 m_Height{height}
238{}
239
240template<typename Valtype> inline
241Rect<Valtype>::Rect( const Vector2D<Valtype>& diagonal ) noexcept
242 : m_Width {-diagonal.dx/2,diagonal.dx/2},
243 m_Height{-diagonal.dy/2,diagonal.dy/2}
244{}
245
246template<typename Valtype> inline
247Rect<Valtype>::Rect( const Position2D<Valtype>& lefttop, const Position2D<Valtype>& rightbottom ) noexcept
248 : m_Width {lefttop.x,rightbottom.x},
249 m_Height{lefttop.y,rightbottom.y}
250{}
251
252template<typename Valtype> inline
253Rect<Valtype>::Rect( Valtype left, Valtype top, Valtype right, Valtype bottom ) noexcept
254 : m_Width {left,right},
255 m_Height{top,bottom}
256{}
257
258template<typename Valtype> inline
260 m_Width.Init();
261 m_Height.Init();
262 return *this;
263}
264
265template<typename Valtype> inline
266Rect<Valtype>& Rect<Valtype>::Init( Valtype left, Valtype top, Valtype width, Valtype height ) noexcept{
267 m_Width.Init( left, width );
268 m_Height.Init( top, height );
269 return *this;
270}
271
272template<typename Valtype> inline
273bool Rect<Valtype>::Normal() const noexcept{
274 return m_Width.Normal() && m_Height.Normal();
275}
276
277template<typename Valtype> inline
279 m_Width.Normalize();
280 m_Height.Normalize();
281 return *this;
282}
283
284template<typename Valtype> inline
285Valtype Rect<Valtype>::Left() const noexcept{
286 return m_Width.m_Near;
287}
288
289template<typename Valtype> inline
290Valtype Rect<Valtype>::Top() const noexcept{
291 return m_Height.m_Near;
292}
293
294template<typename Valtype> inline
295Valtype Rect<Valtype>::Right() const noexcept{
296 return m_Width.m_Far;
297}
298
299template<typename Valtype> inline
300Valtype Rect<Valtype>::Bottom() const noexcept{
301 return m_Height.m_Far;
302}
303
304template<typename Valtype> inline
305Rect<Valtype>& Rect<Valtype>::Left( Valtype val ) noexcept{
306 m_Width.Near(val);
307 return *this;
308}
309
310template<typename Valtype> inline
311Rect<Valtype>& Rect<Valtype>::Top( Valtype val ) noexcept{
312 m_Height.Near(val);
313 return *this;
314}
315
316template<typename Valtype> inline
317Rect<Valtype>& Rect<Valtype>::Right( Valtype val ) noexcept{
318 m_Width.Far(val);
319 return *this;
320}
321
322template<typename Valtype> inline
323Rect<Valtype>& Rect<Valtype>::Bottom( Valtype val ) noexcept{
324 m_Height.Far(val);
325 return *this;
326}
327
328template<typename Valtype> inline
330 return { m_Width.Near(), m_Height.Near() };
331}
332
333template<typename Valtype> inline
335 return { m_Width.Near(), m_Height.Far() };
336}
337
338template<typename Valtype> inline
340 return { m_Width.Far(), m_Height.Far() };
341}
342
343template<typename Valtype> inline
345 return { m_Width.Far(), m_Height.Near() };
346}
347
348template<typename Valtype> inline
350 return { m_Width.Center(), m_Height.Center() };
351}
352
353template<typename Valtype> inline
354Valtype Rect<Valtype>::Width() const noexcept{
355 return m_Width.Length();
356}
357
358template<typename Valtype> inline
359Valtype Rect<Valtype>::Height() const noexcept{
360 return m_Height.Length();
361}
362
363template<typename Valtype> inline
364auto Rect<Valtype>::Area() const noexcept -> decltype(Valtype{}*Valtype{}){
365 return Width() * Height();
366}
367
368template<typename Valtype> inline
369Valtype Rect<Valtype>::Diagonal() const noexcept{
370 return sqrt( Square(Width()) + Square(Height()) );
371}
372
373template<typename Valtype> inline
374bool Rect<Valtype>::IsArea() const noexcept{
375 return (Normal() && Area() != 0.0f);
376}
377
378template<typename Valtype> inline
380 const Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept
381{
382 m_Width.Union( r1.m_Width, r2.m_Width );
383 m_Height.Union( r1.m_Height, r2.m_Height );
384 return *this;
385}
386
387template<typename Valtype> inline
389{
390 m_Width.Union( rect.m_Width );
391 m_Height.Union( rect.m_Height );
392 return *this;
393}
394
395template<typename Valtype> inline
397 const Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept
398{
399 m_Width.Intersection( r1.m_Width, r2.m_Width );
400 m_Height.Intersection( r1.m_Height, r2.m_Height );
401 return *this;
402}
403
404template<typename Valtype> inline
406{
407 m_Width.Intersection( rect.m_Width );
408 m_Height.Intersection( rect.m_Height );
409 return *this;
410}
411
412template<typename Valtype> inline
413bool Rect<Valtype>::Includes( Valtype x, Valtype y ) const noexcept{
414 return m_Width.Includes( x ) && m_Height.Includes( y );
415}
416
417template<typename Valtype> inline
418bool Rect<Valtype>::Includes( const Position2D<Valtype>& pt ) const noexcept{
419 return Includes( pt.x, pt.y );
420}
421
422template<typename Valtype> inline
423Rect<Valtype>& Rect<Valtype>::Expand( Valtype x, Valtype y ) noexcept{
424 m_Width.Expand( x );
425 m_Height.Expand( y );
426 return *this;
427}
428
429template<typename Valtype> inline
431 return Expand( pt.x, pt.y );
432}
433
434template<typename Valtype> inline
435Rect<Valtype>& Rect<Valtype>::Inflate( Valtype dx, Valtype dy ) noexcept{
436 m_Width.Inflate( dx );
437 m_Height.Inflate( dy );
438 return *this;
439}
440
441template<typename Valtype> inline
442Rect<Valtype>& Rect<Valtype>::Deflate( Valtype dx, Valtype dy ) noexcept{
443 return Inflate( -dx, -dy );
444}
445
446template<typename Valtype> inline
447Rect<Valtype>& Rect<Valtype>::Move( Valtype dx, Valtype dy ) noexcept{
448 m_Width.Move( dx );
449 m_Height.Move( dy );
450 return *this;
451}
452
453template<typename Valtype> inline
454Rect<Valtype>& Rect<Valtype>::Move( const Vector2D<Valtype>& offset ) noexcept{
455 return Move( offset.dx, offset.dy );
456}
457
458template<typename Valtype> inline
459void operator+=( Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept{
460 r1.Union(r2);
461}
462
463template<typename Valtype> inline
464Rect<Valtype> operator+( const Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept{
465 Rect<Valtype> retval;
466 retval.Union( r1, r2 );
467 return retval;
468}
469
470template<typename Valtype> inline
471bool Intersect( const Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept{
472 return Intersect( r1.m_Width, r2.m_Width ) && Intersect( r1.m_Height, r2.m_Height );
473}
474
475template<typename Valtype> constexpr
476inline bool operator==( const Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept{
477 return r1.m_Width == r2.m_Width && r1.m_Height == r2.m_Height;
478}
479
480template<typename Valtype> constexpr
481inline bool operator!=( const Rect<Valtype>& r1, const Rect<Valtype>& r2 ) noexcept{
482 return !(r1 == r2);
483}
484
485} // namespace spat
486
Namespace of common utility classes and methods.
Definition Helpers.h:43
constexpr bool operator!=(const Interval< Valtype > &i1, const Interval< Valtype > &i2) noexcept
Interval operator.
Definition Interval.h:701
constexpr Interval< Valtype > operator+(const Interval< Valtype > &i1, const Interval< Valtype > &i2) noexcept
Interval operator.
Definition Interval.h:591
The namespace provides classes and methods for spatial computations.
Definition Box.h:32
Position< Valtype > & operator+=(Position< Valtype > &, const Position< Valtype > &)=delete
Position operator.
bool Intersect(const Rect< Valtype > &r1, const Rect< Valtype > &r2) noexcept
Definition Rect.h:471
An interval describes the area between two numbers. It is understood to contain the near one and exlu...
Definition Interval.h:42
Implements a 2D - position in cartesian coordinates.
Definition Position2D.h:45
Axis aligned rectangle.
Definition Rect.h:41
bool IsArea() const noexcept
Is non zero area?
Definition Rect.h:374
Position2D< Valtype > RightTop() const noexcept
Definition Rect.h:344
Rect & Inflate(Valtype dx, Valtype dy) noexcept
Moves the edges to the outside of the rect, thereyby inflating it.
Definition Rect.h:435
common::Interval< Valtype > m_Width
X dimension.
Definition Rect.h:44
Valtype Left() const noexcept
Definition Rect.h:285
Position2D< Valtype > RightBottom() const noexcept
Definition Rect.h:339
Rect & Union(const Rect &r1, const Rect &r2) noexcept
Calculates the union of the two rects.
Definition Rect.h:379
Valtype Top() const noexcept
Definition Rect.h:290
Position2D< Valtype > LeftTop() const noexcept
Definition Rect.h:329
bool Includes(Valtype x, Valtype y) const noexcept
Definition Rect.h:413
Valtype Bottom() const noexcept
Definition Rect.h:300
Valtype Height() const noexcept
Definition Rect.h:359
Valtype Right() const noexcept
Definition Rect.h:295
auto Area() const noexcept -> decltype(Valtype{} *Valtype{})
Definition Rect.h:364
Position2D< Valtype > Center() const noexcept
Definition Rect.h:349
Rect() noexcept=default
Does not initialize the members.
Rect & Deflate(Valtype dx, Valtype dy) noexcept
Moves the edges to the inward side of the rect, thereyby deflating it.
Definition Rect.h:442
common::Interval< Valtype > m_Height
Y dimension.
Definition Rect.h:45
Rect & Intersection(const Rect &r1, const Rect &r2) noexcept
Calculates the intersection rect of r1 and r2.
Definition Rect.h:396
Rect & Expand(Valtype x, Valtype y) noexcept
Expands the rect so that it touches (being included or laying on border) the point.
Definition Rect.h:423
Valtype Width() const noexcept
Definition Rect.h:354
Rect & Normalize() noexcept
make bottom <= top and left <= right
Definition Rect.h:278
Position2D< Valtype > LeftBottom() const noexcept
Definition Rect.h:334
bool Normal() const noexcept
is bottom <= top and left <= right
Definition Rect.h:273
Valtype Diagonal() const noexcept
Definition Rect.h:369
Implements a 2D - vector in cartesian coordinates.
Definition Vector2D.h:46