1/* -*-c++-*- */
2/* osgEarth - Geospatial SDK for OpenSceneGraph
3* Copyright 2019 Pelican Mapping
4* http://osgearth.org
5*
6* osgEarth is free software; you can redistribute it and/or modify
7* it under the terms of the GNU Lesser General Public License as published by
8* the Free Software Foundation; either version 2 of the License, or
9* (at your option) any later version.
10*
11* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
16* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
17* IN THE SOFTWARE.
18*
19* You should have received a copy of the GNU Lesser General Public License
20* along with this program.  If not, see <http://www.gnu.org/licenses/>
21*/
22#ifndef OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_HFCACHE
23#define OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_HFCACHE 1
24
25#include "Common"
26#include "TileNode"
27#include "TileNodeRegistry"
28#include "MPTerrainEngineOptions"
29#include <osgEarth/Map>
30#include <osgEarth/Progress>
31#include <osgEarth/ThreadingUtils>
32#include <osgEarth/Containers>
33#include <osgEarth/HeightFieldUtils>
34#include <osgEarth/MapFrame>
35#include <osgEarth/MapInfo>
36
37namespace osgEarth { namespace Drivers { namespace MPTerrainEngine
38{
39    using namespace osgEarth;
40
41    /** Key into the height field cache */
42    struct HFKey
43    {
44        TileKey               _key;
45        Revision              _revision;
46        ElevationSamplePolicy _samplePolicy;
47
48        bool operator < (const HFKey& rhs) const {
49            if ( _key < rhs._key ) return true;
50            if ( rhs._key < _key ) return false;
51            if ( _revision < rhs._revision ) return true;
52            if ( _revision > rhs._revision ) return false;
53            return _samplePolicy < rhs._samplePolicy;
54        }
55    };
56
57    /** value in the height field cache */
58    struct HFValue
59    {
60        osg::ref_ptr<osg::HeightField> _hf;
61        bool                           _isFallback;
62    };
63
64    /** caches hightfields for fast neighor lookup */
65    class HeightFieldCache : public osg::Referenced //, public Revisioned
66    {
67    public:
68        HeightFieldCache(const MPTerrainEngineOptions& options);
69
70        void setTileSize(int tileSize)
71        {
72            _tileSize = tileSize;
73        }
74
75        bool getOrCreateHeightField(
76                const MapFrame&                 frame,
77                const TileKey&                  key,
78                const osg::HeightField*         parent_hf,
79                osg::ref_ptr<osg::HeightField>& out_hf,
80                bool&                           out_isFallback,
81                ElevationSamplePolicy           samplePolicy,
82                ElevationInterpolation          interp,
83                ProgressCallback*               progress );
84
85        void clear()
86        {
87            _cache.clear();
88        }
89
90    private:
91        bool                            _enabled;
92        mutable LRUCache<HFKey,HFValue> _cache;
93        int                             _tileSize;
94        bool                            _useParentAsReferenceHF;
95    };
96
97} } } // namespace osgEarth::Drivers::MPTerrainEngine
98
99#endif // OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_HFCACHE
100