From acbad2424e87698a784c791d1f96d87010e1c03e Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 16 Sep 2014 17:40:13 +0000 Subject: [PATCH] Changed osgvolume example to use the new tf plugin rather than having local code for reading transfer function git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14450 16af8721-9629-0410-8352-f15c8da7e697 --- examples/osgvolume/osgvolume.cpp | 44 +-------- src/osgPlugins/p3d/ReaderWriterP3D.cpp | 65 +----------- src/osgPlugins/tf/ReaderWriterTF.cpp | 132 ++++++++++++------------- 3 files changed, 68 insertions(+), 173 deletions(-) diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index ca4d05342..47dade211 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -301,44 +301,6 @@ osg::Image* readRaw(int sizeX, int sizeY, int sizeZ, int numberBytesPerComponent } - -osg::TransferFunction1D* readTransferFunctionFile(const std::string& filename, float colorScale=1.0f) -{ - std::string foundFile = osgDB::findDataFile(filename); - if (foundFile.empty()) - { - std::cout<<"Error: could not find transfer function file : "<> value >> red >> green >> blue >> alpha; - if (fin) - { - std::cout<<"value = "<assign(colorMap); - - return tf; -} - - class TestSupportOperation: public osg::GraphicsOperation { public: @@ -512,11 +474,7 @@ int main( int argc, char **argv ) std::string tranferFunctionFile; while (arguments.read("--tf",tranferFunctionFile)) { - transferFunction = readTransferFunctionFile(tranferFunctionFile); - } - while (arguments.read("--tf-255",tranferFunctionFile)) - { - transferFunction = readTransferFunctionFile(tranferFunctionFile,1.0f/255.0f); + transferFunction = osgDB::readFile(tranferFunctionFile); } while(arguments.read("--test")) diff --git a/src/osgPlugins/p3d/ReaderWriterP3D.cpp b/src/osgPlugins/p3d/ReaderWriterP3D.cpp index 4a07188bf..ad6420deb 100644 --- a/src/osgPlugins/p3d/ReaderWriterP3D.cpp +++ b/src/osgPlugins/p3d/ReaderWriterP3D.cpp @@ -178,10 +178,8 @@ public: void parseModel(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; void parseModelScript(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; - osg::TransferFunction1D* readTransferFunctionFile(const std::string& filename, float scale) const; void parseVolume(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; - void parseStereoPair(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; void parseTimeout(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; @@ -1348,65 +1346,6 @@ void ReaderWriterP3DXML::parseModelScript(osgPresentation::SlideShowConstructor& } } - - -osg::TransferFunction1D* ReaderWriterP3DXML::readTransferFunctionFile(const std::string& filename, float scale) const -{ - std::string foundFile = osgDB::findDataFile(filename); - if (foundFile.empty()) - { - OSG_NOTICE<<"Error: could not find transfer function file : "<> value >> red >> green >> blue >> alpha; - - *readline = 0; - str.getline(readline, sizeof(readline)); - - char* comment = readline; - while(*comment==' ' || *comment=='\t' ) ++comment; - - if (*comment!=0) - { - OSG_NOTICE<<"value = "<assign(colorMap); - - return tf; -} - - void ReaderWriterP3DXML::parseVolume(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const { @@ -1499,12 +1438,12 @@ void ReaderWriterP3DXML::parseVolume(osgPresentation::SlideShowConstructor& cons std::string transferFunctionFile; if (getTrimmedProperty(cur, "tf", transferFunctionFile)) { - volumeData.transferFunction = readTransferFunctionFile(transferFunctionFile, 1.0); + volumeData.transferFunction = osgDB::readFile(transferFunctionFile); } if (getTrimmedProperty(cur, "tf-255", transferFunctionFile)) { - volumeData.transferFunction = readTransferFunctionFile(transferFunctionFile, 1.0/255.0); + volumeData.transferFunction = osgDB::readFile(transferFunctionFile); } if (getProperty(cur, "options", volumeData.options)) {} diff --git a/src/osgPlugins/tf/ReaderWriterTF.cpp b/src/osgPlugins/tf/ReaderWriterTF.cpp index 17df7cfea..7146de796 100644 --- a/src/osgPlugins/tf/ReaderWriterTF.cpp +++ b/src/osgPlugins/tf/ReaderWriterTF.cpp @@ -26,17 +26,66 @@ class ReaderWriterTF : public osgDB::ReaderWriter virtual const char* className() const { return "TransferFunction Reader/Writer"; } - ReadResult readTransferFunction(std::istream& fin, float colorScale) const + ReadResult readTransferFunction(std::istream& fin) const { osg::TransferFunction1D::ColorMap colorMap; + + float colorScale = 1.0f/255.0f; + while(fin) { - float value, red, green, blue, alpha; - fin >> value >> red >> green >> blue >> alpha; - if (fin) + char readline[4096]; + *readline = 0; + fin.getline(readline, sizeof(readline)); + + if (*readline==0) continue; + + if (*readline=='#') { - std::cout<<"value = "<> value; + + if (value=="colour-scale" || value=="color-scale") + { + std::string scaleStr; + str >> scaleStr; + + OSG_NOTICE<<"color-scale = ["<> red >> green >> blue >> alpha; + + *readline = 0; + str.getline(readline, sizeof(readline)); + + char* comment = readline; + while(*comment==' ' || *comment=='\t') ++comment; + + if (*comment!=0) + { + OSG_NOTICE<<"value = "<second; - if (c.r()>maxValue) maxValue = c.r(); - if (c.g()>maxValue) maxValue = c.g(); - if (c.b()>maxValue) maxValue = c.b(); - if (c.a()>maxValue) maxValue = c.a(); - } - - if (maxValue>=2.0f) - { - colorScale = 1.0f/255.0f; - } - } - - if (colorScale!=0.0) + if (colorScale!=1.0) { OSG_NOTICE<<"Rescaling ColorMap by "<getOptionString().find("tf-255")!=std::string::npos) - { - colorScale = 1.0/255.0f; - return true; - } - else - { - return false; - } - - } - - virtual ReadResult readObject(std::istream& fin,const osgDB::ReaderWriter::Options* options =NULL) const - { - float colorScale = 0.0f; // default auto-detect scale - - readColorScale(options, colorScale); - - return readTransferFunction(fin, colorScale); + return readTransferFunction(fin); } virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const @@ -119,22 +130,16 @@ class ReaderWriterTF : public osgDB::ReaderWriter std::string fileName = osgDB::findDataFile( file, options ); if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; - float colorScale = 0.0f; // default auto-detect scale - if (ext=="tf-255") colorScale = 1.0f/255.0f; - - readColorScale(options, colorScale); - osgDB::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary); - if (fin) return readTransferFunction( fin, colorScale); + if (fin) return readTransferFunction( fin); else return ReadResult::ERROR_IN_READING_FILE; } - virtual WriteResult writeTransferFunction(const osg::TransferFunction1D* tf, std::ostream& fout, float colorScale) const + virtual WriteResult writeTransferFunction(const osg::TransferFunction1D* tf, std::ostream& fout) const { - if (colorScale == 0.0) colorScale = 1.0f; - const osg::TransferFunction1D::ColorMap& cm = tf->getColorMap(); + float colorScale = 255.0f; for(osg::TransferFunction1D::ColorMap::const_iterator itr = cm.begin(); itr != cm.end(); ++itr) @@ -147,18 +152,15 @@ class ReaderWriterTF : public osgDB::ReaderWriter } - virtual WriteResult writeObject(const osg::Object& object, std::ostream& fout, const osgDB::ReaderWriter::Options* options) const + virtual WriteResult writeObject(const osg::Object& object, std::ostream& fout, const osgDB::ReaderWriter::Options*) const { const osg::TransferFunction1D* tf = dynamic_cast(&object); if (!tf) return WriteResult::FILE_NOT_HANDLED; - float colorScale = 0.0f; // default auto-detect scale - readColorScale(options, colorScale); - - return writeTransferFunction(tf, fout, colorScale); + return writeTransferFunction(tf, fout); } - virtual WriteResult writeObject(const osg::Object& object, const std::string& fileName, const osgDB::ReaderWriter::Options* options) const + virtual WriteResult writeObject(const osg::Object& object, const std::string& fileName, const osgDB::ReaderWriter::Options*) const { OSG_NOTICE<<"ReaderWriterTF::writeObject"<