inlib  1.2.0
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
inlib::qrot< T > Class Template Reference

List of all members.

Public Member Functions

 qrot ()
 qrot (const a3::vec< T > &a_axis, T a_radians)
virtual ~qrot ()
 qrot (const qrot &a_from)
qrotoperator= (const qrot &a_from)
qrotoperator*= (const qrot &a_q)
bool operator== (const qrot &a_r) const
bool operator!= (const qrot &a_r) const
qrot operator* (const qrot &a_r) const
bool invert ()
bool inverse (qrot &a_r) const
bool set_value (const a3::vec< T > &a_axis, T a_radians)
bool value (a3::vec< T > &a_axis, T &a_radians) const
void value (a4::sqm< T > &matrix) const
void mult_vec (const a3::vec< T > &a_in, a3::vec< T > &a_out) const
const a4::vec< T > & quat () const
a4::vec< T > & quat ()

Protected Member Functions

 qrot (T a_q0, T a_q1, T a_q2, T a_q3)

Static Protected Member Functions

static T one ()
static T minus_one ()

Protected Attributes

a4::vec< T > m_quat

Detailed Description

template<class T>
class inlib::qrot< T >

Definition at line 15 of file qrot.


Constructor & Destructor Documentation

template<class T>
inlib::qrot< T >::qrot ( ) [inline]

Definition at line 17 of file qrot.

  :m_quat(0,0,0,1)   //zero rotation around the positive Z axis.
  {}
template<class T>
inlib::qrot< T >::qrot ( const a3::vec< T > &  a_axis,
a_radians 
) [inline]

Definition at line 20 of file qrot.

                                          {
    if(!set_value(a_axis,a_radians)) {} //FIXME : throw
  }
template<class T>
virtual inlib::qrot< T >::~qrot ( ) [inline, virtual]

Definition at line 26 of file qrot.

{}
template<class T>
inlib::qrot< T >::qrot ( const qrot< T > &  a_from) [inline]

Definition at line 28 of file qrot.

  :m_quat(a_from.m_quat)
  {}
template<class T>
inlib::qrot< T >::qrot ( a_q0,
a_q1,
a_q2,
a_q3 
) [inline, protected]

Definition at line 36 of file qrot.

  :m_quat(a_q0,a_q1,a_q2,a_q3)
  {
    if(!m_quat.normalize()) {} //FIXME throw
  }

Member Function Documentation

template<class T>
bool inlib::qrot< T >::inverse ( qrot< T > &  a_r) const [inline]

Definition at line 94 of file qrot.

                                {
    //Non-destructively inverses the rotation and returns the result.
    T length = m_quat.length();
    if(length==T()) return false;

    // Optimize by doing 1 div and 4 muls instead of 4 divs.
    T inv = one() / length;
  
    a_r.m_quat.set_value(-m_quat.v0() * inv,
                         -m_quat.v1() * inv,
                         -m_quat.v2() * inv,
                          m_quat.v3() * inv);
  
    return true;
  }
template<class T>
bool inlib::qrot< T >::invert ( ) [inline]

Definition at line 79 of file qrot.

               {
    T length = m_quat.length();
    if(length==T()) return false;

    // Optimize by doing 1 div and 4 muls instead of 4 divs.
    T inv = one() / length;

    m_quat.set_value(-m_quat.v0() * inv,
                     -m_quat.v1() * inv,
                     -m_quat.v2() * inv,
                      m_quat.v3() * inv);

    return true;
  }
template<class T>
static T inlib::qrot< T >::minus_one ( ) [inline, static, protected]

Definition at line 245 of file qrot.

{return T(-1);}
template<class T>
void inlib::qrot< T >::mult_vec ( const a3::vec< T > &  a_in,
a3::vec< T > &  a_out 
) const [inline]

Definition at line 217 of file qrot.

                                                            {
    const T x = m_quat.v0();
    const T y = m_quat.v1();
    const T z = m_quat.v2();
    const T w = m_quat.v3();
  
    // first row :
    T v0 =     (w*w + x*x - y*y - z*z) * a_in.v0()
              +        (2*x*y - 2*w*z) * a_in.v1()
              +        (2*x*z + 2*w*y) * a_in.v2();
  
    T v1 =             (2*x*y + 2*w*z) * a_in.v0()
              +(w*w - x*x + y*y - z*z) * a_in.v1()
              +        (2*y*z - 2*w*x) * a_in.v2();
  
    T v2 =             (2*x*z - 2*w*y) * a_in.v0()
              +        (2*y*z + 2*w*x) * a_in.v1()
              +(w*w - x*x - y*y + z*z) * a_in.v2();
  
    a_out.set_value(v0,v1,v2);
  }
template<class T>
static T inlib::qrot< T >::one ( ) [inline, static, protected]

Definition at line 244 of file qrot.

{return T(1);}
template<class T>
bool inlib::qrot< T >::operator!= ( const qrot< T > &  a_r) const [inline]

Definition at line 70 of file qrot.

                                         {
    return !operator==(a_r);
  }
template<class T>
qrot inlib::qrot< T >::operator* ( const qrot< T > &  a_r) const [inline]

