Itasca C++ Interface
Loading...
Searching...
No Matches
quat.h
Go to the documentation of this file.
1#pragma once
7
8#include "avect.h"
9#include "matrix.h"
10#include "vect.h"
11#include <cmath>
12#include <cassert>
13
19
20class Quat2 {
21public:
23 Quat2(): ang_(0) { }
25 Quat2(const double &a): ang_(a) { }
27 ~Quat2() { }
29 Quat2(const Quat2 &q): ang_(q.ang_) { }
31 constexpr Quat2 &operator=(const Quat2 &in) { ang_ = in.ang_; return *this; }
33 void setQuat(const double &a){ ang_ = a; }
34
36 const double &ang() const { return ang_; }
37
39 double &rang() { return ang_; }
40
42 void reset() { ang_=0.0; }
44 void ident() { ang_ = 0.0; }
46 static BASE_EXPORT Quat2 identity() { Quat2 ret(0.0); return ret;}
47
51 static BASE_EXPORT Quat2 fromVect(const DVect2 &v);
56 BASE_EXPORT void fromAxisAngle(const DVect2 &,const double &);
57 BASE_EXPORT void fromEuler(const DAVect2 &v) { *this = Quat2(v.z()) * dDegrad; }
58 BASE_EXPORT void fromEulerZXZ(const DAVect2& v) { *this = Quat2(v.z()) * dDegrad; }
60 BASE_EXPORT void incrementAxisAngle(const DVect2 &,const double &);
62 BASE_EXPORT void spinAboutZ(const double &);
64 BASE_EXPORT DVect2 rotate(const DVect2 &v) const;
67 BASE_EXPORT void fromUnitAxes(const DVect2 &,const DVect2 &);
69 BASE_EXPORT void increment(const DAVect2 &);
70
72 void conj() { ang_*=-1; }
74 Quat2 getConj() const { Quat2 q(-ang_); return q; }
76 void normalize() { }
77
79 Quat2 operator *(const double &d) const { Quat2 q(ang_*d); return q; }
81 Quat2 operator /(const double &d) const { Quat2 q(ang_/d); return q; }
83 friend Quat2 operator *(const double &d,const Quat2 &p2) { Quat2 q(p2*d); return q;}
86 Quat2 operator *(const Quat2 &v) const { Quat2 q(ang_+v.ang_); return q; }
88 Quat2 operator +(const Quat2 &v) const { Quat2 q(ang_+v.ang_); return q; }
89
91 const Quat2 & operator *=(const double &d) { ang_ *= d; return *this; }
93 const Quat2 & operator /=(const double &d) { ang_ /= d; return *this; }
96 const Quat2 & operator *=(const Quat2 &v) { ang_ += v.ang_; return *this; }
99 const Quat2 & operator +=(const Quat2 &v) { ang_ += v.ang_; return *this; }
101 bool operator ==(const Quat2 &v) const { return (ang_ == v.ang_); }
102
103private:
104 double ang_; //the angle of rotation
105};
106
109class Quat3 {
110public:
112 Quat3(): w_(0), i_(0), j_(0), k_(0) { }
115 Quat3(const double &w1,const double &x,const double &y,const double &z) { w_ = w1; i_ = x; j_ = y; k_ = z; }
117 ~Quat3() { }
119 Quat3(const Quat3 &q) { w_ = q.w_; i_ = q.i_; j_ = q.j_; k_ = q.k_; }
121 constexpr const Quat3 &operator=(const Quat3 &q) { w_ = q.w_; i_ = q.i_; j_ = q.j_; k_ = q.k_; return *this; }
123 void setQuat(const double &w1,const double &x,const double &y,const double &z){ w_ = w1; i_ = x; j_ = y; k_ = z; }
124
126 const double &w() const { return w_; }
128 const double &i() const { return i_; }
130 const double &j() const { return j_; }
132 const double &k() const { return k_; }
133
135 double &rw() { return w_; }
137 double &ri() { return i_; }
139 double &rj() { return j_; }
141 double &rk() { return k_; }
142
144 void reset() { w_=0.0; i_=0.0; j_=0.0; k_=0.0; }
146 void ident() { w_=1.0; i_=0.0; j_=0.0; k_=0.0; }
148 static BASE_EXPORT Quat3 identity() { Quat3 ret(1.0,0.0,0.0,0.0); return ret;}
150 BASE_EXPORT bool isNull() const { return (abs(w_) > std::numeric_limits<double>::epsilon() || abs(i_) > std::numeric_limits<double>::epsilon() || abs(j_) > std::numeric_limits<double>::epsilon() || abs(k_) > std::numeric_limits<double>::epsilon()) ? false : true; }
154 BASE_EXPORT DVect3 quatToAAngle() const;
156 static BASE_EXPORT Quat3 fromVect(const DVect3 &v);
158 BASE_EXPORT void fromMatrix(const DMatrix<3,3> &);
160 BASE_EXPORT void fromAxisAngle(const DVect3 &,const double &);
163 BASE_EXPORT void fromEuler(const DVect3 &);
166 BASE_EXPORT void fromEulerZXZ(const DVect3 &);
168 BASE_EXPORT void incrementAxisAngle(const DVect3 &,const double &);
170 BASE_EXPORT void spinAboutX(const double &);
172 BASE_EXPORT void spinAboutY(const double &);
174 BASE_EXPORT void spinAboutZ(const double &);
176 BASE_EXPORT DVect3 rotate(const DVect3 &v) const;
178 BASE_EXPORT DVect3 real() const { return DVect3(i_,j_,k_); }
179
181 BASE_EXPORT void fromUnitAxes(const DVect3 &,const DVect3 &);
183 BASE_EXPORT void increment(const DAVect3 &);
185 BASE_EXPORT DVect3 e1() const;
187 BASE_EXPORT DVect3 e2() const;
189 BASE_EXPORT DVect3 e3() const;
192 BASE_EXPORT DVect3 quatToEuler() const;
193
195 void conj() { i_=-i_; j_=-j_; k_=-k_; }
197 Quat3 getConj() const { Quat3 q(w_,-i_,-j_,-k_); return q; }
199 BASE_EXPORT void normalize() { double m=1.0/std::sqrt(w_*w_+i_*i_+j_*j_+k_*k_); w_*=m; i_*=m; j_*=m; k_*=m; }
200
202 Quat3 operator *(const double &d) const { Quat3 q(w_*d,i_*d,j_*d,k_*d); return q; }
204 Quat3 operator /(const double &d) const { Quat3 q(w_/d,i_/d,j_/d,k_/d); return q; }
206 friend Quat3 operator *(const double &d,const Quat3 &p2) {Quat3 q(p2*d); return q;}
208 Quat3 operator *(const Quat3 &v) const { Quat3 q(w_*v.w_-i_*v.i_-j_*v.j_-k_*v.k_,w_*v.i_+i_*v.w_+j_*v.k_-k_*v.j_,w_*v.j_-i_*v.k_+j_*v.w_+k_*v.i_,w_*v.k_+i_*v.j_-j_*v.i_+k_*v.w_); return q; }
210 Quat3 operator +(const Quat3 &v) const { Quat3 q(w_+v.w_,i_+v.i_,j_+v.j_,k_+v.k_); return q; }
212 Quat3 friend operator *(const DVect3 &v,const Quat3 &q) {Quat3 q2(0.0,v.x(),v.y(),v.z()); return q2*q;}
214 Quat3 operator *(const DVect3 &v) const {Quat3 q2(0.0,v.x(),v.y(),v.z()); return *this*q2;}
215
217 const Quat3 & operator *=(const double &d) { w_ *= d; i_ *= d; j_ *= d; k_ *= d; return *this; }
219 const Quat3 & operator /=(const double &d) { w_ /= d; i_ /= d; j_ /= d; k_ /= d; return *this; }
221 const Quat3 & operator *=(const Quat3 &v) { double tw(w_*v.w_-i_*v.i_-j_*v.j_-k_*v.k_);
222 double ti(w_*v.i_+i_*v.w_+j_*v.k_-k_*v.j_);
223 double tj(w_*v.j_-i_*v.k_+j_*v.w_+k_*v.i_);
224 double tk(w_*v.k_+i_*v.j_-j_*v.i_+k_*v.w_);
225 //double tw(v.w_*w_-v.i_*i_-v.j_*j_-v.k_*k_);
226 //double ti(v.w_*i_+v.i_*w_+v.j_*k_-v.k_*j_);
227 //double tj(v.w_*j_-v.i_*k_+v.j_*w_+v.k_*i_);
228 //double tk(v.w_*k_+v.i_*j_-v.j_*i_+v.k_*w_);
229 w_ = tw; i_ = ti; j_ = tj; k_ = tk;
230 return *this; }
231
232 const Quat3 & operator +=(const Quat3 &v) { w_ += v.w_; i_ += v.i_; j_ += v.j_; k_ += v.k_; return *this; }
234 bool operator ==(const Quat3 &v) const { return (w_ == v.w_ && i_==v.i_ && j_==v.j_ && k_==v.k_); }
235
236private:
237 double w_; //the real amplitude part
238 double i_,j_,k_; //the imaginary parts
239};
240
241namespace base {
242 BASE_EXPORT string ts(const Quat2 &q, int width=0, char notation = '\0', int precision = -1, char fill = ' ');
243
244 BASE_EXPORT string ts(const Quat3 &q, int width=0, char notation = '\0', int precision = -1, char fill = ' ');
245}
246
Angular Vectors.
DMatrix is a Matrix that defaults to type double...
Definition matrix.h:741
2D quaternion-like utility class. In this case only the angle (in radians) is stored as opposed to th...
Definition quat.h:20
Quat2 operator+(const Quat2 &v) const
Addition of quaterions. This is also the composition of rotations in 2D.
Definition quat.h:88
BASE_EXPORT void fromUnitAxes(const DVect2 &, const DVect2 &)
Definition quat.cpp:62
double & rang()
Access a reference to the angle.
Definition quat.h:39
BASE_EXPORT void incrementAxisAngle(const DVect2 &, const double &)
Increment by this rotation. In this case the axis is ignored.
Definition quat.cpp:49
constexpr Quat2 & operator=(const Quat2 &in)
Equals operator.
Definition quat.h:31
Quat2 operator*(const double &d) const
Multiplication by a double.
Definition quat.h:79
void conj()
Convert to the conjugate quaternion - rotation by the opposite sign.
Definition quat.h:72
void setQuat(const double &a)
Set the angle.
Definition quat.h:33
Quat2(const double &a)
Constructor initializing the angle.
Definition quat.h:25
static BASE_EXPORT Quat2 identity()
Return the identity quaternion.
Definition quat.h:46
Quat2(const Quat2 &q)
Copy constructor.
Definition quat.h:29
Quat2 getConj() const
Return the conjugate of the quaternion.
Definition quat.h:74
Quat2()
Default constructor.
Definition quat.h:23
static BASE_EXPORT Quat2 fromVect(const DVect2 &v)
Convert a DVect2 into a quaternion. This assumes a rotation from the positive x axis.
Definition quat.cpp:17
const double & ang() const
Access the angle.
Definition quat.h:36
BASE_EXPORT void fromMatrix(const DMatrix< 2, 2 > &)
Convert a matrix into a quaternion.
Definition quat.cpp:21
BASE_EXPORT void spinAboutZ(const double &)
Just spin about the Z axis.
Definition quat.cpp:53
BASE_EXPORT void increment(const DAVect2 &)
Increment the quaternion due to a rotation about Z by the DAVect.
Definition quat.cpp:66
~Quat2()
Destructor.
Definition quat.h:27
const Quat2 & operator/=(const double &d)
In place division by a double.
Definition quat.h:93
const Quat2 & operator*=(const double &d)
In place multiplaction by a double.
Definition quat.h:91
BASE_EXPORT void fromAxisAngle(const DVect2 &, const double &)
Definition quat.cpp:45
bool operator==(const Quat2 &v) const
Euality operator.
Definition quat.h:101
void reset()
Reset the angle to 0.
Definition quat.h:42
BASE_EXPORT DMatrix< 2, 2 > quatToMatrix() const
Convert the quaternion to a rotation matrix.
Definition quat.cpp:6
const Quat2 & operator+=(const Quat2 &v)
Definition quat.h:99
void normalize()
Normalize the quaternion.
Definition quat.h:76
BASE_EXPORT DVect2 rotate(const DVect2 &v) const
Rotate a DVect2 by the quaternian to get a new DVect2.
Definition quat.cpp:57
void ident()
Reset to the identity.
Definition quat.h:44
Quat2 operator/(const double &d) const
Division by a double.
Definition quat.h:81
3D quaternion utility class.
Definition quat.h:109
Quat3(const Quat3 &q)
Copy constructor.
Definition quat.h:119
BASE_EXPORT DVect3 rotate(const DVect3 &v) const
Rotate a DVect3 by the quaternian to get a new DVect3.
Definition quat.cpp:221
BASE_EXPORT void increment(const DAVect3 &)
Increment the quaternion due to a rotation about the x, y, and z axes by the DAVect.
Definition quat.cpp:257
const Quat3 & operator/=(const double &d)
In place division by a double.
Definition quat.h:219
constexpr const Quat3 & operator=(const Quat3 &q)
Equality operator.
Definition quat.h:121
const double & k() const
Access the third imaginary part.
Definition quat.h:132
Quat3 getConj() const
Return the conjugate of the quaternion.
Definition quat.h:197
void setQuat(const double &w1, const double &x, const double &y, const double &z)
Set the real and imaginary parts.
Definition quat.h:123
void conj()
Convert the quaternion to the conjugate.
Definition quat.h:195
double & rj()
Access a reference to the second imaginary part.
Definition quat.h:139
static BASE_EXPORT Quat3 fromVect(const DVect3 &v)
Convert a DVect3 into a quaternion.
Definition quat.cpp:119
Quat3 operator+(const Quat3 &v) const
Addition of two quaternions.
Definition quat.h:210
BASE_EXPORT void incrementAxisAngle(const DVect3 &, const double &)
Increment by this rotation.
Definition quat.cpp:203
double & rw()
Access a reference to the real part.
Definition quat.h:135
const Quat3 & operator+=(const Quat3 &v)
In place addition of two quaternions.
Definition quat.h:232
BASE_EXPORT DVect3 e1() const
Get component e1 of the rotation matrix.
Definition quat.cpp:261
~Quat3()
Default destructor.
Definition quat.h:117
Quat3 operator*(const double &d) const
Multiplication by a double.
Definition quat.h:202
BASE_EXPORT bool isNull() const
Returns a boolean indicating whether or not the quaternion is null.
Definition quat.h:150
BASE_EXPORT void fromEuler(const DVect3 &)
Definition quat.cpp:173
static BASE_EXPORT Quat3 identity()
Returns the identity quaternion.
Definition quat.h:148
const double & j() const
Access the second imaginary part.
Definition quat.h:130
BASE_EXPORT void fromUnitAxes(const DVect3 &, const DVect3 &)
Take 2 orthogonal axis for an axis system and convert to a quaternion.
Definition quat.cpp:227
void ident()
Reset the quaternion to the identity.
Definition quat.h:146
BASE_EXPORT DVect3 quatToAAngle() const
Convert to Axis Angle where the magnitude of the axis is the angle in radians.
Definition quat.cpp:98
double & rk()
Access a reference to the third imaginary part.
Definition quat.h:141
Quat3()
Default constructor.
Definition quat.h:112
BASE_EXPORT void spinAboutY(const double &)
Just spin about the Y axis.
Definition quat.cpp:213
BASE_EXPORT DVect3 e3() const
Get component e3 of the rotation matrix.
Definition quat.cpp:269
Quat3(const double &w1, const double &x, const double &y, const double &z)
Definition quat.h:115
const double & w() const
Access the real part.
Definition quat.h:126
BASE_EXPORT DVect3 e2() const
Get component e2 of the rotation matrix.
Definition quat.cpp:265
double & ri()
Access a reference to the first imaginary part.
Definition quat.h:137
BASE_EXPORT DVect3 real() const
Return the "real" vector part of the quaternian.
Definition quat.h:178
void reset()
Reset the quaternion.
Definition quat.h:144
BASE_EXPORT void spinAboutX(const double &)
Just spin about the X axis.
Definition quat.cpp:209
BASE_EXPORT void fromAxisAngle(const DVect3 &, const double &)
Take an axis-angle representation and convert it to a quaternion where the angle is in radians.
Definition quat.cpp:159
const double & i() const
Access the first imaginary part.
Definition quat.h:128
BASE_EXPORT DMatrix< 3, 3 > quatToMatrix() const
Convert the quaternion to a rotation matrix.
Definition quat.cpp:70
const Quat3 & operator*=(const double &d)
In place multiplaction by a double.
Definition quat.h:217
BASE_EXPORT void fromEulerZXZ(const DVect3 &)
Definition quat.cpp:195
bool operator==(const Quat3 &v) const
Euality operator.
Definition quat.h:234
BASE_EXPORT void spinAboutZ(const double &)
Just spin about the Z axis.
Definition quat.cpp:217
BASE_EXPORT DVect3 quatToEuler() const
Definition quat.cpp:273
Quat3 operator/(const double &d) const
Division by a double.
Definition quat.h:204
BASE_EXPORT void fromMatrix(const DMatrix< 3, 3 > &)
Convert a matrix into a quaternion.
Definition quat.cpp:128
BASE_EXPORT void normalize()
Normalize the quaternion.
Definition quat.h:199
#define BASE_EXPORT
Definition basedef.h:24
A template-based matrix class, size fixed at compile time.
2D and 3D vector utility classes.