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 #include <osgEarthUtil/Viewpoint> 00020 #include <sstream> 00021 00022 using namespace osgEarth::Util; 00023 using namespace osgEarth; 00024 00025 00026 Viewpoint::Viewpoint() : 00027 _is_valid( false ), 00028 _heading_deg(0.0), 00029 _pitch_deg(0.0), 00030 _range(1.0) 00031 { 00032 //NOP 00033 } 00034 00035 Viewpoint::Viewpoint(const osg::Vec3d& focal_point, 00036 double heading_deg, 00037 double pitch_deg, 00038 double range, 00039 const osgEarth::SpatialReference* srs ) : 00040 _focal_point( focal_point ), 00041 _heading_deg( heading_deg ), 00042 _pitch_deg( pitch_deg ), 00043 _range( range ), 00044 _srs( srs ), 00045 _is_valid( true ) 00046 { 00047 //NOP 00048 } 00049 Viewpoint::Viewpoint(double x, double y, double z, 00050 double heading_deg, 00051 double pitch_deg, 00052 double range, 00053 const osgEarth::SpatialReference* srs ) : 00054 _focal_point( x, y, z ), 00055 _heading_deg( heading_deg ), 00056 _pitch_deg( pitch_deg ), 00057 _range( range ), 00058 _srs( srs ), 00059 _is_valid( true ) 00060 { 00061 //NOP 00062 } 00063 00064 Viewpoint::Viewpoint(const std::string& name, 00065 const osg::Vec3d& focal_point, 00066 double heading_deg, 00067 double pitch_deg, 00068 double range, 00069 const osgEarth::SpatialReference* srs ) : 00070 _name( name ), 00071 _focal_point( focal_point ), 00072 _heading_deg( heading_deg ), 00073 _pitch_deg( pitch_deg ), 00074 _range( range ), 00075 _srs( srs ), 00076 _is_valid( true ) 00077 { 00078 //NOP 00079 } 00080 00081 Viewpoint::Viewpoint(const std::string& name, 00082 double x, double y, double z, 00083 double heading_deg, 00084 double pitch_deg, 00085 double range, 00086 const osgEarth::SpatialReference* srs ) : 00087 _name( name ), 00088 _focal_point( x, y, z ), 00089 _heading_deg( heading_deg ), 00090 _pitch_deg( pitch_deg ), 00091 _range( range ), 00092 _srs( srs ), 00093 _is_valid( true ) 00094 { 00095 //NOP 00096 } 00097 00098 Viewpoint::Viewpoint( const Viewpoint& rhs ) : 00099 _name( rhs._name ), 00100 _focal_point( rhs._focal_point ), 00101 _heading_deg( rhs._heading_deg ), 00102 _pitch_deg( rhs._pitch_deg ), 00103 _range( rhs._range ), 00104 _srs( rhs._srs.get() ), 00105 _is_valid( rhs._is_valid ) 00106 { 00107 //NOP 00108 } 00109 00110 Viewpoint::Viewpoint( const Config& conf ) 00111 { 00112 _name = conf.value("name"); 00113 00114 if ( conf.hasValue("x") ) 00115 { 00116 _focal_point.set( 00117 conf.value<double>("x", 0.0), 00118 conf.value<double>("y", 0.0), 00119 conf.value<double>("z", 0.0) ); 00120 } 00121 else if ( conf.hasValue("lat") ) 00122 { 00123 _focal_point.set( 00124 conf.value<double>("long", 0.0), 00125 conf.value<double>("lat", 0.0), 00126 conf.value<double>("height", 0.0) ); 00127 } 00128 00129 _heading_deg = conf.value<double>("heading", 0.0); 00130 _pitch_deg = conf.value<double>("pitch", 0.0); 00131 _range = conf.value<double>("range", 0.0); 00132 _is_valid = _range > 0.0; 00133 00134 //TODO: SRS 00135 } 00136 00137 Config 00138 Viewpoint::getConfig() const 00139 { 00140 Config conf; 00141 00142 if ( _is_valid ) 00143 { 00144 conf.attr("name") = _name; 00145 if ( getSRS() && getSRS()->isGeographic() ) 00146 { 00147 conf.attr("lat") = osgEarth::toString(_focal_point.y()); 00148 conf.attr("long") = osgEarth::toString(_focal_point.x()); 00149 conf.attr("height") = osgEarth::toString(_focal_point.z()); 00150 } 00151 else 00152 { 00153 conf.attr("x") = osgEarth::toString(_focal_point.x()); 00154 conf.attr("y") = osgEarth::toString(_focal_point.y()); 00155 conf.attr("z") = osgEarth::toString(_focal_point.z()); 00156 } 00157 00158 conf.attr("heading") = osgEarth::toString(_heading_deg); 00159 conf.attr("pitch") = osgEarth::toString(_pitch_deg); 00160 conf.attr("range") = osgEarth::toString(_range); 00161 00162 //TODO: SRS 00163 } 00164 00165 return conf; 00166 } 00167 00168 bool 00169 Viewpoint::isValid() const { 00170 return _is_valid; 00171 } 00172 00173 const std::string& 00174 Viewpoint::getName() const { 00175 return _name; 00176 } 00177 00178 void 00179 Viewpoint::setName( const std::string& name ) { 00180 _name = name; 00181 } 00182 00183 const osg::Vec3d& 00184 Viewpoint::getFocalPoint() const { 00185 return _focal_point; 00186 } 00187 00188 void 00189 Viewpoint::setFocalPoint( const osg::Vec3d& value ) { 00190 _focal_point = value; 00191 } 00192 00193 double 00194 Viewpoint::x() const { 00195 return _focal_point.x(); 00196 } 00197 00198 double& 00199 Viewpoint::x() { 00200 return _focal_point.x(); 00201 } 00202 00203 double 00204 Viewpoint::y() const { 00205 return _focal_point.y(); 00206 } 00207 00208 double& 00209 Viewpoint::y() { 00210 return _focal_point.y(); 00211 } 00212 00213 double 00214 Viewpoint::z() const { 00215 return _focal_point.z(); 00216 } 00217 00218 double& 00219 Viewpoint::z() { 00220 return _focal_point.z(); 00221 } 00222 00223 double 00224 Viewpoint::getHeading() const { 00225 return _heading_deg; 00226 } 00227 00228 void 00229 Viewpoint::setHeading( double value ) { 00230 _heading_deg = value; 00231 } 00232 00233 double 00234 Viewpoint::getPitch() const { 00235 return _pitch_deg; 00236 } 00237 00238 void 00239 Viewpoint::setPitch( double value ) { 00240 _pitch_deg = value; 00241 } 00242 00243 double 00244 Viewpoint::getRange() const { 00245 return _range; 00246 } 00247 00248 void 00249 Viewpoint::setRange( double value ) { 00250 _range = value; 00251 } 00252 00253 const SpatialReference* 00254 Viewpoint::getSRS() const { 00255 return _srs.get(); 00256 } 00257 00258 std::string 00259 Viewpoint::toString() const 00260 { 00261 std::stringstream buf; 00262 buf << "x=" << _focal_point.x() 00263 << ", y=" << _focal_point.y() 00264 << ", z=" << _focal_point.z() 00265 << ", h=" << _heading_deg 00266 << ", p=" << _pitch_deg 00267 << ", d=" << _range 00268 ; 00269 std::string str = buf.str(); 00270 return str; 00271 }