Made osg::Quat support either float or double internal representation, defaulting to double.

Generalised the osgDB::Field so that its getFloat() method can be used with either doubles or
floats governed by the type passed in - this helps support either float/double
Quat and Matrix classes seemlessly.
This commit is contained in:
Robert Osfield
2003-09-29 13:35:02 +00:00
parent 7d69f8e193
commit e693f148cb
10 changed files with 173 additions and 165 deletions

View File

@@ -29,66 +29,83 @@ class SG_EXPORT Quat
public:
/* ----------------------------------------------------------
DATA MEMBERS
The only data member is a
Vec4 which holds the elements
typedef double value_type;
In other words, osg:Quat is composed of an osg::Vec4
The osg::Quat aggregates an osg::Vec4
value_type _v[4]; // a four-vector
These seem to be different jargon for the same thing :-)
---------------------------------------------------------- */
Vec4 _fv; // a four-vector
inline Quat() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0; _v[3]=0.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(): _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( const Vec4& v )
{
_v[0]=v.x();
_v[1]=v.y();
_v[2]=v.z();
_v[3]=v.w();
}
inline Quat( float angle, const Vec3& axis)
inline Quat( value_type angle, const Vec3& axis)
{
makeRotate(angle,axis);
}
inline Quat( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3)
inline Quat( value_type angle1, const Vec3& axis1,
value_type angle2, const Vec3& axis2,
value_type angle3, const Vec3& axis3)
{
makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
}
inline bool operator == (const Quat& rhs) const { return _fv==rhs._fv; }
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& rhs) const { return _fv!=rhs._fv; }
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& rhs) const { return _fv<rhs._fv; }
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 Vec4& asVec4()
inline Vec4 asVec4() const
{
return _fv;
return Vec4(_v[0], _v[1], _v[2], _v[3]);
}
inline const Vec4& asVec4() const
inline Vec3 asVec3() const
{
return _fv;
return Vec3(_v[0], _v[1], _v[2]);
}
inline const Vec3 asVec3() const
{
return Vec3(_fv[0], _fv[1], _fv[2]);
}
inline void set(float x, float y, float z, float w)
inline void set(value_type x, value_type y, value_type z, value_type w)
{
_fv.set(x,y,z,w);
_v[0]=x;
_v[1]=y;
_v[2]=z;
_v[3]=w;
}
inline void set(const osg::Vec4& v)
{
_fv = v;
_v[0]=v.x();
_v[1]=v.y();
_v[2]=v.z();
_v[3]=v.w();
}
void set(const Matrixf& matrix);
@@ -100,21 +117,21 @@ class SG_EXPORT Quat
void get(Matrixd& matrix) const;
inline float& operator [] (int i) { return _fv[i]; }
inline float operator [] (int i) const { return _fv[i]; }
inline value_type & operator [] (int i) { return _v[i]; }
inline value_type operator [] (int i) const { return _v[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 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 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]; }
inline float x() const { return _v[0]; }
inline float y() const { return _v[1]; }
inline float z() const { return _v[2]; }
inline float w() const { return _v[3]; }
/** 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; }
bool zeroRotation() const { return _v[0]==0.0 && _v[1]==0.0 && _v[2]==0.0 && _v[3]==1.0; }
/* -------------------------------------------------------------
@@ -127,50 +144,58 @@ class SG_EXPORT Quat
/// Multiply by scalar
inline const Quat operator * (float rhs) const
{
return Quat(_fv*rhs);
return Quat(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
}
/// Unary multiply by scalar
inline Quat& operator *= (float rhs)
{
_fv*=rhs;
_v[0]*=rhs;
_v[1]*=rhs;
_v[2]*=rhs;
_v[3]*=rhs;
return *this; // enable nesting
}
/// Binary multiply
inline const Quat operator*(const Quat& rhs) const
{
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] );
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] );
}
/// Unary multiply
inline Quat& operator*=(const Quat& rhs)
{
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];
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];
_fv[2] = z;
_fv[1] = y;
_fv[0] = x;
_v[2] = z;
_v[1] = y;
_v[0] = x;
return (*this); // enable nesting
}
/// Divide by scalar
inline const Quat operator / (float rhs) const
inline Quat operator / (float rhs) const
{
return Quat(_fv/rhs);
value_type div = 1.0/rhs;
return Quat(_v[0]*div, _v[1]*div, _v[2]*div, _v[3]*div);
}
/// Unary divide by scalar
inline Quat& operator /= (float rhs)
{
_fv/=rhs;
value_type div = 1.0/rhs;
_v[0]*=div;
_v[1]*=div;
_v[2]*=div;
_v[3]*=div;
return *this;
}
@@ -190,26 +215,34 @@ class SG_EXPORT Quat
/// Binary addition
inline const Quat operator + (const Quat& rhs) const
{
return Quat( _fv + rhs._fv );
return Vec4(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
_v[2]+rhs._v[2], _v[3]+rhs._v[3]);
}
/// Unary addition
inline Quat& operator += (const Quat& rhs)
{
_fv += rhs._fv;
_v[0] += rhs._v[0];
_v[1] += rhs._v[1];
_v[2] += rhs._v[2];
_v[3] += rhs._v[3];
return *this; // enable nesting
}
/// Binary subtraction
inline const Quat operator - (const Quat& rhs) const
{
return Quat( _fv - rhs._fv );
return Quat(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
_v[2]-rhs._v[2], _v[3]-rhs._v[3] );
}
/// Unary subtraction
inline Quat& operator -= (const Quat& rhs)
{
_fv-=rhs._fv;
_v[0]-=rhs._v[0];
_v[1]-=rhs._v[1];
_v[2]-=rhs._v[2];
_v[3]-=rhs._v[3];
return *this; // enable nesting
}
@@ -217,25 +250,25 @@ class SG_EXPORT Quat
Basically just calls operator - () on the Vec4 */
inline const Quat operator - () const
{
return Quat ( -_fv );
return Quat (-_v[0], -_v[1], -_v[2], -_v[3]);
}
/// Length of the quaternion = sqrt( vec . vec )
float length() const
value_type length() const
{
return _fv.length();
return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
}
/// Length of the quaternion = vec . vec
float length2() const
value_type length2() const
{
return _fv.length2();
return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
}
/// Conjugate
inline Quat conj () const
{
return Quat( -_fv[0], -_fv[1], -_fv[2], _fv[3] );
return Quat( -_v[0], -_v[1], -_v[2], _v[3] );
}
/// Multiplicative inverse method: q^(-1) = q^*/(q.q^*)
@@ -254,13 +287,13 @@ class SG_EXPORT Quat
Not inlined - see the Quat.cpp file for implementation
-------------------------------------------------------- */
void makeRotate( float angle,
float x, float y, float z );
void makeRotate ( float angle, const Vec3& vec );
void makeRotate( value_type angle,
value_type x, value_type y, value_type z );
void makeRotate ( value_type angle, const Vec3& vec );
void makeRotate ( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3);
void makeRotate ( value_type angle1, const Vec3& axis1,
value_type angle2, const Vec3& axis2,
value_type angle3, const Vec3& axis3);
/** Make a rotation Quat which will rotate vec1 to vec2.
Generally take adot product to get the angle between these
@@ -270,13 +303,13 @@ class SG_EXPORT Quat
void makeRotate( const Vec3& vec1, const Vec3& vec2 );
/** Return the angle and vector components represented by the quaternion.*/
void getRotate ( float& angle, float& x, float& y, float& z ) const;
void getRotate ( value_type & angle, value_type & x, value_type & y, value_type & z ) const;
/** Return the angle and vector represented by the quaternion.*/
void getRotate ( float& angle, Vec3& vec ) const;
void getRotate ( value_type & angle, Vec3& vec ) const;
/** Spherical Linear Interpolation.
As t goes from 0 to 1, the Quat object goes from "from" to "to". */
void slerp ( float t, const Quat& from, const Quat& to);
void slerp ( value_type t, const Quat& from, const Quat& to);
friend inline std::ostream& operator << (std::ostream& output, const Quat& vec);
@@ -286,10 +319,10 @@ class SG_EXPORT Quat
inline std::ostream& operator << (std::ostream& output, const Quat& vec)
{
output << vec._fv[0] << " "
<< vec._fv[1] << " "
<< vec._fv[2] << " "
<< vec._fv[3];
output << vec._v[0] << " "
<< vec._v[1] << " "
<< vec._v[2] << " "
<< vec._v[3];
return output; // to enable cascading
}

View File

@@ -88,10 +88,7 @@ class OSGDB_EXPORT Field
bool isFloat() const;
bool matchFloat(float f) const;
bool getFloat(float& f) const;
bool isDouble() const;
bool matchDouble(double f) const;
bool getDouble(double& d) const;
bool getFloat(double& f) const;
static FieldType calculateFieldType(const char* str,bool withinQuotes=false);