mBoxBase.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MBOXBASE_H_
  23. #define _MBOXBASE_H_
  24. #ifndef _MPOINT3_H_
  25. #include "math/mPoint3.h"
  26. #endif
  27. /// Base class for box geometries.
  28. class BoxBase
  29. {
  30. public:
  31. /// Indices of the corner points.
  32. ///
  33. /// @note The order defined here is expected by several places
  34. /// in the code!
  35. enum Points
  36. {
  37. NearBottomRight,
  38. NearTopRight,
  39. NearTopLeft,
  40. NearBottomLeft,
  41. FarBottomRight,
  42. FarTopRight,
  43. FarTopLeft,
  44. FarBottomLeft,
  45. NUM_POINTS
  46. };
  47. /// Return the point index for the opposite corner of @a p.
  48. static Points getOppositePoint( Points p )
  49. {
  50. switch( p )
  51. {
  52. case NearBottomRight: return FarTopLeft;
  53. case NearTopRight: return FarBottomLeft;
  54. case NearTopLeft: return FarBottomRight;
  55. case NearBottomLeft: return FarTopRight;
  56. case FarBottomRight: return NearTopLeft;
  57. case FarTopRight: return NearBottomLeft;
  58. case FarTopLeft: return NearBottomLeft;
  59. default:
  60. case FarBottomLeft: return NearTopRight;
  61. }
  62. }
  63. /// Return the point index for the corner point that corresponds
  64. /// to the octant that @a p points to.
  65. static Points getPointIndexFromOctant( const Point3F& p )
  66. {
  67. if( p.x > 0.f ) // Right
  68. {
  69. if( p.y > 0.f ) // Far
  70. {
  71. if( p.z > 0.f ) // Top
  72. return FarTopRight;
  73. else // Bottom
  74. return FarBottomRight;
  75. }
  76. else // Near
  77. {
  78. if( p.z > 0.f ) // Top
  79. return NearTopRight;
  80. else // Bottom
  81. return NearBottomRight;
  82. }
  83. }
  84. else // Left
  85. {
  86. if( p.y > 0.f ) // Far
  87. {
  88. if( p.z > 0.f ) // Top
  89. return FarTopLeft;
  90. else // Bottom
  91. return FarBottomLeft;
  92. }
  93. else // Near
  94. {
  95. if( p.z > 0.f ) // Top
  96. return NearTopLeft;
  97. else // Bottom
  98. return NearBottomLeft;
  99. }
  100. }
  101. }
  102. /// Indices for the side planes of the box. Each pair of planes
  103. /// has successive indices. Also, the planes are ordered by X (left&right),
  104. /// Y (near&far), and Z (top&bottom).
  105. enum Planes
  106. {
  107. LeftPlane,
  108. RightPlane,
  109. NearPlane,
  110. FarPlane,
  111. TopPlane,
  112. BottomPlane,
  113. NUM_PLANES
  114. };
  115. enum PlaneMasks
  116. {
  117. PlaneMaskLeft = ( 1 << LeftPlane ),
  118. PlaneMaskRight = ( 1 << RightPlane ),
  119. PlaneMaskTop = ( 1 << TopPlane ),
  120. PlaneMaskBottom = ( 1 << BottomPlane ),
  121. PlaneMaskNear = ( 1 << NearPlane ),
  122. PlaneMaskFar = ( 1 << FarPlane ),
  123. PlaneMaskAll = 0xFFFFFFFF,
  124. };
  125. ///
  126. static Points getPlanePointIndex( Planes plane, U32 i )
  127. {
  128. switch( plane )
  129. {
  130. case LeftPlane:
  131. switch( i )
  132. {
  133. case 0: return NearBottomLeft;
  134. case 1: return NearTopLeft;
  135. case 2: return FarTopLeft;
  136. case 3: return FarBottomLeft;
  137. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  138. }
  139. break;
  140. case RightPlane:
  141. switch( i )
  142. {
  143. case 0: return NearBottomRight;
  144. case 1: return FarBottomRight;
  145. case 2: return FarTopRight;
  146. case 3: return NearTopRight;
  147. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  148. }
  149. break;
  150. case NearPlane:
  151. switch( i )
  152. {
  153. case 0: return NearBottomLeft;
  154. case 1: return NearBottomRight;
  155. case 2: return NearTopRight;
  156. case 3: return NearTopLeft;
  157. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  158. }
  159. break;
  160. case FarPlane:
  161. switch( i )
  162. {
  163. case 0: return FarBottomLeft;
  164. case 1: return FarTopLeft;
  165. case 2: return FarTopRight;
  166. case 3: return FarBottomRight;
  167. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  168. }
  169. break;
  170. case TopPlane:
  171. switch( i )
  172. {
  173. case 0: return NearTopLeft;
  174. case 1: return NearTopRight;
  175. case 2: return FarTopRight;
  176. case 3: return FarTopLeft;
  177. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  178. }
  179. break;
  180. default:
  181. AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid plane" );
  182. case BottomPlane:
  183. switch( i )
  184. {
  185. case 0: return NearBottomLeft;
  186. case 1: return FarBottomLeft;
  187. case 2: return FarBottomRight;
  188. case 3: return NearBottomRight;
  189. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  190. }
  191. break;
  192. }
  193. }
  194. /// Indices for the edges of the box.
  195. enum Edges
  196. {
  197. NearLeftEdge,
  198. NearBottomEdge,
  199. NearRightEdge,
  200. NearTopEdge,
  201. FarLeftEdge,
  202. FarTopEdge,
  203. FarRightEdge,
  204. FarBottomEdge,
  205. LeftTopEdge,
  206. LeftBottomEdge,
  207. RightTopEdge,
  208. RightBottomEdge,
  209. NUM_EDGES,
  210. InvalidEdge
  211. };
  212. /// Get the start and end point of the given edge.
  213. static void getEdgePointIndices( Edges edge, Points& outP1, Points& outP2 )
  214. {
  215. switch( edge )
  216. {
  217. case NearLeftEdge: outP1 = NearTopLeft; outP2 = NearBottomLeft; return;
  218. case NearBottomEdge: outP1 = NearBottomLeft; outP2 = NearBottomRight; return;
  219. case NearRightEdge: outP1 = NearBottomRight; outP2 = NearTopRight; return;
  220. case NearTopEdge: outP1 = NearTopRight; outP2 = NearTopLeft; return;
  221. case FarLeftEdge: outP1 = FarBottomLeft; outP2 = FarTopLeft; return;
  222. case FarTopEdge: outP1 = FarTopLeft; outP2 = FarTopRight; return;
  223. case FarRightEdge: outP1 = FarTopRight; outP2 = FarBottomRight; return;
  224. case FarBottomEdge: outP1 = FarBottomRight; outP2 = FarBottomLeft; return;
  225. case LeftTopEdge: outP1 = NearTopLeft; outP2 = FarTopLeft; return;
  226. case LeftBottomEdge: outP1 = FarBottomLeft; outP2 = NearBottomLeft; return;
  227. default:
  228. case RightTopEdge: outP1 = FarTopRight; outP2 = NearTopRight; return;
  229. case RightBottomEdge: outP1 = NearBottomRight; outP2 = FarBottomRight; return;
  230. }
  231. }
  232. };
  233. #endif // !_MBOXBASE_H_