NAP
rect.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External Includes
8 #include <utility/dllexport.h>
9 #include <rtti/rtti.h>
10 #include <glm/glm.hpp>
11 
12 namespace nap
13 {
14  namespace math
15  {
19  class NAPAPI Rect final
20  {
21  RTTI_ENABLE()
22  public:
26  constexpr Rect() = default;
27 
35  constexpr Rect(float x, float y, float width, float height) :
36  mMinPosition({ x, y }), mMaxPosition(mMinPosition + glm::vec2(width, height)) {};
37 
43  constexpr Rect(const glm::vec2& min, const glm::vec2& max) :
44  mMinPosition(min), mMaxPosition(max) {}
45 
51  constexpr Rect(glm::vec2&& min, glm::vec2&& max) :
52  mMinPosition(std::move(min)), mMaxPosition(std::move(max)) {}
53 
54  // Equal-to operator overload
55  bool operator==(const Rect& other) const;
56 
57  // Not-equal-to operator overload
58  bool operator!=(const Rect& other) const;
59 
63  float getWidth() const;
64 
68  float getHeight() const;
69 
73  bool hasWidth() const;
74 
78  bool hasHeight() const;
79 
83  bool inside(const glm::vec2& point) const;
84 
88  const glm::vec2& getMin() const { return mMinPosition; }
89 
93  const glm::vec2& getMax() const { return mMaxPosition; }
94 
95  glm::vec2 mMinPosition = { 0.0f, 0.0f }; // min x,y position
96  glm::vec2 mMaxPosition = { 0.0f, 0.0f }; // max x,y position
97  };
98 
99  // Static rectangles
100  static constexpr Rect topRightRect { { 0.0f, 0.0f }, { 1.0f, 1.0f} };
101  static constexpr Rect topLeftRect { { -1.0f, 0.0f }, { 0.0f, 1.0f} };
102  static constexpr Rect bottomLeftRect { { -1.0f, -1.0f }, { 0.0f, 0.0f} };
103  static constexpr Rect bottomRightRect { { 0.0f, -1.0f }, { 1.0f, 0.0f} };
104  static constexpr Rect centeredRect { { -0.5f, -0.5f }, { 0.5f, 0.5f} };
105  }
106 }
nap::math::Rect::getMin
const glm::vec2 & getMin() const
Definition: rect.h:88
nap::math::max
T max(T left, T right)
Definition: mathutils.h:337
nap::math::Rect
Definition: rect.h:19
nap::math::Rect::Rect
constexpr Rect(float x, float y, float width, float height)
Definition: rect.h:35
nap::math::min
T min(T left, T right)
Definition: mathutils.h:331
nap::math::Rect::Rect
constexpr Rect(const glm::vec2 &min, const glm::vec2 &max)
Definition: rect.h:43
nap::math::Rect::getMax
const glm::vec2 & getMax() const
Definition: rect.h:93
nap::math::Rect::Rect
constexpr Rect(glm::vec2 &&min, glm::vec2 &&max)
Definition: rect.h:51
nap
Definition: templateapp.h:17