quaternion.h
Go to the documentation of this file.00001 #ifndef VVELIB_GEOMETRY_QUATERNION_H
00002 #define VVELIB_GEOMETRY_QUATERNION_H
00003
00005
00006 #include <config.h>
00007 #include <geometry/geometry.h>
00008
00009 namespace geometry
00010 {
00011
00017 struct Quaternion : public Point4d
00018 {
00024 Quaternion()
00025 : Point4d(0,0,0,1)
00026 {}
00027
00031 Quaternion(double x, double y, double z, double w)
00032 : Point4d(x,y,z,w)
00033 {}
00034
00038 Quaternion(const Quaternion& other)
00039 : Point4d(other)
00040 {}
00041
00050 Quaternion(const Point3d& axis, double angle);
00051
00056 Quaternion(const Point3d& from, const Point3d& to);
00057
00061 Quaternion(const Matrix3d& m);
00062
00066 Quaternion& operator=(const Quaternion& other);
00067
00073 void setAxisAngle(const Point3d& axis, double angle);
00074
00078 double& w() { return elems[3]; }
00082 const double& w() const { return elems[3]; }
00083
00087 Quaternion& operator+=(const Quaternion& other)
00088 {
00089 for(size_t i = 0 ; i < 4 ; ++i) elems[i] += other.elems[i];
00090 return *this;
00091 }
00092
00096 Quaternion operator+(const Quaternion& other) const
00097 {
00098 Quaternion tmp(*this);
00099 tmp += other;
00100 return tmp;
00101 }
00102
00106 Quaternion operator*(const Quaternion& other) const;
00110 Quaternion& operator*=(const Quaternion& other);
00111
00115 Quaternion& operator*=(double s)
00116 { for(size_t i = 0 ; i < 4 ; ++i) elems[i] *= s; return *this;}
00117
00121 Quaternion inverse() const { double n = util::normsq(*this); return Quaternion(-x()/n, -y()/n, -z()/n, w()/n); }
00122
00126 Quaternion conjugate() const { return Quaternion(-x(), -y(), -z(), w()); }
00127
00131 Quaternion& operator/=(double v)
00132 {
00133 for(size_t i = 0 ; i < 4 ; ++i) elems[i] /= v;
00134 return *this;
00135 }
00136
00140 Quaternion operator/(double v) const
00141 {
00142 Quaternion tmp(*this);
00143 tmp /= v;
00144 return tmp;
00145 }
00146
00153 void setMatrix(Matrix3d& m) const;
00160 void setMatrix(Matrix4d& m) const;
00161
00165 Point3d axis() const;
00166
00170 double angle() const;
00171
00175 Point3d rotate(const Point3d& v) const;
00176
00183 Point3d inverseRotate(const Point3d& v) const;
00184
00185 };
00186
00192 Quaternion slerp(const Quaternion& q1, const Quaternion& q2, double t);
00193
00194 inline Quaternion operator*(double s, const Quaternion& q)
00195 {
00196 Quaternion tmp(q);
00197 tmp *= s;
00198 return tmp;
00199 }
00200
00201 inline Quaternion operator*(const Quaternion& q, double s)
00202 {
00203 Quaternion tmp(q);
00204 tmp *= s;
00205 return tmp;
00206 }
00207
00208 }
00209
00210 namespace std
00211 {
00217 geometry::Quaternion pow(const geometry::Quaternion& q, double p);
00218 };
00219
00220 #endif // VVELIB_GEOMETRY_QUATERNION_H
00221