2007-03-14 20:00:54 +08:00
/* -*-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 OSGTERRAIN_LAYER
#define OSGTERRAIN_LAYER 1
2007-03-22 01:18:53 +08:00
#include <osg/Image>
#include <osg/Shape>
#include <osg/Array>
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 {
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);
2007-03-26 23:52:22 +08:00
META_Object(osgTerrain, Layer);
2007-08-17 02:49:46 +08:00
virtual void setFileName(const std::string& filename) { _filename = filename; }
virtual const std::string& getFileName() const { return _filename; }
2007-03-26 23:52:22 +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(); }
2007-05-12 02:25:06 +08:00
void setValidDataOperator(ValidDataOperator* validDataOp) { _validDataOperator = validDataOp; }
ValidDataOperator* getValidDataOperator() { return _validDataOperator.get(); }
const ValidDataOperator* getValidDataOperator() const { return _validDataOperator.get(); }
2007-03-30 03:42:07 +08:00
virtual unsigned int getNumColumns() const { return 0; }
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; }
2007-05-11 02:07:54 +08:00
virtual bool transform(float offset, float scale) { return false; }
2007-03-30 22:57:57 +08:00
2007-05-12 02:25:06 +08:00
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;
}
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);
}
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;
r = (1.0f-ir)*(1.0f-jr);
if (r>0.0 && getValue(i,j,v))
{
value += v*r;
div += r;
}
r = (ir)*(1.0f-jr);
if (r>0.0 && getValue(i+1,j,v))
{
value += v*r;
div += r;
}
r = (ir)*(jr);
if (r>0.0 && getValue(i+1,j+1,v))
{
value += v*r;
div += r;
}
r = (1.0f-ir)*(jr);
if (r>0.0 && getValue(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."*/
virtual void dirty() {};
/** Set the modified count value. */
virtual void setModifiedCount(unsigned int /*value*/) {};
/** Get modified count value. */
virtual unsigned int getModifiedCount() const { return 0; }
2007-03-28 03:27:36 +08:00
virtual osg::BoundingSphere computeBound() const;
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;
osg::ref_ptr<ValidDataOperator> _validDataOperator;
osg::Vec4 _defaultValue;
2007-03-14 20:00:54 +08:00
};
2007-03-22 01:18:53 +08:00
class OSGTERRAIN_EXPORT ImageLayer : public Layer
{
public:
ImageLayer();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
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);
osg::Image* getImage() { return _image.get(); }
const osg::Image* getImage() const { return _image.get(); }
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
};
class OSGTERRAIN_EXPORT HeightFieldLayer : public Layer
{
public:
HeightFieldLayer();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
HeightFieldLayer(const HeightFieldLayer& hfLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
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
class OSGTERRAIN_EXPORT CompositeLayer : public Layer
{
public:
CompositeLayer();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
CompositeLayer(const CompositeLayer& compositeLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgTerrain, CompositeLayer);
void clear();
void setFileName(unsigned int i, const std::string& filename) { _layers[i].first = filename; if (_layers[i].second.valid()) _layers[i].second->setFileName(filename); }
const std::string& getFileName(unsigned int i) const { return _layers[i].second.valid() ? _layers[i].second->getFileName() : _layers[i].first; }
void setLayer(unsigned int i, Layer* layer) { _layers[i].second = layer; }
Layer* getLayer(unsigned int i) { return _layers[i].second.get(); }
const Layer* getLayer(unsigned int i) const { return _layers[i].second.get(); }
void addLayer(const std::string& filename) { _layers.push_back(FileNameLayerPair(filename,0)); }
void addLayer(Layer* layer) { _layers.push_back(FileNameLayerPair(layer->getFileName(),layer)); }
void removeLayer(unsigned int i) { _layers.erase(_layers.begin()+i); }
unsigned int getNumLayers() const { return _layers.size(); }
protected:
virtual ~CompositeLayer() {}
typedef std::pair< std::string, osg::ref_ptr<Layer> > FileNameLayerPair;
typedef std::vector< FileNameLayerPair > Layers;
Layers _layers;
};
2007-03-14 20:00:54 +08:00
}
#endif