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

FXQuatd.h
1 /********************************************************************************
2 * *
3 * D o u b 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,2024 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 FXQUATD_H
22 #define FXQUATD_H
23 
24 namespace FX {
25 
26 
27 // Forward reference
28 class FXMat3d;
29 
30 
32 class FXAPI FXQuatd {
33 public:
34  double x;
35  double y;
36  double z;
37  double w;
38 public:
39 
43  FXQuatd(){}
44 
48  FXQuatd(const FXQuatd& q):x(q.x),y(q.y),z(q.z),w(q.w){}
49 
53  FXQuatd(const FXdouble v[]):x(v[0]),y(v[1]),z(v[2]),w(v[3]){}
54 
58  FXQuatd(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww):x(xx),y(yy),z(zz),w(ww){}
59 
63  FXQuatd(const FXVec3d& axis,FXdouble phi);
64 
68  FXQuatd(const FXVec3d& fr,const FXVec3d& to);
69 
73  FXQuatd(FXdouble roll,FXdouble pitch,FXdouble yaw);
74 
78  FXQuatd(const FXVec3d& ex,const FXVec3d& ey,const FXVec3d& ez);
79 
84  FXQuatd(const FXVec3d& rot);
85 
89  FXdouble& operator[](FXint i){return (&x)[i];}
90 
94  const FXdouble& operator[](FXint i) const {return (&x)[i];}
95 
99  FXQuatd& operator=(const FXQuatd& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
100 
104  FXQuatd& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
105 
109  FXQuatd& set(const FXQuatd& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
110 
114  FXQuatd& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
115 
119  FXQuatd& set(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww){x=xx;y=yy;z=zz;w=ww;return *this;}
120 
122  FXQuatd& operator*=(FXdouble n){ return set(x*n,y*n,z*n,w*n); }
123  FXQuatd& operator/=(FXdouble n){ return set(x/n,y/n,z/n,w/n); }
124  FXQuatd& operator+=(const FXQuatd& v){ return set(x+v.x,y+v.y,z+v.z,w+v.w); }
125  FXQuatd& operator-=(const FXQuatd& v){ return set(x-v.x,y-v.y,z-v.z,w-v.w); }
126  FXQuatd& operator*=(const FXQuatd& 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  FXQuatd& operator/=(const FXQuatd& b){ return *this*=b.invert(); }
128 
130  operator FXdouble*(){return &x;}
131  operator const FXdouble*() const {return &x;}
132 
134  operator FXVec3d&(){return *reinterpret_cast<FXVec3d*>(this);}
135  operator const FXVec3d&() const {return *reinterpret_cast<const FXVec3d*>(this);}
136 
138  FXbool operator!() const {return x==0.0 && y==0.0 && z==0.0 && w==0.0; }
139 
141  FXQuatd operator+() const { return *this; }
142  FXQuatd operator-() const { return FXQuatd(-x,-y,-z,-w); }
143 
145  FXdouble length2() const { return w*w+z*z+y*y+x*x; }
146  FXdouble length() const { return Math::sqrt(length2()); }
147 
149  FXQuatd& adjust();
150 
155  void setAxisAngle(const FXVec3d& axis,FXdouble phi);
156 
162  void getAxisAngle(FXVec3d& axis,FXdouble& phi) const;
163 
169  void setRotation(const FXVec3d& rot);
170 
175  FXVec3d getRotation() const;
176 
182  void setMRP(const FXVec3d& m);
183 
187  FXVec3d getMRP() const;
188 
190  void setRollPitchYaw(FXdouble roll,FXdouble pitch,FXdouble yaw);
191 
193  void getRollPitchYaw(FXdouble& roll,FXdouble& pitch,FXdouble& yaw) const;
194 
196  void setYawPitchRoll(FXdouble yaw,FXdouble pitch,FXdouble roll);
197 
199  void getYawPitchRoll(FXdouble& yaw,FXdouble& pitch,FXdouble& roll) const;
200 
202  void setRollYawPitch(FXdouble roll,FXdouble yaw,FXdouble pitch);
203 
205  void getRollYawPitch(FXdouble& roll,FXdouble& yaw,FXdouble& pitch) const;
206 
208  void setPitchRollYaw(FXdouble pitch,FXdouble roll,FXdouble yaw);
209 
211  void getPitchRollYaw(FXdouble& pitch,FXdouble& roll,FXdouble& yaw) const;
212 
214  void setPitchYawRoll(FXdouble pitch,FXdouble yaw,FXdouble roll);
215 
217  void getPitchYawRoll(FXdouble& pitch,FXdouble& yaw,FXdouble& roll) const;
218 
220  void setYawRollPitch(FXdouble yaw,FXdouble roll,FXdouble pitch);
221 
223  void getYawRollPitch(FXdouble& yaw,FXdouble& roll,FXdouble& pitch) const;
224 
226  void setAxes(const FXVec3d& ex,const FXVec3d& ey,const FXVec3d& ez);
227 
229  void getAxes(FXVec3d& ex,FXVec3d& ey,FXVec3d& ez) const;
230 
232  FXVec3d getXAxis() const;
233 
235  FXVec3d getYAxis() const;
236 
238  FXVec3d getZAxis() const;
239 
241  FXQuatd exp() const;
242 
244  FXQuatd log() const;
245 
247  FXQuatd pow(FXdouble t) const;
248 
250  FXQuatd conj() const { return FXQuatd(-x,-y,-z,w); }
251 
253  FXQuatd unitinvert() const { return FXQuatd(-x,-y,-z,w); }
254 
256  FXQuatd invert() const { FXdouble m(length2()); return FXQuatd(-x/m,-y/m,-z/m,w/m); }
257 
260  };
261 
262 
264 static inline FXQuatd operator*(const FXQuatd& a,FXdouble n){return FXQuatd(a.x*n,a.y*n,a.z*n,a.w*n);}
265 static inline FXQuatd operator*(FXdouble n,const FXQuatd& a){return FXQuatd(n*a.x,n*a.y,n*a.z,n*a.w);}
266 static inline FXQuatd operator/(const FXQuatd& a,FXdouble n){return FXQuatd(a.x/n,a.y/n,a.z/n,a.w/n);}
267 static inline FXQuatd operator/(FXdouble n,const FXQuatd& a){return FXQuatd(n/a.x,n/a.y,n/a.z,n/a.w);}
268 
270 static inline FXQuatd operator*(const FXQuatd& a,const FXQuatd& b){ return FXQuatd(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 FXQuatd operator/(const FXQuatd& a,const FXQuatd& b){ return a*b.invert(); }
272 
274 extern FXAPI FXVec3d operator*(const FXQuatd& q,const FXVec3d& v);
275 
277 extern FXAPI FXVec3d operator*(const FXVec3d& v,const FXQuatd& q);
278 
280 static inline FXQuatd operator+(const FXQuatd& a,const FXQuatd& b){ return FXQuatd(a.x+b.x,a.y+b.y,a.z+b.z,a.w+b.w); }
281 static inline FXQuatd operator-(const FXQuatd& a,const FXQuatd& b){ return FXQuatd(a.x-b.x,a.y-b.y,a.z-b.z,a.w-b.w); }
282 
284 static inline FXbool operator==(const FXQuatd& a,FXdouble n){return a.x==n && a.y==n && a.z==n && a.w==n;}
285 static inline FXbool operator!=(const FXQuatd& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
286 static inline FXbool operator==(FXdouble n,const FXQuatd& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
287 static inline FXbool operator!=(FXdouble n,const FXQuatd& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
288 
290 static inline FXbool operator==(const FXQuatd& a,const FXQuatd& b){ return a.x==b.x && a.y==b.y && a.z==b.z && a.w==b.w; }
291 static inline FXbool operator!=(const FXQuatd& a,const FXQuatd& b){ return a.x!=b.x || a.y!=b.y || a.z!=b.z || a.w!=b.w; }
292 
294 static inline FXQuatd normalize(const FXQuatd& q){ return q*Math::rsqrt(q.length2()); }
295 
297 static inline FXQuatd fastnormalize(const FXQuatd& q){ return q*((3.0-q.length2())*0.5); }
298 
299 
301 extern FXAPI FXQuatd arc(const FXVec3d& a,const FXVec3d& b);
302 
304 extern FXAPI FXQuatd lerp(const FXQuatd& u,const FXQuatd& v,FXdouble f);
305 
307 extern FXAPI FXQuatd lerpdot(const FXQuatd& u,const FXQuatd& v,FXdouble f);
308 
310 extern FXAPI FXQuatd hermite(const FXQuatd& q0,const FXVec3d& r0,const FXQuatd& q1,const FXVec3d& r1,FXdouble t);
311 
313 extern FXAPI FXVec3d omegaBody(const FXQuatd& q0,const FXQuatd& q1,FXdouble dt);
314 
316 extern FXAPI FXQuatd quatDot(const FXQuatd& q,const FXVec3d& omega);
317 
320 extern FXAPI FXVec3d omegaDot(const FXMat3d& M,const FXVec3d& omega,const FXVec3d& torq);
321 
323 extern FXAPI FXStream& operator<<(FXStream& store,const FXQuatd& v);
324 
326 extern FXAPI FXStream& operator>>(FXStream& store,FXQuatd& v);
327 
328 }
329 
330 #endif
FXQuatd operator+() const
Unary.
Definition: FXQuatd.h:141
FXQuatd & operator=(const FXdouble v[])
Assignment from array of four doubles.
Definition: FXQuatd.h:104
Double-precision quaternion.
Definition: FXQuatd.h:32
Double-precision 3-element vector.
Definition: FXVec3d.h:28
FXQuatd & operator=(const FXQuatd &v)
Assignment from other quaternion.
Definition: FXQuatd.h:99
FXQuatd unitinvert() const
Invert unit quaternion.
Definition: FXQuatd.h:253
FXdouble & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXQuatd.h:89
Definition: FX4Splitter.h:28
FXQuatd(FXdouble xx, FXdouble yy, FXdouble zz, FXdouble ww)
Construct from four components.
Definition: FXQuatd.h:58
FXbool operator!() const
Test if zero.
Definition: FXQuatd.h:138
const FXdouble & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXQuatd.h:94
FXQuatd invert() const
Invert quaternion.
Definition: FXQuatd.h:256
~FXQuatd()
Destructor.
Definition: FXQuatd.h:259
FXMat2d invert() const
Invert.
FXdouble length2() const
Length and square of length.
Definition: FXQuatd.h:145
FXQuatd conj() const
Conjugate quaternion.
Definition: FXQuatd.h:250
FXQuatd()
Default constructor; value is not initialized.
Definition: FXQuatd.h:43
FXQuatd & operator*=(FXdouble n)
Assigning operators.
Definition: FXQuatd.h:122
FXQuatd(const FXdouble v[])
Construct from array of four doubles.
Definition: FXQuatd.h:53
FXQuatd(const FXQuatd &q)
Copy constructor.
Definition: FXQuatd.h:48

Copyright © 1997-2022 Jeroen van der Zijp