Quat
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_QUAT
#define OSG_QUAT 1
#include <osg/Export>
#include <osg/Vec3f>
#include <osg/Vec4f>
#include <osg/Vec3d>
#include <osg/Vec4d>
namespace osg {
class Matrixf;
class Matrixd;
class OSG_EXPORT Quat
{
public:
#ifdef OSG_USE_FLOAT_QUAT
typedef float value_type;
#else
typedef double value_type;
#endif
enum { num_components = 4 };
value_type _v[4]; // a four-vector
inline Quat() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0; _v[3]=1.0; }
inline Quat( value_type x, value_type y, value_type z, value_type w )
{
_v[0]=x;
_v[1]=y;
_v[2]=z;
_v[3]=w;
}
inline Quat( const Vec4f& v )
{
_v[0]=v.x();
_v[1]=v.y();
_v[2]=v.z();
_v[3]=v.w();
}
inline Quat( const Vec4d& v )
{
_v[0]=v.x();
_v[1]=v.y();
_v[2]=v.z();
_v[3]=v.w();
}
inline Quat( value_type angle, const Vec3f& axis)
{
makeRotate(angle,axis);
}
inline Quat( value_type angle, const Vec3d& axis)
{
makeRotate(angle,axis);
}
inline Quat( value_type angle1, const Vec3f& axis1,
value_type angle2, const Vec3f& axis2,
value_type angle3, const Vec3f& axis3)
{
makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
}
inline Quat( value_type angle1, const Vec3d& axis1,
value_type angle2, const Vec3d& axis2,
value_type angle3, const Vec3d& axis3)
{
makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
}
Quat(const Quat& q) { _v[0]=q._v[0]; _v[1]=q._v[1]; _v[2]=q._v[2]; _v[3]=q._v[3]; }
inline Quat& operator = (const Quat& v) { _v[0]=v._v[0]; _v[1]=v._v[1]; _v[2]=v._v[2]; _v[3]=v._v[3]; return *this; }
inline bool operator == (const Quat& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2] && _v[3]==v._v[3]; }
inline bool operator != (const Quat& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; }
inline bool operator < (const Quat& v) const
{
if (_v[0]<v._v[0]) return true;
else if (_v[0]>v._v[0]) return false;
else if (_v[1]<v._v[1]) return true;
else if (_v[1]>v._v[1]) return false;
else if (_v[2]<v._v[2]) return true;
else if (_v[2]>v._v[2]) return false;
else return (_v[3]<v._v[3]);
}
/* ----------------------------------
Methods to access data members
---------------------------------- */
inline Vec4d asVec4() const
{
return Vec4d(_v[0], _v[1], _v[2], _v[3]);
}
inline Vec3d asVec3() const
{
return Vec3d(_v[0], _v[1], _v[2]);
}
inline void set(value_type x, value_type y, value_type z, value_type w)
{
_v[0]=x;
_v[1]=y;
_v[2]=z;
_v[3]=w;
}
inline void set(const osg::Vec4f& v)
{
_v[0]=v.x();
_v[1]=v.y();
_v[2]=v.z();
_v[3]=v.w();
}
inline void set(const osg::Vec4d& v)
{
_v[0]=v.x();
_v[1]=v.y();
_v[2]=v.z();
_v[3]=v.w();
}
void set(const Matrixf& matrix);
void set(const Matrixd& matrix);
void get(Matrixf& matrix) const;
void get(Matrixd& matrix) const;
inline value_type & operator [] (int i) { return _v[i]; }
inline value_type operator [] (int i) const { return _v[i]; }
inline value_type & x() { return _v[0]; }
inline value_type & y() { return _v[1]; }
inline value_type & z() { return _v[2]; }
inline value_type & w() { return _v[3]; }
inline value_type x() const { return _v[0]; }
inline value_type y() const { return _v[1]; }
inline value_type z() const { return _v[2]; }
inline value_type w() const { return _v[3]; }
bool zeroRotation() const { return _v[0]==0.0 && _v[1]==0.0 && _v[2]==0.0 && _v[3]==1.0; }
/* -------------------------------------------------------------
BASIC ARITHMETIC METHODS
Implemented in terms of Vec4s. Some Vec4 operators, e.g.
operator* are not appropriate for quaternions (as
mathematical objects) so they are implemented differently.
Also define methods for conjugate and the multiplicative inverse.
------------------------------------------------------------- */
inline const Quat operator * (value_type rhs) const
{
return Quat(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
}
inline Quat& operator *= (value_type rhs)
{
_v[0]*=rhs;
_v[1]*=rhs;
_v[2]*=rhs;
_v[3]*=rhs;
return *this; // enable nesting
}
inline const Quat operator*(const Quat& rhs) const
{
return Quat( rhs._v[3]*_v[0] + rhs._v[0]*_v[3] + rhs._v[1]*_v[2] - rhs._v[2]*_v[1],
rhs._v[3]*_v[1] - rhs._v[0]*_v[2] + rhs._v[1]*_v[3] + rhs._v[2]*_v[0],
rhs._v[3]*_v[2] + rhs._v[0]*_v[1] - rhs._v[1]*_v[0] + rhs._v[2]*_v[3],
rhs._v[3]*_v[3] - rhs._v[0]*_v[0] - rhs._v[1]*_v[1] - rhs._v[2]*_v[2] );
}
inline Quat& operator*=(const Quat& rhs)
{
value_type x = rhs._v[3]*_v[0] + rhs._v[0]*_v[3] + rhs._v[1]*_v[2] - rhs._v[2]*_v[1];
value_type y = rhs._v[3]*_v[1] - rhs._v[0]*_v[2] + rhs._v[1]*_v[3] + rhs._v[2]*_v[0];
value_type z = rhs._v[3]*_v[2] + rhs._v[0]*_v[1] - rhs._v[1]*_v[0] + rhs._v[2]*_v[3];
_v[3] = rhs._v[3]*_v[3] - rhs._v[0]*_v[0] - rhs._v[1]*_v[1] - rhs._v[2]*_v[2];
_v[2] = z;
_v[1] = y;
_v[0] = x;
return (*this); // enable nesting
}
inline Quat operator / (value_type rhs) const
{
value_type div = 1.0/rhs;
return Quat(_v[0]*div, _v[1]*div, _v[2]*div, _v[3]*div);
}
inline Quat& operator /= (value_type rhs)
{
value_type div = 1.0/rhs;
_v[0]*=div;
_v[1]*=div;
_v[2]*=div;
_v[3]*=div;
return *this;
}
inline const Quat operator/(const Quat& denom) const
{
return ( (*this) * denom.inverse() );
}
inline Quat& operator/=(const Quat& denom)
{
(*this) = (*this) * denom.inverse();
return (*this); // enable nesting
}
inline const Quat operator + (const Quat& rhs) const
{
return Quat(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
_v[2]+rhs._v[2], _v[3]+rhs._v[3]);
}
inline Quat& operator += (const Quat& rhs)
{
_v[0] += rhs._v[0];
_v[1] += rhs._v[1];
_v[2] += rhs._v[2];
_v[3] += rhs._v[3];
return *this; // enable nesting
}
inline const Quat operator - (const Quat& rhs) const
{
return Quat(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
_v[2]-rhs._v[2], _v[3]-rhs._v[3] );
}
inline Quat& operator -= (const Quat& rhs)
{
_v[0]-=rhs._v[0];
_v[1]-=rhs._v[1];
_v[2]-=rhs._v[2];
_v[3]-=rhs._v[3];
return *this; // enable nesting
}
inline const Quat operator - () const
{
return Quat (-_v[0], -_v[1], -_v[2], -_v[3]);
}
value_type length() const
{
return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
}
value_type length2() const
{
return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
}
inline Quat conj () const
{
return Quat( -_v[0], -_v[1], -_v[2], _v[3] );
}
inline const Quat inverse () const
{
return conj() / length2();
}
/* --------------------------------------------------------
METHODS RELATED TO ROTATIONS
Set a quaternion which will perform a rotation of an
angle around the axis given by the vector (x,y,z).
Should be written to also accept an angle and a Vec3?
Define Spherical Linear interpolation method also
Not inlined - see the Quat.cpp file for implementation
-------------------------------------------------------- */
void makeRotate( value_type angle,
value_type x, value_type y, value_type z );
void makeRotate ( value_type angle, const Vec3f& vec );
void makeRotate ( value_type angle, const Vec3d& vec );
void makeRotate ( value_type angle1, const Vec3f& axis1,
value_type angle2, const Vec3f& axis2,
value_type angle3, const Vec3f& axis3);
void makeRotate ( value_type angle1, const Vec3d& axis1,
value_type angle2, const Vec3d& axis2,
value_type angle3, const Vec3d& axis3);
void makeRotate( const Vec3f& vec1, const Vec3f& vec2 );
void makeRotate( const Vec3d& vec1, const Vec3d& vec2 );
void makeRotate_original( const Vec3d& vec1, const Vec3d& vec2 );
void getRotate ( value_type & angle, value_type & x, value_type & y, value_type & z ) const;
void getRotate ( value_type & angle, Vec3f& vec ) const;
void getRotate ( value_type & angle, Vec3d& vec ) const;
void slerp ( value_type t, const Quat& from, const Quat& to);
Vec3f operator* (const Vec3f& v) const
{
// nVidia SDK implementation
Vec3f uv, uuv;
Vec3f qvec(_v[0], _v[1], _v[2]);
uv = qvec ^ v;
uuv = qvec ^ uv;
uv *= ( 2.0f * _v[3] );
uuv *= 2.0f;
return v + uv + uuv;
}
Vec3d operator* (const Vec3d& v) const
{
// nVidia SDK implementation
Vec3d uv, uuv;
Vec3d qvec(_v[0], _v[1], _v[2]);
uv = qvec ^ v;
uuv = qvec ^ uv;
uv *= ( 2.0f * _v[3] );
uuv *= 2.0f;
return v + uv + uuv;
}
protected:
}; // end of class prototype
} // end of namespace
#endif
Vec3d
Vec3f
Vec4d
Vec4f
osg::Matrixd
Definition Matrixd:27
osg::Matrixf
Definition Matrixf:27
osg::Quat::x
value_type x() const
Definition Quat:169
osg::Quat::set
void set(const Matrixf &matrix)
osg::Quat::Quat
Quat(value_type angle, const Vec3d &axis)
Definition Quat:76
osg::Quat::getRotate
void getRotate(value_type &angle, Vec3d &vec) const
osg::Quat::w
value_type & w()
Definition Quat:167
osg::Quat::num_components
@ num_components
Definition Quat:42
osg::Quat::Quat
Quat()
Definition Quat:46
osg::Quat::zeroRotation
bool zeroRotation() const
Definition Quat:175
osg::Quat::asVec4
Vec4d asVec4() const
Definition Quat:118
osg::Quat::y
value_type y() const
Definition Quat:170
osg::Quat::operator*
const Quat operator*(const Quat &rhs) const
Binary multiply.
Definition Quat:202
osg::Quat::operator/
const Quat operator/(const Quat &denom) const
Binary divide.
Definition Quat:244
osg::Quat::inverse
const Quat inverse() const
Multiplicative inverse method: q^(-1) = q^*‍/(q.q^*).
Definition Quat:316
osg::Quat::Quat
Quat(value_type angle, const Vec3f &axis)
Definition Quat:72
osg::Quat::makeRotate
void makeRotate(value_type angle, const Vec3d &vec)
osg::Quat::Quat
Quat(value_type x, value_type y, value_type z, value_type w)
Definition Quat:48
osg::Quat::set
void set(const osg::Vec4d &v)
Definition Quat:144
osg::Quat::getRotate
void getRotate(value_type &angle, Vec3f &vec) const
osg::Quat::y
value_type & y()
Definition Quat:165
osg::Quat::length
value_type length() const
Length of the quaternion = sqrt( vec . vec ).
Definition Quat:298
osg::Quat::makeRotate
void makeRotate(value_type angle1, const Vec3f &axis1, value_type angle2, const Vec3f &axis2, value_type angle3, const Vec3f &axis3)
osg::Quat::makeRotate
void makeRotate(value_type angle, value_type x, value_type y, value_type z)
osg::Quat::Quat
Quat(const Quat &q)
Definition Quat:95
osg::Quat::makeRotate
void makeRotate(const Vec3f &vec1, const Vec3f &vec2)
osg::Quat::getRotate
void getRotate(value_type &angle, value_type &x, value_type &y, value_type &z) const
osg::Quat::makeRotate_original
void makeRotate_original(const Vec3d &vec1, const Vec3d &vec2)
osg::Quat::asVec3
Vec3d asVec3() const
Definition Quat:123
osg::Quat::x
value_type & x()
Definition Quat:164
osg::Quat::conj
Quat conj() const
Conjugate.
Definition Quat:310
osg::Quat::Quat
Quat(const Vec4f &v)
Definition Quat:56
osg::Quat::get
void get(Matrixf &matrix) const
osg::Quat::z
value_type & z()
Definition Quat:166
osg::Quat::_v
value_type _v[4]
Definition Quat:44
osg::Quat::makeRotate
void makeRotate(value_type angle, const Vec3f &vec)
osg::Quat::operator/=
Quat & operator/=(const Quat &denom)
Unary divide.
Definition Quat:250
osg::Quat::operator*=
Quat & operator*=(const Quat &rhs)
Unary multiply.
Definition Quat:211
osg::Quat::Quat
Quat(value_type angle1, const Vec3f &axis1, value_type angle2, const Vec3f &axis2, value_type angle3, const Vec3f &axis3)
Definition Quat:81
osg::Quat::set
void set(value_type x, value_type y, value_type z, value_type w)
Definition Quat:128
osg::Quat::makeRotate
void makeRotate(const Vec3d &vec1, const Vec3d &vec2)
osg::Quat::value_type
double value_type
Definition Quat:38
osg::Quat::z
value_type z() const
Definition Quat:171
osg::Quat::set
void set(const Matrixd &matrix)
osg::Quat::Quat
Quat(value_type angle1, const Vec3d &axis1, value_type angle2, const Vec3d &axis2, value_type angle3, const Vec3d &axis3)
Definition Quat:88
osg::Quat::w
value_type w() const
Definition Quat:172
osg::Quat::Quat
Quat(const Vec4d &v)
Definition Quat:64
osg::Quat::get
void get(Matrixd &matrix) const
osg::Quat::makeRotate
void makeRotate(value_type angle1, const Vec3d &axis1, value_type angle2, const Vec3d &axis2, value_type angle3, const Vec3d &axis3)
osg::Quat::length2
value_type length2() const
Length of the quaternion = vec . vec.
Definition Quat:304
osg::Quat::slerp
void slerp(value_type t, const Quat &from, const Quat &to)
osg::Quat::set
void set(const osg::Vec4f &v)
Definition Quat:136
osg::Vec3d
Definition Vec3d:30
osg::Vec3f
Definition Vec3f:29
osg::Vec4d
Definition Vec4d:29
osg::Vec4d::x
value_type & x()
Definition Vec4d:90
osg::Vec4d::y
value_type & y()
Definition Vec4d:91
osg::Vec4d::z
value_type & z()
Definition Vec4d:92
osg::Vec4d::w
value_type & w()
Definition Vec4d:93
osg::Vec4f
Definition Vec4f:28
osg::Vec4f::y
value_type & y()
Definition Vec4f:88
osg::Vec4f::x
value_type & x()
Definition Vec4f:87
osg::Vec4f::z
value_type & z()
Definition Vec4f:89
osg::Vec4f::w
value_type & w()
Definition Vec4f:90
osg
Definition AlphaFunc:19
Export
OSG_EXPORT
#define OSG_EXPORT
Definition osg/Export:39