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
32 SGGeod(const SGVec3<double>& cart);
33 /// Initialize from a geocentric position
34 /// Note that this conversion is relatively expensive to compute
35 SGGeod(const SGGeoc& geoc);
37 /// Factory from angular values in radians and elevation is 0
38 static SGGeod fromRad(double lon, double lat);
39 /// Factory from angular values in degrees and elevation is 0
40 static SGGeod fromDeg(double lon, double lat);
41 /// Factory from angular values in radians and elevation in ft
42 static SGGeod fromRadFt(double lon, double lat, double elevation);
43 /// Factory from angular values in degrees and elevation in ft
44 static SGGeod fromDegFt(double lon, double lat, double elevation);
45 /// Factory from angular values in radians and elevation in m
46 static SGGeod fromRadM(double lon, double lat, double elevation);
47 /// Factory from angular values in degrees and elevation in m
48 static SGGeod fromDegM(double lon, double lat, double elevation);
50 /// Return the geodetic longitude in radians
51 double getLongitudeRad(void) const;
52 /// Set the geodetic longitude from the argument given in radians
53 void setLongitudeRad(double lon);
55 /// Return the geodetic longitude in degrees
56 double getLongitudeDeg(void) const;
57 /// Set the geodetic longitude from the argument given in degrees
58 void setLongitudeDeg(double lon);
60 /// Return the geodetic latitude in radians
61 double getLatitudeRad(void) const;
62 /// Set the geodetic latitude from the argument given in radians
63 void setLatitudeRad(double lat);
65 /// Return the geodetic latitude in degrees
66 double getLatitudeDeg(void) const;
67 /// Set the geodetic latitude from the argument given in degrees
68 void setLatitudeDeg(double lat);
70 /// Return the geodetic elevation in meters
71 double getElevationM(void) const;
72 /// Set the geodetic elevation from the argument given in meters
73 void setElevationM(double elevation);
75 /// Return the geodetic elevation in feet
76 double getElevationFt(void) const;
77 /// Set the geodetic elevation from the argument given in feet
78 void setElevationFt(double elevation);
81 /// This one is private since construction is not unique if you do
82 /// not know the units of the arguments. Use the factory methods for
84 SGGeod(double lon, double lat, double elevation);
86 /// The actual data, angles in degree, elevation in meters
87 /// The rationale for storing the values in degrees is that most code places
88 /// in flightgear/terragear use degrees as a nativ input and output value.
89 /// The places where it makes sense to use radians is when we convert
90 /// to other representations or compute rotation matrices. But both tasks
91 /// are computionally intensive anyway and that additional 'toRadian'
92 /// conversion does not hurt too much
99 SGGeod::SGGeod(void) :
100 _lon(0), _lat(0), _elevation(0)
105 SGGeod::SGGeod(double lon, double lat, double elevation) :
106 _lon(lon), _lat(lat), _elevation(elevation)
111 SGGeod::SGGeod(const SGVec3<double>& cart)
113 SGGeodesy::SGCartToGeod(cart, *this);
117 SGGeod::SGGeod(const SGGeoc& geoc)
120 SGGeodesy::SGGeocToCart(geoc, cart);
121 SGGeodesy::SGCartToGeod(cart, *this);
126 SGGeod::fromRad(double lon, double lat)
128 #ifdef SG_GEOD_NATIVE_DEGREE
129 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES, 0);
131 return SGGeod(lon, lat, 0);
137 SGGeod::fromDeg(double lon, double lat)
139 #ifdef SG_GEOD_NATIVE_DEGREE
140 return SGGeod(lon, lat, 0);
142 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0);
148 SGGeod::fromRadFt(double lon, double lat, double elevation)
150 #ifdef SG_GEOD_NATIVE_DEGREE
151 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
152 elevation*SG_FEET_TO_METER);
154 return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
160 SGGeod::fromDegFt(double lon, double lat, double elevation)
162 #ifdef SG_GEOD_NATIVE_DEGREE
163 return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
165 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
166 elevation*SG_FEET_TO_METER);
172 SGGeod::fromRadM(double lon, double lat, double elevation)
174 #ifdef SG_GEOD_NATIVE_DEGREE
175 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
178 return SGGeod(lon, lat, elevation);
184 SGGeod::fromDegM(double lon, double lat, double elevation)
186 #ifdef SG_GEOD_NATIVE_DEGREE
187 return SGGeod(lon, lat, elevation);
189 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
196 SGGeod::getLongitudeRad(void) const
198 #ifdef SG_GEOD_NATIVE_DEGREE
199 return _lon*SGD_DEGREES_TO_RADIANS;
207 SGGeod::setLongitudeRad(double lon)
209 #ifdef SG_GEOD_NATIVE_DEGREE
210 _lon = lon*SGD_RADIANS_TO_DEGREES;
218 SGGeod::getLongitudeDeg(void) const
220 #ifdef SG_GEOD_NATIVE_DEGREE
223 return _lon*SGD_RADIANS_TO_DEGREES;
229 SGGeod::setLongitudeDeg(double lon)
231 #ifdef SG_GEOD_NATIVE_DEGREE
234 _lon = lon*SGD_DEGREES_TO_RADIANS;
240 SGGeod::getLatitudeRad(void) const
242 #ifdef SG_GEOD_NATIVE_DEGREE
243 return _lat*SGD_DEGREES_TO_RADIANS;
251 SGGeod::setLatitudeRad(double lat)
253 #ifdef SG_GEOD_NATIVE_DEGREE
254 _lat = lat*SGD_RADIANS_TO_DEGREES;
262 SGGeod::getLatitudeDeg(void) const
264 #ifdef SG_GEOD_NATIVE_DEGREE
267 return _lat*SGD_RADIANS_TO_DEGREES;
273 SGGeod::setLatitudeDeg(double lat)
275 #ifdef SG_GEOD_NATIVE_DEGREE
278 _lat = lat*SGD_DEGREES_TO_RADIANS;
284 SGGeod::getElevationM(void) const
291 SGGeod::setElevationM(double elevation)
293 _elevation = elevation;
298 SGGeod::getElevationFt(void) const
300 return _elevation*SG_METER_TO_FEET;
305 SGGeod::setElevationFt(double elevation)
307 _elevation = elevation*SG_FEET_TO_METER;
310 /// Output to an ostream
311 template<typename char_type, typename traits_type>
313 std::basic_ostream<char_type, traits_type>&
314 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeod& g)
316 return s << "lon = " << g.getLongitudeDeg()
317 << "deg, lat = " << g.getLatitudeDeg()
318 << "deg, elev = " << g.getElevationM()