Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Rect.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_RECT_H
25 #define GF_RECT_H
26 
27 #include <cstddef>
28 #include <algorithm>
29 #include <limits>
30 #include <tuple>
31 
32 #include "Anchor.h"
33 #include "CoreApi.h"
34 #include "Quarter.h"
35 #include "Vector.h"
36 #include "VectorOps.h"
37 
38 namespace gf {
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 inline namespace v1 {
41 #endif
42 
86  template<typename T>
87  struct Rect {
93  constexpr Rect() noexcept
94  : min(gf::Zero)
95  , max(gf::Zero)
96  {
97  }
98 
106  static constexpr Rect<T> fromMinMax(Vector<T, 2> min, Vector<T, 2> max) noexcept {
107  return Rect<T>(min, max);
108  }
109 
117  static constexpr Rect<T> fromPositionSize(Vector<T, 2> position, Vector<T, 2> size) noexcept {
118  return Rect<T>(position, position + size);
119  }
120 
128  static constexpr Rect<T> fromCenterSize(Vector<T, 2> center, Vector<T, 2> size) noexcept {
129  return Rect<T>(center - size / T(2), center + size / T(2));
130  }
131 
137  static constexpr Rect<T> empty() noexcept {
138  return Rect<T>(Vector<T, 2>(std::numeric_limits<T>::max()), Vector<T, 2>(std::numeric_limits<T>::lowest()));
139  }
140 
148  constexpr Vector<T, 2> getPosition() const noexcept {
149  return min;
150  }
151 
157  constexpr Vector<T, 2> getSize() const noexcept {
158  return max - min;
159  }
160 
166  constexpr bool isEmpty() const noexcept {
167  return min.x >= max.x || min.y >= max.y;
168  }
169 
175  constexpr Vector<T, 2> getCenter() const noexcept {
176  return min + (max - min) / 2;
177  }
178 
185  constexpr bool contains(Vector<T, 2> point) const noexcept {
186  return min.x <= point.x && point.x < max.x && min.y <= point.y && point.y < max.y;
187  }
188 
195  constexpr bool contains(const Rect& other) const noexcept {
196  return min.x <= other.min.x && other.max.x <= max.x && min.y <= other.min.y && other.max.y <= max.y;
197  }
198 
205  constexpr bool intersects(const Rect& other) const noexcept {
206  return min.x < other.max.x && other.min.x < max.x && min.y < other.max.y && other.min.y < max.y;
207  }
208 
216  constexpr bool intersects(const Rect& other, Rect& result) const noexcept {
217  if (!intersects(other)) {
218  return false;
219  }
220 
221  result = getIntersection(other);
222  return true;
223  }
224 
231  constexpr Rect getIntersection(const Rect& other) const noexcept {
232  Rect res;
233 
234  res.min.x = std::max(min.x, other.min.x);
235  res.min.y = std::max(min.y, other.min.y);
236  res.max.x = std::min(max.x, other.max.x);
237  res.max.y = std::min(max.y, other.max.y);
238 
239  return res;
240  }
241 
248  constexpr T getIntersectionVolume(const Rect& other) const noexcept {
249  auto xMin = std::max(min.x, other.min.x);
250  auto xMax = std::min(max.x, other.max.x);
251 
252  if (xMin >= xMax) {
253  return T(0);
254  }
255 
256  auto yMin = std::max(min.y, other.min.y);
257  auto yMax = std::min(max.y, other.max.y);
258 
259  if (yMin >= yMax) {
260  return T(0);
261  }
262 
263  return (xMax - xMin) * (yMax - yMin);
264  }
265 
272  constexpr T getIntersectionExtentLength(const Rect& other) const noexcept {
273  auto xMin = std::max(min.x, other.min.x);
274  auto xMax = std::min(max.x, other.max.x);
275 
276  if (xMin >= xMax) {
277  return T(0);
278  }
279 
280  auto yMin = std::max(min.y, other.min.y);
281  auto yMax = std::min(max.y, other.max.y);
282 
283  if (yMin >= yMax) {
284  return T(0);
285  }
286 
287  return (xMax - xMin) + (yMax - yMin);
288  }
289 
295  constexpr void extend(const T (&point)[2]) noexcept {
296  min.x = std::min(min.x, point[0]);
297  min.y = std::min(min.y, point[1]);
298  max.x = std::max(max.x, point[0]);
299  max.y = std::max(max.y, point[1]);
300  }
301 
307  constexpr void extend(Vector<T, 2> point) noexcept {
308  min.x = std::min(min.x, point.x);
309  min.y = std::min(min.y, point.y);
310  max.x = std::max(max.x, point.x);
311  max.y = std::max(max.y, point.y);
312  }
313 
319  constexpr void extend(const Rect& other) noexcept {
320  min.x = std::min(min.x, other.min.x);
321  min.y = std::min(min.y, other.min.y);
322  max.x = std::max(max.x, other.max.x);
323  max.y = std::max(max.y, other.max.y);
324  }
325 
331  constexpr Rect getExtended(const Rect& other) const noexcept {
332  Rect res(*this);
333  res.extend(other);
334  return res;
335  }
336 
342  constexpr T getVolume() const noexcept {
343  return (max.x - min.x) * (max.y - min.y);
344  }
345 
351  constexpr T getExtentLength() const noexcept {
352  return (max.x - min.x) + (max.y - min.y);
353  }
354 
360  constexpr T getMinimumEdge() const noexcept {
361  return std::min(max.x - min.x, max.y - min.y);
362  }
363 
371  constexpr Rect grow(T value) const noexcept {
372  return Rect(min - value, max + value);
373  }
374 
382  constexpr Rect shrink(T value) const noexcept {
383  return Rect(min + value, max - value);
384  }
385 
389  constexpr void normalize() noexcept {
390  if (min.x > max.x) { std::swap(min.x, max.x); }
391  if (min.y > max.y) { std::swap(min.y, max.y); }
392  }
393 
400  constexpr Vector<T, 2> getPositionFromAnchor(Anchor anchor) const noexcept {
401  switch (anchor) {
402  case Anchor::TopLeft:
403  return this->min;
404  case Anchor::TopCenter:
405  return { (this->min.x + this->max.x) / T(2), this->min.y };
406  case Anchor::TopRight:
407  return { this->max.x, this->min.y };
408  case Anchor::CenterLeft:
409  return { this->min.x, (this->min.y + this->max.y) / T(2) };
410  case Anchor::Center:
411  return this->getCenter();
412  case Anchor::CenterRight:
413  return { this->max.x, (this->min.y + this->max.y) / T(2) };
414  case Anchor::BottomLeft:
415  return { this->min.x, this->max.y };
417  return { (this->min.x + this->max.x) / T(2), this->max.y };
418  case Anchor::BottomRight:
419  return this->max;
420  }
421 
422  return this->min;
423  }
424 
430  constexpr Vector<T, 2> getTopLeft() const noexcept {
431  return getPositionFromAnchor(Anchor::TopLeft);
432  }
433 
439  constexpr Vector<T, 2> getTopRight() const noexcept {
440  return getPositionFromAnchor(Anchor::TopRight);
441  }
442 
448  constexpr Vector<T, 2> getBottomLeft() const noexcept {
449  return getPositionFromAnchor(Anchor::BottomLeft);
450  }
451 
457  constexpr Vector<T, 2> getBottomRight() const noexcept {
458  return getPositionFromAnchor(Anchor::BottomRight);
459  }
460 
466  constexpr T getWidth() const noexcept {
467  return this->max.x - this->min.x;
468  }
469 
475  constexpr T getHeight() const noexcept {
476  return this->max.y - this->min.y;
477  }
478 
481 
482  private:
483  constexpr Rect(Vector<T, 2> min, Vector<T, 2> max) noexcept
484  : min(min)
485  , max(max)
486  {
487  }
488  };
489 
497 
505 
512  using RectI = Rect<int>;
513 
521 
529 
530 // MSVC does not like extern template
531 #ifndef _MSC_VER
532  extern template struct GF_CORE_API Rect<float>;
533  extern template struct GF_CORE_API Rect<double>;
534  extern template struct GF_CORE_API Rect<int>;
535  extern template struct GF_CORE_API Rect<unsigned>;
536 #endif
537 
546  template<typename T>
547  inline
548  bool operator==(const Rect<T>& lhs, const Rect<T>& rhs) {
549  return lhs.min == rhs.min && lhs.max == rhs.max;
550  }
551 
560  template<typename T>
561  inline
562  bool operator!=(const Rect<T>& lhs, const Rect<T>& rhs) {
563  return !(lhs == rhs);
564  }
565 
573  template<typename T>
574  constexpr
575  Rect<T> computeBoxQuarter(const Rect<T>& other, Quarter quarter) {
576  Vector<T, 2> size = (other.max - other.min) / 2;
577 
578  switch (quarter) {
579  case Quarter::UpperLeft:
580  return Rect<T>::fromMinMax(other.min, other.max - size);
581  case Quarter::UpperRight:
582  return Rect<T>::fromMinMax({ other.min.x + size.width, other.min.y }, { other.max.x, other.max.y - size.height });
583  case Quarter::LowerRight:
584  return Rect<T>::fromMinMax(other.min + size, other.max);
585  case Quarter::LowerLeft:
586  return Rect<T>::fromMinMax({ other.min.x, other.min.y + size.height }, { other.max.x - size.width, other.max.y });
587  }
588 
589  return other;
590  }
591 
600  template<typename Archive, typename T>
601  Archive& operator|(Archive& ar, Rect<T>& rect) {
602  return ar | rect.min.x | rect.min.y | rect.max.x | rect.max.y;
603  }
604 
605 
606 #ifndef DOXYGEN_SHOULD_SKIP_THIS
607 }
608 #endif
609 }
610 
611 #endif // GF_RECT_H
T x
First coordinate in the (x,y) representation.
Definition: Vector.h:514
constexpr bool contains(Vector< T, 2 > point) const noexcept
Check if a point is inside the rectangle.
Definition: Rect.h:185
constexpr T getIntersectionExtentLength(const Rect &other) const noexcept
Get the extent length of the intersection.
Definition: Rect.h:272
constexpr Rect< T > computeBoxQuarter(const Rect< T > &other, Quarter quarter)
Divide a rectangle in quarters.
Definition: Rect.h:575
constexpr Vector< T, 2 > getBottomLeft() const noexcept
Get the bottom left corner.
Definition: Rect.h:448
Vector< T, 2 > max
The maximum point of the rectangle.
Definition: Rect.h:480
bool operator!=(const Rect< T > &lhs, const Rect< T > &rhs)
Inequality operator.
Definition: Rect.h:562
constexpr bool isEmpty() const noexcept
Check if the rectangle is empty.
Definition: Rect.h:166
constexpr T getExtentLength() const noexcept
Get the extent length of the rectangle.
Definition: Rect.h:351
constexpr Vector< T, 2 > getPositionFromAnchor(Anchor anchor) const noexcept
Get a position from the rectangle and an anchor.
Definition: Rect.h:400
Vector< T, 2 > min
The minimum point of the rectangle.
Definition: Rect.h:479
constexpr bool intersects(const Rect &other) const noexcept
Check if two rectangles interset.
Definition: Rect.h:205
constexpr Rect shrink(T value) const noexcept
Shrink the rectangle.
Definition: Rect.h:382
constexpr Rect getExtended(const Rect &other) const noexcept
Get the rectangle extended by another rectangle.
Definition: Rect.h:331
Upper-right quarter.
Definition: Quarter.h:35
Archive & operator|(Archive &ar, Rect< T > &rect)
Serialize a rectangle.
Definition: Rect.h:601
Lower-right quarter.
Definition: Quarter.h:36
static constexpr Rect< T > fromMinMax(Vector< T, 2 > min, Vector< T, 2 > max) noexcept
Create a rectangle from a min point and a max point.
Definition: Rect.h:106
T y
Second coordinate in the (x,y) representation.
Definition: Vector.h:525
constexpr Vector< T, 2 > getPosition() const noexcept
Get the position of the rectangle.
Definition: Rect.h:148
T width
First coordinate in the size representation.
Definition: Vector.h:517
constexpr void extend(const T(&point)[2]) noexcept
Extend the rectangle with a point (as array)
Definition: Rect.h:295
T height
Second coordinate in the size representation.
Definition: Vector.h:528
constexpr Vector< T, 2 > getTopRight() const noexcept
Get the top right corner.
Definition: Rect.h:439
Utility class for manipulating 2D axis aligned rectangles.
Definition: Rect.h:87
The namespace for gf classes.
Definition: Action.h:35
constexpr Vector< T, 2 > getCenter() const noexcept
Get the center of the rectangle.
Definition: Rect.h:175
static constexpr Rect< T > fromPositionSize(Vector< T, 2 > position, Vector< T, 2 > size) noexcept
Create a rectangle from a position (top-left) and a size.
Definition: Rect.h:117
Quarter
A quarter in a square.
Definition: Quarter.h:33
static constexpr Rect< T > empty() noexcept
Create an empty rectangle.
Definition: Rect.h:137
constexpr void extend(Vector< T, 2 > point) noexcept
Extend the rectangle with a point.
Definition: Rect.h:307
constexpr T getMinimumEdge() const noexcept
Get the minimum edge of the rectangle.
Definition: Rect.h:360
constexpr void extend(const Rect &other) noexcept
Extend the rectangle with a rectangle.
Definition: Rect.h:319
constexpr bool intersects(const Rect &other, Rect &result) const noexcept
Check if two rectangles interset and get the intersetion rectangle.
Definition: Rect.h:216
Upper-left quarter.
Definition: Quarter.h:34
constexpr void normalize() noexcept
Ensures that min coordinates are less than max coordinates.
Definition: Rect.h:389
A 2D vector.
Definition: Vector.h:316
constexpr T getWidth() const noexcept
Get the width of the rectangle.
Definition: Rect.h:466
constexpr T getVolume() const noexcept
Get the volume of the rectangle.
Definition: Rect.h:342
constexpr Rect() noexcept
Default constructor.
Definition: Rect.h:93
constexpr Vector< T, 2 > getTopLeft() const noexcept
Get the top left corner.
Definition: Rect.h:430
static constexpr Rect< T > fromCenterSize(Vector< T, 2 > center, Vector< T, 2 > size) noexcept
Create a rectangle from a center and a size.
Definition: Rect.h:128
constexpr Rect getIntersection(const Rect &other) const noexcept
Compute the intersection of two rectangles.
Definition: Rect.h:231
constexpr Vector< T, 2 > getBottomRight() const noexcept
Get the bottom right corner.
Definition: Rect.h:457
Anchor
An anchor of a box.
Definition: Anchor.h:38
constexpr Rect grow(T value) const noexcept
Grow the rectangle.
Definition: Rect.h:371
constexpr T getIntersectionVolume(const Rect &other) const noexcept
Get the volume of the intersection.
Definition: Rect.h:248
constexpr Vector< T, 2 > getSize() const noexcept
Get the size of the rectangle.
Definition: Rect.h:157
bool operator==(const Rect< T > &lhs, const Rect< T > &rhs)
Equality operator.
Definition: Rect.h:548
constexpr T getHeight() const noexcept
Get the height of the rectangle.
Definition: Rect.h:475
constexpr bool contains(const Rect &other) const noexcept
Check if a rectangle is totally inside the rectangle.
Definition: Rect.h:195
Lower-left quarter.
Definition: Quarter.h:37