1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 Author: Jon S. Berndt, Mathias Froehlich
5 Date started: 04/04/2004
7 ------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) ------------------
8 ------- (C) 2004 Mathias Froehlich (Mathias.Froehlich@web.de) ----
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20 You should have received a copy of the GNU Lesser General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU Lesser General Public License can also be found on
25 the world wide web at http://www.gnu.org.
28 -------------------------------------------------------------------------------
29 04/04/2004 MF Created from code previously in the old positions class.
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 #include "FGJSBBase.h"
43 #include "input_output/FGPropertyManager.h"
44 #include "FGColumnVector3.h"
45 #include "FGMatrix33.h"
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 #define ID_LOCATION "$Id: FGLocation.h,v 1.21 2010/07/09 04:11:45 jberndt Exp $"
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63 /** FGLocation holds an arbitrary location in the Earth centered Earth fixed
64 reference frame (ECEF). This coordinate frame has its center in the middle
65 of the earth. The X-axis points from the center of the Earth towards a
66 location with zero latitude and longitude on the Earth surface. The Y-axis
67 points from the center of the Earth towards a location with zero latitude
68 and 90 deg East longitude on the Earth surface. The Z-axis points from the
69 Earth center to the geographic north pole.
71 This class provides access functions to set and get the location as either
72 the simple X, Y and Z values in ft or longitude/latitude and the radial
73 distance of the location from the Earth center.
75 It is common to associate a parent frame with a location. This frame is
76 usually called the local horizontal frame or simply the local frame. It is
77 also called the NED frame (North, East, Down), as well as the Navigation
78 frame. This frame has its X/Y plane parallel to the surface of the Earth
79 (with the assumption of a spherical Earth). The X-axis points towards north,
80 the Y-axis points east and the Z-axis points to the center of the Earth.
82 Since the local frame is determined by the location (and NOT by the
83 orientation of the vehicle IN any frame), this class also provides the
84 rotation matrices required to transform from the Earth centered (ECEF) frame
85 to the local horizontal frame and back. This class also "owns" the
86 transformations that go from the inertial frame (Earth-centered Inertial, or
87 ECI) to and from the ECEF frame, as well as to and from the local frame.
88 Again, this is because the ECI, ECEF, and local frames do not involve the
89 actual orientation of the vehicle - only the location on the Earth surface,
90 and the angular difference between the ECI and ECEF frames. There are
91 conversion functions for conversion of position vectors given in the one
92 frame to positions in the other frame.
94 The Earth centered reference frame is NOT an inertial frame since it rotates
97 The coordinates in the Earth centered frame are the master values. All other
98 values are computed from these master values and are cached as long as the
99 location is changed by access through a non-const member function. Values
100 are cached to improve performance. It is best practice to work with a
101 natural set of master values. Other parameters that are derived from these
102 master values are calculated only when needed, and IF they are needed and
103 calculated, then they are cached (stored and remembered) so they do not need
104 to be re-calculated until the master values they are derived from are
105 themselves changed (and become stale).
107 Accuracy and round off
111 -that we model a vehicle near the Earth
112 -that the Earth surface radius is about 2*10^7, ft
113 -that we use double values for the representation of the location
115 we have an accuracy of about
117 1e-16*2e7ft/1 = 2e-9 ft
119 left. This should be sufficient for our needs. Note that this is the same
120 relative accuracy we would have when we compute directly with
121 lon/lat/radius. For the radius value this is clear. For the lon/lat pair
122 this is easy to see. Take for example KSFO located at about 37.61 deg north
123 122.35 deg west, which corresponds to 0.65642 rad north and 2.13541 rad
124 west. Both values are of magnitude of about 1. But 1 ft corresponds to about
125 1/(2e7*2*pi) = 7.9577e-09 rad. So the left accuracy with this representation
126 is also about 1*1e-16/7.9577e-09 = 1.2566e-08 which is of the same magnitude
127 as the representation chosen here.
129 The advantage of this representation is that it is a linear space without
130 singularities. The singularities are the north and south pole and most
131 notably the non-steady jump at -pi to pi. It is harder to track this jump
132 correctly especially when we need to work with error norms and derivatives
133 of the equations of motion within the time-stepping code. Also, the rate of
134 change is of the same magnitude for all components in this representation
135 which is an advantage for numerical stability in implicit time-stepping.
137 Note: The latitude is a GEOCENTRIC value. FlightGear converts latitude to a
138 geodetic value and uses that. In order to get best matching relative to a
139 map, geocentric latitude must be converted to geodetic.
141 @see Stevens and Lewis, "Aircraft Control and Simulation", Second edition
142 @see W. C. Durham "Aircraft Dynamics & Control", section 2.2
144 @author Mathias Froehlich
145 @version $Id: FGLocation.h,v 1.21 2010/07/09 04:11:45 jberndt Exp $
148 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
152 class FGLocation : virtual FGJSBBase
155 /** Default constructor. */
158 /** Constructor to set the longitude, latitude and the distance
159 from the center of the earth.
161 @param lat GEOCENTRIC latitude
162 @param radius distance from center of earth to vehicle in feet*/
163 FGLocation(double lon, double lat, double radius);
165 /** Column constructor. */
166 FGLocation(const FGColumnVector3& lv) : mECLoc(lv), mCacheValid(false)
178 /** Copy constructor. */
179 FGLocation(const FGLocation& l)
180 : mECLoc(l.mECLoc), mCacheValid(l.mCacheValid)
182 // if (!mCacheValid) return; // This doesn't seem right.
200 initial_longitude = l.initial_longitude;
203 /** Set the longitude.
204 @param longitude Longitude in rad to set.
205 Sets the longitude of the location represented with this class
206 instance to the value of the given argument. The value is meant
207 to be in rad. The latitude and the radius value are preserved
208 with this call with the exception of radius being equal to
209 zero. If the radius is previously set to zero it is changed to be
210 equal to 1.0 past this call. Longitude is positive east and negative west. */
211 void SetLongitude(double longitude);
213 /** Set the latitude.
214 @param latitude Latitude in rad to set.
215 Sets the latitude of the location represented with this class
216 instance to the value of the given argument. The value is meant
217 to be in rad. The longitude and the radius value are preserved
218 with this call with the exception of radius being equal to
219 zero. If the radius is previously set to zero it is changed to be
220 equal to 1.0 past this call.
221 Latitude is positive north and negative south.
222 The arguments should be within the bounds of -pi/2 <= lat <= pi/2.
223 The behavior of this function with arguments outside this range is
224 left as an exercise to the gentle reader ... */
225 void SetLatitude(double latitude);
227 /** Set the distance from the center of the earth.
228 @param radius Radius in ft to set.
229 Sets the radius of the location represented with this class
230 instance to the value of the given argument. The value is meant
231 to be in ft. The latitude and longitude values are preserved
232 with this call with the exception of radius being equal to
233 zero. If the radius is previously set to zero, latitude and
234 longitude is set equal to zero past this call.
235 The argument should be positive.
236 The behavior of this function called with a negative argument is
237 left as an exercise to the gentle reader ... */
238 void SetRadius(double radius);
240 /** Sets the longitude, latitude and the distance from the center of the earth.
241 @param lon longitude in radians
242 @param lat GEOCENTRIC latitude in radians
243 @param radius distance from center of earth to vehicle in feet*/
244 void SetPosition(double lon, double lat, double radius);
246 /** Sets the longitude, latitude and the distance above the reference ellipsoid.
247 @param lon longitude in radians
248 @param lat GEODETIC latitude in radians
249 @param height distance above the reference ellipsoid to vehicle in feet*/
250 void SetPositionGeodetic(double lon, double lat, double height);
252 /** Sets the semimajor and semiminor axis lengths for this planet.
253 The eccentricity and flattening are calculated from the semimajor
254 and semiminor axis lengths */
255 void SetEllipse(double semimajor, double semiminor);
257 /** Sets the Earth position angle.
258 This is the relative orientation of the ECEF frame with respect to the
260 @param EPA Earth fixed frame (ECEF) rotation offset about the axis with
261 respect to the Inertial (ECI) frame in radians. */
262 void SetEarthPositionAngle(double EPA) {epa = EPA; /*mCacheValid = false;*/ ComputeDerived();}
264 /** Get the longitude.
265 @return the longitude in rad of the location represented with this
266 class instance. The returned values are in the range between
267 -pi <= lon <= pi. Longitude is positive east and negative west. */
268 double GetLongitude() const { ComputeDerived(); return mLon; }
270 /** Get the longitude.
271 @return the longitude in deg of the location represented with this
272 class instance. The returned values are in the range between
273 -180 <= lon <= 180. Longitude is positive east and negative west. */
274 double GetLongitudeDeg() const { ComputeDerived(); return radtodeg*mLon; }
276 /** Get the sine of Longitude. */
277 double GetSinLongitude() const { ComputeDerived(); return -mTec2l(2,1); }
279 /** Get the cosine of Longitude. */
280 double GetCosLongitude() const { ComputeDerived(); return mTec2l(2,2); }
282 /** Get the latitude.
283 @return the latitude in rad of the location represented with this
284 class instance. The returned values are in the range between
285 -pi/2 <= lon <= pi/2. Latitude is positive north and negative south. */
286 double GetLatitude() const { ComputeDerived(); return mLat; }
288 /** Get the geodetic latitude.
289 @return the geodetic latitude in rad of the location represented with this
290 class instance. The returned values are in the range between
291 -pi/2 <= lon <= pi/2. Latitude is positive north and negative south. */
292 double GetGeodLatitudeRad(void) const { ComputeDerived(); return mGeodLat; }
294 /** Get the latitude.
295 @return the latitude in deg of the location represented with this
296 class instance. The returned value is in the range between
297 -90 <= lon <= 90. Latitude is positive north and negative south. */
298 double GetLatitudeDeg() const { ComputeDerived(); return radtodeg*mLat; }
300 /** Get the geodetic latitude in degrees.
301 @return the geodetic latitude in degrees of the location represented by
302 this class instance. The returned value is in the range between
303 -90 <= lon <= 90. Latitude is positive north and negative south. */
304 double GetGeodLatitudeDeg(void) const { ComputeDerived(); return radtodeg*mGeodLat; }
306 /** Gets the geodetic altitude in feet. */
307 double GetGeodAltitude(void) const { return GeodeticAltitude;}
309 /** Get the sine of Latitude. */
310 double GetSinLatitude() const { ComputeDerived(); return -mTec2l(3,3); }
312 /** Get the cosine of Latitude. */
313 double GetCosLatitude() const { ComputeDerived(); return mTec2l(1,3); }
315 /** Get the cosine of Latitude. */
316 double GetTanLatitude() const {
318 double cLat = mTec2l(1,3);
322 return -mTec2l(3,3)/cLat;
325 /** Get the distance from the center of the earth.
326 @return the distance of the location represented with this class
327 instance to the center of the earth in ft. The radius value is
329 double GetRadius() const { return mECLoc.Magnitude(); }
331 /** Transform matrix from local horizontal to earth centered frame.
332 Returns a const reference to the rotation matrix of the transform from
333 the local horizontal frame to the earth centered frame. */
334 const FGMatrix33& GetTl2ec(void) const { ComputeDerived(); return mTl2ec; }
336 /** Transform matrix from the earth centered to local horizontal frame.
337 Returns a const reference to the rotation matrix of the transform from
338 the earth centered frame to the local horizontal frame. */
339 const FGMatrix33& GetTec2l(void) const { ComputeDerived(); return mTec2l; }
341 /** Transform matrix from inertial to earth centered frame.
342 Returns a const reference to the rotation matrix of the transform from
343 the inertial frame to the earth centered frame (ECI to ECEF). */
344 const FGMatrix33& GetTi2ec(void);
346 /** Transform matrix from the earth centered to inertial frame.
347 Returns a const reference to the rotation matrix of the transform from
348 the earth centered frame to the inertial frame (ECEF to ECI). */
349 const FGMatrix33& GetTec2i(void);
351 const FGMatrix33& GetTi2l(void) const {return mTi2l;}
353 const FGMatrix33& GetTl2i(void) const {return mTl2i;}
355 /** Conversion from Local frame coordinates to a location in the
356 earth centered and fixed frame.
357 @param lvec Vector in the local horizontal coordinate frame
358 @return The location in the earth centered and fixed frame */
359 FGLocation LocalToLocation(const FGColumnVector3& lvec) const {
360 ComputeDerived(); return mTl2ec*lvec + mECLoc;
363 /** Conversion from a location in the earth centered and fixed frame
364 to local horizontal frame coordinates.
365 @param ecvec Vector in the earth centered and fixed frame
366 @return The vector in the local horizontal coordinate frame */
367 FGColumnVector3 LocationToLocal(const FGColumnVector3& ecvec) const {
368 ComputeDerived(); return mTec2l*(ecvec - mECLoc);
371 // For time-stepping, locations have vector properties...
373 /** Read access the entries of the vector.
374 @param idx the component index.
375 Return the value of the matrix entry at the given index.
376 Indices are counted starting with 1.
377 Note that the index given in the argument is unchecked. */
378 double operator()(unsigned int idx) const { return mECLoc.Entry(idx); }
380 /** Write access the entries of the vector.
381 @param idx the component index.
382 @return a reference to the vector entry at the given index.
383 Indices are counted starting with 1.
384 Note that the index given in the argument is unchecked. */
385 double& operator()(unsigned int idx) { mCacheValid = false; return mECLoc.Entry(idx); }
387 /** Read access the entries of the vector.
388 @param idx the component index.
389 @return the value of the matrix entry at the given index.
390 Indices are counted starting with 1.
391 This function is just a shortcut for the <tt>double
392 operator()(unsigned int idx) const</tt> function. It is
393 used internally to access the elements in a more convenient way.
394 Note that the index given in the argument is unchecked. */
395 double Entry(unsigned int idx) const { return mECLoc.Entry(idx); }
397 /** Write access the entries of the vector.
398 @param idx the component index.
399 @return a reference to the vector entry at the given index.
400 Indices are counted starting with 1.
401 This function is just a shortcut for the double&
402 operator()(unsigned int idx) function. It is
403 used internally to access the elements in a more convenient way.
404 Note that the index given in the argument is unchecked. */
405 double& Entry(unsigned int idx) {
406 mCacheValid = false; return mECLoc.Entry(idx);
409 /** Sets this location via the supplied vector.
410 The location can be set by an Earth-centered, Earth-fixed (ECEF) frame
411 position vector. The cache is marked as invalid, so any future requests
412 for selected important data will cause the parameters to be calculated.
413 @param v the ECEF column vector in feet.
414 @return a reference to the FGLocation object. */
415 const FGLocation& operator=(const FGColumnVector3& v)
425 /** Sets this location via the supplied location object.
426 @param v A location object reference.
427 @return a reference to the FGLocation object. */
428 const FGLocation& operator=(const FGLocation& l)
431 mCacheValid = l.mCacheValid;
433 // if (!mCacheValid) return *this; // Why is this here for an assignment operator?
451 initial_longitude = l.initial_longitude;
456 /** This operator returns true if the ECEF location vectors for the two
457 location objects are equal. */
458 bool operator==(const FGLocation& l) const {
459 return mECLoc == l.mECLoc;
462 /** This operator returns true if the ECEF location vectors for the two
463 location objects are not equal. */
464 bool operator!=(const FGLocation& l) const { return ! operator==(l); }
466 /** This operator adds the ECEF position vectors.
467 The supplied vector (right side) is added to the ECEF position vector
468 on the left side of the equality, and a pointer to this object is
470 const FGLocation& operator+=(const FGLocation &l) {
476 const FGLocation& operator-=(const FGLocation &l) {
482 const FGLocation& operator*=(double scalar) {
488 const FGLocation& operator/=(double scalar) {
489 return operator*=(1.0/scalar);
492 FGLocation operator+(const FGLocation& l) const {
493 return FGLocation(mECLoc + l.mECLoc);
496 FGLocation operator-(const FGLocation& l) const {
497 return FGLocation(mECLoc - l.mECLoc);
500 FGLocation operator*(double scalar) const {
501 return FGLocation(scalar*mECLoc);
504 /** Cast to a simple 3d vector */
505 operator const FGColumnVector3&() const {
510 /** Computation of derived values.
511 This function re-computes the derived values like lat/lon and
512 transformation matrices. It does this unconditionally. */
513 void ComputeDerivedUnconditional(void) const;
515 /** Computation of derived values.
516 This function checks if the derived values like lat/lon and
517 transformation matrices are already computed. If so, it
518 returns. If they need to be computed this is done here. */
519 void ComputeDerived(void) const {
521 ComputeDerivedUnconditional();
524 /** The coordinates in the earth centered frame. This is the master copy.
525 The coordinate frame has its center in the middle of the earth.
526 Its x-axis points from the center of the earth towards a
527 location with zero latitude and longitude on the earths
528 surface. The y-axis points from the center of the earth towards a
529 location with zero latitude and 90deg longitude on the earths
530 surface. The z-axis points from the earths center to the
531 geographic north pole.
532 @see W. C. Durham "Aircraft Dynamics & Control", section 2.2 */
533 FGColumnVector3 mECLoc;
535 /** The cached lon/lat/radius values. */
538 mutable double mRadius;
539 mutable double mGeodLat;
540 mutable double GeodeticAltitude;
542 double initial_longitude;
544 /** The cached rotation matrices from and to the associated frames. */
545 mutable FGMatrix33 mTl2ec;
546 mutable FGMatrix33 mTec2l;
547 mutable FGMatrix33 mTi2ec;
548 mutable FGMatrix33 mTec2i;
549 mutable FGMatrix33 mTi2l;
550 mutable FGMatrix33 mTl2i;
554 /* Terms for geodetic latitude calculation. Values are from WGS84 model */
555 double a; // Earth semimajor axis in feet (6,378,137.0 meters)
556 double b; // Earth semiminor axis in feet (6,356,752.3142 meters)
559 double e; // Earth eccentricity
560 double e2; // Earth eccentricity squared
562 double f; // Flattening
564 /** A data validity flag.
565 This class implements caching of the derived values like the
566 orthogonal rotation matrices or the lon/lat/radius values. For caching we
567 carry a flag which signals if the values are valid or not.
568 The C++ keyword "mutable" tells the compiler that the data member is
569 allowed to change during a const member function. */
570 mutable bool mCacheValid;
573 /** Scalar multiplication.
575 @param scalar scalar value to multiply with.
576 @param l Vector to multiply.
578 Multiply the Vector with a scalar value. */
579 inline FGLocation operator*(double scalar, const FGLocation& l)
581 return l.operator*(scalar);
584 } // namespace JSBSim
586 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%