Itasca C++ Interface
Loading...
Searching...
No Matches
quat.h
Go to the documentation of this file.
1#pragma once
8#include "avect.h"
9#include "matrix.h"
10#include "vect.h"
11#include <cmath>
12#include <cassert>
13
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; }
59 BASE_EXPORT void incrementAxisAngle(const DVect2 &,const double &);
61 BASE_EXPORT void spinAboutZ(const double &);
63 BASE_EXPORT DVect2 rotate(const DVect2 &v) const;
66 BASE_EXPORT void fromUnitAxes(const DVect2 &,const DVect2 &);
68 BASE_EXPORT void increment(const DAVect2 &);
69
71 void conj() { ang_*=-1; }
73 Quat2 getConj() const { Quat2 q(-ang_); return q; }
75 void normalize() { }
76
78 Quat2 operator *(const double &d) const { Quat2 q(ang_*d); return q; }
80 Quat2 operator /(const double &d) const { Quat2 q(ang_/d); return q; }
82 friend Quat2 operator *(const double &d,const Quat2 &p2) { Quat2 q(p2*d); return q;}
85 Quat2 operator *(const Quat2 &v) const { Quat2 q(ang_+v.ang_); return q; }
87 Quat2 operator +(const Quat2 &v) const { Quat2 q(ang_+v.ang_); return q; }
88
90 const Quat2 & operator *=(const double &d) { ang_ *= d; return *this; }
92 const Quat2 & operator /=(const double &d) { ang_ /= d; return *this; }
95 const Quat2 & operator *=(const Quat2 &v) { ang_ += v.ang_; return *this; }
98 const Quat2 & operator +=(const Quat2 &v) { ang_ += v.ang_; return *this; }
100 bool operator ==(const Quat2 &v) const { return (ang_ == v.ang_); }
101
102private:
103 double ang_; //the angle of rotation
104};
105
108class Quat3 {
109public:
111 Quat3(): w_(0), i_(0), j_(0), k_(0) { }
114 Quat3(const double &w1,const double &x,const double &y,const double &z) { w_ = w1; i_ = x; j_ = y; k_ = z; }
116 ~Quat3() { }
118 Quat3(const Quat3 &q) { w_ = q.w_; i_ = q.i_; j_ = q.j_; k_ = q.k_; }
120 constexpr const Quat3 &operator=(const Quat3 &q) { w_ = q.w_; i_ = q.i_; j_ = q.j_; k_ = q.k_; return *this; }
122 void setQuat(const double &w1,const double &x,const double &y,const double &z){ w_ = w1; i_ = x; j_ = y; k_ = z; }
123
125 const double &w() const { return w_; }
127 const double &i() const { return i_; }
129 const double &j() const { return j_; }
131 const double &k() const { return k_; }
132
134 double &rw() { return w_; }
136 double &ri() { return i_; }
138 double &rj() { return j_; }
140 double &rk() { return k_; }
141
143 void reset() { w_=0.0; i_=0.0; j_=0.0; k_=0.0; }
145 void ident() { w_=1.0; i_=0.0; j_=0.0; k_=0.0; }
147 static BASE_EXPORT Quat3 identity() { Quat3 ret(1.0,0.0,0.0,0.0); return ret;}
149 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; }
153 BASE_EXPORT DVect3 quatToAAngle() const;
155 static BASE_EXPORT Quat3 fromVect(const DVect3 &v);
157 BASE_EXPORT void fromMatrix(const DMatrix<3,3> &);
159 BASE_EXPORT void fromAxisAngle(const DVect3 &,const double &);
162 BASE_EXPORT void fromEuler(const DVect3 &);
164 BASE_EXPORT void incrementAxisAngle(const DVect3 &,const double &);
166 BASE_EXPORT void spinAboutX(const double &);
168 BASE_EXPORT void spinAboutY(const double &);
170 BASE_EXPORT void spinAboutZ(const double &);
172 BASE_EXPORT DVect3 rotate(const DVect3 &v) const;
174 BASE_EXPORT DVect3 real() const { return DVect3(i_,j_,k_); }
175
177 BASE_EXPORT void fromUnitAxes(const DVect3 &,const DVect3 &);
179 BASE_EXPORT void increment(const DAVect3 &);
181 BASE_EXPORT DVect3 e1() const;
183 BASE_EXPORT DVect3 e2() const;
185 BASE_EXPORT DVect3 e3() const;
188 BASE_EXPORT DVect3 quatToEuler() const;
189
191 void conj() { i_=-i_; j_=-j_; k_=-k_; }
193 Quat3 getConj() const { Quat3 q(w_,-i_,-j_,-k_); return q; }
195 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; }
196
198 Quat3 operator *(const double &d) const { Quat3 q(w_*d,i_*d,j_*d,k_*d); return q; }
200 Quat3 operator /(const double &d) const { Quat3 q(w_/d,i_/d,j_/d,k_/d); return q; }
202 friend Quat3 operator *(const double &d,const Quat3 &p2) {Quat3 q(p2*d); return q;}
204 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; }
206 Quat3 operator +(const Quat3 &v) const { Quat3 q(w_+v.w_,i_+v.i_,j_+v.j_,k_+v.k_); return q; }
208 Quat3 friend operator *(const DVect3 &v,const Quat3 &q) {Quat3 q2(0.0,v.x(),v.y(),v.z()); return q2*q;}
210 Quat3 operator *(const DVect3 &v) const {Quat3 q2(0.0,v.x(),v.y(),v.z()); return *this*q2;}
211
213 const Quat3 & operator *=(const double &d) { w_ *= d; i_ *= d; j_ *= d; k_ *= d; return *this; }
215 const Quat3 & operator /=(const double &d) { w_ /= d; i_ /= d; j_ /= d; k_ /= d; return *this; }
217 const Quat3 & operator *=(const Quat3 &v) { double tw(w_*v.w_-i_*v.i_-j_*v.j_-k_*v.k_);
218 double ti(w_*v.i_+i_*v.w_+j_*v.k_-k_*v.j_);
219 double tj(w_*v.j_-i_*v.k_+j_*v.w_+k_*v.i_);
220 double tk(w_*v.k_+i_*v.j_-j_*v.i_+k_*v.w_);
221 //double tw(v.w_*w_-v.i_*i_-v.j_*j_-v.k_*k_);
222 //double ti(v.w_*i_+v.i_*w_+v.j_*k_-v.k_*j_);
223 //double tj(v.w_*j_-v.i_*k_+v.j_*w_+v.k_*i_);
224 //double tk(v.w_*k_+v.i_*j_-v.j_*i_+v.k_*w_);
225 w_ = tw; i_ = ti; j_ = tj; k_ = tk;
226 return *this; }
228 const Quat3 & operator +=(const Quat3 &v) { w_ += v.w_; i_ += v.i_; j_ += v.j_; k_ += v.k_; return *this; }
230 bool operator ==(const Quat3 &v) const { return (w_ == v.w_ && i_==v.i_ && j_==v.j_ && k_==v.k_); }
231
232private:
233 double w_; //the real amplitude part
234 double i_,j_,k_; //the imaginary parts
235};
236
237namespace base {
238 BASE_EXPORT string ts(const Quat2 &q, int width=0, char notation = '\0', int precision = -1, char fill = ' ');
239
240 BASE_EXPORT string ts(const Quat3 &q, int width=0, char notation = '\0', int precision = -1, char fill = ' ');
241}
242
Angular Vectors.
DMatrix is a Matrix that defaults to type double...
Definition matrix.h:758
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:87
friend Quat2 operator*(const double &d, const Quat2 &p2)
Multiplication of a double times a quaternion.
Definition quat.h:82
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
void conj()
Convert to the conjugate quaternion - rotation by the opposite sign.
Definition quat.h:71
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:73
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:92
const Quat2 & operator*=(const double &d)
In place multiplaction by a double.
Definition quat.h:90
BASE_EXPORT void fromAxisAngle(const DVect2 &, const double &)
Definition quat.cpp:45
bool operator==(const Quat2 &v) const
Euality operator.
Definition quat.h:100
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:98
void normalize()
Normalize the quaternion.
Definition quat.h:75
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:80
3D quaternion utility class.
Definition quat.h:108
Quat3(const Quat3 &q)
Copy constructor.
Definition quat.h:118
BASE_EXPORT DVect3 rotate(const DVect3 &v) const
Rotate a DVect3 by the quaternian to get a new DVect3.
Definition quat.cpp:212
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:248
const Quat3 & operator/=(const double &d)
In place division by a double.
Definition quat.h:215
constexpr const Quat3 & operator=(const Quat3 &q)
Equality operator.
Definition quat.h:120
const double & k() const
Access the third imaginary part.
Definition quat.h:131
Quat3 getConj() const
Return the conjugate of the quaternion.
Definition quat.h:193
void setQuat(const double &w1, const double &x, const double &y, const double &z)
Set the real and imaginary parts.
Definition quat.h:122
void conj()
Convert the quaternion to the conjugate.
Definition quat.h:191
double & rj()
Access a reference to the second imaginary part.
Definition quat.h:138
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:206
BASE_EXPORT void incrementAxisAngle(const DVect3 &, const double &)
Increment by this rotation.
Definition quat.cpp:194
double & rw()
Access a reference to the real part.
Definition quat.h:134
const Quat3 & operator+=(const Quat3 &v)
In place addition of two quaternions.
Definition quat.h:228
BASE_EXPORT DVect3 e1() const
Get component e1 of the rotation matrix.
Definition quat.cpp:252
~Quat3()
Default destructor.
Definition quat.h:116
BASE_EXPORT bool isNull() const
Returns a boolean indicating whether or not the quaternion is null.
Definition quat.h:149
BASE_EXPORT void fromEuler(const DVect3 &)
Definition quat.cpp:173
static BASE_EXPORT Quat3 identity()
Returns the identity quaternion.
Definition quat.h:147
const double & j() const
Access the second imaginary part.
Definition quat.h:129
BASE_EXPORT void fromUnitAxes(const DVect3 &, const DVect3 &)
Take 2 orthogonal axis for an axis system and convert to a quaternion.
Definition quat.cpp:218
void ident()
Reset the quaternion to the identity.
Definition quat.h:145
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:140
Quat3()
Default constructor.
Definition quat.h:111
BASE_EXPORT void spinAboutY(const double &)
Just spin about the Y axis.
Definition quat.cpp:204
friend Quat3 operator*(const double &d, const Quat3 &p2)
Product of a double and a quaternion.
Definition quat.h:202
BASE_EXPORT DVect3 e3() const
Get component e3 of the rotation matrix.
Definition quat.cpp:260
Quat3(const double &w1, const double &x, const double &y, const double &z)
Definition quat.h:114
const double & w() const
Access the real part.
Definition quat.h:125
BASE_EXPORT DVect3 e2() const
Get component e2 of the rotation matrix.
Definition quat.cpp:256
double & ri()
Access a reference to the first imaginary part.
Definition quat.h:136
BASE_EXPORT DVect3 real() const
Return the "real" vector part of the quaternian.
Definition quat.h:174
void reset()
Reset the quaternion.
Definition quat.h:143
BASE_EXPORT void spinAboutX(const double &)
Just spin about the X axis.
Definition quat.cpp:200
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:127
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:213
bool operator==(const Quat3 &v) const
Euality operator.
Definition quat.h:230
BASE_EXPORT void spinAboutZ(const double &)
Just spin about the Z axis.
Definition quat.cpp:208
BASE_EXPORT DVect3 quatToEuler() const
Definition quat.cpp:264
Quat3 operator/(const double &d) const
Division by a double.
Definition quat.h:200
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:195
#define BASE_EXPORT
Definition basedef.h:24
A template-based matrix class, size fixed at compile time.
2D and 3D vector utility classes.