Loading...
Searching...
No Matches
aabbox3d.h
Go to the documentation of this file.
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __IRR_AABBOX_3D_H_INCLUDED__
#define __IRR_AABBOX_3D_H_INCLUDED__
#include "irrMath.h"
#include "plane3d.h"
#include "line3d.h"
namespace irr
{
namespace core
{
template <class T>
class aabbox3d
{
public:
aabbox3d(): MinEdge(-1,-1,-1), MaxEdge(1,1,1) {}
aabbox3d(const vector3d<T>& min, const vector3d<T>& max): MinEdge(min), MaxEdge(max) {}
aabbox3d(const vector3d<T>& init): MinEdge(init), MaxEdge(init) {}
aabbox3d(T minx, T miny, T minz, T maxx, T maxy, T maxz): MinEdge(minx, miny, minz), MaxEdge(maxx, maxy, maxz) {}
// operators
inline bool operator==(const aabbox3d<T>& other) const { return (MinEdge == other.MinEdge && other.MaxEdge == MaxEdge);}
inline bool operator!=(const aabbox3d<T>& other) const { return !(MinEdge == other.MinEdge && other.MaxEdge == MaxEdge);}
// functions
void reset(T x, T y, T z)
{
MaxEdge.set(x,y,z);
MinEdge = MaxEdge;
}
void reset(const aabbox3d<T>& initValue)
{
*this = initValue;
}
void reset(const vector3d<T>& initValue)
{
MaxEdge = initValue;
MinEdge = initValue;
}
void addInternalPoint(const vector3d<T>& p)
{
addInternalPoint(p.X, p.Y, p.Z);
}
void addInternalBox(const aabbox3d<T>& b)
{
addInternalPoint(b.MaxEdge);
addInternalPoint(b.MinEdge);
}
void addInternalPoint(T x, T y, T z)
{
if (x>MaxEdge.X) MaxEdge.X = x;
if (y>MaxEdge.Y) MaxEdge.Y = y;
if (z>MaxEdge.Z) MaxEdge.Z = z;
if (x<MinEdge.X) MinEdge.X = x;
if (y<MinEdge.Y) MinEdge.Y = y;
if (z<MinEdge.Z) MinEdge.Z = z;
}
vector3d<T> getCenter() const
{
return (MinEdge + MaxEdge) / 2;
}
vector3d<T> getExtent() const
{
return MaxEdge - MinEdge;
}
bool isEmpty() const
{
return MinEdge.equals ( MaxEdge );
}
T getVolume() const
{
const vector3d<T> e = getExtent();
return e.X * e.Y * e.Z;
}
T getArea() const
{
const vector3d<T> e = getExtent();
return 2*(e.X*e.Y + e.X*e.Z + e.Y*e.Z);
}
void getEdges(vector3d<T> *edges) const
{
const core::vector3d<T> middle = getCenter();
const core::vector3d<T> diag = middle - MaxEdge;
/*
Edges are stored in this way:
Hey, am I an ascii artist, or what? :) niko.
/3--------/7
/ | / |
/ | / |
1---------5 |
| /2- - -|- -6
| / | /
|/ | /
0---------4/
*/
edges[0].set(middle.X + diag.X, middle.Y + diag.Y, middle.Z + diag.Z);
edges[1].set(middle.X + diag.X, middle.Y - diag.Y, middle.Z + diag.Z);
edges[2].set(middle.X + diag.X, middle.Y + diag.Y, middle.Z - diag.Z);
edges[3].set(middle.X + diag.X, middle.Y - diag.Y, middle.Z - diag.Z);
edges[4].set(middle.X - diag.X, middle.Y + diag.Y, middle.Z + diag.Z);
edges[5].set(middle.X - diag.X, middle.Y - diag.Y, middle.Z + diag.Z);
edges[6].set(middle.X - diag.X, middle.Y + diag.Y, middle.Z - diag.Z);
edges[7].set(middle.X - diag.X, middle.Y - diag.Y, middle.Z - diag.Z);
}
void repair()
{
T t;
if (MinEdge.X > MaxEdge.X)
{ t=MinEdge.X; MinEdge.X = MaxEdge.X; MaxEdge.X=t; }
if (MinEdge.Y > MaxEdge.Y)
{ t=MinEdge.Y; MinEdge.Y = MaxEdge.Y; MaxEdge.Y=t; }
if (MinEdge.Z > MaxEdge.Z)
{ t=MinEdge.Z; MinEdge.Z = MaxEdge.Z; MaxEdge.Z=t; }
}
aabbox3d<T> getInterpolated(const aabbox3d<T>& other, f32 d) const
{
f32 inv = 1.0f - d;
return aabbox3d<T>((other.MinEdge*inv) + (MinEdge*d),
(other.MaxEdge*inv) + (MaxEdge*d));
}
bool isPointInside(const vector3d<T>& p) const
{
return (p.X >= MinEdge.X && p.X <= MaxEdge.X &&
p.Y >= MinEdge.Y && p.Y <= MaxEdge.Y &&
p.Z >= MinEdge.Z && p.Z <= MaxEdge.Z);
}
bool isPointTotalInside(const vector3d<T>& p) const
{
return (p.X > MinEdge.X && p.X < MaxEdge.X &&
p.Y > MinEdge.Y && p.Y < MaxEdge.Y &&
p.Z > MinEdge.Z && p.Z < MaxEdge.Z);
}
bool isFullInside(const aabbox3d<T>& other) const
{
return (MinEdge.X >= other.MinEdge.X && MinEdge.Y >= other.MinEdge.Y && MinEdge.Z >= other.MinEdge.Z &&
MaxEdge.X <= other.MaxEdge.X && MaxEdge.Y <= other.MaxEdge.Y && MaxEdge.Z <= other.MaxEdge.Z);
}
bool intersectsWithBox(const aabbox3d<T>& other) const
{
return (MinEdge.X <= other.MaxEdge.X && MinEdge.Y <= other.MaxEdge.Y && MinEdge.Z <= other.MaxEdge.Z &&
MaxEdge.X >= other.MinEdge.X && MaxEdge.Y >= other.MinEdge.Y && MaxEdge.Z >= other.MinEdge.Z);
}
bool intersectsWithLine(const line3d<T>& line) const
{
return intersectsWithLine(line.getMiddle(), line.getVector().normalize(),
(T)(line.getLength() * 0.5));
}
bool intersectsWithLine(const vector3d<T>& linemiddle,
const vector3d<T>& linevect, T halflength) const
{
const vector3d<T> e = getExtent() * (T)0.5;
const vector3d<T> t = getCenter() - linemiddle;
if ((fabs(t.X) > e.X + halflength * fabs(linevect.X)) ||
(fabs(t.Y) > e.Y + halflength * fabs(linevect.Y)) ||
(fabs(t.Z) > e.Z + halflength * fabs(linevect.Z)) )
return false;
T r = e.Y * (T)fabs(linevect.Z) + e.Z * (T)fabs(linevect.Y);
if (fabs(t.Y*linevect.Z - t.Z*linevect.Y) > r )
return false;
r = e.X * (T)fabs(linevect.Z) + e.Z * (T)fabs(linevect.X);
if (fabs(t.Z*linevect.X - t.X*linevect.Z) > r )
return false;
r = e.X * (T)fabs(linevect.Y) + e.Y * (T)fabs(linevect.X);
if (fabs(t.X*linevect.Y - t.Y*linevect.X) > r)
return false;
return true;
}
EIntersectionRelation3D classifyPlaneRelation(const plane3d<T>& plane) const
{
vector3d<T> nearPoint(MaxEdge);
vector3d<T> farPoint(MinEdge);
if (plane.Normal.X > (T)0)
{
nearPoint.X = MinEdge.X;
farPoint.X = MaxEdge.X;
}
if (plane.Normal.Y > (T)0)
{
nearPoint.Y = MinEdge.Y;
farPoint.Y = MaxEdge.Y;
}
if (plane.Normal.Z > (T)0)
{
nearPoint.Z = MinEdge.Z;
farPoint.Z = MaxEdge.Z;
}
if (plane.Normal.dotProduct(nearPoint) + plane.D > (T)0)
return ISREL3D_FRONT;
if (plane.Normal.dotProduct(farPoint) + plane.D > (T)0)
return ISREL3D_CLIPPED;
return ISREL3D_BACK;
}
vector3d<T> MinEdge;
vector3d<T> MaxEdge;
};
typedef aabbox3d<f32> aabbox3df;
typedef aabbox3d<s32> aabbox3di;
} // end namespace core
} // end namespace irr
#endif
irr::core::aabbox3d
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.h:22
irr::core::aabbox3d::isPointTotalInside
bool isPointTotalInside(const vector3d< T > &p) const
Determines if a point is within this box and not its borders.
Definition aabbox3d.h:212
irr::core::aabbox3d::addInternalPoint
void addInternalPoint(T x, T y, T z)
Adds a point to the bounding box.
Definition aabbox3d.h:93
irr::core::aabbox3d::aabbox3d
aabbox3d(const vector3d< T > &init)
Constructor with only one point.
Definition aabbox3d.h:30
irr::core::aabbox3d::getInterpolated
aabbox3d< T > getInterpolated(const aabbox3d< T > &other, f32 d) const
Calculates a new interpolated bounding box.
Definition aabbox3d.h:190
irr::core::aabbox3d::addInternalPoint
void addInternalPoint(const vector3d< T > &p)
Adds a point to the bounding box.
Definition aabbox3d.h:74
irr::core::aabbox3d< f32 >::MaxEdge
vector3d< f32 > MaxEdge
Definition aabbox3d.h:320
irr::core::aabbox3d::getCenter
vector3d< T > getCenter() const
Get center of the bounding box.
Definition aabbox3d.h:106
irr::core::aabbox3d::reset
void reset(const aabbox3d< T > &initValue)
Resets the bounding box.
Definition aabbox3d.h:58
irr::core::aabbox3d::intersectsWithBox
bool intersectsWithBox(const aabbox3d< T > &other) const
Determines if the axis-aligned box intersects with another axis-aligned box.
Definition aabbox3d.h:233
irr::core::aabbox3d::aabbox3d
aabbox3d(const vector3d< T > &min, const vector3d< T > &max)
Constructor with min edge and max edge.
Definition aabbox3d.h:28
irr::core::aabbox3d< f32 >::MinEdge
vector3d< f32 > MinEdge
Definition aabbox3d.h:317
irr::core::aabbox3d::getVolume
T getVolume() const
Get the volume enclosed by the box in cubed units.
Definition aabbox3d.h:127
irr::core::aabbox3d::addInternalBox
void addInternalBox(const aabbox3d< T > &b)
Adds another bounding box.
Definition aabbox3d.h:82
irr::core::aabbox3d::isFullInside
bool isFullInside(const aabbox3d< T > &other) const
Check if this box is completely inside the 'other' box.
Definition aabbox3d.h:223
irr::core::aabbox3d::getArea
T getArea() const
Get the surface area of the box in squared units.
Definition aabbox3d.h:134
irr::core::aabbox3d::repair
void repair()
Repairs the box.
Definition aabbox3d.h:172
irr::core::aabbox3d::operator!=
bool operator!=(const aabbox3d< T > &other) const
Inequality operator.
Definition aabbox3d.h:42
irr::core::aabbox3d::isEmpty
bool isEmpty() const
Check if the box is empty.
Definition aabbox3d.h:121
irr::core::aabbox3d::classifyPlaneRelation
EIntersectionRelation3D classifyPlaneRelation(const plane3d< T > &plane) const
Classifies a relation with a plane.
Definition aabbox3d.h:284
irr::core::aabbox3d::operator==
bool operator==(const aabbox3d< T > &other) const
Equality operator.
Definition aabbox3d.h:38
irr::core::aabbox3d::getEdges
void getEdges(vector3d< T > *edges) const
Stores all 8 edges of the box into an array.
Definition aabbox3d.h:142
irr::core::aabbox3d::intersectsWithLine
bool intersectsWithLine(const line3d< T > &line) const
Tests if the box intersects with a line.
Definition aabbox3d.h:242
irr::core::aabbox3d::intersectsWithLine
bool intersectsWithLine(const vector3d< T > &linemiddle, const vector3d< T > &linevect, T halflength) const
Tests if the box intersects with a line.
Definition aabbox3d.h:253
irr::core::aabbox3d::getExtent
vector3d< T > getExtent() const
Get extent of the box (maximal distance of two points in the box).
Definition aabbox3d.h:113
irr::core::aabbox3d::isPointInside
bool isPointInside(const vector3d< T > &p) const
Determines if a point is within this box.
Definition aabbox3d.h:201
irr::core::aabbox3d::aabbox3d
aabbox3d()
Default Constructor.
Definition aabbox3d.h:26
irr::core::aabbox3d::aabbox3d
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.h:32
irr::core::aabbox3d::reset
void reset(const vector3d< T > &initValue)
Resets the bounding box to a one-point box.
Definition aabbox3d.h:65
irr::core::aabbox3d::reset
void reset(T x, T y, T z)
Resets the bounding box to a one-point box.
Definition aabbox3d.h:50
irr::core::line3d
3D line between two points with intersection methods.
Definition line3d.h:19
irr::core::line3d::getMiddle
vector3d< T > getMiddle() const
Get middle of line.
Definition line3d.h:64
irr::core::line3d::getLength
T getLength() const
Get length of line.
Definition line3d.h:56
irr::core::line3d::getVector
vector3d< T > getVector() const
Get vector of line.
Definition line3d.h:71
irr::core::plane3d
Template plane class with some intersection testing methods.
Definition plane3d.h:34
irr::core::plane3d::Normal
vector3d< T > Normal
Normal vector of the plane.
Definition plane3d.h:228
irr::core::plane3d::D
T D
Distance from origin.
Definition plane3d.h:231
irr::core::vector3d
3d vector template class with lots of operators and methods.
Definition vector3d.h:23
irr::core::vector3d::X
T X
X coordinate of the vector.
Definition vector3d.h:408
irr::core::vector3d::set
vector3d< T > & set(const T nx, const T ny, const T nz)
Definition vector3d.h:113
irr::core::vector3d::Z
T Z
Z coordinate of the vector.
Definition vector3d.h:414
irr::core::vector3d::Y
T Y
Y coordinate of the vector.
Definition vector3d.h:411
irrMath.h
line3d.h
irr::core
Basic classes such as vectors, planes, arrays, lists, and so on can be found in this namespace.
Definition aabbox3d.h:15
irr::core::EIntersectionRelation3D
EIntersectionRelation3D
Enumeration for intersection relations of 3d objects.
Definition plane3d.h:18
irr::core::ISREL3D_FRONT
@ ISREL3D_FRONT
Definition plane3d.h:19
irr::core::ISREL3D_CLIPPED
@ ISREL3D_CLIPPED
Definition plane3d.h:23
irr::core::ISREL3D_BACK
@ ISREL3D_BACK
Definition plane3d.h:20
irr::core::aabbox3df
aabbox3d< f32 > aabbox3df
Typedef for a f32 3d bounding box.
Definition aabbox3d.h:324
irr::core::aabbox3di
aabbox3d< s32 > aabbox3di
Typedef for an integer 3d bounding box.
Definition aabbox3d.h:326
irr
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
irr::f32
float f32
32 bit floating point variable.
Definition irrTypes.h:104
plane3d.h
- Irrlicht Engine Documentation © 2003-2012 by Nikolaus Gebhardt. Generated on for Irrlicht 3D Engine by Doxygen 1.15.0