Layer 22.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * 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 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSGTERRAIN_LAYER
#define OSGTERRAIN_LAYER 1

#include <osg/Image>
#include <osg/Shape>
#include <osg/Array>
#include <osg/TransferFunction>

#include <osgTerrain/Locator>
#include <osgTerrain/ValidDataOperator>

namespace osgTerrain {

#define MAXIMUM_NUMBER_OF_LEVELS 30

/** Extact the setname and filename from a compound string in the from set:setname:filename".
  * Returns a setname of "" when non set:setname: entry is present.*/
extern OSGTERRAIN_EXPORT void extractSetNameAndFileName(const std::string& compoundstring, std::string& setname, std::string& filename);

/** Create a compound string in the form set:setname:filename, or just filename if setname is "".*/
extern OSGTERRAIN_EXPORT std::string createCompoundSetNameAndFileName(const std::string& setname, const std::string& filename);

class OSGTERRAIN_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);
        
        META_Object(osgTerrain, Layer);
        
        /** Set the name of this layer. */
        void setSetName(const std::string& setname) { setName(setname); }

        /** Get the name of this layer. */
        const std::string& getSetName() const { return getName(); }

        /** 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; }

        /** Return the compound name of the layer in the form set::name::filename string.*/
        std::string getCompoundName() const { return createCompoundSetNameAndFileName(getName(), getFileName()); }

        void setLocator(Locator* locator) { _locator = locator; }
        Locator* getLocator() { return _locator.get(); }
        const Locator* getLocator() const { return _locator.get(); }
 
        void setMinLevel(unsigned int minLevel) { _minLevel = minLevel; }
        unsigned int getMinLevel() const { return _minLevel; }

        void setMaxLevel(unsigned int maxLevel) { _maxLevel = maxLevel; }
        unsigned int getMaxLevel() const { return _maxLevel; }

        /** Set the data validation operator. */
        void setValidDataOperator(ValidDataOperator* validDataOp) { _validDataOperator = validDataOp; }

        /** Get the data validation operator. */
        ValidDataOperator* getValidDataOperator() { return _validDataOperator.get(); }

        /** Get the const data validation operator. */
        const ValidDataOperator* getValidDataOperator() const { return _validDataOperator.get(); }


        /** Get the number of columns. */
        virtual unsigned int getNumColumns() const { return 0; }

        /** Get the number of rows. */
        virtual unsigned int getNumRows() const { return 0; }

        void setDefaultValue(const osg::Vec4& value) { _defaultValue = value; }
        const osg::Vec4& getDefaultValue() const { return _defaultValue; }
        

        /** Set the minification texture filter to use when a texture is associated with this layer.*/
        void setMinFilter(osg::Texture::FilterMode filter) { _minFilter = filter; }

        /** Get the minification texture filter to use when a texture is associated with this layer.*/
        osg::Texture::FilterMode getMinFilter() const { return _minFilter; }


        /** Set the magnification texture filter to use when a texture is associated with this layer.*/
        void setMagFilter(osg::Texture::FilterMode filter) { _magFilter = filter; }

        /** Get the magnification texture filter to use when a texture is associated with this layer.*/
        osg::Texture::FilterMode getMagFilter() const { return _magFilter; }



        /** Return image associated with layer if supported. */        
        virtual osg::Image* getImage() { return 0; }

        /** Return const image associated with layer if supported. */        
        virtual const osg::Image* getImage() const { return 0; }


        virtual bool transform(float /*offset*/, float /*scale*/) { return false; }

        /**
         * Get the layer value at position i,j.
         * @param[in] i X-axis (or column) index.
         * @param[in] j Y-axis (or row) index.
         * @param[out] value Returned layer value.
         * @return true if value is valid, else false
         */
        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, float& /*value*/) const { return false; }
        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec2& /*value*/) const { return false; }
        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec3& /*value*/) const { return false; }
        virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec4& /*value*/) const { return false; }

        inline bool getValidValue(unsigned int i, unsigned int j, float& value) const
        {
            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
            return false;
        }

        inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec2& value) const
        {
            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
            return false;
        }

        inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec3& value) const
        {
            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
            return false;
        }

        inline bool getValidValue(unsigned int i, unsigned int j, osg::Vec4& value) const
        {
            if (getValue(i,j,value)) return _validDataOperator.valid() ? (*_validDataOperator)(value) : true;
            return false;
        }


        /**
         * Compute column,row indices from normalized coordinates.
         * @param[in] ndc_x Normalized X-axis coordinate.
         * @param[in] ndc_y Normalized Y-axis coordinate.
         * @param[out] i Returned X-axis (or column) index.
         * @param[out] j Returned Y-axis (or row) index.
         * @param[out] ir Returned X-axis fraction.
         * @param[out] jr Returned Y-axis fraction.
         */
        inline void computeIndices(double ndc_x, double ndc_y, unsigned int& i, unsigned int& j, double& ir, double& jr)
        {
            ndc_x *= double(getNumColumns()-1);
            ndc_y *= double(getNumRows()-1);
            i = (unsigned int)(ndc_x);
            j = (unsigned int)(ndc_y);
            ir = ndc_x - double(i);
            jr = ndc_y - double(j);
        } 

        /**
         * Calculate the interpolated layer value at the given normalized coordinates.
         * @param[in] ndc_x Normalized X-axis coordinate.
         * @param[in] ndc_y Normalized Y-axis coordinate.
         * @param[out] value Returned layer value.
         * @return true if value is valid, else false
         */
        inline bool getInterpolatedValue(double ndc_x, double ndc_y, float& value)
        {
            unsigned int i,j;
            double ir, jr;
            computeIndices(ndc_x, ndc_y, i, j, ir, jr);
            value = 0.0f;
            double div = 0.0f;
            float v,r;
            
            r = (1.0f-ir)*(1.0f-jr);
            if (r>0.0 && getValue(i,j,v)) 
            {
                value += v*r;
                div += r;
            }
            
            r = (ir)*(1.0f-jr);
            if (r>0.0 && getValue(i+1,j,v)) 
            {
                value += v*r;
                div += r;
            }
            
            r = (ir)*(jr);
            if (r>0.0 && getValue(i+1,j+1,v)) 
            {
                value += v*r;
                div += r;
            }
            
            r = (1.0f-ir)*(jr);
            if (r>0.0 && getValue(i,j+1,v)) 
            {
                value += v*r;
                div += r;
            }
            
            if (div != 0.0)
            {
                value /= div;
                return true;
            }

            value = 0.0;
            return false;
        }

        inline bool getInterpolatedValidValue(double ndc_x, double ndc_y, float& value)
        {
            unsigned int i,j;
            double ir, jr;
            computeIndices(ndc_x, ndc_y, i, j, ir, jr);
            value = 0.0f;
            double div = 0.0f;
            float v,r;

            r = (1.0f-ir)*(1.0f-jr);
            if (r>0.0 && getValidValue(i,j,v))
            {
                value += v*r;
                div += r;
            }

            r = (ir)*(1.0f-jr);
            if (r>0.0 && getValidValue(i+1,j,v))
            {
                value += v*r;
                div += r;
            }

            r = (ir)*(jr);
            if (r>0.0 && getValidValue(i+1,j+1,v))
            {
                value += v*r;
                div += r;
            }

            r = (1.0f-ir)*(jr);
            if (r>0.0 && getValidValue(i,j+1,v))
            {
                value += v*r;
                div += r;
            }

            if (div != 0.0)
            {
                value /= div;
                return true;
            }

            value = 0.0;
            return false;
        }

        /** 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(bool treatAsElevationLayer) const;

    protected:

        virtual ~Layer();

        std::string                     _filename;
        osg::ref_ptr<Locator>           _locator;
        unsigned int                    _minLevel;
        unsigned int                    _maxLevel;
        osg::ref_ptr<ValidDataOperator> _validDataOperator;
        osg::Vec4                       _defaultValue;
        osg::Texture::FilterMode        _minFilter;
        osg::Texture::FilterMode        _magFilter;

};

class OSGTERRAIN_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);
        
        META_Object(osgTerrain, 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; }

        virtual bool transform(float offset, float scale);

        void setImage(osg::Image* image);

        /** Return image associated with layer. */  
        virtual osg::Image* getImage() { return _image.get(); }

        /** Return const image associated with layer. */
        virtual const osg::Image* getImage() const { return _image.get(); }

        virtual unsigned int getNumColumns() const { return _image.valid() ? _image->s() : 0; }
        virtual unsigned int getNumRows() const { return _image.valid() ? _image->t() : 0;  }

        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;

        virtual void dirty();
        virtual void setModifiedCount(unsigned int value);
        virtual unsigned int getModifiedCount() const;

    protected:

        virtual ~ImageLayer() {}

        osg::ref_ptr<osg::Image>    _image;

};

