]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGGeoc.hxx
72af480cf5952244c57637252f2f4d0262ba6e84
[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 // You should have received a copy of the GNU General Public License
32 // along with this program; if not, write to the Free Software
33 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
34   /// Note that this conversion is relatively expensive to compute
35   SGGeoc(const SGGeod& geod);
36
37   SGGeoc(const SGVec3<double>& cart);
38
39   /// Factory from angular values in radians and radius in ft
40   static SGGeoc fromRadFt(double lon, double lat, double radius);
41   /// Factory from angular values in degrees and radius in ft
42   static SGGeoc fromDegFt(double lon, double lat, double radius);
43   /// Factory from angular values in radians and radius in m
44   static SGGeoc fromRadM(double lon, double lat, double radius);
45   /// Factory from angular values in degrees and radius in m
46   static SGGeoc fromDegM(double lon, double lat, double radius);
47
48   /// Return the geocentric longitude in radians
49   double getLongitudeRad(void) const;
50   /// Set the geocentric longitude from the argument given in radians
51   void setLongitudeRad(double lon);
52
53   /// Return the geocentric longitude in degrees
54   double getLongitudeDeg(void) const;
55   /// Set the geocentric longitude from the argument given in degrees
56   void setLongitudeDeg(double lon);
57
58   /// Return the geocentric latitude in radians
59   double getLatitudeRad(void) const;
60   /// Set the geocentric latitude from the argument given in radians
61   void setLatitudeRad(double lat);
62
63   /// Return the geocentric latitude in degrees
64   double getLatitudeDeg(void) const;
65   /// Set the geocentric latitude from the argument given in degrees
66   void setLatitudeDeg(double lat);
67
68   /// Return the geocentric radius in meters
69   double getRadiusM(void) const;
70   /// Set the geocentric radius from the argument given in meters
71   void setRadiusM(double radius);
72
73   /// Return the geocentric radius in feet
74   double getRadiusFt(void) const;
75   /// Set the geocentric radius from the argument given in feet
76   void setRadiusFt(double radius);
77
78 private:
79   /// This one is private since construction is not unique if you do
80   /// not know the units of the arguments, use the factory methods for
81   /// that purpose
82   SGGeoc(double lon, double lat, double radius);
83
84   /// The actual data, angles in degree, radius in meters
85   /// The rationale for storing the values in degrees is that most code places
86   /// in flightgear/terragear use degrees as a nativ input and output value.
87   /// The places where it makes sense to use radians is when we convert
88   /// to other representations or compute rotation matrices. But both tasks
89   /// are computionally intensive anyway and that additional 'toRadian'
90   /// conversion does not hurt too much
91   double _lon;
92   double _lat;
93   double _radius;
94 };
95
96 inline
97 SGGeoc::SGGeoc(void) :
98   _lon(0), _lat(0), _radius(0)
99 {
100 }
101
102 inline
103 SGGeoc::SGGeoc(double lon, double lat, double radius) :
104   _lon(lon), _lat(lat), _radius(radius)
105 {
106 }
107
108 inline
109 SGGeoc::SGGeoc(const SGVec3<double>& cart)
110 {
111   SGGeodesy::SGCartToGeoc(cart, *this);
112 }
113
114 inline
115 SGGeoc::SGGeoc(const SGGeod& geod)
116 {
117   SGVec3<double> cart;
118   SGGeodesy::SGGeodToCart(geod, cart);
119   SGGeodesy::SGCartToGeoc(cart, *this);
120 }
121
122 inline
123 SGGeoc
124 SGGeoc::fromRadFt(double lon, double lat, double radius)
125 {
126 #ifdef SG_GEOC_NATIVE_DEGREE
127   return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
128                 radius*SG_FEET_TO_METER);
129 #else
130   return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
131 #endif
132 }
133
134 inline
135 SGGeoc
136 SGGeoc::fromDegFt(double lon, double lat, double radius)
137 {
138 #ifdef SG_GEOC_NATIVE_DEGREE
139   return SGGeoc(lon, lat, radius*SG_FEET_TO_METER);
140 #else
141   return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
142                 radius*SG_FEET_TO_METER);
143 #endif
144 }
145
146 inline
147 SGGeoc
148 SGGeoc::fromRadM(double lon, double lat, double radius)
149 {
150 #ifdef SG_GEOC_NATIVE_DEGREE
151   return SGGeoc(lon*SGD_RADIANS_TO_DEGREES, lat*SGD_RADIANS_TO_DEGREES,
152                 radius);
153 #else
154   return SGGeoc(lon, lat, radius);
155 #endif
156 }
157
158 inline
159 SGGeoc
160 SGGeoc::fromDegM(double lon, double lat, double radius)
161 {
162 #ifdef SG_GEOC_NATIVE_DEGREE
163   return SGGeoc(lon, lat, radius);
164 #else
165   return SGGeoc(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS,
166                 radius);
167 #endif
168 }
169
170 inline
171 double
172 SGGeoc::getLongitudeRad(void) const
173 {
174 #ifdef SG_GEOC_NATIVE_DEGREE
175   return _lon*SGD_DEGREES_TO_RADIANS;
176 #else
177   return _lon;
178 #endif
179 }
180
181 inline
182 void
183 SGGeoc::setLongitudeRad(double lon)
184 {
185 #ifdef SG_GEOC_NATIVE_DEGREE
186   _lon = lon*SGD_RADIANS_TO_DEGREES;
187 #else
188   _lon = lon;
189 #endif
190 }
191
192 inline
193 double
194 SGGeoc::getLongitudeDeg(void) const
195 {
196 #ifdef SG_GEOC_NATIVE_DEGREE
197   return _lon;
198 #else
199   return _lon*SGD_DEGREES_TO_RADIANS;
200 #endif
201 }
202
203 inline
204 void
205 SGGeoc::setLongitudeDeg(double lon)
206 {
207 #ifdef SG_GEOC_NATIVE_DEGREE
208   _lon = lon;
209 #else
210   _lon = lon*SGD_RADIANS_TO_DEGREES;
211 #endif
212 }
213
214 inline
215 double
216 SGGeoc::getLatitudeRad(void) const
217 {
218 #ifdef SG_GEOC_NATIVE_DEGREE
219   return _lat*SGD_DEGREES_TO_RADIANS;
220 #else
221   return _lat;
222 #endif
223 }
224
225 inline
226 void
227 SGGeoc::setLatitudeRad(double lat)
228 {
229 #ifdef SG_GEOC_NATIVE_DEGREE
230   _lat = lat*SGD_RADIANS_TO_DEGREES;
231 #else
232   _lat = lat;
233 #endif
234 }
235
236 inline
237 double
238 SGGeoc::getLatitudeDeg(void) const
239 {
240 #ifdef SG_GEOC_NATIVE_DEGREE
241   return _lat;
242 #else
243   return _lat*SGD_DEGREES_TO_RADIANS;
244 #endif
245 }
246
247 inline
248 void
249 SGGeoc::setLatitudeDeg(double lat)
250 {
251 #ifdef SG_GEOC_NATIVE_DEGREE
252   _lat = lat;
253 #else
254   _lat = lat*SGD_RADIANS_TO_DEGREES;
255 #endif
256 }
257
258 inline
259 double
260 SGGeoc::getRadiusM(void) const
261 {
262   return _radius;
263 }
264
265 inline
266 void
267 SGGeoc::setRadiusM(double radius)
268 {
269   _radius = radius;
270 }
271
272 inline
273 double
274 SGGeoc::getRadiusFt(void) const
275 {
276   return _radius*SG_METER_TO_FEET;
277 }
278
279 inline
280 void
281 SGGeoc::setRadiusFt(double radius)
282 {
283   _radius = radius*SG_FEET_TO_METER;
284 }
285
286 /// Output to an ostream
287 template<typename char_type, typename traits_type>
288 inline
289 std::basic_ostream<char_type, traits_type>&
290 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGGeoc& g)
291 {
292   return s << "lon = " << g.getLongitudeDeg()
293            << ", lat = " << g.getLatitudeDeg()
294            << ", radius = " << g.getRadiusM();
295 }
296
297 #endif