2002-07-17 04:07:32 +08:00
|
|
|
//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield
|
2001-10-04 23:12:57 +08:00
|
|
|
//Distributed under the terms of the GNU Library General Public License (LGPL)
|
|
|
|
//as published by the Free Software Foundation.
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#ifndef OSG_QUAT
|
|
|
|
#define OSG_QUAT 1
|
|
|
|
|
|
|
|
#include <osg/Vec3>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/Vec4>
|
2001-01-11 00:32:10 +08:00
|
|
|
#include <osg/Matrix>
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
|
|
|
/** A quaternion class. It can be used to represent an orientation in 3D space.*/
|
|
|
|
class SG_EXPORT Quat
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------
|
|
|
|
DATA MEMBERS
|
|
|
|
The only data member is a
|
|
|
|
Vec4 which holds the elements
|
|
|
|
|
|
|
|
In other words, osg:Quat is composed of an osg::Vec4
|
|
|
|
The osg::Quat aggregates an osg::Vec4
|
|
|
|
|
2001-10-01 19:15:55 +08:00
|
|
|
These seem to be different jargon for the same thing :-)
|
2001-01-11 00:32:10 +08:00
|
|
|
---------------------------------------------------------- */
|
|
|
|
Vec4 _fv; // a four-vector
|
|
|
|
|
2002-10-08 03:51:11 +08:00
|
|
|
inline Quat(): _fv(0.0f,0.0f,0.0f,1.0f) {}
|
|
|
|
inline Quat( float x, float y, float z, float w ): _fv(x,y,z,w) {}
|
|
|
|
inline Quat( const Vec4& v ): _fv(v) {}
|
|
|
|
|
|
|
|
inline Quat( float angle, const Vec3& axis)
|
|
|
|
{
|
|
|
|
makeRotate(angle,axis);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Quat( float angle1, const Vec3& axis1,
|
|
|
|
float angle2, const Vec3& axis2,
|
|
|
|
float angle3, const Vec3& axis3)
|
|
|
|
{
|
|
|
|
makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
/* ----------------------------------
|
|
|
|
Methods to access data members
|
|
|
|
---------------------------------- */
|
2001-09-20 05:08:56 +08:00
|
|
|
inline Vec4& asVec4()
|
|
|
|
{
|
|
|
|
return _fv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const Vec4& asVec4() const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return _fv;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const Vec3 asVec3() const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return Vec3(_fv[0], _fv[1], _fv[2]);
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline void set(float x, float y, float z, float w)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
_fv.set(x,y,z,w);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void set(const osg::Vec4& v)
|
|
|
|
{
|
|
|
|
_fv = v;
|
|
|
|
}
|
|
|
|
|
2002-08-28 04:18:43 +08:00
|
|
|
inline float& operator [] (int i) { return _fv[i]; }
|
|
|
|
inline float operator [] (int i) const { return _fv[i]; }
|
|
|
|
|
|
|
|
inline float& x() { return _fv[0]; }
|
|
|
|
inline float& y() { return _fv[1]; }
|
|
|
|
inline float& z() { return _fv[2]; }
|
|
|
|
inline float& w() { return _fv[3]; }
|
|
|
|
|
|
|
|
inline float x() const { return _fv[0]; }
|
|
|
|
inline float y() const { return _fv[1]; }
|
|
|
|
inline float z() const { return _fv[2]; }
|
|
|
|
inline float w() const { return _fv[3]; }
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-10-25 20:29:03 +08:00
|
|
|
/** return true if the Quat represents a zero rotation, and therefore can be ignored in computations.*/
|
|
|
|
bool zeroRotation() const { return _fv[0]==0.0f && _fv[1]==0.0f && _fv[2]==0.0f && _fv[3]==1.0f; }
|
|
|
|
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/* -------------------------------------------------------------
|
|
|
|
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.
|
|
|
|
------------------------------------------------------------- */
|
|
|
|
/// Multiply by scalar
|
2002-09-02 20:31:35 +08:00
|
|
|
inline const Quat operator * (float rhs) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return Quat(_fv*rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unary multiply by scalar
|
2002-09-02 20:31:35 +08:00
|
|
|
inline Quat& operator *= (float rhs)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
_fv*=rhs;
|
|
|
|
return *this; // enable nesting
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Binary multiply
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const Quat operator*(const Quat& rhs) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
Added DOFTransform, MatrixTransform and PositionAttitudeTransform to NodeVisitor.
Added osg::Matrix/Quat::makeRotate(angle1,axis1,angle2,axis2,angle3,axis3) and
osg::Matrix::rotate(angle1,axis1,angle2,axis2,angle3,axis3) method.
Made osg::Matrix/Quat::makeRotate(heading,pitch,roll) and
osg::Matrix::rotate(heading,pitch,roll) all deprecated API.
Fixed the Quat*Quat & Quat*=Quat multiplication methods so that they multiplied
in the correct order - they were reversed originally due to the Quat code being
based on code example which used the v' = M v ordering, rather than the OSG's
v' = v M ordering.
2002-08-18 22:42:43 +08:00
|
|
|
return Quat( rhs._fv[3]*_fv[0] + rhs._fv[0]*_fv[3] + rhs._fv[1]*_fv[2] - rhs._fv[2]*_fv[1],
|
|
|
|
rhs._fv[3]*_fv[1] - rhs._fv[0]*_fv[2] + rhs._fv[1]*_fv[3] + rhs._fv[2]*_fv[0],
|
|
|
|
rhs._fv[3]*_fv[2] + rhs._fv[0]*_fv[1] - rhs._fv[1]*_fv[0] + rhs._fv[2]*_fv[3],
|
|
|
|
rhs._fv[3]*_fv[3] - rhs._fv[0]*_fv[0] - rhs._fv[1]*_fv[1] - rhs._fv[2]*_fv[2] );
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unary multiply
|
|
|
|
inline Quat& operator*=(const Quat& rhs)
|
|
|
|
{
|
Added DOFTransform, MatrixTransform and PositionAttitudeTransform to NodeVisitor.
Added osg::Matrix/Quat::makeRotate(angle1,axis1,angle2,axis2,angle3,axis3) and
osg::Matrix::rotate(angle1,axis1,angle2,axis2,angle3,axis3) method.
Made osg::Matrix/Quat::makeRotate(heading,pitch,roll) and
osg::Matrix::rotate(heading,pitch,roll) all deprecated API.
Fixed the Quat*Quat & Quat*=Quat multiplication methods so that they multiplied
in the correct order - they were reversed originally due to the Quat code being
based on code example which used the v' = M v ordering, rather than the OSG's
v' = v M ordering.
2002-08-18 22:42:43 +08:00
|
|
|
float x = rhs._fv[3]*_fv[0] + rhs._fv[0]*_fv[3] + rhs._fv[1]*_fv[2] - rhs._fv[2]*_fv[1];
|
|
|
|
float y = rhs._fv[3]*_fv[1] - rhs._fv[0]*_fv[2] + rhs._fv[1]*_fv[3] + rhs._fv[2]*_fv[0];
|
|
|
|
float z = rhs._fv[3]*_fv[2] + rhs._fv[0]*_fv[1] - rhs._fv[1]*_fv[0] + rhs._fv[2]*_fv[3];
|
|
|
|
_fv[3] = rhs._fv[3]*_fv[3] - rhs._fv[0]*_fv[0] - rhs._fv[1]*_fv[1] - rhs._fv[2]*_fv[2];
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
_fv[2] = z;
|
|
|
|
_fv[1] = y;
|
|
|
|
_fv[0] = x;
|
|
|
|
|
|
|
|
return (*this); // enable nesting
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Divide by scalar
|
2002-09-02 20:31:35 +08:00
|
|
|
inline const Quat operator / (float rhs) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return Quat(_fv/rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unary divide by scalar
|
2002-09-02 20:31:35 +08:00
|
|
|
inline Quat& operator /= (float rhs)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
_fv/=rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Binary divide
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const Quat operator/(const Quat& denom) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return ( (*this) * denom.inverse() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unary divide
|
|
|
|
inline Quat& operator/=(const Quat& denom)
|
|
|
|
{
|
|
|
|
(*this) = (*this) * denom.inverse();
|
|
|
|
return (*this); // enable nesting
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Binary addition
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const Quat operator + (const Quat& rhs) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return Quat( _fv + rhs._fv );
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unary addition
|
|
|
|
inline Quat& operator += (const Quat& rhs)
|
|
|
|
{
|
|
|
|
_fv += rhs._fv;
|
|
|
|
return *this; // enable nesting
|
|
|
|
}
|
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
/// Binary subtraction
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const Quat operator - (const Quat& rhs) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return Quat( _fv - rhs._fv );
|
|
|
|
}
|
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
/// Unary subtraction
|
2001-01-11 00:32:10 +08:00
|
|
|
inline Quat& operator -= (const Quat& rhs)
|
|
|
|
{
|
|
|
|
_fv-=rhs._fv;
|
|
|
|
return *this; // enable nesting
|
|
|
|
}
|
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
/** Negation operator - returns the negative of the quaternion.
|
2001-01-11 00:32:10 +08:00
|
|
|
Basically just calls operator - () on the Vec4 */
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const Quat operator - () const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return Quat ( -_fv );
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Length of the quaternion = sqrt( vec . vec )
|
2002-09-02 20:31:35 +08:00
|
|
|
float length() const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return _fv.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Length of the quaternion = vec . vec
|
2002-09-02 20:31:35 +08:00
|
|
|
float length2() const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return _fv.length2();
|
|
|
|
}
|
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
/// Conjugate
|
2002-09-02 20:31:35 +08:00
|
|
|
inline Quat conj () const
|
2001-12-15 07:18:28 +08:00
|
|
|
{
|
|
|
|
return Quat( -_fv[0], -_fv[1], -_fv[2], _fv[3] );
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
/// Multiplicative inverse method: q^(-1) = q^*/(q.q^*)
|
|
|
|
inline const Quat inverse () const
|
|
|
|
{
|
|
|
|
return conj() / length2();
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
/* --------------------------------------------------------
|
|
|
|
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?
|
|
|
|
|
2001-10-01 19:15:55 +08:00
|
|
|
Define Spherical Linear interpolation method also
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
Not inlined - see the Quat.cpp file for implementation
|
|
|
|
-------------------------------------------------------- */
|
2002-10-08 13:41:19 +08:00
|
|
|
void makeRotate( float angle,
|
2002-09-02 20:31:35 +08:00
|
|
|
float x, float y, float z );
|
|
|
|
void makeRotate ( float angle, const Vec3& vec );
|
2001-01-11 00:32:10 +08:00
|
|
|
|
Added DOFTransform, MatrixTransform and PositionAttitudeTransform to NodeVisitor.
Added osg::Matrix/Quat::makeRotate(angle1,axis1,angle2,axis2,angle3,axis3) and
osg::Matrix::rotate(angle1,axis1,angle2,axis2,angle3,axis3) method.
Made osg::Matrix/Quat::makeRotate(heading,pitch,roll) and
osg::Matrix::rotate(heading,pitch,roll) all deprecated API.
Fixed the Quat*Quat & Quat*=Quat multiplication methods so that they multiplied
in the correct order - they were reversed originally due to the Quat code being
based on code example which used the v' = M v ordering, rather than the OSG's
v' = v M ordering.
2002-08-18 22:42:43 +08:00
|
|
|
void makeRotate ( float angle1, const Vec3& axis1,
|
|
|
|
float angle2, const Vec3& axis2,
|
|
|
|
float angle3, const Vec3& axis3);
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Make a rotation Quat which will rotate vec1 to vec2.
|
|
|
|
Generally take adot product to get the angle between these
|
|
|
|
and then use a cross product to get the rotation axis
|
|
|
|
Watch out for the two special cases of when the vectors
|
|
|
|
are co-incident or opposite in direction.*/
|
2001-12-13 04:29:10 +08:00
|
|
|
void makeRotate( const Vec3& vec1, const Vec3& vec2 );
|
2002-02-27 04:01:04 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Return the angle and vector components represented by the quaternion.*/
|
2001-12-15 07:18:28 +08:00
|
|
|
void getRotate ( float& angle, float& x, float& y, float& z ) const;
|
|
|
|
/** Return the angle and vector represented by the quaternion.*/
|
|
|
|
void getRotate ( float& angle, Vec3& vec ) const;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
/** Spherical Linear Interpolation.
|
2001-01-11 00:32:10 +08:00
|
|
|
As t goes from 0 to 1, the Quat object goes from "from" to "to". */
|
2002-09-02 20:31:35 +08:00
|
|
|
void slerp ( float t, const Quat& from, const Quat& to);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
/** Set quaternion to be equivalent to specified matrix.*/
|
2002-02-27 08:58:54 +08:00
|
|
|
void set( const Matrix& m );
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
/** Get the equivalent matrix for this quaternion.*/
|
2002-02-27 08:58:54 +08:00
|
|
|
void get( Matrix& m ) const;
|
|
|
|
|
|
|
|
/** Get the equivalent matrix for this quaternion.*/
|
|
|
|
Matrix getMatrix() const
|
|
|
|
{
|
|
|
|
Matrix matrix;
|
|
|
|
get(matrix);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
friend inline std::ostream& operator << (std::ostream& output, const Quat& vec);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
}; // end of class prototype
|
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
inline std::ostream& operator << (std::ostream& output, const Quat& vec)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
output << vec._fv[0] << " "
|
|
|
|
<< vec._fv[1] << " "
|
|
|
|
<< vec._fv[2] << " "
|
|
|
|
<< vec._fv[3];
|
|
|
|
return output; // to enable cascading
|
|
|
|
}
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
} // end of namespace
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#endif
|