From 828851c08a2c8d06d0448cc4a62e831e9b45f14a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 14 Jan 2008 14:53:49 +0000 Subject: [PATCH] Refactor osgTerrain::ProxyLayer so that it is now a pure Proxy, defering implementations to an Implementation rather than a subclass of ProxyLayer. Updating the osgTerrain and GDAL plugins to comply with this refactor. --- include/osgTerrain/Layer | 43 ++++++------ src/osgPlugins/gdal/DataSetLayer.cpp | 10 ++- src/osgPlugins/gdal/DataSetLayer.h | 2 +- src/osgPlugins/osgTerrain/CompositeLayer.cpp | 19 ++---- src/osgPlugins/osgTerrain/Terrain.cpp | 16 ++--- src/osgTerrain/Layer.cpp | 72 ++++++++++++++++++++ 6 files changed, 118 insertions(+), 44 deletions(-) diff --git a/include/osgTerrain/Layer b/include/osgTerrain/Layer index c22bb893b..290058d96 100644 --- a/include/osgTerrain/Layer +++ b/include/osgTerrain/Layer @@ -306,35 +306,40 @@ class OSGTERRAIN_EXPORT ProxyLayer : public Layer ProxyLayer(const ProxyLayer& proxyLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); META_Object(osgTerrain, ProxyLayer); + + /** Set the implementation layer that does the actual work.*/ + void setImplementation(Layer* layer) { _implementation = layer; } + + /** Get the implementation layer that does the actual work.*/ + Layer* getImplementation() { return _implementation.get(); } - /** Return if this ProxyLayer is attached to valid file handle.*/ - virtual bool isOpen() const { return false; } + /** Get the const implementation layer that does the actual work.*/ + const Layer* getImplementation() const { return _implementation.get(); } - /** Open a file.*/ - void openFile(const std::string& fileName) - { - if (_filename!=fileName) - { - if (isOpen()) close(); - - _filename = fileName; - } + virtual void setFileName(const std::string& filename); + virtual const std::string& getFileName() const { return _filename; } - if (!isOpen()) open(); - } + virtual unsigned int getNumColumns() const; + virtual unsigned int getNumRows() const; + + virtual bool transform(float offset, float scale); - /** Open the any associated file handle.*/ - virtual void open() {} + virtual bool getValue(unsigned int i, unsigned int j, float& value) const; + virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const; + virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const; + virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const; - /** Open the any associated file handle.*/ - virtual void close() {} + virtual void dirty(); + virtual void setModifiedCount(unsigned int value); + virtual unsigned int getModifiedCount() const; - /** Extract an ImageLayer from the ProxyLayer.*/ - virtual ImageLayer* extractImageLayer(unsigned int /*sourceMinX*/, unsigned int /*sourceMinY*/, unsigned int /*sourceMaxX*/, unsigned int /*sourceMaxY*/, unsigned int /*targetWidth*/=0, unsigned int /*targetHeight*/=0) { return 0; } + virtual osg::BoundingSphere computeBound() const; protected: virtual ~ProxyLayer(); + + osg::ref_ptr _implementation; }; diff --git a/src/osgPlugins/gdal/DataSetLayer.cpp b/src/osgPlugins/gdal/DataSetLayer.cpp index 1a2e7d859..07e889374 100644 --- a/src/osgPlugins/gdal/DataSetLayer.cpp +++ b/src/osgPlugins/gdal/DataSetLayer.cpp @@ -32,11 +32,12 @@ _dataset(0), _gdalReader(0) DataSetLayer::DataSetLayer(const std::string& fileName): _dataset(0), _gdalReader(0) { - openFile(fileName); + setFileName(fileName); + open(); } DataSetLayer::DataSetLayer(const DataSetLayer& dataSetLayer,const osg::CopyOp& copyop): -ProxyLayer(dataSetLayer), _gdalReader(dataSetLayer._gdalReader) + Layer(dataSetLayer), _gdalReader(dataSetLayer._gdalReader) { if (dataSetLayer._dataset) open(); } @@ -52,6 +53,9 @@ void DataSetLayer::open() if (getFileName().empty()) return; + + osg::notify(osg::NOTICE)<<"DataSetLayer::open()"<(GDALOpen(getFileName().c_str(),GA_ReadOnly)); setUpLocator(); @@ -59,6 +63,8 @@ void DataSetLayer::open() void DataSetLayer::close() { + osg::notify(osg::NOTICE)<<"DataSetLayer::close()"<(_dataset)); diff --git a/src/osgPlugins/gdal/DataSetLayer.h b/src/osgPlugins/gdal/DataSetLayer.h index dd11a02d4..550984aa0 100644 --- a/src/osgPlugins/gdal/DataSetLayer.h +++ b/src/osgPlugins/gdal/DataSetLayer.h @@ -21,7 +21,7 @@ namespace GDALPlugin { -class DataSetLayer : public osgTerrain::ProxyLayer +class DataSetLayer : public osgTerrain::Layer { public: diff --git a/src/osgPlugins/osgTerrain/CompositeLayer.cpp b/src/osgPlugins/osgTerrain/CompositeLayer.cpp index a3fc129b1..4b0eb6e4f 100644 --- a/src/osgPlugins/osgTerrain/CompositeLayer.cpp +++ b/src/osgPlugins/osgTerrain/CompositeLayer.cpp @@ -75,21 +75,14 @@ bool CompositeLayer_readLocalData(osg::Object& obj, osgDB::Input &fr) } else if (fr.matchSequence("ProxyLayer %s") || fr.matchSequence("ProxyLayer %w")) { - osg::ref_ptr image = osgDB::readObjectFile(std::string(fr[1].getStr())+".gdal"); - osgTerrain::ProxyLayer* proxyLayer = dynamic_cast(image.get()); - if (proxyLayer) - { - if (locator.valid()) - { - proxyLayer->setLocator(locator.get()); - locator = 0; - } + osgTerrain::ProxyLayer* proxyLayer = new osgTerrain::ProxyLayer; + proxyLayer->setFileName(fr[1].getStr()); - if (minLevel!=0) proxyLayer->setMinLevel(minLevel); - if (maxLevel!=MAXIMUM_NUMBER_OF_LEVELS) proxyLayer->setMaxLevel(maxLevel); + if (locator.valid()) proxyLayer->setLocator(locator.get()); + if (minLevel!=0) proxyLayer->setMinLevel(minLevel); + if (maxLevel!=MAXIMUM_NUMBER_OF_LEVELS) proxyLayer->setMaxLevel(maxLevel); - layer.addLayer(proxyLayer); - } + layer.addLayer(proxyLayer); fr += 2; diff --git a/src/osgPlugins/osgTerrain/Terrain.cpp b/src/osgPlugins/osgTerrain/Terrain.cpp index 8539cef82..99d32ac2e 100644 --- a/src/osgPlugins/osgTerrain/Terrain.cpp +++ b/src/osgPlugins/osgTerrain/Terrain.cpp @@ -151,16 +151,14 @@ bool Terrain_readLocalData(osg::Object& obj, osgDB::Input &fr) if (fr.matchSequence("ProxyLayer %s") || fr.matchSequence("ProxyLayer %w") ) { - osg::ref_ptr image = osgDB::readObjectFile(std::string(fr[1].getStr())+".gdal"); - osgTerrain::ProxyLayer* proxyLayer = dynamic_cast(image.get()); - if (proxyLayer) - { - if (locator) proxyLayer->setLocator(locator); - if (minLevel!=0) proxyLayer->setMinLevel(minLevel); - if (maxLevel!=MAXIMUM_NUMBER_OF_LEVELS) proxyLayer->setMaxLevel(maxLevel); + osgTerrain::ProxyLayer* proxyLayer = new osgTerrain::ProxyLayer; + proxyLayer->setFileName(fr[1].getStr()); - terrain.setElevationLayer(proxyLayer); - } + if (locator) proxyLayer->setLocator(locator); + if (minLevel!=0) proxyLayer->setMinLevel(minLevel); + if (maxLevel!=MAXIMUM_NUMBER_OF_LEVELS) proxyLayer->setMaxLevel(maxLevel); + + terrain.setElevationLayer(proxyLayer); fr += 2; diff --git a/src/osgTerrain/Layer.cpp b/src/osgTerrain/Layer.cpp index 11469ee82..b72c25e40 100644 --- a/src/osgTerrain/Layer.cpp +++ b/src/osgTerrain/Layer.cpp @@ -364,3 +364,75 @@ ProxyLayer::~ProxyLayer() { } +void ProxyLayer::setFileName(const std::string& filename) +{ + _filename = filename; + if (_implementation.valid()) + { + _implementation->setFileName(_filename); + } +} + +unsigned int ProxyLayer::getNumColumns() const +{ + if (_implementation.valid()) return _implementation->getNumColumns(); + else return 0; +} + +unsigned int ProxyLayer::getNumRows() const +{ + if (_implementation.valid()) return _implementation->getNumRows(); + else return 0; +} + +bool ProxyLayer::transform(float offset, float scale) +{ + if (_implementation.valid()) return _implementation->transform(offset,scale); + else return false; +} + +bool ProxyLayer::getValue(unsigned int i, unsigned int j, float& value) const +{ + if (_implementation.valid()) return _implementation->getValue(i,j,value); + else return false; +} + +bool ProxyLayer::getValue(unsigned int i, unsigned int j, osg::Vec2& value) const +{ + if (_implementation.valid()) return _implementation->getValue(i,j,value); + else return false; +} + +bool ProxyLayer::getValue(unsigned int i, unsigned int j, osg::Vec3& value) const +{ + if (_implementation.valid()) return _implementation->getValue(i,j,value); + else return false; +} + +bool ProxyLayer::getValue(unsigned int i, unsigned int j, osg::Vec4& value) const +{ + if (_implementation.valid()) return _implementation->getValue(i,j,value); + else return false; +} + +void ProxyLayer::dirty() +{ + if (_implementation.valid()) _implementation->dirty(); +} + +void ProxyLayer::setModifiedCount(unsigned int value) +{ + if (_implementation.valid()) _implementation->setModifiedCount(value); +}; + +unsigned int ProxyLayer::getModifiedCount() const +{ + return _implementation.valid() ? _implementation->getModifiedCount() : 0; +} + + +osg::BoundingSphere ProxyLayer::computeBound() const +{ + if (_implementation.valid()) return _implementation->computeBound(); +} +