Definition at line 73 of file qrot.

                                        {
    qrot tmp(*this);
    tmp *= a_r;
    return tmp;
  }
template<class T>
qrot& inlib::qrot< T >::operator*= ( const qrot< T > &  a_q) [inline]

Definition at line 43 of file qrot.

                                    {
    //Multiplies the quaternions.
    //Note that order is important when combining quaternions with the
    //multiplication operator.
    // Formula from <http://www.lboro.ac.uk/departments/ma/gallery/quat/>

    T tx = m_quat.v0();
    T ty = m_quat.v1();
    T tz = m_quat.v2();
    T tw = m_quat.v3();

    T qx = a_q.m_quat.v0();
    T qy = a_q.m_quat.v1();
    T qz = a_q.m_quat.v2();
    T qw = a_q.m_quat.v3();

    m_quat.set_value(qw*tx + qx*tw + qy*tz - qz*ty,
                     qw*ty - qx*tz + qy*tw + qz*tx,
                     qw*tz + qx*ty - qy*tx + qz*tw,
                     qw*tw - qx*tx - qy*ty - qz*tz);
    m_quat.normalize();
    return *this;
  }
template<class T>
qrot& inlib::qrot< T >::operator= ( const qrot< T > &  a_from) [inline]

Definition at line 31 of file qrot.

                                     {
    m_quat = a_from.m_quat;
    return *this;
  }
template<class T>
bool inlib::qrot< T >::operator== ( const qrot< T > &  a_r) const [inline]

Definition at line 67 of file qrot.

                                         {
    return m_quat.equal(a_r.m_quat);
  }
template<class T>
const a4::vec<T>& inlib::qrot< T >::quat ( ) const [inline]

Definition at line 240 of file qrot.

{return m_quat;}
template<class T>
a4::vec<T>& inlib::qrot< T >::quat ( ) [inline]

Definition at line 241 of file qrot.

{return m_quat;}
template<class T>
bool inlib::qrot< T >::set_value ( const a3::vec< T > &  a_axis,
a_radians 
) [inline]

Definition at line 111 of file qrot.

                                                     {
    // Reset rotation with the given axis-of-rotation and rotation angle.
    // Make sure axis is not the null vector when calling this method.
    // From <http://www.automation.hut.fi/~jaro/thesis/hyper/node9.html>.
    if(a_axis.length()==T()) return false;
    m_quat.v3(::cos(a_radians/2));
    T sineval = ::sin(a_radians/2);
    a3::vec<T> a = a_axis;
    a.normalize();
    m_quat.v0(a.v0() * sineval);
    m_quat.v1(a.v1() * sineval);
    m_quat.v2(a.v2() * sineval);
    return true;
  }
template<class T>
bool inlib::qrot< T >::value ( a3::vec< T > &  a_axis,
T &  a_radians 
) const [inline]

Definition at line 126 of file qrot.

                                                  {
    //WARNING : can fail.
    if( (m_quat.v3()<minus_one()) || (m_quat.v3()> one()) ){ 
      a_axis.set_value(0,0,1);
      a_radians = 0;
      return false;
    }

    a_radians = ::acos(m_quat.v3()) * 2;
    T sineval = ::sin(a_radians/2);

    if(sineval==T()) { //???
      a_axis.set_value(0,0,1);
      a_radians = 0;
      return false;
    }
    a_axis.set_value(m_quat.v0()/sineval,
                     m_quat.v1()/sineval,
                     m_quat.v2()/sineval);
    return true;
  }
template<class T>
void inlib::qrot< T >::value ( a4::sqm< T > &  matrix) const [inline]

Definition at line 184 of file qrot.

                                     {
    //Return this rotation in the form of a matrix.
    const T x = m_quat.v0();
    const T y = m_quat.v1();
    const T z = m_quat.v2();
    const T w = m_quat.v3();
    // z = w + x * i + y * j + z * k
  
    // first row :
    matrix.set_value(0,0,w*w + x*x - y*y - z*z);
    matrix.set_value(0,1,2*x*y - 2*w*z);
    matrix.set_value(0,2,2*x*z + 2*w*y);
    matrix.set_value(0,3,0);
  
    // second row :
    matrix.set_value(1,0,2*x*y + 2*w*z);
    matrix.set_value(1,1,w*w - x*x + y*y - z*z);
    matrix.set_value(1,2,2*y*z - 2*w*x);
    matrix.set_value(1,3,0);
  
    // third row :
    matrix.set_value(2,0,2*x*z - 2*w*y);
    matrix.set_value(2,1,2*y*z + 2*w*x);
    matrix.set_value(2,2,w*w - x*x - y*y + z*z);
    matrix.set_value(2,3,0);
  
    // fourth row :
    matrix.set_value(3,0,0);
    matrix.set_value(3,1,0);
    matrix.set_value(3,2,0);
    matrix.set_value(3,3,w*w + x*x + y*y + z*z);
  }

Member Data Documentation

template<class T>
a4::vec<T> inlib::qrot< T >::m_quat [protected]

Definition at line 248 of file qrot.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines