Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Rect.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "Portability.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  auto xMin = min.x;
297  auto xMax = max.x;
298  std::tie(min.x, max.x) = std::minmax({ xMin, xMax, point[0] });
299 
300  auto yMin = min.y;
301  auto yMax = max.y;
302  std::tie(min.y, max.y) = std::minmax({ yMin, yMax, point[1] });
303  }
304 
310  constexpr void extend(Vector<T, 2> point) noexcept {
311  auto xMin = min.x;
312  auto xMax = max.x;
313  std::tie(min.x, max.x) = std::minmax({ xMin, xMax, point.x });
314 
315  auto yMin = min.y;
316  auto yMax = max.y;
317  std::tie(min.y, max.y) = std::minmax({ yMin, yMax, point.y });
318  }
319 
325  constexpr void extend(const Rect& other) noexcept {
326  min.x = std::min(min.x, other.min.x);
327  min.y = std::min(min.y, other.min.y);
328  max.x = std::max(max.x, other.max.x);
329  max.y = std::max(max.y, other.max.y);
330  }
331 
337  constexpr Rect getExtended(const Rect& other) const noexcept {
338  Rect res(*this);
339  res.extend(other);
340  return res;
341  }
342 
348  constexpr T getVolume() const noexcept {
349  return (max.x - min.x) * (max.y - min.y);
350  }
351 
357  constexpr T getExtentLength() const noexcept {
358  return (max.x - min.x) + (max.y - min.y);
359  }
360 
366  constexpr T getMinimumEdge() const noexcept {
367  return std::min(max.x - min.x, max.y - min.y);
368  }
369 
377  constexpr Rect grow(T value) const noexcept {
378  return Rect(min - value, max + value);
379  }
380 
388  constexpr Rect shrink(T value) const noexcept {
389  return Rect(min + value, max - value);
390  }
391 
395  constexpr void normalize() noexcept {
396  if (min.x > max.x) { std::swap(min.x, max.x); }
397  if (min.y > max.y) { std::swap(min.y, max.y); }
398  }
399 
406  constexpr Vector<T, 2> getPositionFromAnchor(Anchor anchor) const noexcept {
407  switch (anchor) {
408  case Anchor::TopLeft:
409  return this->min;
410  case Anchor::TopCenter:
411  return { (this->min.x + this->max.x) / T(2), this->min.y };
412  case Anchor::TopRight:
413  return { this->max.x, this->min.y };
414  case Anchor::CenterLeft:
415  return { this->min.x, (this->min.y + this->max.y) / T(2) };
416  case Anchor::Center:
417  return this->getCenter();
418  case Anchor::CenterRight:
419  return { this->max.x, (this->min.y + this->max.y) / T(2) };
420  case Anchor::BottomLeft:
421  return { this->min.x, this->max.y };
423  return { (this->min.x + this->max.x) / T(2), this->max.y };
424  case Anchor::BottomRight:
425  return this->max;
426  }
427 
428  return this->min;
429  }
430 
436  constexpr Vector<T, 2> getTopLeft() const noexcept {
437  return getPositionFromAnchor(Anchor::TopLeft);
438  }
439 
445  constexpr Vector<T, 2> getTopRight() const noexcept {
446  return getPositionFromAnchor(Anchor::TopRight);
447  }
448 
454  constexpr Vector<T, 2> getBottomLeft() const noexcept {
455  return getPositionFromAnchor(Anchor::BottomLeft);
456  }
457 
463  constexpr Vector<T, 2> getBottomRight() const noexcept {
464  return getPositionFromAnchor(Anchor::BottomRight);
465  }
466 
472  constexpr T getWidth() const noexcept {
473  return this->max.x - this->min.x;
474  }
475 
481  constexpr T getHeight() const noexcept {
482  return this->max.y - this->min.y;
483  }
484 
487 
488  private:
489  constexpr Rect(Vector<T, 2> min, Vector<T, 2> max) noexcept
490  : min(min)
491  , max(max)
492  {
493  }
494  };
495 
503 
511 
518  using RectI = Rect<int>;
519 
527 
535 
536 // MSVC does not like extern template
537 #ifndef _MSC_VER
538  extern template struct GF_API Rect<float>;
539  extern template struct GF_API Rect<double>;
540  extern template struct GF_API Rect<int>;
541  extern template struct GF_API Rect<unsigned>;
542 #endif
543 
552  template<typename T>
553  inline
554  bool operator==(const Rect<T>& lhs, const Rect<T>& rhs) {
555  return lhs.min == rhs.min && lhs.max == rhs.max;
556  }
557 
566  template<typename T>
567  inline
568  bool operator!=(const Rect<T>& lhs, const Rect<T>& rhs) {
569  return !(lhs == rhs);
570  }
571 
579  template<typename T>
580  constexpr
581  Rect<T> computeBoxQuarter(const Rect<T>& other, Quarter quarter) {
582  Vector<T, 2> size = (other.max - other.min) / 2;
583 
584  switch (quarter) {
585  case Quarter::UpperLeft:
586  return Rect<T>::fromMinMax(other.min, other.max - size);
587  case Quarter::UpperRight:
588  return Rect<T>::fromMinMax({ other.min.x + size.width, other.min.y }, { other.max.x, other.max.y - size.height });
589  case Quarter::LowerRight:
590  return Rect<T>::fromMinMax(other.min + size, other.max);
591  case Quarter::LowerLeft:
592  return Rect<T>::fromMinMax({ other.min.x, other.min.y + size.height }, { other.max.x - size.width, other.max.y });
593  }
594 
595  return other;
596  }
597 
606  template<typename Archive, typename T>
607  Archive& operator|(Archive& ar, Rect<T>& rect) {
608  return ar | rect.min.x | rect.min.y | rect.max.x | rect.max.y;
609  }
610 
611 
612 #ifndef DOXYGEN_SHOULD_SKIP_THIS
613 }
614 #endif
615 }
616 
617 #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
Upper-right quarter.
Definition: Quarter.h:35
constexpr T getIntersectionExtentLength(const Rect &other) const noexcept
Get the extent length of the intersection.
Definition: Rect.h:272
constexpr Vector< T, 2 > getBottomLeft() const noexcept
Get the bottom left corner.
Definition: Rect.h:454
Vector< T, 2 > max
The maximum point of the rectangle.
Definition: Rect.h:486
bool operator!=(const Rect< T > &lhs, const Rect< T > &rhs)
Inequality operator.
Definition: Rect.h:568
constexpr bool isEmpty() const noexcept
Check if the rectangle is empty.
Definition: Rect.h:166
constexpr Rect< T > computeBoxQuarter(const Rect< T > &other, Quarter quarter)
Divide a rectangle in quarters.
Definition: Rect.h:581
constexpr T getExtentLength() const noexcept
Get the extent length of the rectangle.
Definition: Rect.h:357
constexpr Vector< T, 2 > getPositionFromAnchor(Anchor anchor) const noexcept
Get a position from the rectangle and an anchor.
Definition: Rect.h:406
Vector< T, 2 > min
The minimum point of the rectangle.
Definition: Rect.h:485
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:388
constexpr Rect getExtended(const Rect &other) const noexcept
Get the rectangle extended by another rectangle.
Definition: Rect.h:337
Lower-right quarter.
Definition: Quarter.h:36
Archive & operator|(Archive &ar, Rect< T > &rect)
Serialize a rectangle.
Definition: Rect.h:607
Upper-left quarter.
Definition: Quarter.h:34
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:445
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
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:310
constexpr T getMinimumEdge() const noexcept
Get the minimum edge of the rectangle.
Definition: Rect.h:366
constexpr void extend(const Rect &other) noexcept
Extend the rectangle with a rectangle.
Definition: Rect.h:325
constexpr bool intersects(const Rect &other, Rect &result) const noexcept
Check if two rectangles interset and get the intersetion rectangle.
Definition: Rect.h:216
Lower-left quarter.
Definition: Quarter.h:37
constexpr void normalize() noexcept
Ensures that min coordinates are less than max coordinates.
Definition: Rect.h:395
Quarter
A quarter in a square.
Definition: Quarter.h:33
A 2D vector.
Definition: Vector.h:316
constexpr T getWidth() const noexcept
Get the width of the rectangle.
Definition: Rect.h:472
constexpr T getVolume() const noexcept
Get the volume of the rectangle.
Definition: Rect.h:348
constexpr Rect() noexcept
Default constructor.
Definition: Rect.h:93
constexpr Vector< T, 2 > getTopLeft() const noexcept
Get the top left corner.
Definition: Rect.h:436
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:463
Anchor
An anchor of a box.
Definition: Anchor.h:38
constexpr Rect grow(T value) const noexcept
Grow the rectangle.
Definition: Rect.h:377
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:554
constexpr T getHeight() const noexcept
Get the height of the rectangle.
Definition: Rect.h:481
constexpr bool contains(const Rect &other) const noexcept
Check if a rectangle is totally inside the rectangle.
Definition: Rect.h:195