Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "Quat.h"
#include "V3D.h"
#include <cmath>
#include <boost/test/floating_point_comparison.hpp>
#include <stdexcept>
namespace Mantid
{
namespace Geometry
{
// Use boost float comparison
boost::test_tools::close_at_tolerance<double> quat_tol(boost::test_tools::percent_tolerance(1e-6));
Quat::Quat():w(1),a(0),b(0),c(0)
/*! Null Constructor
* Initialize the quaternion with the identity q=1.0+0i+0j+0k;
*/
{
}
Quat::Quat(const double _w,const double _a, const double _b, const double _c):w(_w),a(_a),b(_b),c(_c)
//! Constructor with values
{
}
Quat::Quat(const Quat& _q)
//! Copy constructor
{
w=_q.w;
a=_q.a;
b=_q.b;
c=_q.c;
}
Quat::Quat(const double _deg,const V3D& _axis)
/*! Constructor from an angle and axis.
* \param _deg :: angle of rotation
* \param _axis :: axis to rotate about
*
* This construct a quaternion to represent a rotation
* of an angle _deg around the _axis. The _axis does not need to be a unit vector
* */
{
setAngleAxis(_deg,_axis);
}
Quat& Quat::operator=(const Quat& q)
{
w=q.w;
a=q.a;
b=q.b;
c=q.c;
return *this;
}
void Quat::set(const double ww, const double aa, const double bb, const double cc)
{
w=ww;
a=aa;
b=bb;
c=cc;
return;
}
void Quat::setAngleAxis(const double _deg, const V3D& _axis)
/*! Constructor from an angle and axis.
* \param _deg :: angle of rotation
* \param _axis :: axis to rotate about
*
* This construct a quaternion to represent a rotation
* of an angle _deg around the _axis. The _axis does not need to be a unit vector
* */
{
double deg2rad=M_PI/180.0;
w=cos(0.5*_deg*deg2rad);
double s=sin(0.5*_deg*deg2rad);
V3D temp(_axis);
temp.normalize();
a=s*temp[0];
b=s*temp[1];
c=s*temp[2];
return;
}
void Quat::operator()(const double ww, const double aa, const double bb, const double cc)
{
this->set(ww,aa,bb,cc);
}
void Quat::operator()(const double angle, const V3D& axis)
{
this->setAngleAxis(angle,axis);
}
Quat::~Quat()
//! Destructor
{}
void Quat::init()
/*! Re-initialise a quaternion to identity.
*/
{
w=1.0;
a=b=c=0.0;
return;
}
Quat Quat::operator+(const Quat& _q) const
/*! Quaternion addition operator
* \param _q :: the quaternion to add
* \return *this+_q
*/
{
return Quat(w+_q.w,a+_q.a,b+_q.b,c+_q.c);
}
Quat& Quat::operator+=(const Quat& _q)
/*! Quaternion self-addition operator
* \param _q :: the quaternion to add
* \return *this+=_q
*/
{
w+=_q.w;a+=_q.a;b+=_q.b;c+=_q.c;
return *this;
}
Quat Quat::operator-(const Quat& _q) const
/*! Quaternion subtraction operator
* \param _q :: the quaternion to add
* \return *this-_q
*/
{
return Quat(w-_q.w,a-_q.a,b-_q.b,c-_q.c);
}
Quat& Quat::operator-=(const Quat& _q)
/*! Quaternion self-substraction operator
* \param _q :: the quaternion to add
* \return *this-=_q
*/
{
w-=_q.w;
a-=_q.a;
b-=_q.b;
c-=_q.c;
return *this;
}
Quat Quat::operator*(const Quat& _q) const
/*! Quaternion multiplication operator
* \param _q :: the quaternion to multiply
* \return *this*_q
*
* Quaternion multiplication is non commutative
* in the same way multiplication of rotation matrices
* isn't.
*/
{
double w1,a1,b1,c1;
w1=w*_q.w-a*_q.a-b*_q.b-c*_q.c;
a1=w*_q.a+_q.w*a+b*_q.c-_q.b*c;
b1=w*_q.b+_q.w*b-a*_q.c+c*_q.a;
c1=w*_q.c+_q.w*c+a*_q.b-_q.a*b;
return Quat(w1,a1,b1,c1);
}
Quat& Quat::operator*=(const Quat& _q)
/*! Quaternion self-multiplication operator
* \param _q :: the quaternion to multiply
* \return *this*=_q
*/
{
double w1,a1,b1,c1;
w1=w*_q.w-a*_q.a-b*_q.b-c*_q.c;
a1=w*_q.a+_q.w*a+b*_q.c-_q.b*c;
b1=w*_q.b+_q.w*b-a*_q.c+c*_q.a;
c1=w*_q.c+_q.w*c+a*_q.b-_q.a*b;
w=w1;a=a1;b=b1;c=c1;
return (*this);
}
bool Quat::operator==(const Quat& q) const
/*! Quaternion equal operator
* \param _q :: the quaternion to compare
*
* Compare two quaternions at 1e-6%tolerance.
* Use boost close_at_tolerance method
*/
{
return (quat_tol(w,q.w) && quat_tol(a,q.a) && quat_tol(b,q.b) && quat_tol(c,q.c));
}
bool Quat::operator!=(const Quat& _q) const
{
/*! Quaternion non-equal operator
* \param _q :: the quaternion to compare
*
* Compare two quaternions at 1e-6%tolerance.
* Use boost close_at_tolerance method
*/
return (!operator==(_q));
}
void Quat::normalize()
/*! Quaternion normalization
*
* Divide all elements by the quaternion norm
*/
{
double overnorm=1.0/len2();
w*=overnorm;
a*=overnorm;
b*=overnorm;
c*=overnorm;
return;
}
void Quat::conjugate()
/*! Quaternion complex conjugate
*
* Reverse the sign of the 3 imaginary components of the
* quaternion
*/
{
a*=-1.0;
b*=-1.0;
c*=-1.0;
return;
}
double Quat::len() const
/*! Quaternion length
*
*/
{
return sqrt(len2());
}
double Quat::len2() const
/*! Quaternion norm (length squared)
*
*/
{
return (w*w+a*a+b*b+c*c);
}
void Quat::inverse()
/*! Inverse a quaternion
*
*/
{
conjugate();
normalize();
return;
}
void Quat::rotate(V3D& v) const
/*! Rotate a vector.
* \param v :: the vector to be rotated
*
* The quaternion needs to be normalized beforehand to
* represent a rotation. If q is thequaternion, the rotation
* is represented by q.v.q-1 where q-1 is the inverse of
* v.
*/
{
Quat qinvert(*this);
qinvert.inverse();
Quat pos(0.0,v[0],v[1],v[2]);
pos*=qinvert;
pos=(*this)*pos;
v[0]=pos[1];
v[1]=pos[2];
v[2]=pos[3];
}
void Quat::GLMatrix(double mat[16])
/*!
*/
{
double aa = a * a;
double ab = a * b;
double ac = a * c;
double aw = a * w;
double bb = b * b;
double bc = b * c;
double bw = b * w;
double cc = c * c;
double cw = c * w;
mat[0] = 1.0 - 2.0 * ( bb + cc );
mat[4] = 2.0 * ( ab - cw );
mat[8] = 2.0 * ( ac + bw );
mat[1] = 2.0 * ( ab + cw );
mat[5] = 1.0 - 2.0 * ( aa + cc );
mat[9] = 2.0 * ( bc - aw );
mat[2] = 2.0 * ( ac - bw );
mat[6] = 2.0 * ( bc + aw );
mat[10] = 1.0 - 2.0 * ( aa + bb );
mat[12] = mat[13] = mat[14] = mat[3] = mat[7] = mat[11] = 0.0;
mat[15] = 1.0;
return;
}
const double& Quat::operator[](const int Index) const
{
switch (Index)
{
case 0: return w;
case 1: return a;
case 2: return b;
case 3: return c;
default:
throw std::runtime_error("Quat::operator[] range error");
}
}
double& Quat::operator[](const int Index)
{
switch (Index)
{
case 0: return w;
case 1: return a;
case 2: return b;
case 3: return c;
default:
throw std::runtime_error("Quat::operator[] range error");
}
}
void Quat::printSelf(std::ostream& os) const
{
os << "[" << w << "," << a << "," << b << "," << c << "]";
return;
}
std::ostream& operator<<(std::ostream& os,const Quat& q)
{
q.printSelf(os);
return os;
}
} // Namespace Geometry
} // Namespce Mantid