IceOBB.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains OBB-related code.
  4. * \file IceOBB.cpp
  5. * \author Pierre Terdiman
  6. * \date January, 29, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. /**
  11. * An Oriented Bounding Box (OBB).
  12. * \class OBB
  13. * \author Pierre Terdiman
  14. * \version 1.0
  15. */
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // Precompiled Header
  19. #include "Stdafx.h"
  20. using namespace IceMaths;
  21. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. /**
  23. * Tests if a point is contained within the OBB.
  24. * \param p [in] the world point to test
  25. * \return true if inside the OBB
  26. */
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. bool OBB::ContainsPoint(const Point& p) const
  29. {
  30. // Point in OBB test using lazy evaluation and early exits
  31. // Translate to box space
  32. Point RelPoint = p - mCenter;
  33. // Point * mRot maps from box space to world space
  34. // mRot * Point maps from world space to box space (what we need here)
  35. float f = mRot.m[0][0] * RelPoint.x + mRot.m[0][1] * RelPoint.y + mRot.m[0][2] * RelPoint.z;
  36. if(f >= mExtents.x || f <= -mExtents.x) return false;
  37. f = mRot.m[1][0] * RelPoint.x + mRot.m[1][1] * RelPoint.y + mRot.m[1][2] * RelPoint.z;
  38. if(f >= mExtents.y || f <= -mExtents.y) return false;
  39. f = mRot.m[2][0] * RelPoint.x + mRot.m[2][1] * RelPoint.y + mRot.m[2][2] * RelPoint.z;
  40. if(f >= mExtents.z || f <= -mExtents.z) return false;
  41. return true;
  42. }
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. /**
  45. * Builds an OBB from an AABB and a world transform.
  46. * \param aabb [in] the aabb
  47. * \param mat [in] the world transform
  48. */
  49. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. void OBB::Create(const AABB& aabb, const Matrix4x4& mat)
  51. {
  52. // Note: must be coherent with Rotate()
  53. aabb.GetCenter(mCenter);
  54. aabb.GetExtents(mExtents);
  55. // Here we have the same as OBB::Rotate(mat) where the obb is (mCenter, mExtents, Identity).
  56. // So following what's done in Rotate:
  57. // - x-form the center
  58. mCenter *= mat;
  59. // - combine rotation with identity, i.e. just use given matrix
  60. mRot = mat;
  61. }
  62. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. /**
  64. * Computes the obb planes.
  65. * \param planes [out] 6 box planes
  66. * \return true if success
  67. */
  68. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. bool OBB::ComputePlanes(Plane* planes) const
  70. {
  71. // Checkings
  72. if(!planes) return false;
  73. Point Axis0 = mRot[0];
  74. Point Axis1 = mRot[1];
  75. Point Axis2 = mRot[2];
  76. // Writes normals
  77. planes[0].n = Axis0;
  78. planes[1].n = -Axis0;
  79. planes[2].n = Axis1;
  80. planes[3].n = -Axis1;
  81. planes[4].n = Axis2;
  82. planes[5].n = -Axis2;
  83. // Compute a point on each plane
  84. Point p0 = mCenter + Axis0 * mExtents.x;
  85. Point p1 = mCenter - Axis0 * mExtents.x;
  86. Point p2 = mCenter + Axis1 * mExtents.y;
  87. Point p3 = mCenter - Axis1 * mExtents.y;
  88. Point p4 = mCenter + Axis2 * mExtents.z;
  89. Point p5 = mCenter - Axis2 * mExtents.z;
  90. // Compute d
  91. planes[0].d = -(planes[0].n|p0);
  92. planes[1].d = -(planes[1].n|p1);
  93. planes[2].d = -(planes[2].n|p2);
  94. planes[3].d = -(planes[3].n|p3);
  95. planes[4].d = -(planes[4].n|p4);
  96. planes[5].d = -(planes[5].n|p5);
  97. return true;
  98. }
  99. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100. /**
  101. * Computes the obb points.
  102. * \param pts [out] 8 box points
  103. * \return true if success
  104. */
  105. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. bool OBB::ComputePoints(Point* pts) const
  107. {
  108. // Checkings
  109. if(!pts) return false;
  110. Point Axis0 = mRot[0];
  111. Point Axis1 = mRot[1];
  112. Point Axis2 = mRot[2];
  113. Axis0 *= mExtents.x;
  114. Axis1 *= mExtents.y;
  115. Axis2 *= mExtents.z;
  116. // 7+------+6 0 = ---
  117. // /| /| 1 = +--
  118. // / | / | 2 = ++-
  119. // / 4+---/--+5 3 = -+-
  120. // 3+------+2 / y z 4 = --+
  121. // | / | / | / 5 = +-+
  122. // |/ |/ |/ 6 = +++
  123. // 0+------+1 *---x 7 = -++
  124. pts[0] = mCenter - Axis0 - Axis1 - Axis2;
  125. pts[1] = mCenter + Axis0 - Axis1 - Axis2;
  126. pts[2] = mCenter + Axis0 + Axis1 - Axis2;
  127. pts[3] = mCenter - Axis0 + Axis1 - Axis2;
  128. pts[4] = mCenter - Axis0 - Axis1 + Axis2;
  129. pts[5] = mCenter + Axis0 - Axis1 + Axis2;
  130. pts[6] = mCenter + Axis0 + Axis1 + Axis2;
  131. pts[7] = mCenter - Axis0 + Axis1 + Axis2;
  132. return true;
  133. }
  134. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  135. /**
  136. * Computes vertex normals.
  137. * \param pts [out] 8 box points
  138. * \return true if success
  139. */
  140. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  141. bool OBB::ComputeVertexNormals(Point* pts) const
  142. {
  143. static const float VertexNormals[] =
  144. {
  145. -INVSQRT3, -INVSQRT3, -INVSQRT3,
  146. INVSQRT3, -INVSQRT3, -INVSQRT3,
  147. INVSQRT3, INVSQRT3, -INVSQRT3,
  148. -INVSQRT3, INVSQRT3, -INVSQRT3,
  149. -INVSQRT3, -INVSQRT3, INVSQRT3,
  150. INVSQRT3, -INVSQRT3, INVSQRT3,
  151. INVSQRT3, INVSQRT3, INVSQRT3,
  152. -INVSQRT3, INVSQRT3, INVSQRT3
  153. };
  154. if(!pts) return false;
  155. const Point* VN = (const Point*)VertexNormals;
  156. for(udword i=0;i<8;i++)
  157. {
  158. pts[i] = VN[i] * mRot;
  159. }
  160. return true;
  161. }
  162. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  163. /**
  164. * Returns edges.
  165. * \return 24 indices (12 edges) indexing the list returned by ComputePoints()
  166. */
  167. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  168. const udword* OBB::GetEdges() const
  169. {
  170. static const udword Indices[] = {
  171. 0, 1, 1, 2, 2, 3, 3, 0,
  172. 7, 6, 6, 5, 5, 4, 4, 7,
  173. 1, 5, 6, 2,
  174. 3, 7, 4, 0
  175. };
  176. return Indices;
  177. }
  178. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  179. /**
  180. * Returns local edge normals.
  181. * \return edge normals in local space
  182. */
  183. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  184. const Point* OBB::GetLocalEdgeNormals() const
  185. {
  186. static const float EdgeNormals[] =
  187. {
  188. 0, -INVSQRT2, -INVSQRT2, // 0-1
  189. INVSQRT2, 0, -INVSQRT2, // 1-2
  190. 0, INVSQRT2, -INVSQRT2, // 2-3
  191. -INVSQRT2, 0, -INVSQRT2, // 3-0
  192. 0, INVSQRT2, INVSQRT2, // 7-6
  193. INVSQRT2, 0, INVSQRT2, // 6-5
  194. 0, -INVSQRT2, INVSQRT2, // 5-4
  195. -INVSQRT2, 0, INVSQRT2, // 4-7
  196. INVSQRT2, -INVSQRT2, 0, // 1-5
  197. INVSQRT2, INVSQRT2, 0, // 6-2
  198. -INVSQRT2, INVSQRT2, 0, // 3-7
  199. -INVSQRT2, -INVSQRT2, 0 // 4-0
  200. };
  201. return (const Point*)EdgeNormals;
  202. }
  203. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  204. /**
  205. * Returns world edge normal
  206. * \param edge_index [in] 0 <= edge index < 12
  207. * \param world_normal [out] edge normal in world space
  208. */
  209. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  210. void OBB::ComputeWorldEdgeNormal(udword edge_index, Point& world_normal) const
  211. {
  212. ASSERT(edge_index<12);
  213. world_normal = GetLocalEdgeNormals()[edge_index] * mRot;
  214. }
  215. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  216. /**
  217. * Computes an LSS surrounding the OBB.
  218. * \param lss [out] the LSS
  219. */
  220. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  221. void OBB::ComputeLSS(LSS& lss) const
  222. {
  223. Point Axis0 = mRot[0];
  224. Point Axis1 = mRot[1];
  225. Point Axis2 = mRot[2];
  226. switch(mExtents.LargestAxis())
  227. {
  228. case 0:
  229. lss.mRadius = (mExtents.y + mExtents.z)*0.5f;
  230. lss.mP0 = mCenter + Axis0 * (mExtents.x - lss.mRadius);
  231. lss.mP1 = mCenter - Axis0 * (mExtents.x - lss.mRadius);
  232. break;
  233. case 1:
  234. lss.mRadius = (mExtents.x + mExtents.z)*0.5f;
  235. lss.mP0 = mCenter + Axis1 * (mExtents.y - lss.mRadius);
  236. lss.mP1 = mCenter - Axis1 * (mExtents.y - lss.mRadius);
  237. break;
  238. case 2:
  239. lss.mRadius = (mExtents.x + mExtents.y)*0.5f;
  240. lss.mP0 = mCenter + Axis2 * (mExtents.z - lss.mRadius);
  241. lss.mP1 = mCenter - Axis2 * (mExtents.z - lss.mRadius);
  242. break;
  243. default: {}
  244. }
  245. }
  246. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  247. /**
  248. * Checks the OBB is inside another OBB.
  249. * \param box [in] the other OBB
  250. * \return TRUE if we're inside the other box
  251. */
  252. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  253. BOOL OBB::IsInside(const OBB& box) const
  254. {
  255. // Make a 4x4 from the box & inverse it
  256. Matrix4x4 M0Inv;
  257. {
  258. Matrix4x4 M0 = box.mRot;
  259. M0.SetTrans(box.mCenter);
  260. InvertPRMatrix(M0Inv, M0);
  261. }
  262. // With our inversed 4x4, create box1 in space of box0
  263. OBB _1in0;
  264. Rotate(M0Inv, _1in0);
  265. // This should cancel out box0's rotation, i.e. it's now an AABB.
  266. // => Center(0,0,0), Rot(identity)
  267. // The two boxes are in the same space so now we can compare them.
  268. // Create the AABB of (box1 in space of box0)
  269. const Matrix3x3& mtx = _1in0.mRot;
  270. float f = fabsf(mtx.m[0][0] * mExtents.x) + fabsf(mtx.m[1][0] * mExtents.y) + fabsf(mtx.m[2][0] * mExtents.z) - box.mExtents.x;
  271. if(f > _1in0.mCenter.x) return FALSE;
  272. if(-f < _1in0.mCenter.x) return FALSE;
  273. f = fabsf(mtx.m[0][1] * mExtents.x) + fabsf(mtx.m[1][1] * mExtents.y) + fabsf(mtx.m[2][1] * mExtents.z) - box.mExtents.y;
  274. if(f > _1in0.mCenter.y) return FALSE;
  275. if(-f < _1in0.mCenter.y) return FALSE;
  276. f = fabsf(mtx.m[0][2] * mExtents.x) + fabsf(mtx.m[1][2] * mExtents.y) + fabsf(mtx.m[2][2] * mExtents.z) - box.mExtents.z;
  277. if(f > _1in0.mCenter.z) return FALSE;
  278. if(-f < _1in0.mCenter.z) return FALSE;
  279. return TRUE;
  280. }