class OSGTERRAIN_EXPORT ContourLayer : public Layer
{
    public:

        ContourLayer(osg::TransferFunction1D* tf=0);

        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
        ContourLayer(const ContourLayer& tfLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
        
        META_Object(osgTerrain, ContourLayer);

        virtual bool transform(float offset, float scale);

        void setTransferFunction(osg::TransferFunction1D* tf);
        osg::TransferFunction1D* getTransferFunction() { return _tf.get(); }
        const osg::TransferFunction1D* getTransferFunction() const { return _tf.get(); }

        /** Return image associated with layer. */  
        virtual osg::Image* getImage() { return _tf.valid() ? _tf->getImage() : 0; }

        /** Return const image associated with layer. */
        virtual const osg::Image* getImage() const { return _tf.valid() ? _tf->getImage() : 0; }


        virtual unsigned int getNumColumns() const { return _tf.valid() ? _tf->getNumberImageCells() : 0; }
        virtual unsigned int getNumRows() const { return _tf.valid() ? 1 : 0;  }

        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;

        virtual void dirty();
        virtual void setModifiedCount(unsigned int value);
        virtual unsigned int getModifiedCount() const;

    protected:

        virtual ~ContourLayer() {}

        osg::ref_ptr<osg::TransferFunction1D>    _tf;

};

class OSGTERRAIN_EXPORT HeightFieldLayer : public Layer
{
    public:

