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
This commit is contained in:
Robert Osfield
2014-09-16 17:40:13 +00:00
parent f3ba656303
commit acbad2424e
3 changed files with 68 additions and 173 deletions

View File

@@ -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 : "<<filename<<std::endl;
return 0;
}
std::cout<<"Reading transfer function "<<filename<<std::endl;
osg::TransferFunction1D::ColorMap colorMap;
osgDB::ifstream fin(foundFile.c_str());
while(fin)
{
float value, red, green, blue, alpha;
fin >> value >> red >> green >> blue >> alpha;
if (fin)
{
std::cout<<"value = "<<value<<" ("<<red<<", "<<green<<", "<<blue<<", "<<alpha<<")"<<std::endl;
colorMap[value] = osg::Vec4(red*colorScale,green*colorScale,blue*colorScale,alpha*colorScale);
}
}
if (colorMap.empty())
{
std::cout<<"Error: No values read from transfer function file: "<<filename<<std::endl;
return 0;
}
osg::TransferFunction1D* tf = new osg::TransferFunction1D;
tf->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<osg::TransferFunction1D>(tranferFunctionFile);
}
while(arguments.read("--test"))

View File

@@ -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 : "<<filename<<std::endl;
return 0;
}
OSG_NOTICE<<"Reading transfer function "<<filename<<std::endl;
osg::TransferFunction1D::ColorMap colorMap;
osgDB::ifstream fin(foundFile.c_str());
while(fin)
{
char readline[4096];
*readline = 0;
fin.getline(readline, sizeof(readline));
if (*readline!=0)
{
std::stringstream str(readline);
float value, red, green, blue, alpha;
str >> 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 = "<<value<<" ("<<red<<", "<<green<<", "<<blue<<", "<<alpha<<") comment = ["<<comment<<"]"<<std::endl;
}
else
{
OSG_NOTICE<<"value = "<<value<<" ("<<red<<", "<<green<<", "<<blue<<", "<<alpha<<")"<<std::endl;
}
colorMap[value] = osg::Vec4(red*scale,green*scale,blue*scale,alpha*scale);
}
}
if (colorMap.empty())
{
OSG_NOTICE<<"Error: No values read from transfer function file: "<<filename<<std::endl;
return 0;
}
osg::TransferFunction1D* tf = new osg::TransferFunction1D;
tf->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<osg::TransferFunction1D>(transferFunctionFile);
}
if (getTrimmedProperty(cur, "tf-255", transferFunctionFile))
{
volumeData.transferFunction = readTransferFunctionFile(transferFunctionFile, 1.0/255.0);
volumeData.transferFunction = osgDB::readFile<osg::TransferFunction1D>(transferFunctionFile);
}
if (getProperty(cur, "options", volumeData.options)) {}

View File

@@ -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<<" ("<<red<<", "<<green<<", "<<blue<<", "<<alpha<<")"<<std::endl;
colorMap[value] = osg::Vec4(red,green,blue,alpha);
OSG_NOTICE<<"comment = ["<<readline<<"]"<<std::endl;
}
else
{
std::stringstream str(readline);
std::string value;
str >> value;
if (value=="colour-scale" || value=="color-scale")
{
std::string scaleStr;
str >> scaleStr;
OSG_NOTICE<<"color-scale = ["<<scaleStr<<"]"<<std::endl;
if (!scaleStr.empty())
{
colorScale = 1.0f/osg::asciiToFloat(scaleStr.c_str());
}
}
else
{
std::string red, green, blue, alpha;
str >> 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 = "<<value<<" ("<<red<<", "<<green<<", "<<blue<<", "<<alpha<<") comment = ["<<comment<<"]"<<std::endl;
}
else
{
OSG_NOTICE<<"value = "<<value<<" ("<<red<<", "<<green<<", "<<blue<<", "<<alpha<<")"<<std::endl;
}
colorMap[osg::asciiToFloat(value.c_str())] = osg::Vec4(osg::asciiToFloat(red.c_str()),osg::asciiToFloat(green.c_str()),osg::asciiToFloat(blue.c_str()),osg::asciiToFloat(alpha.c_str()));
}
}
}
@@ -47,27 +96,7 @@ class ReaderWriterTF : public osgDB::ReaderWriter
// colorMap[value] = osg::Vec4(red*colorScale,green*colorScale,blue*colorScale,alpha*colorScale);
if (colorScale==0.0f)
{
float maxValue = 0.0f;
for(osg::TransferFunction1D::ColorMap::iterator itr = colorMap.begin();
itr != colorMap.end();
++itr)
{
const osg::Vec4& c = itr->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 "<<colorScale<<std::endl;
for(osg::TransferFunction1D::ColorMap::iterator itr = colorMap.begin();
@@ -88,27 +117,9 @@ class ReaderWriterTF : public osgDB::ReaderWriter
return tf;
}
bool readColorScale(const osgDB::ReaderWriter::Options* options, float& colorScale) const
virtual ReadResult readObject(std::istream& fin,const osgDB::ReaderWriter::Options*) const
{
if (options && options->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<const osg::TransferFunction1D*>(&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"<<fileName<<std::endl;
@@ -168,14 +170,10 @@ class ReaderWriterTF : public osgDB::ReaderWriter
std::string ext = osgDB::getFileExtension(fileName);
if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
float colorScale = 0.0f; // default auto-detect scale
if (ext=="tf-255") colorScale = 1.0f/255.0f;
readColorScale(options, colorScale);
osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary);
if(!fout) return WriteResult::ERROR_IN_WRITING_FILE;
return writeTransferFunction(tf, fout, colorScale);
return writeTransferFunction(tf, fout);
}
};