Nirtcpp 2.1.0
Nirtcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
aabbox3d.hpp
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in nirtcpp/nirtcpp.hpp
4
5#ifndef NIRT_AABBOX_3D_HPP_INCLUDED
6#define NIRT_AABBOX_3D_HPP_INCLUDED
7
8#include <nirtcpp/core/engine/irrMath.hpp>
9#include <nirtcpp/core/engine/plane3d.hpp>
10#include <nirtcpp/core/engine/line3d.hpp>
11
12namespace nirt
13{
14namespace core
15{
16
18
20template <class T>
22{
23 public:
24
26 aabbox3d(): MinEdge(-1,-1,-1), MaxEdge(1,1,1) {}
28 aabbox3d(const vector3d<T>& min, const vector3d<T>& max): MinEdge(min), MaxEdge(max) {}
30 aabbox3d(const vector3d<T>& init): MinEdge(init), MaxEdge(init) {}
32 aabbox3d(T minx, T miny, T minz, T maxx, T maxy, T maxz): MinEdge(minx, miny, minz), MaxEdge(maxx, maxy, maxz) {}
33
34 // operators
36
38 inline bool operator==(const aabbox3d<T>& other) const { return (MinEdge == other.MinEdge && other.MaxEdge == MaxEdge);}
40
42 inline bool operator!=(const aabbox3d<T>& other) const { return !(MinEdge == other.MinEdge && other.MaxEdge == MaxEdge);}
43
44 // functions
45
47
50 void reset(T x, T y, T z)
51 {
52 MaxEdge.set(x,y,z);
54 }
55
57
58 void reset(const aabbox3d<T>& initValue)
59 {
60 *this = initValue;
61 }
62
64
65 void reset(const vector3d<T>& initValue)
66 {
67 MaxEdge = initValue;
68 MinEdge = initValue;
69 }
70
72
75 {
76 addInternalPoint(p.X, p.Y, p.Z);
77 }
78
80
87
89
93 void addInternalPoint(T x, T y, T z)
94 {
95 if (x>MaxEdge.X) MaxEdge.X = x;
96 if (y>MaxEdge.Y) MaxEdge.Y = y;
97 if (z>MaxEdge.Z) MaxEdge.Z = z;
98
99 if (x<MinEdge.X) MinEdge.X = x;
100 if (y<MinEdge.Y) MinEdge.Y = y;
101 if (z<MinEdge.Z) MinEdge.Z = z;
102 }
103
105
107 {
108 return (MinEdge + MaxEdge) / 2;
109 }
110
112
114 {
115 return MaxEdge - MinEdge;
116 }
117
119
120 T getRadius() const
121 {
122 const T radius = getExtent().getLength() / 2;
123 return radius;
124 }
125
127
129 bool isEmpty() const
130 {
131 return MinEdge.equals ( MaxEdge );
132 }
133
135 T getVolume() const
136 {
137 const vector3d<T> e = getExtent();
138 return e.X * e.Y * e.Z;
139 }
140
142 T getArea() const
143 {
144 const vector3d<T> e = getExtent();
145 return 2*(e.X*e.Y + e.X*e.Z + e.Y*e.Z);
146 }
147
149
150 void getEdges(vector3d<T> *edges) const
151 {
152 const core::vector3d<T> middle = getCenter();
153 const core::vector3d<T> diag = middle - MaxEdge;
154
155 /*
156 Edges are stored in this way:
157 Hey, am I an ascii artist, or what? :) niko.
158 /3--------/7
159 / | / |
160 / | / |
161 1---------5 |
162 | /2- - -|- -6
163 | / | /
164 |/ | /
165 0---------4/
166 */
167
168 edges[0].set(middle.X + diag.X, middle.Y + diag.Y, middle.Z + diag.Z);
169 edges[1].set(middle.X + diag.X, middle.Y - diag.Y, middle.Z + diag.Z);
170 edges[2].set(middle.X + diag.X, middle.Y + diag.Y, middle.Z - diag.Z);
171 edges[3].set(middle.X + diag.X, middle.Y - diag.Y, middle.Z - diag.Z);
172 edges[4].set(middle.X - diag.X, middle.Y + diag.Y, middle.Z + diag.Z);
173 edges[5].set(middle.X - diag.X, middle.Y - diag.Y, middle.Z + diag.Z);
174 edges[6].set(middle.X - diag.X, middle.Y + diag.Y, middle.Z - diag.Z);
175 edges[7].set(middle.X - diag.X, middle.Y - diag.Y, middle.Z - diag.Z);
176 }
177
179
180 void repair()
181 {
182 T t;
183
184 if (MinEdge.X > MaxEdge.X)
185 { t=MinEdge.X; MinEdge.X = MaxEdge.X; MaxEdge.X=t; }
186 if (MinEdge.Y > MaxEdge.Y)
187 { t=MinEdge.Y; MinEdge.Y = MaxEdge.Y; MaxEdge.Y=t; }
188 if (MinEdge.Z > MaxEdge.Z)
189 { t=MinEdge.Z; MinEdge.Z = MaxEdge.Z; MaxEdge.Z=t; }
190 }
191
192 // Check if MaxEdge > MinEdge
193 bool isValid() const
194 {
195 if (MinEdge.X > MaxEdge.X) return false;
196 if (MinEdge.Y > MaxEdge.Y) return false;
197 if (MinEdge.Z > MaxEdge.Z) return false;
198
199 return true;
200 }
201
203
209 {
210 f32 inv = 1.0f - d;
211 return aabbox3d<T>((other.MinEdge*inv) + (MinEdge*d),
212 (other.MaxEdge*inv) + (MaxEdge*d));
213 }
214
216
219 bool isPointInside(const vector3d<T>& p) const
220 {
221 return (p.X >= MinEdge.X && p.X <= MaxEdge.X &&
222 p.Y >= MinEdge.Y && p.Y <= MaxEdge.Y &&
223 p.Z >= MinEdge.Z && p.Z <= MaxEdge.Z);
224 }
225
227
231 {
232 return (p.X > MinEdge.X && p.X < MaxEdge.X &&
233 p.Y > MinEdge.Y && p.Y < MaxEdge.Y &&
234 p.Z > MinEdge.Z && p.Z < MaxEdge.Z);
235 }
236
238
241 bool isFullInside(const aabbox3d<T>& other) const
242 {
243 return (MinEdge.X >= other.MinEdge.X && MinEdge.Y >= other.MinEdge.Y && MinEdge.Z >= other.MinEdge.Z &&
244 MaxEdge.X <= other.MaxEdge.X && MaxEdge.Y <= other.MaxEdge.Y && MaxEdge.Z <= other.MaxEdge.Z);
245 }
246
249 {
251
253 return out;
254
258
262
263 return out;
264 }
265
267
271 {
272 return (MinEdge.X <= other.MaxEdge.X && MinEdge.Y <= other.MaxEdge.Y && MinEdge.Z <= other.MaxEdge.Z &&
273 MaxEdge.X >= other.MinEdge.X && MaxEdge.Y >= other.MinEdge.Y && MaxEdge.Z >= other.MinEdge.Z);
274 }
275
277
280 {
281 return intersectsWithLine(line.getMiddle(), line.getVector().normalize(),
282 (T)(line.getLength() * 0.5));
283 }
284
286
291 const vector3d<T>& linevect, T halflength) const
292 {
293 const vector3d<T> e = getExtent() * (T)0.5;
294 const vector3d<T> t = getCenter() - linemiddle;
295
296 if ((fabs(t.X) > e.X + halflength * fabs(linevect.X)) ||
297 (fabs(t.Y) > e.Y + halflength * fabs(linevect.Y)) ||
298 (fabs(t.Z) > e.Z + halflength * fabs(linevect.Z)) )
299 return false;
300
301 T r = e.Y * (T)fabs(linevect.Z) + e.Z * (T)fabs(linevect.Y);
302 if (fabs(t.Y*linevect.Z - t.Z*linevect.Y) > r )
303 return false;
304
305 r = e.X * (T)fabs(linevect.Z) + e.Z * (T)fabs(linevect.X);
306 if (fabs(t.Z*linevect.X - t.X*linevect.Z) > r )
307 return false;
308
309 r = e.X * (T)fabs(linevect.Y) + e.Y * (T)fabs(linevect.X);
310 if (fabs(t.X*linevect.Y - t.Y*linevect.X) > r)
311 return false;
312
313 return true;
314 }
315
317
322 {
325
326 if (plane.Normal.X > (T)0)
327 {
328 nearPoint.X = MinEdge.X;
329 farPoint.X = MaxEdge.X;
330 }
331
332 if (plane.Normal.Y > (T)0)
333 {
334 nearPoint.Y = MinEdge.Y;
335 farPoint.Y = MaxEdge.Y;
336 }
337
338 if (plane.Normal.Z > (T)0)
339 {
340 nearPoint.Z = MinEdge.Z;
341 farPoint.Z = MaxEdge.Z;
342 }
343
344 if (plane.Normal.dotProduct(nearPoint) + plane.D > (T)0)
345 return ISREL3D_FRONT;
346
347 if (plane.Normal.dotProduct(farPoint) + plane.D > (T)0)
348 return ISREL3D_CLIPPED;
349
350 return ISREL3D_BACK;
351 }
352
355
358};
359
364
365} // end namespace core
366} // end namespace nirt
367
368#endif
369
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
bool isPointTotalInside(const vector3d< T > &p) const
Determines if a point is within this box and not its borders.
Definition aabbox3d.hpp:230
bool isPointInside(const vector3d< T > &p) const
Determines if a point is within this box.
Definition aabbox3d.hpp:219
void addInternalPoint(const vector3d< T > &p)
Adds a point to the bounding box.
Definition aabbox3d.hpp:74
bool intersectsWithBox(const aabbox3d< T > &other) const
Determines if the axis-aligned box intersects with another axis-aligned box.
Definition aabbox3d.hpp:270
bool operator!=(const aabbox3d< T > &other) const
Inequality operator.
Definition aabbox3d.hpp:42
void getEdges(vector3d< T > *edges) const
Stores all 8 edges of the box into an array.
Definition aabbox3d.hpp:150
bool isEmpty() const
Check if the box is empty.
Definition aabbox3d.hpp:129
bool intersectsWithLine(const line3d< T > &line) const
Tests if the box intersects with a line.
Definition aabbox3d.hpp:279
vector3d< T > MinEdge
The near edge.
Definition aabbox3d.hpp:354
aabbox3d< T > getInterpolated(const aabbox3d< T > &other, f32 d) const
Calculates a new interpolated bounding box.
Definition aabbox3d.hpp:208
vector3d< T > MaxEdge
The far edge.
Definition aabbox3d.hpp:357
void repair()
Repairs the box.
Definition aabbox3d.hpp:180
void reset(const vector3d< T > &initValue)
Resets the bounding box to a one-point box.
Definition aabbox3d.hpp:65
T getRadius() const
Get radius of the bounding sphere.
Definition aabbox3d.hpp:120
bool intersectsWithLine(const vector3d< T > &linemiddle, const vector3d< T > &linevect, T halflength) const
Tests if the box intersects with a line.
Definition aabbox3d.hpp:290
void reset(const aabbox3d< T > &initValue)
Resets the bounding box.
Definition aabbox3d.hpp:58
void addInternalBox(const aabbox3d< T > &b)
Adds another bounding box.
Definition aabbox3d.hpp:82
bool isFullInside(const aabbox3d< T > &other) const
Check if this box is completely inside the 'other' box.
Definition aabbox3d.hpp:241
T getArea() const
Get the surface area of the box in squared units.
Definition aabbox3d.hpp:142
void reset(T x, T y, T z)
Resets the bounding box to a one-point box.
Definition aabbox3d.hpp:50
EIntersectionRelation3D classifyPlaneRelation(const plane3d< T > &plane) const
Classifies a relation with a plane.
Definition aabbox3d.hpp:321
aabbox3d(const vector3d< T > &min, const vector3d< T > &max)
Constructor with min edge and max edge.
Definition aabbox3d.hpp:28
aabbox3d(const vector3d< T > &init)
Constructor with only one point.
Definition aabbox3d.hpp:30
T getVolume() const
Get the volume enclosed by the box in cubed units.
Definition aabbox3d.hpp:135
vector3d< T > getCenter() const
Get center of the bounding box.
Definition aabbox3d.hpp:106
bool operator==(const aabbox3d< T > &other) const
Equality operator.
Definition aabbox3d.hpp:38
vector3d< T > getExtent() const
Get extent of the box (maximal distance of two points in the box)
Definition aabbox3d.hpp:113
aabbox3d< T > intersect(const aabbox3d< T > &other) const
Returns the intersection of this box with another, if possible.
Definition aabbox3d.hpp:248
aabbox3d()
Default Constructor.
Definition aabbox3d.hpp:26
aabbox3d(T minx, T miny, T minz, T maxx, T maxy, T maxz)
Constructor with min edge and max edge as single values, not vectors.
Definition aabbox3d.hpp:32
void addInternalPoint(T x, T y, T z)
Adds a point to the bounding box.
Definition aabbox3d.hpp:93
3d vector template class with lots of operators and methods.
Definition vector3d.hpp:23
T Y
Y coordinate of the vector.
Definition vector3d.hpp:453
T X
X coordinate of the vector.
Definition vector3d.hpp:450
T Z
Z coordinate of the vector.
Definition vector3d.hpp:456
const T & max_(const T &a, const T &b)
returns maximum of two values. Own implementation to get rid of the STL (VS6 problems)
Definition irrMath.hpp:135
EIntersectionRelation3D
Enumeration for intersection relations of 3d objects.
Definition plane3d.hpp:18
const T & min_(const T &a, const T &b)
returns minimum of two values. Own implementation to get rid of the STL (VS6 problems)
Definition irrMath.hpp:121
As of Nirtcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11
float f32
32 bit floating point variable.
Definition irrTypes.hpp:110

Nirtcpp    @cppfx.xyz

Utxcpp    utx::print