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);

View File

@ -49,31 +49,28 @@ void Quat::get(Matrixd& matrix) const
/// Set the elements of the Quat to represent a rotation of angle
/// (radians) around the axis (x,y,z)
void Quat::makeRotate( float angle,
float x,
float y,
float z )
void Quat::makeRotate( value_type angle, value_type x, value_type y, value_type z )
{
float inversenorm = 1.0/sqrt( x*x + y*y + z*z );
float coshalfangle = cos( 0.5*angle );
float sinhalfangle = sin( 0.5*angle );
value_type inversenorm = 1.0/sqrt( x*x + y*y + z*z );
value_type coshalfangle = cos( 0.5*angle );
value_type sinhalfangle = sin( 0.5*angle );
_fv[0] = x * sinhalfangle * inversenorm;
_fv[1] = y * sinhalfangle * inversenorm;
_fv[2] = z * sinhalfangle * inversenorm;
_fv[3] = coshalfangle;
_v[0] = x * sinhalfangle * inversenorm;
_v[1] = y * sinhalfangle * inversenorm;
_v[2] = z * sinhalfangle * inversenorm;
_v[3] = coshalfangle;
}
void Quat::makeRotate( float angle, const Vec3& vec )
void Quat::makeRotate( value_type angle, const Vec3& vec )
{
makeRotate( angle, vec[0], vec[1], vec[2] );
}
void Quat::makeRotate ( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3)
void Quat::makeRotate ( value_type angle1, const Vec3& axis1,
value_type angle2, const Vec3& axis2,
value_type angle3, const Vec3& axis3)
{
Quat q1; q1.makeRotate(angle1,axis1);
Quat q2; q2.makeRotate(angle2,axis2);
@ -89,13 +86,13 @@ void Quat::makeRotate ( float angle1, const Vec3& axis1,
// are co-incident or opposite in direction.
void Quat::makeRotate( const Vec3& from, const Vec3& to )
{
const float epsilon = 0.00001f;
const value_type epsilon = 0.00001;
float length1 = from.length();
float length2 = to.length();
value_type length1 = from.length();
value_type length2 = to.length();
// dot product vec1*vec2
float cosangle = from*to/(length1*length2);
value_type cosangle = from*to/(length1*length2);
if ( fabs(cosangle - 1) < epsilon )
{
@ -120,10 +117,10 @@ void Quat::makeRotate( const Vec3& from, const Vec3& to )
Vec3 axis(from^tmp);
axis.normalize();
_fv[0] = axis[0]; // sin of half angle of PI is 1.0.
_fv[1] = axis[1]; // sin of half angle of PI is 1.0.
_fv[2] = axis[2]; // sin of half angle of PI is 1.0.
_fv[3] = 0; // cos of half angle of PI is zero.
_v[0] = axis[0]; // sin of half angle of PI is 1.0.
_v[1] = axis[1]; // sin of half angle of PI is 1.0.
_v[2] = axis[2]; // sin of half angle of PI is 1.0.
_v[3] = 0; // cos of half angle of PI is zero.
}
else
@ -131,37 +128,41 @@ void Quat::makeRotate( const Vec3& from, const Vec3& to )
// This is the usual situation - take a cross-product of vec1 and vec2
// and that is the axis around which to rotate.
Vec3 axis(from^to);
float angle = acos( cosangle );
value_type angle = acos( cosangle );
makeRotate( angle, axis );
}
}
void Quat::getRotate( float& angle, Vec3& vec ) const
void Quat::getRotate( value_type& angle, Vec3& vec ) const
{
getRotate(angle,vec[0],vec[1],vec[2]);
value_type x,y,z;
getRotate(angle,x,y,z);
vec[0]=x;
vec[1]=y;
vec[2]=z;
}
// Get the angle of rotation and axis of this Quat object.
// Won't give very meaningful results if the Quat is not associated
// with a rotation!
void Quat::getRotate( float& angle, float& x, float& y, float& z ) const
void Quat::getRotate( value_type& angle, value_type& x, value_type& y, value_type& z ) const
{
float sinhalfangle = sqrt( _fv[0]*_fv[0] + _fv[1]*_fv[1] + _fv[2]*_fv[2] );
value_type sinhalfangle = sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
angle = 2 * atan2( sinhalfangle, _fv[3] );
angle = 2.0 * atan2( sinhalfangle, _v[3] );
if(sinhalfangle)
{
x = _fv[0] / sinhalfangle;
y = _fv[1] / sinhalfangle;
z = _fv[2] / sinhalfangle;
x = _v[0] / sinhalfangle;
y = _v[1] / sinhalfangle;
z = _v[2] / sinhalfangle;
}
else
{
x = 0.0f;
y = 0.0f;
z = 1.0f;
x = 0.0;
y = 0.0;
z = 1.0;
}
}
@ -172,7 +173,7 @@ void Quat::getRotate( float& angle, float& x, float& y, float& z ) const
/// Reference: Shoemake at SIGGRAPH 89
/// See also
/// http://www.gamasutra.com/features/programming/19980703/quaternions_01.htm
void Quat::slerp( float t, const Quat& from, const Quat& to )
void Quat::slerp( value_type t, const Quat& from, const Quat& to )
{
const double epsilon = 0.00001;
double omega, cosomega, sinomega, scale_from, scale_to ;
@ -185,7 +186,7 @@ void Quat::slerp( float t, const Quat& from, const Quat& to )
if ( cosomega <0.0 )
{
cosomega = -cosomega;
quatTo.set(-to._fv);
quatTo = -to;
}
if( (1.0 - cosomega) > epsilon )
@ -207,21 +208,20 @@ void Quat::slerp( float t, const Quat& from, const Quat& to )
scale_to = t ;
}
// use Vec4 arithmetic
_fv = (from._fv*scale_from) + (quatTo._fv*scale_to);
*this = (from*scale_from) + (quatTo*scale_to);
// so that we get a Vec4
}
#define QX _fv[0]
#define QY _fv[1]
#define QZ _fv[2]
#define QW _fv[3]
#define QX _v[0]
#define QY _v[1]
#define QZ _v[2]
#define QW _v[3]
#ifdef OSG_USE_UNIT_TESTS
void test_Quat_Eueler(float heading,float pitch,float roll)
void test_Quat_Eueler(value_type heading,value_type pitch,value_type roll)
{
osg::Quat q;
q.makeRotate(heading,pitch,roll);

View File

@ -360,34 +360,12 @@ bool Field::getFloat(float& f) const
}
}
bool Field::isDouble() const
{
getFieldType();
return _fieldType==REAL || _fieldType==INTEGER;
}
bool Field::matchDouble(double d) const
bool Field::getFloat(double& f) const
{
getFieldType();
if (_fieldType==REAL || _fieldType==INTEGER)
{
return atof(_fieldCache)==d;
}
else
{
return false;
}
}
bool Field::getDouble(double& d) const
{
getFieldType();
if (_fieldType==REAL || _fieldType==INTEGER)
{
d = atof(_fieldCache);
f = (float)atof(_fieldCache);
return true;
}
else

View File

@ -46,10 +46,10 @@ bool ClipPlane_readLocalData(Object& obj, Input& fr)
if (fr.matchSequence("plane %f %f %f %f"))
{
double plane[4];
fr[1].getDouble(plane[0]);
fr[2].getDouble(plane[1]);
fr[3].getDouble(plane[2]);
fr[4].getDouble(plane[3]);
fr[1].getFloat(plane[0]);
fr[2].getFloat(plane[1]);
fr[3].getFloat(plane[2]);
fr[4].getFloat(plane[3]);
clipplane.setClipPlane(plane);
fr+=5;

View File

@ -48,7 +48,7 @@ bool DOFTransform_readLocalData(Object& obj, Input& fr)
{
for(int j=0;j<4;++j)
{
fr[k].getDouble(v);
fr[k].getFloat(v);
matrix(i,j)=v;
k++;
}

View File

@ -57,7 +57,7 @@ bool Depth_readLocalData(Object& obj, Input& fr)
}
double znear,zfar;
if (fr[0].matchWord("range") && fr[1].getDouble(znear) && fr[2].getDouble(zfar))
if (fr[0].matchWord("range") && fr[1].getFloat(znear) && fr[2].getFloat(zfar))
{
depth.setRange(znear,zfar);
fr+=2;

View File

@ -16,7 +16,7 @@ bool readMatrix(osg::Matrix& matrix, osgDB::Input& fr)
double v;
while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
{
if (fr[0].getDouble(v))
if (fr[0].getFloat(v))
{
matrix(row,col)=v;
++col;

View File

@ -44,7 +44,7 @@ bool TexMat_readLocalData(Object& obj, Input& fr)
{
for(int j=0;j<4;++j)
{
fr[k].getDouble(v);
fr[k].getFloat(v);
matrix(i,j)=v;
k++;
}

View File

@ -31,7 +31,7 @@ bool BlinkSequence_readLocalData(osg::Object &obj, osgDB::Input &fr)
if (fr.matchSequence("phaseShift %f"))
{
double ps;
fr[1].getDouble(ps);
fr[1].getFloat(ps);
fr += 2;
seq.setPhaseShift(ps);
iteratorAdvanced = true;
@ -40,7 +40,7 @@ bool BlinkSequence_readLocalData(osg::Object &obj, osgDB::Input &fr)
{
double length;
float r, g, b, a;
fr[1].getDouble(length);
fr[1].getFloat(length);
fr[2].getFloat(r);
fr[3].getFloat(g);
fr[4].getFloat(b);
@ -103,7 +103,7 @@ bool BlinkSequence_SequenceGroup_readLocalData(osg::Object &obj, osgDB::Input &f
if (fr.matchSequence("baseTime %f"))
{
fr[1].getDouble(sg._baseTime);
fr[1].getFloat(sg._baseTime);
fr += 2;
iteratorAdvanced = true;
}