Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Quaternion< T > Class Template Reference

#include <quaternion.h>

Inheritance diagram for Quaternion< T >:

Inheritance graph
Collaboration diagram for Quaternion< T >:

Collaboration graph
List of all members.

Public Member Functions

void getAxisAngle (Vector3< T > &axis, T &angle) const
bool isPure () const
bool isReal () const
normalize ()
Quaternionoperator *= (Quaternion)
Quaternionoperator *= (T)
Quaternion operator+ () const
Quaternionoperator+= (Quaternion)
Quaternion operator- () const
Quaternionoperator-= (Quaternion)
Quaternion operator~ () const
 Quaternion (const Matrix3< T > &)
 Quaternion (T, T, T, T)
 Quaternion (T, const Vector3< T > &)
 Quaternion (const Vector3< T > &)
 Quaternion (T)
 Quaternion (const Quaternion< T > &)
 Quaternion ()
void rotate (Vector3< T > axis, T angle)
void setAxisAngle (Vector3< T > axis, T angle)
Matrix3< T > toMatrix3 () const
Matrix4< T > toMatrix4 () const
void xrotate (T angle)
void yrotate (T angle)
void zrotate (T angle)

Static Public Member Functions

static Quaternion< T > slerp (Quaternion< T >, Quaternion< T >, T)
static Quaternion< T > xrotation (T)
static Quaternion< T > yrotation (T)
static Quaternion< T > zrotation (T)

Public Attributes

w
x
y
z

template<class T>
class Quaternion< T >


Constructor & Destructor Documentation

template<class T>
Quaternion< T >::Quaternion  )  [inline]
 

Definition at line 119 of file quaternion.h.

00119                                             : w(0), x(0), y(0), z(0)
00120 {
00121 }

template<class T>
Quaternion< T >::Quaternion const Quaternion< T > &   )  [inline]
 

Definition at line 123 of file quaternion.h.

00123                                                                   :
00124     w(q.w), x(q.x), y(q.y), z(q.z)
00125 {
00126 }

template<class T>
Quaternion< T >::Quaternion  )  [inline]
 

Definition at line 128 of file quaternion.h.

00128                                                 :
00129     w(re), x(0), y(0), z(0)
00130 {
00131 }

template<class T>
Quaternion< T >::Quaternion const Vector3< T > &   )  [inline]
 

Definition at line 134 of file quaternion.h.

00134                                                                 :
00135      w(0), x(im.x), y(im.y), z(im.z)
00136 {
00137 }

template<class T>
Quaternion< T >::Quaternion ,
const Vector3< T > & 
[inline]
 

Definition at line 139 of file quaternion.h.

00139                                                                       :
00140      w(re), x(im.x), y(im.y), z(im.z)
00141 {
00142 }

template<class T>
Quaternion< T >::Quaternion ,
,
,
[inline]
 

Definition at line 144 of file quaternion.h.

00144                                                                   :
00145     w(_w), x(_x), y(_y), z(_z)
00146 {
00147 }

template<class T>
Quaternion< T >::Quaternion const Matrix3< T > &   ) 
 

Definition at line 150 of file quaternion.h.

References sqrt(), Quaternion< T >::w, Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

00151 {
00152     T trace = m[0][0] + m[1][1] + m[2][2];
00153     T root;
00154 
00155     if (trace > 0)
00156     {
00157         root = (T) sqrt(trace + 1);
00158         w = (T) 0.5 * root;
00159         root = (T) 0.5 / root;
00160         x = (m[2][1] - m[1][2]) * root;
00161         y = (m[0][2] - m[2][0]) * root;
00162         z = (m[1][0] - m[0][1]) * root;
00163     }
00164     else
00165     {
00166         int i = 0;
00167         if (m[1][1] > m[i][i])
00168             i = 1;
00169         if (m[2][2] > m[i][i])
00170             i = 2;
00171         int j = (i == 2) ? 0 : i + 1;
00172         int k = (j == 2) ? 0 : j + 1;
00173 
00174         root = (T) sqrt(m[i][i] - m[j][j] - m[k][k] + 1);
00175         T* xyz[3] = { &x, &y, &z };
00176         *xyz[i] = (T) 0.5 * root;
00177         root = (T) 0.5 / root;
00178         w = (m[k][j] - m[j][k]) * root;
00179         *xyz[j] = (m[j][i] + m[i][j]) * root;
00180         *xyz[k] = (m[k][i] + m[i][k]) * root;
00181     }
00182 }


