source: experimental/distortionNG/ReaderWriterDist.cpp @ 368

Last change on this file since 368 was 368, checked in by Torben Dannhauer, 12 years ago
File size: 4.8 KB
RevLine 
[365]1#include "ReaderWriterDist.h"
2#include <osg/KdTree>
3
4ReaderWriterDist::ReaderWriterDist()
5{
6        supportsExtension( "dist", "distortion set loader");
[366]7        extensionToAdd = ".dist";
[365]8
[366]9        // Configure Compression and instantiate read/write-options
10        bool asAscii = true;
11        bool compressionEnabled = false;
[365]12
[366]13        std::string readOptionString = "";
14        std::string writeOptionString = "";
15        if(asAscii)
16        {
17                readOptionString = "Ascii";
18                writeOptionString = "Ascii";
19        }
20        if (compressionEnabled)
21                writeOptionString+=" Compressor=zlib";
22        readOptions = new osgDB::Options( readOptionString.c_str() );
23        writeOptions = new osgDB::Options( writeOptionString.c_str() );
24
25        // Get ReaderWriter
26        rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgt"); 
[365]27}
28
[367]29ReaderWriterDist::Options* ReaderWriterDist::prepareReading( ReadResult& result, std::string& fileName, std::ios::openmode& mode, const Options* options ) const
[365]30{
[367]31        std::string ext = osgDB::getLowerCaseFileExtension( fileName );
32        if ( !acceptsExtension(ext) )
33        {
34                result = ReadResult::FILE_NOT_HANDLED;
35                return 0;
36        }
37        fileName = osgDB::findDataFile( fileName, options );
38        if ( fileName.empty() )
39        {
40                result = ReadResult::FILE_NOT_FOUND;
41                return 0;
42        }
[365]43
[367]44        osg::ref_ptr<Options> local_opt = options ?
45                static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
46        local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
[365]47
[367]48        mode |= std::ios::binary;
49
50        return local_opt.release();
[366]51}
52
[367]53ReaderWriterDist::ReadResult ReaderWriterDist::readObject( const std::string& file, const Options* options) const
[366]54{
[368]55        // This function must be reimplemented because the osgb|t|x plugin would test for a valid filename extension and thus would block the fiel due to it's .dist extension.
[367]56        ReadResult result = ReadResult::FILE_LOADED;
57        std::string fileName = file;
58        std::ios::openmode mode = std::ios::in;
59        Options* local_opt = prepareReading( result, fileName, mode, options );
60        if ( !result.success() ) return result;
61
62        osgDB::ifstream istream( fileName.c_str(), mode );
63        return readObject( istream, local_opt );
64}
65
66ReaderWriterDist::ReadResult ReaderWriterDist::readObject( std::istream& fin, const Options* options ) const
67{
[368]68        // This function uses the osgb|t|x plugin to perform the operation
[366]69        if ( rw )
[365]70        {
[366]71                osgDB::ReaderWriter::ReadResult rr = rw->readObject( fin, readOptions );
72                if (rr.success())
73                {
74                        return( rr.takeObject() );
75                }
76                else
77                        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to convert stream to node" << std::endl;
[365]78        }
[367]79        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
80        return( ReadResult::NOT_IMPLEMENTED );
[366]81}
[365]82
[367]83ReaderWriterDist::Options* ReaderWriterDist::prepareWriting( WriteResult& result, const std::string& fileName, std::ios::openmode& mode, const Options* options ) const
[366]84{
[367]85        std::string ext = osgDB::getFileExtension( fileName );
86        if ( !acceptsExtension(ext) ) result = WriteResult::FILE_NOT_HANDLED;
[365]87
[367]88        osg::ref_ptr<Options> local_opt = options ?
89                static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
90        local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
[366]91
[367]92        mode |= std::ios::binary;
[366]93
[367]94        return local_opt.release();
[365]95}
96
[367]97ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, const std::string& fileName, const osgDB::ReaderWriter::Options* options ) const
[366]98{
[368]99        // This function must be reimplemented because the osgb|t|x plugin would test for a valid filename extension and thus would block the fiel due to it's .dist extension.
[367]100        WriteResult result = WriteResult::FILE_SAVED;
101        std::ios::openmode mode = std::ios::out;
102        osg::ref_ptr<Options> local_opt = prepareWriting( result, fileName, mode, options );
103        if ( !result.success() ) return result;
104
105        osgDB::ofstream fout( fileName.c_str(), mode );
106        if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE;
107
108        result = writeObject( object, fout, local_opt.get() );
109        fout.close();
110        return result;
111}
112
113ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, std::ostream& fout, const osgDB::ReaderWriter::Options* options ) const
114{
[368]115        // This function uses the osgb|t|x plugin to perform the operation
[366]116        if ( rw )
117        {
[367]118                osgDB::ReaderWriter::WriteResult wr = rw->writeObject( object, fout, writeOptions );
[366]119                if (wr.success() )                     
120                {
121                        OSG_NOTIFY( osg::WARN ) << "schreiben geschafft!"<< std::endl;
122                }
123                else OSG_NOTIFY( osg::WARN ) << "ERROR: Save failed: " << wr.message() << std::endl;
124                return(wr);
125        }
126        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
[367]127        return( WriteResult::NOT_IMPLEMENTED );
[366]128}
129
[365]130// Add ourself to the Registry to instantiate the reader/writer.
[367]131REGISTER_OSGPLUGIN(dist, ReaderWriterDist)
Note: See TracBrowser for help on using the repository browser.