| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include "ReadShowFile.h" |
|---|
| 14 | #include "ShowEventHandler.h" |
|---|
| 15 | |
|---|
| 16 | #include <osgPresentation/SlideEventHandler> |
|---|
| 17 | |
|---|
| 18 | #include <osg/ImageStream> |
|---|
| 19 | #include <osg/Shape> |
|---|
| 20 | #include <osg/ShapeDrawable> |
|---|
| 21 | #include <osg/Switch> |
|---|
| 22 | |
|---|
| 23 | #include <osgDB/ReadFile> |
|---|
| 24 | #include <osgDB/FileNameUtils> |
|---|
| 25 | #include <osgDB/FileUtils> |
|---|
| 26 | |
|---|
| 27 | #include <osgVolume/VolumeTile> |
|---|
| 28 | |
|---|
| 29 | #include <osgDB/XmlParser> |
|---|
| 30 | |
|---|
| 31 | #include <string.h> |
|---|
| 32 | |
|---|
| 33 | class AddVolumeEditingCallbackVisitor : public osg::NodeVisitor |
|---|
| 34 | { |
|---|
| 35 | public: |
|---|
| 36 | AddVolumeEditingCallbackVisitor(): |
|---|
| 37 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {} |
|---|
| 38 | |
|---|
| 39 | void apply(osg::Group& group) |
|---|
| 40 | { |
|---|
| 41 | osgVolume::VolumeTile* volumeTile = dynamic_cast<osgVolume::VolumeTile*>(&group); |
|---|
| 42 | if (volumeTile) |
|---|
| 43 | { |
|---|
| 44 | volumeTile->addEventCallback(new osgVolume::PropertyAdjustmentCallback()); |
|---|
| 45 | } |
|---|
| 46 | else |
|---|
| 47 | { |
|---|
| 48 | traverse(group); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | bool p3d::getFileNames(osg::ArgumentParser& arguments, FileNameList& xmlFiles, FileNameList& normalFiles) |
|---|
| 55 | { |
|---|
| 56 | |
|---|
| 57 | for(int pos=1;pos<arguments.argc();++pos) |
|---|
| 58 | { |
|---|
| 59 | if (!arguments.isOption(pos)) |
|---|
| 60 | { |
|---|
| 61 | std::string ext = osgDB::getFileExtension(arguments[pos]); |
|---|
| 62 | if (osgDB::equalCaseInsensitive(ext,"xml") || osgDB::equalCaseInsensitive(ext,"p3d")) |
|---|
| 63 | { |
|---|
| 64 | xmlFiles.push_back(arguments[pos]); |
|---|
| 65 | } |
|---|
| 66 | else |
|---|
| 67 | { |
|---|
| 68 | normalFiles.push_back(arguments[pos]); |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | return (!xmlFiles.empty() || !normalFiles.empty()); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | bool p3d::readEnvVars(osg::ArgumentParser& arguments) |
|---|
| 76 | { |
|---|
| 77 | bool readVars = false; |
|---|
| 78 | |
|---|
| 79 | for(int i=1; i<arguments.argc(); ++i) |
|---|
| 80 | { |
|---|
| 81 | if (!arguments.isOption(i)) |
|---|
| 82 | { |
|---|
| 83 | std::string ext = osgDB::getLowerCaseFileExtension(arguments[i]); |
|---|
| 84 | if (ext=="xml" || ext=="p3d") |
|---|
| 85 | { |
|---|
| 86 | std::string file = osgDB::findDataFile(arguments[i]); |
|---|
| 87 | if (!file.empty()) |
|---|
| 88 | { |
|---|
| 89 | std::string path = osgDB::getFilePath(file); |
|---|
| 90 | if (!path.empty()) |
|---|
| 91 | { |
|---|
| 92 | osgDB::getDataFilePathList().push_front(path); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | if (p3d::readEnvVars(file)) readVars = true; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | return readVars; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | bool p3d::readEnvVars(const std::string& fileName) |
|---|
| 105 | { |
|---|
| 106 | std::string ext = osgDB::getFileExtension(fileName); |
|---|
| 107 | if (!osgDB::equalCaseInsensitive(ext,"xml") && |
|---|
| 108 | !osgDB::equalCaseInsensitive(ext,"p3d")) return false; |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | osg::ref_ptr<osgDB::XmlNode> doc = new osgDB::XmlNode; |
|---|
| 112 | osgDB::XmlNode* root = 0; |
|---|
| 113 | |
|---|
| 114 | osgDB::XmlNode::Input input; |
|---|
| 115 | input.open(fileName); |
|---|
| 116 | input.readAllDataIntoBuffer(); |
|---|
| 117 | |
|---|
| 118 | doc->read(input); |
|---|
| 119 | |
|---|
| 120 | if (doc == NULL ) |
|---|
| 121 | { |
|---|
| 122 | fprintf(stderr,"Document not parsed successfully. \n"); |
|---|
| 123 | return false; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | for(osgDB::XmlNode::Children::iterator itr = doc->children.begin(); |
|---|
| 127 | itr != doc->children.end() && !root; |
|---|
| 128 | ++itr) |
|---|
| 129 | { |
|---|
| 130 | if ((*itr)->name=="presentation") root = itr->get(); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if (root == NULL) |
|---|
| 134 | { |
|---|
| 135 | fprintf(stderr,"empty document\n"); |
|---|
| 136 | return false; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | if (root->name!="presentation") |
|---|
| 140 | { |
|---|
| 141 | fprintf(stderr,"document of the wrong type, root node != presentation"); |
|---|
| 142 | return false; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | bool readVars = false; |
|---|
| 146 | |
|---|
| 147 | for(osgDB::XmlNode::Children::iterator itr = root->children.begin(); |
|---|
| 148 | itr != root->children.end(); |
|---|
| 149 | ++itr) |
|---|
| 150 | { |
|---|
| 151 | osgDB::XmlNode* cur = itr->get(); |
|---|
| 152 | |
|---|
| 153 | if (cur->name=="env") |
|---|
| 154 | { |
|---|
| 155 | char* str = strdup(cur->contents.c_str()); |
|---|
| 156 | osg::notify(osg::INFO)<<"putenv("<<str<<")"<<std::endl; |
|---|
| 157 | putenv(str); |
|---|
| 158 | readVars = true; |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | return readVars; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | osgDB::Options* createOptions(const osgDB::ReaderWriter::Options* options) |
|---|
| 166 | { |
|---|
| 167 | osg::ref_ptr<osgDB::Options> local_options = options ? options->cloneOptions() : 0; |
|---|
| 168 | if (!local_options) |
|---|
| 169 | { |
|---|
| 170 | local_options = osgDB::Registry::instance()->getOptions() ? |
|---|
| 171 | osgDB::Registry::instance()->getOptions()->cloneOptions() : |
|---|
| 172 | new osgDB::Options; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | local_options->setPluginStringData("P3D_EVENTHANDLER","none"); |
|---|
| 176 | return local_options.release(); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | osg::Node* p3d::readHoldingSlide(const std::string& filename) |
|---|
| 180 | { |
|---|
| 181 | std::string ext = osgDB::getFileExtension(filename); |
|---|
| 182 | if (!osgDB::equalCaseInsensitive(ext,"xml") && |
|---|
| 183 | !osgDB::equalCaseInsensitive(ext,"p3d")) return 0; |
|---|
| 184 | |
|---|
| 185 | osg::ref_ptr<osgDB::ReaderWriter::Options> options = createOptions(0); |
|---|
| 186 | options->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_NONE); |
|---|
| 187 | options->setOptionString("preview"); |
|---|
| 188 | |
|---|
| 189 | return osgDB::readNodeFile(filename, options.get()); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | osg::Node* p3d::readPresentation(const std::string& filename,const osgDB::ReaderWriter::Options* options) |
|---|
| 193 | { |
|---|
| 194 | std::string ext = osgDB::getFileExtension(filename); |
|---|
| 195 | if (!osgDB::equalCaseInsensitive(ext,"xml") && |
|---|
| 196 | !osgDB::equalCaseInsensitive(ext,"p3d")) return 0; |
|---|
| 197 | |
|---|
| 198 | osg::ref_ptr<osgDB::Options> local_options = createOptions(options); |
|---|
| 199 | local_options->setOptionString("main"); |
|---|
| 200 | |
|---|
| 201 | return osgDB::readNodeFile(filename, local_options.get()); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | osg::Node* p3d::readShowFiles(osg::ArgumentParser& arguments,const osgDB::ReaderWriter::Options* options) |
|---|
| 205 | { |
|---|
| 206 | osg::ref_ptr<osgDB::Options> local_options = createOptions(options); |
|---|
| 207 | local_options->setOptionString("main"); |
|---|
| 208 | |
|---|
| 209 | typedef std::vector< osg::ref_ptr<osg::Node> > NodeList; |
|---|
| 210 | NodeList nodeList; |
|---|
| 211 | |
|---|
| 212 | std::string filename; |
|---|
| 213 | while (arguments.read("--image",filename)) |
|---|
| 214 | { |
|---|
| 215 | osg::ref_ptr<osg::Image> image = readImageFile(filename.c_str(), local_options.get()); |
|---|
| 216 | if (image.valid()) nodeList.push_back(osg::createGeodeForImage(image.get())); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | while (arguments.read("--movie",filename)) |
|---|
| 220 | { |
|---|
| 221 | osg::ref_ptr<osg::Image> image = readImageFile(filename.c_str(), local_options.get()); |
|---|
| 222 | osg::ref_ptr<osg::ImageStream> imageStream = dynamic_cast<osg::ImageStream*>(image.get()); |
|---|
| 223 | if (image.valid()) |
|---|
| 224 | { |
|---|
| 225 | imageStream->play(); |
|---|
| 226 | nodeList.push_back(osg::createGeodeForImage(imageStream.get())); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | while (arguments.read("--dem",filename)) |
|---|
| 231 | { |
|---|
| 232 | osg::HeightField* hf = readHeightFieldFile(filename.c_str(), local_options.get()); |
|---|
| 233 | if (hf) |
|---|
| 234 | { |
|---|
| 235 | osg::Geode* geode = new osg::Geode; |
|---|
| 236 | geode->addDrawable(new osg::ShapeDrawable(hf)); |
|---|
| 237 | nodeList.push_back(geode); |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | for(int pos=1;pos<arguments.argc();++pos) |
|---|
| 243 | { |
|---|
| 244 | if (!arguments.isOption(pos)) |
|---|
| 245 | { |
|---|
| 246 | |
|---|
| 247 | osg::Node *node = osgDB::readNodeFile( arguments[pos], local_options.get()); |
|---|
| 248 | |
|---|
| 249 | if(node) |
|---|
| 250 | { |
|---|
| 251 | if (node->getName().empty()) node->setName( arguments[pos] ); |
|---|
| 252 | nodeList.push_back(node); |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | if (nodeList.empty()) |
|---|
| 258 | { |
|---|
| 259 | return NULL; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | osg::ref_ptr<osg::Node> root; |
|---|
| 263 | |
|---|
| 264 | if (nodeList.size()==1) |
|---|
| 265 | { |
|---|
| 266 | root = nodeList.front().get(); |
|---|
| 267 | } |
|---|
| 268 | else |
|---|
| 269 | { |
|---|
| 270 | |
|---|
| 271 | osg::Switch* sw = new osg::Switch; |
|---|
| 272 | for(NodeList::iterator itr=nodeList.begin(); |
|---|
| 273 | itr!=nodeList.end(); |
|---|
| 274 | ++itr) |
|---|
| 275 | { |
|---|
| 276 | sw->addChild((*itr).get()); |
|---|
| 277 | } |
|---|
| 278 | sw->setSingleChildOn(0); |
|---|
| 279 | |
|---|
| 280 | sw->setEventCallback(new p3d::ShowEventHandler()); |
|---|
| 281 | |
|---|
| 282 | root = sw; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | if (root.valid()) |
|---|
| 286 | { |
|---|
| 287 | osg::notify(osg::INFO)<<"Got node now adding callback"<<std::endl; |
|---|
| 288 | |
|---|
| 289 | AddVolumeEditingCallbackVisitor avecv; |
|---|
| 290 | root->accept(avecv); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | return root.release(); |
|---|
| 294 | } |
|---|