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