Added support for vector and map containers in osgDB::Serailizer's and lua plugin.

This commit is contained in:
Robert Osfield 2014-02-26 08:26:51 +00:00
parent 4ef5d9eb5f
commit 69e9f2c973
8 changed files with 618 additions and 294 deletions

View File

@ -104,6 +104,9 @@ class OSG_EXPORT TransferFunction1D : public osg::TransferFunction
typedef std::map<float, osg::Vec4> ColorMap; typedef std::map<float, osg::Vec4> ColorMap;
/** set the color map and automatically update the image to make sure they are in sync.*/
void setColorMap(const ColorMap& vcm) { assign(vcm); }
/** Get the color map that stores the mapping between the the transfer function value and the colour it maps to.*/ /** Get the color map that stores the mapping between the the transfer function value and the colour it maps to.*/
ColorMap& getColorMap() { return _colorMap; } ColorMap& getColorMap() { return _colorMap; }

View File

@ -195,6 +195,13 @@ inline Object* getUserObject(osg::Object* object, const std::string& name)
return udc ? udc->getUserObject(name) : 0; return udc ? udc->getUserObject(name) : 0;
} }
/** Convinience function for getting the User Object associated with specificed name from an Object's UserDataContainer.*/
inline const Object* getUserObject(const osg::Object* object, const std::string& name)
{
const osg::UserDataContainer* udc = object->getUserDataContainer();
return udc ? udc->getUserObject(name) : 0;
}
} }

View File

