CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Rect.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
11
14
17
18namespace CeresEngine::inline Math {
19
21 template<typename T> using TPoint2 = TVector2<T>;
22
25
27 template<typename T> struct TSize2 {
29 T width = 0.0;
30
32 T height = 0.0;
33
34 TSize2() = default;
35 TSize2(T width, T height) : width(width), height(height) {}
36
37 explicit TSize2(const TVector2<T>& vec) : width(vec.x), height(vec.y) {}
38 explicit operator TVector2<T>() const { return Vector2(width, height); }
39
42 [[nodiscard]] TSize2 transform(const TMatrix3<T>& matrix) const;
43
44 friend bool operator==(const TSize2& lhs, const TSize2& rhs) { return lhs.width == rhs.width && lhs.height == rhs.height; }
45 friend bool operator!=(const TSize2& lhs, const TSize2& rhs) { return !(rhs == lhs); }
46
47 [[nodiscard]] friend inline TSize2 operator+(const TSize2& lhs, const TSize2& rhs) { return {lhs.width + rhs.width, lhs.height + rhs.height}; }
48 [[nodiscard]] friend inline TSize2 operator+(const TSize2& lhs, T rhs) { return {lhs.width + rhs, lhs.height + rhs}; }
49
50 [[nodiscard]] friend inline TSize2 operator-(const TSize2& lhs, const TSize2& rhs) { return {lhs.width - rhs.width, lhs.height - rhs.height}; }
51 [[nodiscard]] friend inline TSize2 operator-(const TSize2& lhs, T rhs) { return {lhs.width - rhs, lhs.height - rhs}; }
52
53 [[nodiscard]] friend inline TSize2 operator*(const TSize2& lhs, T rhs) { return {lhs.width * rhs, lhs.height * rhs}; }
54 [[nodiscard]] friend inline TSize2 operator/(const TSize2& lhs, T rhs) { return {lhs.width / rhs, lhs.height / rhs}; }
55 };
56
57 extern template struct TSize2<double>;
58
61
62 template<typename T> struct TRect2Edge {
64 T bottom = 0.0;
65
67 T left = 0.0;
68
70 T right = 0.0;
71
73 T top = 0.0;
74 };
75
76 extern template struct TRect2Edge<double>;
77
80
83 template<typename T> struct TRect2 {
84 TPoint2<T> origin = TPoint2<T>(0.0, 0.0);
85 TSize2<T> size = TSize2<T>(0.0, 0.0);
86
87 TRect2() = default;
88 TRect2(const TPoint2<T>& origin, const TSize2<T>& size) : origin(origin), size(size) {}
89 TRect2(const TPoint2<T>& center, const TRect2Edge<T>& edge);
90 TRect2(T x, T y, T width, T height) : origin(x, y), size(width, height) {}
91
93 static TRect2 fromCenterAndSize(const TPoint2<T>& center, const TSize2<T>& size) { return TRect2(center - TPoint2<T>(size / T(2)), size); }
94
96 static TRect2 fromCenterAndSize(const TPoint2<T>& center, T size) { return TRect2(center - size / T(2), TSize2<T>(size, size)); }
97
98 public:
100 [[nodiscard]] bool contains(const TPoint2<T>& point) const;
101
102 [[nodiscard]] bool contains(const TRect2<T>& rect) const;
103
107 [[nodiscard]] bool overlaps(const TRect2& other) const;
108
111 void encapsulate(const TRect2& other);
112
115 void clip(const TRect2& clipRect);
116
117 [[nodiscard]] TRect2 intersection(const TRect2& other) const;
118 [[nodiscard]] bool fullyContained(const TRect2& other) const;
119
121
123 [[nodiscard]] TRect2 inset(T value) const { return inset(TRect2Edge<T>{value, value, value, value}); }
124
126
128 [[nodiscard]] TRect2 outset(T value) const { return outset(TRect2Edge<T>{value, value, value, value}); }
129
137
145
153
156
160
161 [[nodiscard]] TPoint2<T> getTopLeft() const { return origin; }
162
164 void setTopLeft(const TPoint2<T>& topLeft) { origin = topLeft; }
165
166 [[nodiscard]] TPoint2<T> getTopRight() const { return origin + TPoint2<T>(size.width, 0); }
167
169 void setTopRight(const TPoint2<T>& topRight) {
170 size.width = topRight.x - origin.x;
171 origin.y = topRight.y;
172 }
173
174 [[nodiscard]] TPoint2<T> getBottomLeft() const { return origin + TPoint2<T>(0, size.height); }
175
177 void setBottomLeft(const TPoint2<T>& bottomLeft) {
178 origin.x = bottomLeft.x;
179 size.height = bottomLeft.y - origin.y;
180 }
181
182 [[nodiscard]] TPoint2<T> getBottomRight() const { return origin + TPoint2<T>(size.width, size.height); }
183
185 void setBottomRight(const TPoint2<T>& bottomRight) {
186 size.width = bottomRight.x - origin.x;
187 size.height = bottomRight.y - origin.y;
188 }
189
190 friend bool operator==(const TRect2& lhs, const TRect2& rhs) { return lhs.origin == rhs.origin && lhs.size == rhs.size; }
191 friend bool operator!=(const TRect2& lhs, const TRect2& rhs) { return !(rhs == lhs); }
192
197 template<std::size_t I> [[nodiscard]] friend T& get(TRect2& rect) noexcept {
198 static_assert(I <= 3);
199 if constexpr(I == 0) {
200 return rect.origin.x;
201 } else if constexpr(I == 1) {
202 return rect.origin.y;
203 } else if constexpr(I == 2) {
204 return rect.size.width;
205 } else if constexpr(I == 3) {
206 return rect.size.height;
207 }
208 }
209
214 template<std::size_t I> [[nodiscard]] friend const T& get(const TRect2& rect) noexcept {
215 static_assert(I <= 3);
216 if constexpr(I == 0) {
217 return rect.origin.x;
218 } else if constexpr(I == 1) {
219 return rect.origin.y;
220 } else if constexpr(I == 2) {
221 return rect.size.width;
222 } else if constexpr(I == 3) {
223 return rect.size.height;
224 }
225 }
226 };
227
228 extern template struct TRect2<double>;
229
230 template<typename T> [[nodiscard]] constexpr inline T MinX(const TRect2<T>& rect) { return rect.origin.x; }
231 template<typename T> [[nodiscard]] constexpr inline T MinY(const TRect2<T>& rect) { return rect.origin.y; }
232 template<typename T> [[nodiscard]] constexpr inline T Width(const TRect2<T>& rect) { return rect.size.width; }
233 template<typename T> [[nodiscard]] constexpr inline T Height(const TRect2<T>& rect) { return rect.size.height; }
234 template<typename T> [[nodiscard]] constexpr inline T MaxX(const TRect2<T>& rect) { return rect.origin.x + rect.size.width; }
235 template<typename T> [[nodiscard]] constexpr inline T MaxY(const TRect2<T>& rect) { return rect.origin.y + rect.size.height; }
236 template<typename T> [[nodiscard]] constexpr inline T MidX(const TRect2<T>& rect) { return rect.origin.x + rect.size.width / 2; }
237 template<typename T> [[nodiscard]] constexpr inline T MidY(const TRect2<T>& rect) { return rect.origin.y + rect.size.height / 2; }
238
241
250 template<typename T> struct TAffineTransform {
251 TMatrix3<T> raw = glm::identity<TMatrix3<T>>();
252
253 public:
254 TAffineTransform() = default;
255 CE_EXPLICIT(false) TAffineTransform(const TMatrix3<T>& raw) : raw(raw) {}
256
257 public:
274
276 [[nodiscard]] TAffineTransform operator*(const TAffineTransform& rhs) const { return concatenating(rhs); }
277
292
295
297 [[nodiscard]] TAffineTransform operator>>(T angle) { return rotatedBy(angle); }
298
300 [[nodiscard]] TAffineTransform operator<<(T angle) { return rotatedBy(-angle); }
301
313 [[nodiscard]] TAffineTransform scaledBy(T sx, T sy) const { return TAffineTransform(scale(raw, TVector2<T>{sx, sy})); }
314
317
319 [[nodiscard]] TAffineTransform operator*(const TVector2<T>& point) { return scaledBy(point.x, point.y); }
320
322 [[nodiscard]] TAffineTransform operator/(const TVector2<T>& point) { return scaledBy(T(1) / point.x, T(1) / point.y); }
323
325 [[nodiscard]] TAffineTransform operator*(const T& scale) { return scaledBy(scale, scale); }
326
328 [[nodiscard]] TAffineTransform operator/(const T& scale) { return scaledBy(scale, scale); }
329
340 [[nodiscard]] TAffineTransform translatedBy(T x, T y) const { return TAffineTransform(translate(raw, TVector2<T>{x, y})); }
341
344
346 [[nodiscard]] TAffineTransform operator+(const TVector2<T>& point) { return translatedBy(point.x, point.y); }
347
349 [[nodiscard]] TAffineTransform operator-(const TVector2<T>& point) { return translatedBy(-point.x, -point.y); }
350
363 [[nodiscard]] TAffineTransform invert() const { return TAffineTransform(inverse(raw)); }
364
365 public:
371 [[nodiscard]] TPoint2<T> operator()(const TPoint2<T>& point) const { return TPoint2<T>(raw * TVector3<T>(point, T(1))); }
372
378 [[nodiscard]] TSize2<T> operator()(const TSize2<T>& size) const { return TSize2<T>(TVector2<T>(raw * TVector3<T>(TVector2<T>(size), T(0)))); }
379
392 rect.transform(raw);
393 return rect;
394 }
395
396 friend bool operator==(const TAffineTransform& lhs, const TAffineTransform& rhs) { return lhs.raw == rhs.raw; }
397 friend bool operator!=(const TAffineTransform& lhs, const TAffineTransform& rhs) { return !(rhs == lhs); }
398 };
399
402
403} // namespace CeresEngine::inline Math
404
405template<typename T> struct std::tuple_size<CeresEngine::TRect2<T>> : std::integral_constant<std::size_t, 4> {};
406
407template<std::size_t N, typename T> struct std::tuple_element<N, CeresEngine::TRect2<T>> {
408 using type = decltype(get<N>(std::declval<CeresEngine::TRect2<T>>()));
409};
410
411template<typename T> struct std::tuple_size<const CeresEngine::TRect2<T>> : std::tuple_size<CeresEngine::TRect2<T>> {};
412
413template<std::size_t N, typename T> struct std::tuple_element<N, const CeresEngine::TRect2<T>> {
414 using type = decltype(get<N>(std::declval<const CeresEngine::TRect2<T>>()));
415};
416
417template<typename T> struct std::hash<CeresEngine::TSize2<T>> {
418 using Type = CeresEngine::TSize2<T>;
419 size_t operator()(const Type& obj) const { return CeresEngine::hash(obj.width, obj.height); }
420};
421
422template<typename T> struct std::hash<CeresEngine::TRect2Edge<T>> {
423 using Type = CeresEngine::TRect2Edge<T>;
424 size_t operator()(const Type& obj) const { return CeresEngine::hash(obj.origin, obj.size); }
425};
426
427template<typename T> struct std::hash<CeresEngine::TRect2<T>> {
428 using Type = CeresEngine::TRect2<T>;
429 size_t operator()(const Type& obj) const { return CeresEngine::hash(obj.origin, obj.size); }
430};
431
432template<typename T> struct std::hash<CeresEngine::TAffineTransform<T>> {
434 constexpr size_t operator()(const Type& obj) const { return CeresEngine::hash(obj.raw); }
435};
#define CE_EXPLICIT(EXPR)
Definition Macros.hpp:413
Definition Application.hpp:19
@ Width
The view's width is flexible.
@ Height
The view's height is flexible.
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Angle.hpp:20
constexpr T MidX(const TRect2< T > &rect)
Definition Rect.hpp:236
TVector2< double > Vector2
A two dimensional vector with (x, y) coordinates.
Definition Vector.hpp:49
constexpr T MidY(const TRect2< T > &rect)
Definition Rect.hpp:237
constexpr T MaxX(const TRect2< T > &rect)
Definition Rect.hpp:234
TMatrix< 3, 3, T > TMatrix3
A 3x3 type that uses a internal representation of type T
Definition Matrix.hpp:41
TMatrix< 4, 4, T > TMatrix4
A 4x4 type that uses a internal representation of type T
Definition Matrix.hpp:54
constexpr T MinY(const TRect2< T > &rect)
Definition Rect.hpp:231
T angle(const TVector2< T > &v1, const TVector2< T > &v2)
Definition Math.hpp:670
constexpr T MinX(const TRect2< T > &rect)
Definition Rect.hpp:230
constexpr T MaxY(const TRect2< T > &rect)
Definition Rect.hpp:235
TPoint2< double > Point2
A type that contains a point in a two-dimensional coordinate system.
Definition Rect.hpp:24
TVector< 2, T > TVector2
A two dimensional vector type that uses a internal representation of type T
Definition Vector.hpp:45
TVector< 3, T > TVector3
A three dimensional vector type that uses a internal representation of type T
Definition Vector.hpp:80
TMatrix4< T > rotate(const TVector3< T > &angle)
Definition Math.hpp:737
TVector2< T > TPoint2
A type that contains a point in a two-dimensional coordinate system.
Definition Rect.hpp:21
An affine transformation matrix for use in drawing 2D graphics.
Definition Rect.hpp:250
TAffineTransform operator/(const T &scale)
Returns an affine transformation matrix constructed by scaling an existing affine transform.
Definition Rect.hpp:328
TAffineTransform operator<<(T angle)
Returns an affine transformation matrix constructed by rotating an existing affine transform.
Definition Rect.hpp:300
static TAffineTransform translation(T x, T y)
Returns an affine transformation matrix constructed by translating an existing affine transform.
Definition Rect.hpp:343
TAffineTransform operator*(const T &scale)
Returns an affine transformation matrix constructed by scaling an existing affine transform.
Definition Rect.hpp:325
TAffineTransform concatenating(const TAffineTransform &t) const
Returns an affine transformation matrix constructed by combining two existing affine transforms.
Definition Rect.hpp:273
TAffineTransform operator*(const TVector2< T > &point)
Returns an affine transformation matrix constructed by scaling an existing affine transform.
Definition Rect.hpp:319
TAffineTransform translatedBy(T x, T y) const
Returns an affine transformation matrix constructed by translating an existing affine transform.
Definition Rect.hpp:340
static TAffineTransform rotation(T angle)
Returns an affine transformation matrix constructed by rotating an existing affine transform.
Definition Rect.hpp:294
TAffineTransform operator-(const TVector2< T > &point)
Returns an affine transformation matrix constructed by translating an existing affine transform.
Definition Rect.hpp:349
TAffineTransform operator+(const TVector2< T > &point)
Returns an affine transformation matrix constructed by translating an existing affine transform.
Definition Rect.hpp:346
TAffineTransform operator/(const TVector2< T > &point)
Returns an affine transformation matrix constructed by scaling an existing affine transform.
Definition Rect.hpp:322
TRect2< T > operator()(TRect2< T > rect) const
Applies an affine transform to a rectangle.
Definition Rect.hpp:391
TPoint2< T > operator()(const TPoint2< T > &point) const
Returns the point resulting from an affine transformation of an existing point.
Definition Rect.hpp:371
TAffineTransform scaledBy(T sx, T sy) const
Returns an affine transformation matrix constructed by scaling an existing affine transform.
Definition Rect.hpp:313
static TAffineTransform scaling(T sx, T sy)
Returns an affine transformation matrix constructed by scaling an existing affine transform.
Definition Rect.hpp:316
TAffineTransform invert() const
Returns an affine transformation matrix constructed by inverting an existing affine transform.
Definition Rect.hpp:363
TSize2< T > operator()(const TSize2< T > &size) const
Returns the size resulting from an affine transformation of an existing size.
Definition Rect.hpp:378
TAffineTransform operator>>(T angle)
Returns an affine transformation matrix constructed by rotating an existing affine transform.
Definition Rect.hpp:297
TMatrix3< T > raw
Definition Rect.hpp:251
friend bool operator!=(const TAffineTransform &lhs, const TAffineTransform &rhs)
Definition Rect.hpp:397
friend bool operator==(const TAffineTransform &lhs, const TAffineTransform &rhs)
Definition Rect.hpp:396
TAffineTransform operator*(const TAffineTransform &rhs) const
Returns an affine transformation matrix constructed by combining two existing affine transforms.
Definition Rect.hpp:276
TAffineTransform rotatedBy(T angle) const
Returns an affine transformation matrix constructed by rotating an existing affine transform.
Definition Rect.hpp:291
Definition Rect.hpp:62
Represents a 2D rectangle using real values.
Definition Rect.hpp:83
friend T & get(TRect2 &rect) noexcept
Gets a value from the rectangle.
Definition Rect.hpp:197
TPoint2< T > getTopRight() const
Definition Rect.hpp:166
TSize2< T > size
Definition Rect.hpp:85
TRect2 intersection(const TRect2 &other) const
TRect2(const TPoint2< T > &origin, const TSize2< T > &size)
Definition Rect.hpp:88
TPoint2< T > getBottomLeft() const
Definition Rect.hpp:174
bool contains(const TPoint2< T > &point) const
Returns true if the rectangle contains the provided point.
TRect2(T x, T y, T width, T height)
Definition Rect.hpp:90
TRect2 outset(T value) const
Definition Rect.hpp:128
void transform(const TMatrix4< T > &matrix)
Transforms the bounds by the given matrix.
bool overlaps(const TRect2 &other) const
Returns true if the rectangle overlaps the provided rectangle.
void setBottomLeft(const TPoint2< T > &bottomLeft)
Definition Rect.hpp:177
TPoint2< T > getBottomRight() const
Definition Rect.hpp:182
void transform(const TMatrix3< T > &matrix)
Transforms the bounds by the given matrix.
void setTopLeft(const TPoint2< T > &topLeft)
Definition Rect.hpp:164
TPoint2< T > origin
Definition Rect.hpp:84
TRect2 outset(const TRect2Edge< T > &insets) const
void setBottomRight(const TPoint2< T > &bottomRight)
Definition Rect.hpp:185
TRect2 inset(T value) const
Definition Rect.hpp:123
TRect2 inset(const TRect2Edge< T > &insets) const
void encapsulate(const TRect2 &other)
Extends this rectangle so that the provided rectangle is completely contained within it.
TRect2()=default
void setTopRight(const TPoint2< T > &topRight)
Definition Rect.hpp:169
TSize2< T > getHalfSize() const
Extents of the rectangle (distance from center to one of the corners)
bool contains(const TRect2< T > &rect) const
void clip(const TRect2 &clipRect)
Clips current rectangle so that it does not overlap the provided rectangle.
static TRect2 fromCenterAndSize(const TPoint2< T > &center, T size)
Creates a new 2D rect by passing the center and it's size.
Definition Rect.hpp:96
friend bool operator==(const TRect2 &lhs, const TRect2 &rhs)
Definition Rect.hpp:190
TPoint2< T > getCenter() const
Center of the rectangle.
static Vector< TRect2 > getIntersectionGroups(const Vector< TRect2 > &rects)
Computes a set of non-intersecting rects that enclose all given rects.
TPoint2< T > getTopLeft() const
Definition Rect.hpp:161
static TRect2 fromCenterAndSize(const TPoint2< T > &center, const TSize2< T > &size)
Creates a new 2D rect by passing the center and it's size.
Definition Rect.hpp:93
friend bool operator!=(const TRect2 &lhs, const TRect2 &rhs)
Definition Rect.hpp:191
friend const T & get(const TRect2 &rect) noexcept
Gets a value from the rectangle.
Definition Rect.hpp:214
bool fullyContained(const TRect2 &other) const
TRect2(const TPoint2< T > &center, const TRect2Edge< T > &edge)
A structure that contains width and height values.
Definition Rect.hpp:27
friend TSize2 operator-(const TSize2 &lhs, T rhs)
Definition Rect.hpp:51
TSize2(const TVector2< T > &vec)
Definition Rect.hpp:37
TSize2 transform(const TMatrix3< T > &matrix) const
Transforms the size by the given matrix.
friend TSize2 operator+(const TSize2 &lhs, T rhs)
Definition Rect.hpp:48
friend TSize2 operator/(const TSize2 &lhs, T rhs)
Definition Rect.hpp:54
friend TSize2 operator-(const TSize2 &lhs, const TSize2 &rhs)
Definition Rect.hpp:50
TSize2(T width, T height)
Definition Rect.hpp:35
TSize2()=default
friend bool operator!=(const TSize2 &lhs, const TSize2 &rhs)
Definition Rect.hpp:45
friend bool operator==(const TSize2 &lhs, const TSize2 &rhs)
Definition Rect.hpp:44
T width
A width value.
Definition Rect.hpp:29
friend TSize2 operator+(const TSize2 &lhs, const TSize2 &rhs)
Definition Rect.hpp:47
friend TSize2 operator*(const TSize2 &lhs, T rhs)
Definition Rect.hpp:53
T height
A height value.
Definition Rect.hpp:32