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
31 /// Factory from angular values in radians and elevation is 0
32 static SGGeod fromRad(double lon, double lat);
33 /// Factory from angular values in degrees and elevation is 0
34 static SGGeod fromDeg(double lon, double lat);
35 /// Factory from angular values in radians and elevation in ft
36 static SGGeod fromRadFt(double lon, double lat, double elevation);
37 /// Factory from angular values in degrees and elevation in ft
38 static SGGeod fromDegFt(double lon, double lat, double elevation);
39 /// Factory from angular values in radians and elevation in m
40 static SGGeod fromRadM(double lon, double lat, double elevation);
41 /// Factory from angular values in degrees and elevation in m
42 static SGGeod fromDegM(double lon, double lat, double elevation);
43 /// Factory to convert position from a cartesian position assumed to be
44 /// in wgs84 measured in meters
45 /// Note that this conversion is relatively expensive to compute
46 static SGGeod fromCart(const SGVec3<double>& cart);
47 /// Factory to convert position from a geocentric position
48 /// Note that this conversion is relatively expensive to compute
49 static SGGeod fromGeoc(const SGGeoc& geoc);
51 /// Return the geodetic longitude in radians
52 double getLongitudeRad(void) const;
53 /// Set the geodetic longitude from the argument given in radians
54 void setLongitudeRad(double lon);
56 /// Return the geodetic longitude in degrees
57 double getLongitudeDeg(void) const;
58 /// Set the geodetic longitude from the argument given in degrees
59 void setLongitudeDeg(double lon);
61 /// Return the geodetic latitude in radians
62 double getLatitudeRad(void) const;
63 /// Set the geodetic latitude from the argument given in radians
64 void setLatitudeRad(double lat);
66 /// Return the geodetic latitude in degrees
67 double getLatitudeDeg(void) const;
68 /// Set the geodetic latitude from the argument given in degrees
69 void setLatitudeDeg(double lat);
71 /// Return the geodetic elevation in meters
72 double getElevationM(void) const;
73 /// Set the geodetic elevation from the argument given in meters
74 void setElevationM(double elevation);
76 /// Return the geodetic elevation in feet
77 double getElevationFt(void) const;
78 /// Set the geodetic elevation from the argument given in feet
79 void setElevationFt(double elevation);
82 /// This one is private since construction is not unique if you do
83 /// not know the units of the arguments. Use the factory methods for
85 SGGeod(double lon, double lat, double elevation);
87 /// The actual data, angles in degree, elevation in meters
88 /// The rationale for storing the values in degrees is that most code places
89 /// in flightgear/terragear use degrees as a nativ input and output value.
90 /// The places where it makes sense to use radians is when we convert
91 /// to other representations or compute rotation matrices. But both tasks
92 /// are computionally intensive anyway and that additional 'toRadian'
93 /// conversion does not hurt too much
100 SGGeod::SGGeod(void) :
101 _lon(0), _lat(0), _elevation(0)
106 SGGeod::SGGeod(double lon, double lat, double elevation) :
107 _lon(lon), _lat(lat), _elevation(elevation)
113 SGGeod::fromRad(double lon, double lat)
115 #ifdef SG_GEOD_NATIVE_DEGREE
116 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES, 0);
118 return SGGeod(lon, lat, 0);
124 SGGeod::fromDeg(double lon, double lat)
126 #ifdef SG_GEOD_NATIVE_DEGREE
127 return SGGeod(lon, lat, 0);
129 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0);
135 SGGeod::fromRadFt(double lon, double lat, double elevation)
137 #ifdef SG_GEOD_NATIVE_DEGREE
138 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
139 elevation*SG_FEET_TO_METER);
141 return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
147 SGGeod::fromDegFt(double lon, double lat, double elevation)
149 #ifdef SG_GEOD_NATIVE_DEGREE
150 return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
152 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
153 elevation*SG_FEET_TO_METER);
159 SGGeod::fromRadM(double lon, double lat, double elevation)
161 #ifdef SG_GEOD_NATIVE_DEGREE
162 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
165 return SGGeod(lon, lat, elevation);
171 SGGeod::fromDegM(double lon, double lat, double elevation)
173 #ifdef SG_GEOD_NATIVE_DEGREE
174 return SGGeod(lon, lat, elevation);
176 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
183 SGGeod::fromCart(const SGVec3<double>& cart)
186 SGGeodesy::SGCartToGeod(cart, geod);
192 SGGeod::fromGeoc(const SGGeoc& geoc)
195 SGGeodesy::SGGeocToCart(geoc, cart);
197 SGGeodesy::SGCartToGeod(cart, geod);
203 SGGeod::getLongitudeRad(void) const
205 #ifdef SG_GEOD_NATIVE_DEGREE
206 return _lon*SGD_DEGREES_TO_RADIANS;
214 SGGeod::setLongitudeRad(double lon)
216 #ifdef SG_GEOD_NATIVE_DEGREE
217 _lon = lon*SGD_RADIANS_TO_DEGREES;
225 SGGeod::getLongitudeDeg(void) const
227 #ifdef SG_GEOD_NATIVE_DEGREE
230 return _lon*SGD_RADIANS_TO_DEGREES;
236 SGGeod::setLongitudeDeg(double lon)
238 #ifdef SG_GEOD_NATIVE_DEGREE
241 _lon = lon*SGD_DEGREES_TO_RADIANS;
247 SGGeod::getLatitudeRad(void) const
249 #ifdef SG_GEOD_NATIVE_DEGREE
250 return _lat*SGD_DEGREES_TO_RADIANS;
258 SGGeod::setLatitudeRad(double lat)
260 #ifdef SG_GEOD_NATIVE_DEGREE
261 _lat = lat*SGD_RADIANS_TO_DEGREES;
269 SGGeod::getLatitudeDeg(void) const
271 #ifdef SG_GEOD_NATIVE_DEGREE
274 return _lat*SGD_RADIANS_TO_DEGREES;
280 SGGeod::setLatitudeDeg(double lat)
282 #ifdef SG_GEOD_NATIVE_DEGREE
285 _lat = lat*SGD_DEGREES_TO_RADIANS;
291 SGGeod::getElevationM(void) const
298 SGGeod::setElevationM(double elevation)
300 _elevation = elevation;
305 SGGeod::getElevationFt(void) const
307 return _elevation*SG_METER_TO_FEET;
312 SGGeod::setElevationFt(double elevation)
314 _elevation = elevation*SG_FEET_TO_METER;
317 /// Output to an ostream
318 template<typename char_type, typename traits_type>
320 std::basic_ostream<char_type, traits_type>&
321 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeod& g)
323 return s << "lon = " << g.getLongitudeDeg()
324 << "deg, lat = " << g.getLatitudeDeg()
325 << "deg, elev = " << g.getElevationM()