1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSGVOLUME_LAYER
15#define OSGVOLUME_LAYER 1
18#include <osg/TransferFunction>
20#include <osgVolume/Locator>
21#include <osgVolume/Property>
25/** Data strucutre for passing details about the loading imagery on to osgVolume for use when setting up dimensions etc.*/
26class OSGVOLUME_EXPORT ImageDetails : public osg::Object
32 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
33 ImageDetails(const ImageDetails&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
35 META_Object(osgVolume, ImageDetails);
37 void setTexelOffset(const osg::Vec4& offset) { _texelOffset = offset; }
38 const osg::Vec4& getTexelOffset() const { return _texelOffset; }
40 void setTexelScale(const osg::Vec4& scale) { _texelScale = scale; }
41 const osg::Vec4& getTexelScale() const { return _texelScale; }
43 void setMatrix(osg::RefMatrix* matrix) { _matrix = matrix; }
44 osg::RefMatrix* getMatrix() { return _matrix.get(); }
45 const osg::RefMatrix* getMatrix() const { return _matrix.get(); }
49 osg::Vec4 _texelOffset;
50 osg::Vec4 _texelScale;
51 osg::ref_ptr<osg::RefMatrix> _matrix;
55/** Base class for representing a single layer of volume data.*/
56class OSGVOLUME_EXPORT Layer : public osg::Object
62 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
63 Layer(const Layer&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
65 META_Object(osgVolume, Layer);
67 /** Set the file name of the data associated with this layer. */
68 virtual void setFileName(const std::string& filename) { _filename = filename; }
70 /** Get the file name of the layer. */
71 virtual const std::string& getFileName() const { return _filename; }
73 void setLocator(Locator* locator) { _locator = locator; }
75 template<class T> void setLocator(const osg::ref_ptr<T>& locator) { setLocator(locator.get()); }
77 Locator* getLocator() { return _locator.get(); }
78 const Locator* getLocator() const { return _locator.get(); }
80 void setDefaultValue(const osg::Vec4& value) { _defaultValue = value; }
81 const osg::Vec4& getDefaultValue() const { return _defaultValue; }
83 /** Set the minification texture filter to use when do texture associated with this layer.*/
84 void setMinFilter(osg::Texture::FilterMode filter) { _minFilter = filter; }
86 /** Get the minification texture filter to use when do texture associated with this layer.*/
87 osg::Texture::FilterMode getMinFilter() const { return _minFilter; }
89 /** Set the magniification texture filter to use when do texture associated with this layer.*/
90 void setMagFilter(osg::Texture::FilterMode filter) { _magFilter = filter; }
92 /** Get the magnification texture filter to use when do texture associated with this layer.*/
93 osg::Texture::FilterMode getMagFilter() const { return _magFilter; }
95 /** Return image associated with layer if supported. */
96 virtual osg::Image* getImage() { return 0; }
98 /** Return const image associated with layer if supported. */
99 virtual const osg::Image* getImage() const { return 0; }
102 /** Set the Property (or Properties via the CompositeProperty) that informs the VolumeTechnique how this layer should be rendered.*/
103 void setProperty(Property* property) { _property = property; }
105 template<class T> void setProperty(const osg::ref_ptr<T>& p) { setProperty(p.get()); }
107 /** Get the Property that informs the VolumeTechnique how this layer should be rendered.*/
108 Property* getProperty() { return _property.get(); }
110 /** Get the const Property that informs the VolumeTechnique how this layer should be rendered.*/
111 const Property* getProperty() const { return _property.get(); }
113 /** Add a property, automatically creating a CompositePorperty if one isn't already assigned.*/
114 void addProperty(Property* property);
116 template<class T> void addProperty(const osg::ref_ptr<T>& p) { addProperty(p.get()); }
118 /** Specify whether ImageLayer requires update traversal. */
119 virtual bool requiresUpdateTraversal() const { return false; }
121 /** Call update on the Layer.*/
122 virtual void update(osg::NodeVisitor& /*nv*/) {}
124 /** increment the modified count."*/
125 virtual void dirty() {};
127 /** Set the modified count value. */
128 virtual void setModifiedCount(unsigned int /*value*/) {};
130 /** Get modified count value. */
131 virtual unsigned int getModifiedCount() const { return 0; }
133 virtual osg::BoundingSphere computeBound() const;
139 std::string _filename;
140 osg::ref_ptr<Locator> _locator;
141 osg::Vec4 _defaultValue;
142 osg::Texture::FilterMode _minFilter;
143 osg::Texture::FilterMode _magFilter;
145 osg::ref_ptr<Property> _property;
149class OSGVOLUME_EXPORT ImageLayer : public Layer
153 ImageLayer(osg::Image* image=0);
155 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
156 ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
158 META_Object(osgVolume, ImageLayer);
160 void setFileName(const std::string& filename) { _filename = filename; if (_image.valid()) _image->setFileName(filename); }
161 virtual const std::string& getFileName() const { return _image.get() ? _image->getFileName() : _filename; }
163 void setImage(osg::Image* image);
165 template<class T> void setImage(const osg::ref_ptr<T>& image) { setImage(image.get()); }
167 /** Return image associated with layer. */
168 virtual osg::Image* getImage() { return _image.get(); }
170 /** Return const image associated with layer. */
171 virtual const osg::Image* getImage() const { return _image.get(); }
174 void setTexelOffset(const osg::Vec4& offset) { _texelOffset = offset; }
175 const osg::Vec4& getTexelOffset() const { return _texelOffset; }
177 void setTexelScale(const osg::Vec4& scale) { _texelScale = scale; }
178 const osg::Vec4& getTexelScale() const { return _texelScale; }
181 /** Compute the min and max pixel colors.*/
182 bool computeMinMax(osg::Vec4& min, osg::Vec4& max);
184 /** Apply color transformation to pixels using c' = offset + c * scale .*/
185 void offsetAndScaleImage(const osg::Vec4& offset, const osg::Vec4& scale);
187 /** Compute the min max range of the image, and then remap this to a 0 to 1 range.*/
188 void rescaleToZeroToOneRange();
190 /** Compute the min color component of the image and then translate and pixels by this offset to make the new min component 0.*/
191 void translateMinToZero();
193 virtual bool requiresUpdateTraversal() const;
195 virtual void update(osg::NodeVisitor& /*nv*/);
197 virtual void dirty();
198 virtual void setModifiedCount(unsigned int value);
199 virtual unsigned int getModifiedCount() const;
203 virtual ~ImageLayer() {}
205 osg::Vec4 _texelOffset;
206 osg::Vec4 _texelScale;
207 osg::ref_ptr<osg::Image> _image;
211class OSGVOLUME_EXPORT CompositeLayer : public Layer
217 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
218 CompositeLayer(const CompositeLayer& compositeLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
220 META_Object(osgVolume, CompositeLayer);
224 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); }
225 const std::string& getFileName(unsigned int i) const { return _layers[i].layer.valid() ? _layers[i].layer->getFileName() : _layers[i].filename; }
227 void setLayer(unsigned int i, Layer* layer) { if (i>=_layers.size()) _layers.resize(i+1); _layers[i].layer = layer; }
229 template<class T> void setLayer(unsigned int i, const osg::ref_ptr<T>& layer) { setLayer(i, layer.get()); }
231 Layer* getLayer(unsigned int i) { return i<_layers.size() ? _layers[i].layer.get() : 0; }
233 const Layer* getLayer(unsigned int i) const { return i<_layers.size() ? _layers[i].layer.get() : 0; }
235 void addLayer(Layer* layer) { _layers.push_back(NameLayer(layer->getFileName(),layer)); }
237 template<class T> void addLayer(const osg::ref_ptr<T>& layer) { addLayer(layer.get()); }
239 void removeLayer(unsigned int i) { _layers.erase(_layers.begin()+i); }
241 unsigned int getNumLayers() const { return _layers.size(); }
243 bool requiresUpdateTraversal() const;
245 virtual void update(osg::NodeVisitor& /*nv*/);
249 virtual ~CompositeLayer() {}
255 NameLayer(const NameLayer& cnl):
256 filename(cnl.filename),
259 NameLayer(const std::string& fn, Layer* l):
263 NameLayer& operator = (const NameLayer& cnl)
265 if (&cnl==this) return *this;
267 filename = cnl.filename;
272 std::string filename;
273 osg::ref_ptr<Layer> layer;
276 typedef std::vector< NameLayer > Layers;
281/** Compute a 3d image that represent the normal map of the specified 3d image.*/
282extern OSGVOLUME_EXPORT osg::Image* createNormalMapTexture(osg::Image* image_3d);
284/** Create an image that has a transfer function applied specified Image.*/
285extern OSGVOLUME_EXPORT osg::Image* applyTransferFunction(osg::Image* image, osg::TransferFunction1D* transferFunction);