aabbox3d.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 irrlicht.h
  4. #ifndef IRR_AABBOX_3D_H_INCLUDED
  5. #define IRR_AABBOX_3D_H_INCLUDED
  6. #include "irrMath.h"
  7. #include "plane3d.h"
  8. #include "line3d.h"
  9. namespace irr
  10. {
  11. namespace core
  12. {
  13. //! Axis aligned bounding box in 3d dimensional space.
  14. /** Has some useful methods used with occlusion culling or clipping.
  15. */
  16. template <class T>
  17. class aabbox3d
  18. {
  19. public:
  20. //! Default Constructor.
  21. aabbox3d(): MinEdge(-1,-1,-1), MaxEdge(1,1,1) {}
  22. //! Constructor with min edge and max edge.
  23. aabbox3d(const vector3d<T>& min, const vector3d<T>& max): MinEdge(min), MaxEdge(max) {}
  24. //! Constructor with only one point.
  25. aabbox3d(const vector3d<T>& init): MinEdge(init), MaxEdge(init) {}
  26. //! Constructor with min edge and max edge as single values, not vectors.
  27. aabbox3d(T minx, T miny, T minz, T maxx, T maxy, T maxz): MinEdge(minx, miny, minz), MaxEdge(maxx, maxy, maxz) {}
  28. // operators
  29. //! Equality operator
  30. /** \param other box to compare with.
  31. \return True if both boxes are equal, else false. */
  32. inline bool operator==(const aabbox3d<T>& other) const { return (MinEdge == other.MinEdge && other.MaxEdge == MaxEdge);}
  33. //! Inequality operator
  34. /** \param other box to compare with.
  35. \return True if both boxes are different, else false. */
  36. inline bool operator!=(const aabbox3d<T>& other) const { return !(MinEdge == other.MinEdge && other.MaxEdge == MaxEdge);}
  37. // functions
  38. //! Resets the bounding box to a one-point box.
  39. /** \param x X coord of the point.
  40. \param y Y coord of the point.
  41. \param z Z coord of the point. */
  42. void reset(T x, T y, T z)
  43. {
  44. MaxEdge.set(x,y,z);
  45. MinEdge = MaxEdge;
  46. }
  47. //! Resets the bounding box.
  48. /** \param initValue New box to set this one to. */
  49. void reset(const aabbox3d<T>& initValue)
  50. {
  51. *this = initValue;
  52. }
  53. //! Resets the bounding box to a one-point box.
  54. /** \param initValue New point. */
  55. void reset(const vector3d<T>& initValue)
  56. {
  57. MaxEdge = initValue;
  58. MinEdge = initValue;
  59. }
  60. //! Adds a point to the bounding box
  61. /** The box grows bigger, if point was outside of the box.
  62. \param p: Point to add into the box. */
  63. void addInternalPoint(const vector3d<T>& p)
  64. {
  65. addInternalPoint(p.X, p.Y, p.Z);
  66. }
  67. //! Adds another bounding box
  68. /** The box grows bigger, if the new box was outside of the box.
  69. \param b: Other bounding box to add into this box. */
  70. void addInternalBox(const aabbox3d<T>& b)
  71. {
  72. addInternalPoint(b.MaxEdge);
  73. addInternalPoint(b.MinEdge);
  74. }
  75. //! Adds a point to the bounding box
  76. /** The box grows bigger, if point is outside of the box.
  77. \param x X coordinate of the point to add to this box.
  78. \param y Y coordinate of the point to add to this box.
  79. \param z Z coordinate of the point to add to this box. */
  80. void addInternalPoint(T x, T y, T z)
  81. {
  82. if (x>MaxEdge.X) MaxEdge.X = x;
  83. if (y>MaxEdge.Y) MaxEdge.Y = y;
  84. if (z>MaxEdge.Z) MaxEdge.Z = z;
  85. if (x<MinEdge.X) MinEdge.X = x;
  86. if (y<MinEdge.Y) MinEdge.Y = y;
  87. if (z<MinEdge.Z) MinEdge.Z = z;
  88. }
  89. //! Get center of the bounding box
  90. /** \return Center of the bounding box. */
  91. vector3d<T> getCenter() const
  92. {
  93. return (MinEdge + MaxEdge) / 2;
  94. }
  95. //! Get extent of the box (maximal distance of two points in the box)
  96. /** \return Extent of the bounding box. */
  97. vector3d<T> getExtent() const
  98. {
  99. return MaxEdge - MinEdge;
  100. }
  101. //! Get radius of the bounding sphere
  102. /** \return Radius of the bounding sphere. */
  103. T getRadius() const
  104. {
  105. const T radius = getExtent().getLength() / 2;
  106. return radius;
  107. }
  108. //! Check if the box is empty.
  109. /** This means that there is no space between the min and max edge.
  110. \return True if box is empty, else false. */
  111. bool isEmpty() const
  112. {
  113. return MinEdge.equals ( MaxEdge );
  114. }
  115. //! Get the volume enclosed by the box in cubed units
  116. T getVolume() const
  117. {
  118. const vector3d<T> e = getExtent();
  119. return e.X * e.Y * e.Z;
  120. }
  121. //! Get the surface area of the box in squared units
  122. T getArea() const
  123. {
  124. const vector3d<T> e = getExtent();
  125. return 2*(e.X*e.Y + e.X*e.Z + e.Y*e.Z);
  126. }
  127. //! Stores all 8 edges of the box into an array
  128. /** \param edges: Pointer to array of 8 edges. */
  129. void getEdges(vector3d<T> *edges) const
  130. {
  131. const core::vector3d<T> middle = getCenter();
  132. const core::vector3d<T> diag = middle - MaxEdge;
  133. /*
  134. Edges are stored in this way:
  135. Hey, am I an ascii artist, or what? :) niko.
  136. /3--------/7
  137. / | / |
  138. / | / |
  139. 1---------5 |
  140. | /2- - -|- -6
  141. | / | /
  142. |/ | /
  143. 0---------4/
  144. */
  145. edges[0].set(middle.X + diag.X, middle.Y + diag.Y, middle.Z + diag.Z);
  146. edges[1].set(middle.X + diag.X, middle.Y - diag.Y, middle.Z + diag.Z);
  147. edges[2].set(middle.X + diag.X, middle.Y + diag.Y, middle.Z - diag.Z);
  148. edges[3].set(middle.X + diag.X, middle.Y - diag.Y, middle.Z - diag.Z);
  149. edges[4].set(middle.X - diag.X, middle.Y + diag.Y, middle.Z + diag.Z);
  150. edges[5].set(middle.X - diag.X, middle.Y - diag.Y, middle.Z + diag.Z);
  151. edges[6].set(middle.X - diag.X, middle.Y + diag.Y, middle.Z - diag.Z);
  152. edges[7].set(middle.X - diag.X, middle.Y - diag.Y, middle.Z - diag.Z);
  153. }
  154. //! Repairs the box.
  155. /** Necessary if for example MinEdge and MaxEdge are swapped. */
  156. void repair()
  157. {
  158. T t;
  159. if (MinEdge.X > MaxEdge.X)
  160. { t=MinEdge.X; MinEdge.X = MaxEdge.X; MaxEdge.X=t; }
  161. if (MinEdge.Y > MaxEdge.Y)
  162. { t=MinEdge.Y; MinEdge.Y = MaxEdge.Y; MaxEdge.Y=t; }
  163. if (MinEdge.Z > MaxEdge.Z)
  164. { t=MinEdge.Z; MinEdge.Z = MaxEdge.Z; MaxEdge.Z=t; }
  165. }
  166. // Check if MaxEdge > MinEdge
  167. bool isValid() const
  168. {
  169. if (MinEdge.X > MaxEdge.X) return false;
  170. if (MinEdge.Y > MaxEdge.Y) return false;
  171. if (MinEdge.Z > MaxEdge.Z) return false;
  172. return true;
  173. }
  174. //! Calculates a new interpolated bounding box.
  175. /** d=0 returns other, d=1 returns this, all other values blend between
  176. the two boxes.
  177. \param other Other box to interpolate between
  178. \param d Value between 0.0f and 1.0f.
  179. \return Interpolated box. */
  180. aabbox3d<T> getInterpolated(const aabbox3d<T>& other, f32 d) const
  181. {
  182. const f32 inv = 1.0f - d;
  183. return aabbox3d<T>((other.MinEdge*inv) + (MinEdge*d),
  184. (other.MaxEdge*inv) + (MaxEdge*d));
  185. }
  186. //! Determines if a point is within this box.
  187. /** Border is included (IS part of the box)!
  188. \param p: Point to check.
  189. \return True if the point is within the box and false if not */
  190. bool isPointInside(const vector3d<T>& p) const
  191. {
  192. return (p.X >= MinEdge.X && p.X <= MaxEdge.X &&
  193. p.Y >= MinEdge.Y && p.Y <= MaxEdge.Y &&
  194. p.Z >= MinEdge.Z && p.Z <= MaxEdge.Z);
  195. }
  196. //! Determines if a point is within this box and not its borders.
  197. /** Border is excluded (NOT part of the box)!
  198. \param p: Point to check.
  199. \return True if the point is within the box and false if not. */
  200. bool isPointTotalInside(const vector3d<T>& p) const
  201. {
  202. return (p.X > MinEdge.X && p.X < MaxEdge.X &&
  203. p.Y > MinEdge.Y && p.Y < MaxEdge.Y &&
  204. p.Z > MinEdge.Z && p.Z < MaxEdge.Z);
  205. }
  206. //! Check if this box is completely inside the 'other' box.
  207. /** \param other: Other box to check against.
  208. \return True if this box is completely inside the other box,
  209. otherwise false. */
  210. bool isFullInside(const aabbox3d<T>& other) const
  211. {
  212. return (MinEdge.X >= other.MinEdge.X && MinEdge.Y >= other.MinEdge.Y && MinEdge.Z >= other.MinEdge.Z &&
  213. MaxEdge.X <= other.MaxEdge.X && MaxEdge.Y <= other.MaxEdge.Y && MaxEdge.Z <= other.MaxEdge.Z);
  214. }
  215. //! Returns the intersection of this box with another, if possible.
  216. aabbox3d<T> intersect(const aabbox3d<T>& other) const
  217. {
  218. aabbox3d<T> out;
  219. if (!intersectsWithBox(other))
  220. return out;
  221. out.MaxEdge.X = min_(MaxEdge.X, other.MaxEdge.X);
  222. out.MaxEdge.Y = min_(MaxEdge.Y, other.MaxEdge.Y);
  223. out.MaxEdge.Z = min_(MaxEdge.Z, other.MaxEdge.Z);
  224. out.MinEdge.X = max_(MinEdge.X, other.MinEdge.X);
  225. out.MinEdge.Y = max_(MinEdge.Y, other.MinEdge.Y);
  226. out.MinEdge.Z = max_(MinEdge.Z, other.MinEdge.Z);
  227. return out;
  228. }
  229. //! Determines if the axis-aligned box intersects with another axis-aligned box.
  230. /** \param other: Other box to check a intersection with.
  231. \return True if there is an intersection with the other box,
  232. otherwise false. */
  233. bool intersectsWithBox(const aabbox3d<T>& other) const
  234. {
  235. return (MinEdge.X <= other.MaxEdge.X && MinEdge.Y <= other.MaxEdge.Y && MinEdge.Z <= other.MaxEdge.Z &&
  236. MaxEdge.X >= other.MinEdge.X && MaxEdge.Y >= other.MinEdge.Y && MaxEdge.Z >= other.MinEdge.Z);
  237. }
  238. //! Tests if the box intersects with a line
  239. /** \param line: Line to test intersection with.
  240. \return True if there is an intersection , else false. */
  241. bool intersectsWithLine(const line3d<T>& line) const
  242. {
  243. return intersectsWithLine(line.getMiddle(), line.getVector().normalize(),
  244. (T)(line.getLength() * 0.5));
  245. }
  246. //! Tests if the box intersects with a line
  247. /** \param linemiddle Center of the line.
  248. \param linevect Vector of the line.
  249. \param halflength Half length of the line.
  250. \return True if there is an intersection, else false. */
  251. bool intersectsWithLine(const vector3d<T>& linemiddle,
  252. const vector3d<T>& linevect, T halflength) const
  253. {
  254. const vector3d<T> e = getExtent() * (T)0.5;
  255. const vector3d<T> t = getCenter() - linemiddle;
  256. if ((fabs(t.X) > e.X + halflength * fabs(linevect.X)) ||
  257. (fabs(t.Y) > e.Y + halflength * fabs(linevect.Y)) ||
  258. (fabs(t.Z) > e.Z + halflength * fabs(linevect.Z)) )
  259. return false;
  260. T r = e.Y * (T)fabs(linevect.Z) + e.Z * (T)fabs(linevect.Y);
  261. if (fabs(t.Y*linevect.Z - t.Z*linevect.Y) > r )
  262. return false;
  263. r = e.X * (T)fabs(linevect.Z) + e.Z * (T)fabs(linevect.X);
  264. if (fabs(t.Z*linevect.X - t.X*linevect.Z) > r )
  265. return false;
  266. r = e.X * (T)fabs(linevect.Y) + e.Y * (T)fabs(linevect.X);
  267. if (fabs(t.X*linevect.Y - t.Y*linevect.X) > r)
  268. return false;
  269. return true;
  270. }
  271. //! Classifies a relation with a plane.
  272. /** \param plane Plane to classify relation to.
  273. \return Returns ISREL3D_FRONT if the box is in front of the plane,
  274. ISREL3D_BACK if the box is behind the plane, and
  275. ISREL3D_CLIPPED if it is on both sides of the plane. */
  276. EIntersectionRelation3D classifyPlaneRelation(const plane3d<T>& plane) const
  277. {
  278. vector3d<T> nearPoint(MaxEdge);
  279. vector3d<T> farPoint(MinEdge);
  280. if (plane.Normal.X > (T)0)
  281. {
  282. nearPoint.X = MinEdge.X;
  283. farPoint.X = MaxEdge.X;
  284. }
  285. if (plane.Normal.Y > (T)0)
  286. {
  287. nearPoint.Y = MinEdge.Y;
  288. farPoint.Y = MaxEdge.Y;
  289. }
  290. if (plane.Normal.Z > (T)0)
  291. {
  292. nearPoint.Z = MinEdge.Z;
  293. farPoint.Z = MaxEdge.Z;
  294. }
  295. if (plane.Normal.dotProduct(nearPoint) + plane.D > (T)0)
  296. return ISREL3D_FRONT;
  297. if (plane.Normal.dotProduct(farPoint) + plane.D > (T)0)
  298. return ISREL3D_CLIPPED;
  299. return ISREL3D_BACK;
  300. }
  301. //! The near edge
  302. vector3d<T> MinEdge;
  303. //! The far edge
  304. vector3d<T> MaxEdge;
  305. };
  306. //! Typedef for a f32 3d bounding box.
  307. typedef aabbox3d<f32> aabbox3df;
  308. //! Typedef for an integer 3d bounding box.
  309. typedef aabbox3d<s32> aabbox3di;
  310. } // end namespace core
  311. } // end namespace irr
  312. #endif