4 #ifndef BALL_MATHS_QUATERNION_H
5 #define BALL_MATHS_QUATERNION_H
7 #ifndef BALL_MATHS_MATRIX44_H
11 #ifndef BALL_MATHS_VECTOR3_H
15 #include <boost/math/quaternion.hpp>
31 :
public boost::math::quaternion<T>
95 void set(
const boost::math::quaternion<T>& q);
111 void set(
const T&
w,
const T&
i,
const T&
j,
const T&
k);
261 void dump(std::ostream& s = std::cout,
Size depth = 0)
const;
267 template <
typename T>
269 : boost::math::quaternion<T>()
274 template <
typename T>
276 : boost::math::quaternion<T>(q)
280 template <
typename T>
282 : boost::math::quaternion<T>(q)
286 template <
typename T>
288 : boost::math::quaternion<T>(w, i, j, k)
292 template <
typename T>
294 : boost::math::quaternion<T>()
299 template <
typename T>
304 template <
typename T>
310 template <
typename T>
313 boost::math::quaternion<T>::operator= (q);
316 template <
typename T>
319 boost::math::quaternion<T>::operator= (q);
322 template <
typename T>
326 fromAxisAngle(axis, angle);
329 template <
typename T>
340 template <
typename T>
348 template <
typename T>
356 template <
typename T>
361 this->b = this->
c = this->d = (T)0;
364 template <
typename T>
368 T length = boost::math::norm(*
this);
380 template <
typename T>
400 template <
typename T>
407 this->b = this->
c = this->d = (T)0;
412 T omega = (T) (angle * 0.5);
413 T sin_omega = (T)::sin(omega);
415 this->a = (T)::cos(omega);
416 this->b = axis.
x * sin_omega / length;
417 this->
c = axis.
y * sin_omega / length;
418 this->d = axis.
z * sin_omega / length;
422 template <
typename T>
425 T half_yaw = yaw / 2.0;
426 T half_pitch = pitch / 2.0;
427 T half_roll = roll / 2.0;
429 T cosYaw = cos(half_yaw);
430 T sinYaw = sin(half_yaw);
432 T cosPitch = cos(half_pitch);
433 T sinPitch = sin(half_pitch);
435 T cosRoll = cos(half_roll);
436 T sinRoll = sin(half_roll);
439 this->a = cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw;
440 this->b = sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw;
441 this->
c = cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw;
442 this->d = cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw;
446 template <
typename T>
449 T length = sqrt(this->b*this->b + this->
c*this->
c + this->d*this->d);
453 axis.
x = axis.
y = angle= (T)0;
458 angle = 2 * (T)::acos(this->a);
459 axis.
x = this->b / length;
460 axis.
y = this->
c / length;
461 axis.
z = this->d / length;
466 template <
typename T>
470 getRotationMatrix(matrix);
471 T sinYaw, cosYaw, sinPitch, cosPitch, sinRoll, cosRoll;
473 sinPitch = -matrix(2,0);
474 cosPitch = sqrt(1 - sinPitch*sinPitch);
478 sinRoll = matrix(2,1) / cosPitch;
479 cosRoll = matrix(2,2) / cosPitch;
480 sinYaw = matrix(1,0) / cosPitch;
481 cosYaw = matrix(0,0) / cosPitch;
485 sinRoll = -matrix(1,2);
486 cosRoll = matrix(1,1);
492 yaw = atan2(sinYaw, cosYaw);
495 pitch = atan2(sinPitch, cosPitch);
498 roll = atan2(sinRoll, cosRoll);
502 template <
typename T>
509 template <
typename T>
512 T length = sqrt(this->b*this->b + this->
c*this->
c + this->d*this->d);
519 return (T)(2 * (T)::acos(this->a));
523 template <
typename T>
526 T length = sqrt(this->b*this->b + this->
c*this->
c + this->d*this->d);
533 return TVector3<T>(this->b/length, this->
c/length, this->d/length);
537 template <
typename T>
540 T s = 2.0 / boost::math::norm(*
this);
543 (T)(1.0 - s * (this->
c * this->
c + this->d * this->d)),
544 (T)(s * (this->b * this->
c - this->d * this->a)),
545 (T)(s * (this->d * this->b + this->
c * this->a)),
548 (T)(s * (this->b * this->
c + this->d * this->a)),
549 (T)(1.0 - s * (this->d * this->d + this->b * this->b)),
550 (T)(s * (this->
c * this->d - this->b * this->a)),
553 (T)(s * (this->d * this->b - this->
c * this->a)),
554 (T)(s * (this->
c * this->d + this->b * this->a)),
555 (T)(1.0 - s * (this->
c * this->
c + this->b * this->b)),
567 template <
typename T>
572 return conj(*
this) / boost::math::norm(*
this);
575 template <
typename T>
582 template <
typename T>
588 template <
typename T>
594 template <
typename T>
600 template <
typename T>
606 template <
typename T>
612 template <
typename T>
618 template <
typename T>
624 template <
typename T>
630 template <
typename T>
635 s >> c >> q.
w() >> c >> q.
i() >> c >> q.
j() >> c >>q.
k() >>
c;
639 template <
typename T>
640 std::ostream& operator << (std::ostream& s, const TQuaternion<T>& q)
643 s <<
'(' << q.w() <<
',' << q.i() <<
','
644 << q.j() <<
',' << q.k() <<
')';
649 template <
typename T>
657 s <<
" w: " << this->w() << std::endl;
660 s <<
" i: " << this->i() << std::endl;
663 s <<
" j: " << this->j() << std::endl;
666 s <<
" k: " << this->
k() << std::endl;
677 #endif // BALL_MATHS_QUATERNION_H