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_GEOD_NATIVE_DEGREE
26 /// Class representing a geodetic location
29 /// Default constructor, initializes the instance to lat = lon = elev = 0
31 /// Initialize from a cartesian vector assumed to be in meters
32 /// 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
36 SGGeod(const SGGeoc& geoc);
38 /// Factory from angular values in radians and elevation is 0
39 static SGGeod fromRad(double lon, double lat);
40 /// Factory from angular values in degrees and elevation is 0
41 static SGGeod fromDeg(double lon, double lat);
42 /// Factory from angular values in radians and elevation in ft
43 static SGGeod fromRadFt(double lon, double lat, double elevation);
44 /// Factory from angular values in degrees and elevation in ft
45 static SGGeod fromDegFt(double lon, double lat, double elevation);
46 /// Factory from angular values in radians and elevation in m
47 static SGGeod fromRadM(double lon, double lat, double elevation);
48 /// Factory from angular values in degrees and elevation in m
49 static SGGeod fromDegM(double lon, double lat, double elevation);
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)
112 SGGeod::SGGeod(const SGVec3<double>& cart)
114 SGGeodesy::SGCartToGeod(cart, *this);
118 SGGeod::SGGeod(const SGGeoc& geoc)
121 SGGeodesy::SGGeocToCart(geoc, cart);
122 SGGeodesy::SGCartToGeod(cart, *this);
127 SGGeod::fromRad(double lon, double lat)
129 #ifdef SG_GEOD_NATIVE_DEGREE
130 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES, 0);
132 return SGGeod(lon, lat, 0);
138 SGGeod::fromDeg(double lon, double lat)
140 #ifdef SG_GEOD_NATIVE_DEGREE
141 return SGGeod(lon, lat, 0);
143 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0);
149 SGGeod::fromRadFt(double lon, double lat, double elevation)
151 #ifdef SG_GEOD_NATIVE_DEGREE
152 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
153 elevation*SG_FEET_TO_METER);
155 return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
161 SGGeod::fromDegFt(double lon, double lat, double elevation)
163 #ifdef SG_GEOD_NATIVE_DEGREE
164 return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
166 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
167 elevation*SG_FEET_TO_METER);
173 SGGeod::fromRadM(double lon, double lat, double elevation)
175 #ifdef SG_GEOD_NATIVE_DEGREE
176 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
179 return SGGeod(lon, lat, elevation);
185 SGGeod::fromDegM(double lon, double lat, double elevation)
187 #ifdef SG_GEOD_NATIVE_DEGREE
188 return SGGeod(lon, lat, elevation);
190 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
197 SGGeod::getLongitudeRad(void) const
199 #ifdef SG_GEOD_NATIVE_DEGREE
200 return _lon*SGD_DEGREES_TO_RADIANS;
208 SGGeod::setLongitudeRad(double lon)
210 #ifdef SG_GEOD_NATIVE_DEGREE
211 _lon = lon*SGD_RADIANS_TO_DEGREES;
219 SGGeod::getLongitudeDeg(void) const
221 #ifdef SG_GEOD_NATIVE_DEGREE
224 return _lon*SGD_RADIANS_TO_DEGREES;
230 SGGeod::setLongitudeDeg(double lon)
232 #ifdef SG_GEOD_NATIVE_DEGREE
235 _lon = lon*SGD_DEGREES_TO_RADIANS;
241 SGGeod::getLatitudeRad(void) const
243 #ifdef SG_GEOD_NATIVE_DEGREE
244 return _lat*SGD_DEGREES_TO_RADIANS;
252 SGGeod::setLatitudeRad(double lat)
254 #ifdef SG_GEOD_NATIVE_DEGREE
255 _lat = lat*SGD_RADIANS_TO_DEGREES;
263 SGGeod::getLatitudeDeg(void) const
265 #ifdef SG_GEOD_NATIVE_DEGREE
268 return _lat*SGD_RADIANS_TO_DEGREES;
274 SGGeod::setLatitudeDeg(double lat)
276 #ifdef SG_GEOD_NATIVE_DEGREE
279 _lat = lat*SGD_DEGREES_TO_RADIANS;
285 SGGeod::getElevationM(void) const
292 SGGeod::setElevationM(double elevation)
294 _elevation = elevation;
299 SGGeod::getElevationFt(void) const
301 return _elevation*SG_METER_TO_FEET;
306 SGGeod::setElevationFt(double elevation)
308 _elevation = elevation*SG_FEET_TO_METER;
311 /// Output to an ostream
312 template<typename char_type, typename traits_type>
314 std::basic_ostream<char_type, traits_type>&
315 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeod& g)
317 return s << "lon = " << g.getLongitudeDeg()
318 << "deg, lat = " << g.getLatitudeDeg()
319 << "deg, elev = " << g.getElevationM()