Back to index

nux  3.0.0
Public Member Functions | Public Attributes | Private Member Functions | Friends
nux::Quaternion Class Reference

#include <Quaternion.h>

List of all members.

Public Member Functions

 Quaternion ()
 Quaternion (const Quaternion &s)
 Quaternion (const Vector3 &vec, float angle)
 Quaternion (const Vector4 &vec)
 Quaternion (float axis_x, float axis_y, float axis_z, float angle_radian)
 Quaternion (float euler_x, float euler_y, float euler_z)
 ~Quaternion ()
Quaternionoperator= (const Quaternion &quat)
Quaternion operator+ (const Quaternion &quat) const
Quaternion operator- (const Quaternion &quat) const
Quaternion operator* (const Quaternion &quat) const
Quaternion operator* (const float &f) const
Quaternion operator/ (const float &f) const
Quaternionoperator+= (const Quaternion &quat)
Quaternionoperator-= (const Quaternion &quat)
Quaternionoperator*= (const Quaternion &quat)
Quaternionoperator*= (const float &f)
Quaternionoperator/= (const float &f)
Quaternion operator+ () const
Quaternion operator- () const
bool operator== (const Quaternion &) const
bool operator!= (const Quaternion &) const
void Conjugate ()
void Inverse ()
void Normalize ()
float DotProduct (const Quaternion &quat) const
float Length () const
void GetAngleAxis (Vector3 &axis, float &angle_radian) const
Matrix4 GetMatrix () const

Public Attributes

float x
float y
float z
float w

Private Member Functions

void FromAngleAxis (float axis_x, float axis_y, float axis_z, float angle_radian)
void FromEulerZXY (float euler_x, float euler_y, float euler_z)

Friends

Quaternion operator* (float f, const Quaternion &quat)

Detailed Description

Definition at line 39 of file Quaternion.h.


Constructor & Destructor Documentation

Definition at line 34 of file Quaternion.cpp.

  {
    x = y = z = w = 0.0f;
  }

Definition at line 39 of file Quaternion.cpp.

  {
    x = quat.x;
    y = quat.y;
    z = quat.z;
    w = quat.w;
  }
nux::Quaternion::Quaternion ( const Vector3 vec,
float  angle 
)

Definition at line 47 of file Quaternion.cpp.

  {
    FromAngleAxis (vec.x, vec.y, vec.z, angle);
  }

Here is the call graph for this function:

Definition at line 52 of file Quaternion.cpp.

  {
    FromAngleAxis (vec.w, vec.x, vec.y, vec.z);
  }

Here is the call graph for this function:

nux::Quaternion::Quaternion ( float  axis_x,
float  axis_y,
float  axis_z,
float  angle_radian 
)

Definition at line 57 of file Quaternion.cpp.

  {
    FromAngleAxis (axis_x, axis_y, axis_z, angle_radian);
  }

Here is the call graph for this function:

nux::Quaternion::Quaternion ( float  euler_x,
float  euler_y,
float  euler_z 
)

Definition at line 62 of file Quaternion.cpp.

  {
    FromEulerZXY (euler_x, euler_y, euler_z);
  }

Here is the call graph for this function:

Definition at line 67 of file Quaternion.cpp.

  {
  }

Member Function Documentation

Definition at line 245 of file Quaternion.cpp.

  {
    x = -x;
    y = -y;
    z = -z;
  }

Here is the caller graph for this function:

float nux::Quaternion::DotProduct ( const Quaternion quat) const

Definition at line 285 of file Quaternion.cpp.

  {
    float d_p;
    d_p = x * quat.x + y * quat.y + z * quat.z + w * quat.w;
    return d_p;
  }

Here is the caller graph for this function:

void nux::Quaternion::FromAngleAxis ( float  axis_x,
float  axis_y,
float  axis_z,
float  angle_radian 
) [private]

Definition at line 325 of file Quaternion.cpp.

  {
    float ax = axis_x;
    float ay = axis_y;
    float az = axis_z;

    // required: Normalize the axis

    float len = 1.0f / (float) std::sqrt ( ax * ax + ay * ay + az * az );

    ax = ax * len;
    ay = ay * len;
    az = az * len;

    float sin_theta_over_two = (float) std::sin ( angle_radian / 2.0);
    x = ax * sin_theta_over_two;
    y = ay * sin_theta_over_two;
    z = az * sin_theta_over_two;
    w = (float) std::cos (angle_radian / 2.0);
  }

Here is the caller graph for this function:

void nux::Quaternion::FromEulerZXY ( float  euler_x,
float  euler_y,
float  euler_z 
) [private]