        HeightFieldLayer(osg::HeightField* hf=0);

        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
        HeightFieldLayer(const HeightFieldLayer& hfLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
        
        META_Object(osgTerrain, HeightFieldLayer);

        void setFileName(const std::string& filename) { _filename = filename; }
        virtual const std::string& getFileName() const { return _filename; }

        virtual bool transform(float offset, float scale);

        void setHeightField(osg::HeightField* hf);
        osg::HeightField* getHeightField() { return _heightField.get(); }
        const osg::HeightField* getHeightField() const { return _heightField.get(); }

        virtual unsigned int getNumColumns() const { return _heightField.valid() ? _heightField->getNumColumns() : 0; }
        virtual unsigned int getNumRows() const { return _heightField.valid() ? _heightField->getNumRows() : 0;  }

        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;

        virtual void dirty();
        virtual void setModifiedCount(unsigned int value);
        virtual unsigned int getModifiedCount() const;

    protected:

        virtual ~HeightFieldLayer() {}

        unsigned int                    _modifiedCount;
        osg::ref_ptr<osg::HeightField>  _heightField;

};


class OSGTERRAIN_EXPORT ProxyLayer : public Layer
{
    public:

        ProxyLayer();

        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
        ProxyLayer(const ProxyLayer& proxyLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
        
        META_Object(osgTerrain, ProxyLayer);
        
        /** Return image associated with layer if supported. */        
        virtual osg::Image* getImage()
        {
            return _implementation.valid() ? _implementation->getImage() : 0;
        }

        /** Return const image associated with layer if supported. */
        virtual const osg::Image* getImage() const
        {
            return _implementation.valid() ? _implementation->getImage() : 0;
        }

        /** Set the implementation layer that does the actual work.*/
        void setImplementation(Layer* layer) { _implementation = layer; }
        
        /** Get the implementation layer that does the actual work.*/
        Layer* getImplementation() { return _implementation.get(); }

        /** Get the const implementation layer that does the actual work.*/
        const Layer* getImplementation() const { return _implementation.get(); }

        virtual void setFileName(const std::string& filename);
        virtual const std::string& getFileName() const { return _filename; }

        virtual unsigned int getNumColumns() const;
        virtual unsigned int getNumRows() const;
        
        virtual bool transform(float offset, float scale);

        virtual bool getValue(unsigned int i, unsigned int j, float& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const;
        virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const;

        virtual void dirty();
        virtual void setModifiedCount(unsigned int value);
        virtual unsigned int getModifiedCount() const;

        virtual osg::BoundingSphere computeBound(bool treatAsElevationLayer) const;

    protected:

        virtual ~ProxyLayer();
        
        osg::ref_ptr<Layer> _implementation;


};

class OSGTERRAIN_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);
        
