2012-03-22 01:36:20 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2007-03-14 20:00:54 +08:00
*
2012-03-22 01:36:20 +08:00
* 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
2007-03-14 20:00:54 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2012-03-22 01:36:20 +08:00
*
2007-03-14 20:00:54 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2012-03-22 01:36:20 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2007-03-14 20:00:54 +08:00
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGTERRAIN_LAYER
#define OSGTERRAIN_LAYER 1
2007-03-22 01:18:53 +08:00
#include <osg/Image>
#include <osg/Shape>
#include <osg/Array>
2008-02-22 19:52:23 +08:00
#include <osg/TransferFunction>
2007-03-14 20:00:54 +08:00
#include <osgTerrain/Locator>
2007-05-12 02:25:06 +08:00
#include <osgTerrain/ValidDataOperator>
2007-03-14 20:00:54 +08:00
namespace osgTerrain {
2007-12-20 23:51:13 +08:00
#define MAXIMUM_NUMBER_OF_LEVELS 30
2008-08-29 00:15:57 +08:00
/** Extact the setname and filename from a compound string in the from set:setname:filename".
* Returns a setname of "" when non set:setname: entry is present.*/
2008-09-01 17:57:35 +08:00
extern OSGTERRAIN_EXPORT void extractSetNameAndFileName(const std::string& compoundstring, std::string& setname, std::string& filename);
2008-08-29 00:15:57 +08:00
/** Create a compound string in the form set:setname:filename, or just filename if setname is "".*/
2010-12-08 18:34:29 +08:00
extern OSGTERRAIN_EXPORT std::string createCompoundSetNameAndFileName(const std::string& setname, const std::string& filename);
2008-08-29 00:15:57 +08:00
2007-03-14 20:00:54 +08:00
class OSGTERRAIN_EXPORT Layer : public osg::Object
{
public:
Layer();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
Layer(const Layer&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
2012-03-22 01:36:20 +08:00
2007-03-26 23:52:22 +08:00
META_Object(osgTerrain, Layer);
2012-03-22 01:36:20 +08:00
2010-12-08 18:34:29 +08:00
/** Set the name of this layer. */
2008-09-02 20:55:26 +08:00
void setSetName(const std::string& setname) { setName(setname); }
2010-12-08 18:34:29 +08:00
/** Get the name of this layer. */
2008-09-02 20:55:26 +08:00
const std::string& getSetName() const { return getName(); }
2008-02-20 20:34:13 +08:00
/** Set the file name of the data associated with this layer. */
2007-08-17 02:49:46 +08:00
virtual void setFileName(const std::string& filename) { _filename = filename; }
2008-02-20 20:34:13 +08:00
/** Get the file name of the layer. */
2007-08-17 02:49:46 +08:00
virtual const std::string& getFileName() const { return _filename; }
2008-08-29 00:15:57 +08:00
/** Return the compound name of the layer in the form set::name::filename string.*/
2010-12-08 18:34:29 +08:00
std::string getCompoundName() const { return createCompoundSetNameAndFileName(getName(), getFileName()); }
2008-02-20 20:34:13 +08:00
2007-03-14 20:00:54 +08:00
void setLocator(Locator* locator) { _locator = locator; }
Locator* getLocator() { return _locator.get(); }
const Locator* getLocator() const { return _locator.get(); }
2012-03-22 01:36:20 +08:00
2007-12-20 23:51:13 +08:00
void setMinLevel(unsigned int minLevel) { _minLevel = minLevel; }
unsigned int getMinLevel() const { return _minLevel; }
void setMaxLevel(unsigned int maxLevel) { _maxLevel = maxLevel; }
unsigned int getMaxLevel() const { return _maxLevel; }
2007-03-14 20:00:54 +08:00
2010-12-08 18:34:29 +08:00
/** Set the data validation operator. */
2007-05-12 02:25:06 +08:00
void setValidDataOperator(ValidDataOperator* validDataOp) { _validDataOperator = validDataOp; }
2010-12-08 18:34:29 +08:00
/** Get the data validation operator. */
2007-05-12 02:25:06 +08:00
ValidDataOperator* getValidDataOperator() { return _validDataOperator.get(); }
2010-12-08 18:34:29 +08:00
/** Get the const data validation operator. */
2007-05-12 02:25:06 +08:00
const ValidDataOperator* getValidDataOperator() const { return _validDataOperator.get(); }
2010-12-08 18:34:29 +08:00
/** Get the number of columns. */
2007-03-30 03:42:07 +08:00
virtual unsigned int getNumColumns() const { return 0; }
2010-12-08 18:34:29 +08:00
/** Get the number of rows. */
2007-03-30 03:42:07 +08:00
virtual unsigned int getNumRows() const { return 0; }
2007-03-30 22:57:57 +08:00
void setDefaultValue(const osg::Vec4& value) { _defaultValue = value; }
const osg::Vec4& getDefaultValue() const { return _defaultValue; }
2012-03-22 01:36:20 +08:00
2008-02-22 19:52:23 +08:00
2010-12-08 18:34:29 +08:00
/** Set the minification texture filter to use when a texture is associated with this layer.*/
2008-10-20 16:43:25 +08:00
void setMinFilter(osg::Texture::FilterMode filter) { _minFilter = filter; }
2010-12-08 18:34:29 +08:00
/** Get the minification texture filter to use when a texture is associated with this layer.*/
2008-10-20 16:43:25 +08:00
osg::Texture::FilterMode getMinFilter() const { return _minFilter; }
2010-12-08 18:34:29 +08:00
/** Set the magnification texture filter to use when a texture is associated with this layer.*/
2008-10-20 16:43:25 +08:00
void setMagFilter(osg::Texture::FilterMode filter) { _magFilter = filter; }
2010-12-08 18:34:29 +08:00
/** Get the magnification texture filter to use when a texture is associated with this layer.*/
2008-10-20 16:43:25 +08:00
osg::Texture::FilterMode getMagFilter() const { return _magFilter; }
2008-02-22 19:52:23 +08:00
2012-03-22 01:36:20 +08:00
/** Return image associated with layer if supported. */
2008-02-22 19:52:23 +08:00
virtual osg::Image* getImage() { return 0; }
2012-03-22 01:36:20 +08:00
/** Return const image associated with layer if supported. */
2008-02-22 19:52:23 +08:00
virtual const osg::Image* getImage() const { return 0; }
2009-01-07 18:32:59 +08:00
virtual bool transform(float /*offset*/, float /*scale*/) { return false; }
2007-03-30 22:57:57 +08:00
2010-12-08 18:34:29 +08:00
/**
* Get the layer value at position i,j.
* @param[in] i X-axis (or column) index.
* @param[in] j Y-axis (or row) index.
* @param[out] value Returned layer value.
* @return true if value is valid, else false
*/
2007-03-30 03:42:07 +08:00
virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, float& /*value*/) const { return false; }
virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec2& /*value*/) const { return false; }
virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec3& /*value*/) const { return false; }
virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec4& /*value*/) const { return false; }
2007-05-12 02:25:06 +08:00
inline bool getValidValue(unsigned int i, unsigned int j, float& value) const
{
if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
return false;
}
inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec2& value) const
{
if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
return false;
}
inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec3& value) const
{
if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
return false;
}
inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec4& value) const
{
if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
return false;
}
2010-12-08 18:34:29 +08:00
/**
* Compute column,row indices from normalized coordinates.
* @param[in] ndc_x Normalized X-axis coordinate.
* @param[in] ndc_y Normalized Y-axis coordinate.
* @param[out] i Returned X-axis (or column) index.
* @param[out] j Returned Y-axis (or row) index.
* @param[out] ir Returned X-axis fraction.
* @param[out] jr Returned Y-axis fraction.
*/
2007-03-30 03:42:07 +08:00
inline void computeIndices(double ndc_x, double ndc_y, unsigned int& i, unsigned int& j, double& ir, double& jr)
{
ndc_x *= double(getNumColumns()-1);
ndc_y *= double(getNumRows()-1);
i = (unsigned int)(ndc_x);
j = (unsigned int)(ndc_y);
ir = ndc_x - double(i);
jr = ndc_y - double(j);
2012-03-22 01:36:20 +08:00
}
2007-03-30 03:42:07 +08:00
2010-12-08 18:34:29 +08:00
/**
* Calculate the interpolated layer value at the given normalized coordinates.
* @param[in] ndc_x Normalized X-axis coordinate.
* @param[in] ndc_y Normalized Y-axis coordinate.
* @param[out] value Returned layer value.
* @return true if value is valid, else false
*/
2007-03-30 03:42:07 +08:00
inline bool getInterpolatedValue(double ndc_x, double ndc_y, float& value)
{
unsigned int i,j;
double ir, jr;
computeIndices(ndc_x, ndc_y, i, j, ir, jr);
value = 0.0f;
double div = 0.0f;
float v,r;
2012-03-22 01:36:20 +08:00
2007-03-30 03:42:07 +08:00
r = (1.0f-ir)*(1.0f-jr);
2012-03-22 01:36:20 +08:00
if (r>0.0 && getValue(i,j,v))
2007-03-30 03:42:07 +08:00
{
value += v*r;
div += r;
}
2012-03-22 01:36:20 +08:00
2007-03-30 03:42:07 +08:00
r = (ir)*(1.0f-jr);
2012-03-22 01:36:20 +08:00
if (r>0.0 && getValue(i+1,j,v))
2007-03-30 03:42:07 +08:00
{
value += v*r;
div += r;
}
2012-03-22 01:36:20 +08:00
2007-03-30 03:42:07 +08:00
r = (ir)*(jr);
2012-03-22 01:36:20 +08:00
if (r>0.0 && getValue(i+1,j+1,v))
2007-03-30 03:42:07 +08:00
{
value += v*r;
div += r;
}
2012-03-22 01:36:20 +08:00
2007-03-30 03:42:07 +08:00
r = (1.0f-ir)*(jr);
2012-03-22 01:36:20 +08:00
if (r>0.0 && getValue(i,j+1,v))
2007-03-30 03:42:07 +08:00
{
value += v*r;
div += r;
}
2012-03-22 01:36:20 +08:00
2007-03-30 03:42:07 +08:00
if (div != 0.0)
{
value /= div;
return true;
}
2010-12-08 18:34:29 +08:00
2007-03-30 03:42:07 +08:00
value = 0.0;
return false;
}
2010-03-25 19:11:35 +08:00
inline bool getInterpolatedValidValue(double ndc_x, double ndc_y, float& value)
{
unsigned int i,j;
double ir, jr;
computeIndices(ndc_x, ndc_y, i, j, ir, jr);
value = 0.0f;
double div = 0.0f;
float v,r;
r = (1.0f-ir)*(1.0f-jr);
if (r>0.0 && getValidValue(i,j,v))
{
value += v*r;
div += r;
}
r = (ir)*(1.0f-jr);
if (r>0.0 && getValidValue(i+1,j,v))
{
value += v*r;
div += r;
}
r = (ir)*(jr);
if (r>0.0 && getValidValue(i+1,j+1,v))
{
value += v*r;
div += r;
}
r = (1.0f-ir)*(jr);
if (r>0.0 && getValidValue(i,j+1,v))
{
value += v*r;
div += r;
}
if (div != 0.0)
{
value /= div;
return true;
}
value = 0.0;
return false;
}
2007-07-07 00:47:08 +08:00
/** increment the modified count."*/
2010-12-08 18:34:29 +08:00
virtual void dirty() {}
2007-07-07 00:47:08 +08:00
/** Set the modified count value. */
2010-12-08 18:34:29 +08:00
virtual void setModifiedCount(unsigned int /*value*/) {}
2007-07-07 00:47:08 +08:00
/** Get modified count value. */
virtual unsigned int getModifiedCount() const { return 0; }
2008-01-17 23:37:35 +08:00
virtual osg::BoundingSphere computeBound(bool treatAsElevationLayer) const;
2007-03-28 03:27:36 +08:00
2007-03-14 20:00:54 +08:00
protected:
virtual ~Layer();
2007-08-17 02:49:46 +08:00
std::string _filename;
2007-05-12 02:25:06 +08:00
osg::ref_ptr<Locator> _locator;
2007-12-20 23:51:13 +08:00
unsigned int _minLevel;
unsigned int _maxLevel;
2007-05-12 02:25:06 +08:00
osg::ref_ptr<ValidDataOperator> _validDataOperator;
osg::Vec4 _defaultValue;
2008-10-20 16:43:25 +08:00
osg::Texture::FilterMode _minFilter;
osg::Texture::FilterMode _magFilter;
2007-03-14 20:00:54 +08:00
};
2007-03-22 01:18:53 +08:00
class OSGTERRAIN_EXPORT ImageLayer : public Layer
{
public:
2008-02-22 19:52:23 +08:00
ImageLayer(osg::Image* image=0);
2007-03-22 01:18:53 +08:00
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
2012-03-22 01:36:20 +08:00
2007-08-17 02:49:46 +08:00
META_Object(osgTerrain, ImageLayer);
void setFileName(const std::string& filename) { _filename = filename; if (_image.valid()) _image->setFileName(filename); }
virtual const std::string& getFileName() const { return _image.get() ? _image->getFileName() : _filename; }
2007-05-11 02:07:54 +08:00
virtual bool transform(float offset, float scale);
2007-03-22 01:18:53 +08:00
void setImage(osg::Image* image);
2008-02-22 19:52:23 +08:00
2012-03-22 01:36:20 +08:00
/** Return image associated with layer. */
2008-02-22 19:52:23 +08:00
virtual osg::Image* getImage() { return _image.get(); }
/** Return const image associated with layer. */
virtual const osg::Image* getImage() const { return _image.get(); }
2007-03-22 01:18:53 +08:00
2007-03-30 03:42:07 +08:00
virtual unsigned int getNumColumns() const { return _image.valid() ? _image->s() : 0; }
virtual unsigned int getNumRows() const { return _image.valid() ? _image->t() : 0; }
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;
2007-07-07 00:47:08 +08:00
virtual void dirty();
virtual void setModifiedCount(unsigned int value);
virtual unsigned int getModifiedCount() const;
2007-03-22 01:18:53 +08:00
protected:
virtual ~ImageLayer() {}
2007-08-17 02:49:46 +08:00
osg::ref_ptr<osg::Image> _image;
2007-03-22 01:18:53 +08:00
};
2008-02-22 19:52:23 +08:00
class OSGTERRAIN_EXPORT ContourLayer : public Layer
{
public:
ContourLayer(osg::TransferFunction1D* tf=0);
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ContourLayer(const ContourLayer& tfLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
2012-03-22 01:36:20 +08:00
2008-02-22 19:52:23 +08:00
META_Object(osgTerrain, ContourLayer);
virtual bool transform(float offset, float scale);
void setTransferFunction(osg::TransferFunction1D* tf);
osg::TransferFunction1D* getTransferFunction() { return _tf.get(); }
const osg::TransferFunction1D* getTransferFunction() const { return _tf.get(); }
2012-03-22 01:36:20 +08:00
/** Return image associated with layer. */
2008-02-22 19:52:23 +08:00
virtual osg::Image* getImage() { return _tf.valid() ? _tf->getImage() : 0; }
/** Return const image associated with layer. */
virtual const osg::Image* getImage() const { return _tf.valid() ? _tf->getImage() : 0; }
2009-02-02 22:43:27 +08:00
virtual unsigned int getNumColumns() const { return _tf.valid() ? _tf->getNumberImageCells() : 0; }
2008-02-22 19:52:23 +08:00
virtual unsigned int getNumRows() const { return _tf.valid() ? 1 : 0; }
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;
virtual void dirty();
virtual void setModifiedCount(unsigned int value);
virtual unsigned int getModifiedCount() const;
protected:
virtual ~ContourLayer() {}
osg::ref_ptr<osg::TransferFunction1D> _tf;
};
2007-03-22 01:18:53 +08:00
class OSGTERRAIN_EXPORT HeightFieldLayer : public Layer
{
public:
2008-02-22 19:52:23 +08:00
HeightFieldLayer(osg::HeightField* hf=0);
2007-03-22 01:18:53 +08:00
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
HeightFieldLayer(const HeightFieldLayer& hfLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
2012-03-22 01:36:20 +08:00
2007-08-17 02:49:46 +08:00
META_Object(osgTerrain, HeightFieldLayer);
void setFileName(const std::string& filename) { _filename = filename; }
virtual const std::string& getFileName() const { return _filename; }
2007-05-11 02:07:54 +08:00
virtual bool transform(float offset, float scale);
2007-03-22 01:18:53 +08:00
void setHeightField(osg::HeightField* hf);
osg::HeightField* getHeightField() { return _heightField.get(); }
const osg::HeightField* getHeightField() const { return _heightField.get(); }
2007-03-30 03:42:07 +08:00
virtual unsigned int getNumColumns() const { return _heightField.valid() ? _heightField->getNumColumns() : 0; }
virtual unsigned int getNumRows() const { return _heightField.valid() ? _heightField->getNumRows() : 0; }
2007-03-22 01:18:53 +08:00
2007-03-30 03:42:07 +08:00
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;
2007-03-22 01:18:53 +08:00
2007-07-07 00:47:08 +08:00
virtual void dirty();
virtual void setModifiedCount(unsigned int value);
virtual unsigned int getModifiedCount() const;
2007-03-22 01:18:53 +08:00
protected:
2007-03-30 03:42:07 +08:00
virtual ~HeightFieldLayer() {}
2007-03-22 01:18:53 +08:00
2007-07-07 00:47:08 +08:00
unsigned int _modifiedCount;
osg::ref_ptr<osg::HeightField> _heightField;
2007-03-22 01:18:53 +08:00
};
2007-08-17 02:49:46 +08:00
2007-08-29 18:52:03 +08:00
class OSGTERRAIN_EXPORT ProxyLayer : public Layer
{
public:
ProxyLayer();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ProxyLayer(const ProxyLayer& proxyLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
2012-03-22 01:36:20 +08:00
2007-08-29 18:52:03 +08:00
META_Object(osgTerrain, ProxyLayer);
2012-03-22 01:36:20 +08:00
/** Return image associated with layer if supported. */
2008-09-11 21:21:58 +08:00
virtual osg::Image* getImage()
{
return _implementation.valid() ? _implementation->getImage() : 0;
}
/** Return const image associated with layer if supported. */
virtual const osg::Image* getImage() const
{
return _implementation.valid() ? _implementation->getImage() : 0;
}
2008-01-14 22:53:49 +08:00
/** Set the implementation layer that does the actual work.*/
void setImplementation(Layer* layer) { _implementation = layer; }
2012-03-22 01:36:20 +08:00
2008-01-14 22:53:49 +08:00
/** Get the implementation layer that does the actual work.*/
Layer* getImplementation() { return _implementation.get(); }
2007-08-29 18:52:03 +08:00
2008-01-14 22:53:49 +08:00
/** Get the const implementation layer that does the actual work.*/
const Layer* getImplementation() const { return _implementation.get(); }
2007-08-30 00:29:05 +08:00
2008-01-14 22:53:49 +08:00
virtual void setFileName(const std::string& filename);
virtual const std::string& getFileName() const { return _filename; }
2007-08-30 00:29:05 +08:00
2008-01-14 22:53:49 +08:00
virtual unsigned int getNumColumns() const;
virtual unsigned int getNumRows() const;
2012-03-22 01:36:20 +08:00
2008-01-14 22:53:49 +08:00
virtual bool transform(float offset, float scale);
2007-08-30 00:29:05 +08:00
2008-01-14 22:53:49 +08:00
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;
2007-08-30 00:29:05 +08:00
2008-01-14 22:53:49 +08:00
virtual void dirty();
virtual void setModifiedCount(unsigned int value);
virtual unsigned int getModifiedCount() const;
2007-08-30 00:29:05 +08:00
2008-01-17 23:37:35 +08:00
virtual osg::BoundingSphere computeBound(bool treatAsElevationLayer) const;
2007-08-29 18:52:03 +08:00
protected:
2007-08-29 20:18:41 +08:00
virtual ~ProxyLayer();
2012-03-22 01:36:20 +08:00
2008-01-14 22:53:49 +08:00
osg::ref_ptr<Layer> _implementation;
2007-08-29 18:52:03 +08:00
};
2008-08-29 00:15:57 +08:00
class OSGTERRAIN_EXPORT CompositeLayer : public Layer
2008-08-27 18:13:30 +08:00
{
public:
2008-08-29 00:15:57 +08:00
CompositeLayer();
2008-08-27 18:13:30 +08:00
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
2008-08-29 00:15:57 +08:00
CompositeLayer(const CompositeLayer& compositeLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
2012-03-22 01:36:20 +08:00
2008-08-29 00:15:57 +08:00
META_Object(osgTerrain, CompositeLayer);
2008-08-27 18:13:30 +08:00
void clear();
2010-12-08 18:34:29 +08:00
/** Set the set name of layer 'i'. */
2008-08-29 00:15:57 +08:00
void setSetName(unsigned int i, const std::string& setname) { _layers[i].setname = setname; if (_layers[i].layer.valid()) _layers[i].layer->setName(setname); }
2010-12-08 18:34:29 +08:00
/** Get the set name of layer 'i'. */
2008-08-29 00:15:57 +08:00
const std::string& getSetName(unsigned int i) const { return _layers[i].layer.valid() ? _layers[i].layer->getName() : _layers[i].setname; }
2010-12-08 18:34:29 +08:00
/** Set the file name of the data associated with layer 'i'. */
2008-08-29 00:15:57 +08:00
void setFileName(unsigned int i, const std::string& filename) { _layers[i].filename = filename; if (_layers[i].layer.valid()) _layers[i].layer->setFileName(filename); }
2010-12-08 18:34:29 +08:00
/** Get the file name of the data associated with layer 'i'. */
2008-08-29 00:15:57 +08:00
const std::string& getFileName(unsigned int i) const { return _layers[i].layer.valid() ? _layers[i].layer->getFileName() : _layers[i].filename; }
2008-08-27 18:13:30 +08:00
2008-08-29 00:15:57 +08:00
void setCompoundName(unsigned int i, const std::string& compoundname);
std::string getCompoundName(unsigned int i) const;
2008-08-27 18:13:30 +08:00
2008-08-29 00:15:57 +08:00
2008-09-11 22:28:42 +08:00
void setLayer(unsigned int i, Layer* layer) { if (i>=_layers.size()) _layers.resize(i+1); _layers[i].layer = layer; }
Layer* getLayer(unsigned int i) { return i<_layers.size() ? _layers[i].layer.get() : 0; }
const Layer* getLayer(unsigned int i) const { return i<_layers.size() ? _layers[i].layer.get() : 0; }
2008-08-29 00:15:57 +08:00
void addLayer(const std::string& compoundname);
2008-08-29 17:28:28 +08:00
void addLayer(const std::string& setname, const std::string& filename);
2012-03-22 01:36:20 +08:00
2008-08-29 00:15:57 +08:00
void addLayer(Layer* layer) { _layers.push_back(CompoundNameLayer(layer->getName(),layer->getFileName(),layer)); }
2008-08-27 18:13:30 +08:00
void removeLayer(unsigned int i) { _layers.erase(_layers.begin()+i); }
2012-03-22 01:36:20 +08:00
2008-08-27 18:13:30 +08:00
unsigned int getNumLayers() const { return _layers.size(); }
protected:
2008-08-29 00:15:57 +08:00
virtual ~CompositeLayer() {}
struct CompoundNameLayer
{
CompoundNameLayer() {}
2012-03-22 01:36:20 +08:00
2008-08-29 00:15:57 +08:00
CompoundNameLayer(const CompoundNameLayer& cnl):
setname(cnl.setname),
filename(cnl.filename),
layer(cnl.layer) {}
CompoundNameLayer(const std::string& sn, const std::string& fn, Layer* l):
setname(sn),
filename(fn),
layer(l) {}
CompoundNameLayer& operator = (const CompoundNameLayer& cnl)
{
if (&cnl==this) return *this;
2012-03-22 01:36:20 +08:00
2008-08-29 00:15:57 +08:00
setname = cnl.setname;
filename = cnl.filename;
layer = cnl.layer;
return *this;
}
std::string setname;
std::string filename;
osg::ref_ptr<Layer> layer;
};
2012-03-22 01:36:20 +08:00
2008-08-29 00:15:57 +08:00
typedef std::vector< CompoundNameLayer > Layers;
2012-03-22 01:36:20 +08:00
2008-08-27 18:13:30 +08:00
Layers _layers;
};
2008-08-29 00:15:57 +08:00
class OSGTERRAIN_EXPORT SwitchLayer : public CompositeLayer
{
public:
SwitchLayer();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
SwitchLayer(const SwitchLayer& switchLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
2012-03-22 01:36:20 +08:00
2008-08-29 00:15:57 +08:00
META_Object(osgTerrain, SwitchLayer);
2012-03-22 01:36:20 +08:00
2008-08-29 00:15:57 +08:00
void setActiveLayer(int i) { _activeLayer = i; }
int getActiveLayer() const { return _activeLayer; }
2012-03-22 01:36:20 +08:00
/** Return image associated with layer if supported. */
2008-09-11 21:21:58 +08:00
virtual osg::Image* getImage()
{
2012-03-22 01:36:20 +08:00
if (_activeLayer < 0) return 0;
2008-09-11 21:21:58 +08:00
if (_activeLayer >= static_cast<int>(getNumLayers())) return 0;
2012-03-22 01:36:20 +08:00
return _layers[_activeLayer].layer->getImage();
2008-09-11 21:21:58 +08:00
}
/** Return const image associated with layer if supported. */
virtual const osg::Image* getImage() const
{
2012-03-22 01:36:20 +08:00
if (_activeLayer < 0) return 0;
2008-09-11 21:21:58 +08:00
if (_activeLayer >= static_cast<int>(getNumLayers())) return 0;
2012-03-22 01:36:20 +08:00
return _layers[_activeLayer].layer->getImage();
2008-09-11 21:21:58 +08:00
}
2008-08-29 00:15:57 +08:00
protected:
virtual ~SwitchLayer() {}
2012-03-22 01:36:20 +08:00
2008-08-29 00:15:57 +08:00
int _activeLayer;
};
2007-03-14 20:00:54 +08:00
}
#endif