1 | #include "ReaderWriterDist.h" |
---|
2 | #include <osg/KdTree> |
---|
3 | |
---|
4 | ReaderWriterDist::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 | |
---|
29 | ReaderWriterDist::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 | |
---|
53 | ReaderWriterDist::ReadResult ReaderWriterDist::readObject( const std::string& file, const Options* options) const |
---|
54 | { |
---|
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. |
---|
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 | |
---|
66 | ReaderWriterDist::ReadResult ReaderWriterDist::readObject( std::istream& fin, const Options* options ) const |
---|
67 | { |
---|
68 | // This function uses the osgb|t|x plugin to perform the operation |
---|
69 | if ( rw ) |
---|
70 | { |
---|
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; |
---|
78 | } |
---|
79 | OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl; |
---|
80 | return( ReadResult::NOT_IMPLEMENTED ); |
---|
81 | } |
---|
82 | |
---|
83 | ReaderWriterDist::Options* ReaderWriterDist::prepareWriting( WriteResult& result, const std::string& fileName, std::ios::openmode& mode, const Options* options ) const |
---|
84 | { |
---|
85 | std::string ext = osgDB::getFileExtension( fileName ); |
---|
86 | if ( !acceptsExtension(ext) ) result = WriteResult::FILE_NOT_HANDLED; |
---|
87 | |
---|
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)); |
---|
91 | |
---|
92 | mode |= std::ios::binary; |
---|
93 | |
---|
94 | return local_opt.release(); |
---|
95 | } |
---|
96 | |
---|
97 | ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, const std::string& fileName, const osgDB::ReaderWriter::Options* options ) const |
---|
98 | { |
---|
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. |
---|
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 | |
---|
113 | ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, std::ostream& fout, const osgDB::ReaderWriter::Options* options ) const |
---|
114 | { |
---|
115 | // This function uses the osgb|t|x plugin to perform the operation |
---|
116 | if ( rw ) |
---|
117 | { |
---|
118 | osgDB::ReaderWriter::WriteResult wr = rw->writeObject( object, fout, writeOptions ); |
---|
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; |
---|
127 | return( WriteResult::NOT_IMPLEMENTED ); |
---|
128 | } |
---|
129 | |
---|
130 | // Add ourself to the Registry to instantiate the reader/writer. |
---|
131 | REGISTER_OSGPLUGIN(dist, ReaderWriterDist) |
---|