[365] | 1 | #include "ReaderWriterDist.h" |
---|
| 2 | #include <osg/KdTree> |
---|
| 3 | |
---|
| 4 | ReaderWriterDist::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"); |
---|
| 27 | |
---|
[365] | 28 | } |
---|
| 29 | |
---|
| 30 | ReaderWriterDist::ReadResult ReaderWriterDist::readNode( const std::string& file, const osgDB::ReaderWriter::Options* options) const |
---|
| 31 | { |
---|
| 32 | std::string ext = osgDB::getLowerCaseFileExtension( file ); |
---|
| 33 | if( !acceptsExtension(ext) ) |
---|
| 34 | return ReadResult::FILE_NOT_HANDLED; |
---|
| 35 | |
---|
[366] | 36 | // check if file exists |
---|
| 37 | std::string fileName = osgDB::findDataFile( file, options ); |
---|
[365] | 38 | if( fileName.empty() ) |
---|
| 39 | return ReadResult::FILE_NOT_FOUND; |
---|
| 40 | |
---|
[366] | 41 | std::ifstream stream( fileName.c_str(), std::ios::in ); |
---|
| 42 | if (!stream) return ReadResult::ERROR_IN_READING_FILE; |
---|
| 43 | return readNode( stream, options ); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | ReaderWriterDist::ReadResult ReaderWriterDist:: readNode( std::istream& fin, const osgDB::ReaderWriter::Options* options ) const |
---|
| 47 | { |
---|
| 48 | if ( rw ) |
---|
[365] | 49 | { |
---|
[366] | 50 | osgDB::ReaderWriter::ReadResult rr = rw->readObject( fin, readOptions ); |
---|
| 51 | if (rr.success()) |
---|
| 52 | { |
---|
| 53 | return( rr.takeObject() ); |
---|
| 54 | } |
---|
| 55 | else |
---|
| 56 | OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to convert stream to node" << std::endl; |
---|
[365] | 57 | } |
---|
[366] | 58 | else |
---|
| 59 | OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl; |
---|
| 60 | } |
---|
[365] | 61 | |
---|
[366] | 62 | ReaderWriterDist::WriteResult writeNode( const osg::Node& node, const std::string& fileName, const Options* options ) const |
---|
| 63 | { |
---|
| 64 | std::string ext = osgDB::getLowerCaseFileExtension( file ); |
---|
| 65 | if( !acceptsExtension(ext) ) |
---|
| 66 | return WriteResult::FILE_NOT_HANDLED; |
---|
[365] | 67 | |
---|
[366] | 68 | std::ifstream stream( fileName.c_str(), std::ios::in ); |
---|
| 69 | if (!stream) return ReadResult::ERROR_IN_READING_FILE; |
---|
| 70 | return readNode( stream, options ); |
---|
| 71 | // ------------------ |
---|
| 72 | WriteResult result = WriteResult::FILE_SAVED; |
---|
| 73 | std::ios::openmode mode = std::ios::out; |
---|
| 74 | osg::ref_ptr<Options> local_opt = prepareWriting( result, fileName, mode, options ); |
---|
| 75 | if ( !result.success() ) return result; |
---|
| 76 | |
---|
| 77 | osgDB::ofstream fout( fileName.c_str(), mode ); |
---|
| 78 | if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE; |
---|
| 79 | |
---|
| 80 | result = writeNode( node, fout, local_opt.get() ); |
---|
| 81 | fout.close(); |
---|
| 82 | return result; |
---|
[365] | 83 | } |
---|
| 84 | |
---|
[366] | 85 | ReaderWriterDist::WriteResult writeNode( const osg::Node& node, std::ostream& fout, const Options* options ) const |
---|
| 86 | { |
---|
| 87 | if ( rw ) |
---|
| 88 | { |
---|
| 89 | osgDB::ReaderWriter::WriteResult wr = rw->writeObject( node, fout, writeOptions ); |
---|
| 90 | if (wr.success() ) |
---|
| 91 | { |
---|
| 92 | OSG_NOTIFY( osg::WARN ) << "schreiben geschafft!"<< std::endl; |
---|
| 93 | } |
---|
| 94 | else OSG_NOTIFY( osg::WARN ) << "ERROR: Save failed: " << wr.message() << std::endl; |
---|
| 95 | return(wr); |
---|
| 96 | } |
---|
| 97 | OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl; |
---|
| 98 | } |
---|
| 99 | |
---|
[365] | 100 | // Add ourself to the Registry to instantiate the reader/writer. |
---|
[366] | 101 | REGISTER_OSGPLUGIN(dist, ReaderWriterDist) |
---|