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.
This commit is contained in:
Robert Osfield 2008-01-14 14:53:49 +00:00
parent 767bdf4d21
commit 828851c08a
6 changed files with 118 additions and 44 deletions

View File

@ -307,35 +307,40 @@ class OSGTERRAIN_EXPORT ProxyLayer : public Layer
META_Object(osgTerrain, ProxyLayer); META_Object(osgTerrain, ProxyLayer);
/** Return if this ProxyLayer is attached to valid file handle.*/ /** Set the implementation layer that does the actual work.*/
virtual bool isOpen() const { return false; } void setImplementation(Layer* layer) { _implementation = layer; }
/** Open a file.*/ /** Get the implementation layer that does the actual work.*/
void openFile(const std::string& fileName) Layer* getImplementation() { return _implementation.get(); }
{
if (_filename!=fileName)
{
if (isOpen()) close();
_filename = fileName; /** Get the const implementation layer that does the actual work.*/
} const Layer* getImplementation() const { return _implementation.get(); }
if (!isOpen()) open(); virtual void setFileName(const std::string& filename);
} virtual const std::string& getFileName() const { return _filename; }
/** Open the any associated file handle.*/ virtual unsigned int getNumColumns() const;
virtual void open() {} virtual unsigned int getNumRows() const;
/** Open the any associated file handle.*/ virtual bool transform(float offset, float scale);
virtual void close() {}
/** Extract an ImageLayer from the ProxyLayer.*/ virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
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 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;
virtual void dirty();
virtual void setModifiedCount(unsigned int value);
virtual unsigned int getModifiedCount() const;
virtual osg::BoundingSphere computeBound() const;
protected: protected:
virtual ~ProxyLayer(); virtual ~ProxyLayer();
osg::ref_ptr<Layer> _implementation;
}; };

View File

@ -32,11 +32,12 @@ _dataset(0), _gdalReader(0)
DataSetLayer::DataSetLayer(const std::string& fileName): DataSetLayer::DataSetLayer(const std::string& fileName):
_dataset(0), _gdalReader(0) _dataset(0), _gdalReader(0)
{ {
openFile(fileName); setFileName(fileName);
open();
} }
DataSetLayer::DataSetLayer(const DataSetLayer& dataSetLayer,const osg::CopyOp& copyop): DataSetLayer::DataSetLayer(const DataSetLayer& dataSetLayer,const osg::CopyOp& copyop):
ProxyLayer(dataSetLayer), _gdalReader(dataSetLayer._gdalReader) Layer(dataSetLayer), _gdalReader(dataSetLayer._gdalReader)
{ {
if (dataSetLayer._dataset) open(); if (dataSetLayer._dataset) open();
} }
@ -52,6 +53,9 @@ void DataSetLayer::open()
if (getFileName().empty()) return; if (getFileName().empty()) return;
osg::notify(osg::NOTICE)<<"DataSetLayer::open()"<<getFileName()<<std::endl;
_dataset = static_cast<GDALDataset*>(GDALOpen(getFileName().c_str(),GA_ReadOnly)); _dataset = static_cast<GDALDataset*>(GDALOpen(getFileName().c_str(),GA_ReadOnly));
setUpLocator(); setUpLocator();
@ -59,6 +63,8 @@ void DataSetLayer::open()
void DataSetLayer::close() void DataSetLayer::close()
{ {
osg::notify(osg::NOTICE)<<"DataSetLayer::close()"<<getFileName()<<std::endl;
if (_dataset) if (_dataset)
{ {
GDALClose(static_cast<GDALDatasetH>(_dataset)); GDALClose(static_cast<GDALDatasetH>(_dataset));

View File

@ -21,7 +21,7 @@
namespace GDALPlugin { namespace GDALPlugin {
class DataSetLayer : public osgTerrain::ProxyLayer class DataSetLayer : public osgTerrain::Layer
{ {
public: public:

View File

@ -75,21 +75,14 @@ bool CompositeLayer_readLocalData(osg::Object& obj, osgDB::Input &fr)
} }
else if (fr.matchSequence("ProxyLayer %s") || fr.matchSequence("ProxyLayer %w")) else if (fr.matchSequence("ProxyLayer %s") || fr.matchSequence("ProxyLayer %w"))
{ {
osg::ref_ptr<osg::Object> image = osgDB::readObjectFile(std::string(fr[1].getStr())+".gdal"); osgTerrain::ProxyLayer* proxyLayer = new osgTerrain::ProxyLayer;
osgTerrain::ProxyLayer* proxyLayer = dynamic_cast<osgTerrain::ProxyLayer*>(image.get()); proxyLayer->setFileName(fr[1].getStr());
if (proxyLayer)
{
if (locator.valid())
{
proxyLayer->setLocator(locator.get());
locator = 0;
}
if (locator.valid()) proxyLayer->setLocator(locator.get());
if (minLevel!=0) proxyLayer->setMinLevel(minLevel); if (minLevel!=0) proxyLayer->setMinLevel(minLevel);
if (maxLevel!=MAXIMUM_NUMBER_OF_LEVELS) proxyLayer->setMaxLevel(maxLevel); if (maxLevel!=MAXIMUM_NUMBER_OF_LEVELS) proxyLayer->setMaxLevel(maxLevel);
layer.addLayer(proxyLayer); layer.addLayer(proxyLayer);
}
fr += 2; fr += 2;

View File

@ -151,16 +151,14 @@ bool Terrain_readLocalData(osg::Object& obj, osgDB::Input &fr)
if (fr.matchSequence("ProxyLayer %s") || fr.matchSequence("ProxyLayer %w") ) if (fr.matchSequence("ProxyLayer %s") || fr.matchSequence("ProxyLayer %w") )
{ {
osg::ref_ptr<osg::Object> image = osgDB::readObjectFile(std::string(fr[1].getStr())+".gdal"); osgTerrain::ProxyLayer* proxyLayer = new osgTerrain::ProxyLayer;
osgTerrain::ProxyLayer* proxyLayer = dynamic_cast<osgTerrain::ProxyLayer*>(image.get()); proxyLayer->setFileName(fr[1].getStr());
if (proxyLayer)
{
if (locator) proxyLayer->setLocator(locator); if (locator) proxyLayer->setLocator(locator);
if (minLevel!=0) proxyLayer->setMinLevel(minLevel); if (minLevel!=0) proxyLayer->setMinLevel(minLevel);
if (maxLevel!=MAXIMUM_NUMBER_OF_LEVELS) proxyLayer->setMaxLevel(maxLevel); if (maxLevel!=MAXIMUM_NUMBER_OF_LEVELS) proxyLayer->setMaxLevel(maxLevel);
terrain.setElevationLayer(proxyLayer); terrain.setElevationLayer(proxyLayer);
}
fr += 2; fr += 2;

View File

@ -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();
}