Angles.h (7254B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below. 23 24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. 25 26 =========================================================================== 27 */ 28 29 #ifndef __MATH_ANGLES_H__ 30 #define __MATH_ANGLES_H__ 31 32 /* 33 =============================================================================== 34 35 Euler angles 36 37 =============================================================================== 38 */ 39 40 // angle indexes 41 #define PITCH 0 // up / down 42 #define YAW 1 // left / right 43 #define ROLL 2 // fall over 44 45 class idVec3; 46 class idQuat; 47 class idRotation; 48 class idMat3; 49 class idMat4; 50 51 class idAngles { 52 public: 53 float pitch; 54 float yaw; 55 float roll; 56 57 idAngles(); 58 idAngles( float pitch, float yaw, float roll ); 59 explicit idAngles( const idVec3 &v ); 60 61 void Set( float pitch, float yaw, float roll ); 62 idAngles & Zero(); 63 64 float operator[]( int index ) const; 65 float & operator[]( int index ); 66 idAngles operator-() const; // negate angles, in general not the inverse rotation 67 idAngles & operator=( const idAngles &a ); 68 idAngles operator+( const idAngles &a ) const; 69 idAngles & operator+=( const idAngles &a ); 70 idAngles operator-( const idAngles &a ) const; 71 idAngles & operator-=( const idAngles &a ); 72 idAngles operator*( const float a ) const; 73 idAngles & operator*=( const float a ); 74 idAngles operator/( const float a ) const; 75 idAngles & operator/=( const float a ); 76 77 friend idAngles operator*( const float a, const idAngles &b ); 78 79 bool Compare( const idAngles &a ) const; // exact compare, no epsilon 80 bool Compare( const idAngles &a, const float epsilon ) const; // compare with epsilon 81 bool operator==( const idAngles &a ) const; // exact compare, no epsilon 82 bool operator!=( const idAngles &a ) const; // exact compare, no epsilon 83 84 idAngles & Normalize360(); // normalizes 'this' 85 idAngles & Normalize180(); // normalizes 'this' 86 87 void Clamp( const idAngles &min, const idAngles &max ); 88 89 int GetDimension() const; 90 91 void ToVectors( idVec3 *forward, idVec3 *right = NULL, idVec3 *up = NULL ) const; 92 idVec3 ToForward() const; 93 idQuat ToQuat() const; 94 idRotation ToRotation() const; 95 idMat3 ToMat3() const; 96 idMat4 ToMat4() const; 97 idVec3 ToAngularVelocity() const; 98 const float * ToFloatPtr() const; 99 float * ToFloatPtr(); 100 const char * ToString( int precision = 2 ) const; 101 }; 102 103 extern idAngles ang_zero; 104 105 ID_INLINE idAngles::idAngles() { 106 } 107 108 ID_INLINE idAngles::idAngles( float pitch, float yaw, float roll ) { 109 this->pitch = pitch; 110 this->yaw = yaw; 111 this->roll = roll; 112 } 113 114 ID_INLINE idAngles::idAngles( const idVec3 &v ) { 115 this->pitch = v[0]; 116 this->yaw = v[1]; 117 this->roll = v[2]; 118 } 119 120 ID_INLINE void idAngles::Set( float pitch, float yaw, float roll ) { 121 this->pitch = pitch; 122 this->yaw = yaw; 123 this->roll = roll; 124 } 125 126 ID_INLINE idAngles &idAngles::Zero() { 127 pitch = yaw = roll = 0.0f; 128 return *this; 129 } 130 131 ID_INLINE float idAngles::operator[]( int index ) const { 132 assert( ( index >= 0 ) && ( index < 3 ) ); 133 return ( &pitch )[ index ]; 134 } 135 136 ID_INLINE float &idAngles::operator[]( int index ) { 137 assert( ( index >= 0 ) && ( index < 3 ) ); 138 return ( &pitch )[ index ]; 139 } 140 141 ID_INLINE idAngles idAngles::operator-() const { 142 return idAngles( -pitch, -yaw, -roll ); 143 } 144 145 ID_INLINE idAngles &idAngles::operator=( const idAngles &a ) { 146 pitch = a.pitch; 147 yaw = a.yaw; 148 roll = a.roll; 149 return *this; 150 } 151 152 ID_INLINE idAngles idAngles::operator+( const idAngles &a ) const { 153 return idAngles( pitch + a.pitch, yaw + a.yaw, roll + a.roll ); 154 } 155 156 ID_INLINE idAngles& idAngles::operator+=( const idAngles &a ) { 157 pitch += a.pitch; 158 yaw += a.yaw; 159 roll += a.roll; 160 161 return *this; 162 } 163 164 ID_INLINE idAngles idAngles::operator-( const idAngles &a ) const { 165 return idAngles( pitch - a.pitch, yaw - a.yaw, roll - a.roll ); 166 } 167 168 ID_INLINE idAngles& idAngles::operator-=( const idAngles &a ) { 169 pitch -= a.pitch; 170 yaw -= a.yaw; 171 roll -= a.roll; 172 173 return *this; 174 } 175 176 ID_INLINE idAngles idAngles::operator*( const float a ) const { 177 return idAngles( pitch * a, yaw * a, roll * a ); 178 } 179 180 ID_INLINE idAngles& idAngles::operator*=( float a ) { 181 pitch *= a; 182 yaw *= a; 183 roll *= a; 184 return *this; 185 } 186 187 ID_INLINE idAngles idAngles::operator/( const float a ) const { 188 float inva = 1.0f / a; 189 return idAngles( pitch * inva, yaw * inva, roll * inva ); 190 } 191 192 ID_INLINE idAngles& idAngles::operator/=( float a ) { 193 float inva = 1.0f / a; 194 pitch *= inva; 195 yaw *= inva; 196 roll *= inva; 197 return *this; 198 } 199 200 ID_INLINE idAngles operator*( const float a, const idAngles &b ) { 201 return idAngles( a * b.pitch, a * b.yaw, a * b.roll ); 202 } 203 204 ID_INLINE bool idAngles::Compare( const idAngles &a ) const { 205 return ( ( a.pitch == pitch ) && ( a.yaw == yaw ) && ( a.roll == roll ) ); 206 } 207 208 ID_INLINE bool idAngles::Compare( const idAngles &a, const float epsilon ) const { 209 if ( idMath::Fabs( pitch - a.pitch ) > epsilon ) { 210 return false; 211 } 212 213 if ( idMath::Fabs( yaw - a.yaw ) > epsilon ) { 214 return false; 215 } 216 217 if ( idMath::Fabs( roll - a.roll ) > epsilon ) { 218 return false; 219 } 220 221 return true; 222 } 223 224 ID_INLINE bool idAngles::operator==( const idAngles &a ) const { 225 return Compare( a ); 226 } 227 228 ID_INLINE bool idAngles::operator!=( const idAngles &a ) const { 229 return !Compare( a ); 230 } 231 232 ID_INLINE void idAngles::Clamp( const idAngles &min, const idAngles &max ) { 233 if ( pitch < min.pitch ) { 234 pitch = min.pitch; 235 } else if ( pitch > max.pitch ) { 236 pitch = max.pitch; 237 } 238 if ( yaw < min.yaw ) { 239 yaw = min.yaw; 240 } else if ( yaw > max.yaw ) { 241 yaw = max.yaw; 242 } 243 if ( roll < min.roll ) { 244 roll = min.roll; 245 } else if ( roll > max.roll ) { 246 roll = max.roll; 247 } 248 } 249 250 ID_INLINE int idAngles::GetDimension() const { 251 return 3; 252 } 253 254 ID_INLINE const float *idAngles::ToFloatPtr() const { 255 return &pitch; 256 } 257 258 ID_INLINE float *idAngles::ToFloatPtr() { 259 return &pitch; 260 } 261 262 #endif /* !__MATH_ANGLES_H__ */