Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
aabbox2f.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022-2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#ifndef JAU_AABBOX2F_HPP_
25#define JAU_AABBOX2F_HPP_
26
27#include <jau/math/vec2f.hpp>
28
29namespace jau::math::geom {
30
31 /** \addtogroup Math
32 *
33 * @{
34 */
35
36 /**
37 * Axis Aligned Bounding Box. Defined by two 3D coordinates (low and high)
38 * The low being the the lower left corner of the box, and the high being the upper
39 * right corner of the box.
40 *
41 * A few references for collision detection, intersections:
42 * - http://www.realtimerendering.com/intersections.html
43 * - http://www.codercorner.com/RayAABB.cpp
44 * - http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter0.htm
45 * - http://realtimecollisiondetection.net/files/levine_swept_sat.txt
46 */
47 class AABBox2f {
48 public:
49 /** bottom left (low) */
51 /** top right (high) */
53
54 /**
55 * Create an Axis Aligned bounding box (AABBox)
56 * where the low and and high MAX float Values.
57 */
58 AABBox2f() noexcept {
59 reset();
60 }
61
62 /**
63 * Create an AABBox with given bl (low) and tr (high)
64 */
65 AABBox2f(const Point2f& bl_, const Point2f& tr_) noexcept
66 : bl( bl_ ), tr( tr_ ) {
67 }
68
69 constexpr AABBox2f(const AABBox2f& o) noexcept = default;
70 constexpr AABBox2f(AABBox2f&& o) noexcept = default;
71 AABBox2f& operator=(const AABBox2f&) noexcept = default;
72 AABBox2f& operator=(AABBox2f&&) noexcept = default;
73
74 /**
75 * Reset this box to the inverse low/high, allowing the next {@link #resize(float, float, float)} command to hit.
76 * @return this AABBox for chaining
77 */
78 AABBox2f& reset() noexcept {
83 return *this;
84 }
85
86 /**
87 * Resize the AABBox to encapsulate another AABox
88 * @param newBox AABBox to be encapsulated in
89 * @return this AABBox for chaining
90 */
91 AABBox2f& resize(const AABBox2f& o) noexcept {
92 /** test bl (low) */
93 if (o.bl.x < bl.x) {
94 bl.x = o.bl.x;
95 }
96 if (o.bl.y < bl.y) {
97 bl.y = o.bl.y;
98 }
99
100 /** test tr (high) */
101 if (o.tr.x > tr.x) {
102 tr.x = o.tr.x;
103 }
104 if (o.tr.y > tr.y) {
105 tr.y = o.tr.y;
106 }
107
108 return *this;
109 }
110
111 /**
112 * Resize the AABBox to encapsulate the passed coordinates.
113 * @param x x-axis coordinate value
114 * @param y y-axis coordinate value
115 * @return this AABBox for chaining
116 */
117 AABBox2f& resize(float x, float y) noexcept {
118 /** test bl (low) */
119 if (x < bl.x) {
120 bl.x = x;
121 }
122 if (y < bl.y) {
123 bl.y = y;
124 }
125
126 /** test tr (high) */
127 if (x > tr.x) {
128 tr.x = x;
129 }
130 if (y > tr.y) {
131 tr.y = y;
132 }
133
134 return *this;
135 }
136
137 /**
138 * Resize the AABBox to encapsulate the passed point.
139 * @param x x-axis coordinate value
140 * @param y y-axis coordinate value
141 * @return this AABBox for chaining
142 */
143 AABBox2f& resize(const Point2f& p) noexcept {
144 return resize(p.x, p.y);
145 }
146
147 AABBox2f box() const noexcept { return *this; }
148
149 /**
150 * Check if the point is bounded/contained by this AABBox
151 * @return true if {x, y} belongs to (low.x, high.x) and y belong to (low.y, high.y)
152 */
153 bool contains(const float x, const float y) const noexcept {
154 return !( x<bl.x || x>tr.x ||
155 y<bl.y || y>tr.y );
156 }
157
158 /**
159 * Check if the point is bounded/contained by this AABBox
160 * @return true if p belongs to (low.x, high.x) and y belong to (low.y, high.y)
161 */
162 bool contains(const Point2f& p) const noexcept { return contains(p.x, p.y); }
163
164 bool intersects(const AABBox2f& o) const noexcept {
165 return !( tr.x < o.bl.x ||
166 tr.y < o.bl.y ||
167 bl.x > o.tr.x ||
168 bl.y > o.tr.y );
169 }
170
171#if 0
172 bool intersection(aabbox_t a, aabbox_t b){
173 if(aabbox_t::intersects(a)){
174 return false;
175 }
176
177 }
178#endif
179
180 std::string toString() const noexcept {
181 return "aabb[bl " + bl.toString() +
182 ", tr " + tr.toString() +
183 "]"; }
184 };
185
186 /**@}*/
187
188} // namespace jau::math::geom
189
190#endif /* JAU_AABBOX2F_HPP_ */
value_type x
Definition: vec2f.hpp:68
std::string toString() const noexcept
Definition: vec2f.hpp:203
value_type y
Definition: vec2f.hpp:69
Axis Aligned Bounding Box.
Definition: aabbox2f.hpp:47
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:91
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:117
bool contains(const Point2f &p) const noexcept
Check if the point is bounded/contained by this AABBox.
Definition: aabbox2f.hpp:162
Point2f bl
bottom left (low)
Definition: aabbox2f.hpp:50
Point2f tr
top right (high)
Definition: aabbox2f.hpp:52
AABBox2f() noexcept
Create an Axis Aligned bounding box (AABBox) where the low and and high MAX float Values.
Definition: aabbox2f.hpp:58
AABBox2f & reset() noexcept
Reset this box to the inverse low/high, allowing the next resize(float, float, float) command to hit.
Definition: aabbox2f.hpp:78
std::string toString() const noexcept
Definition: aabbox2f.hpp:180
AABBox2f(const Point2f &bl_, const Point2f &tr_) noexcept
Create an AABBox with given bl (low) and tr (high)
Definition: aabbox2f.hpp:65
bool intersects(const AABBox2f &o) const noexcept
Definition: aabbox2f.hpp:164
AABBox2f box() const noexcept
Definition: aabbox2f.hpp:147
AABBox2f & resize(const Point2f &p) noexcept
Resize the AABBox to encapsulate the passed point.
Definition: aabbox2f.hpp:143
bool contains(const float x, const float y) const noexcept
Check if the point is bounded/contained by this AABBox.
Definition: aabbox2f.hpp:153
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
Definition: base_math.hpp:191