osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/Caching

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003 * Copyright 2008-2010 Pelican Mapping
00004 * http://osgearth.org
00005 *
00006 * osgEarth is free software; you can redistribute it and/or modify
00007 * it under the terms of the GNU Lesser General Public License as published by
00008 * the Free Software Foundation; either version 2 of the License, or
00009 * (at your option) any later version.
00010 *
00011 * This program is distributed in the hope that it will be useful,
00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 * GNU Lesser General Public License for more details.
00015 *
00016 * You should have received a copy of the GNU Lesser General Public License
00017 * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018 */
00019 
00020 #ifndef OSGEARTH_CACHING_H
00021 #define OSGEARTH_CACHING_H 1
00022 
00023 #include <osgEarth/Common>
00024 #include <osgEarth/Config>
00025 #include <osgEarth/TMS>
00026 #include <osgEarth/TileKey>
00027 
00028 #include <osg/Referenced>
00029 #include <osg/Object>
00030 #include <osg/Image>
00031 #include <osg/Shape>
00032 #include <osg/Timer>
00033 #include <osgDB/ReadFile>
00034 
00035 #include <OpenThreads/ReadWriteMutex>
00036 
00037 #include <string>
00038 #include <list>
00039 #include <map>
00040 
00041 namespace osgEarth
00042 {
00046     class CacheOptions : public DriverConfigOptions // no export (header only)
00047     {
00048     public:
00049         CacheOptions( const ConfigOptions& options =ConfigOptions() )
00050             : DriverConfigOptions( options ),
00051               _cacheOnly( false )
00052         { 
00053             fromConfig( _conf ); 
00054         }
00055 
00057         optional<bool>& cacheOnly() { return _cacheOnly; }
00058         const optional<bool>& cacheOnly() const { return _cacheOnly; }
00059 
00060     public:
00061         virtual Config getConfig() const {
00062             Config conf = ConfigOptions::getConfig();
00063             conf.updateIfSet( "cache_only", _cacheOnly );
00064             return conf;
00065         }
00066         virtual void mergeConfig( const Config& conf ) {
00067             ConfigOptions::mergeConfig( conf );            
00068             fromConfig( conf );
00069         }
00070 
00073         void setReferenceURI(const std::string& referenceURI) { _referenceURI = referenceURI; }
00074         const std::string& getReferenceURI() const { return _referenceURI; }
00075 
00076     private:
00077         void fromConfig( const Config& conf ) {
00078             conf.getIfSet( "cache_only", _cacheOnly );
00079         }
00080 
00081         optional<bool> _cacheOnly;
00082         std::string _referenceURI;
00083     };
00084 
00085     //----------------------------------------------------------------------
00086 
00090     class DiskCacheOptions : public CacheOptions
00091     {
00092     public:
00093         DiskCacheOptions( const ConfigOptions& options =ConfigOptions() )
00094             : CacheOptions( options ),
00095               _writeWorldFiles( false ),
00096                           _imageWriterPluginOptions("")
00097         {
00098             fromConfig( _conf );
00099         }
00100 
00102         void setPath( const std::string& value ) { _path = value; }
00103         const std::string& path() const { return _path; }
00104 
00106         optional<bool>& writeWorldFiles() { return _writeWorldFiles; }
00107         const optional<bool>& writeWorldFiles() const { return _writeWorldFiles; }
00108 
00110         optional<std::string>& imageWriterPluginOptions() { return _imageWriterPluginOptions; }
00111         const optional<std::string>& imageWriterPluginOptions() const { return _imageWriterPluginOptions; }
00112 
00113     public:
00114         virtual Config getConfig() const {
00115             Config conf = CacheOptions::getConfig();
00116             conf.update("path", _path);
00117             conf.updateIfSet("write_world_files", _writeWorldFiles);
00118             conf.updateIfSet("image_writer_plugin_options", _imageWriterPluginOptions);
00119             return conf;
00120         }
00121         virtual void mergeConfig( const Config& conf ) {
00122             CacheOptions::mergeConfig( conf );
00123             fromConfig( conf );
00124         }
00125 
00126     private:
00127         void fromConfig( const Config& conf ) {
00128             _path = conf.value("path");
00129             conf.getIfSet("write_world_files", _writeWorldFiles);
00130             conf.getIfSet("image_writer_plugin_options", _imageWriterPluginOptions);
00131                         
00132         }
00133 
00134         std::string           _path;
00135         optional<bool>        _writeWorldFiles;
00136                 optional<std::string> _imageWriterPluginOptions;
00137     };
00138 
00139     //----------------------------------------------------------------------
00140 
00146     class TMSCacheOptions : public DiskCacheOptions // no export (header only)
00147     {
00148     public:
00149         TMSCacheOptions( const ConfigOptions& options =ConfigOptions() )
00150             : DiskCacheOptions( options ),
00151               _invertY( false )
00152         {
00153             setDriver("tms");
00154             fromConfig( _conf );
00155         }
00156 
00158         optional<bool>& invertY() { return _invertY; }
00159         const optional<bool>& invertY() const { return _invertY; }
00160 
00161     public:
00162         virtual Config getConfig() const {
00163             Config conf = DiskCacheOptions::getConfig();
00164             conf.updateIfSet("invert_y", _invertY);
00165             return conf;
00166         }
00167         virtual void mergeConfig( const Config& conf ) {
00168             DiskCacheOptions::mergeConfig( conf );
00169             fromConfig( conf );
00170         }
00171 
00172     private:
00173         void fromConfig( const Config& conf ) {
00174             conf.getIfSet("invert_y", _invertY);
00175         }
00176 
00177         optional<bool> _invertY;
00178     };
00179 
00180   //----------------------------------------------------------------------
00181 
00185     struct CacheSpec
00186     {
00187         CacheSpec() { }
00188         CacheSpec( const std::string& cacheId, const std::string& format, const std::string& name ="")
00189             :  _cacheId( cacheId ), _format(format), _name(name) { }
00190 
00191         bool empty() const { return _cacheId.empty(); }
00192 
00193         const std::string& cacheId() const { return _cacheId; }
00194         const std::string& format() const { return _format; }
00195         const std::string& name() const { return _name; }
00196 
00197     private:
00198         std::string _cacheId;
00199         std::string _format;
00200         std::string _name; // this is only here so you can see what layer the cache spec is referencing.
00201     };
00202 
00203   //----------------------------------------------------------------------
00204 
00209   class OSGEARTH_EXPORT Cache : public osg::Object
00210   {
00211   public:
00215     virtual bool getImage( const TileKey& key, const CacheSpec& spec, osg::ref_ptr<const osg::Image>& out_image ) =0;
00216 
00220     virtual void setImage( const TileKey& key, const CacheSpec& spec, const osg::Image* image ) = 0;
00221 
00225     virtual bool getHeightField( const TileKey& key, const CacheSpec& spec, osg::ref_ptr<const osg::HeightField>& out_hf );
00226 
00230     virtual void setHeightField( const TileKey& key, const CacheSpec& spec, const osg::HeightField* hf );
00231 
00235     virtual const std::string& getReferenceURI() { return _refURI; }
00236 
00240     virtual void setReferenceURI( const std::string& value ) { _refURI = value; }
00241 
00245     virtual bool isCached( const TileKey& key, const CacheSpec& spec) const { return false; }
00246 
00250     virtual void storeProperties( const CacheSpec& spec, const Profile* profile, unsigned int tileSize ) 
00251         { }
00252 
00256     virtual bool loadProperties( 
00257         const std::string&           cacheId, 
00258         CacheSpec&                   out_spec, 
00259         osg::ref_ptr<const Profile>& out_profile,
00260         unsigned int&                out_tileSize ) { return false; }
00261 
00270     virtual bool compact( bool async =true ) { return false; }
00271 
00280     virtual bool purge(
00281         const std::string& cacheId,
00282         int olderThanTimeStamp =0L,
00283         bool async =true ) { return false; }
00284 
00285     const CacheOptions& getCacheOptions() const { return _options; }
00286 
00287   public:
00288       Cache( const CacheOptions& options =CacheOptions() );
00289       Cache( const Cache& rhs, const osg::CopyOp& op =osg::CopyOp::DEEP_COPY_ALL );
00290 
00291   protected:
00292       CacheOptions _options;
00293       std::string _refURI;
00294   };
00295 
00296   //----------------------------------------------------------------------
00297 
00301   class OSGEARTH_EXPORT MemCache : public Cache
00302   {
00303   public:
00304     MemCache( int maxTilesInCache =16 );
00305     MemCache( const MemCache& rhs, const osg::CopyOp& op =osg::CopyOp::DEEP_COPY_ALL );
00306     META_Object(osgEarth,MemCache);
00307 
00311     unsigned int getMaxNumTilesInCache() const;
00312 
00316     void setMaxNumTilesInCache(unsigned int max);
00317 
00321     virtual bool isCached( const TileKey& key, const CacheSpec& spec ) const;
00322 
00326     virtual bool getImage( const TileKey& key, const CacheSpec& spec, osg::ref_ptr<const osg::Image>& out_image );
00327 
00331     virtual void setImage( const TileKey& key, const CacheSpec& spec, const osg::Image* image );
00332 
00336     virtual bool getHeightField( const TileKey& key, const CacheSpec& spec, osg::ref_ptr<const osg::HeightField>& out_hf );
00337 
00341     virtual void setHeightField( const TileKey& key, const CacheSpec& spec, const osg::HeightField* hf );
00342 
00344     virtual bool purge( const std::string& cacheId, int olderThan, bool async );
00345 
00346   protected:
00350     bool getObject( const TileKey& key, const CacheSpec& spec, osg::ref_ptr<const osg::Object>& out_result );
00351 
00355     void setObject( const TileKey& key, const CacheSpec& spec, const osg::Object* image );
00356 
00357     struct CachedObject
00358     {
00359       std::string _key;
00360       osg::ref_ptr<const osg::Object> _object;
00361     };
00362 
00363     typedef std::list<CachedObject> ObjectList;
00364     ObjectList _objects;
00365 
00366     typedef std::map<std::string,ObjectList::iterator> KeyToIteratorMap;
00367     KeyToIteratorMap _keyToIterMap;
00368 
00369     unsigned int _maxNumTilesInCache;
00370     OpenThreads::Mutex _mutex;
00371 
00372   };
00373 
00377   class OSGEARTH_EXPORT DiskCache : public Cache
00378   {
00379   public:
00380     DiskCache( const DiskCacheOptions& options =DiskCacheOptions() );
00381 
00382     DiskCache( const DiskCache& rhs, const osg::CopyOp& op =osg::CopyOp::DEEP_COPY_ALL );
00383 
00384     META_Object(osgEarth,DiskCache);
00385 
00389     std::string getPath() const;
00390 
00394     virtual bool isCached( const TileKey& key, const CacheSpec& spec ) const;
00395 
00399     virtual std::string getFilename( const TileKey& key, const CacheSpec& spec ) const;
00400 
00404     virtual bool getImage( const TileKey& key, const CacheSpec& spec, osg::ref_ptr<const osg::Image>& out_image );
00405 
00409     virtual void setImage( const TileKey& key, const CacheSpec& spec, const osg::Image* image );
00410 
00414     virtual void storeProperties( const CacheSpec& spec, const Profile* profile, unsigned int tileSize );
00415 
00419     virtual bool loadProperties( 
00420         const std::string&           cacheId, 
00421         CacheSpec&                   out_spec, 
00422         osg::ref_ptr<const Profile>& out_profile,
00423         unsigned int&                out_tileSize );
00424 
00425 
00426   protected:
00427     std::string getTMSPath(const std::string& cacheId) const;
00428 
00429     struct LayerProperties
00430     {
00431       std::string _format;
00432       unsigned int _tile_size;
00433       osg::ref_ptr< const Profile > _profile;
00434     };
00435 
00436     typedef std::map< std::string, LayerProperties > LayerPropertiesCache;
00437     LayerPropertiesCache _layerPropertiesCache;
00438     bool        _writeWorldFilesOverride;     
00439 
00440   private:
00441       DiskCacheOptions _options;
00442   };
00443 
00448   class OSGEARTH_EXPORT TMSCache : public DiskCache
00449   {
00450   public:
00451     TMSCache(const TMSCacheOptions& options =TMSCacheOptions() );
00452 
00453     TMSCache(const TMSCache& rhs, const osg::CopyOp& op =osg::CopyOp::DEEP_COPY_ALL);
00454     META_Object(osgEarth,TMSCache);
00455 
00459     const bool& getInvertY() {return _invertY; }
00460 
00464     void setInvertY(const bool &invertY) {_invertY = invertY;}
00465 
00469     virtual std::string getFilename( const TileKey& key, const CacheSpec& spec ) const;
00470 
00471   private:
00472     bool _invertY;
00473     TMSCacheOptions _options;
00474   };
00475 
00476   //----------------------------------------------------------------------
00477 
00481   class OSGEARTH_EXPORT CacheDriver : public osgDB::ReaderWriter
00482   {
00483   public:
00484       const CacheOptions& getCacheOptions( const osgDB::ReaderWriter::Options* options ) const;
00485   };
00486 
00487   //----------------------------------------------------------------------
00488 
00493   class OSGEARTH_EXPORT CacheFactory
00494   {
00495   public:
00496       static Cache* create( const CacheOptions& options);
00497   };
00498 }
00499 
00500 #endif // OSGEARTH_CACHING_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines