]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGLocation.h
Fix the tank properties if no content was defined in fg
[flightgear.git] / src / FDM / JSBSim / math / FGLocation.h
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 (jon@jsbsim.org) ------------------
8  -------           (C) 2004  Mathias Froehlich (Mathias.Froehlich@web.de) ----
9
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
13  version.
14
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
18  details.
19
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.
23
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.
26
27 HISTORY
28 -------------------------------------------------------------------------------
29 04/04/2004   MF   Created from code previously in the old positions class.
30
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 SENTRY
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
34
35 #ifndef FGLOCATION_H
36 #define FGLOCATION_H
37
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 INCLUDES
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41
42 #include "FGJSBBase.h"
43 #include "input_output/FGPropertyManager.h"
44 #include "FGColumnVector3.h"
45 #include "FGMatrix33.h"
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 DEFINITIONS
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 #define ID_LOCATION "$Id: FGLocation.h,v 1.25 2010/09/18 22:47:24 jberndt Exp $"
52
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 FORWARD DECLARATIONS
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56
57 namespace JSBSim {
58
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 CLASS DOCUMENTATION
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62
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.
70
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.
74
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.
81
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.
93
94     The Earth centered reference frame is NOT an inertial frame since it rotates
95     with the Earth.
96
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).
106
107     Accuracy and round off
108
109     Given,
110
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
114     
115     we have an accuracy of about
116     
117     1e-16*2e7ft/1 = 2e-9 ft
118     
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.
128
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.
136
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.
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     @version $Id: FGLocation.h,v 1.25 2010/09/18 22:47:24 jberndt Exp $
146   */
147
148 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 CLASS DECLARATION
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
151
152 class FGLocation : virtual FGJSBBase
153 {
154 public:
155   /** Default constructor. */
156   FGLocation(void);
157
158   /** Constructor to set the longitude, latitude and the distance
159       from the center of the earth.
160       @param lon longitude
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);
164
165   /** Column constructor. */
166   FGLocation(const FGColumnVector3& lv);
167
168   /** Copy constructor. */
169   FGLocation(const FGLocation& l);
170
171   /** Set the longitude.
172       @param longitude Longitude in rad to set.
173       Sets the longitude of the location represented with this class
174       instance to the value of the given argument. The value is meant
175       to be in rad. The latitude and the radius value are preserved
176       with this call with the exception of radius being equal to
177       zero. If the radius is previously set to zero it is changed to be
178       equal to 1.0 past this call. Longitude is positive east and negative west. */
179   void SetLongitude(double longitude);
180
181   /** Set the latitude.
182       @param latitude Latitude in rad to set.
183       Sets the latitude of the location represented with this class
184       instance to the value of the given argument. The value is meant
185       to be in rad. The longitude and the radius value are preserved
186       with this call with the exception of radius being equal to
187       zero. If the radius is previously set to zero it is changed to be
188       equal to 1.0 past this call.
189       Latitude is positive north and negative south.
190       The arguments should be within the bounds of -pi/2 <= lat <= pi/2.
191       The behavior of this function with arguments outside this range is
192       left as an exercise to the gentle reader ... */
193   void SetLatitude(double latitude);
194
195   /** Set the distance from the center of the earth.
196       @param radius Radius in ft to set.
197       Sets the radius of the location represented with this class
198       instance to the value of the given argument. The value is meant
199       to be in ft. The latitude and longitude values are preserved
200       with this call with the exception of radius being equal to
201       zero. If the radius is previously set to zero, latitude and
202       longitude is set equal to zero past this call.
203       The argument should be positive.
204       The behavior of this function called with a negative argument is
205       left as an exercise to the gentle reader ... */
206   void SetRadius(double radius);
207
208   /** Sets the longitude, latitude and the distance from the center of the earth.
209       @param lon longitude in radians
210       @param lat GEOCENTRIC latitude in radians
211       @param radius distance from center of earth to vehicle in feet*/
212   void SetPosition(double lon, double lat, double radius);
213
214   /** Sets the longitude, latitude and the distance above the reference ellipsoid.
215       @param lon longitude in radians
216       @param lat GEODETIC latitude in radians
217       @param height distance above the reference ellipsoid to vehicle in feet*/
218   void SetPositionGeodetic(double lon, double lat, double height);
219
220   /** Sets the semimajor and semiminor axis lengths for this planet.
221       The eccentricity and flattening are calculated from the semimajor
222       and semiminor axis lengths */
223   void SetEllipse(double semimajor, double semiminor);
224
225   /** Sets the Earth position angle.
226       This is the relative orientation of the ECEF frame with respect to the
227       Inertial frame.
228       @param EPA Earth fixed frame (ECEF) rotation offset about the axis with
229                  respect to the Inertial (ECI) frame in radians. */
230   void SetEarthPositionAngle(double EPA) {epa = EPA; mCacheValid = false;}
231
232   /** Get the longitude.
233       @return the longitude in rad of the location represented with this
234       class instance. The returned values are in the range between
235       -pi <= lon <= pi. Longitude is positive east and negative west. */
236   double GetLongitude() const { ComputeDerived(); return mLon; }
237
238   /** Get the longitude.
239       @return the longitude in deg of the location represented with this
240       class instance. The returned values are in the range between
241       -180 <= lon <= 180.  Longitude is positive east and negative west. */
242   double GetLongitudeDeg() const { ComputeDerived(); return radtodeg*mLon; }
243
244   /** Get the sine of Longitude. */
245   double GetSinLongitude() const { ComputeDerived(); return -mTec2l(2,1); }
246
247   /** Get the cosine of Longitude. */
248   double GetCosLongitude() const { ComputeDerived(); return mTec2l(2,2); }
249
250   /** Get the latitude.
251       @return the latitude in rad of the location represented with this
252       class instance. The returned values are in the range between
253       -pi/2 <= lon <= pi/2. Latitude is positive north and negative south. */
254   double GetLatitude() const { ComputeDerived(); return mLat; }
255
256   /** Get the geodetic latitude.
257       @return the geodetic latitude in rad of the location represented with this
258       class instance. The returned values are in the range between
259       -pi/2 <= lon <= pi/2. Latitude is positive north and negative south. */
260   double GetGeodLatitudeRad(void) const { ComputeDerived(); return mGeodLat; }
261
262   /** Get the latitude.
263       @return the latitude in deg of the location represented with this
264       class instance. The returned value is in the range between
265       -90 <= lon <= 90. Latitude is positive north and negative south. */
266   double GetLatitudeDeg() const { ComputeDerived(); return radtodeg*mLat; }
267
268   /** Get the geodetic latitude in degrees.
269       @return the geodetic latitude in degrees of the location represented by
270       this class instance. The returned value is in the range between
271       -90 <= lon <= 90. Latitude is positive north and negative south. */
272   double GetGeodLatitudeDeg(void) const { ComputeDerived(); return radtodeg*mGeodLat; }
273
274   /** Gets the geodetic altitude in feet. */
275   double GetGeodAltitude(void) const {ComputeDerived(); return GeodeticAltitude;}
276
277   /** Get the sine of Latitude. */
278   double GetSinLatitude() const { ComputeDerived(); return -mTec2l(3,3); }
279
280   /** Get the cosine of Latitude. */
281   double GetCosLatitude() const { ComputeDerived(); return mTec2l(1,3); }
282
283   /** Get the cosine of Latitude. */
284   double GetTanLatitude() const {
285     ComputeDerived();
286     double cLat = mTec2l(1,3);
287     if (cLat == 0.0)
288       return 0.0;
289     else
290       return -mTec2l(3,3)/cLat;
291   }
292
293   /** Get the distance from the center of the earth.
294       @return the distance of the location represented with this class
295       instance to the center of the earth in ft. The radius value is
296       always positive. */
297   //double GetRadius() const { return mECLoc.Magnitude(); } // may not work with FlightGear
298   double GetRadius() const { ComputeDerived(); return mRadius; }
299
300   /** Transform matrix from local horizontal to earth centered frame.
301       Returns a const reference to the rotation matrix of the transform from
302       the local horizontal frame to the earth centered frame. */
303   const FGMatrix33& GetTl2ec(void) const { ComputeDerived(); return mTl2ec; }
304
305   /** Transform matrix from the earth centered to local horizontal frame.
306       Returns a const reference to the rotation matrix of the transform from
307       the earth centered frame to the local horizontal frame. */
308   const FGMatrix33& GetTec2l(void) const { ComputeDerived(); return mTec2l; }
309
310   /** Transform matrix from inertial to earth centered frame.
311       Returns a const reference to the rotation matrix of the transform from
312       the inertial frame to the earth centered frame (ECI to ECEF). */
313   const FGMatrix33& GetTi2ec(void);
314
315   /** Transform matrix from the earth centered to inertial frame.
316       Returns a const reference to the rotation matrix of the transform from
317       the earth centered frame to the inertial frame (ECEF to ECI). */
318   const FGMatrix33& GetTec2i(void);
319
320   const FGMatrix33& GetTi2l(void) const {ComputeDerived(); return mTi2l;}
321
322   const FGMatrix33& GetTl2i(void) const {ComputeDerived(); return mTl2i;}
323
324   /** Conversion from Local frame coordinates to a location in the
325       earth centered and fixed frame.
326       @param lvec Vector in the local horizontal coordinate frame
327       @return The location in the earth centered and fixed frame */
328   FGLocation LocalToLocation(const FGColumnVector3& lvec) const {
329     ComputeDerived(); return mTl2ec*lvec + mECLoc;
330   }
331
332   /** Conversion from a location in the earth centered and fixed frame
333       to local horizontal frame coordinates.
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 v A location object reference. 
396       @return a reference to the FGLocation object. */
397   const 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 supplied vector (right side) is added to the ECEF position vector
411       on the left side of the equality, and a pointer to this object is
412       returned. */
413   const FGLocation& operator+=(const FGLocation &l) {
414     mCacheValid = false;
415     mECLoc += l.mECLoc;
416     return *this;
417   }
418
419   const FGLocation& operator-=(const FGLocation &l) {
420     mCacheValid = false;
421     mECLoc -= l.mECLoc;
422     return *this;
423   }
424
425   const FGLocation& operator*=(double scalar) {
426     mCacheValid = false;
427     mECLoc *= scalar;
428     return *this;
429   }
430
431   const FGLocation& operator/=(double scalar) {
432     return operator*=(1.0/scalar);
433   }
434
435   FGLocation operator+(const FGLocation& l) const {
436     return FGLocation(mECLoc + l.mECLoc);
437   }
438
439   FGLocation operator-(const FGLocation& l) const {
440     return FGLocation(mECLoc - l.mECLoc);
441   }
442
443   FGLocation operator*(double scalar) const {
444     return FGLocation(scalar*mECLoc);
445   }
446
447   /** Cast to a simple 3d vector */
448   operator const FGColumnVector3&() const {
449     return mECLoc;
450   }
451
452 private:
453   /** Computation of derived values.
454       This function re-computes the derived values like lat/lon and
455       transformation matrices. It does this unconditionally. */
456   void ComputeDerivedUnconditional(void) const;
457
458   /** Computation of derived values.
459       This function checks if the derived values like lat/lon and
460       transformation matrices are already computed. If so, it
461       returns. If they need to be computed this is done here. */
462   void ComputeDerived(void) const {
463     if (!mCacheValid)
464       ComputeDerivedUnconditional();
465   }
466
467   /** The coordinates in the earth centered frame. This is the master copy.
468       The coordinate frame has its center in the middle of the earth.
469       Its x-axis points from the center of the earth towards a
470       location with zero latitude and longitude on the earths
471       surface. The y-axis points from the center of the earth towards a
472       location with zero latitude and 90deg longitude on the earths
473       surface. The z-axis points from the earths center to the
474       geographic north pole.
475       @see W. C. Durham "Aircraft Dynamics & Control", section 2.2 */
476   FGColumnVector3 mECLoc;
477
478   /** The cached lon/lat/radius values. */
479   mutable double mLon;
480   mutable double mLat;
481   mutable double mRadius;
482   mutable double mGeodLat;
483   mutable double GeodeticAltitude;
484   
485   double initial_longitude;
486
487   /** The cached rotation matrices from and to the associated frames. */
488   mutable FGMatrix33 mTl2ec;
489   mutable FGMatrix33 mTec2l;
490   mutable FGMatrix33 mTi2ec;
491   mutable FGMatrix33 mTec2i;
492   mutable FGMatrix33 mTi2l;
493   mutable FGMatrix33 mTl2i;
494
495   double epa;
496
497   /* Terms for geodetic latitude calculation. Values are from WGS84 model */
498   double a;    // Earth semimajor axis in feet (6,378,137.0 meters)
499   double b;    // Earth semiminor axis in feet (6,356,752.3142 meters)
500   double a2;
501   double b2;
502   double e;    // Earth eccentricity
503   double e2;   // Earth eccentricity squared
504   double eps2; //
505   double f;    // Flattening
506
507   /** A data validity flag.
508       This class implements caching of the derived values like the
509       orthogonal rotation matrices or the lon/lat/radius values. For caching we
510       carry a flag which signals if the values are valid or not.
511       The C++ keyword "mutable" tells the compiler that the data member is
512       allowed to change during a const member function. */
513   mutable bool mCacheValid;
514 };
515
516 /** Scalar multiplication.
517
518     @param scalar scalar value to multiply with.
519     @param l Vector to multiply.
520
521     Multiply the Vector with a scalar value. */
522 inline FGLocation operator*(double scalar, const FGLocation& l)
523 {
524   return l.operator*(scalar);
525 }
526
527 } // namespace JSBSim
528
529 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530 #endif