2012-03-22 01:36:20 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
2009-01-05 19:26:26 +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
2009-01-05 19:26:26 +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
*
2009-01-05 19:26:26 +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
2009-01-05 19:26:26 +08:00
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGVOLUME_LAYER
#define OSGVOLUME_LAYER 1
#include <osg/Image>
2009-01-17 01:59:38 +08:00
#include <osg/TransferFunction>
2009-01-05 19:26:26 +08:00
#include <osgVolume/Locator>
2009-01-14 23:16:29 +08:00
#include <osgVolume/Property>
2009-01-05 19:26:26 +08:00
namespace osgVolume {
2009-09-01 18:48:32 +08:00
/** Data strucutre for passing details about the loading imagery on to osgVolume for use when setting up dimensions etc.*/
class OSGVOLUME_EXPORT ImageDetails : public osg::Object
{
public:
ImageDetails();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ImageDetails(const ImageDetails&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgVolume, ImageDetails);
2009-09-03 21:40:50 +08:00
void setTexelOffset(const osg::Vec4& offset) { _texelOffset = offset; }
const osg::Vec4& getTexelOffset() const { return _texelOffset; }
2009-09-01 18:48:32 +08:00
2009-09-03 21:40:50 +08:00
void setTexelScale(const osg::Vec4& scale) { _texelScale = scale; }
const osg::Vec4& getTexelScale() const { return _texelScale; }
2009-09-01 18:48:32 +08:00
void setMatrix(osg::RefMatrix* matrix) { _matrix = matrix; }
osg::RefMatrix* getMatrix() { return _matrix.get(); }
const osg::RefMatrix* getMatrix() const { return _matrix.get(); }
protected:
2009-09-03 21:40:50 +08:00
osg::Vec4 _texelOffset;
osg::Vec4 _texelScale;
2009-09-01 18:48:32 +08:00
osg::ref_ptr<osg::RefMatrix> _matrix;
};
/** Base class for representing a single layer of volume data.*/
2009-01-05 19:26:26 +08:00
class OSGVOLUME_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);
2009-09-01 18:48:32 +08:00
2009-01-05 19:26:26 +08:00
META_Object(osgVolume, Layer);
2009-09-01 18:48:32 +08:00
2009-01-05 19:26:26 +08:00
/** Set the file name of the data associated with this layer. */
virtual void setFileName(const std::string& filename) { _filename = filename; }
/** Get the file name of the layer. */
virtual const std::string& getFileName() const { return _filename; }
void setLocator(Locator* locator) { _locator = locator; }
2015-10-22 22:14:53 +08:00
template<class T> void setLocator(const osg::ref_ptr<T>& locator) { setLocator(locator.get()); }
2009-01-05 19:26:26 +08:00
Locator* getLocator() { return _locator.get(); }
const Locator* getLocator() const { return _locator.get(); }
2009-09-01 18:48:32 +08:00
2009-01-05 19:26:26 +08:00
void setDefaultValue(const osg::Vec4& value) { _defaultValue = value; }
const osg::Vec4& getDefaultValue() const { return _defaultValue; }
/** Set the minification texture filter to use when do texture associated with this layer.*/
void setMinFilter(osg::Texture::FilterMode filter) { _minFilter = filter; }
/** Get the minification texture filter to use when do texture associated with this layer.*/
osg::Texture::FilterMode getMinFilter() const { return _minFilter; }
/** Set the magniification texture filter to use when do texture associated with this layer.*/
void setMagFilter(osg::Texture::FilterMode filter) { _magFilter = filter; }
/** Get the magnification texture filter to use when do texture associated with this layer.*/
osg::Texture::FilterMode getMagFilter() const { return _magFilter; }
2009-09-01 18:48:32 +08:00
/** Return image associated with layer if supported. */
2009-01-05 19:26:26 +08:00
virtual osg::Image* getImage() { return 0; }
2009-09-01 18:48:32 +08:00
/** Return const image associated with layer if supported. */
2009-01-05 19:26:26 +08:00
virtual const osg::Image* getImage() const { return 0; }
2009-01-14 23:16:29 +08:00
/** Set the Property (or Properties via the CompositeProperty) that informs the VolumeTechnique how this layer should be rendered.*/
void setProperty(Property* property) { _property = property; }
2009-01-05 19:26:26 +08:00
2015-10-22 22:14:53 +08:00
template<class T> void setProperty(const osg::ref_ptr<T>& p) { setProperty(p.get()); }
2009-01-14 23:16:29 +08:00
/** Get the Property that informs the VolumeTechnique how this layer should be rendered.*/
Property* getProperty() { return _property.get(); }
2009-01-05 19:26:26 +08:00
2009-01-14 23:16:29 +08:00
/** Get the const Property that informs the VolumeTechnique how this layer should be rendered.*/
const Property* getProperty() const { return _property.get(); }
2009-01-05 19:26:26 +08:00
2009-01-15 23:57:04 +08:00
/** Add a property, automatically creating a CompositePorperty if one isn't already assigned.*/
void addProperty(Property* property);
2015-10-22 22:14:53 +08:00
template<class T> void addProperty(const osg::ref_ptr<T>& p) { addProperty(p.get()); }
2009-01-05 19:26:26 +08:00
2009-02-11 02:51:43 +08:00
/** Specify whether ImageLayer requires update traversal. */
virtual bool requiresUpdateTraversal() const { return false; }
/** Call update on the Layer.*/
2009-09-01 18:48:32 +08:00
virtual void update(osg::NodeVisitor& /*nv*/) {}
2009-02-11 02:51:43 +08:00
2009-01-05 19:26:26 +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; }
virtual osg::BoundingSphere computeBound() const;
protected:
virtual ~Layer();
std::string _filename;
osg::ref_ptr<Locator> _locator;
osg::Vec4 _defaultValue;
osg::Texture::FilterMode _minFilter;
osg::Texture::FilterMode _magFilter;
2009-09-01 18:48:32 +08:00
2009-01-14 23:16:29 +08:00
osg::ref_ptr<Property> _property;
2009-01-05 19:26:26 +08:00
};
class OSGVOLUME_EXPORT ImageLayer : public Layer
{
public:
ImageLayer(osg::Image* image=0);
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
2009-09-01 18:48:32 +08:00
2009-01-05 19:26:26 +08:00
META_Object(osgVolume, 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; }
void setImage(osg::Image* image);
2015-10-22 22:14:53 +08:00
template<class T> void setImage(const osg::ref_ptr<T>& image) { setImage(image.get()); }
2009-09-01 18:48:32 +08:00
/** Return image associated with layer. */
2009-01-05 19:26:26 +08:00
virtual osg::Image* getImage() { return _image.get(); }
/** Return const image associated with layer. */
virtual const osg::Image* getImage() const { return _image.get(); }
2009-09-01 18:48:32 +08:00
2009-09-03 21:40:50 +08:00
void setTexelOffset(const osg::Vec4& offset) { _texelOffset = offset; }
const osg::Vec4& getTexelOffset() const { return _texelOffset; }
2009-09-01 18:48:32 +08:00
2009-09-03 21:40:50 +08:00
void setTexelScale(const osg::Vec4& scale) { _texelScale = scale; }
const osg::Vec4& getTexelScale() const { return _texelScale; }
2009-09-01 18:48:32 +08:00
2009-01-16 18:23:17 +08:00
/** Compute the min and max pixel colors.*/
bool computeMinMax(osg::Vec4& min, osg::Vec4& max);
2009-09-01 18:48:32 +08:00
2009-01-16 18:23:17 +08:00
/** Apply color transformation to pixels using c' = offset + c * scale .*/
void offsetAndScaleImage(const osg::Vec4& offset, const osg::Vec4& scale);
2009-09-01 18:48:32 +08:00
2009-01-16 18:23:17 +08:00
/** Compute the min max range of the image, and then remap this to a 0 to 1 range.*/
void rescaleToZeroToOneRange();
2009-09-01 18:48:32 +08:00
2009-01-16 18:23:17 +08:00
/** Compute the min color component of the image and then translate and pixels by this offset to make the new min component 0.*/
void translateMinToZero();
2009-01-05 19:26:26 +08:00
2009-02-11 02:51:43 +08:00
virtual bool requiresUpdateTraversal() const;
virtual void update(osg::NodeVisitor& /*nv*/);
2009-01-05 19:26:26 +08:00
virtual void dirty();
virtual void setModifiedCount(unsigned int value);
virtual unsigned int getModifiedCount() const;
protected:
virtual ~ImageLayer() {}
2009-09-03 21:40:50 +08:00
osg::Vec4 _texelOffset;
osg::Vec4 _texelScale;
2009-01-05 19:26:26 +08:00
osg::ref_ptr<osg::Image> _image;
};
class OSGVOLUME_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);
2009-09-01 18:48:32 +08:00
2009-01-05 19:26:26 +08:00
META_Object(osgVolume, CompositeLayer);
void clear();
2009-05-09 16:49:27 +08:00
void setFileName(unsigned int i, const std::string& filename) { if (i>=_layers.size()) _layers.resize(i+1); _layers[i].filename = filename; if (_layers[i].layer.valid()) _layers[i].layer->setFileName(filename); }
2009-01-05 19:26:26 +08:00
const std::string& getFileName(unsigned int i) const { return _layers[i].layer.valid() ? _layers[i].layer->getFileName() : _layers[i].filename; }
void setLayer(unsigned int i, Layer* layer) { if (i>=_layers.size()) _layers.resize(i+1); _layers[i].layer = layer; }
2015-10-22 22:14:53 +08:00
template<class T> void setLayer(unsigned int i, const osg::ref_ptr<T>& layer) { setLayer(i, layer.get()); }
2009-01-05 19:26:26 +08:00
Layer* getLayer(unsigned int i) { return i<_layers.size() ? _layers[i].layer.get() : 0; }
2015-10-22 22:14:53 +08:00
2009-01-05 19:26:26 +08:00
const Layer* getLayer(unsigned int i) const { return i<_layers.size() ? _layers[i].layer.get() : 0; }
void addLayer(Layer* layer) { _layers.push_back(NameLayer(layer->getFileName(),layer)); }
2015-10-22 22:14:53 +08:00
template<class T> void addLayer(const osg::ref_ptr<T>& layer) { addLayer(layer.get()); }
2009-01-05 19:26:26 +08:00
void removeLayer(unsigned int i) { _layers.erase(_layers.begin()+i); }
2009-09-01 18:48:32 +08:00
2009-01-05 19:26:26 +08:00
unsigned int getNumLayers() const { return _layers.size(); }
2009-02-11 02:51:43 +08:00
bool requiresUpdateTraversal() const;
2009-09-01 18:48:32 +08:00
2009-02-11 02:51:43 +08:00
virtual void update(osg::NodeVisitor& /*nv*/);
2009-01-05 19:26:26 +08:00
protected:
virtual ~CompositeLayer() {}
struct NameLayer
{
NameLayer() {}
2009-09-01 18:48:32 +08:00
2009-01-05 19:26:26 +08:00
NameLayer(const NameLayer& cnl):
filename(cnl.filename),
layer(cnl.layer) {}
NameLayer(const std::string& fn, Layer* l):
filename(fn),
layer(l) {}
NameLayer& operator = (const NameLayer& cnl)
{
if (&cnl==this) return *this;
2009-09-01 18:48:32 +08:00
2009-01-05 19:26:26 +08:00
filename = cnl.filename;
layer = cnl.layer;
return *this;
}
std::string filename;
osg::ref_ptr<Layer> layer;
};
2009-09-01 18:48:32 +08:00
2009-01-05 19:26:26 +08:00
typedef std::vector< NameLayer > Layers;
2009-09-01 18:48:32 +08:00
2009-01-05 19:26:26 +08:00
Layers _layers;
};
2009-01-17 01:59:38 +08:00
/** Compute a 3d image that represent the normal map of the specified 3d image.*/
extern OSGVOLUME_EXPORT osg::Image* createNormalMapTexture(osg::Image* image_3d);
/** Create an image that has a transfer function applied specified Image.*/
extern OSGVOLUME_EXPORT osg::Image* applyTransferFunction(osg::Image* image, osg::TransferFunction1D* transferFunction);
2009-01-05 19:26:26 +08:00
}
#endif