From a95d66a587580a92500c2a1237eeae8ac533a2e3 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 26 Apr 2017 17:10:58 +0100 Subject: [PATCH] Replaced dynamoc_cast with asCullStack() to improve performance of AutoTransform, screen size based LOD and PagedLOD. --- include/osg/NodeVisitor | 8 ++++++++ include/osgUtil/CullVisitor | 9 +++++++++ src/osg/AutoTransform.cpp | 2 +- src/osg/LOD.cpp | 2 +- src/osg/PagedLOD.cpp | 2 +- 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/osg/NodeVisitor b/include/osg/NodeVisitor index bf77ec975..dc6975636 100644 --- a/include/osg/NodeVisitor +++ b/include/osg/NodeVisitor @@ -52,6 +52,7 @@ class Camera; class CameraView; class Drawable; class Geometry; +class CullStack; @@ -149,6 +150,13 @@ class OSG_EXPORT NodeVisitor : public virtual Object * Equivalent to dynamic_cast(this).*/ virtual const osgUtil::IntersectionVisitor* asIntersectionVisitor() const { return 0; } + /** Convert 'this' into a osg::CullStack pointer if Object is a osg::CullStack, otherwise return 0. + * Equivalent to dynamic_cast(this).*/ + virtual osg::CullStack* asCullStack() { return 0; } + + /** convert 'const this' into a const osg::CullStack pointer if Object is a osg::CullStack, otherwise return 0. + * Equivalent to dynamic_cast(this).*/ + virtual const osg::CullStack* asCullStack() const { return 0; } diff --git a/include/osgUtil/CullVisitor b/include/osgUtil/CullVisitor index 3fab4f367..2e36bc2e2 100644 --- a/include/osgUtil/CullVisitor +++ b/include/osgUtil/CullVisitor @@ -67,6 +67,15 @@ class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor, public osg::CullStac * Equivalent to dynamic_cast(this).*/ virtual const osgUtil::CullVisitor* asCullVisitor() const { return this; } + /** Convert 'this' into a osg::CullStack pointer if Object is a osg::CullStack, otherwise return 0. + * Equivalent to dynamic_cast(this).*/ + virtual osg::CullStack* asCullStack() { return static_cast(this); } + + /** convert 'const this' into a const osg::CullStack pointer if Object is a osg::CullStack, otherwise return 0. + * Equivalent to dynamic_cast(this).*/ + virtual const osg::CullStack* asCullStack() const { return static_cast(this); } + + using osg::NodeVisitor::clone; /** Create a shallow copy of the CullVisitor, used by CullVisitor::create() to clone the prototype. */ diff --git a/src/osg/AutoTransform.cpp b/src/osg/AutoTransform.cpp index c387131b6..5612db316 100644 --- a/src/osg/AutoTransform.cpp +++ b/src/osg/AutoTransform.cpp @@ -140,7 +140,7 @@ osg::Matrixd AutoTransform::computeMatrix(const osg::NodeVisitor* nv) const Quat rotation = _rotation; osg::Vec3d scale = _scale; - const CullStack* cs = dynamic_cast(nv); + const CullStack* cs = nv ? nv->asCullStack() : 0; if (cs) { osg::Vec3d eyePoint = cs->getEyeLocal(); diff --git a/src/osg/LOD.cpp b/src/osg/LOD.cpp index 679164b78..1ad24cfd6 100644 --- a/src/osg/LOD.cpp +++ b/src/osg/LOD.cpp @@ -51,7 +51,7 @@ void LOD::traverse(NodeVisitor& nv) } else { - osg::CullStack* cullStack = dynamic_cast(&nv); + osg::CullStack* cullStack = nv.asCullStack(); if (cullStack && cullStack->getLODScale()) { required_range = cullStack->clampedPixelSize(getBound()) / cullStack->getLODScale(); diff --git a/src/osg/PagedLOD.cpp b/src/osg/PagedLOD.cpp index 46ea5570c..e2680b2bd 100644 --- a/src/osg/PagedLOD.cpp +++ b/src/osg/PagedLOD.cpp @@ -150,7 +150,7 @@ void PagedLOD::traverse(NodeVisitor& nv) } else { - osg::CullStack* cullStack = dynamic_cast(&nv); + osg::CullStack* cullStack = nv.asCullStack(); if (cullStack && cullStack->getLODScale()>0.0f) { required_range = cullStack->clampedPixelSize(getBound()) / cullStack->getLODScale();