Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/math/quaternion.h @ 8824

Last change on this file since 8824 was 8802, checked in by patrick, 18 years ago

merged the network branche back to trunk

File size: 7.1 KB
RevLine 
[5420]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
[4578]16/*!
[6616]17 * @file quaternion.h
18 * A basic 3D quaternion math framework
[5008]19 *
20 * Contains classes to handle vectors, lines, rotations and planes
[4578]21*/
[2043]22
[6616]23#ifndef __QUATERNION_H_
24#define __QUATERNION_H_
[2043]25
26#include <math.h>
[3860]27#include "compiler.h"
[3449]28//! PI the circle-constant
[2043]29#define PI 3.14159265359f
[6616]30#include "vector.h"
[2043]31
[2190]32//! Quaternion
[2043]33/**
[4476]34   Class to handle 3-dimensional rotation efficiently
[2190]35*/
36class Quaternion
37{
38 public:
[4994]39  /** creates a Default quaternion (multiplicational identity Quaternion)*/
[3822]40  inline Quaternion () { w = 1; v = Vector(0,0,0); }
[4994]41  /** creates a Quaternion looking into the direction v @param v: the direction @param f: the value */
[3971]42  inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; }
[3541]43  Quaternion (float m[4][4]);
[8724]44  Quaternion (float m[3][3]);
[4994]45  /** turns a rotation along an axis into a Quaternion @param angle: the amount of radians to rotate @param axis: the axis to rotate around */
[6627]46  inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2.0); v = axis * sin(angle/2.0); }
[3541]47  Quaternion (const Vector& dir, const Vector& up);
48  Quaternion (float roll, float pitch, float yaw);
[5052]49
50  /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */
51  inline bool operator== (const Quaternion& q) const { return (unlikely(this->v==q.v&&this->w==q.w))?true:false; };
[8802]52  /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */
53  inline bool operator!= (const Quaternion& q) const { return (unlikely(this->v!=q.v&&this->w!=q.w))?true:false; };
[4997]54  /** @param f: a real value @return a Quaternion containing the quotient */
55  inline Quaternion operator/ (const float& f) const { return (unlikely(f==0.0)) ? Quaternion() : Quaternion(this->v/f, this->w/f); };
[4836]56  /** @param f: the value to divide by @returns the quaternion devided by f (this /= f) */
[4477]57  inline const Quaternion& operator/= (const float& f) {*this = *this / f; return *this;}
[4997]58  /** @param f: a real value @return a Quaternion containing the product */
59  inline Quaternion operator* (const float& f) const { return Quaternion(this->v*f, this->w*f); };
[4836]60  /** @param f: the value to multiply by @returns the quaternion multiplied by f (this *= f) */
[4477]61  inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;}
[4999]62  /** @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)) */
63  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,
64                                                                         this->w*q.v.y + this->v.y*q.w + this->v.z*q.v.x - this->v.x*q.v.z,
65                                                                         this->w*q.v.z + this->v.z*q.w + this->v.x*q.v.y - this->v.y*q.v.x),
[5006]66                                                                         this->w*q.w - this->v.x*q.v.x - this->v.y*q.v.y - this->v.z*q.v.z); };
[4836]67  /** @param q: the Quaternion to multiply by @returns the quaternion multiplied by q (this *= q) */
[4997]68  inline const Quaternion& operator*= (const Quaternion& q) {*this = *this * q; return *this; };
69  /** @param q the Quaternion by which to devide @returns the division from this by q (this / q) */
70  inline Quaternion operator/ (const Quaternion& q) const { return *this * q.inverse(); };
71  /** @param q the Quaternion by which to devide @returns the division from this by q (this /= q) */
72  inline const Quaternion& operator/= (const Quaternion& q) { *this = *this * q.inverse(); return *this; };
[4836]73  /** @param q the Quaternion to add to this @returns the quaternion added with q (this + q) */
[4477]74  inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); };
[4836]75  /** @param q the Quaternion to add to this @returns the quaternion added with q (this += q) */
[4477]76  inline const Quaternion& operator+= (const Quaternion& q) { this->v += q.v; this->w += q.w; return *this; };
[4836]77  /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this - q) */
[3822]78  inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); }
[4836]79  /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this -= q) */
[4477]80  inline const Quaternion& operator-= (const Quaternion& q) { this->v -= q.v; this->w -= q.w; return *this; };
[4994]81  /** copy constructor @param q: the Quaternion to set this to. @returns the Quaternion q (or this) */
[3966]82  inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;}
[4994]83  /** conjugates this Quaternion @returns the conjugate */
[4998]84  inline Quaternion conjugate () const { return Quaternion(Vector(-v.x, -v.y, -v.z), this->w); };
[4997]85  /** @returns the norm of The Quaternion */
[5000]86  inline float norm () const { return sqrt(w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
[4997]87  /** @returns the inverted Quaterntion of this */
[5000]88  inline Quaternion inverse () const { return conjugate() / (w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
[5419]89  /** @returns the dot Product of a Quaternion */
90  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; };
91  /** @retuns the Distance between two Quaternions */
92  inline float distance(const Quaternion& q) const { return 2*acos(fabsf(this->dot(q))); };
[4997]93  /** @param v: the Vector  @return a new Vector representing v rotated by the Quaternion */
94  inline Vector apply (const Vector& v) const { return (*this * Quaternion(v, 0) * conjugate()).v; };
[3541]95  void matrix (float m[4][4]) const;
[4998]96  /** @returns the normalized Quaternion (|this|) */
97  inline Quaternion getNormalized() const { float n = this->norm(); return Quaternion(this->v/n, this->w/n); };
98  /** normalizes the current Quaternion */
99  inline void normalize() { float n = this->norm(); this->v /= n; this->w/=n; };
[4578]100
[7348]101  float getHeading() const;
[8731]102  Quaternion getHeadingQuat() const;
[7348]103  float getAttitude() const;
[8731]104  Quaternion getAttitudeQuat() const;
[7348]105  float getBank() const;
[8731]106  Quaternion getBankQuat() const;
[4998]107  /** @returns the rotational axis of this Quaternion */
108  inline Vector getSpacialAxis() const { return this->v / sin(acos(w));/*sqrt(v.x*v.x + v.y*v.y + v.z+v.z);*/ };
109  /** @returns the rotational angle of this Quaternion around getSpacialAxis()  !! IN DEGREE !! */
[5435]110  inline float getSpacialAxisAngle() const { return 360.0 / M_PI * acos( this->w ); };
[4998]111
[7191]112
113  inline void slerpTo(const Quaternion& toQuat, float t);
[4998]114  static Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t);
115
[7003]116  void debug() const;
117  void debug2() const;
[4477]118
[4998]119
[4477]120 public:
121  Vector    v;        //!< Imaginary Vector
122  float     w;        //!< Real part of the number
[2190]123};
124
[3971]125
[8724]126
[6616]127#endif /* __QUATERNION_H_ */
[3971]128
Note: See TracBrowser for help on using the repository browser.