Definition at line 362 of file Quaternion.cpp.

  {
    float roll_axis[3]  = {0.0f, 0.0f, 1.0f};
    float pitch_axis[3] = {1.0f, 0.0f, 0.0f};
    float yaw_axis[3]   = {0.0f, 1.0f, 0.0f};

    Quaternion roll (euler_z, roll_axis[0], roll_axis[1], roll_axis[2]);
    Quaternion pitch (euler_x, pitch_axis[0], pitch_axis[1], pitch_axis[2]);
    Quaternion yaw (euler_y, yaw_axis[0], yaw_axis[1], yaw_axis[2]);

    (*this) = roll * pitch * yaw;
  }

Here is the caller graph for this function:

void nux::Quaternion::GetAngleAxis ( Vector3 axis,
float &  angle_radian 
) const

Definition at line 375 of file Quaternion.cpp.

  {
    Quaternion qt;

    qt.x = x;
    qt.y = y;
    qt.z = z;
    qt.w = w;

    // make a unit quaternion
    qt.Normalize();                                 // qt = sin(angle/2)*Uq + cos(angle/2)
    angle_radian = 2.0f * (float) std::acos (qt.w);
    float one_over_sin = 1.0f / (float) std::sqrt (qt.x * qt.x + qt.y * qt.y + qt.z * qt.z);
    //float one_over_sin = 1.0f / (float) sin(angle_radian / 2.0f);

    axis.x = qt.x * one_over_sin;
    axis.y = qt.y * one_over_sin;
    axis.z = qt.z * one_over_sin;
  }

Here is the call graph for this function:

Definition at line 407 of file Quaternion.cpp.

  {
    Matrix4 mat4;
    /*float x = x;
    float y = y;
    float z = z;
    float w = w;*/
    float s;

    s = 2.0f / ( (float) std::sqrt (x * x + y * y + z * z + w * w) );

    mat4.m[0][0]  = 1.0f - s * (y * y + z * z);
    mat4.m[0][1]  = s * (x * y - w * z);
    mat4.m[0][2]  = s * (x * z + w * y);
    mat4.m[0][3]  = 0.0f;

    mat4.m[1][0]  = s * (x * y + w * z);
    mat4.m[1][1]  = 1.0f - s * (x * x + z * z);
    mat4.m[1][2]  = s * (y * z - w * x);
    mat4.m[1][3]  = 0.0f;

    mat4.m[2][0]  = s * (x * z - w * y);
    mat4.m[2][1]  = s * (y * z + w * x);
    mat4.m[2][2] = 1.0f - s * (x * x + y * y);
    mat4.m[2][3] = 0.0f;

    mat4.m[3][0] = 0.0f;
    mat4.m[3][1] = 0.0f;
    mat4.m[3][2] = 0.0f;
    mat4.m[3][3] = 1.0f;

    return mat4;
  }

Definition at line 252 of file Quaternion.cpp.

  {
    float len;
    len = (float) std::sqrt (x * x + y * y + z * z + w * w);
    Conjugate();
    x = x / len;
    y = y / len;
    z = z / len;
    w = w / len;
  }

Here is the call graph for this function:

float nux::Quaternion::Length ( ) const

Definition at line 303 of file Quaternion.cpp.

  {
    float len;
    len = (float) std::sqrt (x * x + y * y + z * z + w * w);
    return len;
  }

Definition at line 263 of file Quaternion.cpp.

  {
    float len;
    len = (float) std::sqrt (x * x + y * y + z * z + w * w);
    x = x / len;
    y = y / len;
    z = z / len;
    w = w / len;

  }

Here is the caller graph for this function:

bool nux::Quaternion::operator!= ( const Quaternion quat) const

Definition at line 240 of file Quaternion.cpp.

  {
    return ! ( (*this) == quat);
  }
Quaternion nux::Quaternion::operator* ( const Quaternion quat) const

Definition at line 101 of file Quaternion.cpp.

  {
    Quaternion qt;
    float x1, x2, y1, y2, z1, z2, w1, w2;

    x1 = x;
    y1 = y;
    z1 = z;
    w1 = w;
    x2 = quat.x;
    y2 = quat.y;
    z2 = quat.z;
    w2 = quat.w;

    qt.x = w1 * x2 + x1 * w2 - z1 * y2 + y1 * z2;
    qt.y = w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2;
    qt.z = w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2;
    qt.w = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2;

    return qt;
  }
Quaternion nux::Quaternion::operator* ( const float &  f) const

