From cb8025d9137104db86a363efcf8d6eb1f114ef81 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 15 Dec 2001 16:56:39 +0000 Subject: [PATCH] Renamed osg::Matric::makeIdent() to osg::Matrix::makeIdentity() to make it consistent with the rest of the osg::Matrix naming. Updated OSG distribution to account for new name. Added support for the STATIC/DYNAMIC osg::Transform::Type to the .osg ASCII reader/writer plugin and the flt reader plugin. Removed the non cost version of osg::Transform::getMatrix() as this could by pass the dirty mechinism. --- include/osg/Matrix | 82 +++++++++++----------- include/osg/Transform | 2 +- include/osgUtil/CullVisitor | 2 +- src/Demos/osgcluster/osgcluster.cpp | 2 +- src/Demos/osgconv/OrientationConverter.cpp | 6 +- src/osg/Billboard.cpp | 4 +- src/osg/Camera.cpp | 2 +- src/osg/Matrix.cpp | 2 +- src/osg/Transform.cpp | 2 +- src/osgPlugins/flt/FltFile.cpp | 4 +- src/osgPlugins/flt/flt2osg.cpp | 13 ++-- src/osgPlugins/osg/Transform.cpp | 22 ++++++ src/osgUtil/Optimizer.cpp | 2 +- 13 files changed, 85 insertions(+), 60 deletions(-) diff --git a/include/osg/Matrix b/include/osg/Matrix index 10ac4e86d..5732b193f 100644 --- a/include/osg/Matrix +++ b/include/osg/Matrix @@ -18,10 +18,6 @@ class Quat; class SG_EXPORT Matrix : public Object { - private: - float _mat[4][4]; - bool fully_realized; - public: META_Object(Matrix); @@ -56,18 +52,16 @@ class SG_EXPORT Matrix : public Object float * ptr() { ensureRealized(); return (float *)_mat; } const float * ptr() const { ensureRealized(); return (const float *)_mat; } - inline void ensureRealized() const { if (!fully_realized) const_cast(this)->makeIdent();} + inline void ensureRealized() const { if (!fully_realized) const_cast(this)->makeIdentity();} - void makeIdent(); + void makeIdentity(); + void makeScale( const Vec3& ); void makeScale( float, float, float ); void makeTranslate( const Vec3& ); void makeTranslate( float, float, float ); - //TODO: original preTrans was optimized (M=Tr*M) - // but also has the assumption that M (this) is an affine transformation Matrix - // can I still do something to optimize the same case now? - + void makeRotate( const Vec3& from, const Vec3& to ); void makeRotate( float angle, const Vec3& axis ); void makeRotate( float angle, float x, float y, float z ); @@ -77,7 +71,7 @@ class SG_EXPORT Matrix : public Object bool invert( const Matrix& ); bool invertAffine( const Matrix& ); - //basic utility functions to create new matrices or vectors + //basic utility functions to create new matrices inline static Matrix identity( void ); inline static Matrix scale( const Vec3& ); inline static Matrix scale( float, float, float ); @@ -110,51 +104,57 @@ class SG_EXPORT Matrix : public Object void preMult( const Matrix& ); void postMult( const Matrix& ); - // Helper class to optimize product expressions somewhat - class MatrixProduct { - public: - const Matrix& A; - const Matrix& B; - - MatrixProduct( const Matrix& lhs, const Matrix& rhs ) : A(lhs), B(rhs) {} - }; - - inline MatrixProduct operator * ( const Matrix& other ) const - { return MatrixProduct(*this, other); } - inline void operator *= ( const Matrix& other ) - { if( this == &other ) { - Matrix temp(other); - postMult( temp ); - } - else postMult( other ); - } - inline void operator = ( const MatrixProduct& p ) - { - if( this == &(p.A)) postMult(p.B); - else if( this == &(p.B)) preMult(p.A); - else mult( p.A, p.B ); + { if( this == &other ) { + Matrix temp(other); + postMult( temp ); } + else postMult( other ); + } - Matrix( const MatrixProduct& p ) //allows implicit evaluation of the product - { mult( p.A, p.B ); } - - - Matrix operator *( const Matrix &m ) + inline Matrix operator * ( const Matrix &m ) const { osg::Matrix r; r.mult(*this,m); return r; } - float * operator [] ( int i ) { return &_mat[i][0]; } + +// temporarily commented out while waiting for a more generic implementation +// of MatrixProduct proxy class. +// // Helper class to optimize product expressions somewhat +// class MatrixProduct { +// public: +// const Matrix& A; +// const Matrix& B; +// +// MatrixProduct( const Matrix& lhs, const Matrix& rhs ) : A(lhs), B(rhs) {} +// }; +// +// inline MatrixProduct operator * ( const Matrix& other ) const +// { return MatrixProduct(*this, other); } +// +// inline void operator = ( const MatrixProduct& p ) +// { +// if( this == &(p.A)) postMult(p.B); +// else if( this == &(p.B)) preMult(p.A); +// else mult( p.A, p.B ); +// } +// +// Matrix( const MatrixProduct& p ) //allows implicit evaluation of the product +// { mult( p.A, p.B ); } + + private: + float _mat[4][4]; + bool fully_realized; + }; //static utility methods inline Matrix Matrix::identity(void) { Matrix m; - m.makeIdent(); + m.makeIdentity(); return m; } diff --git a/include/osg/Transform b/include/osg/Transform index f5b81551b..bacee23c4 100644 --- a/include/osg/Transform +++ b/include/osg/Transform @@ -38,10 +38,10 @@ class SG_EXPORT Transform : public Group * Matrix explicity set the Matrix to STATIC. The default * value is DYNAMIC.*/ inline void setType(Type type) { _type = type; } + /** Get the Transform Type.*/ inline const Type getType() const { return _type; } - inline Matrix& getMatrix() { return *_matrix; } inline const Matrix& getMatrix() const { return *_matrix; } inline void setMatrix(const Matrix& mat ) diff --git a/include/osgUtil/CullVisitor b/include/osgUtil/CullVisitor index ae473da36..624681439 100644 --- a/include/osgUtil/CullVisitor +++ b/include/osgUtil/CullVisitor @@ -323,7 +323,7 @@ class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor if (_currentReuseMatrixIndex<_reuseMatrixList.size()) { osg::Matrix* matrix = _reuseMatrixList[_currentReuseMatrixIndex++].get(); - matrix->makeIdent(); + matrix->makeIdentity(); return matrix; } diff --git a/src/Demos/osgcluster/osgcluster.cpp b/src/Demos/osgcluster/osgcluster.cpp index 295414989..4c0b5fc6d 100644 --- a/src/Demos/osgcluster/osgcluster.cpp +++ b/src/Demos/osgcluster/osgcluster.cpp @@ -42,7 +42,7 @@ class CameraPacket { osg::Vec3 lv = _center-_eye; osg::Matrix matrix; - matrix.makeIdent(); + matrix.makeIdentity(); matrix.makeRotate(angle_offset,_up.x(),_up.y(),_up.z()); lv = lv*matrix; diff --git a/src/Demos/osgconv/OrientationConverter.cpp b/src/Demos/osgconv/OrientationConverter.cpp index 132c313ca..30ddd5815 100644 --- a/src/Demos/osgconv/OrientationConverter.cpp +++ b/src/Demos/osgconv/OrientationConverter.cpp @@ -11,10 +11,10 @@ using namespace osg; OrientationConverter::OrientationConverter( void ) { - R.makeIdent(); - T.makeIdent(); + R.makeIdentity(); + T.makeIdentity(); _trans_set = false; - S.makeIdent(); + S.makeIdentity(); } void OrientationConverter::setRotation( const Vec3 &from, const Vec3 &to ) diff --git a/src/osg/Billboard.cpp b/src/osg/Billboard.cpp index e36f51e76..9f6477e64 100644 --- a/src/osg/Billboard.cpp +++ b/src/osg/Billboard.cpp @@ -82,7 +82,7 @@ void Billboard::calcRotation(const Vec3& eye_local, const Vec3& pos_local,Matrix float ev_length = ev.length(); if (ev_length>0.0f) { - mat.makeIdent(); + mat.makeIdentity(); //float rotation_zrotation_z = atan2f(ev.x(),ev.y()); //mat.makeRotate(inRadians(rotation_z),0.0f,0.0f,1.0f); float inv = 1.0f/ev_length; @@ -134,7 +134,7 @@ void Billboard::calcRotation(const Vec3& eye_local, const Vec3& pos_local,Matrix void Billboard::calcTransform(const Vec3& eye_local, const Vec3& pos_local,Matrix& mat) const { // mat.makeTranslate(pos_local[0],pos_local[1],pos_local[2]); - // mat.makeIdent(); + // mat.makeIdentity(); calcRotation(eye_local,pos_local,mat); // mat.postTrans(pos_local[0],pos_local[1],pos_local[2]); diff --git a/src/osg/Camera.cpp b/src/osg/Camera.cpp index ca8d4959b..7cafe2348 100644 --- a/src/osg/Camera.cpp +++ b/src/osg/Camera.cpp @@ -657,7 +657,7 @@ void Camera::calculateMatricesAndClippingVolume() const else { _modelViewMatrix = new Matrix; - _modelViewMatrix->makeIdent(); + _modelViewMatrix->makeIdentity(); } break; case(USE_EYE_AND_QUATERNION): // not implemented yet, default to eye,center,up. diff --git a/src/osg/Matrix.cpp b/src/osg/Matrix.cpp index ea93ae9a2..c60279720 100644 --- a/src/osg/Matrix.cpp +++ b/src/osg/Matrix.cpp @@ -95,7 +95,7 @@ void Matrix::setTrans( const Vec3& v ) _mat[3][2] = v[2]; } -void Matrix::makeIdent() +void Matrix::makeIdentity() { SET_ROW(0, 1, 0, 0, 0 ) SET_ROW(1, 0, 1, 0, 0 ) diff --git a/src/osg/Transform.cpp b/src/osg/Transform.cpp index e5e6dcf3d..4a99a6ea8 100644 --- a/src/osg/Transform.cpp +++ b/src/osg/Transform.cpp @@ -6,7 +6,7 @@ Transform::Transform() { _type = DYNAMIC; _matrix = new osg::Matrix(); - _matrix->makeIdent(); + _matrix->makeIdentity(); } diff --git a/src/osgPlugins/flt/FltFile.cpp b/src/osgPlugins/flt/FltFile.cpp index cdc442dad..2068b15e1 100644 --- a/src/osgPlugins/flt/FltFile.cpp +++ b/src/osgPlugins/flt/FltFile.cpp @@ -113,8 +113,8 @@ bool FltFile::readFile(const std::string& fileName) // havn't found file, look in OSGFILEPATH char* newFileName = osgDB::findFile(fileName.c_str()); - if (!newFileName) return NULL; - if (!fin.open(newFileName)) return NULL; + if (!newFileName) return false; + if (!fin.open(newFileName)) return false; } osg::notify(osg::INFO) << "Loading " << fileName << " ... " << std::endl; diff --git a/src/osgPlugins/flt/flt2osg.cpp b/src/osgPlugins/flt/flt2osg.cpp index 27ec44f56..4afde2ac4 100644 --- a/src/osgPlugins/flt/flt2osg.cpp +++ b/src/osgPlugins/flt/flt2osg.cpp @@ -551,6 +551,7 @@ osg::Node* ConvertFromFLT::visitDOF(osg::Group* osgParent, DofRecord* rec) visitPrimaryNode(transform, (PrimNodeRecord*)rec); transform->setName(rec->getData()->szIdent); + transform->setType(osg::Transform::DYNAMIC); // note for Judd (and others) shouldn't there be code in here to set up the transform matrix? // as a transform with an identity matrix is effectively only a @@ -1088,8 +1089,8 @@ osg::Node* ConvertFromFLT::visitMatrix(osg::Group* osgParent, MatrixRecord* rec) { SMatrix* pSMatrix = (SMatrix*)rec->getData(); - osg::Transform* dcs = new osg::Transform; - if (dcs) + osg::Transform* transform = new osg::Transform; + if (transform) { osg::Matrix m; for(int i=0;i<4;++i) @@ -1107,9 +1108,11 @@ osg::Node* ConvertFromFLT::visitMatrix(osg::Group* osgParent, MatrixRecord* rec) pos *= (float)_unitScale; m *= osg::Matrix::translate(pos); - dcs->setMatrix(m); - osgParent->addChild(dcs); - return (osg::Node*)dcs; + transform->setType(osg::Transform::STATIC); + transform->setMatrix(m); + + osgParent->addChild(transform); + return (osg::Node*)transform; } return NULL; diff --git a/src/osgPlugins/osg/Transform.cpp b/src/osgPlugins/osg/Transform.cpp index e5ff9b7bb..7b7c2d1e6 100644 --- a/src/osgPlugins/osg/Transform.cpp +++ b/src/osgPlugins/osg/Transform.cpp @@ -39,6 +39,22 @@ bool Transform_readLocalData(Object& obj, Input& fr) Transform& transform = static_cast(obj); + + if (fr[0].matchWord("Type")) + { + if (fr[1].matchWord("DYNAMIC")) + { + transform.setType(osg::Transform::DYNAMIC); + fr +=2 ; + } + else if (fr[1].matchWord("STATIC")) + { + transform.setType(osg::Transform::STATIC); + fr +=2 ; + } + + } + static Matrix s_matrix; if (Matrix* tmpMatrix = static_cast(fr.readObjectOfType(s_matrix))) @@ -59,6 +75,12 @@ bool Transform_writeLocalData(const Object& obj, Output& fw) { const Transform& transform = static_cast(obj); + switch(transform.getType()) + { + case(osg::Transform::STATIC): fw.indent() << "Type STATIC" << std::endl; + default: fw.indent() << "Type DYNAMIC" << std::endl; + } + fw.writeObject(transform.getMatrix()); return true; diff --git a/src/osgUtil/Optimizer.cpp b/src/osgUtil/Optimizer.cpp index 7cecd20c1..12376a46c 100644 --- a/src/osgUtil/Optimizer.cpp +++ b/src/osgUtil/Optimizer.cpp @@ -367,7 +367,7 @@ void Optimizer::FlattenStaticTransformsVisitor::apply(osg::Transform& transform) _transformList.insert(&transform); // reset the matrix to identity. - transform.getMatrix().makeIdent(); + transform.setMatrix(osg::Matrix::identity()); transform.dirtyBound();