1 // Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU Library General Public
14 // License along with this library; if not, write to the
15 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 // Boston, MA 02111-1307, USA.
22 #include <simgear/constants.h>
24 // #define SG_GEOC_NATIVE_DEGREE
26 /// Class representing a geocentric location
29 /// Default constructor, initializes the instance to lat = lon = lat = 0
31 // You should have received a copy of the GNU General Public License
32 // along with this program; if not, write to the Free Software
33 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 /// Note that this conversion is relatively expensive to compute
35 SGGeoc(const SGGeod& geod);
37 SGGeoc(const SGVec3<double>& cart);
39 /// Factory from angular values in radians and radius in ft
40 static SGGeoc fromRadFt(double lon, double lat, double radius);
41 /// Factory from angular values in degrees and radius in ft
42 static SGGeoc fromDegFt(double lon, double lat, double radius);
43 /// Factory from angular values in radians and radius in m
44 static SGGeoc fromRadM(double lon, double lat, double radius);
45 /// Factory from angular values in degrees and radius in m
46 static SGGeoc fromDegM(double lon, double lat, double radius);
48 /// Return the geocentric longitude in radians
49 double getLongitudeRad(void) const;
50 /// Set the geocentric longitude from the argument given in radians
51 void setLongitudeRad(double lon);
53 /// Return the geocentric longitude in degrees
54 double getLongitudeDeg(void) const;
55 /// Set the geocentric longitude from the argument given in degrees
56 void setLongitudeDeg(double lon);
58 /// Return the geocentric latitude in radians
59 double getLatitudeRad(void) const;
60 /// Set the geocentric latitude from the argument given in radians
61 void setLatitudeRad(double lat);
63 /// Return the geocentric latitude in degrees
64 double getLatitudeDeg(void) const;
65 /// Set the geocentric latitude from the argument given in degrees
66 void setLatitudeDeg(double lat);
68 /// Return the geocentric radius in meters
69 double getRadiusM(void) const;
70 /// Set the geocentric radius from the argument given in meters
71 void setRadiusM(double radius);
73 /// Return the geocentric radius in feet
74 double getRadiusFt(void) const;
75 /// Set the geocentric radius from the argument given in feet
76 void setRadiusFt(double radius);
79 /// This one is private since construction is not unique if you do
80 /// not know the units of the arguments, use the factory methods for
82 SGGeoc(double lon, double lat, double radius);
84 /// The actual data, angles in degree, radius in meters
85 /// The rationale for storing the values in degrees is that most code places
86 /// in flightgear/terragear use degrees as a nativ input and output value.
87 /// The places where it makes sense to use radians is when we convert
88 /// to other representations or compute rotation matrices. But both tasks
89 /// are computionally intensive anyway and that additional 'toRadian'
90 /// conversion does not hurt too much
97 SGGeoc::SGGeoc(void) :
98 _lon(0), _lat(0), _radius(0)
103 SGGeoc::SGGeoc(double lon, double lat, double radius) :
104 _lon(lon), _lat(lat), _radius(radius)
109 SGGeoc::SGGeoc(const SGVec3<double>& cart)
111 SGGeodesy::SGCartToGeoc(cart, *this);
115 SGGeoc::SGGeoc(const SGGeod& geod)
118 SGGeodesy::SGGeodToCart(geod, cart);
119 SGGeodesy::SGCartToGeoc(cart, *this);
124 SGGeoc::fromRadFt(double lon, double lat, double radius)
126 #ifdef SG_GEOC_NATIVE_DEGREE
127 return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
128 radius*SG_FEET_TO_METER);
130 return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
136 SGGeoc::fromDegFt(double lon, double lat, double radius)
138 #ifdef SG_GEOC_NATIVE_DEGREE
139 return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
141 return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
142 radius*SG_FEET_TO_METER);
148 SGGeoc::fromRadM(double lon, double lat, double radius)
150 #ifdef SG_GEOC_NATIVE_DEGREE
151 return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
154 return SGGeoc(lon, lat, radius);
160 SGGeoc::fromDegM(double lon, double lat, double radius)
162 #ifdef SG_GEOC_NATIVE_DEGREE
163 return SGGeoc(lon, lat, radius);
165 return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
172 SGGeoc::getLongitudeRad(void) const
174 #ifdef SG_GEOC_NATIVE_DEGREE
175 return _lon*SGD_DEGREES_TO_RADIANS;
183 SGGeoc::setLongitudeRad(double lon)
185 #ifdef SG_GEOC_NATIVE_DEGREE
186 _lon = lon*SGD_RADIANS_TO_DEGREES;
194 SGGeoc::getLongitudeDeg(void) const
196 #ifdef SG_GEOC_NATIVE_DEGREE
199 return _lon*SGD_DEGREES_TO_RADIANS;
205 SGGeoc::setLongitudeDeg(double lon)
207 #ifdef SG_GEOC_NATIVE_DEGREE
210 _lon = lon*SGD_RADIANS_TO_DEGREES;
216 SGGeoc::getLatitudeRad(void) const
218 #ifdef SG_GEOC_NATIVE_DEGREE
219 return _lat*SGD_DEGREES_TO_RADIANS;
227 SGGeoc::setLatitudeRad(double lat)
229 #ifdef SG_GEOC_NATIVE_DEGREE
230 _lat = lat*SGD_RADIANS_TO_DEGREES;
238 SGGeoc::getLatitudeDeg(void) const
240 #ifdef SG_GEOC_NATIVE_DEGREE
243 return _lat*SGD_DEGREES_TO_RADIANS;
249 SGGeoc::setLatitudeDeg(double lat)
251 #ifdef SG_GEOC_NATIVE_DEGREE
254 _lat = lat*SGD_RADIANS_TO_DEGREES;
260 SGGeoc::getRadiusM(void) const
267 SGGeoc::setRadiusM(double radius)
274 SGGeoc::getRadiusFt(void) const
276 return _radius*SG_METER_TO_FEET;
281 SGGeoc::setRadiusFt(double radius)
283 _radius = radius*SG_FEET_TO_METER;
286 /// Output to an ostream
287 template<typename char_type, typename traits_type>
289 std::basic_ostream<char_type, traits_type>&
290 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeoc& g)
292 return s << "lon = " << g.getLongitudeDeg()
293 << ", lat = " << g.getLatitudeDeg()
294 << ", radius = " << g.getRadiusM();