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 explicit Quat2(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; }
32 constexpr Quat2 &operator=(double d) { ang_ = d; return *this; }
33 constexpr auto operator<=>(const Quat2 &in) const { return ang_ <=> in.ang_; }
34
36 void setQuat(double a){ ang_ = a; }
37
39 double ang() const { return ang_; }
40
42 double &rang() { return ang_; }
43
45 void reset() { ang_=0.0; }
47 void ident() { ang_ = 0.0; }
49 static BASE_EXPORT Quat2 identity() { Quat2 ret(0.0); return ret;}
50
54 static BASE_EXPORT Quat2 fromVect(const DVect2 &v);
59 BASE_EXPORT void fromAxisAngle(const DVect2 &,double);
60 BASE_EXPORT void fromEuler(const DAVect2 &v) { *this = Quat2(v.z()) * dDegrad; }
61 BASE_EXPORT void fromEulerZXZ(const DAVect2& v) { *this = Quat2(v.z()) * dDegrad; }
63 BASE_EXPORT void incrementAxisAngle(const DVect2 &,double);
65 BASE_EXPORT void spinAboutZ(double);
67 BASE_EXPORT DVect2 rotate(const DVect2 &v) const;
70 BASE_EXPORT void fromUnitAxes(const DVect2 &,const DVect2 &);
72 BASE_EXPORT void increment(const DAVect2 &);
73
75 void conj() { ang_*=-1; }
77 Quat2 getConj() const { Quat2 q(-ang_); return q; }
79 void normalize() { }
80
82 Quat2 operator *(double d) const { Quat2 q(ang_*d); return q; }
84 Quat2 operator /(double d) const { Quat2 q(ang_/d); return q; }
86 friend Quat2 operator *(double d,const Quat2 &p2) { Quat2 q(p2*d); return q;}
89 Quat2 operator *(const Quat2 &v) const { Quat2 q(ang_+v.ang_); return q; }
91 Quat2 operator +(const Quat2 &v) const { Quat2 q(ang_+v.ang_); return q; }
92
94 const Quat2 & operator *=(double d) { ang_ *= d; return *this; }
96 const Quat2 & operator /=(double d) { ang_ /= d; return *this; }
99 const Quat2 & operator *=(const Quat2 &v) { ang_ += v.ang_; return *this; }
102 const Quat2 & operator +=(const Quat2 &v) { ang_ += v.ang_; return *this; }
104 bool operator ==(const Quat2 &v) const { return (ang_ == v.ang_); }
105
106private:
107 double ang_; //the angle of rotation
108};
109
112class Quat3 {
113public:
115 Quat3(): w_(0), i_(0), j_(0), k_(0) { }
118 Quat3(double w1,double x,double y,double z) { w_ = w1; i_ = x; j_ = y; k_ = z; }
120 ~Quat3() { }
122 Quat3(const Quat3 &q) { w_ = q.w_; i_ = q.i_; j_ = q.j_; k_ = q.k_; }
123 inline constexpr std::partial_ordering operator<=>(const Quat3 &in) const;
125 constexpr const Quat3 &operator=(const Quat3 &q) { w_ = q.w_; i_ = q.i_; j_ = q.j_; k_ = q.k_; return *this; }
127 void setQuat(double w1,double x,double y,double z){ w_ = w1; i_ = x; j_ = y; k_ = z; }
128
130 double w() const { return w_; }
132 double i() const { return i_; }
134 double j() const { return j_; }
136 double k() const { return k_; }
137
139 double &rw() { return w_; }
141 double &ri() { return i_; }
143 double &rj() { return j_; }
145 double &rk() { return k_; }
146
148 void reset() { w_=0.0; i_=0.0; j_=0.0; k_=0.0; }
150 void ident() { w_=1.0; i_=0.0; j_=0.0; k_=0.0; }
152 static BASE_EXPORT Quat3 identity() { Quat3 ret(1.0,0.0,0.0,0.0); return ret;}
154 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; }
158 BASE_EXPORT DVect3 quatToAAngle() const;
160 static BASE_EXPORT Quat3 fromVect(const DVect3 &v);
162 BASE_EXPORT void fromMatrix(const DMatrix<3,3> &);
164 BASE_EXPORT void fromAxisAngle(const DVect3 &,double);
167 BASE_EXPORT void fromEuler(const DVect3 &);
170 BASE_EXPORT void fromEulerZXZ(const DVect3 &);
172 BASE_EXPORT void incrementAxisAngle(const DVect3 &,double);
174 BASE_EXPORT void spinAboutX(double);
176 BASE_EXPORT void spinAboutY(double);
178 BASE_EXPORT void spinAboutZ(double);
180 BASE_EXPORT DVect3 rotate(const DVect3 &v) const;
182 BASE_EXPORT DVect3 real() const { return DVect3(i_,j_,k_); }
183
185 BASE_EXPORT void fromUnitAxes(const DVect3 &,const DVect3 &);
187 BASE_EXPORT void increment(const DAVect3 &);
189 BASE_EXPORT DVect3 e1() const;
191 BASE_EXPORT DVect3 e2() const;
193 BASE_EXPORT DVect3 e3() const;
196 BASE_EXPORT DVect3 quatToEuler() const;
197
199 void conj() { i_=-i_; j_=-j_; k_=-k_; }
201 Quat3 getConj() const { Quat3 q(w_,-i_,-j_,-k_); return q; }
203 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; }
204
206 Quat3 operator *(double d) const { Quat3 q(w_*d,i_*d,j_*d,k_*d); return q; }
208 Quat3 operator /(double d) const { Quat3 q(w_/d,i_/d,j_/d,k_/d); return q; }
210 friend Quat3 operator *(double d,const Quat3 &p2) {Quat3 q(p2*d); return q;}
212 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; }
214 Quat3 operator +(const Quat3 &v) const { Quat3 q(w_+v.w_,i_+v.i_,j_+v.j_,k_+v.k_); return q; }
216 Quat3 friend operator *(const DVect3 &v,const Quat3 &q) {Quat3 q2(0.0,v.x(),v.y(),v.z()); return q2*q;}
218 Quat3 operator *(const DVect3 &v) const {Quat3 q2(0.0,v.x(),v.y(),v.z()); return *this*q2;}
219
221 const Quat3 & operator *=(double d) { w_ *= d; i_ *= d; j_ *= d; k_ *= d; return *this; }
223 const Quat3 & operator /=(double d) { w_ /= d; i_ /= d; j_ /= d; k_ /= d; return *this; }
225 const Quat3 & operator *=(const Quat3 &v) { double tw(w_*v.w_-i_*v.i_-j_*v.j_-k_*v.k_);
226 double ti(w_*v.i_+i_*v.w_+j_*v.k_-k_*v.j_);
227 double tj(w_*v.j_-i_*v.k_+j_*v.w_+k_*v.i_);
228 double tk(w_*v.k_+i_*v.j_-j_*v.i_+k_*v.w_);
229 //double tw(v.w_*w_-v.i_*i_-v.j_*j_-v.k_*k_);
230 //double ti(v.w_*i_+v.i_*w_+v.j_*k_-v.k_*j_);
231 //double tj(v.w_*j_-v.i_*k_+v.j_*w_+v.k_*i_);
232 //double tk(v.w_*k_+v.i_*j_-v.j_*i_+v.k_*w_);
233 w_ = tw; i_ = ti; j_ = tj; k_ = tk;
234 return *this; }
235
236 const Quat3 & operator +=(const Quat3 &v) { w_ += v.w_; i_ += v.i_; j_ += v.j_; k_ += v.k_; return *this; }
238 bool operator ==(const Quat3 &v) const { return (w_ == v.w_ && i_==v.i_ && j_==v.j_ && k_==v.k_); }
239
240private:
241 double w_; //the real amplitude part
242 double i_,j_,k_; //the imaginary parts
243};
244
245constexpr std::partial_ordering Quat3::operator<=>(const Quat3 &in) const {
246 auto i = w_ <=> in.w_;
247 if (i!=std::partial_ordering::equivalent) return i;
248 i = i_ <=> in.i_;
249 if (i!=std::partial_ordering::equivalent) return i;
250 i = j_ <=> in.j_;
251 if (i!=std::partial_ordering::equivalent) return i;
252 return k_ <=> in.k_;
253}
254
255namespace base {
256 BASE_EXPORT string ts(const Quat2 &q, int width=0, char notation = '\0', int precision = -1, char fill = ' ');
257
258 BASE_EXPORT string ts(const Quat3 &q, int width=0, char notation = '\0', int precision = -1, char fill = ' ');
259}
260
Angular Vectors.
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:91
const Quat2 & operator*=(double d)
In place multiplaction by a double.
Definition quat.h:94
BASE_EXPORT void fromUnitAxes(const DVect2 &, const DVect2 &)
Definition quat.cpp:62
double & rang()
Access a reference to the angle.
Definition quat.h:42
Quat2(double a)
Constructor initializing the angle.
Definition quat.h:25
BASE_EXPORT void incrementAxisAngle(const DVect2 &, 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
BASE_EXPORT void fromAxisAngle(const DVect2 &, double)
Definition quat.cpp:45
Quat2 operator*(double d) const
Multiplication by a double.
Definition quat.h:82
void conj()
Convert to the conjugate quaternion - rotation by the opposite sign.
Definition quat.h:75
static BASE_EXPORT Quat2 identity()
Return the identity quaternion.
Definition quat.h:49
void setQuat(double a)
Set the angle.
Definition quat.h:36
Quat2(const Quat2 &q)
Copy constructor.
Definition quat.h:29
Quat2 getConj() const
Return the conjugate of the quaternion.
Definition quat.h:77
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
BASE_EXPORT void fromMatrix(const DMatrix< 2, 2 > &)
Convert a matrix into a quaternion.
Definition quat.cpp:21
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
bool operator==(const Quat2 &v) const
Euality operator.
Definition quat.h:104
void reset()
Reset the angle to 0.
Definition quat.h:45
BASE_EXPORT void spinAboutZ(double)
Just spin about the Z axis.
Definition quat.cpp:53
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:102
Quat2 operator/(double d) const
Division by a double.
Definition quat.h:84
const Quat2 & operator/=(double d)
In place division by a double.
Definition quat.h:96
void normalize()
Normalize the quaternion.
Definition quat.h:79
double ang() const
Access the angle.
Definition quat.h:39
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:47
3D quaternion utility class.
Definition quat.h:112
Quat3(const Quat3 &q)
Copy constructor.
Definition quat.h:122
BASE_EXPORT DVect3 rotate(const DVect3 &v) const
Rotate a DVect3 by the quaternian to get a new DVect3.
Definition quat.cpp:231
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:270
constexpr const Quat3 & operator=(const Quat3 &q)
Equality operator.
Definition quat.h:125
Quat3 getConj() const
Return the conjugate of the quaternion.
Definition quat.h:201
const Quat3 & operator/=(double d)
In place division by a double.
Definition quat.h:223
void conj()
Convert the quaternion to the conjugate.
Definition quat.h:199
double & rj()
Access a reference to the second imaginary part.
Definition quat.h:143
static BASE_EXPORT Quat3 fromVect(const DVect3 &v)
Convert a DVect3 into a quaternion.
Definition quat.cpp:125
Quat3 operator+(const Quat3 &v) const
Addition of two quaternions.
Definition quat.h:214
Quat3 operator*(double d) const
Multiplication by a double.
Definition quat.h:206
BASE_EXPORT void incrementAxisAngle(const DVect3 &, double)
Increment by this rotation.
Definition quat.cpp:209
Quat3 operator/(double d) const
Division by a double.
Definition quat.h:208
double & rw()
Access a reference to the real part.
Definition quat.h:139
const Quat3 & operator+=(const Quat3 &v)
In place addition of two quaternions.
Definition quat.h:236
BASE_EXPORT DVect3 e1() const
Get component e1 of the rotation matrix.
Definition quat.cpp:275
~Quat3()
Default destructor.
Definition quat.h:120
BASE_EXPORT void spinAboutX(double)
Just spin about the X axis.
Definition quat.cpp:219
BASE_EXPORT bool isNull() const
Returns a boolean indicating whether or not the quaternion is null.
Definition quat.h:154
BASE_EXPORT void fromEuler(const DVect3 &)
Definition quat.cpp:179
static BASE_EXPORT Quat3 identity()
Returns the identity quaternion.
Definition quat.h:152
const Quat3 & operator*=(double d)
In place multiplaction by a double.
Definition quat.h:221
Quat3(double w1, double x, double y, double z)
Definition quat.h:118
BASE_EXPORT void fromUnitAxes(const DVect3 &, const DVect3 &)
Take 2 orthogonal axis for an axis system and convert to a quaternion.
Definition quat.cpp:240
void setQuat(double w1, double x, double y, double z)
Set the real and imaginary parts.
Definition quat.h:127
void ident()
Reset the quaternion to the identity.
Definition quat.h:150
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 j() const
Access the second imaginary part.
Definition quat.h:134
double & rk()
Access a reference to the third imaginary part.
Definition quat.h:145
Quat3()
Default constructor.
Definition quat.h:115
BASE_EXPORT DVect3 e3() const
Get component e3 of the rotation matrix.
Definition quat.cpp:283
BASE_EXPORT DVect3 e2() const
Get component e2 of the rotation matrix.
Definition quat.cpp:279
double & ri()
Access a reference to the first imaginary part.
Definition quat.h:141
BASE_EXPORT DVect3 real() const
Return the "real" vector part of the quaternian.
Definition quat.h:182
void reset()
Reset the quaternion.
Definition quat.h:148
BASE_EXPORT void fromAxisAngle(const DVect3 &, double)
Take an axis-angle representation and convert it to a quaternion where the angle is in radians.
Definition quat.cpp:165
BASE_EXPORT void spinAboutY(double)
Just spin about the Y axis.
Definition quat.cpp:223
double i() const
Access the first imaginary part.
Definition quat.h:132
BASE_EXPORT void spinAboutZ(double)
Just spin about the Z axis.
Definition quat.cpp:227
BASE_EXPORT DMatrix< 3, 3 > quatToMatrix() const
Convert the quaternion to a rotation matrix.
Definition quat.cpp:70
double w() const
Access the real part.
Definition quat.h:130
BASE_EXPORT void fromEulerZXZ(const DVect3 &)
Definition quat.cpp:201
double k() const
Access the third imaginary part.
Definition quat.h:136
bool operator==(const Quat3 &v) const
Euality operator.
Definition quat.h:238
BASE_EXPORT DVect3 quatToEuler() const
Definition quat.cpp:287
BASE_EXPORT void fromMatrix(const DMatrix< 3, 3 > &)
Convert a matrix into a quaternion.
Definition quat.cpp:134
BASE_EXPORT void normalize()
Normalize the quaternion.
Definition quat.h:203
Matrix< double, SX, SY > DMatrix
DMatrix is a Matrix that defaults to type double...
Definition matrix.h:668
#define BASE_EXPORT
Definition basedef.h:25
A template-based matrix class, size fixed at compile time.
2D and 3D vector utility classes.