Introduce osgDB::PropetyInterface class that provided a generic interface for get/setting properties on scene graph objects, utilizing the osgDB serializers to do

the actual interface query and set/gets.
This commit is contained in:
Robert Osfield
2013-09-19 16:19:32 +00:00
parent f42481b60f
commit 250d9f2ed7
9 changed files with 909 additions and 18 deletions

View File

@@ -27,6 +27,9 @@
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osg/io_utils>
#include<osgDB/PropertyInterface>
int main(int argc, char** argv)
{
@@ -158,5 +161,300 @@ int main(int argc, char** argv)
osgDB::writeNodeFile(*presentation, "pres.osgt");
return viewer.run();
typedef std::map<int, std::string> TypeNameMap;
TypeNameMap typeNameMap;
#define TYPENAME(A) typeNameMap[osgDB::BaseSerializer::A] = #A;
TYPENAME(RW_UNDEFINED)
TYPENAME(RW_USER)
TYPENAME(RW_OBJECT)
TYPENAME(RW_IMAGE)
TYPENAME(RW_LIST)
TYPENAME(RW_BOOL)
TYPENAME(RW_CHAR)
TYPENAME(RW_UCHAR)
TYPENAME(RW_SHORT)
TYPENAME(RW_USHORT)
TYPENAME(RW_INT)
TYPENAME(RW_UINT)
TYPENAME(RW_FLOAT)
TYPENAME(RW_DOUBLE)
TYPENAME(RW_VEC2F)
TYPENAME(RW_VEC2D)
TYPENAME(RW_VEC3F)
TYPENAME(RW_VEC3D)
TYPENAME(RW_VEC4F)
TYPENAME(RW_VEC4D)
TYPENAME(RW_QUAT)
TYPENAME(RW_PLANE)
TYPENAME(RW_MATRIXF)
TYPENAME(RW_MATRIXD)
TYPENAME(RW_MATRIX)
TYPENAME(RW_GLENUM)
TYPENAME(RW_STRING)
TYPENAME(RW_ENUM)
TYPENAME(RW_VEC2B)
TYPENAME(RW_VEC2UB)
TYPENAME(RW_VEC2S)
TYPENAME(RW_VEC2US)
TYPENAME(RW_VEC2I)
TYPENAME(RW_VEC2UI)
TYPENAME(RW_VEC3B)
TYPENAME(RW_VEC3UB)
TYPENAME(RW_VEC3S)
TYPENAME(RW_VEC3US)
TYPENAME(RW_VEC3I)
TYPENAME(RW_VEC3UI)
TYPENAME(RW_VEC4B)
TYPENAME(RW_VEC4UB)
TYPENAME(RW_VEC4S)
TYPENAME(RW_VEC4US)
TYPENAME(RW_VEC4I)
TYPENAME(RW_VEC4UI)
#if 0
osgDB::ObjectWrapperManager* owm = osgDB::Registry::instance()->getObjectWrapperManager();
if (owm)
{
const osgDB::ObjectWrapperManager::WrapperMap& wrapperMap = owm->getWrapperMap();
for(osgDB::ObjectWrapperManager::WrapperMap::const_iterator itr = wrapperMap.begin();
itr != wrapperMap.end();
++itr)
{
osgDB::ObjectWrapper* ow = itr->second.get();
OSG_NOTICE<<std::endl<<"Wrapper : "<<itr->first<<", Domain="<<ow->getDomain()<<", Name="<<ow->getName()<<std::endl;
const osgDB::StringList& associates = ow->getAssociates();
for(osgDB::StringList::const_iterator aitr = associates.begin();
aitr != associates.end();
++aitr)
{
OSG_NOTICE<<" associate = "<<*aitr<<std::endl;
}
osgDB::StringList properties;
std::vector<int> types;
ow->writeSchema(properties, types);
OSG_NOTICE<<" properties.size() = "<<properties.size()<<", types.size() = "<<types.size()<<std::endl;
unsigned int numProperties = std::min(properties.size(), types.size());
for(unsigned int i=0; i<numProperties; ++i)
{
OSG_NOTICE<<" property = "<<properties[i]<<", type = "<<types[i]<<", typeName = "<<typeNameMap[types[i]]<<std::endl;
}
}
#if 0
osgDB::ObjectWrapperManager::IntLookupMap& intLookupMap = owm->getLookupMap();
for(osgDB::ObjectWrapperManager::IntLookupMap::iterator itr = intLookupMap.begin();
itr != intLookupMap.end();
++itr)
{
OSG_NOTICE<<std::endl<<"IntLookMap["<<itr->first<<"]"<<std::endl;
osgDB::IntLookup::StringToValue& stv = itr->second.getStringToValue();
for(osgDB::IntLookup::StringToValue::iterator sitr = stv.begin();
sitr != stv.end();
++sitr)
{
OSG_NOTICE<<" "<<sitr->first<<", "<<sitr->second<<std::endl;
}
}
#endif
}
#endif
presentation->setName("[this is a test]");
osgDB::PropertyInterface pi;
if (pi.setProperty(presentation.get(), "Name", std::string("[this is new improved test]")))
{
OSG_NOTICE<<"setProperty(presentation.get(), Name) succeeded."<<std::endl;
}
else
{
OSG_NOTICE<<"setProperty(presentation.get(), Name) failed."<<std::endl;
}
std::string name;
if (pi.getProperty(presentation.get(), "Name", name))
{
OSG_NOTICE<<"getProperty(presentation.get(), Name) succeeded, Name = "<<name<<std::endl;
}
else
{
OSG_NOTICE<<"getProperty(presentation.get(), Name) failed."<<std::endl;
}
OSG_NOTICE<<std::endl;
// presentation->setDataVariance(osg::Object::DYNAMIC);
int variance = 1234;
if (pi.getProperty(presentation.get(), "DataVariance", variance))
{
OSG_NOTICE<<"getProperty(presentation.get(), DataVariance) succeeded, variance = "<<variance<<std::endl;
}
else
{
OSG_NOTICE<<"getProperty(presentation.get(), DataVariance) failed."<<std::endl;
}
OSG_NOTICE<<std::endl;
if (pi.setProperty(presentation.get(), "DataVariance", 1))
{
OSG_NOTICE<<"setProperty(presentation.get(), DataVariance) succeeded."<<std::endl;
}
else
{
OSG_NOTICE<<"setProperty(presentation.get(), DataVariance) failed."<<std::endl;
}
OSG_NOTICE<<std::endl;
if (pi.getProperty(presentation.get(), "DataVariance", variance))
{
OSG_NOTICE<<"2nd getProperty(presentation.get(), DataVariance) succeeded, variance = "<<variance<<std::endl;
}
else
{
OSG_NOTICE<<"2nd getProperty(presentation.get(), DataVariance) failed."<<std::endl;
}
OSG_NOTICE<<std::endl;
presentation->setMatrix(osg::Matrixd::translate(osg::Vec3d(1.0,2.0,3.0)));
// if (pi.setProperty(presentation.get(), "Matrix", osg::Matrixd::scale(1.0,2.0,2.0)))
if (pi.setProperty(presentation.get(), "Matrix", osg::Matrixd::scale(2.0,2.0,2.0)))
{
OSG_NOTICE<<"setProperty(..,Matrix) succedded."<<std::endl;
}
else
{
OSG_NOTICE<<"setProperty(..,Matrix) failed."<<std::endl;
}
osg::Matrixd matrix;
if (pi.getProperty(presentation.get(), "Matrix", matrix))
{
OSG_NOTICE<<"getProperty(presentation.get(), ...) succeeded, Matrix = "<<matrix<<std::endl;
}
else
{
OSG_NOTICE<<"getProperty(presentation.get(), ...) failed."<<std::endl;
}
#if 1
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
osg::ref_ptr<osg::Node> node = new osg::Node;
osgDB::PropertyInterface::PropertyList properties;
if (pi.getSupportedProperties(presentation.get(), properties, true))
{
OSG_NOTICE<<"Have supported properites found."<<std::endl;
for(osgDB::PropertyInterface::PropertyList::iterator itr = properties.begin();
itr != properties.end();
++itr)
{
OSG_NOTICE<<"Property "<<itr->first<<", "<<itr->second<<std::endl;
}
}
else
{
OSG_NOTICE<<"No supported properites found."<<std::endl;
}
OSG_NOTICE<<"Type(float) = "<<osgDB::getTypeEnum<float>()<<", "<<osgDB::getTypeString<float>()<<std::endl;
OSG_NOTICE<<"Type(bool) = "<<osgDB::getTypeEnum<bool>()<<", "<<osgDB::getTypeString<bool>()<<std::endl;
OSG_NOTICE<<"Type(osg::Vec3) = "<<osgDB::getTypeEnum<osg::Vec3>()<<", "<<osgDB::getTypeString<osg::Vec3>()<<std::endl;
OSG_NOTICE<<"Type(osg::Matrixd) = "<<osgDB::getTypeEnum<osg::Matrixd>()<<", "<<osgDB::getTypeString<osg::Matrixd>()<<std::endl;
OSG_NOTICE<<"Type(osg::Vec2ui) = "<<osgDB::getTypeEnum<osg::Vec2ui>()<<", "<<osgDB::getTypeString<osg::Vec2ui>()<<std::endl;
OSG_NOTICE<<"Type(GLenum) = "<<osgDB::getTypeEnum<GLenum>()<<", "<<osgDB::getTypeString<GLenum>()<<std::endl;
OSG_NOTICE<<"Type(int) = "<<osgDB::getTypeEnum<int>()<<", "<<osgDB::getTypeString<int>()<<std::endl;
OSG_NOTICE<<"Type(osg::Image*) = "<<osgDB::getTypeEnum<osg::Image*>()<<", "<<osgDB::getTypeString<osg::Image*>()<<std::endl;
OSG_NOTICE<<"Type(osg::Object*) = "<<osgDB::getTypeEnum<osg::Object*>()<<", "<<osgDB::getTypeString<osg::Object*>()<<std::endl;
OSG_NOTICE<<"Type(osg::Referenced*) = "<<osgDB::getTypeEnum<osg::Referenced*>()<<", "<<osgDB::getTypeString<osg::Referenced*>()<<std::endl;
osg::Object* ptr = presentation.get();
OSG_NOTICE<<"Type(ptr) = "<<osgDB::getTypeEnumFromPtr(ptr)<<", "<<osgDB::getTypeStringFromPtr(ptr)<<std::endl;
OSG_NOTICE<<"Type(presentation) = "<<osgDB::getTypeEnumFromPtr(presentation.get())<<", "<<osgDB::getTypeStringFromPtr(presentation.get())<<std::endl;
osg::Image* image2 = 0;
OSG_NOTICE<<"Type(image) = "<<osgDB::getTypeEnumFromPtr(image2)<<", "<<osgDB::getTypeStringFromPtr(image2)<<std::endl;
osg::Vec3 pos;
OSG_NOTICE<<"Type(pos) = "<<osgDB::getTypeEnumFrom(pos)<<", "<<osgDB::getTypeStringFrom(pos)<<std::endl;
OSG_NOTICE<<"Type(std::string) = "<<osgDB::getTypeEnum<std::string>()<<", "<<osgDB::getTypeString<std::string>()<<std::endl;
osgDB::BaseSerializer::Type type;
if (pi.getPropertyType(presentation.get(), "Name", type))
{
OSG_NOTICE<<"Property Type, Name = "<< type<<std::endl;
}
#endif
osg::Matrixd mymatrix = osg::Matrix::translate(-1,2,3);
pi.setProperty(presentation.get(), "mymatrix", mymatrix);
osg::Matrixd copyofmatrix;
if (pi.getProperty(presentation.get(), "mymatrix", copyofmatrix))
{
OSG_NOTICE<<"mymatrix = "<<copyofmatrix<<std::endl;
}
if (pi.getProperty(presentation.get(), "Matrix", copyofmatrix))
{
OSG_NOTICE<<"Matrix = "<<copyofmatrix<<std::endl;
}
std::string teststring="Another test";
pi.setProperty(presentation.get(),"mystring",teststring);
std::string astring;
if (pi.getProperty(presentation.get(),"mystring",astring))
{
OSG_NOTICE<<"mystring = "<<astring<<std::endl;
}
else
{
OSG_NOTICE<<"failed to get mystring"<<std::endl;
}
#define PRINT_TYPE(O,PN) \
{ \
osgDB::BaseSerializer::Type type; \
if (pi.getPropertyType(O, #PN, type)) \
{ \
OSG_NOTICE<<#PN<<" : type "<<type<<", "<<typeNameMap[type]<<std::endl; \
} \
else \
{ \
OSG_NOTICE<<#PN<<" : failed to get type"<<std::endl; \
} \
}
PRINT_TYPE(presentation.get(), Name)
PRINT_TYPE(presentation.get(), Matrix)
PRINT_TYPE(presentation.get(), DataVariance)
PRINT_TYPE(presentation.get(), mystring)
PRINT_TYPE(presentation.get(), mymatrix)
// return viewer.run();
}