10 /// Default constructor. Does not initialize at all.
11 /// If you need them zero initialized, SGQuat::zeros()
14 /// Initialize with nans in the debug build, that will guarantee to have
15 /// a fast uninitialized default constructor in the release but shows up
16 /// uninitialized values in the debug build very fast ...
18 for (unsigned i = 0; i < 4; ++i)
19 _data[i] = SGLimits<T>::quiet_NaN();
22 /// Constructor. Initialize by the given values
23 SGQuat(T _x, T _y, T _z, T _w)
24 { x() = _x; y() = _y; z() = _z; w() = _w; }
25 /// Constructor. Initialize by the content of a plain array,
26 /// make sure it has at least 4 elements
27 explicit SGQuat(const T* d)
28 { _data[0] = d[0]; _data[1] = d[1]; _data[2] = d[2]; _data[3] = d[3]; }
30 /// Return a unit quaternion
31 static SGQuat unit(void)
32 { return fromRealImag(1, SGVec3<T>(0)); }
34 /// Return a quaternion from euler angles
35 static SGQuat fromEuler(T z, T y, T x)
38 T zd2 = T(0.5)*z; T yd2 = T(0.5)*y; T xd2 = T(0.5)*x;
39 T Szd2 = sin(zd2); T Syd2 = sin(yd2); T Sxd2 = sin(xd2);
40 T Czd2 = cos(zd2); T Cyd2 = cos(yd2); T Cxd2 = cos(xd2);
41 T Cxd2Czd2 = Cxd2*Czd2; T Cxd2Szd2 = Cxd2*Szd2;
42 T Sxd2Szd2 = Sxd2*Szd2; T Sxd2Czd2 = Sxd2*Czd2;
43 q.w() = Cxd2Czd2*Cyd2 + Sxd2Szd2*Syd2;
44 q.x() = Sxd2Czd2*Cyd2 - Cxd2Szd2*Syd2;
45 q.y() = Cxd2Czd2*Syd2 + Sxd2Szd2*Cyd2;
46 q.z() = Cxd2Szd2*Cyd2 - Sxd2Czd2*Syd2;
50 /// Return a quaternion from euler angles
51 static SGQuat fromYawPitchRoll(T y, T p, T r)
52 { return fromEuler(y, p, r); }
54 /// Return a quaternion from euler angles
55 static SGQuat fromHeadAttBank(T h, T a, T b)
56 { return fromEuler(h, a, b); }
58 /// Return a quaternion rotation the the horizontal local frame from given
59 /// longitude and latitude
60 static SGQuat fromLonLat(T lon, T lat)
64 T yd2 = T(-0.25)*SGMisc<value_type>::pi() - T(0.5)*lat;
76 /// Create a quaternion from the angle axis representation
77 static SGQuat fromAngleAxis(T angle, const SGVec3<T>& axis)
80 return fromRealImag(cos(angle2), T(sin(angle2))*axis);
83 /// Create a quaternion from the angle axis representation where the angle
84 /// is stored in the axis' length
85 static SGQuat fromAngleAxis(const SGVec3<T>& axis)
88 if (nAxis <= SGLimits<T>::min())
89 return SGQuat(1, 0, 0, 0);
91 return fromRealImag(cos(angle2), T(sin(angle2)/nAxis)*axis);
94 /// Return a quaternion from real and imaginary part
95 static SGQuat fromRealImag(T r, const SGVec3<T>& i)
105 /// Return an all zero vector
106 static SGQuat zeros(void)
107 { return SGQuat(0, 0, 0, 0); }
109 /// write the euler angles into the references
110 void getEulerRad(T& zRad, T& yRad, T& xRad) const
112 value_type sqrQW = w()*w();
113 value_type sqrQX = x()*x();
114 value_type sqrQY = y()*y();
115 value_type sqrQZ = z()*z();
117 value_type num = 2*(y()*z() + w()*x());
118 value_type den = sqrQW - sqrQX - sqrQY + sqrQZ;
119 if (fabs(den) < SGLimits<value_type>::min() &&
120 fabs(num) < SGLimits<value_type>::min())
123 xRad = atan2(num, den);
125 value_type tmp = 2*(x()*z() - w()*y());
127 yRad = 0.5*SGMisc<value_type>::pi();
129 yRad = -0.5*SGMisc<value_type>::pi();
133 num = 2*(x()*y() + w()*z());
134 den = sqrQW + sqrQX - sqrQY - sqrQZ;
135 if (fabs(den) < SGLimits<value_type>::min() &&
136 fabs(num) < SGLimits<value_type>::min())
139 value_type psi = atan2(num, den);
141 psi += 2*SGMisc<value_type>::pi();
146 /// write the euler angles in degrees into the references
147 void getEulerDeg(T& zDeg, T& yDeg, T& xDeg) const
149 getEulerRad(zDeg, yDeg, xDeg);
150 zDeg *= 180/SGMisc<value_type>::pi();
151 yDeg *= 180/SGMisc<value_type>::pi();
152 xDeg *= 180/SGMisc<value_type>::pi();
155 /// write the angle axis representation into the references
156 void getAngleAxis(T& angle, SGVec3<T>& axis) const
159 if (nrm < SGLimits<T>::min()) {
161 axis = SGVec3<T>(0, 0, 0);
164 angle = acos(SGMisc<T>::max(-1, SGMisc<T>::min(1, rNrm*w())));
166 if (fabs(sAng) < SGLimits<T>::min())
167 axis = SGVec3<T>(1, 0, 0);
169 axis = (rNrm/sAng)*imag(*this);
174 /// write the angle axis representation into the references
175 void getAngleAxis(SGVec3<T>& axis) const
178 getAngleAxis(angle, axis);
182 /// Access by index, the index is unchecked
183 const T& operator()(unsigned i) const
185 /// Access by index, the index is unchecked
186 T& operator()(unsigned i)
189 /// Access the x component
190 const T& x(void) const
192 /// Access the x component
195 /// Access the y component
196 const T& y(void) const
198 /// Access the y component
201 /// Access the z component
202 const T& z(void) const
204 /// Access the z component
207 /// Access the w component
208 const T& w(void) const
210 /// Access the w component
214 /// Get the data pointer, usefull for interfacing with plib's sg*Vec
215 const T* data(void) const
217 /// Get the data pointer, usefull for interfacing with plib's sg*Vec
221 /// Readonly interface function to ssg's sgQuat/sgdQuat
222 const T (&sg(void) const)[4]
224 /// Interface function to ssg's sgQuat/sgdQuat
229 SGQuat& operator+=(const SGQuat& v)
230 { _data[0]+=v(0);_data[1]+=v(1);_data[2]+=v(2);_data[3]+=v(3);return *this; }
231 /// Inplace subtraction
232 SGQuat& operator-=(const SGQuat& v)
233 { _data[0]-=v(0);_data[1]-=v(1);_data[2]-=v(2);_data[3]-=v(3);return *this; }
234 /// Inplace scalar multiplication
236 SGQuat& operator*=(S s)
237 { _data[0] *= s; _data[1] *= s; _data[2] *= s; _data[3] *= s; return *this; }
238 /// Inplace scalar multiplication by 1/s
240 SGQuat& operator/=(S s)
241 { return operator*=(1/T(s)); }
242 /// Inplace quaternion multiplication
243 SGQuat& operator*=(const SGQuat& v);
245 /// Transform a vector from the current coordinate frame to a coordinate
246 /// frame rotated with the quaternion
247 SGVec3<T> transform(const SGVec3<T>& v) const
249 value_type r = 2/dot(*this, *this);
250 SGVec3<T> qimag = imag(*this);
251 value_type qr = real(*this);
252 return (r*qr*qr - 1)*v + (r*dot(qimag, v))*qimag - (r*qr)*cross(qimag, v);
254 /// Transform a vector from the coordinate frame rotated with the quaternion
255 /// to the current coordinate frame
256 SGVec3<T> backTransform(const SGVec3<T>& v) const
258 value_type r = 2/dot(*this, *this);
259 SGVec3<T> qimag = imag(*this);
260 value_type qr = real(*this);
261 return (r*qr*qr - 1)*v + (r*dot(qimag, v))*qimag + (r*qr)*cross(qimag, v);
264 /// Rotate a given vector with the quaternion
265 SGVec3<T> rotate(const SGVec3<T>& v) const
266 { return backTransform(v); }
267 /// Rotate a given vector with the inverse quaternion
268 SGVec3<T> rotateBack(const SGVec3<T>& v) const
269 { return transform(v); }
271 /// Return the time derivative of the quaternion given the angular velocity
273 derivative(const SGVec3<T>& angVel)
277 deriv.w() = 0.5*(-x()*angVel(0) - y()*angVel(1) - z()*angVel(2));
278 deriv.x() = 0.5*( w()*angVel(0) - z()*angVel(1) + y()*angVel(2));
279 deriv.y() = 0.5*( z()*angVel(0) + w()*angVel(1) - x()*angVel(2));
280 deriv.z() = 0.5*(-y()*angVel(0) + x()*angVel(1) + w()*angVel(2));
290 /// Unary +, do nothing ...
294 operator+(const SGQuat<T>& v)
297 /// Unary -, do nearly nothing
301 operator-(const SGQuat<T>& v)
302 { return SGQuat<T>(-v(0), -v(1), -v(2), -v(3)); }
308 operator+(const SGQuat<T>& v1, const SGQuat<T>& v2)
309 { return SGQuat<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(3)); }
315 operator-(const SGQuat<T>& v1, const SGQuat<T>& v2)
316 { return SGQuat<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2), v1(3)-v2(3)); }
318 /// Scalar multiplication
319 template<typename S, typename T>
322 operator*(S s, const SGQuat<T>& v)
323 { return SGQuat<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
325 /// Scalar multiplication
326 template<typename S, typename T>
329 operator*(const SGQuat<T>& v, S s)
330 { return SGQuat<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
332 /// Quaterion multiplication
336 operator*(const SGQuat<T>& v1, const SGQuat<T>& v2)
339 v.x() = v1.w()*v2.x() + v1.x()*v2.w() + v1.y()*v2.z() - v1.z()*v2.y();
340 v.y() = v1.w()*v2.y() - v1.x()*v2.z() + v1.y()*v2.w() + v1.z()*v2.x();
341 v.z() = v1.w()*v2.z() + v1.x()*v2.y() - v1.y()*v2.x() + v1.z()*v2.w();
342 v.w() = v1.w()*v2.w() - v1.x()*v2.x() - v1.y()*v2.y() - v1.z()*v2.z();
346 /// Now define the inplace multiplication
350 SGQuat<T>::operator*=(const SGQuat& v)
351 { (*this) = (*this)*v; return *this; }
353 /// The conjugate of the quaternion, this is also the
354 /// inverse for normalized quaternions
358 conj(const SGQuat<T>& v)
359 { return SGQuat<T>(-v(0), -v(1), -v(2), v(3)); }
361 /// The conjugate of the quaternion, this is also the
362 /// inverse for normalized quaternions
366 inverse(const SGQuat<T>& v)
367 { return (1/dot(v, v))*SGQuat<T>(-v(0), -v(1), -v(2), v(3)); }
369 /// The imagniary part of the quaternion
373 real(const SGQuat<T>& v)
376 /// The imagniary part of the quaternion
380 imag(const SGQuat<T>& v)
381 { return SGVec3<T>(v.x(), v.y(), v.z()); }
383 /// Scalar dot product
387 dot(const SGQuat<T>& v1, const SGQuat<T>& v2)
388 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
390 /// The euclidean norm of the vector, that is what most people call length
394 norm(const SGQuat<T>& v)
395 { return sqrt(dot(v, v)); }
397 /// The euclidean norm of the vector, that is what most people call length
401 length(const SGQuat<T>& v)
402 { return sqrt(dot(v, v)); }
404 /// The 1-norm of the vector, this one is the fastest length function we
405 /// can implement on modern cpu's
409 norm1(const SGQuat<T>& v)
410 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
412 /// The euclidean norm of the vector, that is what most people call length
416 normalize(const SGQuat<T>& q)
417 { return (1/norm(q))*q; }
419 /// Return true if exactly the same
423 operator==(const SGQuat<T>& v1, const SGQuat<T>& v2)
424 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
426 /// Return true if not exactly the same
430 operator!=(const SGQuat<T>& v1, const SGQuat<T>& v2)
431 { return ! (v1 == v2); }
433 /// Return true if equal to the relative tolerance tol
434 /// Note that this is not the same than comparing quaternions to represent
435 /// the same rotation
439 equivalent(const SGQuat<T>& v1, const SGQuat<T>& v2, T tol)
440 { return norm1(v1 - v2) < tol*(norm1(v1) + norm1(v2)); }
442 /// Return true if about equal to roundoff of the underlying type
443 /// Note that this is not the same than comparing quaternions to represent
444 /// the same rotation
448 equivalent(const SGQuat<T>& v1, const SGQuat<T>& v2)
449 { return equivalent(v1, v2, 100*SGLimits<T>::epsilon()); }
455 isNaN(const SGQuat<T>& v)
457 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
458 || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
462 /// quaternion interpolation for t in [0,1] interpolate between src (=0)
467 interpolate(T t, const SGQuat<T>& src, const SGQuat<T>& dst)
469 T cosPhi = dot(src, dst);
470 // need to take the shorter way ...
471 int signCosPhi = SGMisc<T>::sign(cosPhi);
472 // cosPhi must be corrected for that sign
473 cosPhi = fabs(cosPhi);
475 // first opportunity to fail - make sure acos will succeed later -
480 // now the half angle between the orientations
483 // need the scales now, if the angle is very small, do linear interpolation
484 // to avoid instabilities
486 if (fabs(o) < SGLimits<T>::epsilon()) {
490 // note that we can give a positive lower bound for sin(o) here
493 scale0 = sin((1 - t)*o)*so;
494 scale1 = sin(t*o)*so;
497 return scale0*src + signCosPhi*scale1*dst;
500 /// Output to an ostream
501 template<typename char_type, typename traits_type, typename T>
503 std::basic_ostream<char_type, traits_type>&
504 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGQuat<T>& v)
505 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
507 /// Two classes doing actually the same on different types
508 typedef SGQuat<float> SGQuatf;
509 typedef SGQuat<double> SGQuatd;
513 toQuatf(const SGQuatd& v)
514 { return SGQuatf(v(0), v(1), v(2), v(3)); }
518 toQuatd(const SGQuatf& v)
519 { return SGQuatd(v(0), v(1), v(2), v(3)); }