]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGGeoc.hxx
Modified Files:
[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 private:
78   /// This one is private since construction is not unique if you do
79   /// not know the units of the arguments, use the factory methods for
80   /// that purpose
81   SGGeoc(double lon, double lat, double radius);
82
83   /// The actual data, angles in degree, radius in meters
84   /// The rationale for storing the values in degrees is that most code places
85   /// in flightgear/terragear use degrees as a nativ input and output value.
86   /// The places where it makes sense to use radians is when we convert
87   /// to other representations or compute rotation matrices. But both tasks
88   /// are computionally intensive anyway and that additional 'toRadian'
89   /// conversion does not hurt too much
90   double _lon;
91   double _lat;
92   double _radius;
93 };
94
95 inline
96 SGGeoc::SGGeoc(void) :
97   _lon(0), _lat(0), _radius(0)
98 {
99 }
100
101 inline
102 SGGeoc::SGGeoc(double lon, double lat, double radius) :
103   _lon(lon), _lat(lat), _radius(radius)
104 {
105 }
106
107 inline
108 SGGeoc
109 SGGeoc::fromRadFt(double lon, double lat, double radius)
110 {
111 #ifdef SG_GEOC_NATIVE_DEGREE
112   return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
113                 radius*SG_FEET_TO_METER);
114 #else
115   return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
116 #endif
117 }
118
119 inline
120 SGGeoc
121 SGGeoc::fromDegFt(double lon, double lat, double radius)
122 {
123 #ifdef SG_GEOC_NATIVE_DEGREE
124   return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
125 #else
126   return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
127                 radius*SG_FEET_TO_METER);
128 #endif
129 }
130
131 inline
132 SGGeoc
133 SGGeoc::fromRadM(double lon, double lat, double radius)
134 {
135 #ifdef SG_GEOC_NATIVE_DEGREE
136   return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
137                 radius);
138 #else
139   return SGGeoc(lon, lat, radius);
140 #endif
141 }
142
143 inline
144 SGGeoc
145 SGGeoc::fromDegM(double lon, double lat, double radius)
146 {
147 #ifdef SG_GEOC_NATIVE_DEGREE
148   return SGGeoc(lon, lat, radius);
149 #else
150   return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
151                 radius);
152 #endif
153 }
154
155 inline
156 SGGeoc
157 SGGeoc::fromCart(const SGVec3<double>& cart)
158 {
159   SGGeoc geoc;
160   SGGeodesy::SGCartToGeoc(cart, geoc);
161   return geoc;
162 }
163
164 inline
165 SGGeoc
166 SGGeoc::fromGeod(const SGGeod& geod)
167 {
168   SGVec3<double> cart;
169   SGGeodesy::SGGeodToCart(geod, cart);
170   SGGeoc geoc;
171   SGGeodesy::SGCartToGeoc(cart, geoc);
172   return geoc;
173 }
174
175 inline
176 double
177 SGGeoc::getLongitudeRad(void) const
178 {
179 #ifdef SG_GEOC_NATIVE_DEGREE
180   return _lon*SGD_DEGREES_TO_RADIANS;
181 #else
182   return _lon;
183 #endif
184 }
185
186 inline
187 void
188 SGGeoc::setLongitudeRad(double lon)
189 {
190 #ifdef SG_GEOC_NATIVE_DEGREE
191   _lon = lon*SGD_RADIANS_TO_DEGREES;
192 #else
193   _lon = lon;
194 #endif
195 }
196
197 inline
198 double
199 SGGeoc::getLongitudeDeg(void) const
200 {
201 #ifdef SG_GEOC_NATIVE_DEGREE
202   return _lon;
203 #else
204   return _lon*SGD_DEGREES_TO_RADIANS;
205 #endif
206 }
207
208 inline
209 void
210 SGGeoc::setLongitudeDeg(double lon)
211 {
212 #ifdef SG_GEOC_NATIVE_DEGREE
213   _lon = lon;
214 #else
215   _lon = lon*SGD_RADIANS_TO_DEGREES;
216 #endif
217 }
218
219 inline
220 double
221 SGGeoc::getLatitudeRad(void) const
222 {
223 #ifdef SG_GEOC_NATIVE_DEGREE
224   return _lat*SGD_DEGREES_TO_RADIANS;
225 #else
226   return _lat;
227 #endif
228 }
229
230 inline
231 void
232 SGGeoc::setLatitudeRad(double lat)
233 {
234 #ifdef SG_GEOC_NATIVE_DEGREE
235   _lat = lat*SGD_RADIANS_TO_DEGREES;
236 #else
237   _lat = lat;
238 #endif
239 }
240
241 inline
242 double
243 SGGeoc::getLatitudeDeg(void) const
244 {
245 #ifdef SG_GEOC_NATIVE_DEGREE
246   return _lat;
247 #else
248   return _lat*SGD_DEGREES_TO_RADIANS;
249 #endif
250 }
251
252 inline
253 void
254 SGGeoc::setLatitudeDeg(double lat)
255 {
256 #ifdef SG_GEOC_NATIVE_DEGREE
257   _lat = lat;
258 #else
259   _lat = lat*SGD_RADIANS_TO_DEGREES;
260 #endif
261 }
262
263 inline
264 double
265 SGGeoc::getRadiusM(void) const
266 {
267   return _radius;
268 }
269
270 inline
271 void
272 SGGeoc::setRadiusM(double radius)
273 {
274   _radius = radius;
275 }
276
277 inline
278 double
279 SGGeoc::getRadiusFt(void) const
280 {
281   return _radius*SG_METER_TO_FEET;
282 }
283
284 inline
285 void
286 SGGeoc::setRadiusFt(double radius)
287 {
288   _radius = radius*SG_FEET_TO_METER;
289 }
290
291 /// Output to an ostream
292 template<typename char_type, typename traits_type>
293 inline
294 std::basic_ostream<char_type, traits_type>&
295 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeoc& g)
296 {
297   return s << "lon = " << g.getLongitudeDeg()
298            << ", lat = " << g.getLatitudeDeg()
299            << ", radius = " << g.getRadiusM();
300 }
301
302 #endif