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

@@ -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;
}