diff --git a/VisualStudio/osg/osg.dsp b/VisualStudio/osg/osg.dsp index 196b99f06..c147b5b82 100755 --- a/VisualStudio/osg/osg.dsp +++ b/VisualStudio/osg/osg.dsp @@ -248,6 +248,10 @@ SOURCE=..\..\src\osg\ColorMatrix.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osg\ComputeBoundsVisitor.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osg\ConvexPlanarOccluder.cpp # End Source File # Begin Source File @@ -724,6 +728,10 @@ SOURCE=..\..\Include\Osg\ColorMatrix # End Source File # Begin Source File +SOURCE=..\..\src\osg\ComputeBoundsVisitor +# End Source File +# Begin Source File + SOURCE=..\..\Include\Osg\ConvexPlanarOccluder # End Source File # Begin Source File diff --git a/include/osg/ComputeBoundsVisitor b/include/osg/ComputeBoundsVisitor new file mode 100644 index 000000000..b70b338cd --- /dev/null +++ b/include/osg/ComputeBoundsVisitor @@ -0,0 +1,59 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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_COMPUTEBOUNDSVISITOR +#define OSG_COMPUTEBOUNDSVISITOR 1 + +#include +#include +#include + +namespace osg { + +class OSG_EXPORT ComputeBoundsVisitor : public osg::NodeVisitor +{ +public: + + ComputeBoundsVisitor(TraversalMode traversalMode = TRAVERSE_ALL_CHILDREN); + + virtual void reset(); + + osg::BoundingBox& getBoundingBox() { return _bb; } + + void getPolytope(osg::Polytope& polytope, float margin=0.1) const; + + void getBase(osg::Polytope& polytope, float margin=0.1) const; + + void apply(osg::Node& node); + + void apply(osg::Transform& transform); + + void apply(osg::Geode& geode); + + inline void pushMatrix(osg::Matrix& matrix) { _matrixStack.push_back(matrix); } + + inline void popMatrix() { _matrixStack.pop_back(); } + + void applyDrawable(osg::Drawable* drawable); + +protected: + + typedef std::vector MatrixStack; + + MatrixStack _matrixStack; + osg::BoundingBox _bb; +}; + +} + +#endif diff --git a/src/osg/ComputeBoundsVisitor.cpp b/src/osg/ComputeBoundsVisitor.cpp new file mode 100644 index 000000000..ab67baf33 --- /dev/null +++ b/src/osg/ComputeBoundsVisitor.cpp @@ -0,0 +1,97 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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. +*/ + +#include +#include +#include +#include + +using namespace osg; + +ComputeBoundsVisitor::ComputeBoundsVisitor(TraversalMode traversalMode): + osg::NodeVisitor(traversalMode) +{ +} + +void ComputeBoundsVisitor::reset() +{ + _matrixStack.clear(); + _bb.init(); +} + +void ComputeBoundsVisitor::getPolytope(osg::Polytope& polytope, float margin) const +{ + float delta = _bb.radius()*margin; + polytope.add( osg::Plane(0.0, 0.0, 1.0, -(_bb.zMin()-delta)) ); + polytope.add( osg::Plane(0.0, 0.0, -1.0, (_bb.zMax()+delta)) ); + + polytope.add( osg::Plane(1.0, 0.0, 0.0, -(_bb.xMin()-delta)) ); + polytope.add( osg::Plane(-1.0, 0.0, 0.0, (_bb.xMax()+delta)) ); + + polytope.add( osg::Plane(0.0, 1.0, 0.0, -(_bb.yMin()-delta)) ); + polytope.add( osg::Plane(0.0, -1.0, 0.0, (_bb.yMax()+delta)) ); +} + +void ComputeBoundsVisitor::getBase(osg::Polytope& polytope, float margin) const +{ + float delta = _bb.radius()*margin; + polytope.add( osg::Plane(0.0, 0.0, 1.0, -(_bb.zMin()-delta)) ); +} + +void ComputeBoundsVisitor::apply(osg::Node& node) +{ + traverse(node); +} + +void ComputeBoundsVisitor::apply(osg::Transform& transform) +{ + osg::Matrix matrix; + if (!_matrixStack.empty()) matrix = _matrixStack.back(); + + transform.computeLocalToWorldMatrix(matrix,this); + + pushMatrix(matrix); + + traverse(transform); + + popMatrix(); +} + +void ComputeBoundsVisitor::apply(osg::Geode& geode) +{ + for(unsigned int i=0; igetBound()); + else + { + osg::Matrix& matrix = _matrixStack.back(); + const osg::BoundingBox& dbb = drawable->getBound(); + if (dbb.valid()) + { + _bb.expandBy(dbb.corner(0) * matrix); + _bb.expandBy(dbb.corner(1) * matrix); + _bb.expandBy(dbb.corner(2) * matrix); + _bb.expandBy(dbb.corner(3) * matrix); + _bb.expandBy(dbb.corner(4) * matrix); + _bb.expandBy(dbb.corner(5) * matrix); + _bb.expandBy(dbb.corner(6) * matrix); + _bb.expandBy(dbb.corner(7) * matrix); + } + } +} diff --git a/src/osg/GNUmakefile b/src/osg/GNUmakefile index 384f7560d..23609f4cf 100644 --- a/src/osg/GNUmakefile +++ b/src/osg/GNUmakefile @@ -25,6 +25,7 @@ CXXFILES =\ CollectOccludersVisitor.cpp\ ColorMask.cpp\ ColorMatrix.cpp\ + ComputeBoundsVisitor.cpp\ ConvexPlanarOccluder.cpp\ ConvexPlanarPolygon.cpp\ CoordinateSystemNode.cpp\