openscenegraph
OutputStream
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13// Written by Wang Rui, (C) 2010
14
15#ifndef OSGDB_OUTPUTSTREAM
16#define OSGDB_OUTPUTSTREAM
17
18#include <osg/Version>
19#include <osg/Vec2>
20#include <osg/Vec3>
21#include <osg/Vec4>
22#include <osg/Quat>
23#include <osg/Matrix>
24#include <osg/BoundingBox>
25#include <osg/BoundingSphere>
26#include <osg/Array>
27#include <osg/PrimitiveSet>
28#include <osgDB/ReaderWriter>
29#include <osgDB/StreamOperator>
30#include <iostream>
31#include <sstream>
32
33namespace osgDB
34{
35
36class OutputException : public osg::Referenced
37{
38public:
39 OutputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
40 {
41 for ( unsigned int i=0; i<fields.size(); ++i )
42 {
43 _field += fields[i];
44 _field += " ";
45 }
46 }
47
48 const std::string& getField() const { return _field; }
49 const std::string& getError() const { return _error; }
50
51protected:
52 std::string _field;
53 std::string _error;
54};
55
56class OSGDB_EXPORT OutputStream
57{
58public:
59 typedef std::map<const osg::Array*, unsigned int> ArrayMap;
60 typedef std::map<const osg::Object*, unsigned int> ObjectMap;
61
62 enum WriteType
63 {
64 WRITE_UNKNOWN = 0,
65 WRITE_SCENE,
66 WRITE_IMAGE,
67 WRITE_OBJECT
68 };
69
70 enum WriteImageHint
71 {
72 WRITE_USE_IMAGE_HINT = 0, /*!< Use image hint, write inline data or use external */
73 WRITE_USE_EXTERNAL, /*!< Use external file on disk and write only the filename */
74 WRITE_INLINE_DATA, /*!< Write Image::data() to stream */
75 WRITE_INLINE_FILE, /*!< Write the image file itself to stream */
76 WRITE_EXTERNAL_FILE /*!< Write Image::data() to disk and use it as external file */
77 };
78
79 OutputStream( const osgDB::Options* options );
80 virtual ~OutputStream();
81
82 void setFileVersion( const std::string& d, int v );
83 int getFileVersion( const std::string& d=std::string() ) const;
84
85 bool isBinary() const { return _out->isBinary(); }
86 const std::string& getSchemaName() const { return _schemaName; }
87 const osgDB::Options* getOptions() const { return _options.get(); }
88
89 void setWriteImageHint( WriteImageHint hint ) { _writeImageHint = hint; }
90 WriteImageHint getWriteImageHint() const { return _writeImageHint; }
91
92 // Serialization related functions
93 OutputStream& operator<<( bool b ) { _out->writeBool(b); return *this; }
94 OutputStream& operator<<( char c ) { _out->writeChar(c); return *this; }
95 OutputStream& operator<<( signed char c) { _out->writeChar(c); return *this; }
96 OutputStream& operator<<( unsigned char c ) { _out->writeUChar(c); return *this; }
97 OutputStream& operator<<( short s ) { _out->writeShort(s); return *this; }
98 OutputStream& operator<<( unsigned short s ) { _out->writeUShort(s); return *this; }
99 OutputStream& operator<<( int i ) { _out->writeInt(i); return *this; }
100 OutputStream& operator<<( unsigned int i ) { _out->writeUInt(i); return *this; }
101 OutputStream& operator<<( long l ) { _out->writeLong(l); return *this; }
102 OutputStream& operator<<( unsigned long l ) { _out->writeULong(l); return *this; }
103 OutputStream& operator<<( float f ) { _out->writeFloat(f); return *this; }
104 OutputStream& operator<<( double d ) { _out->writeDouble(d); return *this; }
105 OutputStream& operator<<( long long ll ) { _out->writeInt64(ll); return *this; }
106 OutputStream& operator<<( unsigned long long ull ) { _out->writeUInt64(ull); return *this; }
107 OutputStream& operator<<( const std::string& s ) { _out->writeString(s); return *this; }
108 OutputStream& operator<<( const char* s ) { _out->writeString(s); return *this; }
109 OutputStream& operator<<( std::ostream& (*fn)(std::ostream&) ) { _out->writeStream(fn); return *this; }
110 OutputStream& operator<<( std::ios_base& (*fn)(std::ios_base&) ) { _out->writeBase(fn); return *this; }
111
112 OutputStream& operator<<( const ObjectGLenum& value ) { _out->writeGLenum(value); return *this; }
113 OutputStream& operator<<( const ObjectProperty& prop ) { _out->writeProperty(prop); return *this; }
114 OutputStream& operator<<( const ObjectMark& mark ) { _out->writeMark(mark); return *this; }
115
116 OutputStream& operator<<( const osg::Vec2b& v );
117 OutputStream& operator<<( const osg::Vec3b& v );
118 OutputStream& operator<<( const osg::Vec4b& v );
119 OutputStream& operator<<( const osg::Vec2ub& v );
120 OutputStream& operator<<( const osg::Vec3ub& v );
121 OutputStream& operator<<( const osg::Vec4ub& v );
122 OutputStream& operator<<( const osg::Vec2s& v );
123 OutputStream& operator<<( const osg::Vec3s& v );
124 OutputStream& operator<<( const osg::Vec4s& v );
125 OutputStream& operator<<( const osg::Vec2us& v );
126 OutputStream& operator<<( const osg::Vec3us& v );
127 OutputStream& operator<<( const osg::Vec4us& v );
128 OutputStream& operator<<( const osg::Vec2i& v );
129 OutputStream& operator<<( const osg::Vec3i& v );
130 OutputStream& operator<<( const osg::Vec4i& v );
131 OutputStream& operator<<( const osg::Vec2ui& v );
132 OutputStream& operator<<( const osg::Vec3ui& v );
133 OutputStream& operator<<( const osg::Vec4ui& v );
134 OutputStream& operator<<( const osg::Vec2f& v );
135 OutputStream& operator<<( const osg::Vec3f& v );
136 OutputStream& operator<<( const osg::Vec4f& v );
137 OutputStream& operator<<( const osg::Vec2d& v );
138 OutputStream& operator<<( const osg::Vec3d& v );
139 OutputStream& operator<<( const osg::Vec4d& v );
140 OutputStream& operator<<( const osg::Quat& q );
141 OutputStream& operator<<( const osg::Plane& p );
142 OutputStream& operator<<( const osg::Matrixf& mat );
143 OutputStream& operator<<( const osg::Matrixd& mat );
144 OutputStream& operator<<( const osg::BoundingBoxf& bb );
145 OutputStream& operator<<( const osg::BoundingBoxd& bb );
146 OutputStream& operator<<( const osg::BoundingSpheref& bb );
147 OutputStream& operator<<( const osg::BoundingSphered& bb );
148
149 OutputStream& operator<<( const osg::Image* img ) { writeImage(img); return *this; }
150 OutputStream& operator<<( const osg::Array* a ) { if (_targetFileVersion >= 112) writeObject(a); else writeArray(a); return *this; }
151 OutputStream& operator<<( const osg::PrimitiveSet* p ) { if (_targetFileVersion >= 112) writeObject(p); else writePrimitiveSet(p); return *this; }
152 OutputStream& operator<<( const osg::Object* obj ) { writeObject(obj); return *this; }
153
154 OutputStream& operator<<( const osg::ref_ptr<osg::Image>& ptr ) { writeImage(ptr.get()); return *this; }
155 OutputStream& operator<<( const osg::ref_ptr<osg::Array>& ptr ) { if (_targetFileVersion >= 112) writeObject(ptr.get()); else writeArray(ptr.get()); return *this; }
156 OutputStream& operator<<( const osg::ref_ptr<osg::PrimitiveSet>& ptr ) { if (_targetFileVersion >= 112) writeObject(ptr.get()); else writePrimitiveSet(ptr.get()); return *this; }
157
158 template<typename T> OutputStream& operator<<( const osg::ref_ptr<T>& ptr ) { writeObject(ptr.get()); return *this; }
159
160 // Convenient methods for writing
161 void writeWrappedString( const std::string& str ) { _out->writeWrappedString(str); }
162 void writeCharArray( const char* s, unsigned int size ) { _out->writeCharArray(s, size); }
163
164 // method for converting all data structure sizes to unsigned int to ensure architecture portability.
165 template<typename T>
166 void writeSize(T size) { *this<<static_cast<unsigned int>(size); }
167
168 // Global writing functions
169 void writeArray( const osg::Array* a );
170 void writePrimitiveSet( const osg::PrimitiveSet* p );
171 void writeImage( const osg::Image* img );
172 void writeObject( const osg::Object* obj );
173 void writeObjectFields( const osg::Object* obj );
174 void writeObjectFields( const osg::Object* obj, const std::string& compoundName );
175
176 /// set an output iterator, used directly when not using OutputStream with a traditional file related stream.
177 void setOutputIterator( OutputIterator* oi ) { _out = oi; }
178
179 /// start writing to OutputStream treating it as a traditional file related stream, handles headers and versioning
180 void start( OutputIterator* outIterator, WriteType type );
181
182 void compress( std::ostream* ostream );
183
184 // Schema handlers
185 void writeSchema( std::ostream& fout );
186
187 // Exception handlers
188 inline void throwException( const std::string& msg );
189 const OutputException* getException() const { return _exception.get(); }
190
191 // Property & mask variables
192 ObjectProperty PROPERTY;
193 ObjectMark BEGIN_BRACKET;
194 ObjectMark END_BRACKET;
195
196protected:
197 template<typename T>
198 void writeArrayImplementation( const T*, int write_size, unsigned int numInRow=1 );
199
200 unsigned int findOrCreateArrayID( const osg::Array* array, bool& newID );
201 unsigned int findOrCreateObjectID( const osg::Object* obj, bool& newID );
202
203 ArrayMap _arrayMap;
204 ObjectMap _objectMap;
205
206 typedef std::map<std::string, int> VersionMap;
207 VersionMap _domainVersionMap;
208 WriteImageHint _writeImageHint;
209 bool _useSchemaData;
210 bool _useRobustBinaryFormat;
211
212 typedef std::map<std::string, std::string> SchemaMap;
213 SchemaMap _inbuiltSchemaMap;
214 std::vector<std::string> _fields;
215 std::string _schemaName;
216 std::string _compressorName;
217 std::stringstream _compressSource;
218 osg::ref_ptr<OutputIterator> _out;
219 osg::ref_ptr<OutputException> _exception;
220 osg::ref_ptr<const osgDB::Options> _options;
221
222 int _targetFileVersion;
223};
224
225void OutputStream::throwException( const std::string& msg )
226{
227 _exception = new OutputException(_fields, msg);
228}
229
230}
231
232#endif