From 1729648038a1573e78a48d72da7f14eee7313997 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 8 May 2009 08:34:32 +0000 Subject: [PATCH] Added support for pushing handling relative paths set internally in the presentation file. Added setting of env vars defined in presentation file. --- src/osgPlugins/p3d/ReaderWriterP3D.cpp | 66 +++++++++++++++++++-- src/osgPlugins/p3d/SlideShowConstructor.cpp | 27 +++++---- src/osgPlugins/p3d/SlideShowConstructor.h | 4 +- 3 files changed, 78 insertions(+), 19 deletions(-) diff --git a/src/osgPlugins/p3d/ReaderWriterP3D.cpp b/src/osgPlugins/p3d/ReaderWriterP3D.cpp index 11e16acf2..52f1a130b 100644 --- a/src/osgPlugins/p3d/ReaderWriterP3D.cpp +++ b/src/osgPlugins/p3d/ReaderWriterP3D.cpp @@ -113,7 +113,7 @@ public: virtual ReadResult readNode(std::istream& fin, const Options* options) const; - ReadResult readNode(osgDB::XmlNode::Input& input, const osgDB::ReaderWriter::Options* options) const; + ReadResult readNode(osgDB::XmlNode::Input& input, osgDB::ReaderWriter::Options* options) const; void parseModel(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode*cur) const; @@ -1394,11 +1394,15 @@ osgDB::ReaderWriter::ReadResult ReaderWriterP3DXML::readNode(const std::string& std::string fileName = osgDB::findDataFile( file ); if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; + // code for setting up the database path so that internally referenced file are searched for on relative paths. + osg::ref_ptr local_opt = options ? static_cast(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; + local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); + osgDB::XmlNode::Input input; input.open(fileName); input.readAllDataIntoBuffer(); - return readNode(input, options); + return readNode(input, local_opt); } osgDB::ReaderWriter::ReadResult ReaderWriterP3DXML::readNode(std::istream& fin, const Options* options) const @@ -1407,10 +1411,12 @@ osgDB::ReaderWriter::ReadResult ReaderWriterP3DXML::readNode(std::istream& fin, input.attach(fin); input.readAllDataIntoBuffer(); - return readNode(input, options); + osg::ref_ptr local_opt = options ? static_cast(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; + + return readNode(input, local_opt); } -osgDB::ReaderWriter::ReadResult ReaderWriterP3DXML::readNode(osgDB::XmlNode::Input& input, const osgDB::ReaderWriter::Options* options) const +osgDB::ReaderWriter::ReadResult ReaderWriterP3DXML::readNode(osgDB::XmlNode::Input& input, osgDB::ReaderWriter::Options* options) const { bool readOnlyHoldingPage = options ? options->getOptionString()=="holding_slide" : false; @@ -1450,12 +1456,62 @@ osgDB::ReaderWriter::ReadResult ReaderWriterP3DXML::readNode(osgDB::XmlNode::Inp return ReadResult::FILE_NOT_HANDLED; } - osgPresentation::SlideShowConstructor constructor; + osgPresentation::SlideShowConstructor constructor(options); osgDB::FilePathList previousPaths = osgDB::getDataFilePathList(); bool readSlide = false; + std::string pathToPresentation; + if (options && !(options->getDatabasePathList().empty())) + { + pathToPresentation = options->getDatabasePathList().front(); + } + + for(osgDB::XmlNode::Children::iterator itr = root->children.begin(); + itr != root->children.end(); + ++itr) + { + osgDB::XmlNode* cur = itr->get(); + + if (cur->name=="env") + { + char* str = strdup(cur->contents.c_str()); + osg::notify(osg::INFO)<<"putenv("<children.begin(); + itr != root->children.end(); + ++itr) + { + osgDB::XmlNode* cur = itr->get(); + + if (cur->name == "path") + { + std::string newpath = expandEnvVarsInFileName(cur->contents); + + // now check if an absolue or http path + std::string::size_type colonPos = newpath.find_first_of(':'); + std::string::size_type backslashPos = newpath.find_first_of('/'); + std::string::size_type forwardslashPos = newpath.find_first_of('\\'); + bool relativePath = colonPos == std::string::npos && + backslashPos != 0 && + forwardslashPos != 0; + if (relativePath) + { + newpath = osgDB::concatPaths(pathToPresentation, newpath); + osg::notify(osg::NOTICE)<<"relative path = "<contents<<", newpath="< image; if (fileType == osgDB::DIRECTORY) { - image = osgDB::readImageFile(foundFile+".dicom"); + image = osgDB::readImageFile(foundFile+".dicom", _options.get()); } else if (fileType == osgDB::REGULAR_FILE) { - image = osgDB::readImageFile( foundFile ); + image = osgDB::readImageFile( foundFile, _options.get() ); } if (!image) return; @@ -1561,7 +1562,7 @@ osg::Node* SlideShowConstructor::attachMaterialAnimation(osg::Node* model, const if (!positionData.animation_material_filename.empty()) { - std::string absolute_animation_file_path = osgDB::findDataFile(positionData.animation_material_filename); + std::string absolute_animation_file_path = osgDB::findDataFile(positionData.animation_material_filename, _options.get()); if (!absolute_animation_file_path.empty()) { std::ifstream animation_filestream(absolute_animation_file_path.c_str()); @@ -1741,7 +1742,7 @@ osg::AnimationPathCallback* SlideShowConstructor::getAnimationPathCallback(const { if (!positionData.path.empty()) { - std::string absolute_animation_file_path = osgDB::findDataFile(positionData.path); + std::string absolute_animation_file_path = osgDB::findDataFile(positionData.path, _options.get()); if (!absolute_animation_file_path.empty()) { @@ -1773,7 +1774,7 @@ osg::AnimationPathCallback* SlideShowConstructor::getAnimationPathCallback(const fr.attach(&animation_filestream); static osg::ref_ptr s_path = new osg::AnimationPath; - osg::ref_ptr object = osgDB::readObjectFile(absolute_animation_file_path); // fr.readObjectOfType(*s_path); + osg::ref_ptr object = osgDB::readObjectFile(absolute_animation_file_path, _options.get()); // fr.readObjectOfType(*s_path); object = fr.readObject(); // fr.readObjectOfType(*s_path); if (object.valid()) { diff --git a/src/osgPlugins/p3d/SlideShowConstructor.h b/src/osgPlugins/p3d/SlideShowConstructor.h index 4f60b4545..b010387e8 100644 --- a/src/osgPlugins/p3d/SlideShowConstructor.h +++ b/src/osgPlugins/p3d/SlideShowConstructor.h @@ -227,7 +227,7 @@ public: }; - SlideShowConstructor(); + SlideShowConstructor(const osgDB::ReaderWriter::Options* options); void createPresentation(); @@ -361,6 +361,8 @@ protected: return stateset; } + osg::ref_ptr _options; + osg::Vec3 _slideOrigin; osg::Vec3 _eyeOrigin; float _slideWidth;