]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/splines/math_quaternion.h
various: add explicit default contructors
[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         quat_t( const quat_t & ) = default;
42
43         friend void toQuat( idVec3_t &src, quat_t &dst );
44         friend void toQuat( angles_t &src, quat_t &dst );
45         friend void toQuat( mat3_t &src, quat_t &dst );
46
47         float *vec4( void );
48
49         float  operator[]( int index ) const;
50         float &operator[]( int index );
51
52         void set( float x, float y, float z, float w );
53
54         quat_t &operator=( const quat_t &a ) = default;
55
56         friend quat_t operator+( quat_t a, quat_t b );
57         quat_t &operator+=( quat_t a );
58
59         friend quat_t operator-( quat_t a, quat_t b );
60         quat_t &operator-=( quat_t a );
61
62         friend quat_t operator*( quat_t a, float b );
63         friend quat_t operator*( float a, quat_t b );
64         quat_t &operator*=( float a );
65
66         friend int operator==( quat_t a, quat_t b );
67         friend int operator!=( quat_t a, quat_t b );
68
69         float Length( void );
70         quat_t &Normalize( void );
71
72         quat_t operator-();
73 };
74
75 inline quat_t::quat_t() {
76 }
77
78 inline quat_t::quat_t( float x, float y, float z, float w ) {
79         this->x = x;
80         this->y = y;
81         this->z = z;
82         this->w = w;
83 }
84
85 inline float *quat_t::vec4( void ) {
86         return &x;
87 }
88
89 inline float quat_t::operator[]( int index ) const {
90         assert( ( index >= 0 ) && ( index < 4 ) );
91         return ( &x )[ index ];
92 }
93
94 inline float& quat_t::operator[]( int index ) {
95         assert( ( index >= 0 ) && ( index < 4 ) );
96         return ( &x )[ index ];
97 }
98
99 inline void quat_t::set( float x, float y, float z, float w ) {
100         this->x = x;
101         this->y = y;
102         this->z = z;
103         this->w = w;
104 }
105
106 inline quat_t operator+( quat_t a, quat_t b ) {
107         return quat_t( a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w );
108 }
109
110 inline quat_t& quat_t::operator+=( quat_t a ) {
111         x += a.x;
112         y += a.y;
113         z += a.z;
114         w += a.w;
115
116         return *this;
117 }
118
119 inline quat_t operator-( quat_t a, quat_t b ) {
120         return quat_t( a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w );
121 }
122
123 inline quat_t& quat_t::operator-=( quat_t a ) {
124         x -= a.x;
125         y -= a.y;
126         z -= a.z;
127         w -= a.w;
128
129         return *this;
130 }
131
132 inline quat_t operator*( quat_t a, float b ) {
133         return quat_t( a.x * b, a.y * b, a.z * b, a.w * b );
134 }
135
136 inline quat_t operator*( float a, quat_t b ) {
137         return b * a;
138 }
139
140 inline quat_t& quat_t::operator*=( float a ) {
141         x *= a;
142         y *= a;
143         z *= a;
144         w *= a;
145
146         return *this;
147 }
148
149 inline int operator==( quat_t a, quat_t b ) {
150         return ( ( a.x == b.x ) && ( a.y == b.y ) && ( a.z == b.z ) && ( a.w == b.w ) );
151 }
152
153 inline int operator!=( quat_t a, quat_t b ) {
154         return ( ( a.x != b.x ) || ( a.y != b.y ) || (( a.z != b.z ) && ( a.w != b.w )) );
155 }
156
157 inline float quat_t::Length( void ) {
158         float length;
159
160         length = x * x + y * y + z * z + w * w;
161         return ( float )sqrt( length );
162 }
163
164 inline quat_t& quat_t::Normalize( void ) {
165         float length;
166         float ilength;
167
168         length = this->Length();
169         if ( length ) {
170                 ilength = 1 / length;
171                 x *= ilength;
172                 y *= ilength;
173                 z *= ilength;
174                 w *= ilength;
175         }
176
177         return *this;
178 }
179
180 inline quat_t quat_t::operator-() {
181         return quat_t( -x, -y, -z, -w );
182 }
183
184 #endif /* !__MATH_QUATERNION_H__ */