        META_Object(osgTerrain, CompositeLayer);

        void clear();

        /** Set the set name of layer 'i'. */
        void setSetName(unsigned int i, const std::string& setname) { _layers[i].setname = setname; if (_layers[i].layer.valid()) _layers[i].layer->setName(setname); }

        /** Get the set name of layer 'i'. */
        const std::string& getSetName(unsigned int i) const { return _layers[i].layer.valid() ? _layers[i].layer->getName() : _layers[i].setname; }

        /** Set the file name of the data associated with layer 'i'. */
        void setFileName(unsigned int i, const std::string& filename) { _layers[i].filename = filename; if (_layers[i].layer.valid()) _layers[i].layer->setFileName(filename); }

        /** Get the file name of the data associated with layer 'i'. */
        const std::string& getFileName(unsigned int i) const { return _layers[i].layer.valid() ? _layers[i].layer->getFileName() : _layers[i].filename; }

        void setCompoundName(unsigned int i, const std::string& compoundname);
        std::string getCompoundName(unsigned int i) const;


        void setLayer(unsigned int i, Layer* layer) { if (i>=_layers.size()) _layers.resize(i+1); _layers[i].layer = layer; }
        Layer* getLayer(unsigned int i) { return i<_layers.size() ? _layers[i].layer.get() : 0; }
        const Layer* getLayer(unsigned int i) const { return i<_layers.size() ? _layers[i].layer.get() : 0; }

        void addLayer(const std::string& compoundname);
        void addLayer(const std::string& setname, const std::string& filename);
        
        void addLayer(Layer* layer) { _layers.push_back(CompoundNameLayer(layer->getName(),layer->getFileName(),layer)); }

        void removeLayer(unsigned int i) { _layers.erase(_layers.begin()+i); }
        
        unsigned int getNumLayers() const { return _layers.size(); }

    protected:

        virtual ~CompositeLayer() {}

        struct CompoundNameLayer
        {
            CompoundNameLayer() {}
        
            CompoundNameLayer(const CompoundNameLayer& cnl):
                setname(cnl.setname),
                filename(cnl.filename),
                layer(cnl.layer) {}

            CompoundNameLayer(const std::string& sn, const std::string& fn, Layer* l):
                setname(sn),
                filename(fn),
                layer(l) {}

            CompoundNameLayer& operator = (const CompoundNameLayer& cnl)
            {
                if (&cnl==this) return *this;
                
                setname = cnl.setname;
                filename = cnl.filename;
                layer = cnl.layer;
                return *this;
            }

            std::string         setname;
            std::string         filename;
            osg::ref_ptr<Layer> layer;
        };
        
        typedef std::vector< CompoundNameLayer > Layers;
        
        Layers _layers;
};


class OSGTERRAIN_EXPORT SwitchLayer : public CompositeLayer
{
    public:

        SwitchLayer();

        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
        SwitchLayer(const SwitchLayer& switchLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
        
        META_Object(osgTerrain, SwitchLayer);
        
        void setActiveLayer(int i) { _activeLayer = i; }
        int getActiveLayer() const { return _activeLayer; }

        /** Return image associated with layer if supported. */        
        virtual osg::Image* getImage()
        {
            if (_activeLayer < 0) return 0;            
            if (_activeLayer >= static_cast<int>(getNumLayers())) return 0;
            return _layers[_activeLayer].layer->getImage();            
        }

        /** Return const image associated with layer if supported. */
        virtual const osg::Image* getImage() const
        {
            if (_activeLayer < 0) return 0;            
            if (_activeLayer >= static_cast<int>(getNumLayers())) return 0;
            return _layers[_activeLayer].layer->getImage();            
        }

    protected:

        virtual ~SwitchLayer() {}
        
        int _activeLayer;
};



}

#endif