2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 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.
14 Doom 3 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.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 In addition, the Doom 3 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 Source Code. If not, please request a copy in writing from id Software at the address below.
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.
26 ===========================================================================
29 #include "../precompiled.h"
37 idAngles idQuat::ToAngles( void ) const {
38 return ToMat3().ToAngles();
46 idRotation idQuat::ToRotation( void ) const {
53 angle = idMath::ACos( w );
54 if ( angle == 0.0f ) {
55 vec.Set( 0.0f, 0.0f, 1.0f );
57 //vec *= (1.0f / sin( angle ));
59 vec.FixDegenerateNormal();
60 angle *= 2.0f * idMath::M_RAD2DEG;
62 return idRotation( vec3_origin, vec, angle );
70 idMat3 idQuat::ToMat3( void ) const {
93 mat[ 0 ][ 0 ] = 1.0f - ( yy + zz );
94 mat[ 0 ][ 1 ] = xy - wz;
95 mat[ 0 ][ 2 ] = xz + wy;
97 mat[ 1 ][ 0 ] = xy + wz;
98 mat[ 1 ][ 1 ] = 1.0f - ( xx + zz );
99 mat[ 1 ][ 2 ] = yz - wx;
101 mat[ 2 ][ 0 ] = xz - wy;
102 mat[ 2 ][ 1 ] = yz + wx;
103 mat[ 2 ][ 2 ] = 1.0f - ( xx + yy );
109 =====================
111 =====================
113 idMat4 idQuat::ToMat4( void ) const {
114 return ToMat3().ToMat4();
118 =====================
120 =====================
122 idCQuat idQuat::ToCQuat( void ) const {
124 return idCQuat( -x, -y, -z );
126 return idCQuat( x, y, z );
131 idQuat::ToAngularVelocity
134 idVec3 idQuat::ToAngularVelocity( void ) const {
141 return vec * idMath::ACos( w );
149 const char *idQuat::ToString( int precision ) const {
150 return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
154 =====================
157 Spherical linear interpolation between two quaternions.
158 =====================
160 idQuat &idQuat::Slerp( const idQuat &from, const idQuat &to, float t ) {
162 float omega, cosom, sinom, scale0, scale1;
179 cosom = from.x * to.x + from.y * to.y + from.z * to.z + from.w * to.w;
180 if ( cosom < 0.0f ) {
187 if ( ( 1.0f - cosom ) > 1e-6f ) {
189 omega = acos( cosom );
190 sinom = 1.0f / sin( omega );
191 scale0 = sin( ( 1.0f - t ) * omega ) * sinom;
192 scale1 = sin( t * omega ) * sinom;
194 scale0 = 1.0f - cosom * cosom;
195 sinom = idMath::InvSqrt( scale0 );
196 omega = idMath::ATan16( scale0 * sinom, cosom );
197 scale0 = idMath::Sin16( ( 1.0f - t ) * omega ) * sinom;
198 scale1 = idMath::Sin16( t * omega ) * sinom;
205 *this = ( scale0 * from ) + ( scale1 * temp );
214 idAngles idCQuat::ToAngles( void ) const {
215 return ToQuat().ToAngles();
223 idRotation idCQuat::ToRotation( void ) const {
224 return ToQuat().ToRotation();
232 idMat3 idCQuat::ToMat3( void ) const {
233 return ToQuat().ToMat3();
241 idMat4 idCQuat::ToMat4( void ) const {
242 return ToQuat().ToMat4();
250 const char *idCQuat::ToString( int precision ) const {
251 return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );