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_GEOC_NATIVE_DEGREE
25 /// Class representing a geocentric location
28 /// Default constructor, initializes the instance to lat = lon = lat = 0
30 /// Initialize from a cartesian vector assumed to be in meters
31 /// Note that this conversion is relatively expensive to compute
33 SGGeoc(const SGVec3<double>& cart);
34 /// Initialize from a geodetic position
35 /// Note that this conversion is relatively expensive to compute
37 SGGeoc(const SGGeod& geod);
39 /// Factory from angular values in radians and radius in ft
40 static SGGeoc fromRadFt(double lon, double lat, double radius);
41 /// Factory from angular values in degrees and radius in ft
42 static SGGeoc fromDegFt(double lon, double lat, double radius);
43 /// Factory from angular values in radians and radius in m
44 static SGGeoc fromRadM(double lon, double lat, double radius);
45 /// Factory from angular values in degrees and radius in m
46 static SGGeoc fromDegM(double lon, double lat, double radius);
47 /// Factory to convert position from a cartesian position assumed to be
48 /// in wgs84 measured in meters
49 /// Note that this conversion is relatively expensive to compute
50 static SGGeoc fromCart(const SGVec3<double>& cart);
51 /// Factory to convert position from a geodetic position
52 /// Note that this conversion is relatively expensive to compute
53 static SGGeoc fromGeod(const SGGeod& geod);
55 /// Return the geocentric longitude in radians
56 double getLongitudeRad(void) const;
57 /// Set the geocentric longitude from the argument given in radians
58 void setLongitudeRad(double lon);
60 /// Return the geocentric longitude in degrees
61 double getLongitudeDeg(void) const;
62 /// Set the geocentric longitude from the argument given in degrees
63 void setLongitudeDeg(double lon);
65 /// Return the geocentric latitude in radians
66 double getLatitudeRad(void) const;
67 /// Set the geocentric latitude from the argument given in radians
68 void setLatitudeRad(double lat);
70 /// Return the geocentric latitude in degrees
71 double getLatitudeDeg(void) const;
72 /// Set the geocentric latitude from the argument given in degrees
73 void setLatitudeDeg(double lat);
75 /// Return the geocentric radius in meters
76 double getRadiusM(void) const;
77 /// Set the geocentric radius from the argument given in meters
78 void setRadiusM(double radius);
80 /// Return the geocentric radius in feet
81 double getRadiusFt(void) const;
82 /// Set the geocentric radius from the argument given in feet
83 void setRadiusFt(double radius);
86 /// This one is private since construction is not unique if you do
87 /// not know the units of the arguments, use the factory methods for
89 SGGeoc(double lon, double lat, double radius);
91 /// The actual data, angles in degree, radius in meters
92 /// The rationale for storing the values in degrees is that most code places
93 /// in flightgear/terragear use degrees as a nativ input and output value.
94 /// The places where it makes sense to use radians is when we convert
95 /// to other representations or compute rotation matrices. But both tasks
96 /// are computionally intensive anyway and that additional 'toRadian'
97 /// conversion does not hurt too much
104 SGGeoc::SGGeoc(void) :
105 _lon(0), _lat(0), _radius(0)
110 SGGeoc::SGGeoc(double lon, double lat, double radius) :
111 _lon(lon), _lat(lat), _radius(radius)
116 SGGeoc::SGGeoc(const SGVec3<double>& cart)
118 SGGeodesy::SGCartToGeoc(cart, *this);
122 SGGeoc::SGGeoc(const SGGeod& geod)
125 SGGeodesy::SGGeodToCart(geod, cart);
126 SGGeodesy::SGCartToGeoc(cart, *this);
131 SGGeoc::fromRadFt(double lon, double lat, double radius)
133 #ifdef SG_GEOC_NATIVE_DEGREE
134 return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
135 radius*SG_FEET_TO_METER);
137 return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
143 SGGeoc::fromDegFt(double lon, double lat, double radius)
145 #ifdef SG_GEOC_NATIVE_DEGREE
146 return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
148 return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
149 radius*SG_FEET_TO_METER);
155 SGGeoc::fromRadM(double lon, double lat, double radius)
157 #ifdef SG_GEOC_NATIVE_DEGREE
158 return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
161 return SGGeoc(lon, lat, radius);
167 SGGeoc::fromDegM(double lon, double lat, double radius)
169 #ifdef SG_GEOC_NATIVE_DEGREE
170 return SGGeoc(lon, lat, radius);
172 return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
179 SGGeoc::fromCart(const SGVec3<double>& cart)
182 SGGeodesy::SGCartToGeoc(cart, geoc);
188 SGGeoc::fromGeod(const SGGeod& geod)
191 SGGeodesy::SGGeodToCart(geod, cart);
193 SGGeodesy::SGCartToGeoc(cart, geoc);
199 SGGeoc::getLongitudeRad(void) const
201 #ifdef SG_GEOC_NATIVE_DEGREE
202 return _lon*SGD_DEGREES_TO_RADIANS;
210 SGGeoc::setLongitudeRad(double lon)
212 #ifdef SG_GEOC_NATIVE_DEGREE
213 _lon = lon*SGD_RADIANS_TO_DEGREES;
221 SGGeoc::getLongitudeDeg(void) const
223 #ifdef SG_GEOC_NATIVE_DEGREE
226 return _lon*SGD_DEGREES_TO_RADIANS;
232 SGGeoc::setLongitudeDeg(double lon)
234 #ifdef SG_GEOC_NATIVE_DEGREE
237 _lon = lon*SGD_RADIANS_TO_DEGREES;
243 SGGeoc::getLatitudeRad(void) const
245 #ifdef SG_GEOC_NATIVE_DEGREE
246 return _lat*SGD_DEGREES_TO_RADIANS;
254 SGGeoc::setLatitudeRad(double lat)
256 #ifdef SG_GEOC_NATIVE_DEGREE
257 _lat = lat*SGD_RADIANS_TO_DEGREES;
265 SGGeoc::getLatitudeDeg(void) const
267 #ifdef SG_GEOC_NATIVE_DEGREE
270 return _lat*SGD_DEGREES_TO_RADIANS;
276 SGGeoc::setLatitudeDeg(double lat)
278 #ifdef SG_GEOC_NATIVE_DEGREE
281 _lat = lat*SGD_RADIANS_TO_DEGREES;
287 SGGeoc::getRadiusM(void) const
294 SGGeoc::setRadiusM(double radius)
301 SGGeoc::getRadiusFt(void) const
303 return _radius*SG_METER_TO_FEET;
308 SGGeoc::setRadiusFt(double radius)
310 _radius = radius*SG_FEET_TO_METER;
313 /// Output to an ostream
314 template<typename char_type, typename traits_type>
316 std::basic_ostream<char_type, traits_type>&
317 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeoc& g)
319 return s << "lon = " << g.getLongitudeDeg()
320 << ", lat = " << g.getLatitudeDeg()
321 << ", radius = " << g.getRadiusM();