Definition at line 123 of file Quaternion.cpp.

  {
    Quaternion qt;
    qt.x = x * f;
    qt.y = y * f;
    qt.z = z * f;
    qt.w = w * f;
    return qt;
  }
Quaternion & nux::Quaternion::operator*= ( const Quaternion quat)

Definition at line 162 of file Quaternion.cpp.

  {
    Quaternion qt;
    float x1, x2, y1, y2, z1, z2, w1, w2;

    x1 = x;
    y1 = y;
    z1 = z;
    w1 = w;
    x2 = quat.x;
    y2 = quat.y;
    z2 = quat.z;
    w2 = quat.w;

    qt.x = w1 * x2 + x1 * w2 - z1 * y2 + y1 * z2;
    qt.y = w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2;
    qt.z = w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2;
    qt.w = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2;

    x = qt.x;
    y = qt.y;
    z = qt.z;
    w = qt.w;
    return *this;
  }
Quaternion & nux::Quaternion::operator*= ( const float &  f)

Definition at line 188 of file Quaternion.cpp.

  {
    x = x * f;
    y = y * f;
    z = z * f;
    w = w * f;
    return *this;
  }
Quaternion nux::Quaternion::operator+ ( const Quaternion quat) const

Definition at line 81 of file Quaternion.cpp.

  {
    Quaternion qt;
    qt.x = x + quat.x;
    qt.y = y + quat.y;
    qt.z = z + quat.z;
    qt.w = w + quat.w;
    return qt;
  }
Quaternion nux::Quaternion::operator+ ( ) const

Definition at line 207 of file Quaternion.cpp.

  {
    Quaternion qt;
    qt.x = x;
    qt.y = y;
    qt.z = z;
    qt.w = w;
    return qt;
  }
Quaternion & nux::Quaternion::operator+= ( const Quaternion quat)

Definition at line 144 of file Quaternion.cpp.

  {
    x = x + quat.x;
    y = y + quat.y;
    z = z + quat.z;
    w = w + quat.w;
    return *this;
  }
Quaternion nux::Quaternion::operator- ( const Quaternion quat) const

Definition at line 91 of file Quaternion.cpp.

  {
    Quaternion qt;
    qt.x = x - quat.x;
    qt.y = y - quat.y;
    qt.z = z - quat.z;
    qt.w = w - quat.w;
    return qt;
  }
Quaternion nux::Quaternion::operator- ( ) const

Definition at line 217 of file Quaternion.cpp.

  {
    Quaternion qt;
    qt.x = -x;
    qt.y = -y;
    qt.z = -z;
    qt.w = -w;
    return qt;
  }
Quaternion & nux::Quaternion::operator-= ( const Quaternion quat)

Definition at line 153 of file Quaternion.cpp.

  {
    x = x - quat.x;
    y = y - quat.y;
    z = z - quat.z;
    w = w - quat.w;
    return *this;
  }
Quaternion nux::Quaternion::operator/ ( const float &  f) const

Definition at line 133 of file Quaternion.cpp.

  {
    Quaternion qt;
    qt.x = x / f;
    qt.y = y / f;
    qt.z = z / f;
    qt.w = w / f;
    return qt;
  }
Quaternion & nux::Quaternion::operator/= ( const float &  f)

Definition at line 197 of file Quaternion.cpp.

  {
    x = x / f;
    y = y / f;
    z = z / f;
    w = w / f;
    return *this;
  }
Quaternion & nux::Quaternion::operator= ( const Quaternion quat)

Definition at line 71 of file Quaternion.cpp.

  {
    x = quat.x;
    y = quat.y;
    z = quat.z;
    w = quat.w;

    return (*this);
  }
bool nux::Quaternion::operator== ( const Quaternion quat) const

Definition at line 227 of file Quaternion.cpp.

  {
    if ( (x == quat.x) &&
         (y == quat.y) &&
         (z == quat.z) &&
         (w == quat.w) )
    {
      return true;
    }

    return true;
  }

Friends And Related Function Documentation

Quaternion operator* ( float  f,
const Quaternion quat 
) [friend]

Definition at line 464 of file Quaternion.cpp.

  {
    Quaternion qt;
    qt.x = quat.x * f;
    qt.y = quat.y * f;
    qt.z = quat.z * f;
    qt.w = quat.w * f;
    return qt;
  }

Member Data Documentation

Definition at line 90 of file Quaternion.h.

Definition at line 90 of file Quaternion.h.

Definition at line 90 of file Quaternion.h.

Definition at line 90 of file Quaternion.h.


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