source: experimental/distortionNG/ReaderWriterDist.cpp @ 367

Last change on this file since 367 was 367, checked in by Torben Dannhauer, 12 years ago

Dist-Plugin compiles, but not yet tested

File size: 4.3 KB
Line 
1#include "ReaderWriterDist.h"
2#include <osg/KdTree>
3
4ReaderWriterDist::ReaderWriterDist()
5{
6        supportsExtension( "dist", "distortion set loader");
7        extensionToAdd = ".dist";
8
9        // Configure Compression and instantiate read/write-options
10        bool asAscii = true;
11        bool compressionEnabled = false;
12
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"); 
27}
28
29ReaderWriterDist::Options* ReaderWriterDist::prepareReading( ReadResult& result, std::string& fileName, std::ios::openmode& mode, const Options* options ) const
30{
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        }
43
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));
47
48        mode |= std::ios::binary;
49
50        return local_opt.release();
51}
52
53ReaderWriterDist::ReadResult ReaderWriterDist::readObject( const std::string& file, const Options* options) const
54{
55        ReadResult result = ReadResult::FILE_LOADED;
56        std::string fileName = file;
57        std::ios::openmode mode = std::ios::in;
58        Options* local_opt = prepareReading( result, fileName, mode, options );
59        if ( !result.success() ) return result;
60
61        osgDB::ifstream istream( fileName.c_str(), mode );
62        return readObject( istream, local_opt );
63}
64
65ReaderWriterDist::ReadResult ReaderWriterDist::readObject( std::istream& fin, const Options* options ) const
66{
67        if ( rw )
68        {
69                osgDB::ReaderWriter::ReadResult rr = rw->readObject( fin, readOptions );
70                if (rr.success())
71                {
72                        return( rr.takeObject() );
73                }
74                else
75                        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to convert stream to node" << std::endl;
76        }
77        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
78        return( ReadResult::NOT_IMPLEMENTED );
79}
80
81ReaderWriterDist::Options* ReaderWriterDist::prepareWriting( WriteResult& result, const std::string& fileName, std::ios::openmode& mode, const Options* options ) const
82{
83        std::string ext = osgDB::getFileExtension( fileName );
84        if ( !acceptsExtension(ext) ) result = WriteResult::FILE_NOT_HANDLED;
85
86        osg::ref_ptr<Options> local_opt = options ?
87                static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
88        local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
89
90        mode |= std::ios::binary;
91
92        return local_opt.release();
93}
94
95ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, const std::string& fileName, const osgDB::ReaderWriter::Options* options ) const
96{
97        WriteResult result = WriteResult::FILE_SAVED;
98        std::ios::openmode mode = std::ios::out;
99        osg::ref_ptr<Options> local_opt = prepareWriting( result, fileName, mode, options );
100        if ( !result.success() ) return result;
101
102        osgDB::ofstream fout( fileName.c_str(), mode );
103        if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE;
104
105        result = writeObject( object, fout, local_opt.get() );
106        fout.close();
107        return result;
108}
109
110ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, std::ostream& fout, const osgDB::ReaderWriter::Options* options ) const
111{
112        if ( rw )
113        {
114                osgDB::ReaderWriter::WriteResult wr = rw->writeObject( object, fout, writeOptions );
115                if (wr.success() )                     
116                {
117                        OSG_NOTIFY( osg::WARN ) << "schreiben geschafft!"<< std::endl;
118                }
119                else OSG_NOTIFY( osg::WARN ) << "ERROR: Save failed: " << wr.message() << std::endl;
120                return(wr);
121        }
122        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
123        return( WriteResult::NOT_IMPLEMENTED );
124}
125
126// Add ourself to the Registry to instantiate the reader/writer.
127REGISTER_OSGPLUGIN(dist, ReaderWriterDist)
Note: See TracBrowser for help on using the repository browser.