IceOBB.cpp 12 KB

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