SgNode* YAMLSceneLoaderImpl::load(const std::string& filename)
{
    SgNodePtr scene;
    MappingPtr topNode;
    
    try {
        YAMLReader reader;
        topNode = reader.loadDocument(filename)->toMapping();
        if(topNode){
            boost::filesystem::path filepath(filename);
            sceneReader.setBaseDirectory(filepath.parent_path().string());
            sceneReader.readHeader(*topNode);
            ValueNodePtr sceneElements = topNode->findMapping("scene");
            if(!sceneElements->isValid()){
                os() << format(_("Scene file \"{}\" does not have the \"scene\" node."), filename) << endl;
            } else {
                scene = sceneReader.readNodeList(*sceneElements);
                if(!scene){
                    os() << format(_("Scene file \"{}\" is an empty scene."), filename) << endl;
                    scene = new SgNode;
                }
            }
        }
    } catch(const ValueNode::Exception& ex){
        os() << ex.message();
    }

    os().flush();

    sceneReader.clear();
    
    return scene.retn();
}
Beispiel #2
0
bool BodyLoaderImpl::load(Body* body, const std::string& filename)
{
    bool result = false;

    filesystem::path orgpath(filename);
    string ext = getExtension(orgpath);
    string modelFilename;
    MappingPtr yamlDoc;

    try {
        if(ext != "yaml"){
            modelFilename = filename;
        } else {
            YAMLReader parser;
            yamlDoc = parser.loadDocument(filename)->toMapping();

            ValueNode* modelFileNode = yamlDoc->find("modelFile");
            if(modelFileNode->isValid()){
                filesystem::path mpath(modelFileNode->toString());
                if(mpath.has_root_path()){
                    modelFilename = getNativePathString(mpath);
                } else {
                    modelFilename = getNativePathString(orgpath.parent_path() / mpath);
                }
                ext = getExtension(mpath);
            }
        }

        LoaderMap::iterator p = loaderMap.find(ext);
        if(p != loaderMap.end()){
            loader = p->second;
        } else {
            boost::lock_guard<boost::mutex> lock(loaderFactoryMapMutex);
            LoaderFactoryMap::iterator q = loaderFactoryMap.find(ext);
            if(q != loaderFactoryMap.end()){
                LoaderFactory factory = q->second;
                loader = factory();
                loaderMap[ext] = loader;
            }
        }

        if(!loader){
            (*os) << str(boost::format(_("The file format of \"%1%\" is not supported by the body loader.\n"))
                         % getFilename(filesystem::path(modelFilename)));

        } else {
            loader->setMessageSink(*os);
            loader->setVerbose(isVerbose);
            loader->setShapeLoadingEnabled(isShapeLoadingEnabled);

            YAMLBodyLoader* yamlBodyLoader = 0;
            if(yamlDoc){
                YAMLBodyLoader* yamlBodyLoader = dynamic_cast<YAMLBodyLoader*>(loader.get());
            }

            if(yamlBodyLoader){
                result = yamlBodyLoader->read(body, yamlDoc);

            } else {
                int dn = defaultDivisionNumber;
                if(yamlDoc){
                    Mapping& geometryInfo = *yamlDoc->findMapping("geometry");
                    if(geometryInfo.isValid()){
                        geometryInfo.read("divisionNumber", dn);
                    }
                }
                if(dn > 0){
                    loader->setDefaultDivisionNumber(dn);
                }

                if(defaultCreaseAngle >= 0.0){
                    loader->setDefaultCreaseAngle(defaultCreaseAngle);
                }
            
                if(yamlDoc){
                    body->resetInfo(yamlDoc);
                } else {
                    body->info()->clear();
                }

                result = loader->load(body, modelFilename);
            }
        }
        
    } catch(const ValueNode::Exception& ex){
        (*os) << ex.message();
    } catch(const nonexistent_key_error& error){
        if(const std::string* message = boost::get_error_info<error_info_message>(error)){
            (*os) << *message;
        }
    } catch(const std::exception& ex){
        (*os) << ex.what();
    }
    os->flush();
    
    return result;
}