Member Function Documentation

template<class T>
void Quaternion< T >::getAxisAngle Vector3< T > &  axis,
T &  angle
const
 

Definition at line 503 of file quaternion.h.

References sqrt(), Quaternion< T >::w, Vector3< T >::x, Quaternion< T >::x, Vector3< T >::y, Quaternion< T >::y, Vector3< T >::z, and Quaternion< T >::z.

Referenced by CommandParser::parseCommand(), and WriteFavoritesList().

00505 {
00506     // The quaternion has the form:
00507     // w = cos(angle/2), (x y z) = sin(angle/2)*axis
00508 
00509     T magSquared = x * x + y * y + z * z;
00510     if (magSquared > (T) 1e-10)
00511     {
00512         T s =  (T) 1 / (T) sqrt(magSquared);
00513         axis.x = x * s;
00514         axis.y = y * s;
00515         axis.z = z * s;
00516         if (w <= -1 || w >= 1)
00517             angle = 0;
00518         else
00519             angle = (T) acos(w) * 2;
00520     }
00521     else
00522     {
00523         // The angle is zero, so we pick an arbitrary unit axis
00524         axis.x = 1;
00525         axis.y = 0;
00526         axis.z = 0;
00527         angle = 0;
00528     }
00529 }

template<class T>
bool Quaternion< T >::isPure  )  const
 

Definition at line 470 of file quaternion.h.

References Quaternion< T >::w.

00471 {
00472     return w == 0;
00473 }

template<class T>
bool Quaternion< T >::isReal  )  const
 

Definition at line 465 of file quaternion.h.

References Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

Referenced by cos(), exp(), log(), sin(), and sqrt().

00466 {
00467     return (x == 0 && y == 0 && z == 0);
00468 }

template<class T>
T Quaternion< T >::normalize  ) 
 

Definition at line 475 of file quaternion.h.

References sqrt(), Quaternion< T >::w, Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

Referenced by CelestiaCore::mouseMove(), Observer::orbit(), and Observer::update().

00476 {
00477     T s = (T) sqrt(w * w + x * x + y * y + z * z);
00478     T invs = (T) 1 / (T) s;
00479     x *= invs;
00480     y *= invs;
00481     z *= invs;
00482     w *= invs;
00483 
00484     return s;
00485 }

template<class T>
Quaternion< T > & Quaternion< T >::operator *= Quaternion< T >   ) 
 

Definition at line 197 of file quaternion.h.

References Quaternion< T >::w, Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

00198 {
00199     *this = Quaternion<T>(w * q.w - x * q.x - y * q.y - z * q.z,
00200                           w * q.x + x * q.w + y * q.z - z * q.y,
00201                           w * q.y + y * q.w + z * q.x - x * q.z,
00202                           w * q.z + z * q.w + x * q.y - y * q.x);
00203                           
00204     return *this;
00205 }

template<class T>
Quaternion< T > & Quaternion< T >::operator *=  )  [inline]
 

Definition at line 207 of file quaternion.h.

References Quaternion< T >::w, Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

00208 {
00209     x *= s; y *= s; z *= s; w *= s;
00210     return *this;
00211 }

template<class T>
Quaternion< T > Quaternion< T >::operator+  )  const [inline]
 

Definition at line 224 of file quaternion.h.

00225 {
00226     return *this;
00227 }

template<class T>
Quaternion< T > & Quaternion< T >::operator+= Quaternion< T >   )  [inline]
 

Definition at line 185 of file quaternion.h.

References Quaternion< T >::w, Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

00186 {
00187     x += a.x; y += a.y; z += a.z; w += a.w;
00188     return *this;
00189 }

template<class T>
Quaternion< T > Quaternion< T >::operator-  )  const [inline]
 

Definition at line 219 of file quaternion.h.

References Quaternion< T >::w, Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

00220 {
00221     return Quaternion<T>(-w, -x, -y, -z);
00222 }

template<class T>
Quaternion< T > & Quaternion< T >::operator-= Quaternion< T >   )  [inline]
 

Definition at line 191 of file quaternion.h.

