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_TILEVISITOR_H
23#define OSGEARTH_TILEVISITOR_H 1
24
25#include <osgEarth/Common>
26#include <osgEarth/TileHandler>
27#include <osgEarth/Profile>
28#include <osgEarth/TaskService>
29
30namespace osgEarth
31{
32    /**
33    * Utility class that traverses a Profile and emits TileKey's based on a collection of extents and min/max levels
34    */
35    class OSGEARTH_EXPORT TileVisitor : public osg::Referenced
36    {
37    public:
38
39        TileVisitor();
40
41        TileVisitor(TileHandler* handler);
42
43        /**
44        * Sets the minimum level to visit
45        */
46        void setMinLevel(const unsigned int& minLevel) {_minLevel = minLevel;}
47
48        /**
49        * Gets the minimum level to visit
50        */
51        const unsigned int getMinLevel() const {return _minLevel;}
52
53        /**
54        * Sets the maximum level to visit
55        */
56        void setMaxLevel(const unsigned int& maxLevel) {_maxLevel = maxLevel;}
57
58        /**
59        * Gets the maximum level to visit
60        */
61        const unsigned int getMaxLevel() const {return _maxLevel;}
62
63
64        /**
65        * Extents to visit
66        */
67        void addExtent( const GeoExtent& extent );
68        const std::vector< GeoExtent >& getExtents() const { return _extents; }
69
70        virtual void run(const Profile* mapProfile);
71
72        bool intersects( const GeoExtent& extent );
73
74        void setTileHandler( TileHandler* handler );
75
76        void setProgressCallback( ProgressCallback* progress );
77
78        void incrementProgress( unsigned int progress );
79
80        void resetProgress();
81
82
83    protected:
84
85        void estimate();
86
87        virtual bool handleTile( const TileKey& key );
88
89        void processKey( const TileKey& key );
90
91        unsigned int _minLevel;
92        unsigned int _maxLevel;
93
94        std::vector< GeoExtent > _extents;
95
96        osg::ref_ptr< TileHandler > _tileHandler;
97
98        osg::ref_ptr< ProgressCallback > _progress;
99
100        osg::ref_ptr< const Profile > _profile;
101
102        OpenThreads::Mutex _progressMutex;
103
104        unsigned int _total;
105        unsigned int _processed;
106    };
107
108
109    /**
110    * A TileVisitor that pushes all of it's generated keys onto a TaskService queue and handles them in background threads.
111    */
112    class OSGEARTH_EXPORT MultithreadedTileVisitor: public TileVisitor
113    {
114    public:
115        MultithreadedTileVisitor();
116
117        MultithreadedTileVisitor( TileHandler* handler );
118
119        unsigned int getNumThreads() const;
120        void setNumThreads( unsigned int numThreads);
121
122        virtual void run(const Profile* mapProfile);
123
124    protected:
125
126        virtual bool handleTile( const TileKey& key );
127
128        unsigned int _numThreads;
129
130        // The work queue to pass seed operations to
131        osg::ref_ptr<osgEarth::TaskService> _taskService;
132    };
133
134
135    typedef std::vector< TileKey > TileKeyList;
136
137
138    /**
139     * A list of TileKeys that you can serialize to a file
140     */
141    class OSGEARTH_EXPORT TaskList
142    {
143    public:
144        TaskList(const Profile* profile);
145
146        /**
147         * Loads the tiles from the given file.
148         */
149        bool load( const std::string &filename);
150
151        /**
152         * Saves the tiles to the given file.
153         */
154        void save( const std::string& filename);
155
156        /**
157         * Gets the list of keys
158         */
159        TileKeyList& getKeys();
160
161        const TileKeyList& getKeys() const;
162
163    protected:
164        TileKeyList _keys;
165        osg::ref_ptr< const Profile > _profile;
166    };
167
168
169
170
171    /**
172    * A TileVisitor that launches an external process to process tiles.
173    */
174    class OSGEARTH_EXPORT MultiprocessTileVisitor: public TileVisitor
175    {
176    public:
177        MultiprocessTileVisitor();
178
179        MultiprocessTileVisitor( TileHandler* handler );
180
181        unsigned int getNumProcesses() const;
182        void setNumProcesses( unsigned int numProcesses);
183
184        unsigned int getBatchSize() const;
185        void setBatchSize( unsigned int batchSize );
186
187        virtual void run(const Profile* mapProfile);
188
189        const std::string& getEarthFile() const;
190        void setEarthFile( const std::string& earthFile );
191
192    protected:
193
194        virtual bool handleTile( const TileKey& key );
195
196        void processBatch();
197
198        TileKeyList _batch;
199
200        unsigned int _batchSize;
201        unsigned int _numProcesses;
202
203        std::string _earthFile;
204
205        // The work queue to pass seed operations to
206        osg::ref_ptr<osgEarth::TaskService> _taskService;
207    };
208
209
210    /**
211    * A TileVisitor that simply emits keys from a list.  Useful for running a list of tasks.
212    */
213    class OSGEARTH_EXPORT TileKeyListVisitor : public TileVisitor
214    {
215    public:
216        TileKeyListVisitor();
217
218        void setKeys(const TileKeyList& keys);
219
220        virtual void run(const Profile* mapProfile);
221
222    protected:
223
224        TileKeyList _keys;
225    };
226
227
228
229} // namespace osgEarth
230
231#endif // OSGEARTH_TRAVERSAL_DATA_H
232