]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGGeod.hxx
MSVC has no std::isnan
[simgear.git] / simgear / math / SGGeod.hxx
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 SGGeod_H
19 #define SGGeod_H
20
21 #include <simgear/constants.h>
22
23 #ifndef _MSC_VER
24 using std::isnan;
25 #endif
26
27 // #define SG_GEOD_NATIVE_DEGREE
28
29 /// Class representing a geodetic location
30 class SGGeod {
31 public:
32   /// Default constructor, initializes the instance to lat = lon = elev = 0
33   SGGeod(void);
34
35   /// Factory from angular values in radians and elevation is 0
36   static SGGeod fromRad(double lon, double lat);
37   /// Factory from angular values in degrees and elevation is 0
38   static SGGeod fromDeg(double lon, double lat);
39   /// Factory from angular values in radians and elevation in ft
40   static SGGeod fromRadFt(double lon, double lat, double elevation);
41   /// Factory from angular values in degrees and elevation in ft
42   static SGGeod fromDegFt(double lon, double lat, double elevation);
43   /// Factory from angular values in radians and elevation in m
44   static SGGeod fromRadM(double lon, double lat, double elevation);
45   /// Factory from angular values in degrees and elevation in m
46   static SGGeod fromDegM(double lon, double lat, double elevation);
47   /// Factory from an other SGGeod and a different elevation in m
48   static SGGeod fromGeodM(const SGGeod& geod, double elevation);
49   /// Factory from an other SGGeod and a different elevation in ft
50   static SGGeod fromGeodFt(const SGGeod& geod, double elevation);
51   /// Factory to convert position from a cartesian position assumed to be
52   /// in wgs84 measured in meters
53   /// Note that this conversion is relatively expensive to compute
54   static SGGeod fromCart(const SGVec3<double>& cart);
55   /// Factory to convert position from a geocentric position
56   /// Note that this conversion is relatively expensive to compute
57   static SGGeod fromGeoc(const SGGeoc& geoc);
58
59   /// Return the geodetic longitude in radians
60   double getLongitudeRad(void) const;
61   /// Set the geodetic longitude from the argument given in radians
62   void setLongitudeRad(double lon);
63
64   /// Return the geodetic longitude in degrees
65   double getLongitudeDeg(void) const;
66   /// Set the geodetic longitude from the argument given in degrees
67   void setLongitudeDeg(double lon);
68
69   /// Return the geodetic latitude in radians
70   double getLatitudeRad(void) const;
71   /// Set the geodetic latitude from the argument given in radians
72   void setLatitudeRad(double lat);
73
74   /// Return the geodetic latitude in degrees
75   double getLatitudeDeg(void) const;
76   /// Set the geodetic latitude from the argument given in degrees
77   void setLatitudeDeg(double lat);
78
79   /// Return the geodetic elevation in meters
80   double getElevationM(void) const;
81   /// Set the geodetic elevation from the argument given in meters
82   void setElevationM(double elevation);
83
84   /// Return the geodetic elevation in feet
85   double getElevationFt(void) const;
86   /// Set the geodetic elevation from the argument given in feet
87   void setElevationFt(double elevation);
88
89   /// Compare two geodetic positions for equality
90   bool operator == ( const SGGeod & other ) const;
91
92   /// check the Geod contains sane values (finite, inside appropriate
93   /// ranges for lat/lon)
94   bool isValid() const;
95 private:
96   /// This one is private since construction is not unique if you do
97   /// not know the units of the arguments. Use the factory methods for
98   /// that purpose
99   SGGeod(double lon, double lat, double elevation);
100
101   //// FIXME: wrong comment!
102   /// The actual data, angles in degrees, elevation in meters
103   /// The rationale for storing the values in degrees is that most code places
104   /// in flightgear/terragear use degrees as a nativ input and output value.
105   /// The places where it makes sense to use radians is when we convert
106   /// to other representations or compute rotation matrices. But both tasks
107   /// are computionally intensive anyway and that additional 'toRadian'
108   /// conversion does not hurt too much
109   double _lon;
110   double _lat;
111   double _elevation;
112 };
113
114 inline
115 SGGeod::SGGeod(void) :
116   _lon(0), _lat(0), _elevation(0)
117 {
118 }
119
120 inline
121 SGGeod::SGGeod(double lon, double lat, double elevation) :
122   _lon(lon), _lat(lat), _elevation(elevation)
123 {
124 }
125
126 inline
127 SGGeod
128 SGGeod::fromRad(double lon, double lat)
129 {
130 #ifdef SG_GEOD_NATIVE_DEGREE
131   return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES, 0);
132 #else
133   return SGGeod(lon, lat, 0);
134 #endif
135 }
136
137 inline
138 SGGeod
139 SGGeod::fromDeg(double lon, double lat)
140 {
141 #ifdef SG_GEOD_NATIVE_DEGREE
142   return SGGeod(lon, lat, 0);
143 #else
144   return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0);
145 #endif
146 }
147
148 inline
149 SGGeod
150 SGGeod::fromRadFt(double lon, double lat, double elevation)
151 {
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);
155 #else
156   return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
157 #endif
158 }
159
160 inline
161 SGGeod
162 SGGeod::fromDegFt(double lon, double lat, double elevation)
163 {
164 #ifdef SG_GEOD_NATIVE_DEGREE
165   return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
166 #else
167   return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
168                 elevation*SG_FEET_TO_METER);
169 #endif
170 }
171
172 inline
173 SGGeod
174 SGGeod::fromRadM(double lon, double lat, double elevation)
175 {
176 #ifdef SG_GEOD_NATIVE_DEGREE
177   return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
178                 elevation);
179 #else
180   return SGGeod(lon, lat, elevation);
181 #endif
182 }
183
184 inline
185 SGGeod
186 SGGeod::fromDegM(double lon, double lat, double elevation)
187 {
188 #ifdef SG_GEOD_NATIVE_DEGREE
189   return SGGeod(lon, lat, elevation);
190 #else
191   return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
192                 elevation);
193 #endif
194 }
195
196 inline
197 SGGeod
198 SGGeod::fromGeodM(const SGGeod& geod, double elevation)
199 {
200   return SGGeod(geod._lon, geod._lat, elevation);
201 }
202
203 inline
204 SGGeod
205 SGGeod::fromGeodFt(const SGGeod& geod, double elevation)
206 {
207   return SGGeod(geod._lon, geod._lat, elevation*SG_FEET_TO_METER);
208 }
209
210 inline
211 SGGeod
212 SGGeod::fromCart(const SGVec3<double>& cart)
213 {
214   SGGeod geod;
215   SGGeodesy::SGCartToGeod(cart, geod);
216   return geod;
217 }
218
219 inline
220 SGGeod
221 SGGeod::fromGeoc(const SGGeoc& geoc)
222 {
223   SGVec3<double> cart;
224   SGGeodesy::SGGeocToCart(geoc, cart);
225   SGGeod geod;
226   SGGeodesy::SGCartToGeod(cart, geod);
227   return geod;
228 }
229
230 inline
231 double
232 SGGeod::getLongitudeRad(void) const
233 {
234 #ifdef SG_GEOD_NATIVE_DEGREE
235   return _lon*SGD_DEGREES_TO_RADIANS;
236 #else
237   return _lon;
238 #endif
239 }
240
241 inline
242 void
243 SGGeod::setLongitudeRad(double lon)
244 {
245 #ifdef SG_GEOD_NATIVE_DEGREE
246   _lon = lon*SGD_RADIANS_TO_DEGREES;
247 #else
248   _lon = lon;
249 #endif
250 }
251
252 inline
253 double
254 SGGeod::getLongitudeDeg(void) const
255 {
256 #ifdef SG_GEOD_NATIVE_DEGREE
257   return _lon;
258 #else
259   return _lon*SGD_RADIANS_TO_DEGREES;
260 #endif
261 }
262
263 inline
264 void
265 SGGeod::setLongitudeDeg(double lon)
266 {
267 #ifdef SG_GEOD_NATIVE_DEGREE
268   _lon = lon;
269 #else
270   _lon = lon*SGD_DEGREES_TO_RADIANS;
271 #endif
272 }
273
274 inline
275 double
276 SGGeod::getLatitudeRad(void) const
277 {
278 #ifdef SG_GEOD_NATIVE_DEGREE
279   return _lat*SGD_DEGREES_TO_RADIANS;
280 #else
281   return _lat;
282 #endif
283 }
284
285 inline
286 void
287 SGGeod::setLatitudeRad(double lat)
288 {
289 #ifdef SG_GEOD_NATIVE_DEGREE
290   _lat = lat*SGD_RADIANS_TO_DEGREES;
291 #else
292   _lat = lat;
293 #endif
294 }
295
296 inline
297 double
298 SGGeod::getLatitudeDeg(void) const
299 {
300 #ifdef SG_GEOD_NATIVE_DEGREE
301   return _lat;
302 #else
303   return _lat*SGD_RADIANS_TO_DEGREES;
304 #endif
305 }
306
307 inline
308 void
309 SGGeod::setLatitudeDeg(double lat)
310 {
311 #ifdef SG_GEOD_NATIVE_DEGREE
312   _lat = lat;
313 #else
314   _lat = lat*SGD_DEGREES_TO_RADIANS;
315 #endif
316 }
317
318 inline
319 double
320 SGGeod::getElevationM(void) const
321 {
322   return _elevation;
323 }
324
325 inline
326 void
327 SGGeod::setElevationM(double elevation)
328 {
329   _elevation = elevation;
330 }
331
332 inline
333 double
334 SGGeod::getElevationFt(void) const
335 {
336   return _elevation*SG_METER_TO_FEET;
337 }
338
339 inline
340 void
341 SGGeod::setElevationFt(double elevation)
342 {
343   _elevation = elevation*SG_FEET_TO_METER;
344 }
345
346 inline
347 bool
348 SGGeod::operator == ( const SGGeod & other ) const
349 {
350   return _lon == other._lon &&
351          _lat == other._lat &&
352          _elevation == other._elevation;
353 }
354
355 inline
356 bool
357 SGGeod::isValid() const
358 {
359   if (isnan(_lon) || isnan(_lat)) return false;
360 #ifdef SG_GEOD_NATIVE_DEGREE
361   return (_lon >= -180.0) && (_lon <= 180.0) &&
362   (_lat >= -90.0) && (_lat <= 90.0);
363 #else
364   return (_lon >= -SGD_PI) && (_lon <= SGD_PI) &&
365   (_lat >= -SGD_PI_2) && (_lat <= SGD_PI_2);
366 #endif
367 }
368
369 /// Output to an ostream
370 template<typename char_type, typename traits_type>
371 inline
372 std::basic_ostream<char_type, traits_type>&
373 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeod& g)
374 {
375   return s << "lon = " << g.getLongitudeDeg()
376            << "deg, lat = " << g.getLatitudeDeg()
377            << "deg, elev = " << g.getElevationM()
378            << "m";
379 }
380
381 #endif