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 General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <simgear/constants.h>
23 // #define SG_GEOD_NATIVE_DEGREE
25 /// Class representing a geodetic location
28 /// Default constructor, initializes the instance to lat = lon = elev = 0
30 /// Initialize from a cartesian vector assumed to be in meters
31 /// Note that this conversion is relatively expensive to compute
33 SGGeod(const SGVec3<double>& cart);
34 /// Initialize from a geocentric position
35 /// Note that this conversion is relatively expensive to compute
37 SGGeod(const SGGeoc& geoc);
39 /// Factory from angular values in radians and elevation is 0
40 static SGGeod fromRad(double lon, double lat);
41 /// Factory from angular values in degrees and elevation is 0
42 static SGGeod fromDeg(double lon, double lat);
43 /// Factory from angular values in radians and elevation in ft
44 static SGGeod fromRadFt(double lon, double lat, double elevation);
45 /// Factory from angular values in degrees and elevation in ft
46 static SGGeod fromDegFt(double lon, double lat, double elevation);
47 /// Factory from angular values in radians and elevation in m
48 static SGGeod fromRadM(double lon, double lat, double elevation);
49 /// Factory from angular values in degrees and elevation in m
50 static SGGeod fromDegM(double lon, double lat, double elevation);
51 /// Factory to convert position from a cartesian position assumed to be
52 /// in wgs84 measured in meters
53 /// Note that this conversion is relatively expensive to compute
54 static SGGeod fromCart(const SGVec3<double>& cart);
55 /// Factory to convert position from a geocentric position
56 /// Note that this conversion is relatively expensive to compute
57 static SGGeod fromGeoc(const SGGeoc& geoc);
59 /// Return the geodetic longitude in radians
60 double getLongitudeRad(void) const;
61 /// Set the geodetic longitude from the argument given in radians
62 void setLongitudeRad(double lon);
64 /// Return the geodetic longitude in degrees
65 double getLongitudeDeg(void) const;
66 /// Set the geodetic longitude from the argument given in degrees
67 void setLongitudeDeg(double lon);
69 /// Return the geodetic latitude in radians
70 double getLatitudeRad(void) const;
71 /// Set the geodetic latitude from the argument given in radians
72 void setLatitudeRad(double lat);
74 /// Return the geodetic latitude in degrees
75 double getLatitudeDeg(void) const;
76 /// Set the geodetic latitude from the argument given in degrees
77 void setLatitudeDeg(double lat);
79 /// Return the geodetic elevation in meters
80 double getElevationM(void) const;
81 /// Set the geodetic elevation from the argument given in meters
82 void setElevationM(double elevation);
84 /// Return the geodetic elevation in feet
85 double getElevationFt(void) const;
86 /// Set the geodetic elevation from the argument given in feet
87 void setElevationFt(double elevation);
90 /// This one is private since construction is not unique if you do
91 /// not know the units of the arguments. Use the factory methods for
93 SGGeod(double lon, double lat, double elevation);
95 /// The actual data, angles in degree, elevation in meters
96 /// The rationale for storing the values in degrees is that most code places
97 /// in flightgear/terragear use degrees as a nativ input and output value.
98 /// The places where it makes sense to use radians is when we convert
99 /// to other representations or compute rotation matrices. But both tasks
100 /// are computionally intensive anyway and that additional 'toRadian'
101 /// conversion does not hurt too much
108 SGGeod::SGGeod(void) :
109 _lon(0), _lat(0), _elevation(0)
114 SGGeod::SGGeod(double lon, double lat, double elevation) :
115 _lon(lon), _lat(lat), _elevation(elevation)
120 SGGeod::SGGeod(const SGVec3<double>& cart)
122 SGGeodesy::SGCartToGeod(cart, *this);
126 SGGeod::SGGeod(const SGGeoc& geoc)
129 SGGeodesy::SGGeocToCart(geoc, cart);
130 SGGeodesy::SGCartToGeod(cart, *this);
135 SGGeod::fromRad(double lon, double lat)
137 #ifdef SG_GEOD_NATIVE_DEGREE
138 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES, 0);
140 return SGGeod(lon, lat, 0);
146 SGGeod::fromDeg(double lon, double lat)
148 #ifdef SG_GEOD_NATIVE_DEGREE
149 return SGGeod(lon, lat, 0);
151 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0);
157 SGGeod::fromRadFt(double lon, double lat, double elevation)
159 #ifdef SG_GEOD_NATIVE_DEGREE
160 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
161 elevation*SG_FEET_TO_METER);
163 return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
169 SGGeod::fromDegFt(double lon, double lat, double elevation)
171 #ifdef SG_GEOD_NATIVE_DEGREE
172 return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
174 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
175 elevation*SG_FEET_TO_METER);
181 SGGeod::fromRadM(double lon, double lat, double elevation)
183 #ifdef SG_GEOD_NATIVE_DEGREE
184 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
187 return SGGeod(lon, lat, elevation);
193 SGGeod::fromDegM(double lon, double lat, double elevation)
195 #ifdef SG_GEOD_NATIVE_DEGREE
196 return SGGeod(lon, lat, elevation);
198 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
205 SGGeod::fromCart(const SGVec3<double>& cart)
208 SGGeodesy::SGCartToGeod(cart, geod);
214 SGGeod::fromGeoc(const SGGeoc& geoc)
217 SGGeodesy::SGGeocToCart(geoc, cart);
219 SGGeodesy::SGCartToGeod(cart, geod);
225 SGGeod::getLongitudeRad(void) const
227 #ifdef SG_GEOD_NATIVE_DEGREE
228 return _lon*SGD_DEGREES_TO_RADIANS;
236 SGGeod::setLongitudeRad(double lon)
238 #ifdef SG_GEOD_NATIVE_DEGREE
239 _lon = lon*SGD_RADIANS_TO_DEGREES;
247 SGGeod::getLongitudeDeg(void) const
249 #ifdef SG_GEOD_NATIVE_DEGREE
252 return _lon*SGD_RADIANS_TO_DEGREES;
258 SGGeod::setLongitudeDeg(double lon)
260 #ifdef SG_GEOD_NATIVE_DEGREE
263 _lon = lon*SGD_DEGREES_TO_RADIANS;
269 SGGeod::getLatitudeRad(void) const
271 #ifdef SG_GEOD_NATIVE_DEGREE
272 return _lat*SGD_DEGREES_TO_RADIANS;
280 SGGeod::setLatitudeRad(double lat)
282 #ifdef SG_GEOD_NATIVE_DEGREE
283 _lat = lat*SGD_RADIANS_TO_DEGREES;
291 SGGeod::getLatitudeDeg(void) const
293 #ifdef SG_GEOD_NATIVE_DEGREE
296 return _lat*SGD_RADIANS_TO_DEGREES;
302 SGGeod::setLatitudeDeg(double lat)
304 #ifdef SG_GEOD_NATIVE_DEGREE
307 _lat = lat*SGD_DEGREES_TO_RADIANS;
313 SGGeod::getElevationM(void) const
320 SGGeod::setElevationM(double elevation)
322 _elevation = elevation;
327 SGGeod::getElevationFt(void) const
329 return _elevation*SG_METER_TO_FEET;
334 SGGeod::setElevationFt(double elevation)
336 _elevation = elevation*SG_FEET_TO_METER;
339 /// Output to an ostream
340 template<typename char_type, typename traits_type>
342 std::basic_ostream<char_type, traits_type>&
343 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeod& g)
345 return s << "lon = " << g.getLongitudeDeg()
346 << "deg, lat = " << g.getLatitudeDeg()
347 << "deg, elev = " << g.getElevationM()