2010-01-21 04:13:33 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
// Written by Wang Rui, (C) 2010
|
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
#ifndef OSGDB_OUTPUTSTREAM
|
|
|
|
#define OSGDB_OUTPUTSTREAM
|
2010-01-21 04:13:33 +08:00
|
|
|
|
2014-02-24 18:19:48 +08:00
|
|
|
#include <osg/Version>
|
2010-01-21 04:13:33 +08:00
|
|
|
#include <osg/Vec2>
|
|
|
|
#include <osg/Vec3>
|
|
|
|
#include <osg/Vec4>
|
|
|
|
#include <osg/Quat>
|
|
|
|
#include <osg/Matrix>
|
2014-02-11 00:44:13 +08:00
|
|
|
#include <osg/BoundingBox>
|
|
|
|
#include <osg/BoundingSphere>
|
2010-01-21 04:13:33 +08:00
|
|
|
#include <osg/Array>
|
|
|
|
#include <osg/PrimitiveSet>
|
|
|
|
#include <osgDB/ReaderWriter>
|
2010-01-25 19:03:21 +08:00
|
|
|
#include <osgDB/StreamOperator>
|
2010-01-21 04:13:33 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace osgDB
|
|
|
|
{
|
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
class OutputException : public osg::Referenced
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2010-01-28 01:09:05 +08:00
|
|
|
OutputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
|
|
|
|
{
|
|
|
|
for ( unsigned int i=0; i<fields.size(); ++i )
|
|
|
|
{
|
|
|
|
_field += fields[i];
|
|
|
|
_field += " ";
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
const std::string& getField() const { return _field; }
|
|
|
|
const std::string& getError() const { return _error; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
protected:
|
|
|
|
std::string _field;
|
|
|
|
std::string _error;
|
|
|
|
};
|
|
|
|
|
2010-01-21 17:25:45 +08:00
|
|
|
class OSGDB_EXPORT OutputStream
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef std::map<const osg::Array*, unsigned int> ArrayMap;
|
|
|
|
typedef std::map<const osg::Object*, unsigned int> ObjectMap;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
enum WriteType
|
|
|
|
{
|
|
|
|
WRITE_UNKNOWN = 0,
|
|
|
|
WRITE_SCENE,
|
2010-03-10 21:48:41 +08:00
|
|
|
WRITE_IMAGE,
|
|
|
|
WRITE_OBJECT
|
2010-01-21 04:13:33 +08:00
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
enum WriteImageHint
|
|
|
|
{
|
|
|
|
WRITE_USE_IMAGE_HINT = 0, /*!< Use image hint, write inline data or use external */
|
|
|
|
WRITE_USE_EXTERNAL, /*!< Use external file on disk and write only the filename */
|
|
|
|
WRITE_INLINE_DATA, /*!< Write Image::data() to stream */
|
|
|
|
WRITE_INLINE_FILE, /*!< Write the image file itself to stream */
|
|
|
|
WRITE_EXTERNAL_FILE /*!< Write Image::data() to disk and use it as external file */
|
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
OutputStream( const osgDB::Options* options );
|
2010-01-21 04:13:33 +08:00
|
|
|
virtual ~OutputStream();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
From Wang Rui, "The file attached includes two new features for the serialization IO functionality. First, custom serializer version control should work now, just by defining a new REGISTER_CUSTOM_OBJECT_WRAPPER macro. For example:
// A custom class
namespace CustomDomain {
class MyGroup : public osg::Group
{
public:
META_Node( CustomDomain, MyGroup );
void setMyName( const std::string& n );
const std::string& getMyName() const;
void setMyID( int id );
int getMyID() const;
...
};
}
// The serialization wrapper using a custom domain name
REGISTER_CUSTOM_OBJECT_WRAPPER( MyDomain,
CustomDomain_MyGroup,
new CustomDomain::MyGroup,
CustomDomain::MyGroup,
"osg::Object osg::Node osg::Group CustomDomain::MyGroup" )
{
ADD_STRING_SERIALIZER( MyName, std::string() );
{
UPDATE_TO_VERSION_SCOPED( 1 ); // Updated for a new domain version
ADD_INT_SERIALIZER( MyID, 0 );
}
}
Save the class instance as follows:
osgDB::writeNodeFile( *myGroup, "serializer_test.osgt", new osgDB::Options("CustomDomains=MyDomain:1") );
The output file will include the domain version definition and all the class data, and can be read back. We can also force setting the domain version by the CustomDomains option while reading the saved files. If we save the class instance without any options, MyID will be ignored because the default domain version is 0.
This may help third-party libraries like osgEarth to maintain their own serializers without regarding to the OSG soversion changes.
Another feature added is a more robust binary format, which in fact adds a size-offset at each block's beginning. When there are problems or unsupported data types while reading, we can now directly jump to the block end indicated by the offset value. So a .osgb file will automatically ignore bad data and read remains as normal (at present it will fail at all). This feature will not break the backward compatibility, and can be disabled by setting "RobustBinaryFormat=false" while writing out.
Hope these changes can work smoothly with present and future community projects. Maybe we should also consider have an osgserializer example to test and demonstrate all things we can do now."
2013-06-24 16:48:55 +08:00
|
|
|
void setFileVersion( const std::string& d, int v ) { _domainVersionMap[d] = v; }
|
|
|
|
int getFileVersion( const std::string& d=std::string() ) const;
|
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
bool isBinary() const { return _out->isBinary(); }
|
2010-01-28 01:09:05 +08:00
|
|
|
const std::string& getSchemaName() const { return _schemaName; }
|
2010-02-26 01:53:51 +08:00
|
|
|
const osgDB::Options* getOptions() const { return _options.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
void setWriteImageHint( WriteImageHint hint ) { _writeImageHint = hint; }
|
|
|
|
WriteImageHint getWriteImageHint() const { return _writeImageHint; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
// Serialization related functions
|
2010-01-25 19:03:21 +08:00
|
|
|
OutputStream& operator<<( bool b ) { _out->writeBool(b); return *this; }
|
|
|
|
OutputStream& operator<<( char c ) { _out->writeChar(c); return *this; }
|
2014-04-29 21:41:35 +08:00
|
|
|
OutputStream& operator<<( signed char c) { _out->writeChar(c); return *this; }
|
2010-01-25 19:03:21 +08:00
|
|
|
OutputStream& operator<<( unsigned char c ) { _out->writeUChar(c); return *this; }
|
|
|
|
OutputStream& operator<<( short s ) { _out->writeShort(s); return *this; }
|
|
|
|
OutputStream& operator<<( unsigned short s ) { _out->writeUShort(s); return *this; }
|
|
|
|
OutputStream& operator<<( int i ) { _out->writeInt(i); return *this; }
|
|
|
|
OutputStream& operator<<( unsigned int i ) { _out->writeUInt(i); return *this; }
|
|
|
|
OutputStream& operator<<( long l ) { _out->writeLong(l); return *this; }
|
|
|
|
OutputStream& operator<<( unsigned long l ) { _out->writeULong(l); return *this; }
|
|
|
|
OutputStream& operator<<( float f ) { _out->writeFloat(f); return *this; }
|
|
|
|
OutputStream& operator<<( double d ) { _out->writeDouble(d); return *this; }
|
|
|
|
OutputStream& operator<<( const std::string& s ) { _out->writeString(s); return *this; }
|
2011-01-19 00:14:24 +08:00
|
|
|
OutputStream& operator<<( const char* s ) { _out->writeString(s); return *this; }
|
2010-01-25 19:03:21 +08:00
|
|
|
OutputStream& operator<<( std::ostream& (*fn)(std::ostream&) ) { _out->writeStream(fn); return *this; }
|
|
|
|
OutputStream& operator<<( std::ios_base& (*fn)(std::ios_base&) ) { _out->writeBase(fn); return *this; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
OutputStream& operator<<( const ObjectGLenum& value ) { _out->writeGLenum(value); return *this; }
|
|
|
|
OutputStream& operator<<( const ObjectProperty& prop ) { _out->writeProperty(prop); return *this; }
|
|
|
|
OutputStream& operator<<( const ObjectMark& mark ) { _out->writeMark(mark); return *this; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
OutputStream& operator<<( const osg::Vec2b& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec3b& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec4b& v );
|
2013-06-28 16:57:42 +08:00
|
|
|
OutputStream& operator<<( const osg::Vec2ub& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec3ub& v );
|
2010-01-21 04:13:33 +08:00
|
|
|
OutputStream& operator<<( const osg::Vec4ub& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec2s& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec3s& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec4s& v );
|
2013-06-28 16:57:42 +08:00
|
|
|
OutputStream& operator<<( const osg::Vec2us& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec3us& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec4us& v );
|
2013-06-03 21:13:18 +08:00
|
|
|
OutputStream& operator<<( const osg::Vec2i& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec3i& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec4i& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec2ui& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec3ui& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec4ui& v );
|
2010-01-21 04:13:33 +08:00
|
|
|
OutputStream& operator<<( const osg::Vec2f& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec3f& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec4f& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec2d& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec3d& v );
|
|
|
|
OutputStream& operator<<( const osg::Vec4d& v );
|
|
|
|
OutputStream& operator<<( const osg::Quat& q );
|
|
|
|
OutputStream& operator<<( const osg::Plane& p );
|
|
|
|
OutputStream& operator<<( const osg::Matrixf& mat );
|
|
|
|
OutputStream& operator<<( const osg::Matrixd& mat );
|
2014-02-11 00:44:13 +08:00
|
|
|
OutputStream& operator<<( const osg::BoundingBoxf& bb );
|
|
|
|
OutputStream& operator<<( const osg::BoundingBoxd& bb );
|
|
|
|
OutputStream& operator<<( const osg::BoundingSpheref& bb );
|
|
|
|
OutputStream& operator<<( const osg::BoundingSphered& bb );
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
OutputStream& operator<<( const osg::Image* img ) { writeImage(img); return *this; }
|
2014-02-24 18:19:48 +08:00
|
|
|
OutputStream& operator<<( const osg::Array* a ) { if (OPENSCENEGRAPH_SOVERSION>=112) writeObject(a); else writeArray(a); return *this; }
|
|
|
|
OutputStream& operator<<( const osg::PrimitiveSet* p ) { if (OPENSCENEGRAPH_SOVERSION>=112) writeObject(p); else writePrimitiveSet(p); return *this; }
|
2010-01-21 04:13:33 +08:00
|
|
|
OutputStream& operator<<( const osg::Object* obj ) { writeObject(obj); return *this; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
OutputStream& operator<<( const osg::ref_ptr<osg::Image>& ptr ) { writeImage(ptr.get()); return *this; }
|
2014-02-24 18:19:48 +08:00
|
|
|
OutputStream& operator<<( const osg::ref_ptr<osg::Array>& ptr ) { if (OPENSCENEGRAPH_SOVERSION>=112) writeObject(ptr.get()); else writeArray(ptr.get()); return *this; }
|
|
|
|
OutputStream& operator<<( const osg::ref_ptr<osg::PrimitiveSet>& ptr ) { if (OPENSCENEGRAPH_SOVERSION>=112) writeObject(ptr.get()); else writePrimitiveSet(ptr.get()); return *this; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2014-02-24 18:19:48 +08:00
|
|
|
template<typename T> OutputStream& operator<<( const osg::ref_ptr<T>& ptr ) { writeObject(ptr.get()); return *this; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
// Convenient methods for writing
|
2010-03-10 21:48:41 +08:00
|
|
|
void writeWrappedString( const std::string& str ) { _out->writeWrappedString(str); }
|
2010-01-25 19:03:21 +08:00
|
|
|
void writeCharArray( const char* s, unsigned int size ) { _out->writeCharArray(s, size); }
|
2010-02-11 01:03:09 +08:00
|
|
|
|
|
|
|
// method for converting all data structure sizes to unsigned int to ensure architecture portability.
|
|
|
|
template<typename T>
|
|
|
|
void writeSize(T size) { *this<<static_cast<unsigned int>(size); }
|
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
// Global writing functions
|
|
|
|
void writeArray( const osg::Array* a );
|
|
|
|
void writePrimitiveSet( const osg::PrimitiveSet* p );
|
|
|
|
void writeImage( const osg::Image* img );
|
|
|
|
void writeObject( const osg::Object* obj );
|
2010-09-24 00:12:05 +08:00
|
|
|
void writeObjectFields( const osg::Object* obj );
|
2014-03-06 18:27:26 +08:00
|
|
|
void writeObjectFields( const osg::Object* obj, const std::string& compoundName );
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-05-16 16:50:59 +08:00
|
|
|
/// set an output iterator, used directly when not using OutputStream with a traditional file releated stream.
|
|
|
|
void setOutputIterator( OutputIterator* oi ) { _out = oi; }
|
|
|
|
|
|
|
|
/// start writing to OutputStream treating it as a traditional file releated stream, handles headers and versioning
|
2010-01-25 19:03:21 +08:00
|
|
|
void start( OutputIterator* outIterator, WriteType type );
|
2011-05-16 16:50:59 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
void compress( std::ostream* ostream );
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
// Schema handlers
|
|
|
|
void writeSchema( std::ostream& fout );
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
// Exception handlers
|
|
|
|
inline void throwException( const std::string& msg );
|
|
|
|
const OutputException* getException() const { return _exception.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2012-04-05 21:53:47 +08:00
|
|
|
// Property & mask variables
|
|
|
|
ObjectProperty PROPERTY;
|
|
|
|
ObjectMark BEGIN_BRACKET;
|
|
|
|
ObjectMark END_BRACKET;
|
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
protected:
|
|
|
|
template<typename T>
|
2010-02-11 01:03:09 +08:00
|
|
|
void writeArrayImplementation( const T*, int write_size, unsigned int numInRow=1 );
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-09-24 00:12:05 +08:00
|
|
|
unsigned int findOrCreateArrayID( const osg::Array* array, bool& newID );
|
|
|
|
unsigned int findOrCreateObjectID( const osg::Object* obj, bool& newID );
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
ArrayMap _arrayMap;
|
|
|
|
ObjectMap _objectMap;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2013-06-24 17:02:32 +08:00
|
|
|
typedef std::map<std::string, int> VersionMap;
|
|
|
|
VersionMap _domainVersionMap;
|
2010-01-21 04:13:33 +08:00
|
|
|
WriteImageHint _writeImageHint;
|
2013-06-24 17:02:32 +08:00
|
|
|
bool _useSchemaData;
|
|
|
|
bool _useRobustBinaryFormat;
|
|
|
|
|
|
|
|
typedef std::map<std::string, std::string> SchemaMap;
|
|
|
|
SchemaMap _inbuiltSchemaMap;
|
2010-01-28 01:09:05 +08:00
|
|
|
std::vector<std::string> _fields;
|
|
|
|
std::string _schemaName;
|
2010-01-21 04:13:33 +08:00
|
|
|
std::string _compressorName;
|
|
|
|
std::stringstream _compressSource;
|
2010-01-28 01:09:05 +08:00
|
|
|
osg::ref_ptr<OutputIterator> _out;
|
|
|
|
osg::ref_ptr<OutputException> _exception;
|
2010-02-26 01:53:51 +08:00
|
|
|
osg::ref_ptr<const osgDB::Options> _options;
|
2010-01-21 04:13:33 +08:00
|
|
|
};
|
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
void OutputStream::throwException( const std::string& msg )
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
2010-01-28 01:09:05 +08:00
|
|
|
_exception = new OutputException(_fields, msg);
|
2010-01-21 04:13:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|