]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGGeod.hxx
Ignore generated binaries.
[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 Library General Public
14 // License along with this library; if not, write to the
15 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 // Boston, MA  02111-1307, USA.
17 //
18
19 #ifndef SGGeod_H
20 #define SGGeod_H
21
22 #include <simgear/constants.h>
23
24 // #define SG_GEOD_NATIVE_DEGREE
25
26 /// Class representing a geodetic location
27 class SGGeod {
28 public:
29   /// Default constructor, initializes the instance to lat = lon = elev = 0
30   SGGeod(void);
31   /// Initialize from a cartesian vector assumed to be in meters
32   /// Note that this conversion is relatively expensive to compute
33   SGGeod(const SGVec3<double>& cart);
34   /// Initialize from a geocentric position
35   /// Note that this conversion is relatively expensive to compute
36   SGGeod(const SGGeoc& geoc);
37
38   /// Factory from angular values in radians and elevation is 0
39   static SGGeod fromRad(double lon, double lat);
40   /// Factory from angular values in degrees and elevation is 0
41   static SGGeod fromDeg(double lon, double lat);
42   /// Factory from angular values in radians and elevation in ft
43   static SGGeod fromRadFt(double lon, double lat, double elevation);
44   /// Factory from angular values in degrees and elevation in ft
45   static SGGeod fromDegFt(double lon, double lat, double elevation);
46   /// Factory from angular values in radians and elevation in m
47   static SGGeod fromRadM(double lon, double lat, double elevation);
48   /// Factory from angular values in degrees and elevation in m
49   static SGGeod fromDegM(double lon, double lat, double elevation);
50
51   /// Return the geodetic longitude in radians
52   double getLongitudeRad(void) const;
53   /// Set the geodetic longitude from the argument given in radians
54   void setLongitudeRad(double lon);
55
56   /// Return the geodetic longitude in degrees
57   double getLongitudeDeg(void) const;
58   /// Set the geodetic longitude from the argument given in degrees
59   void setLongitudeDeg(double lon);
60
61   /// Return the geodetic latitude in radians
62   double getLatitudeRad(void) const;
63   /// Set the geodetic latitude from the argument given in radians
64   void setLatitudeRad(double lat);
65
66   /// Return the geodetic latitude in degrees
67   double getLatitudeDeg(void) const;
68   /// Set the geodetic latitude from the argument given in degrees
69   void setLatitudeDeg(double lat);
70
71   /// Return the geodetic elevation in meters
72   double getElevationM(void) const;
73   /// Set the geodetic elevation from the argument given in meters
74   void setElevationM(double elevation);
75
76   /// Return the geodetic elevation in feet
77   double getElevationFt(void) const;
78   /// Set the geodetic elevation from the argument given in feet
79   void setElevationFt(double elevation);
80
81 private:
82   /// This one is private since construction is not unique if you do
83   /// not know the units of the arguments. Use the factory methods for
84   /// that purpose
85   SGGeod(double lon, double lat, double elevation);
86
87   /// The actual data, angles in degree, elevation in meters
88   /// The rationale for storing the values in degrees is that most code places
89   /// in flightgear/terragear use degrees as a nativ input and output value.
90   /// The places where it makes sense to use radians is when we convert
91   /// to other representations or compute rotation matrices. But both tasks
92   /// are computionally intensive anyway and that additional 'toRadian'
93   /// conversion does not hurt too much
94   double _lon;
95   double _lat;
96   double _elevation;
97 };
98
99 inline
100 SGGeod::SGGeod(void) :
101   _lon(0), _lat(0), _elevation(0)
102 {
103 }
104
105 inline
106 SGGeod::SGGeod(double lon, double lat, double elevation) :
107   _lon(lon), _lat(lat), _elevation(elevation)
108 {
109 }
110
111 inline
112 SGGeod::SGGeod(const SGVec3<double>& cart)
113 {
114   SGGeodesy::SGCartToGeod(cart, *this);
115 }
116
117 inline
118 SGGeod::SGGeod(const SGGeoc& geoc)
119 {
120   SGVec3<double> cart;
121   SGGeodesy::SGGeocToCart(geoc, cart);
122   SGGeodesy::SGCartToGeod(cart, *this);
123 }
124
125 inline
126 SGGeod
127 SGGeod::fromRad(double lon, double lat)
128 {
129 #ifdef SG_GEOD_NATIVE_DEGREE
130   return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES, 0);
131 #else
132   return SGGeod(lon, lat, 0);
133 #endif
134 }
135
136 inline
137 SGGeod
138 SGGeod::fromDeg(double lon, double lat)
139 {
140 #ifdef SG_GEOD_NATIVE_DEGREE
141   return SGGeod(lon, lat, 0);
142 #else
143   return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0);
144 #endif
145 }
146
147 inline
148 SGGeod
149 SGGeod::fromRadFt(double lon, double lat, double elevation)
150 {
151 #ifdef SG_GEOD_NATIVE_DEGREE
152   return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
153                 elevation*SG_FEET_TO_METER);
154 #else
155   return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
156 #endif
157 }
158
159 inline
160 SGGeod
161 SGGeod::fromDegFt(double lon, double lat, double elevation)
162 {
163 #ifdef SG_GEOD_NATIVE_DEGREE
164   return SGGeod(lon, lat, elevation*SG_FEET_TO_METER);
165 #else
166   return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
167                 elevation*SG_FEET_TO_METER);
168 #endif
169 }
170
171 inline
172 SGGeod
173 SGGeod::fromRadM(double lon, double lat, double elevation)
174 {
175 #ifdef SG_GEOD_NATIVE_DEGREE
176   return SGGeod(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
177                 elevation);
178 #else
179   return SGGeod(lon, lat, elevation);
180 #endif
181 }
182
183 inline
184 SGGeod
185 SGGeod::fromDegM(double lon, double lat, double elevation)
186 {
187 #ifdef SG_GEOD_NATIVE_DEGREE
188   return SGGeod(lon, lat, elevation);
189 #else
190   return SGGeod(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
191                 elevation);
192 #endif
193 }
194
195 inline
196 double
197 SGGeod::getLongitudeRad(void) const
198 {
199 #ifdef SG_GEOD_NATIVE_DEGREE
200   return _lon*SGD_DEGREES_TO_RADIANS;
201 #else
202   return _lon;
203 #endif
204 }
205
206 inline
207 void
208 SGGeod::setLongitudeRad(double lon)
209 {
210 #ifdef SG_GEOD_NATIVE_DEGREE
211   _lon = lon*SGD_RADIANS_TO_DEGREES;
212 #else
213   _lon = lon;
214 #endif
215 }
216
217 inline
218 double
219 SGGeod::getLongitudeDeg(void) const
220 {
221 #ifdef SG_GEOD_NATIVE_DEGREE
222   return _lon;
223 #else
224   return _lon*SGD_RADIANS_TO_DEGREES;
225 #endif
226 }
227
228 inline
229 void
230 SGGeod::setLongitudeDeg(double lon)
231 {
232 #ifdef SG_GEOD_NATIVE_DEGREE
233   _lon = lon;
234 #else
235   _lon = lon*SGD_DEGREES_TO_RADIANS;
236 #endif
237 }
238
239 inline
240 double
241 SGGeod::getLatitudeRad(void) const
242 {
243 #ifdef SG_GEOD_NATIVE_DEGREE
244   return _lat*SGD_DEGREES_TO_RADIANS;
245 #else
246   return _lat;
247 #endif
248 }
249
250 inline
251 void
252 SGGeod::setLatitudeRad(double lat)
253 {
254 #ifdef SG_GEOD_NATIVE_DEGREE
255   _lat = lat*SGD_RADIANS_TO_DEGREES;
256 #else
257   _lat = lat;
258 #endif
259 }
260
261 inline
262 double
263 SGGeod::getLatitudeDeg(void) const
264 {
265 #ifdef SG_GEOD_NATIVE_DEGREE
266   return _lat;
267 #else
268   return _lat*SGD_RADIANS_TO_DEGREES;
269 #endif
270 }
271
272 inline
273 void
274 SGGeod::setLatitudeDeg(double lat)
275 {
276 #ifdef SG_GEOD_NATIVE_DEGREE
277   _lat = lat;
278 #else
279   _lat = lat*SGD_DEGREES_TO_RADIANS;
280 #endif
281 }
282
283 inline
284 double
285 SGGeod::getElevationM(void) const
286 {
287   return _elevation;
288 }
289
290 inline
291 void
292 SGGeod::setElevationM(double elevation)
293 {
294   _elevation = elevation;
295 }
296
297 inline
298 double
299 SGGeod::getElevationFt(void) const
300 {
301   return _elevation*SG_METER_TO_FEET;
302 }
303
304 inline
305 void
306 SGGeod::setElevationFt(double elevation)
307 {
308   _elevation = elevation*SG_FEET_TO_METER;
309 }
310
311 /// Output to an ostream
312 template<typename char_type, typename traits_type>
313 inline
314 std::basic_ostream<char_type, traits_type>&
315 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeod& g)
316 {
317   return s << "lon = " << g.getLongitudeDeg()
318            << "deg, lat = " << g.getLatitudeDeg()
319            << "deg, elev = " << g.getElevationM()
320            << "m";
321 }
322
323 #endif