openscenegraph
osgVolume/Layer
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
2 *
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.
7 *
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.
12*/
13
14#ifndef OSGVOLUME_LAYER
15#define OSGVOLUME_LAYER 1
16
17#include <osg/Image>
18#include <osg/TransferFunction>
19
20#include <osgVolume/Locator>
21#include <osgVolume/Property>
22
23namespace osgVolume {
24
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
27{
28 public:
29
30 ImageDetails();
31
32 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
33 ImageDetails(const ImageDetails&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
34
35 META_Object(osgVolume, ImageDetails);
36
37 void setTexelOffset(const osg::Vec4& offset) { _texelOffset = offset; }
38 const osg::Vec4& getTexelOffset() const { return _texelOffset; }
39
40 void setTexelScale(const osg::Vec4& scale) { _texelScale = scale; }
41 const osg::Vec4& getTexelScale() const { return _texelScale; }
42
43 void setMatrix(osg::RefMatrix* matrix) { _matrix = matrix; }
44 osg::RefMatrix* getMatrix() { return _matrix.get(); }
45 const osg::RefMatrix* getMatrix() const { return _matrix.get(); }
46
47 protected:
48
49 osg::Vec4 _texelOffset;
50 osg::Vec4 _texelScale;
51 osg::ref_ptr<osg::RefMatrix> _matrix;
52
53};
54
55/** Base class for representing a single layer of volume data.*/
56class OSGVOLUME_EXPORT Layer : public osg::Object
57{
58 public:
59
60 Layer();
61
62 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
63 Layer(const Layer&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
64
65 META_Object(osgVolume, Layer);
66
67 /** Set the file name of the data associated with this layer. */
68 virtual void setFileName(const std::string& filename) { _filename = filename; }
69
70 /** Get the file name of the layer. */
71 virtual const std::string& getFileName() const { return _filename; }
72
73 void setLocator(Locator* locator) { _locator = locator; }
74
75 template<class T> void setLocator(const osg::ref_ptr<T>& locator) { setLocator(locator.get()); }
76
77 Locator* getLocator() { return _locator.get(); }
78 const Locator* getLocator() const { return _locator.get(); }
79
80 void setDefaultValue(const osg::Vec4& value) { _defaultValue = value; }
81 const osg::Vec4& getDefaultValue() const { return _defaultValue; }
82
83 /** Set the minification texture filter to use when do texture associated with this layer.*/
84 void setMinFilter(osg::Texture::FilterMode filter) { _minFilter = filter; }
85
86 /** Get the minification texture filter to use when do texture associated with this layer.*/
87 osg::Texture::FilterMode getMinFilter() const { return _minFilter; }
88
89 /** Set the magniification texture filter to use when do texture associated with this layer.*/
90 void setMagFilter(osg::Texture::FilterMode filter) { _magFilter = filter; }
91
92 /** Get the magnification texture filter to use when do texture associated with this layer.*/
93 osg::Texture::FilterMode getMagFilter() const { return _magFilter; }
94
95 /** Return image associated with layer if supported. */
96 virtual osg::Image* getImage() { return 0; }
97
98 /** Return const image associated with layer if supported. */
99 virtual const osg::Image* getImage() const { return 0; }
100
101
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; }
104
105 template<class T> void setProperty(const osg::ref_ptr<T>& p) { setProperty(p.get()); }
106
107 /** Get the Property that informs the VolumeTechnique how this layer should be rendered.*/
108 Property* getProperty() { return _property.get(); }
109
110 /** Get the const Property that informs the VolumeTechnique how this layer should be rendered.*/
111 const Property* getProperty() const { return _property.get(); }
112
113 /** Add a property, automatically creating a CompositePorperty if one isn't already assigned.*/
114 void addProperty(Property* property);
115
116 template<class T> void addProperty(const osg::ref_ptr<T>& p) { addProperty(p.get()); }
117
118 /** Specify whether ImageLayer requires update traversal. */
119 virtual bool requiresUpdateTraversal() const { return false; }
120
121 /** Call update on the Layer.*/
122 virtual void update(osg::NodeVisitor& /*nv*/) {}
123
124 /** increment the modified count."*/
125 virtual void dirty() {};
126
127 /** Set the modified count value. */
128 virtual void setModifiedCount(unsigned int /*value*/) {};
129
130 /** Get modified count value. */
131 virtual unsigned int getModifiedCount() const { return 0; }
132
133 virtual osg::BoundingSphere computeBound() const;
134
135 protected:
136
137 virtual ~Layer();
138
139 std::string _filename;
140 osg::ref_ptr<Locator> _locator;
141 osg::Vec4 _defaultValue;
142 osg::Texture::FilterMode _minFilter;
143 osg::Texture::FilterMode _magFilter;
144
145 osg::ref_ptr<Property> _property;
146
147};
148
149class OSGVOLUME_EXPORT ImageLayer : public Layer
150{
151 public:
152
153 ImageLayer(osg::Image* image=0);
154
155 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
156 ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
157
158 META_Object(osgVolume, ImageLayer);
159
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; }
162
163 void setImage(osg::Image* image);
164
165 template<class T> void setImage(const osg::ref_ptr<T>& image) { setImage(image.get()); }
166
167 /** Return image associated with layer. */
168 virtual osg::Image* getImage() { return _image.get(); }
169
170 /** Return const image associated with layer. */
171 virtual const osg::Image* getImage() const { return _image.get(); }
172
173
174 void setTexelOffset(const osg::Vec4& offset) { _texelOffset = offset; }
175 const osg::Vec4& getTexelOffset() const { return _texelOffset; }
176
177 void setTexelScale(const osg::Vec4& scale) { _texelScale = scale; }
178 const osg::Vec4& getTexelScale() const { return _texelScale; }
179
180
181 /** Compute the min and max pixel colors.*/
182 bool computeMinMax(osg::Vec4& min, osg::Vec4& max);
183
184 /** Apply color transformation to pixels using c' = offset + c * scale .*/
185 void offsetAndScaleImage(const osg::Vec4& offset, const osg::Vec4& scale);
186
187 /** Compute the min max range of the image, and then remap this to a 0 to 1 range.*/
188 void rescaleToZeroToOneRange();
189
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();
192
193 virtual bool requiresUpdateTraversal() const;
194
195 virtual void update(osg::NodeVisitor& /*nv*/);
196
197 virtual void dirty();
198 virtual void setModifiedCount(unsigned int value);
199 virtual unsigned int getModifiedCount() const;
200
201 protected:
202
203 virtual ~ImageLayer() {}
204
205 osg::Vec4 _texelOffset;
206 osg::Vec4 _texelScale;
207 osg::ref_ptr<osg::Image> _image;
208
209};
210
211class OSGVOLUME_EXPORT CompositeLayer : public Layer
212{
213 public:
214
215 CompositeLayer();
216
217 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
218 CompositeLayer(const CompositeLayer& compositeLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
219
220 META_Object(osgVolume, CompositeLayer);
221
222 void clear();
223
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; }
226
227 void setLayer(unsigned int i, Layer* layer) { if (i>=_layers.size()) _layers.resize(i+1); _layers[i].layer = layer; }
228
229 template<class T> void setLayer(unsigned int i, const osg::ref_ptr<T>& layer) { setLayer(i, layer.get()); }
230
231 Layer* getLayer(unsigned int i) { return i<_layers.size() ? _layers[i].layer.get() : 0; }
232
233 const Layer* getLayer(unsigned int i) const { return i<_layers.size() ? _layers[i].layer.get() : 0; }
234
235 void addLayer(Layer* layer) { _layers.push_back(NameLayer(layer->getFileName(),layer)); }
236
237 template<class T> void addLayer(const osg::ref_ptr<T>& layer) { addLayer(layer.get()); }
238
239 void removeLayer(unsigned int i) { _layers.erase(_layers.begin()+i); }
240
241 unsigned int getNumLayers() const { return _layers.size(); }
242
243 bool requiresUpdateTraversal() const;
244
245 virtual void update(osg::NodeVisitor& /*nv*/);
246
247 protected:
248
249 virtual ~CompositeLayer() {}
250
251 struct NameLayer
252 {
253 NameLayer() {}
254
255 NameLayer(const NameLayer& cnl):
256 filename(cnl.filename),
257 layer(cnl.layer) {}
258
259 NameLayer(const std::string& fn, Layer* l):
260 filename(fn),
261 layer(l) {}
262
263 NameLayer& operator = (const NameLayer& cnl)
264 {
265 if (&cnl==this) return *this;
266
267 filename = cnl.filename;
268 layer = cnl.layer;
269 return *this;
270 }
271
272 std::string filename;
273 osg::ref_ptr<Layer> layer;
274 };
275
276 typedef std::vector< NameLayer > Layers;
277
278 Layers _layers;
279};
280
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);
283
284/** Create an image that has a transfer function applied specified Image.*/
285extern OSGVOLUME_EXPORT osg::Image* applyTransferFunction(osg::Image* image, osg::TransferFunction1D* transferFunction);
286
287}
288
289#endif