1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 Author: Jon S. Berndt, Mathias Froehlich
5 Date started: 04/04/2004
7 ------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.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 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 General Public License for more
20 You should have received a copy of the GNU 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 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 "FGPropertyManager.h"
44 #include "FGColumnVector3.h"
45 #include "FGMatrix33.h"
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 #define ID_LOCATION "$Id$"
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63 /** Holds an arbitrary location in the earth centered reference frame.
64 This coordinate frame has its center in the middle of the earth.
65 Its x-axis points from the center of the earth towards a location
66 with zero latitude and longitude on the earths surface. The y-axis
67 points from the center of the earth towards a location with zero
68 latitude and 90deg longitude on the earths surface. The z-axis
69 points from the earths center to the geographic north pole.
71 This class provides access functions to set and get the location as
72 either the simple x, y and z values in ft or longitude/latitude and
73 the radial distance of the location from the earth center.
75 It is common to associate a parent frame with a location. This
76 frame is usually called the local horizontal frame or simply the local
77 frame. This frame has its x/y plane parallel to the surface of the earth
78 (with the assumption of a spherical earth). The x-axis points
79 towards north, the y-axis points towards east and the z-axis
80 points to the center of the earth.
82 Since this frame is determined by the location, this class also
83 provides the rotation matrices required to transform from the
84 earth centered frame to the local horizontal frame and back. There
85 are also conversion functions for conversion of position vectors
86 given in the one frame to positions in the other frame.
88 The earth centered reference frame is *NOT* an inertial frame
89 since it rotates with the earth.
91 The coordinates in the earth centered frame are the master values.
92 All other values are computed from these master values and are
93 cached as long as the location is changed by access through a
94 non-const member function. Values are cached to improve performance.
95 It is best practice to work with a natural set of master values.
96 Other parameters that are derived from these master values are calculated
97 only when needed, and IF they are needed and calculated, then they are
98 cached (stored and remembered) so they do not need to be re-calculated
99 until the master values they are derived from are themselves changed
102 Accuracy and round off:
104 Given that we model a vehicle near the earth, the earths surface
105 radius is about 2*10^7, ft and that we use double values for the
106 representation of the location, we have an accuracy of about
107 1e-16*2e7ft/1=2e-9ft left. This should be sufficient for our needs.
108 Note that this is the same relative accuracy we would have when we
109 compute directly with lon/lat/radius. For the radius value this
110 is clear. For the lon/lat pair this is easy to see. Take for
111 example KSFO located at about 37.61deg north 122.35deg west, which
112 corresponds to 0.65642rad north and 2.13541rad west. Both values
113 are of magnitude of about 1. But 1ft corresponds to about
114 1/(2e7*2*pi)=7.9577e-09rad. So the left accuracy with this
115 representation is also about 1*1e-16/7.9577e-09=1.2566e-08 which
116 is of the same magnitude as the representation chosen here.
118 The advantage of this representation is that it is a linear space
119 without singularities. The singularities are the north and south
120 pole and most notably the non-steady jump at -pi to pi. It is
121 harder to track this jump correctly especially when we need to
122 work with error norms and derivatives of the equations of motion
123 within the time-stepping code. Also, the rate of change is of the
124 same magnitude for all components in this representation which is
125 an advantage for numerical stability in implicit time-stepping too.
127 Note: The latitude is a GEOCENTRIC value. FlightGear
128 converts latitude to a geodetic value and uses that. In order to get best
129 matching relative to a map, geocentric latitude must be converted to geodetic.
131 @see W. C. Durham "Aircraft Dynamics & Control", section 2.2
133 @author Mathias Froehlich
137 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
141 class FGLocation : virtual FGJSBBase
144 /** Default constructor. */
145 FGLocation() { mCacheValid = false; }
147 /** Constructor to set the longitude, latitude and the distance
148 from the center of the earth.
150 @param lat GEOCENTRIC latitude
151 @param distance from center of earth to vehicle in feet*/
152 FGLocation(double lon, double lat, double radius);
154 /** Copy constructor. */
155 FGLocation(const FGColumnVector3& lv)
156 : mECLoc(lv), mCacheValid(false) {}
158 /** Copy constructor. */
159 FGLocation(const FGLocation& l)
160 : mECLoc(l.mECLoc), mCacheValid(l.mCacheValid) {
172 /** Get the longitude.
173 @return the longitude in rad of the location represented with this
174 class instance. The returned values are in the range between
175 -pi <= lon <= pi. Longitude is positive east and negative west. */
176 double GetLongitude() const { ComputeDerived(); return mLon; }
178 /** Get the longitude.
179 @return the longitude in deg of the location represented with this
180 class instance. The returned values are in the range between
181 -180 <= lon <= 180. Longitude is positive east and negative west. */
182 double GetLongitudeDeg() const { ComputeDerived(); return radtodeg*mLon; }
184 /** Set the longitude.
185 @param longitude Longitude in rad to set.
186 Sets the longitude of the location represented with this class
187 instance to the value of the given argument. The value is meant
188 to be in rad. The latitude and the radius value are preserved
189 with this call with the exception of radius being equal to
190 zero. If the radius is previously set to zero it is changed to be
191 equal to 1.0 past this call. Longitude is positive east and negative west. */
192 void SetLongitude(double longitude);
194 /** Get the sine of Longitude. */
195 double GetSinLongitude() const { ComputeDerived(); return -mTec2l(2,1); }
197 /** Get the cosine of Longitude. */
198 double GetCosLongitude() const { ComputeDerived(); return mTec2l(2,2); }
200 /** Get the latitude.
201 @return the latitude in rad of the location represented with this
202 class instance. The returned values are in the range between
203 -pi/2 <= lon <= pi/2. Latitude is positive north and negative south. */
204 double GetLatitude() const { ComputeDerived(); return mLat; }
206 /** Get the latitude.
207 @return the latitude in deg of the location represented with this
208 class instance. The returned values are in the range between
209 -90 <= lon <= 90. Latitude is positive north and negative south. */
210 double GetLatitudeDeg() const { ComputeDerived(); return radtodeg*mLat; }
212 /** Set the latitude.
213 @param latitude Latitude in rad to set.
214 Sets the latitude of the location represented with this class
215 instance to the value of the given argument. The value is meant
216 to be in rad. The longitude and the radius value are preserved
217 with this call with the exception of radius being equal to
218 zero. If the radius is previously set to zero it is changed to be
219 equal to 1.0 past this call.
220 Latitude is positive north and negative south.
221 The arguments should be within the bounds of -pi/2 <= lat <= pi/2.
222 The behavior of this function with arguments outside this range is
223 left as an exercise to the gentle reader ... */
224 void SetLatitude(double latitude);
226 /** Get the sine of Latitude. */
227 double GetSinLatitude() const { ComputeDerived(); return -mTec2l(3,3); }
229 /** Get the cosine of Latitude. */
230 double GetCosLatitude() const { ComputeDerived(); return mTec2l(1,3); }
232 /** Get the cosine of Latitude. */
233 double GetTanLatitude() const {
235 double cLat = mTec2l(1,3);
239 return -mTec2l(3,3)/cLat;
242 /** Get the distance from the center of the earth.
243 @return the distance of the location represented with this class
244 instance to the center of the earth in ft. The radius value is
246 double GetRadius() const { ComputeDerived(); return mRadius; }
248 /** Set the distance from the center of the earth.
249 @param radius Radius in ft to set.
250 Sets the radius of the location represented with this class
251 instance to the value of the given argument. The value is meant
252 to be in ft. The latitude and longitude values are preserved
253 with this call with the exception of radius being equal to
254 zero. If the radius is previously set to zero, latitude and
255 longitude is set equal to zero past this call.
256 The argument should be positive.
257 The behavior of this function called with a negative argument is
258 left as an exercise to the gentle reader ... */
259 void SetRadius(double radius);
261 /** Transform matrix from local horizontal to earth centered frame.
262 Returns a const reference to the rotation matrix of the transform from
263 the local horizontal frame to the earth centered frame. */
264 const FGMatrix33& GetTl2ec(void) const { ComputeDerived(); return mTl2ec; }
266 /** Transform matrix from the earth centered to local horizontal frame.
267 Returns a const reference to the rotation matrix of the transform from
268 the earth centered frame to the local horizontal frame. */
269 const FGMatrix33& GetTec2l(void) const { ComputeDerived(); return mTec2l; }
271 /** Conversion from Local frame coordinates to a location in the
272 earth centered and fixed frame.
273 @parm lvec Vector in the local horizontal coordinate frame
274 @return The location in the earth centered and fixed frame */
275 FGLocation LocalToLocation(const FGColumnVector3& lvec) const {
276 ComputeDerived(); return mTl2ec*lvec + mECLoc;
279 /** Conversion from a location in the earth centered and fixed frame
280 to local horizontal frame coordinates.
281 @parm ecvec Vector in the earth centered and fixed frame
282 @return The vector in the local horizontal coordinate frame */
283 FGColumnVector3 LocationToLocal(const FGColumnVector3& ecvec) const {
284 ComputeDerived(); return mTec2l*(ecvec - mECLoc);
287 // For time-stepping, locations have vector properties...
289 /** Read access the entries of the vector.
290 @param idx the component index.
291 Return the value of the matrix entry at the given index.
292 Indices are counted starting with 1.
293 Note that the index given in the argument is unchecked. */
294 double operator()(unsigned int idx) const { return Entry(idx); }
296 /** Write access the entries of the vector.
297 @param idx the component index.
298 @return a reference to the vector entry at the given index.
299 Indices are counted starting with 1.
300 Note that the index given in the argument is unchecked. */
301 double& operator()(unsigned int idx) { return Entry(idx); }
303 /** Read access the entries of the vector.
304 @param idx the component index.
305 @return the value of the matrix entry at the given index.
306 Indices are counted starting with 1.
307 This function is just a shortcut for the @ref double
308 operator()(unsigned int idx) const function. It is
309 used internally to access the elements in a more convenient way.
310 Note that the index given in the argument is unchecked. */
311 double Entry(unsigned int idx) const { return mECLoc.Entry(idx); }
313 /** Write access the entries of the vector.
314 @param idx the component index.
315 @return a reference to the vector entry at the given index.
316 Indices are counted starting with 1.
317 This function is just a shortcut for the double&
318 operator()(unsigned int idx) function. It is
319 used internally to access the elements in a more convenient way.
320 Note that the index given in the argument is unchecked. */
321 double& Entry(unsigned int idx) {
322 mCacheValid = false; return mECLoc.Entry(idx);
325 const FGLocation& operator=(const FGLocation& l) {
327 mCacheValid = l.mCacheValid;
340 bool operator==(const FGLocation& l) const {
341 return mECLoc == l.mECLoc;
343 bool operator!=(const FGLocation& l) const { return ! operator==(l); }
344 const FGLocation& operator+=(const FGLocation &l) {
349 const FGLocation& operator-=(const FGLocation &l) {
354 const FGLocation& operator*=(double scalar) {
359 const FGLocation& operator/=(double scalar) {
360 return operator*=(1.0/scalar);
362 FGLocation operator+(const FGLocation& l) const {
363 return FGLocation(mECLoc + l.mECLoc);
365 FGLocation operator-(const FGLocation& l) const {
366 return FGLocation(mECLoc - l.mECLoc);
369 FGLocation operator*(double scalar) const {
370 return FGLocation(scalar*mECLoc);
373 /** Cast to a simple 3d vector */
374 operator const FGColumnVector3&() const {
378 /** Ties into the property tree.
379 Ties the variables represented by this class into the property tree. */
380 void bind(FGPropertyManager*, const string&) const;
382 /** Remove from property tree.
383 Unties the variables represented by this class into the property tree. */
384 void unbind(FGPropertyManager*, const string&) const;
387 /** Computation of derived values.
388 This function re-computes the derived values like lat/lon and
389 transformation matrices. It does this unconditionally. */
390 void ComputeDerivedUnconditional(void) const;
392 /** Computation of derived values.
393 This function checks if the derived values like lat/lon and
394 transformation matrices are already computed. If so, it
395 returns. If they need to be computed this is done here. */
396 void ComputeDerived(void) const {
398 ComputeDerivedUnconditional();
401 /** The coordinates in the earth centered frame. This is the master copy.
402 The coordinate frame has its center in the middle of the earth.
403 Its x-axis points from the center of the earth towards a
404 location with zero latitude and longitude on the earths
405 surface. The y-axis points from the center of the earth towards a
406 location with zero latitude and 90deg longitude on the earths
407 surface. The z-axis points from the earths center to the
408 geographic north pole.
409 @see W. C. Durham "Aircraft Dynamics & Control", section 2.2 */
410 FGColumnVector3 mECLoc;
412 /** The cached lon/lat/radius values. */
415 mutable double mRadius;
417 /** The cached rotation matrices from and to the associated frames. */
418 mutable FGMatrix33 mTl2ec;
419 mutable FGMatrix33 mTec2l;
421 /** A data validity flag.
422 This class implements caching of the derived values like the
423 orthogonal rotation matrices or the lon/lat/radius values. For caching we
424 carry a flag which signals if the values are valid or not.
425 The C++ keyword "mutable" tells the compiler that the data member is
426 allowed to change during a const member function. */
427 mutable bool mCacheValid;
430 /** Scalar multiplication.
432 @param scalar scalar value to multiply with.
433 @param l Vector to multiply.
435 Multiply the Vector with a scalar value. */
436 inline FGLocation operator*(double scalar, const FGLocation& l)
438 return l.operator*(scalar);
441 } // namespace JSBSim
443 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%