2011-06-03 06:04:08 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 2011 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSG_VALUEOBJECT
|
|
|
|
#define OSG_VALUEOBJECT 1
|
|
|
|
|
|
|
|
#include <osg/Object>
|
2011-06-09 20:57:14 +08:00
|
|
|
#include <osg/UserDataContainer>
|
2014-02-11 00:44:13 +08:00
|
|
|
#include <osg/BoundingBox>
|
|
|
|
#include <osg/BoundingSphere>
|
2011-06-03 06:04:08 +08:00
|
|
|
|
2016-02-04 21:10:28 +08:00
|
|
|
#include <typeinfo>
|
|
|
|
|
2011-06-03 06:04:08 +08:00
|
|
|
namespace osg {
|
|
|
|
|
2015-04-13 18:43:56 +08:00
|
|
|
// forward declare core OSG math classes
|
2016-07-04 20:36:28 +08:00
|
|
|
class Vec2b;
|
|
|
|
class Vec3b;
|
|
|
|
class Vec4b;
|
|
|
|
class Vec2ub;
|
|
|
|
class Vec3ub;
|
|
|
|
class Vec4ub;
|
|
|
|
|
|
|
|
class Vec2s;
|
|
|
|
class Vec3s;
|
|
|
|
class Vec4s;
|
|
|
|
class Vec2us;
|
|
|
|
class Vec3us;
|
|
|
|
class Vec4us;
|
|
|
|
|
|
|
|
class Vec2i;
|
|
|
|
class Vec3i;
|
|
|
|
class Vec4i;
|
|
|
|
class Vec2ui;
|
|
|
|
class Vec3ui;
|
|
|
|
class Vec4ui;
|
|
|
|
|
2011-06-03 06:04:08 +08:00
|
|
|
class Vec2f;
|
|
|
|
class Vec3f;
|
|
|
|
class Vec4f;
|
2016-07-04 20:36:28 +08:00
|
|
|
|
2011-06-03 06:04:08 +08:00
|
|
|
class Vec2d;
|
|
|
|
class Vec3d;
|
|
|
|
class Vec4d;
|
2016-07-04 20:36:28 +08:00
|
|
|
|
2011-06-03 06:04:08 +08:00
|
|
|
class Quat;
|
|
|
|
class Plane;
|
|
|
|
class Matrixf;
|
|
|
|
class Matrixd;
|
|
|
|
|
|
|
|
class ValueObject : public Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ValueObject() : Object(true) {}
|
|
|
|
ValueObject(const std::string& name) : Object(true) { setName(name); }
|
|
|
|
ValueObject(const ValueObject& rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY): Object(rhs,copyop) {}
|
|
|
|
|
|
|
|
META_Object(osg, ValueObject)
|
|
|
|
|
|
|
|
class GetValueVisitor
|
|
|
|
{
|
|
|
|
public:
|
2013-11-22 17:48:02 +08:00
|
|
|
virtual ~GetValueVisitor() {}
|
2013-06-28 20:00:43 +08:00
|
|
|
virtual void apply(bool /*value*/) {}
|
|
|
|
virtual void apply(char /*value*/) {}
|
|
|
|
virtual void apply(unsigned char /*value*/) {}
|
|
|
|
virtual void apply(short /*value*/) {}
|
|
|
|
virtual void apply(unsigned short /*value*/) {}
|
|
|
|
virtual void apply(int /*value*/) {}
|
|
|
|
virtual void apply(unsigned int /*value*/) {}
|
|
|
|
virtual void apply(float /*value*/) {}
|
|
|
|
virtual void apply(double /*value*/) {}
|
|
|
|
virtual void apply(const std::string& /*value*/) {}
|
2016-07-04 20:36:28 +08:00
|
|
|
|
|
|
|
virtual void apply(const osg::Vec2b& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec3b& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec4b& /*value*/) {}
|
|
|
|
|
|
|
|
virtual void apply(const osg::Vec2ub& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec3ub& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec4ub& /*value*/) {}
|
|
|
|
|
|
|
|
virtual void apply(const osg::Vec2s& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec3s& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec4s& /*value*/) {}
|
|
|
|
|
|
|
|
virtual void apply(const osg::Vec2us& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec3us& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec4us& /*value*/) {}
|
|
|
|
|
|
|
|
virtual void apply(const osg::Vec2i& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec3i& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec4i& /*value*/) {}
|
|
|
|
|
|
|
|
virtual void apply(const osg::Vec2ui& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec3ui& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec4ui& /*value*/) {}
|
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
virtual void apply(const osg::Vec2f& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec3f& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec4f& /*value*/) {}
|
2016-07-04 20:36:28 +08:00
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
virtual void apply(const osg::Vec2d& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec3d& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Vec4d& /*value*/) {}
|
2016-07-04 20:36:28 +08:00
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
virtual void apply(const osg::Quat& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Plane& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Matrixf& /*value*/) {}
|
|
|
|
virtual void apply(const osg::Matrixd& /*value*/) {}
|
2014-02-11 00:44:13 +08:00
|
|
|
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*/) {}
|
2011-06-03 06:04:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class SetValueVisitor
|
|
|
|
{
|
|
|
|
public:
|
2013-11-22 17:48:02 +08:00
|
|
|
virtual ~SetValueVisitor() {}
|
2013-06-28 20:00:43 +08:00
|
|
|
virtual void apply(bool& /*value*/) {}
|
|
|
|
virtual void apply(char& /*value*/) {}
|
|
|
|
virtual void apply(unsigned char& /*value*/) {}
|
|
|
|
virtual void apply(short& /*value*/) {}
|
|
|
|
virtual void apply(unsigned short& /*value*/) {}
|
|
|
|
virtual void apply(int& /*value*/) {}
|
|
|
|
virtual void apply(unsigned int& /*value*/) {}
|
|
|
|
virtual void apply(float& /*value*/) {}
|
|
|
|
virtual void apply(double& /*value*/) {}
|
|
|
|
virtual void apply(std::string& /*value*/) {}
|
2016-07-04 20:36:28 +08:00
|
|
|
|
|
|
|
virtual void apply(osg::Vec2b& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec3b& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec4b& /*value*/) {}
|
|
|
|
|
|
|
|
virtual void apply(osg::Vec2ub& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec3ub& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec4ub& /*value*/) {}
|
|
|
|
|
|
|
|
virtual void apply(osg::Vec2s& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec3s& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec4s& /*value*/) {}
|
|
|
|
|
|
|
|
virtual void apply(osg::Vec2us& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec3us& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec4us& /*value*/) {}
|
|
|
|
|
|
|
|
virtual void apply(osg::Vec2i& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec3i& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec4i& /*value*/) {}
|
|
|
|
|
|
|
|
virtual void apply(osg::Vec2ui& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec3ui& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec4ui& /*value*/) {}
|
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
virtual void apply(osg::Vec2f& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec3f& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec4f& /*value*/) {}
|
2016-07-04 20:36:28 +08:00
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
virtual void apply(osg::Vec2d& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec3d& /*value*/) {}
|
|
|
|
virtual void apply(osg::Vec4d& /*value*/) {}
|
2016-07-04 20:36:28 +08:00
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
virtual void apply(osg::Quat& /*value*/) {}
|
|
|
|
virtual void apply(osg::Plane& /*value*/) {}
|
|
|
|
virtual void apply(osg::Matrixf& /*value*/) {}
|
|
|
|
virtual void apply(osg::Matrixd& /*value*/) {}
|
2014-02-11 00:44:13 +08:00
|
|
|
virtual void apply(osg::BoundingBoxf& /*value*/) {}
|
|
|
|
virtual void apply(osg::BoundingBoxd& /*value*/) {}
|
|
|
|
virtual void apply(osg::BoundingSpheref& /*value*/) {}
|
|
|
|
virtual void apply(osg::BoundingSphered& /*value*/) {}
|
2011-06-03 06:04:08 +08:00
|
|
|
};
|
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
virtual bool get(GetValueVisitor& /*gvv*/) const { return false; }
|
|
|
|
virtual bool set(SetValueVisitor& /*gvv*/) { return false; }
|
2013-12-26 01:36:32 +08:00
|
|
|
|
|
|
|
protected:
|
2011-09-12 18:43:24 +08:00
|
|
|
virtual ~ValueObject() {}
|
2011-06-03 06:04:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template< typename T >
|
|
|
|
struct ValueObjectClassNameTrait
|
|
|
|
{
|
2011-07-15 17:47:34 +08:00
|
|
|
static const char* className() { return "TemplateValueObject"; }
|
2011-06-03 06:04:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template< typename T >
|
|
|
|
class TemplateValueObject : public ValueObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TemplateValueObject():
|
|
|
|
ValueObject(),
|
|
|
|
_value() {}
|
|
|
|
|
2016-02-04 21:10:28 +08:00
|
|
|
TemplateValueObject(const T& value) :
|
|
|
|
ValueObject(),
|
|
|
|
_value(value) {}
|
|
|
|
|
2011-06-03 06:04:08 +08:00
|
|
|
TemplateValueObject(const std::string& name, const T& value) :
|
|
|
|
ValueObject(name),
|
|
|
|
_value(value) {}
|
|
|
|
|
|
|
|
TemplateValueObject(const TemplateValueObject& rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY) :
|
|
|
|
ValueObject(rhs,copyop),
|
|
|
|
_value(rhs._value) {}
|
|
|
|
|
|
|
|
virtual Object* cloneType() const { return new TemplateValueObject(); }
|
|
|
|
virtual Object* clone(const CopyOp& copyop) const { return new TemplateValueObject(*this, copyop); }
|
|
|
|
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const TemplateValueObject*>(obj)!=NULL; }
|
|
|
|
virtual const char* libraryName() const { return "osg"; }
|
2011-07-15 17:47:34 +08:00
|
|
|
virtual const char* className() const { return ValueObjectClassNameTrait<T>::className(); }
|
2011-06-03 06:04:08 +08:00
|
|
|
|
|
|
|
void setValue(const T& value) { _value = value; }
|
|
|
|
const T& getValue() const { return _value; }
|
|
|
|
|
|
|
|
virtual bool get(GetValueVisitor& gvv) const { gvv.apply(_value); return true; }
|
|
|
|
virtual bool set(SetValueVisitor& svv) { svv.apply(_value); return true; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2011-09-12 18:43:24 +08:00
|
|
|
virtual ~TemplateValueObject() {}
|
2011-06-03 06:04:08 +08:00
|
|
|
static const char* s_TemplateValueObject_className;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-06-03 06:04:08 +08:00
|
|
|
T _value;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define META_ValueObject(TYPE,NAME) \
|
2011-07-15 17:47:34 +08:00
|
|
|
template<> struct ValueObjectClassNameTrait<TYPE> { static const char* className() { return #NAME; } }; \
|
2011-06-03 06:04:08 +08:00
|
|
|
typedef TemplateValueObject<TYPE> NAME;
|
|
|
|
|
|
|
|
META_ValueObject(std::string, StringValueObject)
|
|
|
|
META_ValueObject(bool, BoolValueObject)
|
|
|
|
META_ValueObject(char, CharValueObject)
|
|
|
|
META_ValueObject(unsigned char, UCharValueObject)
|
|
|
|
META_ValueObject(short, ShortValueObject)
|
|
|
|
META_ValueObject(unsigned short, UShortValueObject)
|
|
|
|
META_ValueObject(int, IntValueObject)
|
|
|
|
META_ValueObject(unsigned int, UIntValueObject)
|
|
|
|
META_ValueObject(float, FloatValueObject)
|
|
|
|
META_ValueObject(double, DoubleValueObject)
|
|
|
|
META_ValueObject(Vec2f, Vec2fValueObject)
|
|
|
|
META_ValueObject(Vec3f, Vec3fValueObject)
|
|
|
|
META_ValueObject(Vec4f, Vec4fValueObject)
|
|
|
|
META_ValueObject(Vec2d, Vec2dValueObject)
|
|
|
|
META_ValueObject(Vec3d, Vec3dValueObject)
|
|
|
|
META_ValueObject(Vec4d, Vec4dValueObject)
|
|
|
|
META_ValueObject(Quat, QuatValueObject)
|
|
|
|
META_ValueObject(Plane, PlaneValueObject)
|
|
|
|
META_ValueObject(Matrixf, MatrixfValueObject)
|
|
|
|
META_ValueObject(Matrixd, MatrixdValueObject)
|
2014-02-11 00:44:13 +08:00
|
|
|
META_ValueObject(BoundingBoxf, BoundingBoxfValueObject)
|
|
|
|
META_ValueObject(BoundingBoxd, BoundingBoxdValueObject)
|
|
|
|
META_ValueObject(BoundingSpheref, BoundingSpherefValueObject)
|
|
|
|
META_ValueObject(BoundingSphered, BoundingSpheredValueObject)
|
2011-06-03 06:04:08 +08:00
|
|
|
|
2011-06-10 19:42:02 +08:00
|
|
|
/** provide implementation of osg::Object::getUserValue(..) template*/
|
2011-06-03 06:04:08 +08:00
|
|
|
template<typename T>
|
|
|
|
bool osg::Object::getUserValue(const std::string& name, T& value) const
|
|
|
|
{
|
|
|
|
typedef TemplateValueObject<T> UserValueObject;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2016-02-04 21:10:28 +08:00
|
|
|
const osg::UserDataContainer* udc = asUserDataContainer();
|
2011-06-10 19:42:02 +08:00
|
|
|
if (!udc) udc = _userDataContainer;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2016-02-04 21:10:28 +08:00
|
|
|
if (!udc) return false;
|
|
|
|
|
|
|
|
const Object* obj = udc->getUserObject(name);
|
|
|
|
if (obj && typeid(*obj)==typeid(UserValueObject))
|
2011-06-03 06:04:08 +08:00
|
|
|
{
|
2016-02-04 21:10:28 +08:00
|
|
|
const UserValueObject* uvo = static_cast<const UserValueObject*>(obj);
|
2011-06-03 06:04:08 +08:00
|
|
|
value = uvo->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-10 19:42:02 +08:00
|
|
|
/** provide implementation of osg::Object::setUserValue(..) template.*/
|
2011-06-03 06:04:08 +08:00
|
|
|
template<typename T>
|
|
|
|
void osg::Object::setUserValue(const std::string& name, const T& value)
|
|
|
|
{
|
|
|
|
typedef TemplateValueObject<T> UserValueObject;
|
|
|
|
|
2016-02-04 21:10:28 +08:00
|
|
|
osg::UserDataContainer* udc = asUserDataContainer();
|
2011-06-10 19:42:02 +08:00
|
|
|
if (!udc)
|
|
|
|
{
|
|
|
|
getOrCreateUserDataContainer();
|
|
|
|
udc = _userDataContainer;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2011-06-10 19:42:02 +08:00
|
|
|
unsigned int i = udc->getUserObjectIndex(name);
|
2016-02-04 21:10:28 +08:00
|
|
|
if (i<udc->getNumUserObjects())
|
|
|
|
{
|
|
|
|
Object* obj = udc->getUserObject(i);
|
|
|
|
if (typeid(*obj)==typeid(UserValueObject))
|
|
|
|
{
|
|
|
|
UserValueObject* uvo = static_cast<UserValueObject*>(obj);
|
|
|
|
uvo->setValue(value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
udc->setUserObject(i, new UserValueObject(name, value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
udc->addUserObject(new UserValueObject(name,value));
|
|
|
|
}
|
2011-06-03 06:04:08 +08:00
|
|
|
}
|
|
|
|
|
2013-12-26 01:36:32 +08:00
|
|
|
|
2016-02-04 21:10:28 +08:00
|
|
|
template<class P, class T>
|
|
|
|
T* getOrCreateUserObjectOfType(P* parent)
|
|
|
|
{
|
|
|
|
T* object=0;
|
|
|
|
const char* name = typeid(T).name();
|
|
|
|
osg::UserDataContainer* udc = parent->getOrCreateUserDataContainer();
|
|
|
|
unsigned int index = udc->getUserObjectIndex(name);
|
|
|
|
if (index<udc->getNumUserObjects())
|
|
|
|
{
|
|
|
|
osg::Object* userObject = udc->getUserObject(index);
|
|
|
|
if (typeid(*userObject)==typeid(T))
|
|
|
|
{
|
|
|
|
object = static_cast<T*>(userObject);
|
|
|
|
// OSG_NOTICE<<"Reusing "<<name<<std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// OSG_NOTICE<<"Replacing "<<name<<", original object "<<userObject->className()<<std::endl;
|
|
|
|
|
|
|
|
object = new T;
|
|
|
|
object->setName(name);
|
|
|
|
udc->setUserObject(index, object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
object = new T;
|
|
|
|
object->setName(name);
|
|
|
|
udc->addUserObject(object);
|
|
|
|
// OSG_NOTICE<<"Creating new "<<name<<std::endl;
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-06-03 06:04:08 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|