]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/splines/math_angles.h
eol style
[xonotic/netradiant.git] / libs / splines / math_angles.h
1 /*
2 Copyright (C) 1999-2007 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_ANGLES_H__
23 #define __MATH_ANGLES_H__
24
25 #include <stdlib.h>
26 #include <assert.h>
27
28 #include "math_vector.h"
29
30 class mat3_t;
31 class quat_t;
32 class idVec3;
33 typedef idVec3 &vec3_p;
34
35 class angles_t {
36 public:
37         float                   pitch;
38         float                   yaw;
39         float                   roll;
40
41                                         angles_t();
42                                         angles_t( float pitch, float yaw, float roll );
43                                         angles_t( const idVec3 &vec );
44
45         friend void             toAngles( idVec3 &src, angles_t &dst );
46         friend void             toAngles( quat_t &src, angles_t &dst );
47         friend void             toAngles( mat3_t &src, angles_t &dst );
48
49                                         operator vec3_p();
50
51         float                   operator[]( int index ) const;
52         float&                  operator[]( int index );
53
54         void                    set( float pitch, float yaw, float roll );
55
56         void                    operator=( angles_t const &a );
57         void                    operator=( idVec3 const &a );
58
59         friend angles_t operator+( const angles_t &a, const angles_t &b );
60         angles_t                &operator+=( angles_t const &a );
61         angles_t                &operator+=( idVec3 const &a );
62
63         friend angles_t operator-( angles_t &a, angles_t &b );
64         angles_t                &operator-=( angles_t &a );
65
66         friend angles_t operator*( const angles_t &a, float b );
67         friend angles_t operator*( float a, const angles_t &b );
68         angles_t                &operator*=( float a );
69
70         friend int              operator==(     angles_t &a, angles_t &b );
71                                         
72         friend int              operator!=(     angles_t &a, angles_t &b );
73
74         void                    toVectors( idVec3 *forward, idVec3 *right = NULL, idVec3 *up = NULL );
75         idVec3                  toForward( void );
76
77         angles_t                &Zero( void );
78
79         angles_t                &Normalize360( void );
80         angles_t                &Normalize180( void );
81 };
82
83 extern angles_t ang_zero;
84
85 inline angles_t::angles_t() {}
86
87 inline angles_t::angles_t( float pitch, float yaw, float roll ) {
88         this->pitch = pitch;
89         this->yaw       = yaw;
90         this->roll      = roll;
91 }
92
93 inline angles_t::angles_t( const idVec3 &vec ) {
94         this->pitch = vec.x;
95         this->yaw       = vec.y;
96         this->roll      = vec.z;
97 }
98
99 inline float angles_t::operator[]( int index ) const {
100         assert( ( index >= 0 ) && ( index < 3 ) );
101         return ( &pitch )[ index ];
102 }
103
104 inline float& angles_t::operator[]( int index ) {
105         assert( ( index >= 0 ) && ( index < 3 ) );
106         return ( &pitch )[ index ];
107 }
108
109 inline angles_t::operator vec3_p( void ) {
110         return *( idVec3 * )&pitch;
111 }
112
113 inline void angles_t::set( float pitch, float yaw, float roll ) {
114         this->pitch = pitch;
115         this->yaw       = yaw;
116         this->roll      = roll;
117 }
118
119 inline void angles_t::operator=( angles_t const &a ) {
120         pitch   = a.pitch;
121         yaw             = a.yaw;
122         roll    = a.roll;
123 }
124
125 inline void angles_t::operator=( idVec3 const &a ) {
126         pitch   = a[ 0 ];
127         yaw             = a[ 1 ];
128         roll    = a[ 2 ];
129 }
130
131 inline angles_t operator+( const angles_t &a, const angles_t &b ) {
132         return angles_t( a.pitch + b.pitch, a.yaw + b.yaw, a.roll + b.roll );
133 }
134
135 inline angles_t& angles_t::operator+=( angles_t const &a ) {
136         pitch   += a.pitch;
137         yaw             += a.yaw;
138         roll    += a.roll;
139
140         return *this;
141 }
142
143 inline angles_t& angles_t::operator+=( idVec3 const &a ) {
144         pitch   += a.x;
145         yaw     += a.y;
146         roll    += a.z;
147
148         return *this;
149 }
150
151 inline angles_t operator-( angles_t &a, angles_t &b ) {
152         return angles_t( a.pitch - b.pitch, a.yaw - b.yaw, a.roll - b.roll );
153 }
154
155 inline angles_t& angles_t::operator-=( angles_t &a ) {
156         pitch   -= a.pitch;
157         yaw             -= a.yaw;
158         roll    -= a.roll;
159
160         return *this;
161 }
162
163 inline angles_t operator*( const angles_t &a, float b ) {
164         return angles_t( a.pitch * b, a.yaw * b, a.roll * b );
165 }
166
167 inline angles_t operator*( float a, const angles_t &b ) {
168         return angles_t( a * b.pitch, a * b.yaw, a * b.roll );
169 }
170
171 inline angles_t& angles_t::operator*=( float a ) {
172         pitch   *= a;
173         yaw             *= a;
174         roll    *= a;
175
176         return *this;
177 }
178
179 inline int operator==( angles_t &a, angles_t &b ) {
180         return ( ( a.pitch == b.pitch ) && ( a.yaw == b.yaw ) && ( a.roll == b.roll ) );
181 }
182
183 inline int operator!=( angles_t &a, angles_t &b ) {
184         return ( ( a.pitch != b.pitch ) || ( a.yaw != b.yaw ) || ( a.roll != b.roll ) );
185 }
186
187 inline angles_t& angles_t::Zero( void ) {
188         pitch   = 0.0f;
189         yaw             = 0.0f;
190         roll    = 0.0f;
191
192         return *this;
193 }
194
195 #endif /* !__MATH_ANGLES_H__ */