]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGGeoc.hxx
Correct finite precision issues.
[simgear.git] / simgear / math / SGGeoc.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 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 private:
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
85   /// that purpose
86   SGGeoc(double lon, double lat, double radius);
87
88   /// The actual data, angles in degree, radius 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
95   double _lon;
96   double _lat;
97   double _radius;
98 };
99
100 inline
101 SGGeoc::SGGeoc(void) :
102   _lon(0), _lat(0), _radius(0)
103 {
104 }
105
106 inline
107 SGGeoc::SGGeoc(double lon, double lat, double radius) :
108   _lon(lon), _lat(lat), _radius(radius)
109 {
110 }
111
112 inline
113 SGGeoc
114 SGGeoc::fromRadFt(double lon, double lat, double radius)
115 {
116 #ifdef SG_GEOC_NATIVE_DEGREE
117   return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
118                 radius*SG_FEET_TO_METER);
119 #else
120   return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
121 #endif
122 }
123
124 inline
125 SGGeoc
126 SGGeoc::fromDegFt(double lon, double lat, double radius)
127 {
128 #ifdef SG_GEOC_NATIVE_DEGREE
129   return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
130 #else
131   return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
132                 radius*SG_FEET_TO_METER);
133 #endif
134 }
135
136 inline
137 SGGeoc
138 SGGeoc::fromRadM(double lon, double lat, double radius)
139 {
140 #ifdef SG_GEOC_NATIVE_DEGREE
141   return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
142                 radius);
143 #else
144   return SGGeoc(lon, lat, radius);
145 #endif
146 }
147
148 inline
149 SGGeoc
150 SGGeoc::fromDegM(double lon, double lat, double radius)
151 {
152 #ifdef SG_GEOC_NATIVE_DEGREE
153   return SGGeoc(lon, lat, radius);
154 #else
155   return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
156                 radius);
157 #endif
158 }
159
160 inline
161 SGGeoc
162 SGGeoc::fromCart(const SGVec3<double>& cart)
163 {
164   SGGeoc geoc;
165   SGGeodesy::SGCartToGeoc(cart, geoc);
166   return geoc;
167 }
168
169 inline
170 SGGeoc
171 SGGeoc::fromGeod(const SGGeod& geod)
172 {
173   SGVec3<double> cart;
174   SGGeodesy::SGGeodToCart(geod, cart);
175   SGGeoc geoc;
176   SGGeodesy::SGCartToGeoc(cart, geoc);
177   return geoc;
178 }
179
180 inline
181 double
182 SGGeoc::getLongitudeRad(void) const
183 {
184 #ifdef SG_GEOC_NATIVE_DEGREE
185   return _lon*SGD_DEGREES_TO_RADIANS;
186 #else
187   return _lon;
188 #endif
189 }
190
191 inline
192 void
193 SGGeoc::setLongitudeRad(double lon)
194 {
195 #ifdef SG_GEOC_NATIVE_DEGREE
196   _lon = lon*SGD_RADIANS_TO_DEGREES;
197 #else
198   _lon = lon;
199 #endif
200 }
201
202 inline
203 double
204 SGGeoc::getLongitudeDeg(void) const
205 {
206 #ifdef SG_GEOC_NATIVE_DEGREE
207   return _lon;
208 #else
209   return _lon*SGD_RADIANS_TO_DEGREES;
210 #endif
211 }
212
213 inline
214 void
215 SGGeoc::setLongitudeDeg(double lon)
216 {
217 #ifdef SG_GEOC_NATIVE_DEGREE
218   _lon = lon;
219 #else
220   _lon = lon*SGD_DEGREES_TO_RADIANS;
221 #endif
222 }
223
224 inline
225 double
226 SGGeoc::getLatitudeRad(void) const
227 {
228 #ifdef SG_GEOC_NATIVE_DEGREE
229   return _lat*SGD_DEGREES_TO_RADIANS;
230 #else
231   return _lat;
232 #endif
233 }
234
235 inline
236 void
237 SGGeoc::setLatitudeRad(double lat)
238 {
239 #ifdef SG_GEOC_NATIVE_DEGREE
240   _lat = lat*SGD_RADIANS_TO_DEGREES;
241 #else
242   _lat = lat;
243 #endif
244 }
245
246 inline
247 double
248 SGGeoc::getLatitudeDeg(void) const
249 {
250 #ifdef SG_GEOC_NATIVE_DEGREE
251   return _lat;
252 #else
253   return _lat*SGD_RADIANS_TO_DEGREES;
254 #endif
255 }
256
257 inline
258 void
259 SGGeoc::setLatitudeDeg(double lat)
260 {
261 #ifdef SG_GEOC_NATIVE_DEGREE
262   _lat = lat;
263 #else
264   _lat = lat*SGD_DEGREES_TO_RADIANS;
265 #endif
266 }
267
268 inline
269 double
270 SGGeoc::getRadiusM(void) const
271 {
272   return _radius;
273 }
274
275 inline
276 void
277 SGGeoc::setRadiusM(double radius)
278 {
279   _radius = radius;
280 }
281
282 inline
283 double
284 SGGeoc::getRadiusFt(void) const
285 {
286   return _radius*SG_METER_TO_FEET;
287 }
288
289 inline
290 void
291 SGGeoc::setRadiusFt(double radius)
292 {
293   _radius = radius*SG_FEET_TO_METER;
294 }
295
296 inline
297 SGGeoc
298 SGGeoc::advanceRadM(double course, double distance) const
299 {
300   SGGeoc result;
301   SGGeodesy::advanceRadM(*this, course, distance, result);
302   return result;
303 }
304
305 inline
306 double
307 SGGeoc::courseRad(const SGGeoc& from, const SGGeoc& to)
308 {
309   return SGGeodesy::courseRad(from, to);
310 }
311
312 inline
313 double
314 SGGeoc::courseDeg(const SGGeoc& from, const SGGeoc& to)
315 {
316   return SGMiscd::rad2deg(courseRad(from, to));
317 }
318
319 inline
320 double
321 SGGeoc::distanceM(const SGGeoc& from, const SGGeoc& to)
322 {
323   return SGGeodesy::distanceM(from, to);
324 }
325
326 /// Output to an ostream
327 template<typename char_type, typename traits_type>
328 inline
329 std::basic_ostream<char_type, traits_type>&
330 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeoc& g)
331 {
332   return s << "lon = " << g.getLongitudeDeg()
333            << ", lat = " << g.getLatitudeDeg()
334            << ", radius = " << g.getRadiusM();
335 }
336
337 #endif