IceAABB.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains AABB-related code.
  4. * \file IceAABB.cpp
  5. * \author Pierre Terdiman
  6. * \date January, 29, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. /**
  11. * AABB class.
  12. * \class AABB
  13. * \author Pierre Terdiman
  14. * \version 1.0
  15. */
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. #include "../Opcode.h"
  19. using namespace IceMaths;
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. /**
  22. * Computes the sum of two AABBs.
  23. * \param aabb [in] the other AABB
  24. * \return Self-Reference
  25. */
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. AABB& AABB::Add(const AABB& aabb)
  28. {
  29. // Compute new min & max values
  30. Point Min; GetMin(Min);
  31. Point Tmp; aabb.GetMin(Tmp);
  32. Min.Min(Tmp);
  33. Point Max; GetMax(Max);
  34. aabb.GetMax(Tmp);
  35. Max.Max(Tmp);
  36. // Update this
  37. SetMinMax(Min, Max);
  38. return *this;
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. /**
  42. * Makes a cube from the AABB.
  43. * \param cube [out] the cube AABB
  44. * \return cube edge length
  45. */
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. float AABB::MakeCube(AABB& cube) const
  48. {
  49. Point Ext; GetExtents(Ext);
  50. float Max = Ext.Max();
  51. Point Cnt; GetCenter(Cnt);
  52. cube.SetCenterExtents(Cnt, Point(Max, Max, Max));
  53. return Max;
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. /**
  57. * Makes a sphere from the AABB.
  58. * \param sphere [out] sphere containing the AABB
  59. */
  60. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. void AABB::MakeSphere(Sphere& sphere) const
  62. {
  63. GetExtents(sphere.mCenter);
  64. sphere.mRadius = sphere.mCenter.Magnitude() * 1.00001f; // To make sure sphere::Contains(*this) succeeds
  65. GetCenter(sphere.mCenter);
  66. }
  67. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. /**
  69. * Checks a box is inside another box.
  70. * \param box [in] the other AABB
  71. * \return true if current box is inside input box
  72. */
  73. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. bool AABB::IsInside(const AABB& box) const
  75. {
  76. if(box.GetMin(0)>GetMin(0)) return false;
  77. if(box.GetMin(1)>GetMin(1)) return false;
  78. if(box.GetMin(2)>GetMin(2)) return false;
  79. if(box.GetMax(0)<GetMax(0)) return false;
  80. if(box.GetMax(1)<GetMax(1)) return false;
  81. if(box.GetMax(2)<GetMax(2)) return false;
  82. return true;
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85. /**
  86. * Computes the AABB planes.
  87. * \param planes [out] 6 planes surrounding the box
  88. * \return true if success
  89. */
  90. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  91. bool AABB::ComputePlanes(Plane* planes) const
  92. {
  93. // Checkings
  94. if(!planes) return false;
  95. Point Center, Extents;
  96. GetCenter(Center);
  97. GetExtents(Extents);
  98. // Writes normals
  99. planes[0].n = Point(1.0f, 0.0f, 0.0f);
  100. planes[1].n = Point(-1.0f, 0.0f, 0.0f);
  101. planes[2].n = Point(0.0f, 1.0f, 0.0f);
  102. planes[3].n = Point(0.0f, -1.0f, 0.0f);
  103. planes[4].n = Point(0.0f, 0.0f, 1.0f);
  104. planes[5].n = Point(0.0f, 0.0f, -1.0f);
  105. // Compute a point on each plane
  106. Point p0 = Point(Center.x+Extents.x, Center.y, Center.z);
  107. Point p1 = Point(Center.x-Extents.x, Center.y, Center.z);
  108. Point p2 = Point(Center.x, Center.y+Extents.y, Center.z);
  109. Point p3 = Point(Center.x, Center.y-Extents.y, Center.z);
  110. Point p4 = Point(Center.x, Center.y, Center.z+Extents.z);
  111. Point p5 = Point(Center.x, Center.y, Center.z-Extents.z);
  112. // Compute d
  113. planes[0].d = -(planes[0].n|p0);
  114. planes[1].d = -(planes[1].n|p1);
  115. planes[2].d = -(planes[2].n|p2);
  116. planes[3].d = -(planes[3].n|p3);
  117. planes[4].d = -(planes[4].n|p4);
  118. planes[5].d = -(planes[5].n|p5);
  119. return true;
  120. }
  121. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  122. /**
  123. * Computes the aabb points.
  124. * \param pts [out] 8 box points
  125. * \return true if success
  126. */
  127. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  128. bool AABB::ComputePoints(Point* pts) const
  129. {
  130. // Checkings
  131. if(!pts) return false;
  132. // Get box corners
  133. Point min; GetMin(min);
  134. Point max; GetMax(max);
  135. // 7+------+6 0 = ---
  136. // /| /| 1 = +--
  137. // / | / | 2 = ++-
  138. // / 4+---/--+5 3 = -+-
  139. // 3+------+2 / y z 4 = --+
  140. // | / | / | / 5 = +-+
  141. // |/ |/ |/ 6 = +++
  142. // 0+------+1 *---x 7 = -++
  143. // Generate 8 corners of the bbox
  144. pts[0] = Point(min.x, min.y, min.z);
  145. pts[1] = Point(max.x, min.y, min.z);
  146. pts[2] = Point(max.x, max.y, min.z);
  147. pts[3] = Point(min.x, max.y, min.z);
  148. pts[4] = Point(min.x, min.y, max.z);
  149. pts[5] = Point(max.x, min.y, max.z);
  150. pts[6] = Point(max.x, max.y, max.z);
  151. pts[7] = Point(min.x, max.y, max.z);
  152. return true;
  153. }
  154. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. /**
  156. * Gets vertex normals.
  157. * \param pts [out] 8 box points
  158. * \return true if success
  159. */
  160. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  161. const Point* AABB::GetVertexNormals() const
  162. {
  163. static float VertexNormals[] =
  164. {
  165. -INVSQRT3, -INVSQRT3, -INVSQRT3,
  166. INVSQRT3, -INVSQRT3, -INVSQRT3,
  167. INVSQRT3, INVSQRT3, -INVSQRT3,
  168. -INVSQRT3, INVSQRT3, -INVSQRT3,
  169. -INVSQRT3, -INVSQRT3, INVSQRT3,
  170. INVSQRT3, -INVSQRT3, INVSQRT3,
  171. INVSQRT3, INVSQRT3, INVSQRT3,
  172. -INVSQRT3, INVSQRT3, INVSQRT3
  173. };
  174. return (const Point*)VertexNormals;
  175. }
  176. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  177. /**
  178. * Returns edges.
  179. * \return 24 indices (12 edges) indexing the list returned by ComputePoints()
  180. */
  181. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  182. const udword* AABB::GetEdges() const
  183. {
  184. static udword Indices[] = {
  185. 0, 1, 1, 2, 2, 3, 3, 0,
  186. 7, 6, 6, 5, 5, 4, 4, 7,
  187. 1, 5, 6, 2,
  188. 3, 7, 4, 0
  189. };
  190. return Indices;
  191. }
  192. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  193. /**
  194. * Returns edge normals.
  195. * \return edge normals in local space
  196. */
  197. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  198. const Point* AABB::GetEdgeNormals() const
  199. {
  200. static float EdgeNormals[] =
  201. {
  202. 0, -INVSQRT2, -INVSQRT2, // 0-1
  203. INVSQRT2, 0, -INVSQRT2, // 1-2
  204. 0, INVSQRT2, -INVSQRT2, // 2-3
  205. -INVSQRT2, 0, -INVSQRT2, // 3-0
  206. 0, INVSQRT2, INVSQRT2, // 7-6
  207. INVSQRT2, 0, INVSQRT2, // 6-5
  208. 0, -INVSQRT2, INVSQRT2, // 5-4
  209. -INVSQRT2, 0, INVSQRT2, // 4-7
  210. INVSQRT2, -INVSQRT2, 0, // 1-5
  211. INVSQRT2, INVSQRT2, 0, // 6-2
  212. -INVSQRT2, INVSQRT2, 0, // 3-7
  213. -INVSQRT2, -INVSQRT2, 0 // 4-0
  214. };
  215. return (const Point*)EdgeNormals;
  216. }
  217. // ===========================================================================
  218. // (C) 1996-98 Vienna University of Technology
  219. // ===========================================================================
  220. // NAME: bboxarea
  221. // TYPE: c++ code
  222. // PROJECT: Bounding Box Area
  223. // CONTENT: Computes area of 2D projection of 3D oriented bounding box
  224. // VERSION: 1.0
  225. // ===========================================================================
  226. // AUTHORS: ds Dieter Schmalstieg
  227. // ep Erik Pojar
  228. // ===========================================================================
  229. // HISTORY:
  230. //
  231. // 19-sep-99 15:23:03 ds last modification
  232. // 01-dec-98 15:23:03 ep created
  233. // ===========================================================================
  234. //----------------------------------------------------------------------------
  235. // SAMPLE CODE STARTS HERE
  236. //----------------------------------------------------------------------------
  237. // NOTE: This sample program requires OPEN INVENTOR!
  238. //indexlist: this table stores the 64 possible cases of classification of
  239. //the eyepoint with respect to the 6 defining planes of the bbox (2^6=64)
  240. //only 26 (3^3-1, where 1 is "inside" cube) of these cases are valid.
  241. //the first 6 numbers in each row are the indices of the bbox vertices that
  242. //form the outline of which we want to compute the area (counterclockwise
  243. //ordering), the 7th entry means the number of vertices in the outline.
  244. //there are 6 cases with a single face and and a 4-vertex outline, and
  245. //20 cases with 2 or 3 faces and a 6-vertex outline. a value of 0 indicates
  246. //an invalid case.
  247. // Original list was made of 7 items, I added an 8th element:
  248. // - to padd on a cache line
  249. // - to repeat the first entry to avoid modulos
  250. //
  251. // I also replaced original ints with sbytes.
  252. static const sbyte gIndexList[64][8] =
  253. {
  254. {-1,-1,-1,-1,-1,-1,-1, 0}, // 0 inside
  255. { 0, 4, 7, 3, 0,-1,-1, 4}, // 1 left
  256. { 1, 2, 6, 5, 1,-1,-1, 4}, // 2 right
  257. {-1,-1,-1,-1,-1,-1,-1, 0}, // 3 -
  258. { 0, 1, 5, 4, 0,-1,-1, 4}, // 4 bottom
  259. { 0, 1, 5, 4, 7, 3, 0, 6}, // 5 bottom, left
  260. { 0, 1, 2, 6, 5, 4, 0, 6}, // 6 bottom, right
  261. {-1,-1,-1,-1,-1,-1,-1, 0}, // 7 -
  262. { 2, 3, 7, 6, 2,-1,-1, 4}, // 8 top
  263. { 0, 4, 7, 6, 2, 3, 0, 6}, // 9 top, left
  264. { 1, 2, 3, 7, 6, 5, 1, 6}, //10 top, right
  265. {-1,-1,-1,-1,-1,-1,-1, 0}, //11 -
  266. {-1,-1,-1,-1,-1,-1,-1, 0}, //12 -
  267. {-1,-1,-1,-1,-1,-1,-1, 0}, //13 -
  268. {-1,-1,-1,-1,-1,-1,-1, 0}, //14 -
  269. {-1,-1,-1,-1,-1,-1,-1, 0}, //15 -
  270. { 0, 3, 2, 1, 0,-1,-1, 4}, //16 front
  271. { 0, 4, 7, 3, 2, 1, 0, 6}, //17 front, left
  272. { 0, 3, 2, 6, 5, 1, 0, 6}, //18 front, right
  273. {-1,-1,-1,-1,-1,-1,-1, 0}, //19 -
  274. { 0, 3, 2, 1, 5, 4, 0, 6}, //20 front, bottom
  275. { 1, 5, 4, 7, 3, 2, 1, 6}, //21 front, bottom, left
  276. { 0, 3, 2, 6, 5, 4, 0, 6}, //22 front, bottom, right
  277. {-1,-1,-1,-1,-1,-1,-1, 0}, //23 -
  278. { 0, 3, 7, 6, 2, 1, 0, 6}, //24 front, top
  279. { 0, 4, 7, 6, 2, 1, 0, 6}, //25 front, top, left
  280. { 0, 3, 7, 6, 5, 1, 0, 6}, //26 front, top, right
  281. {-1,-1,-1,-1,-1,-1,-1, 0}, //27 -
  282. {-1,-1,-1,-1,-1,-1,-1, 0}, //28 -
  283. {-1,-1,-1,-1,-1,-1,-1, 0}, //29 -
  284. {-1,-1,-1,-1,-1,-1,-1, 0}, //30 -
  285. {-1,-1,-1,-1,-1,-1,-1, 0}, //31 -
  286. { 4, 5, 6, 7, 4,-1,-1, 4}, //32 back
  287. { 0, 4, 5, 6, 7, 3, 0, 6}, //33 back, left
  288. { 1, 2, 6, 7, 4, 5, 1, 6}, //34 back, right
  289. {-1,-1,-1,-1,-1,-1,-1, 0}, //35 -
  290. { 0, 1, 5, 6, 7, 4, 0, 6}, //36 back, bottom
  291. { 0, 1, 5, 6, 7, 3, 0, 6}, //37 back, bottom, left
  292. { 0, 1, 2, 6, 7, 4, 0, 6}, //38 back, bottom, right
  293. {-1,-1,-1,-1,-1,-1,-1, 0}, //39 -
  294. { 2, 3, 7, 4, 5, 6, 2, 6}, //40 back, top
  295. { 0, 4, 5, 6, 2, 3, 0, 6}, //41 back, top, left
  296. { 1, 2, 3, 7, 4, 5, 1, 6}, //42 back, top, right
  297. {-1,-1,-1,-1,-1,-1,-1, 0}, //43 invalid
  298. {-1,-1,-1,-1,-1,-1,-1, 0}, //44 invalid
  299. {-1,-1,-1,-1,-1,-1,-1, 0}, //45 invalid
  300. {-1,-1,-1,-1,-1,-1,-1, 0}, //46 invalid
  301. {-1,-1,-1,-1,-1,-1,-1, 0}, //47 invalid
  302. {-1,-1,-1,-1,-1,-1,-1, 0}, //48 invalid
  303. {-1,-1,-1,-1,-1,-1,-1, 0}, //49 invalid
  304. {-1,-1,-1,-1,-1,-1,-1, 0}, //50 invalid
  305. {-1,-1,-1,-1,-1,-1,-1, 0}, //51 invalid
  306. {-1,-1,-1,-1,-1,-1,-1, 0}, //52 invalid
  307. {-1,-1,-1,-1,-1,-1,-1, 0}, //53 invalid
  308. {-1,-1,-1,-1,-1,-1,-1, 0}, //54 invalid
  309. {-1,-1,-1,-1,-1,-1,-1, 0}, //55 invalid
  310. {-1,-1,-1,-1,-1,-1,-1, 0}, //56 invalid
  311. {-1,-1,-1,-1,-1,-1,-1, 0}, //57 invalid
  312. {-1,-1,-1,-1,-1,-1,-1, 0}, //58 invalid
  313. {-1,-1,-1,-1,-1,-1,-1, 0}, //59 invalid
  314. {-1,-1,-1,-1,-1,-1,-1, 0}, //60 invalid
  315. {-1,-1,-1,-1,-1,-1,-1, 0}, //61 invalid
  316. {-1,-1,-1,-1,-1,-1,-1, 0}, //62 invalid
  317. {-1,-1,-1,-1,-1,-1,-1, 0} //63 invalid
  318. };
  319. const sbyte* AABB::ComputeOutline(const Point& local_eye, sdword& num) const
  320. {
  321. // Get box corners
  322. Point min; GetMin(min);
  323. Point max; GetMax(max);
  324. // Compute 6-bit code to classify eye with respect to the 6 defining planes of the bbox
  325. int pos = ((local_eye.x < min.x) ? 1 : 0) // 1 = left
  326. + ((local_eye.x > max.x) ? 2 : 0) // 2 = right
  327. + ((local_eye.y < min.y) ? 4 : 0) // 4 = bottom
  328. + ((local_eye.y > max.y) ? 8 : 0) // 8 = top
  329. + ((local_eye.z < min.z) ? 16 : 0) // 16 = front
  330. + ((local_eye.z > max.z) ? 32 : 0); // 32 = back
  331. // Look up number of vertices in outline
  332. num = (sdword)gIndexList[pos][7];
  333. // Zero indicates invalid case
  334. if(!num) return null;
  335. return &gIndexList[pos][0];
  336. }
  337. // calculateBoxArea: computes the screen-projected 2D area of an oriented 3D bounding box
  338. //const Point& eye, //eye point (in bbox object coordinates)
  339. //const AABB& box, //3d bbox
  340. //const Matrix4x4& mat, //free transformation for bbox
  341. //float width, float height, int& num)
  342. float AABB::ComputeBoxArea(const Point& eye, const Matrix4x4& mat, float width, float height, sdword& num) const
  343. {
  344. const sbyte* Outline = ComputeOutline(eye, num);
  345. if(!Outline) return -1.0f;
  346. // Compute box vertices
  347. Point vertexBox[8], dst[8];
  348. ComputePoints(vertexBox);
  349. // Transform all outline corners into 2D screen space
  350. for(sdword i=0;i<num;i++)
  351. {
  352. HPoint Projected;
  353. vertexBox[Outline[i]].ProjectToScreen(width, height, mat, Projected);
  354. dst[i] = Projected;
  355. }
  356. float Sum = (dst[num-1][0] - dst[0][0]) * (dst[num-1][1] + dst[0][1]);
  357. for(int i=0; i<num-1; i++)
  358. Sum += (dst[i][0] - dst[i+1][0]) * (dst[i][1] + dst[i+1][1]);
  359. return Sum * 0.5f; //return computed value corrected by 0.5
  360. }