source: experimental/distortionNG/ReaderWriterDist.cpp @ 376

Last change on this file since 376 was 371, checked in by Torben Dannhauer, 12 years ago
File size: 5.8 KB
RevLine 
[371]1/* osgVisual test. distortionNG, experimental.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
[365]19#include "ReaderWriterDist.h"
20#include <osg/KdTree>
21
22ReaderWriterDist::ReaderWriterDist()
23{
24        supportsExtension( "dist", "distortion set loader");
[366]25        extensionToAdd = ".dist";
[365]26
[366]27        // Configure Compression and instantiate read/write-options
28        bool asAscii = true;
29        bool compressionEnabled = false;
[365]30
[366]31        std::string readOptionString = "";
32        std::string writeOptionString = "";
33        if(asAscii)
34        {
35                readOptionString = "Ascii";
36                writeOptionString = "Ascii";
37        }
38        if (compressionEnabled)
39                writeOptionString+=" Compressor=zlib";
40        readOptions = new osgDB::Options( readOptionString.c_str() );
41        writeOptions = new osgDB::Options( writeOptionString.c_str() );
42
43        // Get ReaderWriter
44        rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgt"); 
[365]45}
46
[367]47ReaderWriterDist::Options* ReaderWriterDist::prepareReading( ReadResult& result, std::string& fileName, std::ios::openmode& mode, const Options* options ) const
[365]48{
[367]49        std::string ext = osgDB::getLowerCaseFileExtension( fileName );
50        if ( !acceptsExtension(ext) )
51        {
52                result = ReadResult::FILE_NOT_HANDLED;
53                return 0;
54        }
55        fileName = osgDB::findDataFile( fileName, options );
56        if ( fileName.empty() )
57        {
58                result = ReadResult::FILE_NOT_FOUND;
59                return 0;
60        }
[365]61
[367]62        osg::ref_ptr<Options> local_opt = options ?
63                static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
64        local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
[365]65
[367]66        mode |= std::ios::binary;
67
68        return local_opt.release();
[366]69}
70
[367]71ReaderWriterDist::ReadResult ReaderWriterDist::readObject( const std::string& file, const Options* options) const
[366]72{
[368]73        // 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]74        ReadResult result = ReadResult::FILE_LOADED;
75        std::string fileName = file;
76        std::ios::openmode mode = std::ios::in;
77        Options* local_opt = prepareReading( result, fileName, mode, options );
78        if ( !result.success() ) return result;
79
80        osgDB::ifstream istream( fileName.c_str(), mode );
81        return readObject( istream, local_opt );
82}
83
84ReaderWriterDist::ReadResult ReaderWriterDist::readObject( std::istream& fin, const Options* options ) const
85{
[368]86        // This function uses the osgb|t|x plugin to perform the operation
[366]87        if ( rw )
[365]88        {
[366]89                osgDB::ReaderWriter::ReadResult rr = rw->readObject( fin, readOptions );
90                if (rr.success())
91                {
92                        return( rr.takeObject() );
93                }
94                else
95                        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to convert stream to node" << std::endl;
[365]96        }
[367]97        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
98        return( ReadResult::NOT_IMPLEMENTED );
[366]99}
[365]100
[367]101ReaderWriterDist::Options* ReaderWriterDist::prepareWriting( WriteResult& result, const std::string& fileName, std::ios::openmode& mode, const Options* options ) const
[366]102{
[367]103        std::string ext = osgDB::getFileExtension( fileName );
104        if ( !acceptsExtension(ext) ) result = WriteResult::FILE_NOT_HANDLED;
[365]105
[367]106        osg::ref_ptr<Options> local_opt = options ?
107                static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
108        local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
[366]109
[367]110        mode |= std::ios::binary;
[366]111
[367]112        return local_opt.release();
[365]113}
114
[367]115ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, const std::string& fileName, const osgDB::ReaderWriter::Options* options ) const
[366]116{
[368]117        // 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]118        WriteResult result = WriteResult::FILE_SAVED;
119        std::ios::openmode mode = std::ios::out;
120        osg::ref_ptr<Options> local_opt = prepareWriting( result, fileName, mode, options );
121        if ( !result.success() ) return result;
122
123        osgDB::ofstream fout( fileName.c_str(), mode );
124        if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE;
125
126        result = writeObject( object, fout, local_opt.get() );
127        fout.close();
128        return result;
129}
130
131ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, std::ostream& fout, const osgDB::ReaderWriter::Options* options ) const
132{
[368]133        // This function uses the osgb|t|x plugin to perform the operation
[366]134        if ( rw )
135        {
[367]136                osgDB::ReaderWriter::WriteResult wr = rw->writeObject( object, fout, writeOptions );
[366]137                if (wr.success() )                     
138                {
139                        OSG_NOTIFY( osg::WARN ) << "schreiben geschafft!"<< std::endl;
140                }
141                else OSG_NOTIFY( osg::WARN ) << "ERROR: Save failed: " << wr.message() << std::endl;
142                return(wr);
143        }
144        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
[367]145        return( WriteResult::NOT_IMPLEMENTED );
[366]146}
147
[365]148// Add ourself to the Registry to instantiate the reader/writer.
[367]149REGISTER_OSGPLUGIN(dist, ReaderWriterDist)
Note: See TracBrowser for help on using the repository browser.