mBoxBase.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. InvalidPoint = NUM_POINTS
  47. };
  48. /// Return the point index for the opposite corner of @a p.
  49. static Points getOppositePoint( Points p )
  50. {
  51. switch( p )
  52. {
  53. case NearBottomRight: return FarTopLeft;
  54. case NearTopRight: return FarBottomLeft;
  55. case NearTopLeft: return FarBottomRight;
  56. case NearBottomLeft: return FarTopRight;
  57. case FarBottomRight: return NearTopLeft;
  58. case FarTopRight: return NearBottomLeft;
  59. case FarTopLeft: return NearBottomLeft;
  60. default:
  61. case FarBottomLeft: return NearTopRight;
  62. }
  63. }
  64. /// Return the point index for the corner point that corresponds
  65. /// to the octant that @a p points to.
  66. static Points getPointIndexFromOctant( const Point3F& p )
  67. {
  68. if( p.x > 0.f ) // Right
  69. {
  70. if( p.y > 0.f ) // Far
  71. {
  72. if( p.z > 0.f ) // Top
  73. return FarTopRight;
  74. else // Bottom
  75. return FarBottomRight;
  76. }
  77. else // Near
  78. {
  79. if( p.z > 0.f ) // Top
  80. return NearTopRight;
  81. else // Bottom
  82. return NearBottomRight;
  83. }
  84. }
  85. else // Left
  86. {
  87. if( p.y > 0.f ) // Far
  88. {
  89. if( p.z > 0.f ) // Top
  90. return FarTopLeft;
  91. else // Bottom
  92. return FarBottomLeft;
  93. }
  94. else // Near
  95. {
  96. if( p.z > 0.f ) // Top
  97. return NearTopLeft;
  98. else // Bottom
  99. return NearBottomLeft;
  100. }
  101. }
  102. }
  103. /// Indices for the side planes of the box. Each pair of planes
  104. /// has successive indices. Also, the planes are ordered by X (left&right),
  105. /// Y (near&far), and Z (top&bottom).
  106. enum Planes
  107. {
  108. LeftPlane,
  109. RightPlane,
  110. NearPlane,
  111. FarPlane,
  112. TopPlane,
  113. BottomPlane,
  114. NUM_PLANES
  115. };
  116. enum PlaneMasks : U32
  117. {
  118. PlaneMaskLeft = ( 1 << LeftPlane ),
  119. PlaneMaskRight = ( 1 << RightPlane ),
  120. PlaneMaskTop = ( 1 << TopPlane ),
  121. PlaneMaskBottom = ( 1 << BottomPlane ),
  122. PlaneMaskNear = ( 1 << NearPlane ),
  123. PlaneMaskFar = ( 1 << FarPlane ),
  124. PlaneMaskAll = 0xFFFFFFFF,
  125. };
  126. ///
  127. static Points getPlanePointIndex( Planes plane, U32 i )
  128. {
  129. switch( plane )
  130. {
  131. case LeftPlane:
  132. switch( i )
  133. {
  134. case 0: return NearBottomLeft;
  135. case 1: return NearTopLeft;
  136. case 2: return FarTopLeft;
  137. case 3: return FarBottomLeft;
  138. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  139. }
  140. break;
  141. case RightPlane:
  142. switch( i )
  143. {
  144. case 0: return NearBottomRight;
  145. case 1: return FarBottomRight;
  146. case 2: return FarTopRight;
  147. case 3: return NearTopRight;
  148. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  149. }
  150. break;
  151. case NearPlane:
  152. switch( i )
  153. {
  154. case 0: return NearBottomLeft;
  155. case 1: return NearBottomRight;
  156. case 2: return NearTopRight;
  157. case 3: return NearTopLeft;
  158. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  159. }
  160. break;
  161. case FarPlane:
  162. switch( i )
  163. {
  164. case 0: return FarBottomLeft;
  165. case 1: return FarTopLeft;
  166. case 2: return FarTopRight;
  167. case 3: return FarBottomRight;
  168. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  169. }
  170. break;
  171. case TopPlane:
  172. switch( i )
  173. {
  174. case 0: return NearTopLeft;
  175. case 1: return NearTopRight;
  176. case 2: return FarTopRight;
  177. case 3: return FarTopLeft;
  178. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  179. }
  180. break;
  181. case BottomPlane:
  182. switch( i )
  183. {
  184. case 0: return NearBottomLeft;
  185. case 1: return FarBottomLeft;
  186. case 2: return FarBottomRight;
  187. case 3: return NearBottomRight;
  188. default: AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid index" );
  189. }
  190. break;
  191. default:
  192. AssertFatal( false, "BoxBase::getPlanePointIndex - Invalid plane" );
  193. }
  194. return InvalidPoint;
  195. }
  196. /// Indices for the edges of the box.
  197. enum Edges
  198. {
  199. NearLeftEdge,
  200. NearBottomEdge,
  201. NearRightEdge,
  202. NearTopEdge,
  203. FarLeftEdge,
  204. FarTopEdge,
  205. FarRightEdge,
  206. FarBottomEdge,
  207. LeftTopEdge,
  208. LeftBottomEdge,
  209. RightTopEdge,
  210. RightBottomEdge,
  211. NUM_EDGES,
  212. InvalidEdge
  213. };
  214. /// Get the start and end point of the given edge.
  215. static void getEdgePointIndices( Edges edge, Points& outP1, Points& outP2 )
  216. {
  217. switch( edge )
  218. {
  219. case NearLeftEdge: outP1 = NearTopLeft; outP2 = NearBottomLeft; return;
  220. case NearBottomEdge: outP1 = NearBottomLeft; outP2 = NearBottomRight; return;
  221. case NearRightEdge: outP1 = NearBottomRight; outP2 = NearTopRight; return;
  222. case NearTopEdge: outP1 = NearTopRight; outP2 = NearTopLeft; return;
  223. case FarLeftEdge: outP1 = FarBottomLeft; outP2 = FarTopLeft; return;
  224. case FarTopEdge: outP1 = FarTopLeft; outP2 = FarTopRight; return;
  225. case FarRightEdge: outP1 = FarTopRight; outP2 = FarBottomRight; return;
  226. case FarBottomEdge: outP1 = FarBottomRight; outP2 = FarBottomLeft; return;
  227. case LeftTopEdge: outP1 = NearTopLeft; outP2 = FarTopLeft; return;
  228. case LeftBottomEdge: outP1 = FarBottomLeft; outP2 = NearBottomLeft; return;
  229. default:
  230. case RightTopEdge: outP1 = FarTopRight; outP2 = NearTopRight; return;
  231. case RightBottomEdge: outP1 = NearBottomRight; outP2 = FarBottomRight; return;
  232. }
  233. }
  234. };
  235. #endif // !_MBOXBASE_H_