osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarthUtil/Annotation.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 <osgEarthUtil/Annotation>
00021 #include <osgEarth/HTTPClient>
00022 #include <osgEarth/Utils>
00023 #include <osgEarthSymbology/GeometryFactory>
00024 #include <osgEarthFeatures/GeometryCompiler>
00025 #include <osgEarthFeatures/BuildGeometryFilter>
00026 
00027 using namespace osgEarth;
00028 using namespace osgEarth::Features;
00029 using namespace osgEarth::Symbology;
00030 using namespace osgEarth::Util::Annotation;
00031 using namespace osgEarth::Util::Controls;
00032 
00033 //------------------------------------------------------------------------
00034 
00035 PlacemarkNode::PlacemarkNode(MapNode*           mapNode,
00036                              const osg::Vec3d&  mapPosition,
00037                              osg::Image*        image,
00038                              const std::string& text,
00039                              const Style&       style ) :
00040 _mapNode( mapNode ),
00041 _image  ( image ),
00042 _text   ( text ),
00043 _style  ( style )
00044 {
00045     init();
00046     setPosition( mapPosition );
00047 }
00048 
00049 void
00050 PlacemarkNode::setPosition( const osg::Vec3d& pos )
00051 {
00052     if ( _mapNode.valid() )
00053     {
00054         osg::Vec3d world;
00055         if ( _mapNode->getMap()->mapPointToWorldPoint(pos, world) )
00056         {
00057             this->setMatrix( osg::Matrix::translate(world) );
00058             static_cast<CullNodeByHorizon*>(this->getCullCallback())->_world = world;
00059         }
00060     }
00061 }
00062 
00063 void
00064 PlacemarkNode::init()
00065 {
00066     // remove any old stuff to make way for the new stuff.
00067     this->removeChildren(0, this->getNumChildren());
00068 
00069     this->setCullCallback( new CullNodeByHorizon(
00070         osg::Vec3d(0,0,1),
00071         _mapNode->getMap()->getProfile()->getSRS()->getEllipsoid()) );
00072 
00073     _label = new LabelControl(_text);
00074 
00075     TextSymbol* s = _style.get<TextSymbol>();
00076     if ( s )
00077     {
00078         if ( s->font().isSet() )
00079             _label->setFont( osgText::readFontFile( *s->font() ) );
00080         if ( s->size().isSet() )
00081             _label->setFontSize( *s->size() );
00082         if ( s->fill().isSet() )
00083             _label->setForeColor( s->fill()->color() );
00084         if ( s->halo().isSet() )
00085             _label->setHaloColor( s->halo()->color() );
00086         if ( s->content().isSet() && _text.empty() )
00087             _label->setText( s->content()->eval() );
00088     }
00089 
00090     if ( !_image.valid() )
00091     {
00092         MarkerSymbol* marker = _style.get<MarkerSymbol>();
00093         if ( marker )
00094         {
00095             _image = marker->getImage();
00096             if ( !_image.valid() && marker->url().isSet() )
00097             {
00098                 _image = URI(marker->url()->expr()).readImage();
00099             }
00100         }
00101     }
00102     _icon = new ImageControl( _image.get() );
00103 
00104     _container = new HBox();
00105     _container->setChildSpacing( 8 );
00106     
00107     _container->addControl( _icon.get() );
00108     _container->addControl( _label.get() );
00109 
00110     _container->setHorizAlign( Control::ALIGN_RIGHT );
00111     _container->setVertAlign( Control::ALIGN_CENTER );
00112 
00113     //todo: set up the "ANCHOR POINT" for the sweet spot
00114 
00115     // wrap the other controls in a scene node.
00116     ControlNode* node = new ControlNode( _container.get() );
00117     this->addChild( node );
00118 }
00119 
00120 void
00121 PlacemarkNode::setIconImage( osg::Image* image )
00122 {
00123     if ( image )
00124     {
00125         _image = image;
00126         if ( _icon.valid() )
00127             _icon->setImage( image );
00128     }
00129 }
00130 
00131 void
00132 PlacemarkNode::setText( const std::string& text )
00133 {
00134     if ( text != _text )
00135     {
00136         _text = text;
00137         if ( _label.valid() )
00138             _label->setText( text );
00139     }
00140 }
00141 
00142 void
00143 PlacemarkNode::setStyle( const Style& style )
00144 {
00145     _style = style;
00146     init();
00147 }
00148 
00149 //------------------------------------------------------------------------
00150 
00151 GeometryNode::GeometryNode(Geometry*    geom,
00152                            const Style& style) :
00153 DrapeableNode( NULL ),
00154 _geom        ( geom ),
00155 _style       ( style )
00156 {
00157     init();
00158 }
00159 
00160 GeometryNode::GeometryNode(MapNode*     mapNode,
00161                            Geometry*    geom,
00162                            const Style& style,
00163                            bool         draped ) :
00164 DrapeableNode( mapNode, draped ),
00165 _geom        ( geom ),
00166 _style       ( style )
00167 {
00168     init();
00169 }
00170 
00171 void
00172 GeometryNode::init()
00173 {
00174     FeatureList features;
00175     features.push_back( new Feature(_geom.get(), _style) );
00176     BuildGeometryFilter bg;
00177     // no worky.
00178         FilterContext context;
00179     osg::Node* node = bg.push( features, context );
00180     //osg::Node* node = bg.getNode();
00181     setNode( node );
00182 }
00183 
00184 //------------------------------------------------------------------------
00185 
00186 // options:
00187 // - geodetic, draped
00188 // - projected
00189 // - localized
00190 
00191 CircleNode::CircleNode(MapNode*                mapNode,
00192                        const osg::Vec3d&       center,
00193                        const Linear&           radius,
00194                        const Style&            style,
00195                        bool                    draped,
00196                        unsigned                numSegments) :
00197 FeatureNode( mapNode, 0L, draped )
00198 {
00199     if ( mapNode )
00200     {
00201         const SpatialReference* targetSRS = mapNode->getMap()->getProfile()->getSRS();
00202 
00203         GeometryFactory factory( targetSRS );
00204 
00205         Geometry* geom = factory.createCircle(center, radius, numSegments);
00206         if ( geom )
00207         {
00208             Feature* feature = new Feature( geom, style );
00209             feature->geoInterp() = GEOINTERP_GREAT_CIRCLE;
00210             setFeature( feature );
00211         }
00212     }
00213 }
00214 
00215 //------------------------------------------------------------------------
00216 
00217 EllipseNode::EllipseNode(MapNode*          mapNode,
00218                          const osg::Vec3d& center,
00219                          const Linear&     radiusMajor,
00220                          const Linear&     radiusMinor,
00221                          const Angular&    rotationAngle,
00222                          const Style&      style,
00223                          bool              draped,
00224                          unsigned          numSegments) :
00225 FeatureNode( mapNode, 0L, draped )
00226 {
00227     if ( mapNode )
00228     {
00229         GeometryFactory factory( mapNode->getMap()->getProfile()->getSRS() );
00230         Geometry* geom = factory.createEllipse(center, radiusMajor, radiusMinor, rotationAngle, numSegments);
00231         if ( geom )
00232         {
00233             Feature* feature = new Feature( geom, style );
00234             feature->geoInterp() = GEOINTERP_GREAT_CIRCLE;
00235             setFeature( feature );
00236         }
00237     }
00238 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines