Back to index

extremetuxracer  0.5beta
Public Member Functions | Static Public Member Functions | Public Attributes
pp::Quat Class Reference

Quaternion. More...

#include <quat.h>

List of all members.

Public Member Functions

 Quat (void)
 Quat (const double x, const double y, const double z, const double w)
 Quat (const Vec3d &s, const Vec3d &t)
 Quat (const Matrix matrix)
void set (const double x, const double y, const double z, const double w)
Quat conjugate (void) const
Vec3d rotate (const Vec3d &v) const
Quat operator* (const Quat &quat) const

Static Public Member Functions

static Quat interpolate (const Quat &q, Quat r, double t)

Public Attributes

double x
double y
double z
double w

Detailed Description

Quaternion.

Definition at line 32 of file quat.h.


Constructor & Destructor Documentation

pp::Quat::Quat ( void  ) [inline]

Definition at line 40 of file quat.h.

{};

Here is the caller graph for this function:

pp::Quat::Quat ( const double  x,
const double  y,
const double  z,
const double  w 
)

Definition at line 31 of file quat.cpp.

 : x(x), y(y), z(z), w(w)
{
}      
pp::Quat::Quat ( const Vec3d s,
const Vec3d t 
)

Definition at line 36 of file quat.cpp.

{
    Vec3d u = s^t;
       if (u.normalize() < EPS ){
              x=0.0;
              y=0.0;
              z=0.0;
              w=1.0;
    }else{
              double cos2phi = s*t;
              double sinphi = sqrt( ( 1 - cos2phi ) / 2.0 );
              double cosphi = sqrt( ( 1 + cos2phi ) / 2.0 );

              x = sinphi * u.x;
              y = sinphi * u.y;
              z = sinphi * u.z;
              w = cosphi;
    }
}

Here is the call graph for this function:

pp::Quat::Quat ( const Matrix  matrix)

Definition at line 59 of file quat.cpp.

{
    static int nxt[3] = {1, 2, 0};
    double tr = matrix.data[0][0] + matrix.data[1][1] + matrix.data[2][2];

    // check the diagonal
       if (tr > 0.0) {
              double s = sqrt (tr + 1.0);
              w = 0.5 * s;
              s = 0.5 / s;
              x = (matrix.data[1][2] - matrix.data[2][1]) * s;
              y = (matrix.data[2][0] - matrix.data[0][2]) * s;
              z = (matrix.data[0][1] - matrix.data[1][0]) * s;
    } else {                
              // diagonal is negative
              int i = 0;
              if (matrix.data[1][1] > matrix.data[0][0]) i = 1;
              if (matrix.data[2][2] > matrix.data[i][i]) i = 2;
              int j = nxt[i];
              int k = nxt[j];

              double s = sqrt (matrix.data[i][i] - matrix.data[j][j] - matrix.data[k][k] + 1.0);
        
              double q[4];         
              q[i] = s * 0.5;
                             
              if (s != 0.0) s = 0.5 / s;

              q[3] = (matrix.data[j][k] - matrix.data[k][j]) * s;
              q[j] = (matrix.data[i][j] + matrix.data[j][i]) * s;
              q[k] = (matrix.data[i][k] + matrix.data[k][i]) * s;

              x = q[0];
              y = q[1];
              z = q[2];
              w = q[3];
       }
}

Member Function Documentation

Quat pp::Quat::conjugate ( void  ) const

Definition at line 110 of file quat.cpp.

{
       return Quat(-x, -y, -z, w); 
}

Here is the call graph for this function:

Here is the caller graph for this function:

Quat pp::Quat::interpolate ( const Quat q,
Quat  r,
double  t 
) [static]

Definition at line 137 of file quat.cpp.

{
       Quat res;
    double cosphi;
    double sinphi;
    double phi;
    double scale0, scale1;

    cosphi = q.x * r.x + q.y * r.y + q.z * r.z + q.w * r.w;

    // adjust signs (if necessary) 
    if ( cosphi < 0.0 ) {
       cosphi = -cosphi;
       r.x = -r.x;
       r.y = -r.y;
       r.z = -r.z;
       r.w = -r.w;
    }

    if ( 1.0 - cosphi > EPS ) {
       // standard case -- slerp 
       phi = acos( cosphi );
       sinphi = sin( phi );
       scale0 = sin( phi * ( 1.0 - t ) ) / sinphi;
       scale1 = sin( phi * t ) / sinphi;
    } else {
       // use linear interpolation to avoid division by zero
       scale0 = 1.0 - t;
       scale1 = t;
    }

    res.x = scale0 * q.x + scale1 * r.x; 
    res.y = scale0 * q.y + scale1 * r.y; 
    res.z = scale0 * q.z + scale1 * r.z; 
    res.w = scale0 * q.w + scale1 * r.w; 

    return res;
}

Here is the caller graph for this function:

Quat pp::Quat::operator* ( const Quat quat) const

Definition at line 125 of file quat.cpp.

                                     {
       return Quat(
              y * quat.z - z * quat.y + quat.w * x + w * quat.x,
              z * quat.x - x * quat.z + quat.w * y + w * quat.y,
              x * quat.y - y * quat.x + quat.w * z + w * quat.z,
              w * quat.w - x * quat.x - y * quat.y - z * quat.z
       );
}

Here is the call graph for this function:

Vec3d pp::Quat::rotate ( const Vec3d v) const

Definition at line 116 of file quat.cpp.

{
    Quat p(v.x,v.y,v.z,1.0);
    Quat res_q = (*this)*(p*conjugate());
    
       return Vec3d(res_q.x,res_q.y,res_q.z);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void pp::Quat::set ( const double  x,
const double  y,
const double  z,
const double  w 
)

Definition at line 101 of file quat.cpp.

                                                                       {
       this->x=x;
       this->y=y;
       this->z=z;
       this->w=w;
}

Member Data Documentation

double pp::Quat::w

Definition at line 38 of file quat.h.

double pp::Quat::x

Definition at line 35 of file quat.h.

double pp::Quat::y

Definition at line 36 of file quat.h.

double pp::Quat::z

Definition at line 37 of file quat.h.


The documentation for this class was generated from the following files: