]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGGeoc.hxx
Ignore generated binaries.
[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 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 SGGeoc_H
20 #define SGGeoc_H
21
22 #include <simgear/constants.h>
23  
24 // #define SG_GEOC_NATIVE_DEGREE
25
26 /// Class representing a geocentric location
27 class SGGeoc {
28 public:
29   /// Default constructor, initializes the instance to lat = lon = lat = 0
30   SGGeoc(void);
31   /// Initialize from a cartesian vector assumed to be in meters
32   /// Note that this conversion is relatively expensive to compute
33   SGGeoc(const SGVec3<double>& cart);
34   /// Initialize from a geodetic position
35   /// Note that this conversion is relatively expensive to compute
36   SGGeoc(const SGGeod& geod);
37
38   /// Factory from angular values in radians and radius in ft
39   static SGGeoc fromRadFt(double lon, double lat, double radius);
40   /// Factory from angular values in degrees and radius in ft
41   static SGGeoc fromDegFt(double lon, double lat, double radius);
42   /// Factory from angular values in radians and radius in m
43   static SGGeoc fromRadM(double lon, double lat, double radius);
44   /// Factory from angular values in degrees and radius in m
45   static SGGeoc fromDegM(double lon, double lat, double radius);
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::SGGeoc(const SGVec3<double>& cart)
109 {
110   SGGeodesy::SGCartToGeoc(cart, *this);
111 }
112
113 inline
114 SGGeoc::SGGeoc(const SGGeod& geod)
115 {
116   SGVec3<double> cart;
117   SGGeodesy::SGGeodToCart(geod, cart);
118   SGGeodesy::SGCartToGeoc(cart, *this);
119 }
120
121 inline
122 SGGeoc
123 SGGeoc::fromRadFt(double lon, double lat, double radius)
124 {
125 #ifdef SG_GEOC_NATIVE_DEGREE
126   return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
127                 radius*SG_FEET_TO_METER);
128 #else
129   return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
130 #endif
131 }
132
133 inline
134 SGGeoc
135 SGGeoc::fromDegFt(double lon, double lat, double radius)
136 {
137 #ifdef SG_GEOC_NATIVE_DEGREE
138   return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
139 #else
140   return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
141                 radius*SG_FEET_TO_METER);
142 #endif
143 }
144
145 inline
146 SGGeoc
147 SGGeoc::fromRadM(double lon, double lat, double radius)
148 {
149 #ifdef SG_GEOC_NATIVE_DEGREE
150   return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
151                 radius);
152 #else
153   return SGGeoc(lon, lat, radius);
154 #endif
155 }
156
157 inline
158 SGGeoc
159 SGGeoc::fromDegM(double lon, double lat, double radius)
160 {
161 #ifdef SG_GEOC_NATIVE_DEGREE
162   return SGGeoc(lon, lat, radius);
163 #else
164   return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
165                 radius);
166 #endif
167 }
168
169 inline
170 double
171 SGGeoc::getLongitudeRad(void) const
172 {
173 #ifdef SG_GEOC_NATIVE_DEGREE
174   return _lon*SGD_DEGREES_TO_RADIANS;
175 #else
176   return _lon;
177 #endif
178 }
179
180 inline
181 void
182 SGGeoc::setLongitudeRad(double lon)
183 {
184 #ifdef SG_GEOC_NATIVE_DEGREE
185   _lon = lon*SGD_RADIANS_TO_DEGREES;
186 #else
187   _lon = lon;
188 #endif
189 }
190
191 inline
192 double
193 SGGeoc::getLongitudeDeg(void) const
194 {
195 #ifdef SG_GEOC_NATIVE_DEGREE
196   return _lon;
197 #else
198   return _lon*SGD_DEGREES_TO_RADIANS;
199 #endif
200 }
201
202 inline
203 void
204 SGGeoc::setLongitudeDeg(double lon)
205 {
206 #ifdef SG_GEOC_NATIVE_DEGREE
207   _lon = lon;
208 #else
209   _lon = lon*SGD_RADIANS_TO_DEGREES;
210 #endif
211 }
212
213 inline
214 double
215 SGGeoc::getLatitudeRad(void) const
216 {
217 #ifdef SG_GEOC_NATIVE_DEGREE
218   return _lat*SGD_DEGREES_TO_RADIANS;
219 #else
220   return _lat;
221 #endif
222 }
223
224 inline
225 void
226 SGGeoc::setLatitudeRad(double lat)
227 {
228 #ifdef SG_GEOC_NATIVE_DEGREE
229   _lat = lat*SGD_RADIANS_TO_DEGREES;
230 #else
231   _lat = lat;
232 #endif
233 }
234
235 inline
236 double
237 SGGeoc::getLatitudeDeg(void) const
238 {
239 #ifdef SG_GEOC_NATIVE_DEGREE
240   return _lat;
241 #else
242   return _lat*SGD_DEGREES_TO_RADIANS;
243 #endif
244 }
245
246 inline
247 void
248 SGGeoc::setLatitudeDeg(double lat)
249 {
250 #ifdef SG_GEOC_NATIVE_DEGREE
251   _lat = lat;
252 #else
253   _lat = lat*SGD_RADIANS_TO_DEGREES;
254 #endif
255 }
256
257 inline
258 double
259 SGGeoc::getRadiusM(void) const
260 {
261   return _radius;
262 }
263
264 inline
265 void
266 SGGeoc::setRadiusM(double radius)
267 {
268   _radius = radius;
269 }
270
271 inline
272 double
273 SGGeoc::getRadiusFt(void) const
274 {
275   return _radius*SG_METER_TO_FEET;
276 }
277
278 inline
279 void
280 SGGeoc::setRadiusFt(double radius)
281 {
282   _radius = radius*SG_FEET_TO_METER;
283 }
284
285 /// Output to an ostream
286 template<typename char_type, typename traits_type>
287 inline
288 std::basic_ostream<char_type, traits_type>&
289 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeoc& g)
290 {
291   return s << "lon = " << g.getLongitudeDeg()
292            << ", lat = " << g.getLatitudeDeg()
293            << ", radius = " << g.getRadiusM();
294 }
295
296 #endif