References Quaternion< T >::w, Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

00192 {
00193     x -= a.x; y -= a.y; z -= a.z; w -= a.w;
00194     return *this;
00195 }

template<class T>
Quaternion< T > Quaternion< T >::operator~  )  const [inline]
 

Definition at line 214 of file quaternion.h.

References Quaternion< T >::w, Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

00215 {
00216     return Quaternion<T>(w, -x, -y, -z);
00217 }

template<class T>
void Quaternion< T >::rotate Vector3< T >  axis,
angle
 

Definition at line 604 of file quaternion.h.

References Quaternion< T >::setAxisAngle().

Referenced by CommandRotate::process().

00605 {
00606     Quaternion q;
00607     q.setAxisAngle(axis, angle);
00608     *this = q * *this;
00609 }

template<class T>
void Quaternion< T >::setAxisAngle Vector3< T >  axis,
angle
 

Definition at line 489 of file quaternion.h.

References Math< T >::sincos(), Quaternion< T >::w, Vector3< T >::x, Quaternion< T >::x, Vector3< T >::y, Quaternion< T >::y, Vector3< T >::z, and Quaternion< T >::z.

Referenced by celestia_newrotation(), Observer::computeCenterCOParameters(), DeepSkyObject::load(), CelestiaCore::mouseMove(), Universe::pickDeepSkyObject(), Universe::pickStar(), CommandSetOrientation::process(), CommandRotate::process(), CommandOrbit::process(), ReadFavoritesList(), renderBumpMappedMesh(), renderSmoothMesh(), Quaternion< T >::rotate(), and rotation_setaxisangle().

00490 {
00491     T s, c;
00492     
00493     Math<T>::sincos(angle * (T) 0.5, s, c);
00494     x = s * axis.x;
00495     y = s * axis.y;
00496     z = s * axis.z;
00497     w = c;
00498 }

template<class T>
Quaternion< T > Quaternion< T >::slerp Quaternion< T >  ,
Quaternion< T >  ,
[static]
 

Definition at line 577 of file quaternion.h.

References abs(), dot(), and sin().

00580 {
00581     T c = dot(q0, q1);
00582 
00583     // Because of potential rounding errors, we must clamp c to the domain of acos.
00584     if (c > (T) 1.0)
00585         c = (T) 1.0;
00586     else if (c < (T) -1.0)
00587         c = (T) -1.0;
00588 
00589     T angle = (T) acos(c);
00590 
00591     if (abs(angle) < (T) 1.0e-5)
00592         return q0;
00593 
00594     T s = (T) sin(angle);
00595     T is = (T) 1.0 / s;
00596 
00597     return q0 * ((T) sin((1 - t) * angle) * is) +
00598            q1 * ((T) sin(t * angle) * is);
00599 }

template<class T>
Matrix3< T > Quaternion< T >::toMatrix3  )  const
 

Definition at line 553 of file quaternion.h.

References Quaternion< T >::w, Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

Referenced by Observer::computeCenterCOParameters(), displayLocationInfo(), FrameOfReference::fromUniversal(), Location::getPlanetocentricPosition(), Observer::gotoSurface(), Observer::orbit(), SynchronousOrbit::positionAtTime(), Galaxy::renderGalaxyPointSprites(), Renderer::renderParticles(), rotation_transform(), FrameOfReference::toUniversal(), and Observer::update().

00554 {
00555     T wx = w * x * 2;
00556     T wy = w * y * 2;
00557     T wz = w * z * 2;
00558     T xx = x * x * 2;
00559     T xy = x * y * 2;
00560     T xz = x * z * 2;
00561     T yy = y * y * 2;
00562     T yz = y * z * 2;
00563     T zz = z * z * 2;
00564 
00565     return Matrix3<T>(Vector3<T>(1 - yy - zz, xy - wz, xz + wy),
00566                       Vector3<T>(xy + wz, 1 - xx - zz, yz - wx),
00567                       Vector3<T>(xz - wy, yz + wx, 1 - xx - yy));
00568 }

template<class T>
Matrix4< T > Quaternion< T >::toMatrix4  )  const
 

Definition at line 533 of file quaternion.h.

References Quaternion< T >::w, Quaternion< T >::x, Quaternion< T >::y, and Quaternion< T >::z.

