Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/lib/math/quaternion.h @ 9095

Last change on this file since 9095 was 9092, checked in by bensch, 19 years ago

cmath (valgrinded)

File size: 7.2 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Christian Meyer
13   co-programmer: ...
14*/
15
16/*!
17 * @file quaternion.h
18 * A basic 3D quaternion math framework
19 *
20 * Contains classes to handle vectors, lines, rotations and planes
21*/
22
23#ifndef __QUATERNION_H_
24#define __QUATERNION_H_
25
26#include "compiler.h"
27//! PI the circle-constant
28#define PI 3.14159265359f
29#include "vector.h"
30
31//! Quaternion
32/**
33   Class to handle 3-dimensional rotation efficiently
34*/
35class Quaternion
36{
37 public:
38  /** creates a Default quaternion (multiplicational identity Quaternion)*/
39  inline Quaternion () { w = 1; v = Vector(0,0,0); }
40  /** Copy constructor @param q the Quaternion to copy. */
41  inline Quaternion (const Quaternion& q) { w = q.w; v = q.v; };
42  /** creates a Quaternion looking into the direction v @param v: the direction @param f: the value */
43  inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; }
44  /** turns a rotation along an axis into a Quaternion @param angle: the amount of radians to rotate @param axis: the axis to rotate around */
45  inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2.0); v = axis * sin(angle/2.0); }
46  Quaternion (const Vector& dir, const Vector& up);
47  Quaternion (float roll, float pitch, float yaw);
48
49  void from3x3(float m[3][3]);
50  void from4x4(float m[4][4]);
51
52
53  /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */
54  inline bool operator== (const Quaternion& q) const { return (unlikely(this->v==q.v&&this->w==q.w))?true:false; };
55  /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */
56  inline bool operator!= (const Quaternion& q) const { return (unlikely(this->v!=q.v||this->w!=q.w))?true:false; };
57  /** @param f: a real value @return a Quaternion containing the quotient */
58  inline Quaternion operator/ (const float& f) const { return (unlikely(f==0.0)) ? Quaternion() : Quaternion(this->v/f, this->w/f); };
59  /** @param f: the value to divide by @returns the quaternion devided by f (this /= f) */
60  inline const Quaternion& operator/= (const float& f) {*this = *this / f; return *this;}
61  /** @param f: a real value @return a Quaternion containing the product */
62  inline Quaternion operator* (const float& f) const { return Quaternion(this->v*f, this->w*f); };
63  /** @param f: the value to multiply by @returns the quaternion multiplied by f (this *= f) */
64  inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;}
65  /** @param q: another Quaternion to rotate this by @return a quaternion that represents the first one rotated by the second one (WARUNING: this operation is not commutative! e.g. (A*B) != (B*A)) */
66  Quaternion operator* (const Quaternion& q) const { return Quaternion(Vector(this->w*q.v.x + this->v.x*q.w + this->v.y*q.v.z - this->v.z*q.v.y,
67                                                                         this->w*q.v.y + this->v.y*q.w + this->v.z*q.v.x - this->v.x*q.v.z,
68                                                                         this->w*q.v.z + this->v.z*q.w + this->v.x*q.v.y - this->v.y*q.v.x),
69                                                                         this->w*q.w - this->v.x*q.v.x - this->v.y*q.v.y - this->v.z*q.v.z); };
70  /** @param q: the Quaternion to multiply by @returns the quaternion multiplied by q (this *= q) */
71  inline const Quaternion& operator*= (const Quaternion& q) {*this = *this * q; return *this; };
72  /** @param q the Quaternion by which to devide @returns the division from this by q (this / q) */
73  inline Quaternion operator/ (const Quaternion& q) const { return *this * q.inverse(); };
74  /** @param q the Quaternion by which to devide @returns the division from this by q (this /= q) */
75  inline const Quaternion& operator/= (const Quaternion& q) { *this = *this * q.inverse(); return *this; };
76  /** @param q the Quaternion to add to this @returns the quaternion added with q (this + q) */
77  inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); };
78  /** @param q the Quaternion to add to this @returns the quaternion added with q (this += q) */
79  inline const Quaternion& operator+= (const Quaternion& q) { this->v += q.v; this->w += q.w; return *this; };
80  /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this - q) */
81  inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); }
82  /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this -= q) */
83  inline const Quaternion& operator-= (const Quaternion& q) { this->v -= q.v; this->w -= q.w; return *this; };
84  /** copy constructor @param q: the Quaternion to set this to. @returns the Quaternion q (or this) */
85  inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;}
86  /** conjugates this Quaternion @returns the conjugate */
87  inline Quaternion conjugate () const { return Quaternion(Vector(-v.x, -v.y, -v.z), this->w); };
88  /** @returns the norm of The Quaternion */
89  inline float norm () const { return sqrt(w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
90  /** @returns the inverted Quaterntion of this */
91  inline Quaternion inverse () const { return conjugate() / (w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
92  /** @returns the dot Product of a Quaternion */
93  inline float dot (const Quaternion& q) const { return v.x*q.v.x + v.y*q.v.y + v.z*q.v.z + w*q.w; };
94  /** @retuns the Distance between two Quaternions */
95  inline float distance(const Quaternion& q) const { return 2*acos(fabsf(this->dot(q))); };
96  /** @param v: the Vector  @return a new Vector representing v rotated by the Quaternion */
97  inline Vector apply (const Vector& v) const { return (*this * Quaternion(v, 0) * conjugate()).v; };
98  void matrix (float m[4][4]) const;
99  /** @returns the normalized Quaternion (|this|) */
100  inline Quaternion getNormalized() const { float n = this->norm(); return Quaternion(this->v/n, this->w/n); };
101  /** normalizes the current Quaternion */
102  inline void normalize() { float n = this->norm(); this->v /= n; this->w/=n; };
103
104  float getHeading() const;
105  Quaternion getHeadingQuat() const;
106  float getAttitude() const;
107  Quaternion getAttitudeQuat() const;
108  float getBank() const;
109  Quaternion getBankQuat() const;
110  /** @returns the rotational axis of this Quaternion */
111  inline Vector getSpacialAxis() const { return this->v / sin(acos(w));/*sqrt(v.x*v.x + v.y*v.y + v.z+v.z);*/ };
112  /** @returns the rotational angle of this Quaternion around getSpacialAxis()  !! IN DEGREE !! */
113  inline float getSpacialAxisAngle() const { return 360.0 / M_PI * acos( this->w ); };
114
115
116  inline void slerpTo(const Quaternion& toQuat, float t);
117  static Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t);
118
119  void debug() const;
120  void debug2() const;
121
122
123 public:
124  Vector    v;        //!< Imaginary Vector
125  float     w;        //!< Real part of the number
126};
127
128
129
130#endif /* __QUATERNION_H_ */
131
Note: See TracBrowser for help on using the repository browser.