jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
aabbox2f.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11#ifndef JAU_AABBOX2F_HPP_
12#define JAU_AABBOX2F_HPP_
13
14#include <jau/math/vec2f.hpp>
15
16namespace jau::math::geom {
17
18 /** \addtogroup Math
19 *
20 * @{
21 */
22
23 /**
24 * Axis Aligned Bounding Box. Defined by two 3D coordinates (low and high)
25 * The low being the the lower left corner of the box, and the high being the upper
26 * right corner of the box.
27 *
28 * A few references for collision detection, intersections:
29 * - http://www.realtimerendering.com/intersections.html
30 * - http://www.codercorner.com/RayAABB.cpp
31 * - http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter0.htm
32 * - http://realtimecollisiondetection.net/files/levine_swept_sat.txt
33 */
34 class AABBox2f {
35 public:
36 /** bottom left (low) */
38 /** top right (high) */
40
41 /**
42 * Create an Axis Aligned bounding box (AABBox)
43 * where the low and and high MAX float Values.
44 */
45 AABBox2f() noexcept {
46 reset();
47 }
48
49 /**
50 * Create an AABBox with given bl (low) and tr (high)
51 */
52 AABBox2f(const Point2f& bl_, const Point2f& tr_) noexcept
53 : bl( bl_ ), tr( tr_ ) {
54 }
55
56 constexpr AABBox2f(const AABBox2f& o) noexcept = default;
57 constexpr AABBox2f(AABBox2f&& o) noexcept = default;
58 AABBox2f& operator=(const AABBox2f&) noexcept = default;
59 AABBox2f& operator=(AABBox2f&&) noexcept = default;
60
61 /**
62 * Reset this box to the inverse low/high, allowing the next {@link #resize(float, float, float)} command to hit.
63 * @return this AABBox for chaining
64 */
65 AABBox2f& reset() noexcept {
66 bl.x = std::numeric_limits<float>::max();
67 bl.y = std::numeric_limits<float>::max();
68 tr.x = -std::numeric_limits<float>::max();
69 tr.y = -std::numeric_limits<float>::max();
70 return *this;
71 }
72
73 /**
74 * Resize the AABBox to encapsulate another AABox
75 * @param newBox AABBox to be encapsulated in
76 * @return this AABBox for chaining
77 */
78 AABBox2f& resize(const AABBox2f& o) noexcept {
79 /** test bl (low) */
80 if (o.bl.x < bl.x) {
81 bl.x = o.bl.x;
82 }
83 if (o.bl.y < bl.y) {
84 bl.y = o.bl.y;
85 }
86
87 /** test tr (high) */
88 if (o.tr.x > tr.x) {
89 tr.x = o.tr.x;
90 }
91 if (o.tr.y > tr.y) {
92 tr.y = o.tr.y;
93 }
94
95 return *this;
96 }
97
98 /**
99 * Resize the AABBox to encapsulate the passed coordinates.
100 * @param x x-axis coordinate value
101 * @param y y-axis coordinate value
102 * @return this AABBox for chaining
103 */
104 AABBox2f& resize(float x, float y) noexcept {
105 /** test bl (low) */
106 if (x < bl.x) {
107 bl.x = x;
108 }
109 if (y < bl.y) {
110 bl.y = y;
111 }
112
113 /** test tr (high) */
114 if (x > tr.x) {
115 tr.x = x;
116 }
117 if (y > tr.y) {
118 tr.y = y;
119 }
120
121 return *this;
122 }
123
124 /**
125 * Resize the AABBox to encapsulate the passed point.
126 * @param x x-axis coordinate value
127 * @param y y-axis coordinate value
128 * @return this AABBox for chaining
129 */
130 AABBox2f& resize(const Point2f& p) noexcept {
131 return resize(p.x, p.y);
132 }
133
134 AABBox2f box() const noexcept { return *this; }
135
136 /**
137 * Check if the point is bounded/contained by this AABBox
138 * @return true if {x, y} belongs to (low.x, high.x) and y belong to (low.y, high.y)
139 */
140 bool contains(const float x, const float y) const noexcept {
141 return bl.x<=x && x<=tr.x &&
142 bl.y<=y && y<=tr.y;
143 }
144
145 /**
146 * Check if the point is bounded/contained by this AABBox
147 * @return true if p belongs to (low.x, high.x) and y belong to (low.y, high.y)
148 */
149 bool contains(const Point2f& p) const noexcept { return contains(p.x, p.y); }
150
151 bool intersects(const AABBox2f& o) const noexcept {
152 /**
153 * Traditional boolean equation leads to multiple branches,
154 * using max/min approach allowing for branch-less optimizations.
155 *
156 return !( tr.x < o.bl.x ||
157 tr.y < o.bl.y ||
158 bl.x > o.tr.x ||
159 bl.y > o.tr.y );
160 */
161 const Point2f lo = max(bl, o.bl);
162 const Point2f hi = min(tr, o.tr);
163 return lo.x <= hi.x && lo.y <= hi.y;
164 }
165
166#if 0
167 bool intersection(aabbox_t a, aabbox_t b){
168 if(aabbox_t::intersects(a)){
169 return false;
170 }
171
172 }
173#endif
174
175 /** Returns whether this aabbox2f fully contains given aabbox2f. */
176 bool contains(const AABBox2f& o) const noexcept {
177 return tr.x >= o.tr.x &&
178 tr.y >= o.tr.y &&
179 bl.x <= o.bl.x &&
180 bl.y <= o.bl.y;
181 }
182 std::string toString() const noexcept {
183 return "aabb[bl " + bl.toString() +
184 ", tr " + tr.toString() +
185 "]"; }
186 };
187
188 /**@}*/
189
190} // namespace jau::math::geom
191
192#endif /* JAU_AABBOX2F_HPP_ */
value_type x
Definition vec2f.hpp:75
value_type y
Definition vec2f.hpp:76
AABBox2f & operator=(AABBox2f &&) noexcept=default
AABBox2f & operator=(const AABBox2f &) noexcept=default
constexpr AABBox2f(AABBox2f &&o) noexcept=default
AABBox2f & resize(const AABBox2f &o) noexcept
Resize the AABBox to encapsulate another AABox.
Definition aabbox2f.hpp:78
constexpr AABBox2f(const AABBox2f &o) noexcept=default
AABBox2f & resize(float x, float y) noexcept
Resize the AABBox to encapsulate the passed coordinates.
Definition aabbox2f.hpp:104
bool contains(const Point2f &p) const noexcept
Check if the point is bounded/contained by this AABBox.
Definition aabbox2f.hpp:149
Point2f bl
bottom left (low)
Definition aabbox2f.hpp:37
Point2f tr
top right (high)
Definition aabbox2f.hpp:39
AABBox2f() noexcept
Create an Axis Aligned bounding box (AABBox) where the low and and high MAX float Values.
Definition aabbox2f.hpp:45
AABBox2f & reset() noexcept
Reset this box to the inverse low/high, allowing the next resize(float, float, float) command to hit.
Definition aabbox2f.hpp:65
bool contains(const AABBox2f &o) const noexcept
Returns whether this aabbox2f fully contains given aabbox2f.
Definition aabbox2f.hpp:176
std::string toString() const noexcept
Definition aabbox2f.hpp:182
AABBox2f(const Point2f &bl_, const Point2f &tr_) noexcept
Create an AABBox with given bl (low) and tr (high)
Definition aabbox2f.hpp:52
bool intersects(const AABBox2f &o) const noexcept
Definition aabbox2f.hpp:151
AABBox2f box() const noexcept
Definition aabbox2f.hpp:134
AABBox2f & resize(const Point2f &p) noexcept
Resize the AABBox to encapsulate the passed point.
Definition aabbox2f.hpp:130
bool contains(const float x, const float y) const noexcept
Check if the point is bounded/contained by this AABBox.
Definition aabbox2f.hpp:140
constexpr Vector2F< T > max(const Vector2F< T > &lhs, const Vector2F< T > &rhs) noexcept
Definition vec2f.hpp:375
constexpr Vector2F< T > min(const Vector2F< T > &lhs, const Vector2F< T > &rhs) noexcept
Definition vec2f.hpp:369
Point2F< float > Point2f
Definition vec2f.hpp:402