]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/idlib/math/Rotation.cpp
hello world
[icculus/iodoom3.git] / neo / idlib / math / Rotation.cpp
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
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.
13
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.
18
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/>.
21
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.
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 #include "../precompiled.h"
30 #pragma hdrstop
31
32
33 /*
34 ============
35 idRotation::ToAngles
36 ============
37 */
38 idAngles idRotation::ToAngles( void ) const {
39         return ToMat3().ToAngles();
40 }
41
42 /*
43 ============
44 idRotation::ToQuat
45 ============
46 */
47 idQuat idRotation::ToQuat( void ) const {
48         float a, s, c;
49
50         a = angle * ( idMath::M_DEG2RAD * 0.5f );
51         idMath::SinCos( a, s, c );
52         return idQuat( vec.x * s, vec.y * s, vec.z * s, c );
53 }
54
55 /*
56 ============
57 idRotation::toMat3
58 ============
59 */
60 const idMat3 &idRotation::ToMat3( void ) const {
61         float wx, wy, wz;
62         float xx, yy, yz;
63         float xy, xz, zz;
64         float x2, y2, z2;
65         float a, c, s, x, y, z;
66
67         if ( axisValid ) {
68                 return axis;
69         }
70
71         a = angle * ( idMath::M_DEG2RAD * 0.5f );
72         idMath::SinCos( a, s, c );
73
74         x = vec[0] * s;
75         y = vec[1] * s;
76         z = vec[2] * s;
77
78         x2 = x + x;
79         y2 = y + y;
80         z2 = z + z;
81
82         xx = x * x2;
83         xy = x * y2;
84         xz = x * z2;
85
86         yy = y * y2;
87         yz = y * z2;
88         zz = z * z2;
89
90         wx = c * x2;
91         wy = c * y2;
92         wz = c * z2;
93
94         axis[ 0 ][ 0 ] = 1.0f - ( yy + zz );
95         axis[ 0 ][ 1 ] = xy - wz;
96         axis[ 0 ][ 2 ] = xz + wy;
97
98         axis[ 1 ][ 0 ] = xy + wz;
99         axis[ 1 ][ 1 ] = 1.0f - ( xx + zz );
100         axis[ 1 ][ 2 ] = yz - wx;
101
102         axis[ 2 ][ 0 ] = xz - wy;
103         axis[ 2 ][ 1 ] = yz + wx;
104         axis[ 2 ][ 2 ] = 1.0f - ( xx + yy );
105
106         axisValid = true;
107
108         return axis;
109 }
110
111 /*
112 ============
113 idRotation::ToMat4
114 ============
115 */
116 idMat4 idRotation::ToMat4( void ) const {
117         return ToMat3().ToMat4();
118 }
119
120 /*
121 ============
122 idRotation::ToAngularVelocity
123 ============
124 */
125 idVec3 idRotation::ToAngularVelocity( void ) const {
126         return vec * DEG2RAD( angle );
127 }
128
129 /*
130 ============
131 idRotation::Normalize180
132 ============
133 */
134 void idRotation::Normalize180( void ) {
135         angle -= floor( angle / 360.0f ) * 360.0f;
136         if ( angle > 180.0f ) {
137                 angle -= 360.0f;
138         }
139         else if ( angle < -180.0f ) {
140                 angle += 360.0f;
141         }
142 }
143
144 /*
145 ============
146 idRotation::Normalize360
147 ============
148 */
149 void idRotation::Normalize360( void ) {
150         angle -= floor( angle / 360.0f ) * 360.0f;
151         if ( angle > 360.0f ) {
152                 angle -= 360.0f;
153         }
154         else if ( angle < 0.0f ) {
155                 angle += 360.0f;
156         }
157 }