Thrall, Bryan wrote:
Paul Martz wrote on Thursday, May 28, 2009 4:50 PM:
You use a NodeVisitor to walk your scene graph and look for Geodes,
then iterate over the Drawables that are attached to the Geodes, using
dynamic_cast to access the Drawable as a Geometry.

Even better, I notice osg::Drawable has asGeometry(), which is cheaper
than dynamic_cast<> :)
Wow, I wasn't expecting the difference to be this large, as virtual function calls aren't completely free either:

16:51|p...@tabu:~> cat t.cpp
// g++ -o t t.cpp -I ~/osg2.8/include/ -L ~/osg2.8/lib/ -losg -losgDB
#include <osgDB/ReadFile>
#include <sys/time.h>

float
timediff(struct timeval t0, struct timeval t1)
{
return 1000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec) / 1000.0;
}

int main()
{
   osg::Group* cow = (osg::Group*)osgDB::readNodeFile("cow.osg");
   osg::Geode* geode = (osg::Geode*)cow->getChild(0);
   osg::Drawable *draw = geode->getDrawable(0);

   bool            isgeom = true;
   int             i;
   struct timeval  t0, t1, t2;
   const int       N = 100000;

   gettimeofday(&t0, NULL);
   for (i = 0; i < N; i++)
       isgeom = isgeom && (draw->asGeometry() != NULL);

   gettimeofday(&t1, NULL);
   for (i = 0; i < N; i++)
       isgeom = isgeom && (dynamic_cast<osg::Geometry*>(draw) != NULL);

   gettimeofday(&t2, NULL);

printf("%d\n", isgeom); // need to use isgeom, as otherwise it might be optimized away
   printf("asGeometry()    : %.3f ms\n", timediff(t0, t1));
   printf("dynamic_cast<>(): %.3f ms\n", timediff(t1, t2));
}
16:52|p...@tabu:~> g++ -o t t.cpp -I ~/osg2.8/include/ -L ~/osg2.8/lib/ -losg -losgDB
16:52|p...@tabu:~> ./t
1
asGeometry()    : 1.181 ms
dynamic_cast<>(): 3.945 ms
16:52|p...@tabu:~> g++ -O3 -o t t.cpp -I ~/osg2.8/include/ -L ~/osg2.8/lib/ -losg -losgDB
16:52|p...@tabu:~> ./t
1
asGeometry()    : 0.839 ms
dynamic_cast<>(): 3.611 ms
16:52|p...@tabu:~>




Paul
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to