osgEarth 2.1.1
|
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_MAP_H 00021 #define OSGEARTH_MAP_H 1 00022 00023 #include <osgEarth/Common> 00024 #include <osgEarth/Caching> 00025 #include <osgEarth/Profile> 00026 #include <osgEarth/MapOptions> 00027 #include <osgEarth/ImageLayer> 00028 #include <osgEarth/ElevationLayer> 00029 #include <osgEarth/ModelLayer> 00030 #include <osgEarth/MaskLayer> 00031 #include <osgEarth/Revisioning> 00032 #include <osgEarth/ThreadingUtils> 00033 #include <osgDB/ReaderWriter> 00034 #include <osg/TransferFunction> 00035 00036 namespace osgEarth 00037 { 00038 class MapInfo; 00039 00043 struct MapModelChange 00044 { 00045 enum ActionType { 00046 ADD_IMAGE_LAYER, 00047 REMOVE_IMAGE_LAYER, 00048 MOVE_IMAGE_LAYER, 00049 ADD_ELEVATION_LAYER, 00050 REMOVE_ELEVATION_LAYER, 00051 MOVE_ELEVATION_LAYER, 00052 ADD_MODEL_LAYER, 00053 REMOVE_MODEL_LAYER, 00054 MOVE_MODEL_LAYER, 00055 ADD_MASK_LAYER, 00056 REMOVE_MASK_LAYER, 00057 UNSPECIFIED 00058 }; 00059 00060 MapModelChange( ActionType action, Revision mapModeRev, Layer* layer, int firstIndex =-1, int secondIndex =-1 ) 00061 : _action(action), _layer(layer), _modelRevision(mapModeRev), _firstIndex(firstIndex), _secondIndex(secondIndex) { } 00062 00063 const ActionType& getAction() const { return _action; } 00064 const Revision& getRevision() const { return _modelRevision; } 00065 int getFirstIndex() const { return _firstIndex; } 00066 int getSecondIndex() const { return _secondIndex; } 00067 Layer* getLayer() const { return _layer.get(); } 00068 ImageLayer* getImageLayer() const { return static_cast<ImageLayer*>(_layer.get()); } 00069 ElevationLayer* getElevationLayer() const { return static_cast<ElevationLayer*>(_layer.get()); } 00070 ModelLayer* getModelLayer() const { return static_cast<ModelLayer*>(_layer.get()); } 00071 MaskLayer* getMaskLayer() const { return static_cast<MaskLayer*>(_layer.get()); } 00072 00073 private: 00074 ActionType _action; 00075 osg::ref_ptr<Layer> _layer; 00076 Revision _modelRevision; 00077 int _firstIndex, _secondIndex; 00078 }; 00079 00083 struct OSGEARTH_EXPORT MapCallback : public osg::Referenced 00084 { 00085 virtual void onMapInfoEstablished( const MapInfo& mapInfo ) { } 00086 00087 virtual void onMapModelChanged( const MapModelChange& change ); 00088 00089 virtual void onImageLayerAdded( ImageLayer* layer, unsigned int index ) { } 00090 virtual void onImageLayerRemoved( ImageLayer* layer, unsigned int index ) { } 00091 virtual void onImageLayerMoved( ImageLayer* layer, unsigned int oldIndex, unsigned int newIndex ) { } 00092 00093 virtual void onElevationLayerAdded( ElevationLayer* layer, unsigned int index ) { } 00094 virtual void onElevationLayerRemoved( ElevationLayer* layer, unsigned int index ) { } 00095 virtual void onElevationLayerMoved( ElevationLayer* layer, unsigned int oldIndex, unsigned int newIndex ) { } 00096 00097 virtual void onModelLayerAdded( ModelLayer* layer, unsigned int index ) { } 00098 virtual void onModelLayerRemoved( ModelLayer* layer ) { } 00099 virtual void onModelLayerMoved( ModelLayer* layer, unsigned int oldIndex, unsigned int newIndex ) { } 00100 00101 virtual void onMaskLayerAdded( MaskLayer* mask ) { } 00102 virtual void onMaskLayerRemoved( MaskLayer* mask ) { } 00103 }; 00104 00105 typedef std::list< osg::ref_ptr<MapCallback> > MapCallbackList; 00106 00112 class OSGEARTH_EXPORT Map : public osg::Referenced 00113 { 00114 public: 00118 Map( const MapOptions& options =MapOptions() ); 00119 00120 public: 00124 const MapOptions& getMapOptions() const { return _mapOptions; } 00125 00130 const Profile* getProfile() const; 00131 00137 int getImageLayers( ImageLayerVector& out_layers, bool validLayersOnly =false ) const; 00138 00142 int getNumImageLayers() const; 00143 00147 ImageLayer* getImageLayerByName( const std::string& name ) const; 00148 00152 ImageLayer* getImageLayerByUID( UID layerUID ) const; 00153 00157 ImageLayer* getImageLayerAt( int index ) const; 00158 00164 int getElevationLayers( ElevationLayerVector& out_layers, bool validLayersOnly =false ) const; 00165 00169 int getNumElevationLayers() const; 00170 00174 ElevationLayer* getElevationLayerByName( const std::string& name ) const; 00175 00179 ElevationLayer* getElevationLayerByUID( UID layerUID ) const; 00180 00184 ElevationLayer* getElevationLayerAt( int index ) const; 00185 00191 int getModelLayers( ModelLayerVector& out_layers, bool validLayersOnly =false ) const; 00192 00196 int getNumModelLayers() const; 00197 00201 ModelLayer* getModelLayerByName( const std::string& name ) const; 00202 00206 ModelLayer* getModelLayerByUID( UID layerUID ) const; 00207 00211 ModelLayer* getModelLayerAt( int index ) const; 00212 00218 int getTerrainMaskLayers( MaskLayerVector& out_list ) const; 00219 00224 void addMapCallback( MapCallback* callback ) const; 00225 00226 void removeMapCallback( MapCallback* callback ); 00227 00231 void addImageLayer( ImageLayer* layer ); 00232 00236 void insertImageLayer( ImageLayer* layer, unsigned int index ); 00237 00241 void removeImageLayer( ImageLayer* layer ); 00242 00246 void moveImageLayer( ImageLayer* layer, unsigned int newIndex ); 00247 00251 void addElevationLayer( ElevationLayer* layer ); 00252 00256 void removeElevationLayer( ElevationLayer* layer ); 00257 00261 void moveElevationLayer( ElevationLayer* layer, unsigned int newIndex ); 00262 00266 void addModelLayer( ModelLayer* layer ); 00267 00271 void insertModelLayer( ModelLayer* layer, unsigned int index ); 00272 00276 void removeModelLayer( ModelLayer* layer ); 00277 00281 void moveModelLayer( ModelLayer* layer, unsigned int newIndex ); 00282 00286 void addTerrainMaskLayer( MaskLayer* layer ); 00287 00291 void removeTerrainMaskLayer( MaskLayer* layer ); 00292 00293 public: 00297 const osgDB::ReaderWriter::Options* getGlobalOptions() const; 00298 00299 void setGlobalOptions( const osgDB::ReaderWriter::Options* options ); 00300 00304 void setName( const std::string& name ); 00305 00309 const std::string& getName() const { return _name; } 00310 00320 bool getHeightField( 00321 const TileKey& key, 00322 bool fallback, 00323 osg::ref_ptr<osg::HeightField>& out_hf, 00324 bool* out_isFallback =0L, 00325 ElevationInterpolation interpolation =INTERP_AVERAGE, 00326 ElevationSamplePolicy samplePolicy =SAMPLE_FIRST_VALID, 00327 ProgressCallback* progress = 0) const; 00328 00332 void setCache( Cache* cache ); 00333 00337 Cache* getCache() const; 00338 00344 Revision getDataModelRevision() const; 00345 00350 bool isGeocentric() const; 00351 00355 bool toMapPoint( const osg::Vec3d& input, const SpatialReference* input_srs, osg::Vec3d& output ) const; 00356 00360 bool mapPointToWorldPoint( const osg::Vec3d& input, osg::Vec3d& output ) const; 00361 00365 bool worldPointToMapPoint( const osg::Vec3d& input, osg::Vec3d& output ) const; 00366 00372 bool sync( class MapFrame& frame ) const; 00373 00374 enum ModelParts { 00375 IMAGE_LAYERS = 1 << 0, 00376 ELEVATION_LAYERS = 1 << 1, 00377 TERRAIN_LAYERS = IMAGE_LAYERS | ELEVATION_LAYERS, 00378 MODEL_LAYERS = 1 << 2, 00379 MASK_LAYERS = 1 << 3, 00380 MASKED_TERRAIN_LAYERS = TERRAIN_LAYERS | MASK_LAYERS, 00381 ENTIRE_MODEL = 0xff 00382 }; 00383 00384 protected: 00385 00386 ~Map() { } 00387 00388 private: 00389 00390 MapOptions _mapOptions; 00391 std::string _name; 00392 ImageLayerVector _imageLayers; 00393 ElevationLayerVector _elevationLayers; 00394 ModelLayerVector _modelLayers; 00395 MaskLayerVector _terrainMaskLayers; 00396 MapCallbackList _mapCallbacks; 00397 osg::ref_ptr<const osgDB::ReaderWriter::Options> _globalOptions; 00398 Threading::ReadWriteMutex _mapDataMutex; 00399 osg::ref_ptr<const Profile> _profile; 00400 osg::ref_ptr<Cache> _cache; 00401 Revision _dataModelRevision; 00402 00403 private: 00404 void calculateProfile(); 00405 }; 00406 00407 00412 class OSGEARTH_EXPORT MapInfo 00413 { 00414 public: 00415 MapInfo( const Map* map ) 00416 : _profile( map->getProfile() ), 00417 _isGeocentric( map->isGeocentric() ), 00418 _isCube( map->getMapOptions().coordSysType() == MapOptions::CSTYPE_GEOCENTRIC_CUBE ) { } 00419 00420 MapInfo( const MapInfo& rhs ) 00421 : _profile( rhs._profile ), 00422 _isGeocentric( rhs._isGeocentric ), 00423 _isCube( rhs._isCube ) { } 00424 00425 const Profile* getProfile() const { return _profile.get(); } 00426 00427 bool isGeocentric() const { return _isGeocentric; } 00428 bool isCube() const { return _isCube; } 00429 bool isPlateCarre() const { return !_isGeocentric && isGeographicSRS(); } 00430 00431 bool isProjectedSRS() const { return !isGeographicSRS(); } 00432 bool isGeographicSRS() const { return _profile->getSRS()->isGeographic(); } 00433 00434 bool toMapPoint( const osg::Vec3d& input, const SpatialReference* input_srs, osg::Vec3d& output ) const; 00435 bool mapPointToWorldPoint( const osg::Vec3d& input, osg::Vec3d& output ) const; 00436 bool worldPointToMapPoint( const osg::Vec3d& input, osg::Vec3d& output ) const; 00437 00438 private: 00439 osg::ref_ptr<const Profile> _profile; 00440 bool _isGeocentric, _isCube; 00441 }; 00442 00443 00449 class OSGEARTH_EXPORT MapFrame 00450 { 00451 public: 00455 MapFrame( const Map* map, Map::ModelParts parts =Map::TERRAIN_LAYERS, const std::string& name ="" ); 00456 00462 MapFrame( const Map* map, bool copyValidDataOnly, Map::ModelParts parts =Map::TERRAIN_LAYERS, const std::string& name ="" ); 00463 00467 MapFrame( const MapFrame& frame, const std::string& name ="" ); 00468 00473 bool sync(); 00474 00476 const MapInfo& getMapInfo() const { return _mapInfo; } 00477 00479 const Profile* getProfile() const { return _mapInfo.getProfile(); } 00480 00482 const ImageLayerVector& imageLayers() const { return _imageLayers; } 00483 ImageLayer* getImageLayerAt( int index ) const { return _imageLayers[index].get(); } 00484 ImageLayer* getImageLayerByUID( UID uid ) const; 00485 ImageLayer* getImageLayerByName( const std::string& name ) const; 00486 00488 const ElevationLayerVector& elevationLayers() const { return _elevationLayers; } 00489 ElevationLayer* getElevationLayerAt( int index ) const { return _elevationLayers[index].get(); } 00490 ElevationLayer* getElevationLayerByUID( UID uid ) const; 00491 ElevationLayer* getElevationLayerByName( const std::string& name ) const; 00492 00494 const ModelLayerVector& modelLayers() const { return _modelLayers; } 00495 ModelLayer* getModelLayerAt(int index) const { return _modelLayers[index].get(); } 00496 00498 const MaskLayerVector& terrainMaskLayers() const { return _maskLayers; } 00499 00501 int indexOf( ImageLayer* layer ) const; 00502 int indexOf( ElevationLayer* layer ) const; 00503 int indexOf( ModelLayer* layer ) const; 00504 00506 Revision getRevision() const { return _mapDataModelRevision; } 00507 00509 bool isCached( const TileKey& key ) const; 00510 00515 bool getHeightField( 00516 const TileKey& key, 00517 bool fallback, 00518 osg::ref_ptr<osg::HeightField>& out_hf, 00519 bool* out_isFallback =0L, 00520 ElevationInterpolation interpolation =INTERP_AVERAGE, 00521 ElevationSamplePolicy samplePolicy =SAMPLE_FIRST_VALID, 00522 ProgressCallback* progress = 0) const; 00523 00524 private: 00525 bool _initialized; 00526 osg::ref_ptr<const Map> _map; 00527 std::string _name; 00528 MapInfo _mapInfo; 00529 Map::ModelParts _parts; 00530 bool _copyValidDataOnly; 00531 Revision _mapDataModelRevision; 00532 ImageLayerVector _imageLayers; 00533 ElevationLayerVector _elevationLayers; 00534 ModelLayerVector _modelLayers; 00535 MaskLayerVector _maskLayers; 00536 friend class Map; 00537 }; 00538 00539 } 00540 00541 #endif // OSGEARTH_MAP_H