Rotation.h (6428B)
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_ROTATION_H__ 30 #define __MATH_ROTATION_H__ 31 32 /* 33 =============================================================================== 34 35 Describes a complete rotation in degrees about an abritray axis. 36 A local rotation matrix is stored for fast rotation of multiple points. 37 38 =============================================================================== 39 */ 40 41 42 class idAngles; 43 class idQuat; 44 class idMat3; 45 46 class idRotation { 47 48 friend class idAngles; 49 friend class idQuat; 50 friend class idMat3; 51 52 public: 53 idRotation(); 54 idRotation( const idVec3 &rotationOrigin, const idVec3 &rotationVec, const float rotationAngle ); 55 56 void Set( const idVec3 &rotationOrigin, const idVec3 &rotationVec, const float rotationAngle ); 57 void SetOrigin( const idVec3 &rotationOrigin ); 58 void SetVec( const idVec3 &rotationVec ); // has to be normalized 59 void SetVec( const float x, const float y, const float z ); // has to be normalized 60 void SetAngle( const float rotationAngle ); 61 void Scale( const float s ); 62 void ReCalculateMatrix(); 63 const idVec3 & GetOrigin() const; 64 const idVec3 & GetVec() const; 65 float GetAngle() const; 66 67 idRotation operator-() const; // flips rotation 68 idRotation operator*( const float s ) const; // scale rotation 69 idRotation operator/( const float s ) const; // scale rotation 70 idRotation & operator*=( const float s ); // scale rotation 71 idRotation & operator/=( const float s ); // scale rotation 72 idVec3 operator*( const idVec3 &v ) const; // rotate vector 73 74 friend idRotation operator*( const float s, const idRotation &r ); // scale rotation 75 friend idVec3 operator*( const idVec3 &v, const idRotation &r ); // rotate vector 76 friend idVec3 & operator*=( idVec3 &v, const idRotation &r ); // rotate vector 77 78 idAngles ToAngles() const; 79 idQuat ToQuat() const; 80 const idMat3 & ToMat3() const; 81 idMat4 ToMat4() const; 82 idVec3 ToAngularVelocity() const; 83 84 void RotatePoint( idVec3 &point ) const; 85 86 void Normalize180(); 87 void Normalize360(); 88 89 private: 90 idVec3 origin; // origin of rotation 91 idVec3 vec; // normalized vector to rotate around 92 float angle; // angle of rotation in degrees 93 mutable idMat3 axis; // rotation axis 94 mutable bool axisValid; // true if rotation axis is valid 95 }; 96 97 98 ID_INLINE idRotation::idRotation() { 99 } 100 101 ID_INLINE idRotation::idRotation( const idVec3 &rotationOrigin, const idVec3 &rotationVec, const float rotationAngle ) { 102 origin = rotationOrigin; 103 vec = rotationVec; 104 angle = rotationAngle; 105 axisValid = false; 106 } 107 108 ID_INLINE void idRotation::Set( const idVec3 &rotationOrigin, const idVec3 &rotationVec, const float rotationAngle ) { 109 origin = rotationOrigin; 110 vec = rotationVec; 111 angle = rotationAngle; 112 axisValid = false; 113 } 114 115 ID_INLINE void idRotation::SetOrigin( const idVec3 &rotationOrigin ) { 116 origin = rotationOrigin; 117 } 118 119 ID_INLINE void idRotation::SetVec( const idVec3 &rotationVec ) { 120 vec = rotationVec; 121 axisValid = false; 122 } 123 124 ID_INLINE void idRotation::SetVec( float x, float y, float z ) { 125 vec[0] = x; 126 vec[1] = y; 127 vec[2] = z; 128 axisValid = false; 129 } 130 131 ID_INLINE void idRotation::SetAngle( const float rotationAngle ) { 132 angle = rotationAngle; 133 axisValid = false; 134 } 135 136 ID_INLINE void idRotation::Scale( const float s ) { 137 angle *= s; 138 axisValid = false; 139 } 140 141 ID_INLINE void idRotation::ReCalculateMatrix() { 142 axisValid = false; 143 ToMat3(); 144 } 145 146 ID_INLINE const idVec3 &idRotation::GetOrigin() const { 147 return origin; 148 } 149 150 ID_INLINE const idVec3 &idRotation::GetVec() const { 151 return vec; 152 } 153 154 ID_INLINE float idRotation::GetAngle() const { 155 return angle; 156 } 157 158 ID_INLINE idRotation idRotation::operator-() const { 159 return idRotation( origin, vec, -angle ); 160 } 161 162 ID_INLINE idRotation idRotation::operator*( const float s ) const { 163 return idRotation( origin, vec, angle * s ); 164 } 165 166 ID_INLINE idRotation idRotation::operator/( const float s ) const { 167 assert( s != 0.0f ); 168 return idRotation( origin, vec, angle / s ); 169 } 170 171 ID_INLINE idRotation &idRotation::operator*=( const float s ) { 172 angle *= s; 173 axisValid = false; 174 return *this; 175 } 176 177 ID_INLINE idRotation &idRotation::operator/=( const float s ) { 178 assert( s != 0.0f ); 179 angle /= s; 180 axisValid = false; 181 return *this; 182 } 183 184 ID_INLINE idVec3 idRotation::operator*( const idVec3 &v ) const { 185 if ( !axisValid ) { 186 ToMat3(); 187 } 188 return ((v - origin) * axis + origin); 189 } 190 191 ID_INLINE idRotation operator*( const float s, const idRotation &r ) { 192 return r * s; 193 } 194 195 ID_INLINE idVec3 operator*( const idVec3 &v, const idRotation &r ) { 196 return r * v; 197 } 198 199 ID_INLINE idVec3 &operator*=( idVec3 &v, const idRotation &r ) { 200 v = r * v; 201 return v; 202 } 203 204 ID_INLINE void idRotation::RotatePoint( idVec3 &point ) const { 205 if ( !axisValid ) { 206 ToMat3(); 207 } 208 point = ((point - origin) * axis + origin); 209 } 210 211 #endif /* !__MATH_ROTATION_H__ */