osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/Utils

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 #ifndef OSGEARTH_UTILS_H
00020 #define OSGEARTH_UTILS_H 1
00021 
00022 #include <osgEarth/Common>
00023 #include <osgEarth/StringUtils>
00024 
00025 #include <osg/Vec3f>
00026 #include <osg/AutoTransform>
00027 #include <osgGA/GUIEventHandler>
00028 #include <osgViewer/View>
00029 #include <osgUtil/CullVisitor>
00030 
00031 #include <string>
00032 #include <list>
00033 #include <map>
00034 
00035 namespace osg
00036 {
00037     class EllipsoidModel;
00038 }
00039 
00040 namespace osgEarth
00041 {    
00042     //------------------------------------------------------------------------
00043 
00044     class CacheStats
00045     {
00046     public:
00047         CacheStats( unsigned entries, unsigned maxEntries, unsigned queries, float hitRatio )
00048             : _entries(entries), _maxEntries(maxEntries), _queries(queries), _hitRatio(hitRatio) { }
00049 
00050         unsigned _entries;
00051         unsigned _maxEntries;
00052         unsigned _queries;
00053         float    _hitRatio;
00054     };
00055 
00056     //------------------------------------------------------------------------
00057 
00069     template<typename K, typename T>
00070     class LRUCache
00071     {
00072     public:
00073         struct Record {
00074             Record(const T* value) : _value(value) { }
00075             const bool valid() const { return _value != 0L; }
00076             const T& value() const { return *_value; }
00077         private:
00078             bool _valid;
00079             const T* _value;
00080         };
00081 
00082     protected:
00083         typedef typename std::list<K>::iterator lru_iter;
00084         typedef typename std::list<K> lru_type;
00085         typedef typename std::pair<T, lru_iter> map_value_type;
00086         typedef typename std::map<K, map_value_type> map_type;
00087         typedef typename map_type::iterator map_iter;
00088 
00089         map_type _map;
00090         lru_type _lru;
00091         unsigned _max;
00092         unsigned _buf;
00093         unsigned _queries;
00094         unsigned _hits;
00095 
00096     public:
00097         LRUCache( unsigned max =100 ) : _max(max) {
00098             _buf = _max/10;
00099             _queries = 0;
00100             _hits = 0;
00101         }
00102 
00103         void insert( const K& key, const T& value ) {
00104             map_iter mi = _map.find( key );
00105             if ( mi != _map.end() ) {
00106                 _lru.erase( mi->second.second );
00107                 mi->second.first = value;
00108                 _lru.push_back( key );
00109                 mi->second.second = _lru.end();
00110                 mi->second.second--;
00111             }
00112             else {
00113                 _lru.push_back( key );
00114                 lru_iter last = _lru.end(); last--;
00115                 _map[key] = std::make_pair(value, last);
00116             }
00117 
00118             if ( _lru.size() > _max ) {
00119                 for( unsigned i=0; i < _buf; ++i ) {
00120                     const K& key = _lru.front();
00121                     _map.erase( key );
00122                     _lru.pop_front();
00123                 }
00124             }
00125         }
00126 
00127         Record get( const K& key ) {
00128             _queries++;
00129             map_iter mi = _map.find( key );
00130             if ( mi != _map.end() ) {
00131                 _lru.erase( mi->second.second );
00132                 _lru.push_back( key );
00133                 lru_iter new_iter = _lru.end(); new_iter--;
00134                 mi->second.second = new_iter;
00135                 _hits++;
00136                 return Record( &(mi->second.first) );
00137             }
00138             else {
00139                 return Record( 0L );
00140             }
00141         }
00142 
00143         void erase( const K& key ) {
00144             map_iter mi = _map.find( key );
00145             if ( mi != _map.end() ) {
00146                 _lru.erase( mi->second.second );
00147                 _map.erase( mi );
00148             }
00149         }
00150 
00151         void setMaxSize( unsigned max ) {
00152             _max = max;
00153             _buf = max/10;
00154             while( _lru.size() > _max ) {
00155                 const K& key = _lru.front();
00156                 _map.erase( key );
00157                 _lru.pop_front();
00158             }
00159         }
00160 
00161         unsigned getMaxSize() const {
00162             return _max;
00163         }
00164 
00165         CacheStats getStats() const {
00166             return CacheStats(
00167                 _lru.size(), _max, _queries, _queries > 0 ? (float)_hits/(float)_queries : 0.0f );
00168         }
00169     };
00170 
00171 
00177     extern OSGEARTH_EXPORT void removeEventHandler(osgViewer::View* view, osgGA::GUIEventHandler* handler);
00178 
00179 
00183     struct OSGEARTH_EXPORT CullNodeByNormal : public osg::NodeCallback {
00184         osg::Vec3d _normal;
00185         CullNodeByNormal( const osg::Vec3d& normal );
00186         void operator()(osg::Node* node, osg::NodeVisitor* nv);
00187     };
00188 
00189     struct CullDrawableByNormal : public osg::Drawable::CullCallback {
00190         osg::Vec3d _normal;
00191         CullDrawableByNormal( const osg::Vec3d& normal ) : _normal(normal) { }
00192         bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::State* state) const {
00193             return nv && nv->getEyePoint() * _normal <= 0;
00194         }
00195     };
00196 
00197     struct OSGEARTH_EXPORT CullNodeByHorizon : public osg::NodeCallback {
00198         osg::Vec3d _world;
00199         double _r2;
00200         CullNodeByHorizon( const osg::Vec3d& world, const osg::EllipsoidModel* model );
00201         void operator()(osg::Node* node, osg::NodeVisitor* nv );
00202     };
00203 
00204     struct OSGEARTH_EXPORT CullNodeByFrameNumber : public osg::NodeCallback {
00205         unsigned _frame;
00206         CullNodeByFrameNumber() : _frame(0) { }
00207         void operator()( osg::Node* node, osg::NodeVisitor* nv ) {
00208             if ( nv->getFrameStamp()->getFrameNumber() - _frame <= 1 )
00209                 traverse(node, nv);
00210         }
00211     };
00212 
00216     class OSGEARTH_EXPORT PixelAutoTransform : public osg::AutoTransform
00217     {
00218     public:
00219         PixelAutoTransform();
00220 
00225         void setMinPixelWidthAtScaleOne( double pixels ) { _minPixels = pixels; }
00226 
00231         void setSizingNode( osg::Node* node ) { _sizingNode = node; dirty(); }
00232 
00237         void dirty();
00238 
00239     public: // override
00240         void accept( osg::NodeVisitor& nv );
00241 
00242     protected:
00243         double _minPixels;
00244         bool   _dirty;
00245         osg::observer_ptr<osg::Node> _sizingNode;
00246     };
00247 
00251     template<class ValueT, class SuperClass>
00252     class MixinVector : public SuperClass
00253     {
00254         typedef typename std::vector<ValueT> vector_type;
00255     public:
00256         typedef typename vector_type::allocator_type allocator_type;
00257         typedef typename vector_type::value_type value_type;
00258         typedef typename vector_type::const_pointer const_pointer;
00259         typedef typename vector_type::pointer pointer;
00260         typedef typename vector_type::const_reference const_reference;
00261         typedef typename vector_type::reference reference;
00262         typedef typename vector_type::const_iterator const_iterator;
00263         typedef typename vector_type::iterator iterator;
00264         typedef typename vector_type::const_reverse_iterator const_reverse_iterator;
00265         typedef typename vector_type::reverse_iterator reverse_iterator;
00266         typedef typename vector_type::size_type size_type;
00267         typedef typename vector_type::difference_type difference_type;
00268 
00269         explicit MixinVector() : _impl()
00270         {
00271         }
00272 
00273         explicit MixinVector(size_type initial_size, const value_type& fill_value = value_type())
00274         : _impl(initial_size, fill_value)
00275         {
00276         }
00277 
00278         template<class InputIterator>
00279         MixinVector(InputIterator first, InputIterator last)
00280         : _impl(first, last)
00281         {
00282         }
00283 
00284         MixinVector(const vector_type& other)
00285         : _impl(other)
00286         {
00287         }
00288 
00289         MixinVector(const MixinVector& other)
00290         : _impl(other._impl)
00291         {
00292         }
00293 
00294         MixinVector& operator=(const vector_type& other)
00295         {
00296             _impl = other;
00297             return *this;
00298         }
00299 
00300         MixinVector& operator=(const MixinVector& other)
00301         {
00302             _impl = other._impl;
00303             return *this;
00304         }
00305 
00306         virtual ~MixinVector() {}
00307 
00308         void clear() { _impl.clear(); }
00309         void resize(size_type new_size, const value_type& fill_value = value_type()) { _impl.resize(new_size, fill_value); }
00310         void reserve(size_type new_capacity) { _impl.reserve(new_capacity); }
00311         
00312         void swap(vector_type& other) { _impl.swap(other); }
00313         void swap(MixinVector& other) { _impl.swap(other._impl); }
00314 
00315         bool empty() const { return _impl.empty(); }
00316         size_type size() const { return _impl.size(); }
00317         size_type capacity() const { return _impl.capacity(); }
00318         size_type max_size() const { return _impl.max_size(); }
00319         allocator_type get_allocator() const { return _impl.get_allocator(); }
00320 
00321         const_iterator begin() const { return _impl.begin(); }
00322         iterator begin() { return _impl.begin(); }
00323         const_iterator end() const { return _impl.end(); }
00324         iterator end() { return _impl.end(); }
00325 
00326         const_reverse_iterator rbegin() const { return _impl.rbegin(); }
00327         reverse_iterator rbegin() { return _impl.rbegin(); }
00328         const_reverse_iterator rend() const { return _impl.rend(); }
00329         reverse_iterator rend() { return _impl.rend(); }
00330 
00331         const_reference operator[](size_type index) const { return _impl[index]; }
00332         reference operator[](size_type index) { return _impl[index]; }
00333 
00334         const_reference at(size_type index) const { return _impl.at(index); }
00335         reference at(size_type index) { return _impl.at(index); }
00336 
00337         void assign(size_type count, const value_type& value) { _impl.assign(count, value); }
00338         template<class Iter>
00339         void assign(Iter first, Iter last) { _impl.assign(first, last); }
00340 
00341         void push_back(const value_type& value) { _impl.push_back(value); }
00342         void pop_back() { _impl.pop_back(); }
00343 
00344         iterator erase(iterator where) { return _impl.erase(where); }
00345         iterator erase(iterator first, iterator last) { return _impl.erase(first, last); }
00346 
00347         iterator insert(iterator where, const value_type& value) { return _impl.insert(where, value); }
00348 
00349         template<class InputIterator>
00350         void insert(iterator where, InputIterator first, InputIterator last)
00351         {
00352             _impl.insert(where, first, last);
00353         }
00354 
00355         void insert(iterator where, size_type count, const value_type& value)
00356         {
00357             _impl.insert(where, count, value);
00358         }
00359 
00360         const_reference back() const { return _impl.back(); }
00361         reference back() { return _impl.back(); }
00362         const_reference front() const { return _impl.front(); }
00363         reference front() { return _impl.front(); }
00364 
00365         vector_type& asVector() { return _impl; }
00366         const vector_type& asVector() const { return _impl; }
00367 
00368         friend inline bool operator==(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl == right._impl; }
00369         friend inline bool operator==(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl == right; }
00370         friend inline bool operator==(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left == right._impl; }
00371 
00372         friend inline bool operator!=(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl != right._impl; }
00373         friend inline bool operator!=(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl != right; }
00374         friend inline bool operator!=(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left != right._impl; }
00375 
00376         friend inline bool operator<(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl < right._impl; }
00377         friend inline bool operator<(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl < right; }
00378         friend inline bool operator<(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left < right._impl; }
00379 
00380         friend inline bool operator>(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl > right._impl; }
00381         friend inline bool operator>(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl > right; }
00382         friend inline bool operator>(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left > right._impl; }
00383 
00384         friend inline bool operator<=(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl <= right._impl; }
00385         friend inline bool operator<=(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl <= right; }
00386         friend inline bool operator<=(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left <= right._impl; }
00387 
00388         friend inline bool operator>=(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl >= right._impl; }
00389         friend inline bool operator>=(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl >= right; }
00390         friend inline bool operator>=(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left >= right._impl; }
00391 
00392     private:
00393         vector_type _impl;
00394     };
00395 
00396 }
00397 
00398 #endif // OSGEARTH_UTILS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines