]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/splines/math_quaternion.h
allow undo “make detail/structural”, <3 @SpiKe, thanks @Garux, fix #76
[xonotic/netradiant.git] / libs / splines / math_quaternion.h
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #ifndef __MATH_QUATERNION_H__
23 #define __MATH_QUATERNION_H__
24
25 #include <assert.h>
26 #include <math.h>
27
28 class idVec3_t;
29 class angles_t;
30 class mat3_t;
31
32 class quat_t {
33 public:
34 float x;
35 float y;
36 float z;
37 float w;
38
39 quat_t();
40 quat_t( float x, float y, float z, float w );
41
42 friend void     toQuat( idVec3_t &src, quat_t &dst );
43 friend void     toQuat( angles_t &src, quat_t &dst );
44 friend void     toQuat( mat3_t &src, quat_t &dst );
45
46 float           *vec4( void );
47
48 float operator[]( int index ) const;
49 float           &operator[]( int index );
50
51 void            set( float x, float y, float z, float w );
52
53 void operator=( quat_t a );
54
55 friend quat_t operator+( quat_t a, quat_t b );
56 quat_t          &operator+=( quat_t a );
57
58 friend quat_t operator-( quat_t a, quat_t b );
59 quat_t          &operator-=( quat_t a );
60
61 friend quat_t operator*( quat_t a, float b );
62 friend quat_t operator*( float a, quat_t b );
63 quat_t          &operator*=( float a );
64
65 friend int operator==( quat_t a, quat_t b );
66 friend int operator!=( quat_t a, quat_t b );
67
68 float           Length( void );
69 quat_t          &Normalize( void );
70
71 quat_t operator-();
72 };
73
74 inline quat_t::quat_t() {
75 }
76
77 inline quat_t::quat_t( float x, float y, float z, float w ) {
78         this->x = x;
79         this->y = y;
80         this->z = z;
81         this->w = w;
82 }
83
84 inline float *quat_t::vec4( void ) {
85         return &x;
86 }
87
88 inline float quat_t::operator[]( int index ) const {
89         assert( ( index >= 0 ) && ( index < 4 ) );
90         return ( &x )[ index ];
91 }
92
93 inline float& quat_t::operator[]( int index ) {
94         assert( ( index >= 0 ) && ( index < 4 ) );
95         return ( &x )[ index ];
96 }
97
98 inline void quat_t::set( float x, float y, float z, float w ) {
99         this->x = x;
100         this->y = y;
101         this->z = z;
102         this->w = w;
103 }
104
105 inline void quat_t::operator=( quat_t a ) {
106         x = a.x;
107         y = a.y;
108         z = a.z;
109         w = a.w;
110 }
111
112 inline quat_t operator+( quat_t a, quat_t b ) {
113         return quat_t( a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w );
114 }
115
116 inline quat_t& quat_t::operator+=( quat_t a ) {
117         x += a.x;
118         y += a.y;
119         z += a.z;
120         w += a.w;
121
122         return *this;
123 }
124
125 inline quat_t operator-( quat_t a, quat_t b ) {
126         return quat_t( a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w );
127 }
128
129 inline quat_t& quat_t::operator-=( quat_t a ) {
130         x -= a.x;
131         y -= a.y;
132         z -= a.z;
133         w -= a.w;
134
135         return *this;
136 }
137
138 inline quat_t operator*( quat_t a, float b ) {
139         return quat_t( a.x * b, a.y * b, a.z * b, a.w * b );
140 }
141
142 inline quat_t operator*( float a, quat_t b ) {
143         return b * a;
144 }
145
146 inline quat_t& quat_t::operator*=( float a ) {
147         x *= a;
148         y *= a;
149         z *= a;
150         w *= a;
151
152         return *this;
153 }
154
155 inline int operator==( quat_t a, quat_t b ) {
156         return ( ( a.x == b.x ) && ( a.y == b.y ) && ( a.z == b.z ) && ( a.w == b.w ) );
157 }
158
159 inline int operator!=( quat_t a, quat_t b ) {
160         return ( ( a.x != b.x ) || ( a.y != b.y ) || (( a.z != b.z ) && ( a.w != b.w )) );
161 }
162
163 inline float quat_t::Length( void ) {
164         float length;
165
166         length = x * x + y * y + z * z + w * w;
167         return ( float )sqrt( length );
168 }
169
170 inline quat_t& quat_t::Normalize( void ) {
171         float length;
172         float ilength;
173
174         length = this->Length();
175         if ( length ) {
176                 ilength = 1 / length;
177                 x *= ilength;
178                 y *= ilength;
179                 z *= ilength;
180                 w *= ilength;
181         }
182
183         return *this;
184 }
185
186 inline quat_t quat_t::operator-() {
187         return quat_t( -x, -y, -z, -w );
188 }
189
190 #endif /* !__MATH_QUATERNION_H__ */