NGL  6.5
The NCCA Graphics Library
Quaternion.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2009 Jon Macey
3 
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #ifndef QUATERNION_H_
18 #define QUATERNION_H_
19 //----------------------------------------------------------------------------------------------------------------------
26 //----------------------------------------------------------------------------------------------------------------------
27 // must include types.h first for Real and GLEW if required
28 #include "Types.h"
29 #include "Vec4.h"
30 
31 namespace ngl
32 {
33 
34 // need to pre-declare the matrix class
35 class Mat4;
36 
37 
39 {
40 
41  public:
42 
43  //----------------------------------------------------------------------------------------------------------------------
50  //----------------------------------------------------------------------------------------------------------------------
51  Quaternion(const Real _s=0.0f,const Real _x=0.0f,const Real _y=0.0f,const Real _z=0.0f) noexcept:
52  m_s(_s),
53  m_x(_x),
54  m_y(_y),
55  m_z(_z) {;}
56  //----------------------------------------------------------------------------------------------------------------------
61  //----------------------------------------------------------------------------------------------------------------------
62  Quaternion(const Mat4 &_m) noexcept;
63  //----------------------------------------------------------------------------------------------------------------------
69  //----------------------------------------------------------------------------------------------------------------------
70  Quaternion(const Vec3 &_rot) noexcept;
71  //----------------------------------------------------------------------------------------------------------------------
74  //----------------------------------------------------------------------------------------------------------------------
75  Quaternion(const Quaternion& _q ) noexcept:
76  m_s(_q.m_s),
77  m_x(_q.m_x),
78  m_y(_q.m_y),
79  m_z(_q.m_z) {;}
80  //----------------------------------------------------------------------------------------------------------------------
86  //----------------------------------------------------------------------------------------------------------------------
87  void set( Real _s,Real _x,Real _y,Real _z) noexcept
88  {
89  m_s=_s;
90  m_x=_x;
91  m_y=_y;
92  m_z=_z;
93  }
94  //----------------------------------------------------------------------------------------------------------------------
97  //----------------------------------------------------------------------------------------------------------------------
98  Real getS() const noexcept{return m_s;}
99  //----------------------------------------------------------------------------------------------------------------------
102  //----------------------------------------------------------------------------------------------------------------------
103  Real getX() const noexcept{return m_x;}
104  //----------------------------------------------------------------------------------------------------------------------
107  //----------------------------------------------------------------------------------------------------------------------
108  Real getY() const noexcept{return m_y;}
109  //----------------------------------------------------------------------------------------------------------------------
112  //----------------------------------------------------------------------------------------------------------------------
113  Real getZ() const noexcept{return m_z;}
114  //----------------------------------------------------------------------------------------------------------------------
117  //----------------------------------------------------------------------------------------------------------------------
118  Vec4 getVector() const noexcept{return Vec4(m_x,m_y,m_z,0);}
119  //----------------------------------------------------------------------------------------------------------------------
122  //----------------------------------------------------------------------------------------------------------------------
123  void setVector( const Vec4 &_v) noexcept
124  {
125  m_x=_v.m_x;
126  m_y=_v.m_y;
127  m_z=_v.m_z;
128  }
129  //----------------------------------------------------------------------------------------------------------------------
132  //----------------------------------------------------------------------------------------------------------------------
133  void setS(Real &_s) noexcept {m_s=_s;}
134  //----------------------------------------------------------------------------------------------------------------------
137  //----------------------------------------------------------------------------------------------------------------------
138  void setX(Real &_x) noexcept {m_x=_x;}
139  //----------------------------------------------------------------------------------------------------------------------
142  //----------------------------------------------------------------------------------------------------------------------
143  void setY(Real &_y) noexcept {m_y=_y;}
144  //----------------------------------------------------------------------------------------------------------------------
147  //----------------------------------------------------------------------------------------------------------------------
148  void setZ(Real &_z) noexcept {m_z=_z;}
149  //----------------------------------------------------------------------------------------------------------------------
153  //----------------------------------------------------------------------------------------------------------------------
154  Quaternion operator *(const Quaternion& _q) const noexcept;
155  //----------------------------------------------------------------------------------------------------------------------
159  //----------------------------------------------------------------------------------------------------------------------
160  void operator *=(const Quaternion& _q ) noexcept;
161  //----------------------------------------------------------------------------------------------------------------------
165  //----------------------------------------------------------------------------------------------------------------------
166  Quaternion operator *(Real _s ) const noexcept;
167  //----------------------------------------------------------------------------------------------------------------------
171  //----------------------------------------------------------------------------------------------------------------------
172  void operator *=( Real _s ) noexcept;
173  //----------------------------------------------------------------------------------------------------------------------
177  //----------------------------------------------------------------------------------------------------------------------
178  Quaternion operator +(const Quaternion& _q ) const noexcept;
179  //----------------------------------------------------------------------------------------------------------------------
183  //----------------------------------------------------------------------------------------------------------------------
184  Quaternion operator -( const Quaternion& _q) const noexcept;
185  //----------------------------------------------------------------------------------------------------------------------
188  //----------------------------------------------------------------------------------------------------------------------
189  void operator +=(const Quaternion& _q ) noexcept;
190  //----------------------------------------------------------------------------------------------------------------------
193  //----------------------------------------------------------------------------------------------------------------------
194  void operator -=( const Quaternion& _q ) noexcept;
195 
196  //----------------------------------------------------------------------------------------------------------------------
199  //----------------------------------------------------------------------------------------------------------------------
200  void normalise() noexcept;
201  //----------------------------------------------------------------------------------------------------------------------
204  //----------------------------------------------------------------------------------------------------------------------
205  Real magnitude() const noexcept;
206  //----------------------------------------------------------------------------------------------------------------------
209  //----------------------------------------------------------------------------------------------------------------------
210  Quaternion conjugate() const noexcept{return Quaternion(m_s,-m_x,-m_y,-m_z);}
211  //----------------------------------------------------------------------------------------------------------------------
214  //----------------------------------------------------------------------------------------------------------------------
215  Quaternion inverse()const noexcept{return Quaternion(m_s,-m_x,-m_y,-m_z);}
216  //----------------------------------------------------------------------------------------------------------------------
219  //----------------------------------------------------------------------------------------------------------------------
220  void operator -() noexcept{m_x=-m_x; m_y=-m_y; m_z=-m_z;}
221  //----------------------------------------------------------------------------------------------------------------------
225  //----------------------------------------------------------------------------------------------------------------------
226  Quaternion operator-() const noexcept {return Quaternion(m_s,-m_x,-m_y,-m_z ); }
227  //----------------------------------------------------------------------------------------------------------------------
231  //----------------------------------------------------------------------------------------------------------------------
232  bool operator == (const Quaternion& _q) const noexcept;
233  //----------------------------------------------------------------------------------------------------------------------
238  //----------------------------------------------------------------------------------------------------------------------
239  static Quaternion slerp(const Quaternion &_q1,const Quaternion &_q2,const Real &_t) noexcept;
240 
241  //----------------------------------------------------------------------------------------------------------------------
250  //----------------------------------------------------------------------------------------------------------------------
251  Vec4 operator* (const Vec4 &_vec) const noexcept;
252  //----------------------------------------------------------------------------------------------------------------------
255  //----------------------------------------------------------------------------------------------------------------------
256  void rotateX(Real _angle) noexcept;
257  //----------------------------------------------------------------------------------------------------------------------
260  //----------------------------------------------------------------------------------------------------------------------
261  void rotateY(Real _angle) noexcept;
262  //----------------------------------------------------------------------------------------------------------------------
265  //----------------------------------------------------------------------------------------------------------------------
266  void rotateZ(Real _angle) noexcept;
267  //----------------------------------------------------------------------------------------------------------------------
271  //----------------------------------------------------------------------------------------------------------------------
272  void fromAxisAngle(const Vec3 &_axis,Real _angle) noexcept;
273  //----------------------------------------------------------------------------------------------------------------------
279  //----------------------------------------------------------------------------------------------------------------------
280  void fromEulerAngles(const Real _x,const Real _y,const Real _z) noexcept;
281  //----------------------------------------------------------------------------------------------------------------------
285  //----------------------------------------------------------------------------------------------------------------------
286  void rotatePoint(const Quaternion& _r,Vec3 & io_p) noexcept;
287  //----------------------------------------------------------------------------------------------------------------------
291  //----------------------------------------------------------------------------------------------------------------------
292  void toAxisAngle(Vec3 &o_axis,Real &o_angle) noexcept;
293  //----------------------------------------------------------------------------------------------------------------------
296  //----------------------------------------------------------------------------------------------------------------------
297  Mat4 toMat4() const noexcept;
298  //----------------------------------------------------------------------------------------------------------------------
301  //----------------------------------------------------------------------------------------------------------------------
302  Mat4 toMat4Transpose() const noexcept;
303 
304  protected :
313 
314 }; // end of class
315 
316 
317 }
318 
319 #include "Mat4.h"
320 
321 #endif
bool operator==(const ngl::Mat3 &_m1, const ngl::Mat3 &_m2)
Definition: Mat3.h:297
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
main definition of types and namespace
#define NGL_DLLEXPORT
Definition: Types.h:65
Real m_s
the quaternion data for the scalar real part
Definition: Quaternion.h:306
void setVector(const Vec4 &_v) noexcept
mutator for the vector components as an Vec4
Definition: Quaternion.h:123
void setS(Real &_s) noexcept
mutator for the scalar part
Definition: Quaternion.h:133
Real getZ() const noexcept
accesor for the z vector components
Definition: Quaternion.h:113
void setY(Real &_y) noexcept
mutator for the y vector part
Definition: Quaternion.h:143
Real m_y
the quaternion data for y
Definition: Quaternion.h:310
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
implementation files for RibExport class
Definition: AABB.cpp:22
Real m_x
the quaternion data for x
Definition: Quaternion.h:308
Vec4 getVector() const noexcept
accesor for the vector components as an Vec4
Definition: Quaternion.h:118
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
void setX(Real &_x) noexcept
mutator for the x vector part
Definition: Quaternion.h:138
encapsulates a 4d Homogenous Point / Vector object
Quaternion inverse() const noexcept
conjugate negate the vector part can also be done by the -() operator
Definition: Quaternion.h:215
Real getX() const noexcept
accesor for the x vector components
Definition: Quaternion.h:103
Quaternion(const Quaternion &_q) noexcept
copy constructor
Definition: Quaternion.h:75
void setZ(Real &_z) noexcept
mutator for the z vector part
Definition: Quaternion.h:148
Real getS() const noexcept
accesor for the scalar part
Definition: Quaternion.h:98
Quaternion operator-() const noexcept
returns the inverse of the quaternion (aka conjugate) the scalar part remains the same and we reverse...
Definition: Quaternion.h:226
Quaternion(const Real _s=0.0f, const Real _x=0.0f, const Real _y=0.0f, const Real _z=0.0f) noexcept
constructor I use the format used in John Vinces bood where we have a scalar and a vector...
Definition: Quaternion.h:51
Quaternion conjugate() const noexcept
conjugate negate the vector part can also be done by the -() operator
Definition: Quaternion.h:210
Real m_z
the quaternion data for z
Definition: Quaternion.h:312
Vec2 operator*(Real _k, const Vec2 &_v) noexcept
scalar * vector operator
Definition: Vec2.h:286
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
Real getY() const noexcept
accesor for the y vector components
Definition: Quaternion.h:108
GLclampf f
Definition: glew.h:3511