root/OpenSceneGraph/trunk/include/osgTerrain/Layer @ 9618

Revision 9618, 19.6 kB (checked in by robert, 6 years ago)

Refactored osg::TransferFunction?1D to use an std::map internally which is kept in sync with the actual osg::Image that is passed to the GPU.

Added .osg support for osg::TransferFunction?1D.

Updated wrappers

Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 OSGTERRAIN_LAYER
15#define OSGTERRAIN_LAYER 1
16
17#include <osg/Image>
18#include <osg/Shape>
19#include <osg/Array>
20#include <osg/TransferFunction>
21
22#include <osgTerrain/Locator>
23#include <osgTerrain/ValidDataOperator>
24
25namespace osgTerrain {
26
27#define MAXIMUM_NUMBER_OF_LEVELS 30
28
29/** Extact the setname and filename from a compound string in the from set:setname:filename".
30  * Returns a setname of "" when non set:setname: entry is present.*/
31extern OSGTERRAIN_EXPORT void extractSetNameAndFileName(const std::string& compoundstring, std::string& setname, std::string& filename);
32
33/** Create a compound string in the form set:setname:filename, or just filename if setname is "".*/
34extern OSGTERRAIN_EXPORT std::string createCompondSetNameAndFileName(const std::string& setname, const std::string& filename);
35
36class OSGTERRAIN_EXPORT Layer : public osg::Object
37{
38    public:
39
40        Layer();
41
42        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
43        Layer(const Layer&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
44       
45        META_Object(osgTerrain, Layer);
46       
47        void setSetName(const std::string& setname) { setName(setname); }
48        const std::string& getSetName() const { return getName(); }
49
50        /** Set the file name of the data associated with this layer. */
51        virtual void setFileName(const std::string& filename) { _filename = filename; }
52
53        /** Get the file name of the layer. */
54        virtual const std::string& getFileName() const { return _filename; }
55
56        /** Return the compound name of the layer in the form set::name::filename string.*/
57        std::string getCompoundName() const { return createCompondSetNameAndFileName(getName(), getFileName()); }
58
59        void setLocator(Locator* locator) { _locator = locator; }
60        Locator* getLocator() { return _locator.get(); }
61        const Locator* getLocator() const { return _locator.get(); }
62       
63        void setMinLevel(unsigned int minLevel) { _minLevel = minLevel; }
64        unsigned int getMinLevel() const { return _minLevel; }
65
66        void setMaxLevel(unsigned int maxLevel) { _maxLevel = maxLevel; }
67        unsigned int getMaxLevel() const { return _maxLevel; }
68
69        void setValidDataOperator(ValidDataOperator* validDataOp) { _validDataOperator = validDataOp; }
70        ValidDataOperator* getValidDataOperator() { return _validDataOperator.get(); }
71        const ValidDataOperator* getValidDataOperator() const { return _validDataOperator.get(); }
72
73
74        virtual unsigned int getNumColumns() const { return 0; }
75        virtual unsigned int getNumRows() const { return 0; }
76
77        void setDefaultValue(const osg::Vec4& value) { _defaultValue = value; }
78        const osg::Vec4& getDefaultValue() const { return _defaultValue; }
79       
80
81        /** Set the minification texture filter to use when do texture associated with this layer.*/
82        void setMinFilter(osg::Texture::FilterMode filter) { _minFilter = filter; }
83
84        /** Get the minification texture filter to use when do texture associated with this layer.*/
85        osg::Texture::FilterMode getMinFilter() const { return _minFilter; }
86
87
88        /** Set the magniification texture filter to use when do texture associated with this layer.*/
89        void setMagFilter(osg::Texture::FilterMode filter) { _magFilter = filter; }
90
91        /** Get the magnification texture filter to use when do texture associated with this layer.*/
92        osg::Texture::FilterMode getMagFilter() const { return _magFilter; }
93
94
95
96        /** Return image associated with layer if supported. */       
97        virtual osg::Image* getImage() { return 0; }
98
99        /** Return const image associated with layer if supported. */       
100        virtual const osg::Image* getImage() const { return 0; }
101
102
103        virtual bool transform(float /*offset*/, float /*scale*/) { return false; }
104
105
106        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, float& /*value*/) const { return false; }
107        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec2& /*value*/) const { return false; }
108        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec3& /*value*/) const { return false; }
109        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec4& /*value*/) const { return false; }
110
111        inline bool getValidValue(unsigned int i, unsigned int j, float& value) const
112        {
113            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
114            return false;
115        }
116
117        inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec2& value) const
118        {
119            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
120            return false;
121        }
122
123        inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec3& value) const
124        {
125            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
126            return false;
127        }
128
129        inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec4& value) const
130        {
131            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
132            return false;
133        }
134
135
136        inline void computeIndices(double ndc_x, double ndc_y, unsigned int& i, unsigned int& j, double& ir, double& jr)
137        {
138            ndc_x *= double(getNumColumns()-1);
139            ndc_y *= double(getNumRows()-1);
140            i = (unsigned int)(ndc_x);
141            j = (unsigned int)(ndc_y);
142            ir = ndc_x - double(i);
143            jr = ndc_y - double(j);
144        }
145
146
147        inline bool getInterpolatedValue(double ndc_x, double ndc_y, float& value)
148        {
149            unsigned int i,j;
150            double ir, jr;
151            computeIndices(ndc_x, ndc_y, i, j, ir, jr);
152            value = 0.0f;
153            double div = 0.0f;
154            float v,r;
155           
156            r = (1.0f-ir)*(1.0f-jr);
157            if (r>0.0 && getValue(i,j,v))
158            {
159                value += v*r;
160                div += r;
161            }
162           
163            r = (ir)*(1.0f-jr);
164            if (r>0.0 && getValue(i+1,j,v))
165            {
166                value += v*r;
167                div += r;
168            }
169           
170            r = (ir)*(jr);
171            if (r>0.0 && getValue(i+1,j+1,v))
172            {
173                value += v*r;
174                div += r;
175            }
176           
177            r = (1.0f-ir)*(jr);
178            if (r>0.0 && getValue(i,j+1,v))
179            {
180                value += v*r;
181                div += r;
182            }
183           
184            if (div != 0.0)
185            {
186                value /= div;
187                return true;
188            }
189           
190            value = 0.0;
191            return false;
192        }
193
194        /** increment the modified count."*/
195        virtual void dirty() {};
196
197        /** Set the modified count value.  */
198        virtual void setModifiedCount(unsigned int /*value*/) {};
199
200        /** Get modified count value. */
201        virtual unsigned int getModifiedCount() const { return 0; }
202
203        virtual osg::BoundingSphere computeBound(bool treatAsElevationLayer) const;
204
205    protected:
206
207        virtual ~Layer();
208
209        std::string                     _filename;
210        osg::ref_ptr<Locator>           _locator;
211        unsigned int                    _minLevel;
212        unsigned int                    _maxLevel;
213        osg::ref_ptr<ValidDataOperator> _validDataOperator;
214        osg::Vec4                       _defaultValue;
215        osg::Texture::FilterMode        _minFilter;
216        osg::Texture::FilterMode        _magFilter;
217
218};
219
220class OSGTERRAIN_EXPORT ImageLayer : public Layer
221{
222    public:
223
224        ImageLayer(osg::Image* image=0);
225
226        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
227        ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
228       
229        META_Object(osgTerrain, ImageLayer);
230
231        void setFileName(const std::string& filename) { _filename = filename; if (_image.valid()) _image->setFileName(filename); }
232        virtual const std::string& getFileName() const { return _image.get() ? _image->getFileName() : _filename; }
233
234        virtual bool transform(float offset, float scale);
235
236        void setImage(osg::Image* image);
237
238        /** Return image associated with layer. */ 
239        virtual osg::Image* getImage() { return _image.get(); }
240
241        /** Return const image associated with layer. */
242        virtual const osg::Image* getImage() const { return _image.get(); }
243
244        virtual unsigned int getNumColumns() const { return _image.valid() ? _image->s() : 0; }
245        virtual unsigned int getNumRows() const { return _image.valid() ? _image->t() : 0;  }
246
247        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
248        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
249        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
250        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;
251
252        virtual void dirty();
253        virtual void setModifiedCount(unsigned int value);
254        virtual unsigned int getModifiedCount() const;
255
256    protected:
257
258        virtual ~ImageLayer() {}
259
260        osg::ref_ptr<osg::Image>    _image;
261
262};
263
264class OSGTERRAIN_EXPORT ContourLayer : public Layer
265{
266    public:
267
268        ContourLayer(osg::TransferFunction1D* tf=0);
269
270        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
271        ContourLayer(const ContourLayer& tfLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
272       
273        META_Object(osgTerrain, ContourLayer);
274
275        virtual bool transform(float offset, float scale);
276
277        void setTransferFunction(osg::TransferFunction1D* tf);
278        osg::TransferFunction1D* getTransferFunction() { return _tf.get(); }
279        const osg::TransferFunction1D* getTransferFunction() const { return _tf.get(); }
280
281        /** Return image associated with layer. */ 
282        virtual osg::Image* getImage() { return _tf.valid() ? _tf->getImage() : 0; }
283
284        /** Return const image associated with layer. */
285        virtual const osg::Image* getImage() const { return _tf.valid() ? _tf->getImage() : 0; }
286
287
288        virtual unsigned int getNumColumns() const { return _tf.valid() ? _tf->getNumberImageCells() : 0; }
289        virtual unsigned int getNumRows() const { return _tf.valid() ? 1 : 0;  }
290
291        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
292        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
293        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
294        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;
295
296        virtual void dirty();
297        virtual void setModifiedCount(unsigned int value);
298        virtual unsigned int getModifiedCount() const;
299
300    protected:
301
302        virtual ~ContourLayer() {}
303
304        osg::ref_ptr<osg::TransferFunction1D>    _tf;
305
306};
307
308class OSGTERRAIN_EXPORT HeightFieldLayer : public Layer
309{
310    public:
311
312        HeightFieldLayer(osg::HeightField* hf=0);
313
314        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
315        HeightFieldLayer(const HeightFieldLayer& hfLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
316       
317        META_Object(osgTerrain, HeightFieldLayer);
318
319        void setFileName(const std::string& filename) { _filename = filename; }
320        virtual const std::string& getFileName() const { return _filename; }
321
322        virtual bool transform(float offset, float scale);
323
324        void setHeightField(osg::HeightField* hf);
325        osg::HeightField* getHeightField() { return _heightField.get(); }
326        const osg::HeightField* getHeightField() const { return _heightField.get(); }
327
328        virtual unsigned int getNumColumns() const { return _heightField.valid() ? _heightField->getNumColumns() : 0; }
329        virtual unsigned int getNumRows() const { return _heightField.valid() ? _heightField->getNumRows() : 0;  }
330
331        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
332        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
333        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
334        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;
335
336        virtual void dirty();
337        virtual void setModifiedCount(unsigned int value);
338        virtual unsigned int getModifiedCount() const;
339
340    protected:
341
342        virtual ~HeightFieldLayer() {}
343
344        unsigned int                    _modifiedCount;
345        osg::ref_ptr<osg::HeightField>  _heightField;
346
347};
348
349
350class OSGTERRAIN_EXPORT ProxyLayer : public Layer
351{
352    public:
353
354        ProxyLayer();
355
356        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
357        ProxyLayer(const ProxyLayer& proxyLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
358       
359        META_Object(osgTerrain, ProxyLayer);
360       
361        /** Return image associated with layer if supported. */       
362        virtual osg::Image* getImage()
363        {
364            return _implementation.valid() ? _implementation->getImage() : 0;
365        }
366
367        /** Return const image associated with layer if supported. */
368        virtual const osg::Image* getImage() const
369        {
370            return _implementation.valid() ? _implementation->getImage() : 0;
371        }
372
373        /** Set the implementation layer that does the actual work.*/
374        void setImplementation(Layer* layer) { _implementation = layer; }
375       
376        /** Get the implementation layer that does the actual work.*/
377        Layer* getImplementation() { return _implementation.get(); }
378
379        /** Get the const implementation layer that does the actual work.*/
380        const Layer* getImplementation() const { return _implementation.get(); }
381
382        virtual void setFileName(const std::string& filename);
383        virtual const std::string& getFileName() const { return _filename; }
384
385        virtual unsigned int getNumColumns() const;
386        virtual unsigned int getNumRows() const;
387       
388        virtual bool transform(float offset, float scale);
389
390        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
391        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
392        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
393        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;
394
395        virtual void dirty();
396        virtual void setModifiedCount(unsigned int value);
397        virtual unsigned int getModifiedCount() const;
398
399        virtual osg::BoundingSphere computeBound(bool treatAsElevationLayer) const;
400
401    protected:
402
403        virtual ~ProxyLayer();
404       
405        osg::ref_ptr<Layer> _implementation;
406
407
408};
409
410class OSGTERRAIN_EXPORT CompositeLayer : public Layer
411{
412    public:
413
414        CompositeLayer();
415
416        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
417        CompositeLayer(const CompositeLayer& compositeLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
418       
419        META_Object(osgTerrain, CompositeLayer);
420
421        void clear();
422
423        void setSetName(const std::string& setname) { setName(setname); }
424        const std::string& getSetName() const { return getName(); }
425
426        void setSetName(unsigned int i, const std::string& setname) { _layers[i].setname = setname; if (_layers[i].layer.valid()) _layers[i].layer->setName(setname); }
427        const std::string& getSetName(unsigned int i) const { return _layers[i].layer.valid() ? _layers[i].layer->getName() : _layers[i].setname; }
428
429        void setFileName(unsigned int i, const std::string& filename) { _layers[i].filename = filename; if (_layers[i].layer.valid()) _layers[i].layer->setFileName(filename); }
430        const std::string& getFileName(unsigned int i) const { return _layers[i].layer.valid() ? _layers[i].layer->getFileName() : _layers[i].filename; }
431
432        void setCompoundName(unsigned int i, const std::string& compoundname);
433        std::string getCompoundName(unsigned int i) const;
434
435
436        void setLayer(unsigned int i, Layer* layer) { if (i>=_layers.size()) _layers.resize(i+1); _layers[i].layer = layer; }
437        Layer* getLayer(unsigned int i) { return i<_layers.size() ? _layers[i].layer.get() : 0; }
438        const Layer* getLayer(unsigned int i) const { return i<_layers.size() ? _layers[i].layer.get() : 0; }
439
440        void addLayer(const std::string& compoundname);
441        void addLayer(const std::string& setname, const std::string& filename);
442       
443        void addLayer(Layer* layer) { _layers.push_back(CompoundNameLayer(layer->getName(),layer->getFileName(),layer)); }
444
445        void removeLayer(unsigned int i) { _layers.erase(_layers.begin()+i); }
446       
447        unsigned int getNumLayers() const { return _layers.size(); }
448
449    protected:
450
451        virtual ~CompositeLayer() {}
452
453        struct CompoundNameLayer
454        {
455            CompoundNameLayer() {}
456       
457            CompoundNameLayer(const CompoundNameLayer& cnl):
458                setname(cnl.setname),
459                filename(cnl.filename),
460                layer(cnl.layer) {}
461
462            CompoundNameLayer(const std::string& sn, const std::string& fn, Layer* l):
463                setname(sn),
464                filename(fn),
465                layer(l) {}
466
467            CompoundNameLayer& operator = (const CompoundNameLayer& cnl)
468            {
469                if (&cnl==this) return *this;
470               
471                setname = cnl.setname;
472                filename = cnl.filename;
473                layer = cnl.layer;
474                return *this;
475            }
476
477            std::string         setname;
478            std::string         filename;
479            osg::ref_ptr<Layer> layer;
480        };
481       
482        typedef std::vector< CompoundNameLayer > Layers;
483       
484        Layers _layers;
485};
486
487
488class OSGTERRAIN_EXPORT SwitchLayer : public CompositeLayer
489{
490    public:
491
492        SwitchLayer();
493
494        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
495        SwitchLayer(const SwitchLayer& switchLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
496       
497        META_Object(osgTerrain, SwitchLayer);
498       
499        void setActiveLayer(int i) { _activeLayer = i; }
500        int getActiveLayer() const { return _activeLayer; }
501
502        /** Return image associated with layer if supported. */       
503        virtual osg::Image* getImage()
504        {
505            if (_activeLayer < 0) return 0;           
506            if (_activeLayer >= static_cast<int>(getNumLayers())) return 0;
507            return _layers[_activeLayer].layer->getImage();           
508        }
509
510        /** Return const image associated with layer if supported. */
511        virtual const osg::Image* getImage() const
512        {
513            if (_activeLayer < 0) return 0;           
514            if (_activeLayer >= static_cast<int>(getNumLayers())) return 0;
515            return _layers[_activeLayer].layer->getImage();           
516        }
517
518    protected:
519
520        virtual ~SwitchLayer() {}
521       
522        int _activeLayer;
523};
524
525
526
527}
528
529#endif
Note: See TracBrowser for help on using the browser.