@ -249,11 +249,11 @@ inline bool PropertyInterface::getProperty(const osg::Object* object, const std:
if (copyPropertyObjectFromObject(object, propertyName, &value, sizeof(ObjectPtr), getTypeEnum<ObjectPtr>())) return true; if (copyPropertyObjectFromObject(object, propertyName, &value, sizeof(ObjectPtr), getTypeEnum<ObjectPtr>())) return true;
else else
{ {
OSG_NOTICE<<"PropertyInterface::getProperty("<<propertyName<<", Checking UserDataContainer for object ptr"<<std::endl; OSG_INFO<<"PropertyInterface::getProperty("<<propertyName<<", Checking UserDataContainer for object ptr"<<std::endl;
const osg::UserDataContainer* udc = object->getUserDataContainer(); const osg::UserDataContainer* udc = object->getUserDataContainer();
if (udc) if (udc)
{ {
OSG_NOTICE<<" Checking UserDataContainer for object ptr"<<std::endl; OSG_INFO<<" Checking UserDataContainer for object ptr"<<std::endl;
const osg::Object* ptr = udc->getUserObject(propertyName); const osg::Object* ptr = udc->getUserObject(propertyName);
if (ptr) if (ptr)
{ {
@ -279,13 +279,13 @@ inline bool PropertyInterface::setProperty(osg::Object* object, const std::strin
const osg::Object* outgoingObject = udc->getUserObject(objectIndex); const osg::Object* outgoingObject = udc->getUserObject(objectIndex);
if (outgoingObject==value) return true; if (outgoingObject==value) return true;
OSG_NOTICE<<"PropertyInterface::setProperty("<<propertyName<<", "<<value->className()<<") replace object on UserDataContainer"<<std::endl; OSG_INFO<<"PropertyInterface::setProperty("<<propertyName<<", "<<value->className()<<") replace object on UserDataContainer"<<std::endl;
value->setName(propertyName); value->setName(propertyName);
udc->setUserObject(objectIndex, value); udc->setUserObject(objectIndex, value);
} }
else else
{ {
OSG_NOTICE<<"PropertyInterface::setProperty("<<propertyName<<", "<<value->className()<<") Adding object to UserDataContainer"<<std::endl; OSG_INFO<<"PropertyInterface::setProperty("<<propertyName<<", "<<value->className()<<") Adding object to UserDataContainer"<<std::endl;
value->setName(propertyName); value->setName(propertyName);
udc->addUserObject(value); udc->addUserObject(value);
} }

View File

@ -136,7 +136,7 @@ public:
RW_VEC4B, RW_VEC4UB, RW_VEC4S, RW_VEC4US, RW_VEC4I, RW_VEC4UI, RW_VEC4B, RW_VEC4UB, RW_VEC4S, RW_VEC4US, RW_VEC4I, RW_VEC4UI,
RW_BOUNDINGBOXF, RW_BOUNDINGBOXD, RW_BOUNDINGBOXF, RW_BOUNDINGBOXD,
RW_BOUNDINGSPHEREF, RW_BOUNDINGSPHERED, RW_BOUNDINGSPHEREF, RW_BOUNDINGSPHERED,
RW_VECTOR RW_VECTOR, RW_MAP
}; };
BaseSerializer() : _firstVersion(0), _lastVersion(INT_MAX) {} BaseSerializer() : _firstVersion(0), _lastVersion(INT_MAX) {}
@ -789,9 +789,11 @@ class VectorBaseSerializer : public BaseSerializer
{ {
public: public:
VectorBaseSerializer(BaseSerializer::Type elementType):_elementType(elementType) {} VectorBaseSerializer(BaseSerializer::Type elementType, unsigned int elementSize):
_elementType(elementType),_elementSize(elementSize) {}
Type getElementType() const { return _elementType; } Type getElementType() const { return _elementType; }
unsigned int getElementSize() const { return _elementSize; }
virtual unsigned int size(const osg::Object& obj) const { return 0; } virtual unsigned int size(const osg::Object& obj) const { return 0; }
virtual void resize(osg::Object& obj, unsigned int numElements) const {} virtual void resize(osg::Object& obj, unsigned int numElements) const {}
@ -805,6 +807,7 @@ public:
protected: protected:
Type _elementType; Type _elementType;
unsigned int _elementSize;
}; };
@ -819,7 +822,7 @@ public:
typedef void (C::*Setter)( const P& ); typedef void (C::*Setter)( const P& );
VectorSerializer( const char* name, ConstGetter cgf, Getter gf, Setter sf, BaseSerializer::Type elementType, unsigned int numElementsOnRow): VectorSerializer( const char* name, ConstGetter cgf, Getter gf, Setter sf, BaseSerializer::Type elementType, unsigned int numElementsOnRow):
VectorBaseSerializer(elementType), VectorBaseSerializer(elementType, sizeof(ValueType)),
_name(name), _name(name),
_constgetter(cgf), _getter(gf), _setter(sf), _constgetter(cgf), _getter(gf), _setter(sf),
_numElementsOnRow(numElementsOnRow) {} _numElementsOnRow(numElementsOnRow) {}
@ -984,7 +987,7 @@ public:
typedef typename C::const_iterator ConstIterator; typedef typename C::const_iterator ConstIterator;
IsAVectorSerializer( const char* name, BaseSerializer::Type elementType, unsigned int numElementsOnRow) : IsAVectorSerializer( const char* name, BaseSerializer::Type elementType, unsigned int numElementsOnRow) :
VectorBaseSerializer(elementType), VectorBaseSerializer(elementType, sizeof(ValueType)),
_name(name), _name(name),
_numElementsOnRow(numElementsOnRow) {} _numElementsOnRow(numElementsOnRow) {}
@ -1124,6 +1127,153 @@ public:
unsigned int _numElementsOnRow; unsigned int _numElementsOnRow;
}; };
class MapBaseSerializer : public BaseSerializer
{
public:
MapBaseSerializer(BaseSerializer::Type keyType, unsigned int keySize, BaseSerializer::Type elementType, unsigned int elementSize):
_keyType(keyType), _keySize(keySize),
_elementType(elementType),_elementSize(elementSize) {}
Type getKeyType() const { return _keyType; }
unsigned int getKeySize() const { return _keySize; }
Type getElementType() const { return _elementType; }
unsigned int getElementSize() const { return _elementSize; }
virtual void clear(osg::Object& obj) const {}
virtual void setElement(osg::Object& obj, void* ptrKey, void* ptrValue) const {}
virtual void* getElement(osg::Object& obj, void* ptrKey) const { return 0; }
virtual const void* getElement(const osg::Object& obj, void* ptrKey) const { return 0; }
protected:
Type _keyType;
unsigned int _keySize;
Type _elementType;
unsigned int _elementSize;
};
template<typename C, typename P>
class MapSerializer : public MapBaseSerializer
{
public:
typedef typename P::value_type ValueType;
typedef typename P::key_type KeyType;
typedef typename P::mapped_type ElementType;
typedef typename P::const_iterator ConstIterator;
typedef P& (C::*Getter)();
typedef const P& (C::*ConstGetter)() const;
typedef void (C::*Setter)( const P& );
MapSerializer( const char* name, ConstGetter cgf, Getter gf, Setter sf, BaseSerializer::Type keyType, BaseSerializer::Type elementType):
MapBaseSerializer(keyType, sizeof(KeyType), elementType, sizeof(ElementType)),
_name(name),
_constgetter(cgf), _getter(gf), _setter(sf) {}
virtual const std::string& getName() const { return _name; }
virtual void clear(osg::Object& obj) const
{
C& object = OBJECT_CAST<C&>(obj);
P& map = (object.*_getter)();
map.clear();
}
virtual void setElement(osg::Object& obj, void* ptrKey, void* ptrValue) const
{
C& object = OBJECT_CAST<C&>(obj);
P& map = (object.*_getter)();
map[*reinterpret_cast<KeyType*>(ptrKey)] = *reinterpret_cast<ElementType*>(ptrValue);
}
virtual void* getElement(osg::Object& obj, void* ptrKey) const
{
C& object = OBJECT_CAST<C&>(obj);
P& map = (object.*_getter)();
return &(map[*reinterpret_cast<KeyType*>(ptrKey)]);
}
virtual const void* getElement(const osg::Object& obj, void* ptrKey) const
{
const C& object = OBJECT_CAST<const C&>(obj);
const P& map = (object.*_constgetter)();
ConstIterator itr = map.find(*reinterpret_cast<KeyType*>(ptrKey));
if (itr==map.end()) return 0;
else return &(itr->second);
}
virtual bool read( InputStream& is, osg::Object& obj )
{
C& object = OBJECT_CAST<C&>(obj);
unsigned int size = 0;
P map;
if ( is.isBinary() )
{
is >> size;
for ( unsigned int i=0; i<size; ++i )
{
KeyType key;
ElementType value;
is >> key >> value;
map[key] = value;
}
(object.*_setter)( map );
}
else if ( is.matchString(_name) )
{
is >> size;
if ( size>0 )
{
is >> is.BEGIN_BRACKET;
for ( unsigned int i=0; i<size; ++i )
{
KeyType key;
ElementType value;
is >> key >> value;
map[key] = value;
}
is >> is.END_BRACKET;
}
(object.*_setter)( map );
}
return true;
}
virtual bool write( OutputStream& os, const osg::Object& obj )
{
const C& object = OBJECT_CAST<const C&>(obj);
const P& map = (object.*_constgetter)();
unsigned int size = (unsigned int)map.size();
if ( os.isBinary() )
{
os << size;
for ( ConstIterator itr=map.begin();
itr!=map.end(); ++itr )
{
os << itr->first << itr->second;
}
}
else if ( size>0 )
{
os << os.PROPERTY((_name).c_str()) << size << os.BEGIN_BRACKET << std::endl;
for ( ConstIterator itr=map.begin(); itr!=map.end(); ++itr )
{
os << itr->first << itr->second; os << std::endl;
}
os << os.END_BRACKET << std::endl;
}
return true;
}
public:
std::string _name;
ConstGetter _constgetter;
Getter _getter;
Setter _setter;
};
// ADDING MANIPULATORS // ADDING MANIPULATORS
#define ADD_SERIALIZER(S) \ #define ADD_SERIALIZER(S) \
wrapper->addSerializer( (S) ) wrapper->addSerializer( (S) )

View File

@ -242,6 +242,7 @@ PropertyInterface::PropertyInterface():
TYPENAME(LIST) TYPENAME(LIST)
TYPENAME(VECTOR) TYPENAME(VECTOR)
TYPENAME(MAP)
} }
@ -380,7 +381,7 @@ bool PropertyInterface::copyPropertyDataToObject(osg::Object* object, const std:
} }
else else
{ {
OSG_NOTICE<<"PropertyInterface::copyPropertyDataFromObject() no serializer available."<<std::endl; OSG_INFO<<"PropertyInterface::copyPropertyDataFromObject() no serializer available."<<std::endl;
return false; return false;
} }
} }
@ -403,7 +404,7 @@ bool PropertyInterface::copyPropertyObjectFromObject(const osg::Object* object,
} }
else else
{ {
OSG_NOTICE<<"PropertyInterface::copyPropertyObjectFromObject() no serializer available."<<std::endl; OSG_INFO<<"PropertyInterface::copyPropertyObjectFromObject() no serializer available."<<std::endl;
return false; return false;
} }
} }
@ -426,7 +427,7 @@ bool PropertyInterface::copyPropertyObjectToObject(osg::Object* object, const st
} }
else else
{ {
OSG_NOTICE<<"PropertyInterface::copyPropertyObjectToObject() no serializer available."<<std::endl; OSG_INFO<<"PropertyInterface::copyPropertyObjectToObject() no serializer available."<<std::endl;
return false; return false;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -31,8 +31,11 @@ class LuaScriptEngine;
struct SerializerScratchPad : public osg::Referenced struct SerializerScratchPad : public osg::Referenced
{ {
SerializerScratchPad(unsigned int s=256):dataType(osgDB::BaseSerializer::RW_UNDEFINED),dataSize(0) { maxDataSize = s; data = new char[s]; } SerializerScratchPad(unsigned int s=256) : deleteData(true), dataType(osgDB::BaseSerializer::RW_UNDEFINED), dataSize(0) { maxDataSize = s; data = new char[s]; }
SerializerScratchPad(osgDB::BaseSerializer::Type type, const void* ptr, unsigned int s) : deleteData(false), maxDataSize(s), data(const_cast<char*>(reinterpret_cast<const char*>(ptr))), dataType(type),dataSize(s) {}
virtual ~SerializerScratchPad() { if (deleteData && data) delete [] data; }
bool deleteData;
unsigned int maxDataSize; unsigned int maxDataSize;
char* data; char* data;
@ -98,46 +101,50 @@ class LuaScriptEngine : public osg::ScriptEngine
osgDB::PropertyInterface& getPropertyInterface() const { return _pi; } osgDB::PropertyInterface& getPropertyInterface() const { return _pi; }
int pushDataToStack(SerializerScratchPad* ssp) const; int pushDataToStack(SerializerScratchPad* ssp) const;
int popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSerializer::Type type) const; int getDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSerializer::Type type, int pos) const;
int pushPropertyToStack(osg::Object* object, const std::string& propertyName) const; int pushPropertyToStack(osg::Object* object, const std::string& propertyName) const;
int setPropertyFromStack(osg::Object* object, const std::string& propertyName) const; int setPropertyFromStack(osg::Object* object, const std::string& propertyName) const;
bool loadScript(osg::Script* script); bool loadScript(osg::Script* script);
osgDB::BaseSerializer::Type getType() const; int getAbsolutePos(int pos) const { return (pos<0) ? (lua_gettop(_lua)+pos+1) : pos; }
bool getfields(const char* f1, const char* f2, int type) const; osgDB::BaseSerializer::Type getType(int pos) const;
bool getfields(const char* f1, const char* f2, const char* f3, int type) const;
bool getfields(const char* f1, const char* f2, const char* f3, const char* f4, int type) const;
bool getfields(const char* f1, const char* f2, const char* f3, const char* f4, const char* f5, const char* f6, int type) const;
bool getelements(int numElements, int type) const;
bool getvec2() const; bool getfields(int pos, const char* f1, const char* f2, int type) const;
bool getvec3() const; bool getfields(int pos, const char* f1, const char* f2, const char* f3, int type) const;
bool getvec4() const; bool getfields(int pos, const char* f1, const char* f2, const char* f3, const char* f4, int type) const;
bool getmatrix() const; bool getfields(int pos, const char* f1, const char* f2, const char* f3, const char* f4, const char* f5, const char* f6, int type) const;
bool getboundingbox() const; bool getelements(int pos, int numElements, int type) const;
bool getboundingsphere() const;
bool getValue(osg::Vec2f& value) const; bool getvec2(int pos) const;
bool getValue(osg::Vec3f& value) const; bool getvec3(int pos) const;
bool getValue(osg::Vec4f& value) const; bool getvec4(int pos) const;
bool getmatrix(int pos) const;
bool getboundingbox(int pos) const;
bool getboundingsphere(int pos) const;
bool getValue(osg::Vec2d& value) const; bool getValue(int pos, osg::Vec2f& value) const;
bool getValue(osg::Vec3d& value) const; bool getValue(int pos, osg::Vec3f& value) const;
bool getValue(osg::Vec4d& value) const; bool getValue(int pos, osg::Vec4f& value) const;
bool getValue(osg::Quat& value) const;
bool getValue(osg::Plane& value) const;
bool getValue(osg::Matrixf& value) const; bool getValue(int pos, osg::Vec2d& value) const;
bool getValue(osg::Matrixd& value) const; bool getValue(int pos, osg::Vec3d& value) const;
bool getValue(int pos, osg::Vec4d& value) const;
bool getValue(int pos, osg::Quat& value) const;
bool getValue(int pos, osg::Plane& value) const;
bool getValue(osg::BoundingBoxf& value) const; bool getValue(int pos, osg::Matrixf& value) const;
bool getValue(osg::BoundingBoxd& value) const; bool getValue(int pos, osg::Matrixd& value) const;
bool getValue(osg::BoundingSpheref& value) const; bool getValue(int pos, osg::BoundingBoxf& value) const;
bool getValue(osg::BoundingSphered& value) const; bool getValue(int pos, osg::BoundingBoxd& value) const;
bool getValue(int pos, osg::BoundingSpheref& value) const;
bool getValue(int pos, osg::BoundingSphered& value) const;
void pushValue(osgDB::BaseSerializer::Type type, const void* ptr) const;
void pushValue(const osg::Vec2f& value) const; void pushValue(const osg::Vec2f& value) const;
void pushValue(const osg::Vec3f& value) const; void pushValue(const osg::Vec3f& value) const;

View File

@ -3,6 +3,8 @@
#include <osgDB/InputStream> #include <osgDB/InputStream>
#include <osgDB/OutputStream> #include <osgDB/OutputStream>
#if 0
static bool checkColorMap( const osg::TransferFunction1D& func ) static bool checkColorMap( const osg::TransferFunction1D& func )
{ {
return func.getColorMap().size()>0; return func.getColorMap().size()>0;
@ -34,11 +36,21 @@ static bool writeColorMap( osgDB::OutputStream& os, const osg::TransferFunction1
os << os.END_BRACKET << std::endl; os << os.END_BRACKET << std::endl;
return true; return true;
} }
#endif
#define ADD_MAP_SERIALIZER(PROP, TYPE, KEYTYPE, ELEMENTTYPE) \
wrapper->addSerializer( new osgDB::MapSerializer< MyClass, TYPE >( \
#PROP, &MyClass::get##PROP, &MyClass::get##PROP, &MyClass::set##PROP, KEYTYPE, ELEMENTTYPE), osgDB::BaseSerializer::RW_MAP )
REGISTER_OBJECT_WRAPPER( TransferFunction1D, REGISTER_OBJECT_WRAPPER( TransferFunction1D,
new osg::TransferFunction1D, new osg::TransferFunction1D,
osg::TransferFunction1D, osg::TransferFunction1D,
"osg::Object osg::TransferFunction osg::TransferFunction1D" ) "osg::Object osg::TransferFunction osg::TransferFunction1D" )
{ {
#if 0
ADD_USER_SERIALIZER( ColorMap ); // _colorMap ADD_USER_SERIALIZER( ColorMap ); // _colorMap
#else
ADD_MAP_SERIALIZER(ColorMap, osg::TransferFunction1D::ColorMap, osgDB::BaseSerializer::RW_FLOAT, osgDB::BaseSerializer::RW_VEC4F);
#endif
} }