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 // 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 SGGeod(const SGGeoc& geoc);
37 SGGeod(const SGVec3<double>& cart);
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);
52 /// Return the geodetic longitude in radians
53 double getLongitudeRad(void) const;
54 /// Set the geodetic longitude from the argument given in radians
55 void setLongitudeRad(double lon);
57 /// Return the geodetic longitude in degrees
58 double getLongitudeDeg(void) const;
59 /// Set the geodetic longitude from the argument given in degrees
60 void setLongitudeDeg(double lon);
62 /// Return the geodetic latitude in radians
63 double getLatitudeRad(void) const;
64 /// Set the geodetic latitude from the argument given in radians
65 void setLatitudeRad(double lat);
67 /// Return the geodetic latitude in degrees
68 double getLatitudeDeg(void) const;
69 /// Set the geodetic latitude from the argument given in degrees
70 void setLatitudeDeg(double lat);
72 /// Return the geodetic elevation in meters
73 double getElevationM(void) const;
74 /// Set the geodetic elevation from the argument given in meters
75 void setElevationM(double elevation);
77 /// Return the geodetic elevation in feet
78 double getElevationFt(void) const;
79 /// Set the geodetic elevation from the argument given in feet
80 void setElevationFt(double elevation);
83 /// This one is private since construction is not unique if you do
84 /// not know the units of the arguments. Use the factory methods for
86 SGGeod(double lon, double lat, double elevation);
88 /// The actual data, angles in degree, elevation in meters
89 /// The rationale for storing the values in degrees is that most code places
90 /// in flightgear/terragear use degrees as a nativ input and output value.
91 /// The places where it makes sense to use radians is when we convert
92 /// to other representations or compute rotation matrices. But both tasks
93 /// are computionally intensive anyway and that additional 'toRadian'
94 /// conversion does not hurt too much
101 SGGeod::SGGeod(void) :
102 _lon(0), _lat(0), _elevation(0)
107 SGGeod::SGGeod(double lon, double lat, double elevation) :
108 _lon(lon), _lat(lat), _elevation(elevation)
113 SGGeod::SGGeod(const SGVec3<double>& cart)
115 SGGeodesy::SGCartToGeod(cart, *this);
119 SGGeod::SGGeod(const SGGeoc& geoc)
122 SGGeodesy::SGGeocToCart(geoc, cart);
123 SGGeodesy::SGCartToGeod(cart, *this);
128 SGGeod::fromRad(double lon, double lat)
130 #ifdef SG_GEOD_NATIVE_DEGREE
131 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES, 0);
133 return SGGeod(lon, lat, 0);
139 SGGeod::fromDeg(double lon, double lat)
141 #ifdef SG_GEOD_NATIVE_DEGREE
142 return SGGeod(lon, lat, 0);
144 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0);
150 SGGeod::fromRadFt(double lon, double lat, double elevation)
152 #ifdef SG_GEOD_NATIVE_DEGREE
153 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
154 elevation*SG_FEET_TO_METER);
156 return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
162 SGGeod::fromDegFt(double lon, double lat, double elevation)
164 #ifdef SG_GEOD_NATIVE_DEGREE
165 return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
167 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
168 elevation*SG_FEET_TO_METER);
174 SGGeod::fromRadM(double lon, double lat, double elevation)
176 #ifdef SG_GEOD_NATIVE_DEGREE
177 return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
180 return SGGeod(lon, lat, elevation);
186 SGGeod::fromDegM(double lon, double lat, double elevation)
188 #ifdef SG_GEOD_NATIVE_DEGREE
189 return SGGeod(lon, lat, elevation);
191 return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
198 SGGeod::getLongitudeRad(void) const
200 #ifdef SG_GEOD_NATIVE_DEGREE
201 return _lon*SGD_DEGREES_TO_RADIANS;
209 SGGeod::setLongitudeRad(double lon)
211 #ifdef SG_GEOD_NATIVE_DEGREE
212 _lon = lon*SGD_RADIANS_TO_DEGREES;
220 SGGeod::getLongitudeDeg(void) const
222 #ifdef SG_GEOD_NATIVE_DEGREE
225 return _lon*SGD_RADIANS_TO_DEGREES;
231 SGGeod::setLongitudeDeg(double lon)
233 #ifdef SG_GEOD_NATIVE_DEGREE
236 _lon = lon*SGD_DEGREES_TO_RADIANS;
242 SGGeod::getLatitudeRad(void) const
244 #ifdef SG_GEOD_NATIVE_DEGREE
245 return _lat*SGD_DEGREES_TO_RADIANS;
253 SGGeod::setLatitudeRad(double lat)
255 #ifdef SG_GEOD_NATIVE_DEGREE
256 _lat = lat*SGD_RADIANS_TO_DEGREES;
264 SGGeod::getLatitudeDeg(void) const
266 #ifdef SG_GEOD_NATIVE_DEGREE
269 return _lat*SGD_RADIANS_TO_DEGREES;
275 SGGeod::setLatitudeDeg(double lat)
277 #ifdef SG_GEOD_NATIVE_DEGREE
280 _lat = lat*SGD_DEGREES_TO_RADIANS;
286 SGGeod::getElevationM(void) const
293 SGGeod::setElevationM(double elevation)
295 _elevation = elevation;
300 SGGeod::getElevationFt(void) const
302 return _elevation*SG_METER_TO_FEET;
307 SGGeod::setElevationFt(double elevation)
309 _elevation = elevation*SG_FEET_TO_METER;
312 /// Output to an ostream
313 template<typename char_type, typename traits_type>
315 std::basic_ostream<char_type, traits_type>&
316 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeod& g)
318 return s << "lon = " << g.getLongitudeDeg()
319 << "deg, lat = " << g.getLatitudeDeg()
320 << "deg, elev = " << g.getElevationM()