osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarthDrivers/debug/DebugTileSource.cpp

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 #include <osg/Version>
00021 #include <osgEarth/TileSource>
00022 #include <osgEarth/Registry>
00023 #include <osgEarth/ImageUtils>
00024 #include <osgEarth/StringUtils>
00025 #include <osgEarthSymbology/Geometry>
00026 #include <osgEarthSymbology/GeometryRasterizer>
00027 #include <osgDB/FileNameUtils>
00028 #if !((OPENSCENEGRAPH_MAJOR_VERSION <= 2) && (OPENSCENEGRAPH_MINOR_VERSION <= 8))
00029 # include <osgText/Glyph>
00030 #endif
00031 #include <osgText/Font>
00032 #include <osg/Notify>
00033 #include <sstream>
00034 
00035 #include "DebugOptions"
00036 
00037 using namespace osgEarth;
00038 using namespace osgEarth::Drivers;
00039 using namespace osgEarth::Symbology;
00040 
00041 namespace
00042 {
00043     static osg::Vec4 colors[4] = {
00044         osg::Vec4(1,0,0,1),
00045         osg::Vec4(0,1,0,1),
00046         osg::Vec4(0,0,1,1),
00047         osg::Vec4(1,0,1,1)
00048     };
00049 
00050     void copySubImageAndColorize( const osg::Image* src, osg::Image* dst, unsigned dx, unsigned dy, osg::Vec4& newColor)
00051     {
00052         ImageUtils::PixelReader read(src);
00053         ImageUtils::PixelWriter write(dst);
00054 
00055         for( int src_t=0, dst_t=dy; src_t < src->t(); src_t++, dst_t++ )
00056         {
00057             for( int src_s=0, dst_s=dx; src_s < src->s(); src_s++, dst_s++ )
00058             {           
00059                 osg::Vec4 color = read(src_s, src_t);
00060                 if ( color.a() > 0.5f )
00061                     color = newColor;
00062                 write( color, dst_s, dst_t );
00063             }
00064         }
00065     }
00066 }
00067 
00068 class DebugTileSource : public TileSource
00069 {
00070 public:
00071     DebugTileSource( const DebugOptions& options ) : TileSource( options ), _options(options)
00072     {        
00073         _geom = new Ring();
00074         _geom->push_back( osg::Vec3(5, 5, 0) );
00075         _geom->push_back( osg::Vec3(250, 5, 0) );
00076         _geom->push_back( osg::Vec3(250, 250, 0) );
00077         _geom->push_back( osg::Vec3(5, 250, 0) );
00078         _font = osgText::readFontFile( "arial.ttf" );
00079 
00080         _color = osgEarth::htmlColorToVec4f( *_options.colorCode() );
00081     }
00082 
00083     // Yahoo! uses spherical mercator, but the top LOD is a 2x2 tile set.
00084     void initialize( const std::string& referenceURI, const Profile* overrideProfile)
00085     {
00086         if ( overrideProfile )
00087             setProfile( overrideProfile );
00088         else
00089             setProfile( Profile::create("global-geodetic") );
00090     }
00091 
00092     osg::Image* createImage( const TileKey& key, ProgressCallback* progress )
00093     {
00094         // first draw the colored outline:
00095         GeometryRasterizer rasterizer( 256, 256 );
00096         rasterizer.draw( _geom.get(), colors[key.getLevelOfDetail() % 4] );
00097         osg::Image* image = rasterizer.finalize();
00098         
00099         // next render the tile key text:
00100         std::stringstream buf;        
00101         if (*_options.tms())
00102         {
00103             //Print out a TMS key for the TileKey
00104             unsigned int tileX, tileY;
00105             key.getTileXY(tileX, tileY);        
00106             unsigned int numRows, numCols;
00107             key.getProfile()->getNumTiles(key.getLevelOfDetail(), numCols, numRows);
00108             tileY  = numRows - tileY - 1;
00109             buf << key.getLevelOfDetail() << "/" << tileX << "/" << tileY;
00110         }
00111         else
00112         {
00113             buf << key.str();
00114         }        
00115         
00116         std::string text = buf.str();
00117 
00118         unsigned x = 10, y = 10;
00119 
00120         osgText::FontResolution resolution(32, 32);
00121         for( unsigned i=0; i<text.length(); ++i )
00122         {
00123 #if !((OPENSCENEGRAPH_MAJOR_VERSION <= 2) && (OPENSCENEGRAPH_MINOR_VERSION <= 8))
00124             osgText::Glyph* glyph = _font->getGlyph( resolution, text.at(i) );
00125             copySubImageAndColorize( glyph, image, x, y, _color );
00126             x += glyph->s() + 1;
00127 #endif
00128         }
00129 
00130         return image;
00131     }
00132 
00133     virtual std::string getExtension() const 
00134     {
00135         return "png";
00136     }
00137 
00138     virtual bool supportsPersistentCaching() const
00139     {
00140         return false;
00141     }
00142 
00143 private:
00144     const DebugOptions _options;
00145     osg::ref_ptr<Geometry> _geom;
00146     osg::ref_ptr<osgText::Font> _font;
00147     osg::Vec4 _color;
00148 };
00149 
00150 
00151 class DebugTileSourceDriver : public TileSourceDriver
00152 {
00153     public:
00154         DebugTileSourceDriver()
00155         {
00156             supportsExtension( "osgearth_debug", "Debugging driver" );
00157         }
00158 
00159         virtual const char* className()
00160         {
00161             return "Debugging Driver";
00162         }
00163 
00164         virtual ReadResult readObject(const std::string& file_name, const Options* options) const
00165         {
00166             if ( !acceptsExtension(osgDB::getLowerCaseFileExtension( file_name )))
00167                 return ReadResult::FILE_NOT_HANDLED;
00168 
00169             return new DebugTileSource( getTileSourceOptions(options) );
00170         }
00171 };
00172 
00173 REGISTER_OSGPLUGIN(osgearth_debug, DebugTileSourceDriver)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines