1 // Copyright (C) 2006  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
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.
7 //
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.
12 //
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.
16 //
17 
18 #ifndef SGGeoc_H
19 #define SGGeoc_H
20 
21 #include <simgear/constants.h>
22 
23 // #define SG_GEOC_NATIVE_DEGREE
24 
25 /// Class representing a geocentric location
26 class SGGeoc {
27 public:
28   /// Default constructor, initializes the instance to lat = lon = lat = 0
29   SGGeoc(void);
30 
31   /// Factory from angular values in radians and radius in ft
32   static SGGeoc fromRadFt(double lon, double lat, double radius);
33   /// Factory from angular values in degrees and radius in ft
34   static SGGeoc fromDegFt(double lon, double lat, double radius);
35   /// Factory from angular values in radians and radius in m
36   static SGGeoc fromRadM(double lon, double lat, double radius);
37   /// Factory from angular values in degrees and radius in m
38   static SGGeoc fromDegM(double lon, double lat, double radius);
39   /// Factory to convert position from a cartesian position assumed to be
40   /// in wgs84 measured in meters
41   /// Note that this conversion is relatively expensive to compute
42   static SGGeoc fromCart(const SGVec3<double>& cart);
43   /// Factory to convert position from a geodetic position
44   /// Note that this conversion is relatively expensive to compute
45   static SGGeoc fromGeod(const SGGeod& geod);
46 
47   /// Return the geocentric longitude in radians
48   double getLongitudeRad(void) const;
49   /// Set the geocentric longitude from the argument given in radians
50   void setLongitudeRad(double lon);
51 
52   /// Return the geocentric longitude in degrees
53   double getLongitudeDeg(void) const;
54   /// Set the geocentric longitude from the argument given in degrees
55   void setLongitudeDeg(double lon);
56 
57   /// Return the geocentric latitude in radians
58   double getLatitudeRad(void) const;
59   /// Set the geocentric latitude from the argument given in radians
60   void setLatitudeRad(double lat);
61 
62   /// Return the geocentric latitude in degrees
63   double getLatitudeDeg(void) const;
64   /// Set the geocentric latitude from the argument given in degrees
65   void setLatitudeDeg(double lat);
66 
67   /// Return the geocentric radius in meters
68   double getRadiusM(void) const;
69   /// Set the geocentric radius from the argument given in meters
70   void setRadiusM(double radius);
71 
72   /// Return the geocentric radius in feet
73   double getRadiusFt(void) const;
74   /// Set the geocentric radius from the argument given in feet
75   void setRadiusFt(double radius);
76 
77   SGGeoc advanceRadM(double course, double distance) const;
78   static double courseRad(const SGGeoc& from, const SGGeoc& to);
79   static double courseDeg(const SGGeoc& from, const SGGeoc& to);
80   static double distanceM(const SGGeoc& from, const SGGeoc& to);
81 
82   // Compare two geocentric positions for equality
83   bool operator == ( const SGGeoc & other ) const;
84 private:
85   /// This one is private since construction is not unique if you do
86   /// not know the units of the arguments, use the factory methods for
87   /// that purpose
88   SGGeoc(double lon, double lat, double radius);
89 
90   /// The actual data, angles in degree, radius in meters
91   /// The rationale for storing the values in degrees is that most code places
92   /// in flightgear/terragear use degrees as a nativ input and output value.
93   /// The places where it makes sense to use radians is when we convert
94   /// to other representations or compute rotation matrices. But both tasks
95   /// are computionally intensive anyway and that additional 'toRadian'
96   /// conversion does not hurt too much
97   double _lon;
98   double _lat;
99   double _radius;
100 };
101 
102 inline
SGGeoc(void)103 SGGeoc::SGGeoc(void) :
104   _lon(0), _lat(0), _radius(0)
105 {
106 }
107 
108 inline
SGGeoc(double lon,double lat,double radius)109 SGGeoc::SGGeoc(double lon, double lat, double radius) :
110   _lon(lon), _lat(lat), _radius(radius)
111 {
112 }
113 
114 inline
115 SGGeoc
fromRadFt(double lon,double lat,double radius)116 SGGeoc::fromRadFt(double lon, double lat, double radius)
117 {
118 #ifdef SG_GEOC_NATIVE_DEGREE
119   return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
120                 radius*SG_FEET_TO_METER);
121 #else
122   return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
123 #endif
124 }
125 
126 inline
127 SGGeoc
fromDegFt(double lon,double lat,double radius)128 SGGeoc::fromDegFt(double lon, double lat, double radius)
129 {
130 #ifdef SG_GEOC_NATIVE_DEGREE
131   return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
132 #else
133   return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
134                 radius*SG_FEET_TO_METER);
135 #endif
136 }
137 
138 inline
139 SGGeoc
fromRadM(double lon,double lat,double radius)140 SGGeoc::fromRadM(double lon, double lat, double radius)
141 {
142 #ifdef SG_GEOC_NATIVE_DEGREE
143   return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
144                 radius);
145 #else
146   return SGGeoc(lon, lat, radius);
147 #endif
148 }
149 
150 inline
151 SGGeoc
fromDegM(double lon,double lat,double radius)152 SGGeoc::fromDegM(double lon, double lat, double radius)
153 {
154 #ifdef SG_GEOC_NATIVE_DEGREE
155   return SGGeoc(lon, lat, radius);
156 #else
157   return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
158                 radius);
159 #endif
160 }
161 
162 inline
163 SGGeoc
fromCart(const SGVec3<double> & cart)164 SGGeoc::fromCart(const SGVec3<double>& cart)
165 {
166   SGGeoc geoc;
167   SGGeodesy::SGCartToGeoc(cart, geoc);
168   return geoc;
169 }
170 
171 inline
172 SGGeoc
fromGeod(const SGGeod & geod)173 SGGeoc::fromGeod(const SGGeod& geod)
174 {
175   SGVec3<double> cart;
176   SGGeodesy::SGGeodToCart(geod, cart);
177   SGGeoc geoc;
178   SGGeodesy::SGCartToGeoc(cart, geoc);
179   return geoc;
180 }
181 
182 inline
183 double
getLongitudeRad(void) const184 SGGeoc::getLongitudeRad(void) const
185 {
186 #ifdef SG_GEOC_NATIVE_DEGREE
187   return _lon*SGD_DEGREES_TO_RADIANS;
188 #else
189   return _lon;
190 #endif
191 }
192 
193 inline
194 void
setLongitudeRad(double lon)195 SGGeoc::setLongitudeRad(double lon)
196 {
197 #ifdef SG_GEOC_NATIVE_DEGREE
198   _lon = lon*SGD_RADIANS_TO_DEGREES;
199 #else
200   _lon = lon;
201 #endif
202 }
203 
204 inline
205 double
getLongitudeDeg(void) const206 SGGeoc::getLongitudeDeg(void) const
207 {
208 #ifdef SG_GEOC_NATIVE_DEGREE
209   return _lon;
210 #else
211   return _lon*SGD_RADIANS_TO_DEGREES;
212 #endif
213 }
214 
215 inline
216 void
setLongitudeDeg(double lon)217 SGGeoc::setLongitudeDeg(double lon)
218 {
219 #ifdef SG_GEOC_NATIVE_DEGREE
220   _lon = lon;
221 #else
222   _lon = lon*SGD_DEGREES_TO_RADIANS;
223 #endif
224 }
225 
226 inline
227 double
getLatitudeRad(void) const228 SGGeoc::getLatitudeRad(void) const
229 {
230 #ifdef SG_GEOC_NATIVE_DEGREE
231   return _lat*SGD_DEGREES_TO_RADIANS;
232 #else
233   return _lat;
234 #endif
235 }
236 
237 inline
238 void
setLatitudeRad(double lat)239 SGGeoc::setLatitudeRad(double lat)
240 {
241 #ifdef SG_GEOC_NATIVE_DEGREE
242   _lat = lat*SGD_RADIANS_TO_DEGREES;
243 #else
244   _lat = lat;
245 #endif
246 }
247 
248 inline
249 double
getLatitudeDeg(void) const250 SGGeoc::getLatitudeDeg(void) const
251 {
252 #ifdef SG_GEOC_NATIVE_DEGREE
253   return _lat;
254 #else
255   return _lat*SGD_RADIANS_TO_DEGREES;
256 #endif
257 }
258 
259 inline
260 void
setLatitudeDeg(double lat)261 SGGeoc::setLatitudeDeg(double lat)
262 {
263 #ifdef SG_GEOC_NATIVE_DEGREE
264   _lat = lat;
265 #else
266   _lat = lat*SGD_DEGREES_TO_RADIANS;
267 #endif
268 }
269 
270 inline
271 double
getRadiusM(void) const272 SGGeoc::getRadiusM(void) const
273 {
274   return _radius;
275 }
276 
277 inline
278 void
setRadiusM(double radius)279 SGGeoc::setRadiusM(double radius)
280 {
281   _radius = radius;
282 }
283 
284 inline
285 double
getRadiusFt(void) const286 SGGeoc::getRadiusFt(void) const
287 {
288   return _radius*SG_METER_TO_FEET;
289 }
290 
291 inline
292 void
setRadiusFt(double radius)293 SGGeoc::setRadiusFt(double radius)
294 {
295   _radius = radius*SG_FEET_TO_METER;
296 }
297 
298 inline
299 SGGeoc
advanceRadM(double course,double distance) const300 SGGeoc::advanceRadM(double course, double distance) const
301 {
302   SGGeoc result;
303   SGGeodesy::advanceRadM(*this, course, distance, result);
304   return result;
305 }
306 
307 inline
308 double
courseRad(const SGGeoc & from,const SGGeoc & to)309 SGGeoc::courseRad(const SGGeoc& from, const SGGeoc& to)
310 {
311   return SGGeodesy::courseRad(from, to);
312 }
313 
314 inline
315 double
courseDeg(const SGGeoc & from,const SGGeoc & to)316 SGGeoc::courseDeg(const SGGeoc& from, const SGGeoc& to)
317 {
318   return SGMiscd::rad2deg(courseRad(from, to));
319 }
320 
321 inline
322 double
distanceM(const SGGeoc & from,const SGGeoc & to)323 SGGeoc::distanceM(const SGGeoc& from, const SGGeoc& to)
324 {
325   return SGGeodesy::distanceM(from, to);
326 }
327 
328 inline
329 bool
operator ==(const SGGeoc & other) const330 SGGeoc::operator == ( const SGGeoc & other ) const
331 {
332   return _lon == other._lon &&
333          _lat == other._lat &&
334          _radius == other._radius;
335 }
336 
337 /// Output to an ostream
338 template<typename char_type, typename traits_type>
339 inline
340 std::basic_ostream<char_type, traits_type>&
operator <<(std::basic_ostream<char_type,traits_type> & s,const SGGeoc & g)341 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeoc& g)
342 {
343   return s << "lon = " << g.getLongitudeDeg()
344            << ", lat = " << g.getLatitudeDeg()
345            << ", radius = " << g.getRadiusM();
346 }
347 
348 #endif
349