Referenced by Observer::computeCenterParameters(), FrameOfReference::fromUniversal(), Body::getGeographicToHeliocentric(), Simulation::pickObject(), Renderer::renderObject(), Observer::setTargetSpeed(), and FrameOfReference::toUniversal().

00534 {
00535     T wx = w * x * 2;
00536     T wy = w * y * 2;
00537     T wz = w * z * 2;
00538     T xx = x * x * 2;
00539     T xy = x * y * 2;
00540     T xz = x * z * 2;
00541     T yy = y * y * 2;
00542     T yz = y * z * 2;
00543     T zz = z * z * 2;
00544 
00545     return Matrix4<T>(Vector4<T>(1 - yy - zz, xy - wz, xz + wy, 0),
00546                       Vector4<T>(xy + wz, 1 - xx - zz, yz - wx, 0),
00547                       Vector4<T>(xz - wy, yz + wx, 1 - xx - yy, 0),
00548                       Vector4<T>(0, 0, 0, 1));
00549 }

template<class T>
void Quaternion< T >::xrotate angle  ) 
 

Definition at line 614 of file quaternion.h.

References Math< T >::sincos().

Referenced by CelestiaCore::mouseMove().

00615 {
00616     T s, c;
00617 
00618     Math<T>::sincos(angle * (T) 0.5, s, c);
00619     *this = Quaternion<T>(c, s, 0, 0) * *this;
00620 }

template<class T>
Quaternion< T > Quaternion< T >::xrotation  )  [static]
 

Definition at line 643 of file quaternion.h.

References Math< T >::sincos().

00644 {
00645     T s, c;
00646     Math<T>::sincos(angle * (T) 0.5, s, c);
00647     return Quaternion<T>(c, s, 0, 0);
00648 }

template<class T>
void Quaternion< T >::yrotate angle  ) 
 

Definition at line 624 of file quaternion.h.

References Math< T >::sincos().

Referenced by RotationElements::equatorialToPlanetographic(), Body::getEquatorialToGeographic(), CelestiaCore::mouseMove(), Renderer::renderPlanet(), Renderer::renderStar(), and Observer::reverseOrientation().

00625 {
00626     T s, c;
00627 
00628     Math<T>::sincos(angle * (T) 0.5, s, c);
00629     *this = Quaternion<T>(c, 0, s, 0) * *this;
00630 }

template<class T>
Quaternion< T > Quaternion< T >::yrotation  )  [static]
 

Definition at line 650 of file quaternion.h.

References Math< T >::sincos().

00651 {
00652     T s, c;
00653     Math<T>::sincos(angle * (T) 0.5, s, c);
00654     return Quaternion<T>(c, 0, s, 0);
00655 }

template<class T>
void Quaternion< T >::zrotate angle  ) 
 

Definition at line 634 of file quaternion.h.

References Math< T >::sincos().

00635 {
00636     T s, c;
00637 
00638     Math<T>::sincos(angle * (T) 0.5, s, c);
00639     *this = Quaternion<T>(c, 0, 0, s) * *this;
00640 }

template<class T>
Quaternion< T > Quaternion< T >::zrotation  )  [static]
 

Definition at line 657 of file quaternion.h.

References Math< T >::sincos().

00658 {
00659     T s, c;
00660     Math<T>::sincos(angle * (T) 0.5, s, c);
00661     return Quaternion<T>(c, 0, 0, s);
00662 }


Member Data Documentation

template<class T>
T Quaternion< T >::w
 

Definition at line 111 of file quaternion.h.

Referenced by Observer::computeCenterCOParameters(), conjugate(), cos(), dot(), ExactPlanetPickTraversal(), exp(), Quaternion< T >::getAxisAngle(), Observer::getOrientation(), Observer::gotoLocation(), Quaternion< T >::isPure(), log(), norm(), Quaternion< T >::normalize(), observer_getorientation(), observer_gototable(), observer_rotate(), operator *(), Quaternion< T >::operator *=(), operator!=(), operator+(), Quaternion< T >::operator+=(), operator-(), Quaternion< T >::operator-(), Quaternion< T >::operator-=(), operator==(), Quaternion< T >::operator~(), Observer::orbit(), CommandGotoLocation::process(), Quaternion< T >::Quaternion(), real(), Renderer::render(), Renderer::renderCometTail(), Renderer::renderPlanet(), Renderer::renderStar(), Observer::rotate(), Quaternion< T >::setAxisAngle(), Observer::setFrame(), sin(), sqrt(), Quaternion< T >::toMatrix3(), Quaternion< T >::toMatrix4(), toUniversal(), Observer::update(), and Url::Url().

