diff --git a/include/osgTerrain/Layer b/include/osgTerrain/Layer index 39bf2ae92..117731ed5 100644 --- a/include/osgTerrain/Layer +++ b/include/osgTerrain/Layer @@ -40,6 +40,8 @@ class OSGTERRAIN_EXPORT Layer : public osg::Object const Locator* getLocator() const { return _locator.get(); } + virtual osg::BoundingSphere computeBound() const; + protected: virtual ~Layer(); diff --git a/include/osgTerrain/Locator b/include/osgTerrain/Locator index 06190b9ca..21dc65118 100644 --- a/include/osgTerrain/Locator +++ b/include/osgTerrain/Locator @@ -33,8 +33,8 @@ class OSGTERRAIN_EXPORT Locator : public osg::Object META_Object(osgTerrain, Locator); - virtual bool convertLocalToModel(const osg::Vec3d& /*local*/, osg::Vec3d& /*world*/) { return false; }; - virtual bool convertModelToWorld(const osg::Vec3d& /*world*/, osg::Vec3d& /*local*/) { return false; }; + virtual bool convertLocalToModel(const osg::Vec3d& /*local*/, osg::Vec3d& /*world*/) const { return false; } + virtual bool convertModelToWorld(const osg::Vec3d& /*world*/, osg::Vec3d& /*local*/) const { return false; } protected: @@ -62,8 +62,8 @@ public: osg::EllipsoidModel* getEllipsoidModel() { return _em.get(); } const osg::EllipsoidModel* getEllipsoidModel() const { return _em.get(); } - bool convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world); - bool convertModelToWorld(const osg::Vec3d& world, osg::Vec3d& local); + bool convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world) const; + bool convertModelToWorld(const osg::Vec3d& world, osg::Vec3d& local) const; protected: diff --git a/include/osgTerrain/TerrainNode b/include/osgTerrain/TerrainNode index cdbda3be6..c34ad69f8 100644 --- a/include/osgTerrain/TerrainNode +++ b/include/osgTerrain/TerrainNode @@ -73,6 +73,9 @@ class OSGTERRAIN_EXPORT TerrainNode : public osg::Group osg::TransferFunction* getColorTransferFunction() { return _colorTransferFunction.get(); } const osg::TransferFunction* getColorTransferFunction() const { return _colorTransferFunction.get(); } + /** Compute the bounding volume of the terrain by computing the union of the bounding volumes of all layers.*/ + virtual osg::BoundingSphere computeBound() const; + protected: virtual ~TerrainNode(); diff --git a/src/osgTerrain/Layer.cpp b/src/osgTerrain/Layer.cpp index 14bf7fa1f..3e4ca0d4b 100644 --- a/src/osgTerrain/Layer.cpp +++ b/src/osgTerrain/Layer.cpp @@ -28,6 +28,36 @@ Layer::~Layer() { } +osg::BoundingSphere Layer::computeBound() const +{ + osg::BoundingSphere bs; + if (!getLocator()) return bs; + + osg::Vec3d v; + if (getLocator()->convertLocalToModel(osg::Vec3d(0.0,0.0,0.0), v)) + { + bs.expandBy(v); + } + + if (getLocator()->convertLocalToModel(osg::Vec3d(1.0,0.0,0.0), v)) + { + bs.expandBy(v); + } + + if (getLocator()->convertLocalToModel(osg::Vec3d(1.0,1.0,0.0), v)) + { + bs.expandBy(v); + } + + if (getLocator()->convertLocalToModel(osg::Vec3d(0.0,1.0,0.0), v)) + { + bs.expandBy(v); + } + + return bs; +} + + ///////////////////////////////////////////////////////////////////////////// // // ImageLayer diff --git a/src/osgTerrain/Locator.cpp b/src/osgTerrain/Locator.cpp index 514269aeb..d6191de70 100644 --- a/src/osgTerrain/Locator.cpp +++ b/src/osgTerrain/Locator.cpp @@ -58,7 +58,7 @@ void EllipsoidLocator::setExtents(double longitude, double latitude, double delt _height = height; } -bool EllipsoidLocator::convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world) +bool EllipsoidLocator::convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world) const { double longitude = _longitude + local.x() * _deltaLongitude; double latitude = _latitude + local.y() * _deltaLatitude; @@ -70,7 +70,7 @@ bool EllipsoidLocator::convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& return true; } -bool EllipsoidLocator::convertModelToWorld(const osg::Vec3d& world, osg::Vec3d& local) +bool EllipsoidLocator::convertModelToWorld(const osg::Vec3d& world, osg::Vec3d& local) const { double longitude, latitude, height; diff --git a/src/osgTerrain/TerrainNode.cpp b/src/osgTerrain/TerrainNode.cpp index d227cd452..66fa6c915 100644 --- a/src/osgTerrain/TerrainNode.cpp +++ b/src/osgTerrain/TerrainNode.cpp @@ -75,3 +75,14 @@ void TerrainNode::setColorTransferFunction(osg::TransferFunction* tf) { _colorTransferFunction = tf; } + +osg::BoundingSphere TerrainNode::computeBound() const +{ + osg::BoundingSphere bs; + + if (_elevationLayer.valid()) bs.expandBy(_elevationLayer->computeBound()); + + if (_colorLayer.valid()) bs.expandBy(_colorLayer->computeBound()); + + return bs; +}