Branch data Line data Source code
1 : : #pragma once
2 : :
3 : : /*
4 : : MIT License
5 : :
6 : : Copyright (c) 2014-2024 Stephane Cuillerdier (aka aiekick)
7 : :
8 : : Permission is hereby granted, free of charge, to any person obtaining a copy
9 : : of this software and associated documentation files (the "Software"), to deal
10 : : in the Software without restriction, including without limitation the rights
11 : : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 : : copies of the Software, and to permit persons to whom the Software is
13 : : furnished to do so, subject to the following conditions:
14 : :
15 : : The above copyright notice and this permission notice shall be included in all
16 : : copies or substantial portions of the Software.
17 : :
18 : : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 : : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 : : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 : : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 : : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 : : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 : : SOFTWARE.
25 : : */
26 : :
27 : : // ezAABBCC is part of the ezLibs project : https://github.com/aiekick/ezLibs.git
28 : :
29 : : namespace ez {
30 : :
31 : : template <typename T>
32 : : struct AABBCC // copy of b2AABB struct
33 : : {
34 : : vec3<T> lowerBound; ///< the lower left vertex
35 : : vec3<T> upperBound; ///< the upper right vertex
36 : :
37 : 20 : AABBCC() : lowerBound((T)0), upperBound((T)0) {
38 : 20 : }
39 : : AABBCC(vec3<T> vlowerBound, vec3<T> vUpperBound)
40 : : //: lowerBound(vlowerBound), upperBound(vUpperBound)
41 : 1 : {
42 : 1 : lowerBound.x = mini(vlowerBound.x, vUpperBound.x);
43 : 1 : lowerBound.y = mini(vlowerBound.y, vUpperBound.y);
44 : 1 : lowerBound.z = mini(vlowerBound.z, vUpperBound.z);
45 : 1 : upperBound.x = maxi(vlowerBound.x, vUpperBound.x);
46 : 1 : upperBound.y = maxi(vlowerBound.y, vUpperBound.y);
47 : 1 : upperBound.z = maxi(vlowerBound.z, vUpperBound.z);
48 : 1 : }
49 : :
50 : : /// Verify that the bounds are sorted.
51 : : // bool IsValid() const;
52 : :
53 : : /// Add a vector to this vector.
54 : : void operator+=(const vec3<T>& v) {
55 : : lowerBound += v;
56 : : upperBound += v;
57 : : }
58 : :
59 : : /// Subtract a vector from this vector.
60 : : void operator-=(const vec3<T>& v) {
61 : : lowerBound -= v;
62 : : upperBound -= v;
63 : : }
64 : :
65 : : /// Multiply this vector by a scalar.
66 : : void operator*=(T a) {
67 : : lowerBound *= a;
68 : : upperBound *= a;
69 : : }
70 : :
71 : : /// Divide this vector by a scalar.
72 : : void operator/=(T a) {
73 : : lowerBound /= a;
74 : : upperBound /= a;
75 : : }
76 : :
77 : : /// Get the center of the AABB.
78 : 20 : vec3<T> GetCenter() const {
79 : 20 : return (lowerBound + upperBound) / (T)2;
80 : 20 : }
81 : :
82 : : /// Get the extents of the AABB (half-widths).
83 : : vec3<T> GetExtents() const {
84 : : return (upperBound - lowerBound) / (T)2;
85 : : }
86 : :
87 : : /// Get the perimeter length
88 : : T GetPerimeter() const {
89 : : const float wx = upperBound.x - lowerBound.x;
90 : : const float wy = upperBound.y - lowerBound.y;
91 : : const float wz = upperBound.z - lowerBound.z;
92 : : return (T)2 * (wx + wy + wz);
93 : : }
94 : :
95 : : /// Combine an AABB into this one.
96 : : void Combine(const AABBCC<T>& aabb) {
97 : : lowerBound = mini(lowerBound, aabb.lowerBound);
98 : : upperBound = maxi(upperBound, aabb.upperBound);
99 : : }
100 : :
101 : : /// Combine two AABBs into this one.
102 : : void Combine(const AABBCC<T>& aabb1, const AABBCC<T>& aabb2) {
103 : : lowerBound = mini(aabb1.lowerBound, aabb2.lowerBound);
104 : : upperBound = maxi(aabb1.upperBound, aabb2.upperBound);
105 : : }
106 : :
107 : : /// Combine a point into this one.
108 : 3228840 : void Combine(const vec3<T>& pt) {
109 : 3228840 : lowerBound.x = mini(lowerBound.x, pt.x);
110 : 3228840 : lowerBound.y = mini(lowerBound.y, pt.y);
111 : 3228840 : lowerBound.z = mini(lowerBound.z, pt.z);
112 : 3228840 : upperBound.x = maxi(upperBound.x, pt.x);
113 : 3228840 : upperBound.y = maxi(upperBound.y, pt.y);
114 : 3228840 : upperBound.z = maxi(upperBound.z, pt.z);
115 : 3228840 : }
116 : :
117 : : /// Does this aabb contain the provided AABB.
118 : : bool Contains(const AABBCC<T>& aabb) const {
119 : : bool result = true;
120 : : result &= lowerBound.x <= aabb.lowerBound.x;
121 : : result &= lowerBound.y <= aabb.lowerBound.y;
122 : : result &= lowerBound.z <= aabb.lowerBound.z;
123 : : result &= aabb.upperBound.x <= upperBound.x;
124 : : result &= aabb.upperBound.y <= upperBound.y;
125 : : result &= aabb.upperBound.z <= upperBound.z;
126 : : return result;
127 : : }
128 : :
129 : : /// Does this aabb contain the provided vec2<T>.
130 : : bool ContainsPoint(const vec3<T>& pt) const {
131 : : bool result = true;
132 : : result &= lowerBound.x <= pt.x;
133 : : result &= lowerBound.y <= pt.y;
134 : : result &= lowerBound.z <= pt.z;
135 : : result &= pt.x <= upperBound.x;
136 : : result &= pt.y <= upperBound.y;
137 : : result &= pt.z <= upperBound.z;
138 : : return result;
139 : : }
140 : :
141 : : bool Intersects(const AABBCC<T>& other) const {
142 : : bool result = true;
143 : : result |= lowerBound.x <= other.lowerBound.x;
144 : : result |= lowerBound.y <= other.lowerBound.y;
145 : : result |= lowerBound.z <= other.lowerBound.z;
146 : : result |= other.upperBound.x <= upperBound.x;
147 : : result |= other.upperBound.y <= upperBound.y;
148 : : result |= other.upperBound.z <= upperBound.z;
149 : : return result;
150 : : }
151 : :
152 : 21 : const vec3<T> Size() const {
153 : 21 : return vec3<T>(upperBound - lowerBound);
154 : 21 : }
155 : : };
156 : : typedef AABBCC<double> dAABBCC;
157 : : typedef AABBCC<float> fAABBCC;
158 : :
159 : : /// Add a float to a AABBCC<T>.
160 : : template <typename T>
161 : : inline AABBCC<T> operator+(const AABBCC<T>& v, float f) {
162 : : return AABBCC<T>(v.lowerBound + f, v.upperBound + f);
163 : : }
164 : :
165 : : /// Add a AABBCC<T> to a AABBCC<T>.
166 : : template <typename T>
167 : : inline AABBCC<T> operator+(const AABBCC<T>& v, AABBCC<T> f) {
168 : : return AABBCC<T>(v.lowerBound + f.lowerBound, v.upperBound + f.upperBound);
169 : : }
170 : :
171 : : /// Substract a float from a AABBCC<T>.
172 : : template <typename T>
173 : : inline AABBCC<T> operator-(const AABBCC<T>& v, float f) {
174 : : return AABBCC<T>(v.lowerBound - f, v.upperBound - f);
175 : : }
176 : :
177 : : /// Substract a AABBCC<T> to a AABBCC<T>.
178 : : template <typename T>
179 : : inline AABBCC<T> operator-(const AABBCC<T>& v, AABBCC<T> f) {
180 : : return AABBCC<T>(v.lowerBound - f.lowerBound, v.upperBound - f.upperBound);
181 : : }
182 : :
183 : : /// Multiply a float with a AABBCC<T>.
184 : : template <typename T>
185 : : inline AABBCC<T> operator*(const AABBCC<T>& v, float f) {
186 : : return AABBCC<T>(v.lowerBound * f, v.upperBound * f);
187 : : }
188 : :
189 : : /// Multiply a AABBCC<T> with a AABBCC<T>.
190 : : template <typename T>
191 : : inline AABBCC<T> operator*(const AABBCC<T>& v, AABBCC<T> f) {
192 : : return AABBCC<T>(v.lowerBound * f.lowerBound, v.upperBound * f.upperBound);
193 : : }
194 : :
195 : : /// Divide a AABBCC<T> by a float.
196 : : template <typename T>
197 : : inline AABBCC<T> operator/(const AABBCC<T>& v, float f) {
198 : : return AABBCC<T>(v.lowerBound / f, v.upperBound / f);
199 : : }
200 : :
201 : : /// Divide a AABBCC<T> by a float.
202 : : template <typename T>
203 : : inline AABBCC<T> operator/(AABBCC<T>& v, float f) {
204 : : return AABBCC<T>(v.lowerBound / f, v.upperBound / f);
205 : : }
206 : :
207 : : /// Divide a AABBCC<T> by a AABBCC<T>.
208 : : template <typename T>
209 : : inline AABBCC<T> operator/(const AABBCC<T>& v, AABBCC<T> f) {
210 : : return AABBCC<T>(v.lowerBound / f.lowerBound, v.upperBound / f.upperBound);
211 : : }
212 : :
213 : :
214 : : } // namespace ez
|