From fc3d4aa5f0d2ff9ccf6412cfb1dc02169e66862a Mon Sep 17 00:00:00 2001 From: Don BURNS Date: Thu, 12 Feb 2004 18:40:15 +0000 Subject: [PATCH] Submission by Paul Martz for doing units conversions on flight files. --- src/osgPlugins/flt/FltFile.cpp | 28 +++++ src/osgPlugins/flt/FltFile.h | 12 ++ src/osgPlugins/flt/ReaderWriterFLT.cpp | 15 +++ src/osgPlugins/flt/flt2osg.cpp | 153 ++++++++++++++++++++----- 4 files changed, 182 insertions(+), 26 deletions(-) diff --git a/src/osgPlugins/flt/FltFile.cpp b/src/osgPlugins/flt/FltFile.cpp index bfe052938..da2a87ea3 100644 --- a/src/osgPlugins/flt/FltFile.cpp +++ b/src/osgPlugins/flt/FltFile.cpp @@ -26,6 +26,7 @@ FltFile::FltFile( { _useTextureAlphaForTransparancyBinning = true; _doUnitsConversion = true; + _desiredUnits = ConvertToMeters; if (pColorPool) { @@ -251,3 +252,30 @@ int FltFile::getFlightVersion() const } return 0; } + + +std::string FltFile::getDesiredUnitsString() const +{ + switch (_desiredUnits) + { + case ConvertToMeters: + return "ConvertToMeters"; + break; + case ConvertToKilometers: + return "ConvertToKilometers"; + break; + case ConvertToFeet: + return "ConvertToFeet"; + break; + case ConvertToInches: + return "ConvertToInches"; + break; + case ConvertToNauticalMiles: + return "ConvertToNauticalMiles"; + break; + default: + return "Invalid"; + break; + } +} + diff --git a/src/osgPlugins/flt/FltFile.h b/src/osgPlugins/flt/FltFile.h index 136a0c23b..bc8cf6cf8 100644 --- a/src/osgPlugins/flt/FltFile.h +++ b/src/osgPlugins/flt/FltFile.h @@ -51,6 +51,17 @@ class FltFile : public osg::Referenced void setDoUnitsConversion(bool flag) { _doUnitsConversion=flag; } bool getDoUnitsConversion() const { return _doUnitsConversion; } + typedef enum { + ConvertToMeters, + ConvertToKilometers, + ConvertToFeet, + ConvertToInches, + ConvertToNauticalMiles + } ConvertUnits; + void setDesiredUnits( FltFile::ConvertUnits units ) { _desiredUnits=units; } + FltFile::ConvertUnits getDesiredUnits() const { return _desiredUnits; } + std::string getDesiredUnitsString() const; + int getFlightVersion() const; inline HeaderRecord* getHeaderRecord() { return _headerRecord.get(); } @@ -70,6 +81,7 @@ class FltFile : public osg::Referenced bool _useInternalMaterialPalette; bool _useTextureAlphaForTransparancyBinning; bool _doUnitsConversion; + ConvertUnits _desiredUnits; std::string _directory; diff --git a/src/osgPlugins/flt/ReaderWriterFLT.cpp b/src/osgPlugins/flt/ReaderWriterFLT.cpp index b2e6bb93a..474c8095f 100644 --- a/src/osgPlugins/flt/ReaderWriterFLT.cpp +++ b/src/osgPlugins/flt/ReaderWriterFLT.cpp @@ -39,6 +39,21 @@ osgDB::ReaderWriter::ReadResult ReaderWriterFLT::readNode(const std::string& fil osg::notify(osg::DEBUG_INFO) << "FltFile.getUseTextureAlphaForTransparancyBinning()=" << read->getUseTextureAlphaForTransparancyBinning() << std::endl; read->setDoUnitsConversion((options->getOptionString().find("noUnitsConversion")==std::string::npos)); // default to true, unless noUnitsConversion is specified.o osg::notify(osg::DEBUG_INFO) << "FltFile.getDoUnitsConversion()=" << read->getDoUnitsConversion() << std::endl; + + if (read->getDoUnitsConversion()) + { + if (options->getOptionString().find("convertToFeet")!=std::string::npos) + read->setDesiredUnits( FltFile::ConvertToFeet ); + else if (options->getOptionString().find("convertToInches")!=std::string::npos) + read->setDesiredUnits( FltFile::ConvertToInches ); + else if (options->getOptionString().find("convertToMeters")!=std::string::npos) + read->setDesiredUnits( FltFile::ConvertToMeters ); + else if (options->getOptionString().find("convertToKilometers")!=std::string::npos) + read->setDesiredUnits( FltFile::ConvertToKilometers ); + else if (options->getOptionString().find("convertToNauticalMiles")!=std::string::npos) + read->setDesiredUnits( FltFile::ConvertToNauticalMiles ); + osg::notify(osg::DEBUG_INFO) << "FltFile.getDesiredUnits()=" << read->getDesiredUnitsString() << std::endl; + } } osg::Node* node = read->readNode(fileName); diff --git a/src/osgPlugins/flt/flt2osg.cpp b/src/osgPlugins/flt/flt2osg.cpp index e8a5b52cf..38dd6dcac 100644 --- a/src/osgPlugins/flt/flt2osg.cpp +++ b/src/osgPlugins/flt/flt2osg.cpp @@ -169,10 +169,10 @@ osg::Group* ConvertFromFLT::visitInstanceReference(osg::Group& osgParent,Instanc osg::Group* ConvertFromFLT::visitAncillary(osg::Group& osgParent, osg::Group& osgPrimary, PrimNodeRecord* rec) { - // Note: There are databases that contains nodes with both Matrix and GeneralMatrix - // ancillary records. We need just one of these to put into the scenegraph - // Nick. - bool mxFound = false; + // Note: There are databases that contains nodes with both Matrix and GeneralMatrix + // ancillary records. We need just one of these to put into the scenegraph + // Nick. + bool mxFound = false; osg::Group* parent = &osgParent; // Visit ancillary records @@ -189,21 +189,21 @@ osg::Group* ConvertFromFLT::visitAncillary(osg::Group& osgParent, osg::Group& os break; case GENERAL_MATRIX_OP: - // Note: Ancillary record creates osg node - if (!mxFound) - { - parent = visitGeneralMatrix(*parent, osgPrimary, (GeneralMatrixRecord*)child); - mxFound = true; - } + // Note: Ancillary record creates osg node + if (!mxFound) + { + parent = visitGeneralMatrix(*parent, osgPrimary, (GeneralMatrixRecord*)child); + mxFound = true; + } break; case MATRIX_OP: // Note: Ancillary record creates osg node - if (!mxFound) - { - parent = visitMatrix(*parent, osgPrimary, (MatrixRecord*)child); - mxFound = true; - } + if (!mxFound) + { + parent = visitMatrix(*parent, osgPrimary, (MatrixRecord*)child); + mxFound = true; + } break; case COMMENT_OP: @@ -437,25 +437,126 @@ osg::Group* ConvertFromFLT::visitHeader(HeaderRecord* rec) // Unit scale if ( _doUnitsConversion ) { - switch (pSHeader->swVertexCoordUnit) + switch (rec->getFltFile()->getDesiredUnits()) { - case HeaderRecord::METERS: - _unitScale = 1.0; + case FltFile::ConvertToMeters: + switch (pSHeader->swVertexCoordUnit) + { + case HeaderRecord::METERS: + _unitScale = 1.0; + break; + case HeaderRecord::KILOMETERS: + _unitScale = 1000.0; + break; + case HeaderRecord::FEET: + _unitScale = 0.3048; + break; + case HeaderRecord::INCHES: + _unitScale = 0.02540; + break; + case HeaderRecord::NAUTICAL_MILES: + _unitScale = 1852.0; + break; + default: + _unitScale = 1.0; + } break; - case HeaderRecord::KILOMETERS: - _unitScale = 1000.0; + + case FltFile::ConvertToKilometers: + switch (pSHeader->swVertexCoordUnit) + { + case HeaderRecord::METERS: + _unitScale = 0.001; + break; + case HeaderRecord::KILOMETERS: + _unitScale = 1.0; + break; + case HeaderRecord::FEET: + _unitScale = 0.0003048; + break; + case HeaderRecord::INCHES: + _unitScale = 0.0000254; + break; + case HeaderRecord::NAUTICAL_MILES: + _unitScale = 1.852; + break; + default: + _unitScale = 1.0; + } break; - case HeaderRecord::FEET: - _unitScale = 0.3048; + + case FltFile::ConvertToFeet: + switch (pSHeader->swVertexCoordUnit) + { + case HeaderRecord::METERS: + _unitScale = 3.2808399; + break; + case HeaderRecord::KILOMETERS: + _unitScale = 3280.839895; + break; + case HeaderRecord::FEET: + _unitScale = 1.0; + break; + case HeaderRecord::INCHES: + _unitScale = 0.0833333; + break; + case HeaderRecord::NAUTICAL_MILES: + _unitScale = 6076.1154856 ; + break; + default: + _unitScale = 1.0; + } break; - case HeaderRecord::INCHES: - _unitScale = 0.02540; + + case FltFile::ConvertToInches: + switch (pSHeader->swVertexCoordUnit) + { + case HeaderRecord::METERS: + _unitScale = 39.3700787; + break; + case HeaderRecord::KILOMETERS: + _unitScale = 39370.0787402; + break; + case HeaderRecord::FEET: + _unitScale = 12.0; + break; + case HeaderRecord::INCHES: + _unitScale = 1.0; + break; + case HeaderRecord::NAUTICAL_MILES: + _unitScale = 72913.3858268; + break; + default: + _unitScale = 1.0; + } break; - case HeaderRecord::NAUTICAL_MILES: - _unitScale = 1852.0; + + case FltFile::ConvertToNauticalMiles: + switch (pSHeader->swVertexCoordUnit) + { + case HeaderRecord::METERS: + _unitScale = 0.0005399568; + break; + case HeaderRecord::KILOMETERS: + _unitScale = 0.5399568; + break; + case HeaderRecord::FEET: + _unitScale = 0.0001646; + break; + case HeaderRecord::INCHES: + _unitScale = 0.0000137; + break; + case HeaderRecord::NAUTICAL_MILES: + _unitScale = 1.0; + break; + default: + _unitScale = 1.0; + } break; + default: _unitScale = 1.0; + break; } } else