Debian Codegraph @

src/FDM/JSBSim/math/FGLocation.h

raw symbols SCIP
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Header: FGLocation.h
4 Author: Jon S. Berndt, Mathias Froehlich
5 Date started: 04/04/2004
6
7 ------- Copyright (C) 1999 Jon S. Berndt ([email protected]) ------------------
8 ------- (C) 2004 Mathias Froehlich ([email protected]) ----
9 ------- (C) 2011 Ola Røer Thorsen ([email protected]) -----------
10
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 2 of the License, or (at your option) any
14 later version.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19 details.
20
21 You should have received a copy of the GNU Lesser General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc., 59
23 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 Further information about the GNU Lesser General Public License can also be
26 found on the world wide web at http://www.gnu.org.
27
28 HISTORY
29 -------------------------------------------------------------------------------
30 04/04/2004 MF Created from code previously in the old positions class.
31 11/01/2011 ORT Encapsulated ground callback code in FGLocation and removed
32 it from FGFDMExec.
33
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 SENTRY
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37
38 #ifndef FGLOCATION_H
39 #define FGLOCATION_H
40
41 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 INCLUDES
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44
45 #include <cassert>
46
47 #include "FGJSBBase.h"
48 #include "FGColumnVector3.h"
49 #include "FGMatrix33.h"
50
51 /* Setting the -ffast-math compilation flag is highly discouraged */
52 #ifdef __FAST_MATH__
53 #error Usage of -ffast-math is strongly discouraged
54 #endif
55
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 FORWARD DECLARATIONS
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59
60 namespace JSBSim {
61
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 CLASS DOCUMENTATION
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65
66 /** FGLocation holds an arbitrary location in the Earth centered Earth fixed
67 reference frame (ECEF). The coordinate frame ECEF has its center in the
68 middle of the earth. The X-axis points from the center of the Earth towards
69 a location with zero latitude and longitude on the Earth surface. The Y-axis
70 points from the center of the Earth towards a location with zero latitude
71 and 90 deg East longitude on the Earth surface. The Z-axis points from the
72 Earth center to the geographic north pole.
73
74 This class provides access functions to set and get the location as either
75 the simple X, Y and Z values in ft or longitude/latitude and the radial
76 distance of the location from the Earth center.
77
78 It is common to associate a parent frame with a location. This frame is
79 usually called the local horizontal frame or simply the local frame. It is
80 also called the NED frame (North, East, Down), as well as the Navigation
81 frame. This frame has its X/Y plane parallel to the surface of the Earth.
82 The X-axis points towards north, the Y-axis points east and the Z-axis
83 is normal to the reference spheroid (WGS84 for Earth).
84
85 Since the local frame is determined by the location (and NOT by the
86 orientation of the vehicle IN any frame), this class also provides the
87 rotation matrices required to transform from the Earth centered (ECEF) frame
88 to the local horizontal frame and back. This class "owns" the
89 transformations that go from the ECEF frame to and from the local frame.
90 Again, this is because the ECEF, and local frames do not involve the actual
91 orientation of the vehicle - only the location on the Earth surface. There
92 are conversion functions for conversion of position vectors given in the one
93 frame to positions in the other frame.
94
95 The Earth centered reference frame is NOT an inertial frame since it rotates
96 with the Earth.
97
98 The cartesian coordinates (X,Y,Z) in the Earth centered frame are the master
99 values. All other values are computed from these master values and are
100 cached as long as the location is changed by access through a non-const
101 member function. Values are cached to improve performance. It is best
102 practice to work with a natural set of master values. Other parameters that
103 are derived from these master values are calculated only when needed, and IF
104 they are needed and calculated, then they are cached (stored and remembered)
105 so they do not need to be re-calculated until the master values they are
106 derived from are themselves changed (and become stale).
107
108 Accuracy and round off
109
110 Given,
111
112 - that we model a vehicle near the Earth
113 - that the Earth surface average radius is about 2*10^7, ft
114 - that we use double values for the representation of the location
115
116 we have an accuracy of about
117
118 1e-16*2e7ft/1 = 2e-9 ft
119
120 left. This should be sufficient for our needs. Note that this is the same
121 relative accuracy we would have when we compute directly with
122 lon/lat/radius. For the radius value this is clear. For the lon/lat pair
123 this is easy to see. Take for example KSFO located at about 37.61 deg north
124 122.35 deg west, which corresponds to 0.65642 rad north and 2.13541 rad
125 west. Both values are of magnitude of about 1. But 1 ft corresponds to about
126 1/(2e7*2*pi) = 7.9577e-09 rad. So the left accuracy with this representation
127 is also about 1*1e-16/7.9577e-09 = 1.2566e-08 which is of the same magnitude
128 as the representation chosen here.
129
130 The advantage of this representation is that it is a linear space without
131 singularities. The singularities are the north and south pole and most
132 notably the non-steady jump at -pi to pi. It is harder to track this jump
133 correctly especially when we need to work with error norms and derivatives
134 of the equations of motion within the time-stepping code. Also, the rate of
135 change is of the same magnitude for all components in this representation
136 which is an advantage for numerical stability in implicit time-stepping.
137
138 Note: Both GEOCENTRIC and GEODETIC latitudes can be used. In order to get
139 best matching relative to a map, geodetic latitude must be used.
140
141 @see Stevens and Lewis, "Aircraft Control and Simulation", Second edition
142 @see W. C. Durham "Aircraft Dynamics & Control", section 2.2
143
144 @author Mathias Froehlich
145 */
146
147 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 CLASS DECLARATION
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
150
151 class JSBSIM_API FGLocation : public FGJSBBase
152 {
153 public:
154 /** Default constructor. */
155 FGLocation(void);
156
157 /** Constructor to set the longitude, latitude and the distance
158 from the center of the earth.
159 @param lon longitude
160 @param lat GEOCENTRIC latitude
161 @param radius distance from center of earth to vehicle in feet*/
162 FGLocation(double lon, double lat, double radius);
163
164 /** Constructor to initialize the location with the cartesian coordinates
165 (X,Y,Z) contained in the input FGColumnVector3. Distances are in feet,
166 the position is expressed in the ECEF frame.
167 @param lv vector that contain the cartesian coordinates*/
168 FGLocation(const FGColumnVector3& lv);
169
170 /** Copy constructor. */
171 FGLocation(const FGLocation& l);
172
173 /** Set the longitude.
174 @param longitude Longitude in rad to set.
175 Sets the longitude of the location represented with this class instance to
176 the value of the given argument. The value is meant to be in rad. The
177 latitude and the radius value are preserved with this call with the
178 exception of radius being equal to zero. If the radius is previously set
179 to zero it is changed to be equal to 1.0 past this call.
180 Longitude is positive east and negative west.
181 The arguments should be within the bounds of -pi <= lon <= pi.
182 The behavior of this function with arguments outside this range is left as
183 an exercise to the gentle reader ... */
184 void SetLongitude(double longitude);
185
186 /** Set the GEOCENTRIC latitude.
187 @param latitude GEOCENTRIC latitude in rad to set.
188 Sets the latitude of the location represented with this class instance to
189 the value of the given argument. The value is meant to be in rad. The
190 longitude and the radius value are preserved with this call with the
191 exception of radius being equal to zero. If the radius is previously set
192 to zero it is changed to be equal to 1.0 past this call.
193 Latitude is positive north and negative south.
194 The arguments should be within the bounds of -pi/2 <= lat <= pi/2.
195 The behavior of this function with arguments outside this range is left as
196 an exercise to the gentle reader ... */
197 void SetLatitude(double latitude);
198
199 /** Set the distance from the center of the earth.
200 @param radius Radius in ft to set.
201 Sets the radius of the location represented with this class instance to
202 the value of the given argument. The value is meant to be in ft. The
203 latitude and longitude values are preserved with this call with the
204 exception of radius being equal to zero. If the radius is previously set
205 to zero, latitude and longitude is set equal to zero past this call.
206 The argument should be positive.
207 The behavior of this function called with a negative argument is left as
208 an exercise to the gentle reader ... */
209 void SetRadius(double radius);
210
211 /** Sets the longitude, latitude and the distance from the center of the earth.
212 @param lon longitude in radians
213 @param lat GEOCENTRIC latitude in radians
214 @param radius distance from center of earth to vehicle in feet*/
215 void SetPosition(double lon, double lat, double radius);
216
217 /** Sets the longitude, latitude and the distance above the reference spheroid.
218 @param lon longitude in radians
219 @param lat GEODETIC latitude in radians
220 @param height distance above the reference ellipsoid to vehicle in feet*/
221 void SetPositionGeodetic(double lon, double lat, double height);
222
223 /** Sets the semimajor and semiminor axis lengths for this planet.
224 The eccentricity and flattening are calculated from the semimajor
225 and semiminor axis lengths.
226 @param semimajor planet semi-major axis in ft.
227 @param semiminor planet semi-minor axis in ft.*/
228 void SetEllipse(double semimajor, double semiminor);
229
230 /** Get the longitude.
231 @return the longitude in rad of the location represented with this
232 class instance. The returned values are in the range between
233 -pi <= lon <= pi. Longitude is positive east and negative west. */
234 double GetLongitude() const { ComputeDerived(); return mLon; }
235
236 /** Get the longitude.
237 @return the longitude in deg of the location represented with this
238 class instance. The returned values are in the range between
239 -180 <= lon <= 180. Longitude is positive east and negative west. */
240 double GetLongitudeDeg() const { ComputeDerived(); return radtodeg*mLon; }
241
242 /** Get the sine of Longitude. */
243 double GetSinLongitude() const { ComputeDerived(); return -mTec2l(2,1); }
244
245 /** Get the cosine of Longitude. */
246 double GetCosLongitude() const { ComputeDerived(); return mTec2l(2,2); }
247
248 /** Get the GEOCENTRIC latitude in radians.
249 @return the geocentric latitude in rad of the location represented with
250 this class instance. The returned values are in the range between
251 -pi/2 <= lon <= pi/2. Latitude is positive north and negative south. */
252 double GetLatitude() const { ComputeDerived(); return mLat; }
253
254 /** Get the GEODETIC latitude in radians.
255 @return the geodetic latitude in rad of the location represented with this
256 class instance. The returned values are in the range between
257 -pi/2 <= lon <= pi/2. Latitude is positive north and negative south. */
258 double GetGeodLatitudeRad(void) const {
259 assert(mEllipseSet);
260 ComputeDerived(); return mGeodLat;
261 }
262
263 /** Get the GEOCENTRIC latitude in degrees.
264 @return the geocentric latitude in deg of the location represented with
265 this class instance. The returned value is in the range between
266 -90 <= lon <= 90. Latitude is positive north and negative south. */
267 double GetLatitudeDeg() const { ComputeDerived(); return radtodeg*mLat; }
268
269 /** Get the GEODETIC latitude in degrees.
270 @return the geodetic latitude in degrees of the location represented by
271 this class instance. The returned value is in the range between
272 -90 <= lon <= 90. Latitude is positive north and negative south. */
273 double GetGeodLatitudeDeg(void) const {
274 assert(mEllipseSet);
275 ComputeDerived(); return radtodeg*mGeodLat;
276 }
277
278 /** Gets the geodetic altitude in feet. */
279 double GetGeodAltitude(void) const {
280 assert(mEllipseSet);
281 ComputeDerived(); return GeodeticAltitude;
282 }
283
284 /** Get the sea level radius in feet below the current location. */
285 double GetSeaLevelRadius(void) const;
286
287 /** Get the distance from the center of the earth in feet.
288 @return the distance of the location represented with this class
289 instance to the center of the earth in ft. The radius value is
290 always positive. */
291 double GetRadius() const { ComputeDerived(); return mRadius; }
292
293 /** Transform matrix from local horizontal to earth centered frame.
294 @return a const reference to the rotation matrix of the transform from
295 the local horizontal frame to the earth centered frame. */
296 const FGMatrix33& GetTl2ec(void) const { ComputeDerived(); return mTl2ec; }
297
298 /** Transform matrix from the earth centered to local horizontal frame.
299 @return a const reference to the rotation matrix of the transform from
300 the earth centered frame to the local horizontal frame. */
301 const FGMatrix33& GetTec2l(void) const { ComputeDerived(); return mTec2l; }
302
303 /** Get the geodetic distance between the current location and a given
304 location. This corresponds to the shortest distance between the two
305 locations. Earth curvature is taken into account.
306 @param target_longitude the target longitude in radians
307 @param target_latitude the target latitude in radians
308 @return The geodetic distance between the two locations */
309 double GetDistanceTo(double target_longitude, double target_latitude) const;
310
311 /** Get the heading that should be followed from the current location to
312 a given location along the shortest path. Earth curvature is taken into
313 account.
314 @param target_longitude the target longitude in radians
315 @param target_latitude the target latitude in radians
316 @return The heading that should be followed to reach the targeted
317 location along the shortest path */
318 double GetHeadingTo(double target_longitude, double target_latitude) const;
319
320 /** Conversion from Local frame coordinates to a location in the
321 earth centered and fixed frame.
322 This function calculates the FGLocation of an object which position
323 relative to the vehicle is given as in input.
324 @param lvec Vector in the local horizontal coordinate frame
325 @return The location in the earth centered and fixed frame */
326 FGLocation LocalToLocation(const FGColumnVector3& lvec) const {
327 ComputeDerived(); return mTl2ec*lvec + mECLoc;
328 }
329
330 /** Conversion from a location in the earth centered and fixed frame
331 to local horizontal frame coordinates.
332 This function calculates the relative position between the vehicle and
333 the input vector and returns the result expressed in the local frame.
334 @param ecvec Vector in the earth centered and fixed frame
335 @return The vector in the local horizontal coordinate frame */
336 FGColumnVector3 LocationToLocal(const FGColumnVector3& ecvec) const {
337 ComputeDerived(); return mTec2l*(ecvec - mECLoc);
338 }
339
340 // For time-stepping, locations have vector properties...
341
342 /** Read access the entries of the vector.
343 @param idx the component index.
344 Return the value of the matrix entry at the given index.
345 Indices are counted starting with 1.
346 Note that the index given in the argument is unchecked. */
347 double operator()(unsigned int idx) const { return mECLoc.Entry(idx); }
348
349 /** Write access the entries of the vector.
350 @param idx the component index.
351 @return a reference to the vector entry at the given index.
352 Indices are counted starting with 1.
353 Note that the index given in the argument is unchecked. */
354 double& operator()(unsigned int idx) { mCacheValid = false; return mECLoc.Entry(idx); }
355
356 /** Read access the entries of the vector.
357 @param idx the component index.
358 @return the value of the matrix entry at the given index.
359 Indices are counted starting with 1.
360 This function is just a shortcut for the <tt>double
361 operator()(unsigned int idx) const</tt> function. It is
362 used internally to access the elements in a more convenient way.
363 Note that the index given in the argument is unchecked. */
364 double Entry(unsigned int idx) const { return mECLoc.Entry(idx); }
365
366 /** Write access the entries of the vector.
367 @param idx the component index.
368 @return a reference to the vector entry at the given index.
369 Indices are counted starting with 1.
370 This function is just a shortcut for the double&
371 operator()(unsigned int idx) function. It is
372 used internally to access the elements in a more convenient way.
373 Note that the index given in the argument is unchecked. */
374 double& Entry(unsigned int idx) {
375 mCacheValid = false; return mECLoc.Entry(idx);
376 }
377
378 /** Sets this location via the supplied vector.
379 The location can be set by an Earth-centered, Earth-fixed (ECEF) frame
380 position vector. The cache is marked as invalid, so any future requests
381 for selected important data will cause the parameters to be calculated.
382 @param v the ECEF column vector in feet.
383 @return a reference to the FGLocation object. */
384 const FGLocation& operator=(const FGColumnVector3& v)
385 {
386 mECLoc(eX) = v(eX);
387 mECLoc(eY) = v(eY);
388 mECLoc(eZ) = v(eZ);
389 mCacheValid = false;
390 //ComputeDerived();
391 return *this;
392 }
393
394 /** Sets this location via the supplied location object.
395 @param l A location object reference.
396 @return a reference to the FGLocation object. */
397 FGLocation& operator=(const FGLocation& l);
398
399 /** This operator returns true if the ECEF location vectors for the two
400 location objects are equal. */
401 bool operator==(const FGLocation& l) const {
402 return mECLoc == l.mECLoc;
403 }
404
405 /** This operator returns true if the ECEF location vectors for the two
406 location objects are not equal. */
407 bool operator!=(const FGLocation& l) const { return ! operator==(l); }
408
409 /** This operator adds the ECEF position vectors.
410 The cartesian coordinates of the supplied vector (right side) are added to
411 the ECEF position vector on the left side of the equality, and a reference
412 to this object is returned. */
413 const FGLocation& operator+=(const FGLocation &l) {
414 mCacheValid = false;
415 mECLoc += l.mECLoc;
416 return *this;
417 }
418
419 /** This operator substracts the ECEF position vectors.
420 The cartesian coordinates of the supplied vector (right side) are
421 substracted from the ECEF position vector on the left side of the
422 equality, and a reference to this object is returned. */
423 const FGLocation& operator-=(const FGLocation &l) {
424 mCacheValid = false;
425 mECLoc -= l.mECLoc;
426 return *this;
427 }
428
429 /** This operator scales the ECEF position vector.
430 The cartesian coordinates of the ECEF position vector on the left side of
431 the equality are scaled by the supplied value (right side), and a
432 reference to this object is returned. */
433 const FGLocation& operator*=(double scalar) {
434 mCacheValid = false;
435 mECLoc *= scalar;
436 return *this;
437 }
438
439 /** This operator scales the ECEF position vector.
440 The cartesian coordinates of the ECEF position vector on the left side of
441 the equality are scaled by the inverse of the supplied value (right side),
442 and a reference to this object is returned. */
443 const FGLocation& operator/=(double scalar) {
444 return operator*=(1.0/scalar);
445 }
446
447 /** This operator adds two ECEF position vectors.
448 A new object is returned that defines a position which is the sum of the
449 cartesian coordinates of the two positions provided. */
450 FGLocation operator+(const FGLocation& l) const {
451 FGLocation result(mECLoc + l.mECLoc);
452 if (mEllipseSet) result.SetEllipse(a, ec*a);
453 return result;
454 }
455
456 /** This operator substracts two ECEF position vectors.
457 A new object is returned that defines a position which is the difference
458 of the cartesian coordinates of the two positions provided. */
459 FGLocation operator-(const FGLocation& l) const {
460 FGLocation result(mECLoc - l.mECLoc);
461 if (mEllipseSet) result.SetEllipse(a, ec*a);
462 return result;
463 }
464
465 /** This operator scales an ECEF position vector.
466 A new object is returned that defines a position made of the cartesian
467 coordinates of the provided ECEF position scaled by the supplied scalar
468 value. */
469 FGLocation operator*(double scalar) const {
470 FGLocation result(scalar*mECLoc);
471 if (mEllipseSet) result.SetEllipse(a, ec*a);
472 return result;
473 }
474
475 /** Cast to a simple 3d vector */
476 operator const FGColumnVector3&() const {
477 return mECLoc;
478 }
479
480 private:
481 /** Computation of derived values.
482 This function re-computes the derived values like lat/lon and
483 transformation matrices. It does this unconditionally. */
484 void ComputeDerivedUnconditional(void) const;
485
486 /** Computation of derived values.
487 This function checks if the derived values like lat/lon and
488 transformation matrices are already computed. If so, it
489 returns. If they need to be computed this is done here. */
490 void ComputeDerived(void) const {
491 if (!mCacheValid)
492 ComputeDerivedUnconditional();
493 }
494
495 /** The coordinates in the earth centered frame. This is the master copy.
496 The coordinate frame has its center in the middle of the earth.
497 Its x-axis points from the center of the earth towards a
498 location with zero latitude and longitude on the earths
499 surface. The y-axis points from the center of the earth towards a
500 location with zero latitude and 90deg longitude on the earths
501 surface. The z-axis points from the earths center to the
502 geographic north pole.
503 @see W. C. Durham "Aircraft Dynamics & Control", section 2.2 */
504 FGColumnVector3 mECLoc;
505
506 /** The cached lon/lat/radius values. */
507 mutable double mLon;
508 mutable double mLat;
509 mutable double mRadius;
510 mutable double mGeodLat;
511 mutable double GeodeticAltitude;
512
513 /** The cached rotation matrices from and to the associated frames. */
514 mutable FGMatrix33 mTl2ec;
515 mutable FGMatrix33 mTec2l;
516
517 /* Terms for geodetic latitude calculation. Values are from WGS84 model */
518 double a; // Earth semimajor axis in feet
519 double e2; // Earth eccentricity squared
520 double c;
521 double ec;
522 double ec2;
523
524 /** A data validity flag.
525 This class implements caching of the derived values like the
526 orthogonal rotation matrices or the lon/lat/radius values. For caching we
527 carry a flag which signals if the values are valid or not.
528 The C++ keyword "mutable" tells the compiler that the data member is
529 allowed to change during a const member function. */
530 mutable bool mCacheValid;
531 // Flag that checks that geodetic methods are called after SetEllipse() has
532 // been called.
533 bool mEllipseSet = false;
534 };
535
536 /** Scalar multiplication.
537
538 @param scalar scalar value to multiply with.
539 @param l Vector to multiply.
540
541 Multiply the Vector with a scalar value. */
542 inline FGLocation operator*(double scalar, const FGLocation& l)
543 {
544 return l.operator*(scalar);
545 }
546
547 } // namespace JSBSim
548
549 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550 #endif