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

Revision 13041, 21.8 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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 createCompoundSetNameAndFileName(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        /** Set the name of this layer. */
48        void setSetName(const std::string& setname) { setName(setname); }
49
50        /** Get the name of this layer. */
51        const std::string& getSetName() const { return getName(); }
52
53        /** Set the file name of the data associated with this layer. */
54        virtual void setFileName(const std::string& filename) { _filename = filename; }
55
56        /** Get the file name of the layer. */
57        virtual const std::string& getFileName() const { return _filename; }
58
59        /** Return the compound name of the layer in the form set::name::filename string.*/
60        std::string getCompoundName() const { return createCompoundSetNameAndFileName(getName(), getFileName()); }
61
62        void setLocator(Locator* locator) { _locator = locator; }
63        Locator* getLocator() { return _locator.get(); }
64        const Locator* getLocator() const { return _locator.get(); }
65
66        void setMinLevel(unsigned int minLevel) { _minLevel = minLevel; }
67        unsigned int getMinLevel() const { return _minLevel; }
68
69        void setMaxLevel(unsigned int maxLevel) { _maxLevel = maxLevel; }
70        unsigned int getMaxLevel() const { return _maxLevel; }
71
72        /** Set the data validation operator. */
73        void setValidDataOperator(ValidDataOperator* validDataOp) { _validDataOperator = validDataOp; }
74
75        /** Get the data validation operator. */
76        ValidDataOperator* getValidDataOperator() { return _validDataOperator.get(); }
77
78        /** Get the const data validation operator. */
79        const ValidDataOperator* getValidDataOperator() const { return _validDataOperator.get(); }
80
81
82        /** Get the number of columns. */
83        virtual unsigned int getNumColumns() const { return 0; }
84
85        /** Get the number of rows. */
86        virtual unsigned int getNumRows() const { return 0; }
87
88        void setDefaultValue(const osg::Vec4& value) { _defaultValue = value; }
89        const osg::Vec4& getDefaultValue() const { return _defaultValue; }
90
91
92        /** Set the minification texture filter to use when a texture is associated with this layer.*/
93        void setMinFilter(osg::Texture::FilterMode filter) { _minFilter = filter; }
94
95        /** Get the minification texture filter to use when a texture is associated with this layer.*/
96        osg::Texture::FilterMode getMinFilter() const { return _minFilter; }
97
98
99        /** Set the magnification texture filter to use when a texture is associated with this layer.*/
100        void setMagFilter(osg::Texture::FilterMode filter) { _magFilter = filter; }
101
102        /** Get the magnification texture filter to use when a texture is associated with this layer.*/
103        osg::Texture::FilterMode getMagFilter() const { return _magFilter; }
104
105
106
107        /** Return image associated with layer if supported. */
108        virtual osg::Image* getImage() { return 0; }
109
110        /** Return const image associated with layer if supported. */
111        virtual const osg::Image* getImage() const { return 0; }
112
113
114        virtual bool transform(float /*offset*/, float /*scale*/) { return false; }
115
116        /**
117         * Get the layer value at position i,j.
118         * @param[in] i X-axis (or column) index.
119         * @param[in] j Y-axis (or row) index.
120         * @param[out] value Returned layer value.
121         * @return true if value is valid, else false
122         */
123        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, float& /*value*/) const { return false; }
124        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec2& /*value*/) const { return false; }
125        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec3& /*value*/) const { return false; }
126        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec4& /*value*/) const { return false; }
127
128        inline bool getValidValue(unsigned int i, unsigned int j, float& value) const
129        {
130            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
131            return false;
132        }
133
134        inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec2& value) const
135        {
136            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
137            return false;
138        }
139
140        inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec3& value) const
141        {
142            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
143            return false;
144        }
145
146        inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec4& value) const
147        {
148            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
149            return false;
150        }
151
152
153        /**
154         * Compute column,row indices from normalized coordinates.
155         * @param[in] ndc_x Normalized X-axis coordinate.
156         * @param[in] ndc_y Normalized Y-axis coordinate.
157         * @param[out] i Returned X-axis (or column) index.
158         * @param[out] j Returned Y-axis (or row) index.
159         * @param[out] ir Returned X-axis fraction.
160         * @param[out] jr Returned Y-axis fraction.
161         */
162        inline void computeIndices(double ndc_x, double ndc_y, unsigned int& i, unsigned int& j, double& ir, double& jr)
163        {
164            ndc_x *= double(getNumColumns()-1);
165            ndc_y *= double(getNumRows()-1);
166            i = (unsigned int)(ndc_x);
167            j = (unsigned int)(ndc_y);
168            ir = ndc_x - double(i);
169            jr = ndc_y - double(j);
170        }
171
172        /**
173         * Calculate the interpolated layer value at the given normalized coordinates.
174         * @param[in] ndc_x Normalized X-axis coordinate.
175         * @param[in] ndc_y Normalized Y-axis coordinate.
176         * @param[out] value Returned layer value.
177         * @return true if value is valid, else false
178         */
179        inline bool getInterpolatedValue(double ndc_x, double ndc_y, float& value)
180        {
181            unsigned int i,j;
182            double ir, jr;
183            computeIndices(ndc_x, ndc_y, i, j, ir, jr);
184            value = 0.0f;
185            double div = 0.0f;
186            float v,r;
187
188            r = (1.0f-ir)*(1.0f-jr);
189            if (r>0.0 && getValue(i,j,v))
190            {
191                value += v*r;
192                div += r;
193            }
194
195            r = (ir)*(1.0f-jr);
196            if (r>0.0 && getValue(i+1,j,v))
197            {
198                value += v*r;
199                div += r;
200            }
201
202            r = (ir)*(jr);
203            if (r>0.0 && getValue(i+1,j+1,v))
204            {
205                value += v*r;
206                div += r;
207            }
208
209            r = (1.0f-ir)*(jr);
210            if (r>0.0 && getValue(i,j+1,v))
211            {
212                value += v*r;
213                div += r;
214            }
215
216            if (div != 0.0)
217            {
218                value /= div;
219                return true;
220            }
221
222            value = 0.0;
223            return false;
224        }
225
226        inline bool getInterpolatedValidValue(double ndc_x, double ndc_y, float& value)
227        {
228            unsigned int i,j;
229            double ir, jr;
230            computeIndices(ndc_x, ndc_y, i, j, ir, jr);
231            value = 0.0f;
232            double div = 0.0f;
233            float v,r;
234
235            r = (1.0f-ir)*(1.0f-jr);
236            if (r>0.0 && getValidValue(i,j,v))
237            {
238                value += v*r;
239                div += r;
240            }
241
242            r = (ir)*(1.0f-jr);
243            if (r>0.0 && getValidValue(i+1,j,v))
244            {
245                value += v*r;
246                div += r;
247            }
248
249            r = (ir)*(jr);
250            if (r>0.0 && getValidValue(i+1,j+1,v))
251            {
252                value += v*r;
253                div += r;
254            }
255
256            r = (1.0f-ir)*(jr);
257            if (r>0.0 && getValidValue(i,j+1,v))
258            {
259                value += v*r;
260                div += r;
261            }
262
263            if (div != 0.0)
264            {
265                value /= div;
266                return true;
267            }
268
269            value = 0.0;
270            return false;
271        }
272
273        /** increment the modified count."*/
274        virtual void dirty() {}
275
276        /** Set the modified count value.  */
277        virtual void setModifiedCount(unsigned int /*value*/) {}
278
279        /** Get modified count value. */
280        virtual unsigned int getModifiedCount() const { return 0; }
281
282        virtual osg::BoundingSphere computeBound(bool treatAsElevationLayer) const;
283
284    protected:
285
286        virtual ~Layer();
287
288        std::string                     _filename;
289        osg::ref_ptr<Locator>           _locator;
290        unsigned int                    _minLevel;
291        unsigned int                    _maxLevel;
292        osg::ref_ptr<ValidDataOperator> _validDataOperator;
293        osg::Vec4                       _defaultValue;
294        osg::Texture::FilterMode        _minFilter;
295        osg::Texture::FilterMode        _magFilter;
296
297};
298
299class OSGTERRAIN_EXPORT ImageLayer : public Layer
300{
301    public:
302
303        ImageLayer(osg::Image* image=0);
304
305        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
306        ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
307
308        META_Object(osgTerrain, ImageLayer);
309
310        void setFileName(const std::string& filename) { _filename = filename; if (_image.valid()) _image->setFileName(filename); }
311        virtual const std::string& getFileName() const { return _image.get() ? _image->getFileName() : _filename; }
312
313        virtual bool transform(float offset, float scale);
314
315        void setImage(osg::Image* image);
316
317        /** Return image associated with layer. */
318        virtual osg::Image* getImage() { return _image.get(); }
319
320        /** Return const image associated with layer. */
321        virtual const osg::Image* getImage() const { return _image.get(); }
322
323        virtual unsigned int getNumColumns() const { return _image.valid() ? _image->s() : 0; }
324        virtual unsigned int getNumRows() const { return _image.valid() ? _image->t() : 0;  }
325
326        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
327        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
328        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
329        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;
330
331        virtual void dirty();
332        virtual void setModifiedCount(unsigned int value);
333        virtual unsigned int getModifiedCount() const;
334
335    protected:
336
337        virtual ~ImageLayer() {}
338
339        osg::ref_ptr<osg::Image>    _image;
340
341};
342
343class OSGTERRAIN_EXPORT ContourLayer : public Layer
344{
345    public:
346
347        ContourLayer(osg::TransferFunction1D* tf=0);
348
349        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
350        ContourLayer(const ContourLayer& tfLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
351
352        META_Object(osgTerrain, ContourLayer);
353
354        virtual bool transform(float offset, float scale);
355
356        void setTransferFunction(osg::TransferFunction1D* tf);
357        osg::TransferFunction1D* getTransferFunction() { return _tf.get(); }
358        const osg::TransferFunction1D* getTransferFunction() const { return _tf.get(); }
359
360        /** Return image associated with layer. */
361        virtual osg::Image* getImage() { return _tf.valid() ? _tf->getImage() : 0; }
362
363        /** Return const image associated with layer. */
364        virtual const osg::Image* getImage() const { return _tf.valid() ? _tf->getImage() : 0; }
365
366
367        virtual unsigned int getNumColumns() const { return _tf.valid() ? _tf->getNumberImageCells() : 0; }
368        virtual unsigned int getNumRows() const { return _tf.valid() ? 1 : 0;  }
369
370        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
371        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
372        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
373        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;
374
375        virtual void dirty();
376        virtual void setModifiedCount(unsigned int value);
377        virtual unsigned int getModifiedCount() const;
378
379    protected:
380
381        virtual ~ContourLayer() {}
382
383        osg::ref_ptr<osg::TransferFunction1D>    _tf;
384
385};
386
387class OSGTERRAIN_EXPORT HeightFieldLayer : public Layer
388{
389    public:
390
391        HeightFieldLayer(osg::HeightField* hf=0);
392
393        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
394        HeightFieldLayer(const HeightFieldLayer& hfLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
395
396        META_Object(osgTerrain, HeightFieldLayer);
397
398        void setFileName(const std::string& filename) { _filename = filename; }
399        virtual const std::string& getFileName() const { return _filename; }
400
401        virtual bool transform(float offset, float scale);
402
403        void setHeightField(osg::HeightField* hf);
404        osg::HeightField* getHeightField() { return _heightField.get(); }
405        const osg::HeightField* getHeightField() const { return _heightField.get(); }
406
407        virtual unsigned int getNumColumns() const { return _heightField.valid() ? _heightField->getNumColumns() : 0; }
408        virtual unsigned int getNumRows() const { return _heightField.valid() ? _heightField->getNumRows() : 0;  }
409
410        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
411        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
412        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
413        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;
414
415        virtual void dirty();
416        virtual void setModifiedCount(unsigned int value);
417        virtual unsigned int getModifiedCount() const;
418
419    protected:
420
421        virtual ~HeightFieldLayer() {}
422
423        unsigned int                    _modifiedCount;
424        osg::ref_ptr<osg::HeightField>  _heightField;
425
426};
427
428
429class OSGTERRAIN_EXPORT ProxyLayer : public Layer
430{
431    public:
432
433        ProxyLayer();
434
435        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
436        ProxyLayer(const ProxyLayer& proxyLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
437
438        META_Object(osgTerrain, ProxyLayer);
439
440        /** Return image associated with layer if supported. */
441        virtual osg::Image* getImage()
442        {
443            return _implementation.valid() ? _implementation->getImage() : 0;
444        }
445
446        /** Return const image associated with layer if supported. */
447        virtual const osg::Image* getImage() const
448        {
449            return _implementation.valid() ? _implementation->getImage() : 0;
450        }
451
452        /** Set the implementation layer that does the actual work.*/
453        void setImplementation(Layer* layer) { _implementation = layer; }
454
455        /** Get the implementation layer that does the actual work.*/
456        Layer* getImplementation() { return _implementation.get(); }
457
458        /** Get the const implementation layer that does the actual work.*/
459        const Layer* getImplementation() const { return _implementation.get(); }
460
461        virtual void setFileName(const std::string& filename);
462        virtual const std::string& getFileName() const { return _filename; }
463
464        virtual unsigned int getNumColumns() const;
465        virtual unsigned int getNumRows() const;
466
467        virtual bool transform(float offset, float scale);
468
469        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
470        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
471        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
472        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;
473
474        virtual void dirty();
475        virtual void setModifiedCount(unsigned int value);
476        virtual unsigned int getModifiedCount() const;
477
478        virtual osg::BoundingSphere computeBound(bool treatAsElevationLayer) const;
479
480    protected:
481
482        virtual ~ProxyLayer();
483
484        osg::ref_ptr<Layer> _implementation;
485
486
487};
488
489class OSGTERRAIN_EXPORT CompositeLayer : public Layer
490{
491    public:
492
493        CompositeLayer();
494
495        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
496        CompositeLayer(const CompositeLayer& compositeLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
497
498        META_Object(osgTerrain, CompositeLayer);
499
500        void clear();
501
502        /** Set the set name of layer 'i'. */
503        void setSetName(unsigned int i, const std::string& setname) { _layers[i].setname = setname; if (_layers[i].layer.valid()) _layers[i].layer->setName(setname); }
504
505        /** Get the set name of layer 'i'. */
506        const std::string& getSetName(unsigned int i) const { return _layers[i].layer.valid() ? _layers[i].layer->getName() : _layers[i].setname; }
507
508        /** Set the file name of the data associated with layer 'i'. */
509        void setFileName(unsigned int i, const std::string& filename) { _layers[i].filename = filename; if (_layers[i].layer.valid()) _layers[i].layer->setFileName(filename); }
510
511        /** Get the file name of the data associated with layer 'i'. */
512        const std::string& getFileName(unsigned int i) const { return _layers[i].layer.valid() ? _layers[i].layer->getFileName() : _layers[i].filename; }
513
514        void setCompoundName(unsigned int i, const std::string& compoundname);
515        std::string getCompoundName(unsigned int i) const;
516
517
518        void setLayer(unsigned int i, Layer* layer) { if (i>=_layers.size()) _layers.resize(i+1); _layers[i].layer = layer; }
519        Layer* getLayer(unsigned int i) { return i<_layers.size() ? _layers[i].layer.get() : 0; }
520        const Layer* getLayer(unsigned int i) const { return i<_layers.size() ? _layers[i].layer.get() : 0; }
521
522        void addLayer(const std::string& compoundname);
523        void addLayer(const std::string& setname, const std::string& filename);
524
525        void addLayer(Layer* layer) { _layers.push_back(CompoundNameLayer(layer->getName(),layer->getFileName(),layer)); }
526
527        void removeLayer(unsigned int i) { _layers.erase(_layers.begin()+i); }
528
529        unsigned int getNumLayers() const { return _layers.size(); }
530
531    protected:
532
533        virtual ~CompositeLayer() {}
534
535        struct CompoundNameLayer
536        {
537            CompoundNameLayer() {}
538
539            CompoundNameLayer(const CompoundNameLayer& cnl):
540                setname(cnl.setname),
541                filename(cnl.filename),
542                layer(cnl.layer) {}
543
544            CompoundNameLayer(const std::string& sn, const std::string& fn, Layer* l):
545                setname(sn),
546                filename(fn),
547                layer(l) {}
548
549            CompoundNameLayer& operator = (const CompoundNameLayer& cnl)
550            {
551                if (&cnl==this) return *this;
552
553                setname = cnl.setname;
554                filename = cnl.filename;
555                layer = cnl.layer;
556                return *this;
557            }
558
559            std::string         setname;
560            std::string         filename;
561            osg::ref_ptr<Layer> layer;
562        };
563
564        typedef std::vector< CompoundNameLayer > Layers;
565
566        Layers _layers;
567};
568
569
570class OSGTERRAIN_EXPORT SwitchLayer : public CompositeLayer
571{
572    public:
573
574        SwitchLayer();
575
576        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
577        SwitchLayer(const SwitchLayer& switchLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
578
579        META_Object(osgTerrain, SwitchLayer);
580
581        void setActiveLayer(int i) { _activeLayer = i; }
582        int getActiveLayer() const { return _activeLayer; }
583
584        /** Return image associated with layer if supported. */
585        virtual osg::Image* getImage()
586        {
587            if (_activeLayer < 0) return 0;
588            if (_activeLayer >= static_cast<int>(getNumLayers())) return 0;
589            return _layers[_activeLayer].layer->getImage();
590        }
591
592        /** Return const image associated with layer if supported. */
593        virtual const osg::Image* getImage() const
594        {
595            if (_activeLayer < 0) return 0;
596            if (_activeLayer >= static_cast<int>(getNumLayers())) return 0;
597            return _layers[_activeLayer].layer->getImage();
598        }
599
600    protected:
601
602        virtual ~SwitchLayer() {}
603
604        int _activeLayer;
605};
606
607
608
609}
610
611#endif
Note: See TracBrowser for help on using the browser.