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
32 SGGeoc(const SGVec3<double>& cart);
33 /// Initialize from a geodetic position
34 /// Note that this conversion is relatively expensive to compute
35 SGGeoc(const SGGeod& geod);
37 /// Factory from angular values in radians and radius in ft
38 static SGGeoc fromRadFt(double lon, double lat, double radius);
39 /// Factory from angular values in degrees and radius in ft
40 static SGGeoc fromDegFt(double lon, double lat, double radius);
41 /// Factory from angular values in radians and radius in m
42 static SGGeoc fromRadM(double lon, double lat, double radius);
43 /// Factory from angular values in degrees and radius in m
44 static SGGeoc fromDegM(double lon, double lat, double radius);
46 /// Return the geocentric longitude in radians
47 double getLongitudeRad(void) const;
48 /// Set the geocentric longitude from the argument given in radians
49 void setLongitudeRad(double lon);
51 /// Return the geocentric longitude in degrees
52 double getLongitudeDeg(void) const;
53 /// Set the geocentric longitude from the argument given in degrees
54 void setLongitudeDeg(double lon);
56 /// Return the geocentric latitude in radians
57 double getLatitudeRad(void) const;
58 /// Set the geocentric latitude from the argument given in radians
59 void setLatitudeRad(double lat);
61 /// Return the geocentric latitude in degrees
62 double getLatitudeDeg(void) const;
63 /// Set the geocentric latitude from the argument given in degrees
64 void setLatitudeDeg(double lat);
66 /// Return the geocentric radius in meters
67 double getRadiusM(void) const;
68 /// Set the geocentric radius from the argument given in meters
69 void setRadiusM(double radius);
71 /// Return the geocentric radius in feet
72 double getRadiusFt(void) const;
73 /// Set the geocentric radius from the argument given in feet
74 void setRadiusFt(double radius);
77 /// This one is private since construction is not unique if you do
78 /// not know the units of the arguments, use the factory methods for
80 SGGeoc(double lon, double lat, double radius);
82 /// The actual data, angles in degree, radius in meters
83 /// The rationale for storing the values in degrees is that most code places
84 /// in flightgear/terragear use degrees as a nativ input and output value.
85 /// The places where it makes sense to use radians is when we convert
86 /// to other representations or compute rotation matrices. But both tasks
87 /// are computionally intensive anyway and that additional 'toRadian'
88 /// conversion does not hurt too much
95 SGGeoc::SGGeoc(void) :
96 _lon(0), _lat(0), _radius(0)
101 SGGeoc::SGGeoc(double lon, double lat, double radius) :
102 _lon(lon), _lat(lat), _radius(radius)
107 SGGeoc::SGGeoc(const SGVec3<double>& cart)
109 SGGeodesy::SGCartToGeoc(cart, *this);
113 SGGeoc::SGGeoc(const SGGeod& geod)
116 SGGeodesy::SGGeodToCart(geod, cart);
117 SGGeodesy::SGCartToGeoc(cart, *this);
122 SGGeoc::fromRadFt(double lon, double lat, double radius)
124 #ifdef SG_GEOC_NATIVE_DEGREE
125 return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
126 radius*SG_FEET_TO_METER);
128 return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
134 SGGeoc::fromDegFt(double lon, double lat, double radius)
136 #ifdef SG_GEOC_NATIVE_DEGREE
137 return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
139 return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
140 radius*SG_FEET_TO_METER);
146 SGGeoc::fromRadM(double lon, double lat, double radius)
148 #ifdef SG_GEOC_NATIVE_DEGREE
149 return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
152 return SGGeoc(lon, lat, radius);
158 SGGeoc::fromDegM(double lon, double lat, double radius)
160 #ifdef SG_GEOC_NATIVE_DEGREE
161 return SGGeoc(lon, lat, radius);
163 return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
170 SGGeoc::getLongitudeRad(void) const
172 #ifdef SG_GEOC_NATIVE_DEGREE
173 return _lon*SGD_DEGREES_TO_RADIANS;
181 SGGeoc::setLongitudeRad(double lon)
183 #ifdef SG_GEOC_NATIVE_DEGREE
184 _lon = lon*SGD_RADIANS_TO_DEGREES;
192 SGGeoc::getLongitudeDeg(void) const
194 #ifdef SG_GEOC_NATIVE_DEGREE
197 return _lon*SGD_DEGREES_TO_RADIANS;
203 SGGeoc::setLongitudeDeg(double lon)
205 #ifdef SG_GEOC_NATIVE_DEGREE
208 _lon = lon*SGD_RADIANS_TO_DEGREES;
214 SGGeoc::getLatitudeRad(void) const
216 #ifdef SG_GEOC_NATIVE_DEGREE
217 return _lat*SGD_DEGREES_TO_RADIANS;
225 SGGeoc::setLatitudeRad(double lat)
227 #ifdef SG_GEOC_NATIVE_DEGREE
228 _lat = lat*SGD_RADIANS_TO_DEGREES;
236 SGGeoc::getLatitudeDeg(void) const
238 #ifdef SG_GEOC_NATIVE_DEGREE
241 return _lat*SGD_DEGREES_TO_RADIANS;
247 SGGeoc::setLatitudeDeg(double lat)
249 #ifdef SG_GEOC_NATIVE_DEGREE
252 _lat = lat*SGD_RADIANS_TO_DEGREES;
258 SGGeoc::getRadiusM(void) const
265 SGGeoc::setRadiusM(double radius)
272 SGGeoc::getRadiusFt(void) const
274 return _radius*SG_METER_TO_FEET;
279 SGGeoc::setRadiusFt(double radius)
281 _radius = radius*SG_FEET_TO_METER;
284 /// Output to an ostream
285 template<typename char_type, typename traits_type>
287 std::basic_ostream<char_type, traits_type>&
288 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeoc& g)
290 return s << "lon = " << g.getLongitudeDeg()
291 << ", lat = " << g.getLatitudeDeg()
292 << ", radius = " << g.getRadiusM();