From 31e98b51dfef8e28db7bb9cf1f494e65f94d32be Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 10 Feb 2014 16:44:13 +0000 Subject: [PATCH] Added support for serailizing and scripting BoundingBox and BoundingSphere objects --- include/osg/BoundingBox | 3 + include/osg/BoundingSphere | 3 + include/osg/ValueObject | 14 + include/osgDB/InputStream | 6 + include/osgDB/OutputStream | 6 + include/osgDB/PropertyInterface | 6 + include/osgDB/Serializer | 49 ++-- include/osgGA/Widget | 6 +- src/osgDB/InputStream.cpp | 26 ++ src/osgDB/OutputStream.cpp | 12 + src/osgDB/PropertyInterface.cpp | 11 + src/osgGA/Widget.cpp | 2 +- src/osgPlugins/lua/LuaScriptEngine.cpp | 290 +++++++++++++++++-- src/osgPlugins/lua/LuaScriptEngine.h | 21 +- src/osgWrappers/serializers/osgGA/Widget.cpp | 3 + 15 files changed, 405 insertions(+), 53 deletions(-) diff --git a/include/osg/BoundingBox b/include/osg/BoundingBox index c3c4c0ba5..f3e21e039 100644 --- a/include/osg/BoundingBox +++ b/include/osg/BoundingBox @@ -73,6 +73,9 @@ class BoundingBoxImpl -FLT_MAX); } + inline bool operator == (const BoundingBoxImpl& rhs) const { return _min==rhs._min && _max==rhs._max; } + inline bool operator != (const BoundingBoxImpl& rhs) const { return _min!=rhs._min || _max!=rhs._max; } + /** Returns true if the bounding box extents are valid, false otherwise. */ inline bool valid() const { diff --git a/include/osg/BoundingSphere b/include/osg/BoundingSphere index 722899759..c0afdcf8f 100644 --- a/include/osg/BoundingSphere +++ b/include/osg/BoundingSphere @@ -63,6 +63,9 @@ class BoundingSphereImpl * otherwise. */ inline bool valid() const { return _radius>=0.0; } + inline bool operator == (const BoundingSphereImpl& rhs) const { return _center==rhs._center && _radius==rhs._radius; } + inline bool operator != (const BoundingSphereImpl& rhs) const { return _center!=rhs._center || _radius==rhs._radius; } + /** Set the bounding sphere to the given center/radius using floats. */ inline void set(const vec_type& center,value_type radius) { diff --git a/include/osg/ValueObject b/include/osg/ValueObject index 8861d2a1c..7c1d76398 100644 --- a/include/osg/ValueObject +++ b/include/osg/ValueObject @@ -16,6 +16,8 @@ #include #include +#include +#include namespace osg { @@ -65,6 +67,10 @@ class ValueObject : public Object virtual void apply(const osg::Plane& /*value*/) {} virtual void apply(const osg::Matrixf& /*value*/) {} virtual void apply(const osg::Matrixd& /*value*/) {} + virtual void apply(const osg::BoundingBoxf& /*value*/) {} + virtual void apply(const osg::BoundingBoxd& /*value*/) {} + virtual void apply(const osg::BoundingSpheref& /*value*/) {} + virtual void apply(const osg::BoundingSphered& /*value*/) {} }; class SetValueVisitor @@ -91,6 +97,10 @@ class ValueObject : public Object virtual void apply(osg::Plane& /*value*/) {} virtual void apply(osg::Matrixf& /*value*/) {} virtual void apply(osg::Matrixd& /*value*/) {} + virtual void apply(osg::BoundingBoxf& /*value*/) {} + virtual void apply(osg::BoundingBoxd& /*value*/) {} + virtual void apply(osg::BoundingSpheref& /*value*/) {} + virtual void apply(osg::BoundingSphered& /*value*/) {} }; virtual bool get(GetValueVisitor& /*gvv*/) const { return false; } @@ -168,6 +178,10 @@ META_ValueObject(Quat, QuatValueObject) META_ValueObject(Plane, PlaneValueObject) META_ValueObject(Matrixf, MatrixfValueObject) META_ValueObject(Matrixd, MatrixdValueObject) +META_ValueObject(BoundingBoxf, BoundingBoxfValueObject) +META_ValueObject(BoundingBoxd, BoundingBoxdValueObject) +META_ValueObject(BoundingSpheref, BoundingSpherefValueObject) +META_ValueObject(BoundingSphered, BoundingSpheredValueObject) /** provide implementation of osg::Object::getUserValue(..) template*/ template diff --git a/include/osgDB/InputStream b/include/osgDB/InputStream index 7eb714b0b..2baf2d2e9 100644 --- a/include/osgDB/InputStream +++ b/include/osgDB/InputStream @@ -27,6 +27,8 @@ #include #include #include +#include +#include #include #include #include @@ -130,6 +132,10 @@ public: InputStream& operator>>( osg::Plane& p ); InputStream& operator>>( osg::Matrixf& mat ); InputStream& operator>>( osg::Matrixd& mat ); + InputStream& operator>>( osg::BoundingBoxf& bb ); + InputStream& operator>>( osg::BoundingBoxd& bb ); + InputStream& operator>>( osg::BoundingSpheref& bs ); + InputStream& operator>>( osg::BoundingSphered& bs ); InputStream& operator>>( osg::Array*& a ) { a = readArray(); return *this; } InputStream& operator>>( osg::Image*& img ) { img = readImage(); return *this; } diff --git a/include/osgDB/OutputStream b/include/osgDB/OutputStream index b2f86b6c4..c2e120e74 100644 --- a/include/osgDB/OutputStream +++ b/include/osgDB/OutputStream @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #include #include @@ -135,6 +137,10 @@ public: OutputStream& operator<<( const osg::Plane& p ); OutputStream& operator<<( const osg::Matrixf& mat ); OutputStream& operator<<( const osg::Matrixd& mat ); + OutputStream& operator<<( const osg::BoundingBoxf& bb ); + OutputStream& operator<<( const osg::BoundingBoxd& bb ); + OutputStream& operator<<( const osg::BoundingSpheref& bb ); + OutputStream& operator<<( const osg::BoundingSphered& bb ); OutputStream& operator<<( const osg::Array* a ) { writeArray(a); return *this; } OutputStream& operator<<( const osg::Image* img ) { writeImage(img); return *this; } diff --git a/include/osgDB/PropertyInterface b/include/osgDB/PropertyInterface index 6fd40eb68..e927aabef 100644 --- a/include/osgDB/PropertyInterface +++ b/include/osgDB/PropertyInterface @@ -105,6 +105,12 @@ DECLARE_TYPE(osg::Vec4us, VEC4US) DECLARE_TYPE(osg::Vec4i, VEC4I) DECLARE_TYPE(osg::Vec4ui, VEC4UI) +DECLARE_TYPE(osg::BoundingBoxf, BOUNDINGBOXF) +DECLARE_TYPE(osg::BoundingBoxd, BOUNDINGBOXD) + +DECLARE_TYPE(osg::BoundingSpheref, BOUNDINGSPHEREF) +DECLARE_TYPE(osg::BoundingSphered, BOUNDINGSPHERED) + // forward decalare class PropertyOutputIterator; class PropertyInputIterator; diff --git a/include/osgDB/Serializer b/include/osgDB/Serializer index 8d73848a4..bb896149e 100644 --- a/include/osgDB/Serializer +++ b/include/osgDB/Serializer @@ -133,7 +133,9 @@ public: RW_MATRIXF, RW_MATRIXD, RW_MATRIX, RW_GLENUM, RW_STRING, RW_ENUM, RW_VEC2B, RW_VEC2UB, RW_VEC2S, RW_VEC2US, RW_VEC2I, RW_VEC2UI, RW_VEC3B, RW_VEC3UB, RW_VEC3S, RW_VEC3US, RW_VEC3I, RW_VEC3UI, - 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_BOUNDINGSPHEREF, RW_BOUNDINGSPHERED }; BaseSerializer() : _firstVersion(0), _lastVersion(INT_MAX) {} @@ -300,8 +302,7 @@ public: if ( is.isBinary() ) { is >> value; - if ( ParentType::_defaultValue!=value ) - (object.*_setter)( value ); + (object.*_setter)( value ); } else if ( is.matchString(ParentType::_name) ) { @@ -349,8 +350,7 @@ public: if ( is.isBinary() ) { readMatrixImplementation( is, value ); - if ( ParentType::_defaultValue!=value ) - (object.*_setter)( value ); + (object.*_setter)( value ); } else if ( is.matchString(ParentType::_name) ) { @@ -417,8 +417,7 @@ public: if ( is.isBinary() ) { GLenum value; is >> value; - if ( ParentType::_defaultValue!=static_cast

(value) ) - (object.*_setter)( static_cast

(value) ); + (object.*_setter)( static_cast

(value) ); } else if ( is.matchString(ParentType::_name) ) { @@ -466,8 +465,7 @@ public: if ( is.isBinary() ) { is >> value; - if ( ParentType::_defaultValue!=value ) - (object.*_setter)( value ); + (object.*_setter)( value ); } else if ( is.matchString(ParentType::_name) ) { @@ -524,8 +522,7 @@ public: if ( hasObject ) { P* value = dynamic_cast( is.readObject() ); - if ( ParentType::_defaultValue!=value ) - (object.*_setter)( value ); + (object.*_setter)( value ); } } else if ( is.matchString(ParentType::_name) ) @@ -535,8 +532,7 @@ public: { is >> is.BEGIN_BRACKET; P* value = dynamic_cast( is.readObject() ); - if ( ParentType::_defaultValue!=value ) - (object.*_setter)( value ); + (object.*_setter)( value ); is >> is.END_BRACKET; } } @@ -596,8 +592,7 @@ public: if ( hasObject ) { P* value = dynamic_cast( is.readImage() ); - if ( ParentType::_defaultValue!=value ) - (object.*_setter)( value ); + (object.*_setter)( value ); } } else if ( is.matchString(ParentType::_name) ) @@ -607,8 +602,7 @@ public: { is >> is.BEGIN_BRACKET; P* value = dynamic_cast( is.readImage() ); - if ( ParentType::_defaultValue!=value ) - (object.*_setter)( value ); + (object.*_setter)( value ); is >> is.END_BRACKET; } } @@ -673,8 +667,7 @@ public: if ( is.isBinary() ) { is >> value; - if ( ParentType::_defaultValue!=static_cast

(value) ) - (object.*_setter)( static_cast

(value) ); + (object.*_setter)( static_cast

(value) ); } else if ( is.matchString(ParentType::_name) ) { @@ -1019,6 +1012,24 @@ public: wrapper->addSerializer( new osgDB::MatrixSerializer< MyClass >( \ #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_MATRIX ) + +#define ADD_BOUNDINGBOXF_SERIALIZER(PROP, DEF) \ + wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::BoundingBoxf >( \ + #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_BOUNDINGBOXF ) + +#define ADD_BOUNDINGBOXD_SERIALIZER(PROP, DEF) \ + wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::BoundingBoxd >( \ + #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_BOUNDINGBOXD ) + +#define ADD_BOUNDINGSPHEREF_SERIALIZER(PROP, DEF) \ + wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::BoundingSpheref >( \ + #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_BOUNDINGSPHEREF ) + +#define ADD_BOUNDINGSPHERED_SERIALIZER(PROP, DEF) \ + wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::BoundingSphered >( \ + #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_BOUNDINGSPHERED ) + + #define ADD_GLENUM_SERIALIZER(PROP, TYPE, DEF) \ wrapper->addSerializer( new osgDB::GLenumSerializer< MyClass, TYPE >( \ #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_GLENUM ) diff --git a/include/osgGA/Widget b/include/osgGA/Widget index ad19079be..5f26651e9 100644 --- a/include/osgGA/Widget +++ b/include/osgGA/Widget @@ -42,8 +42,8 @@ public: virtual void createGraphics(); virtual void createGraphicsImplementation(); - virtual void setExtents(const osg::BoundingBox& bb); - const osg::BoundingBox& getExtents() const { return _extents; } + virtual void setExtents(const osg::BoundingBoxf& bb); + const osg::BoundingBoxf& getExtents() const { return _extents; } enum FocusBehaviour { @@ -82,7 +82,7 @@ protected: bool _hasEventFocus; bool _graphicsInitialized; - osg::BoundingBox _extents; + osg::BoundingBoxf _extents; }; } diff --git a/src/osgDB/InputStream.cpp b/src/osgDB/InputStream.cpp index cbce77c23..4ef207dce 100644 --- a/src/osgDB/InputStream.cpp +++ b/src/osgDB/InputStream.cpp @@ -289,6 +289,32 @@ InputStream& InputStream::operator>>( osg::Matrixd& mat ) } #endif +InputStream& InputStream::operator>>( osg::BoundingBoxf& bb) +{ + float p0, p1, p2, p3, p4, p5; *this >> p0 >> p1 >> p2 >> p3>> p4>> p5; + bb.set( p0, p1, p2, p3, p4, p5 ); return *this; +} + +InputStream& InputStream::operator>>( osg::BoundingBoxd& bb) +{ + double p0, p1, p2, p3, p4, p5; *this >> p0 >> p1 >> p2 >> p3>> p4>> p5; + bb.set( p0, p1, p2, p3, p4, p5 ); return *this; +} + +InputStream& InputStream::operator>>( osg::BoundingSpheref& bs) +{ + float p0, p1, p2, p3; *this >> p0 >> p1 >> p2 >> p3; + bs.set( osg::Vec3f(p0, p1, p2), p3 ); return *this; +} + +InputStream& InputStream::operator>>( osg::BoundingSphered& bs) +{ + double p0, p1, p2, p3; *this >> p0 >> p1 >> p2 >> p3; + bs.set( osg::Vec3d(p0, p1, p2), p3 ); return *this; +} + + + osg::Array* InputStream::readArray() { osg::ref_ptr array = NULL; diff --git a/src/osgDB/OutputStream.cpp b/src/osgDB/OutputStream.cpp index a568fe969..bf4ee4e62 100644 --- a/src/osgDB/OutputStream.cpp +++ b/src/osgDB/OutputStream.cpp @@ -160,6 +160,18 @@ OutputStream& OutputStream::operator<<( const osg::Plane& p ) { *this << (double)p[0] << (double)p[1] << (double)p[2] << (double)p[3]; return *this; } +OutputStream& OutputStream::operator<<( const osg::BoundingBoxf& bb) +{ *this << bb.xMin() << bb.yMin() << bb.zMin() << bb.xMax() << bb.yMax() << bb.zMax(); return *this; } + +OutputStream& OutputStream::operator<<( const osg::BoundingBoxd& bb) +{ *this << bb.xMin() << bb.yMin() << bb.zMin() << bb.xMax() << bb.yMax() << bb.zMax(); return *this; } + +OutputStream& OutputStream::operator<<( const osg::BoundingSpheref& bs) +{ *this << bs.center().x() << bs.center().y() << bs.center().z() << bs.radius(); return *this; } + +OutputStream& OutputStream::operator<<( const osg::BoundingSphered& bs) +{ *this << bs.center().x() << bs.center().y() << bs.center().z() << bs.radius(); return *this; } + #if 0 OutputStream& OutputStream::operator<<( const osg::Matrixf& mat ) { diff --git a/src/osgDB/PropertyInterface.cpp b/src/osgDB/PropertyInterface.cpp index b56ef3626..111b1e4f0 100644 --- a/src/osgDB/PropertyInterface.cpp +++ b/src/osgDB/PropertyInterface.cpp @@ -208,6 +208,13 @@ PropertyInterface::PropertyInterface(): TYPENAME(MATRIXF) TYPENAME(MATRIXD) TYPENAME(MATRIX) + + TYPENAME(BOUNDINGBOXF) + TYPENAME(BOUNDINGBOXD) + + TYPENAME(BOUNDINGSPHEREF) + TYPENAME(BOUNDINGSPHERED) + TYPENAME(GLENUM) TYPENAME(STRING) TYPENAME(ENUM) @@ -439,6 +446,10 @@ public: virtual void apply(const osg::Plane& /*value*/) { type = osgDB::BaseSerializer::RW_PLANE; } virtual void apply(const osg::Matrixf& /*value*/) { type = osgDB::BaseSerializer::RW_MATRIXF; } virtual void apply(const osg::Matrixd& /*value*/) { type = osgDB::BaseSerializer::RW_MATRIXD; } + virtual void apply(const osg::BoundingBoxf& /*value*/) { type = osgDB::BaseSerializer::RW_BOUNDINGBOXF; } + virtual void apply(const osg::BoundingBoxd& /*value*/) { type = osgDB::BaseSerializer::RW_BOUNDINGBOXD; } + virtual void apply(const osg::BoundingSpheref& /*value*/) { type = osgDB::BaseSerializer::RW_BOUNDINGSPHEREF; } + virtual void apply(const osg::BoundingSphered& /*value*/) { type = osgDB::BaseSerializer::RW_BOUNDINGSPHERED; } }; bool PropertyInterface::getPropertyType(const osg::Object* object, const std::string& propertyName, osgDB::BaseSerializer::Type& type) const diff --git a/src/osgGA/Widget.cpp b/src/osgGA/Widget.cpp index e4debd34a..f285f96cb 100644 --- a/src/osgGA/Widget.cpp +++ b/src/osgGA/Widget.cpp @@ -42,7 +42,7 @@ Widget::Widget(const Widget& widget, const osg::CopyOp& copyop): setNumChildrenRequiringEventTraversal(1); } -void Widget::setExtents(const osg::BoundingBox& bb) +void Widget::setExtents(const osg::BoundingBoxf& bb) { _extents = bb; } diff --git a/src/osgPlugins/lua/LuaScriptEngine.cpp b/src/osgPlugins/lua/LuaScriptEngine.cpp index 42ff1b084..afb6c4614 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.cpp +++ b/src/osgPlugins/lua/LuaScriptEngine.cpp @@ -527,8 +527,12 @@ public: virtual void apply(osg::Vec4d& value) { _lsg->getValue(value); _numberToPop = 4; } virtual void apply(osg::Quat& value) { _lsg->getValue(value); _numberToPop = 4; } virtual void apply(osg::Plane& value) { _lsg->getValue(value); _numberToPop = 4; } - virtual void apply(osg::Matrixf& value) { _lsg->getValue(value); } - virtual void apply(osg::Matrixd& value) { _lsg->getValue(value); } + virtual void apply(osg::Matrixf& value) { _lsg->getValue(value); } + virtual void apply(osg::Matrixd& value) { _lsg->getValue(value); } + virtual void apply(osg::BoundingBoxf& value) { _lsg->getValue(value); } + virtual void apply(osg::BoundingBoxd& value) { _lsg->getValue(value); } + virtual void apply(osg::BoundingSpheref& value) { _lsg->getValue(value); } + virtual void apply(osg::BoundingSphered& value) { _lsg->getValue(value); } }; int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string& propertyName) const @@ -650,19 +654,6 @@ int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string& } break; } -#ifdef OSG_USE_FLOAT_MATRIX - case(osgDB::BaseSerializer::RW_MATRIX): -#endif - case(osgDB::BaseSerializer::RW_MATRIXF): - { - osg::Matrixf value; - if (_pi.getProperty(object, propertyName, value)) - { - pushValue(value); - return 1; - } - break; - } case(osgDB::BaseSerializer::RW_VEC2D): { osg::Vec2d value; @@ -693,6 +684,19 @@ int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string& } break; } +#ifdef OSG_USE_FLOAT_MATRIX + case(osgDB::BaseSerializer::RW_MATRIX): +#endif + case(osgDB::BaseSerializer::RW_MATRIXF): + { + osg::Matrixf value; + if (_pi.getProperty(object, propertyName, value)) + { + pushValue(value); + return 1; + } + break; + } #ifndef OSG_USE_FLOAT_MATRIX case(osgDB::BaseSerializer::RW_MATRIX): #endif @@ -706,6 +710,46 @@ int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string& } break; } + case(osgDB::BaseSerializer::RW_BOUNDINGBOXF): + { + osg::BoundingBoxf value; + if (_pi.getProperty(object, propertyName, value)) + { + pushValue(value); + return 1; + } + break; + } + case(osgDB::BaseSerializer::RW_BOUNDINGBOXD): + { + osg::BoundingBoxd value; + if (_pi.getProperty(object, propertyName, value)) + { + pushValue(value); + return 1; + } + break; + } + case(osgDB::BaseSerializer::RW_BOUNDINGSPHEREF): + { + osg::BoundingSpheref value; + if (_pi.getProperty(object, propertyName, value)) + { + pushValue(value); + return 1; + } + break; + } + case(osgDB::BaseSerializer::RW_BOUNDINGSPHERED): + { + osg::BoundingSphered value; + if (_pi.getProperty(object, propertyName, value)) + { + pushValue(value); + return 1; + } + break; + } case(osgDB::BaseSerializer::RW_LIST): { OSG_NOTICE<<"Need to implement RW_LIST support"<(object); @@ -1491,6 +1701,30 @@ osg::Object* LuaScriptEngine::popParameterObject() const if (getValue(value)) object = new osg::MatrixdValueObject("", value); break; } + case(osgDB::BaseSerializer::RW_BOUNDINGBOXF): + { + osg::BoundingBoxf value; + if (getValue(value)) object = new osg::BoundingBoxfValueObject("", value); + break; + } + case(osgDB::BaseSerializer::RW_BOUNDINGBOXD): + { + osg::BoundingBoxd value; + if (getValue(value)) object = new osg::BoundingBoxdValueObject("", value); + break; + } + case(osgDB::BaseSerializer::RW_BOUNDINGSPHEREF): + { + osg::BoundingSpheref value; + if (getValue(value)) object = new osg::BoundingSpherefValueObject("", value); + break; + } + case(osgDB::BaseSerializer::RW_BOUNDINGSPHERED): + { + osg::BoundingSphered value; + if (getValue(value)) object = new osg::BoundingSpheredValueObject("", value); + break; + } case(osgDB::BaseSerializer::RW_LIST): { OSG_NOTICE<<"Need to implement RW_LIST support"< #include + struct CreateGraphics : public osgDB::MethodObject { virtual bool run(void* objectPtr, osg::Parameters&, osg::Parameters&) const @@ -77,6 +78,8 @@ REGISTER_OBJECT_WRAPPER( Widget, ADD_BOOL_SERIALIZER(HasEventFocus, false); + ADD_BOUNDINGBOXF_SERIALIZER(Extents, osg::BoundingBoxf()); + ADD_METHOD_OBJECT( "createGraphics", CreateGraphics ); ADD_METHOD_OBJECT( "createGraphicsImplementation", CreateGraphicsImplementation );