template<class T>
T Quaternion< T >::x
 

Definition at line 111 of file quaternion.h.

Referenced by Observer::computeCenterCOParameters(), conjugate(), cos(), dot(), ExactPlanetPickTraversal(), exp(), Quaternion< T >::getAxisAngle(), Observer::getOrientation(), Observer::gotoLocation(), imag(), Quaternion< T >::isReal(), log(), norm(), Quaternion< T >::normalize(), observer_getorientation(), observer_gototable(), observer_rotate(), operator *(), Quaternion< T >::operator *=(), operator!=(), operator+(), Quaternion< T >::operator+=(), operator-(), Quaternion< T >::operator-(), Quaternion< T >::operator-=(), operator==(), Quaternion< T >::operator~(), Observer::orbit(), CommandGotoLocation::process(), Quaternion< T >::Quaternion(), Renderer::render(), Renderer::renderCometTail(), Renderer::renderPlanet(), Renderer::renderStar(), Observer::rotate(), rotation_get(), Quaternion< T >::setAxisAngle(), Observer::setFrame(), sin(), sqrt(), Quaternion< T >::toMatrix3(), Quaternion< T >::toMatrix4(), toUniversal(), Observer::update(), and Url::Url().

template<class T>
T Quaternion< T >::y
 

Definition at line 111 of file quaternion.h.

Referenced by Observer::computeCenterCOParameters(), conjugate(), cos(), displayLocationInfo(), dot(), ExactPlanetPickTraversal(), exp(), Quaternion< T >::getAxisAngle(), Observer::getOrientation(), Observer::gotoLocation(), imag(), Quaternion< T >::isReal(), log(), norm(), Quaternion< T >::normalize(), observer_getorientation(), observer_gototable(), observer_rotate(), operator *(), Quaternion< T >::operator *=(), operator!=(), operator+(), Quaternion< T >::operator+=(), operator-(), Quaternion< T >::operator-(), Quaternion< T >::operator-=(), operator==(), Quaternion< T >::operator~(), Observer::orbit(), CommandGotoLocation::process(), Quaternion< T >::Quaternion(), Renderer::render(), Renderer::renderCometTail(), Renderer::renderPlanet(), Renderer::renderStar(), Observer::rotate(), rotation_get(), Quaternion< T >::setAxisAngle(), Observer::setFrame(), sin(), sqrt(), Quaternion< T >::toMatrix3(), Quaternion< T >::toMatrix4(), toUniversal(), Observer::update(), and Url::Url().

template<class T>
T Quaternion< T >::z
 

Definition at line 111 of file quaternion.h.

Referenced by Observer::computeCenterCOParameters(), conjugate(), cos(), dot(), ExactPlanetPickTraversal(), exp(), Quaternion< T >::getAxisAngle(), Observer::getOrientation(), Observer::gotoLocation(), imag(), Quaternion< T >::isReal(), log(), norm(), Quaternion< T >::normalize(), observer_getorientation(), observer_gototable(), observer_rotate(), operator *(), Quaternion< T >::operator *=(), operator!=(), operator+(), Quaternion< T >::operator+=(), operator-(), Quaternion< T >::operator-(), Quaternion< T >::operator-=(), operator==(), Quaternion< T >::operator~(), Observer::orbit(), CommandGotoLocation::process(), Quaternion< T >::Quaternion(), Renderer::render(), Renderer::renderCometTail(), Renderer::renderPlanet(), Renderer::renderStar(), Observer::rotate(), rotation_get(), Quaternion< T >::setAxisAngle(), Observer::setFrame(), sin(), sqrt(), Quaternion< T >::toMatrix3(), Quaternion< T >::toMatrix4(), toUniversal(), Observer::update(), and Url::Url().


The documentation for this class was generated from the following file:
Generated on Sat Jan 14 22:33:32 2006 for Celestia by  doxygen 1.4.1