Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXQuatf.h
1 /********************************************************************************
2 * *
3 * S i n g l e - P r e c i s i o n Q u a t e r n i o n *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1994,2023 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXQUATF_H
22 #define FXQUATF_H
23 
24 namespace FX {
25 
26 
27 // Forward reference
28 class FXMat3f;
29 
30 
32 class FXAPI FXQuatf {
33 public:
34  FXfloat x;
35  FXfloat y;
36  FXfloat z;
37  FXfloat w;
38 public:
39 
43  FXQuatf(){}
44 
48  FXQuatf(const FXQuatf& q):x(q.x),y(q.y),z(q.z),w(q.w){}
49 
53  FXQuatf(const FXfloat v[]):x(v[0]),y(v[1]),z(v[2]),w(v[3]){}
54 
58  FXQuatf(FXfloat xx,FXfloat yy,FXfloat zz,FXfloat ww):x(xx),y(yy),z(zz),w(ww){}
59 
63  FXQuatf(const FXVec3f& axis,FXfloat phi);
64 
68  FXQuatf(const FXVec3f& fr,const FXVec3f& to);
69 
73  FXQuatf(FXfloat roll,FXfloat pitch,FXfloat yaw);
74 
78  FXQuatf(const FXVec3f& ex,const FXVec3f& ey,const FXVec3f& ez);
79 
84  FXQuatf(const FXVec3f& rot);
85 
89  FXfloat& operator[](FXint i){return (&x)[i];}
90 
94  const FXfloat& operator[](FXint i) const {return (&x)[i];}
95 
99  FXQuatf& operator=(const FXQuatf& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
100 
104  FXQuatf& operator=(const FXfloat v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
105 
109  FXQuatf& set(const FXQuatf& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
110 
114  FXQuatf& set(const FXfloat v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
115 
119  FXQuatf& set(FXfloat xx,FXfloat yy,FXfloat zz,FXfloat ww){x=xx;y=yy;z=zz;w=ww;return *this;}
120 
122  FXQuatf& operator*=(FXfloat n){ return set(x*n,y*n,z*n,w*n); }
123  FXQuatf& operator/=(FXfloat n){ return set(x/n,y/n,z/n,w/n); }
124  FXQuatf& operator+=(const FXQuatf& v){ return set(x+v.x,y+v.y,z+v.z,w+v.w); }
125  FXQuatf& operator-=(const FXQuatf& v){ return set(x-v.x,y-v.y,z-v.z,w-v.w); }
126  FXQuatf& operator*=(const FXQuatf& b){ return set(w*b.x+x*b.w+y*b.z-z*b.y, w*b.y+y*b.w+z*b.x-x*b.z, w*b.z+z*b.w+x*b.y-y*b.x, w*b.w-x*b.x-y*b.y-z*b.z); }
127  FXQuatf& operator/=(const FXQuatf& b){ return *this*=b.invert(); }
128 
130  operator FXfloat*(){return &x;}
131  operator const FXfloat*() const {return &x;}
132 
134  operator FXVec3f&(){return *reinterpret_cast<FXVec3f*>(this);}
135  operator const FXVec3f&() const {return *reinterpret_cast<const FXVec3f*>(this);}
136 
138  FXbool operator!() const {return x==0.0f && y==0.0f && z==0.0f && w==0.0f; }
139 
141  FXQuatf operator+() const { return *this; }
142  FXQuatf operator-() const { return FXQuatf(-x,-y,-z,-w); }
143 
145  FXfloat length2() const { return w*w+z*z+y*y+x*x; }
146  FXfloat length() const { return Math::sqrt(length2()); }
147 
149  FXQuatf& adjust();
150 
155  void setAxisAngle(const FXVec3f& axis,FXfloat phi);
156 
162  void getAxisAngle(FXVec3f& axis,FXfloat& phi) const;
163 
169  void setRotation(const FXVec3f& rot);
170 
175  FXVec3f getRotation() const;
176 
182  void setMRP(const FXVec3f& m);
183 
187  FXVec3f getMRP() const;
188 
190  void setRollPitchYaw(FXfloat roll,FXfloat pitch,FXfloat yaw);
191 
193  void getRollPitchYaw(FXfloat& roll,FXfloat& pitch,FXfloat& yaw) const;
194 
196  void setYawPitchRoll(FXfloat yaw,FXfloat pitch,FXfloat roll);
197 
199  void getYawPitchRoll(FXfloat& yaw,FXfloat& pitch,FXfloat& roll) const;
200 
202  void setRollYawPitch(FXfloat roll,FXfloat yaw,FXfloat pitch);
203 
205  void getRollYawPitch(FXfloat& roll,FXfloat& yaw,FXfloat& pitch) const;
206 
208  void setPitchRollYaw(FXfloat pitch,FXfloat roll,FXfloat yaw);
209 
211  void getPitchRollYaw(FXfloat& pitch,FXfloat& roll,FXfloat& yaw) const;
212 
214  void setPitchYawRoll(FXfloat pitch,FXfloat yaw,FXfloat roll);
215 
217  void getPitchYawRoll(FXfloat& pitch,FXfloat& yaw,FXfloat& roll) const;
218 
220  void setYawRollPitch(FXfloat yaw,FXfloat roll,FXfloat pitch);
221 
223  void getYawRollPitch(FXfloat& yaw,FXfloat& roll,FXfloat& pitch) const;
224 
226  void setAxes(const FXVec3f& ex,const FXVec3f& ey,const FXVec3f& ez);
227 
229  void getAxes(FXVec3f& ex,FXVec3f& ey,FXVec3f& ez) const;
230 
232  FXVec3f getXAxis() const;
233 
235  FXVec3f getYAxis() const;
236 
238  FXVec3f getZAxis() const;
239 
241  FXQuatf exp() const;
242 
244  FXQuatf log() const;
245 
247  FXQuatf pow(FXfloat t) const;
248 
250  FXQuatf conj() const { return FXQuatf(-x,-y,-z,w); }
251 
253  FXQuatf unitinvert() const { return FXQuatf(-x,-y,-z,w); }
254 
256  FXQuatf invert() const { FXfloat m(length2()); return FXQuatf(-x/m,-y/m,-z/m,w/m); }
257 
260  };
261 
262 
264 static inline FXQuatf operator*(const FXQuatf& a,FXfloat n){return FXQuatf(a.x*n,a.y*n,a.z*n,a.w*n);}
265 static inline FXQuatf operator*(FXfloat n,const FXQuatf& a){return FXQuatf(n*a.x,n*a.y,n*a.z,n*a.w);}
266 static inline FXQuatf operator/(const FXQuatf& a,FXfloat n){return FXQuatf(a.x/n,a.y/n,a.z/n,a.w/n);}
267 static inline FXQuatf operator/(FXfloat n,const FXQuatf& a){return FXQuatf(n/a.x,n/a.y,n/a.z,n/a.w);}
268 
270 static inline FXQuatf operator*(const FXQuatf& a,const FXQuatf& b){ return FXQuatf(a.w*b.x+a.x*b.w+a.y*b.z-a.z*b.y, a.w*b.y+a.y*b.w+a.z*b.x-a.x*b.z, a.w*b.z+a.z*b.w+a.x*b.y-a.y*b.x, a.w*b.w-a.x*b.x-a.y*b.y-a.z*b.z); }
271 static inline FXQuatf operator/(const FXQuatf& a,const FXQuatf& b){ return a*b.invert(); }
272 
274 extern FXAPI FXVec3f operator*(const FXQuatf& q,const FXVec3f& v);
275 
277 extern FXAPI FXVec3f operator*(const FXVec3f& v,const FXQuatf& q);
278 
280 static inline FXQuatf operator+(const FXQuatf& a,const FXQuatf& b){ return FXQuatf(a.x+b.x,a.y+b.y,a.z+b.z,a.w+b.w); }
281 static inline FXQuatf operator-(const FXQuatf& a,const FXQuatf& b){ return FXQuatf(a.x-b.x,a.y-b.y,a.z-b.z,a.w-b.w); }
282 
284 static inline FXbool operator==(const FXQuatf& a,FXfloat n){return a.x==n && a.y==n && a.z==n && a.w==n;}
285 static inline FXbool operator!=(const FXQuatf& a,FXfloat n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
286 static inline FXbool operator==(FXfloat n,const FXQuatf& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
287 static inline FXbool operator!=(FXfloat n,const FXQuatf& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
288 
290 static inline FXbool operator==(const FXQuatf& a,const FXQuatf& b){ return a.x==b.x && a.y==b.y && a.z==b.z && a.w==b.w; }
291 static inline FXbool operator!=(const FXQuatf& a,const FXQuatf& b){ return a.x!=b.x || a.y!=b.y || a.z!=b.z || a.w!=b.w; }
292 
294 static inline FXQuatf normalize(const FXQuatf& q){ return q*Math::rsqrt(q.length2()); }
295 
297 static inline FXQuatf fastnormalize(const FXQuatf& q){ return q*((3.0f-q.length2())*0.5f); }
298 
299 
301 extern FXAPI FXQuatf arc(const FXVec3f& a,const FXVec3f& b);
302 
304 extern FXAPI FXQuatf lerp(const FXQuatf& u,const FXQuatf& v,FXfloat f);
305 
307 extern FXAPI FXQuatf lerpdot(const FXQuatf& u,const FXQuatf& v,FXfloat f);
308 
309 
310 
312 extern FXAPI FXQuatf hermite(const FXQuatf& q0,const FXVec3f& r0,const FXQuatf& q1,const FXVec3f& r1,FXfloat t);
313 
315 extern FXAPI FXVec3f omegaBody(const FXQuatf& q0,const FXQuatf& q1,FXfloat dt);
316 
318 extern FXAPI FXQuatf quatDot(const FXQuatf& q,const FXVec3f& omega);
319 
322 extern FXAPI FXVec3f omegaDot(const FXMat3f& M,const FXVec3f& omega,const FXVec3f& torq);
323 
325 extern FXAPI FXStream& operator<<(FXStream& store,const FXQuatf& v);
326 
328 extern FXAPI FXStream& operator>>(FXStream& store,FXQuatf& v);
329 
330 }
331 
332 #endif
~FXQuatf()
Destructor.
Definition: FXQuatf.h:259
FXfloat length2() const
Length and square of length.
Definition: FXQuatf.h:145
FXQuatf(const FXfloat v[])
Construct from array of four floats.
Definition: FXQuatf.h:53
const FXfloat & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXQuatf.h:94
FXQuatf()
Default constructor; value is not initialized.
Definition: FXQuatf.h:43
FXQuatf & operator=(const FXfloat v[])
Assignment from array of four floats.
Definition: FXQuatf.h:104
FXbool operator!() const
Test if zero.
Definition: FXQuatf.h:138
FXQuatf(FXfloat xx, FXfloat yy, FXfloat zz, FXfloat ww)
Construct from four components.
Definition: FXQuatf.h:58
FXQuatf & operator*=(FXfloat n)
Assigning operators.
Definition: FXQuatf.h:122
FXQuatf unitinvert() const
Invert unit quaternion.
Definition: FXQuatf.h:253
FXQuatf conj() const
Conjugate quaternion.
Definition: FXQuatf.h:250
Definition: FX4Splitter.h:28
FXfloat & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXQuatf.h:89
FXQuatf invert() const
Invert quaternion.
Definition: FXQuatf.h:256
Single-precision quaternion.
Definition: FXQuatf.h:32
Single-precision 3-element vector.
Definition: FXVec3f.h:28
FXQuatf & operator=(const FXQuatf &v)
Assignment from other quaternion.
Definition: FXQuatf.h:99
FXQuatf operator+() const
Unary.
Definition: FXQuatf.h:141
FXMat2d invert() const
Invert.
FXQuatf(const FXQuatf &q)
Copy constructor.
Definition: FXQuatf.h:48

Copyright © 1997-2022 Jeroen van der Zijp