]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGLocation.cpp
Frederic Bouvier:
[flightgear.git] / src / FDM / JSBSim / FGLocation.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGLocation.cpp
4  Author:       Jon S. Berndt
5  Date started: 04/04/2004
6  Purpose:      Store an arbitrary location on the globe
7
8  ------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) ------------------
9  -------           (C) 2004  Mathias Froehlich (Mathias.Froehlich@web.de) ----
10
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  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 General Public License for more
19  details.
20
21  You should have received a copy of the GNU General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24
25  Further information about the GNU General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 ------------------------------------------------------------------------------
30 This class encapsulates an arbitrary position in the globe with its accessors.
31 It has vector properties, so you can add multiply ....
32
33 HISTORY
34 ------------------------------------------------------------------------------
35 04/04/2004   MF    Created
36
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41 #ifdef FGFS
42 #  include <simgear/compiler.h>
43 #  ifdef SG_HAVE_STD_INCLUDES
44 #    include <cmath>
45 #  else
46 #    include <math.h>
47 #  endif
48 #else
49 #  if defined(sgi) && !defined(__GNUC__)
50 #    include <math.h>
51 #  else
52 #    include <cmath>
53 #  endif
54 #endif
55
56 #include "FGLocation.h"
57 #include "FGPropertyManager.h"
58
59 namespace JSBSim {
60
61 static const char *IdSrc = "$Id$";
62 static const char *IdHdr = ID_LOCATION;
63
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 CLASS IMPLEMENTATION
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67
68 FGLocation::FGLocation(double lon, double lat, double radius)
69 {
70   mCacheValid = false;
71
72   double sinLat = sin(lat);
73   double cosLat = cos(lat);
74   double sinLon = sin(lon);
75   double cosLon = cos(lon);
76   mECLoc = FGColumnVector3( radius*cosLat*cosLon,
77                             radius*cosLat*sinLon,
78                             radius*sinLat );
79 }
80
81 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82
83 void FGLocation::SetLongitude(double longitude)
84 {
85   double rtmp = sqrt(mECLoc(eX)*mECLoc(eX) + mECLoc(eY)*mECLoc(eY));
86   // Fast return if we are on the north or south pole ...
87   if (rtmp == 0.0)
88     return;
89
90   mCacheValid = false;
91
92   mECLoc(eX) = rtmp*sin(longitude);
93   mECLoc(eY) = rtmp*cos(longitude);
94 }
95
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97
98 void FGLocation::SetLatitude(double latitude)
99 {
100   mCacheValid = false;
101
102   double r = mECLoc.Magnitude();
103   if (r == 0.0) {
104     mECLoc(eX) = 1.0;
105     r = 1.0;
106   }
107
108   double rtmp = sqrt(mECLoc(eX)*mECLoc(eX) + mECLoc(eY)*mECLoc(eY));
109   if (rtmp != 0.0) {
110     double fac = r/rtmp*cos(latitude);
111     mECLoc(eX) *= fac;
112     mECLoc(eY) *= fac;
113   } else {
114     mECLoc(eX) = r*cos(latitude);
115     mECLoc(eY) = 0.0;
116   }
117   mECLoc(eZ) = r*sin(latitude);
118 }
119
120 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121
122 void FGLocation::SetRadius(double radius)
123 {
124   mCacheValid = false;
125
126   double rold = mECLoc.Magnitude();
127   if (rold == 0.0)
128     mECLoc(eX) = radius;
129   else
130     mECLoc *= radius/rold;
131 }
132
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134
135 void FGLocation::ComputeDerivedUnconditional(void) const
136 {
137   // The radius is just the Euclidean norm of the vector.
138   mRadius = mECLoc.Magnitude();
139
140   // The distance of the location to the y-axis, which is the axis
141   // through the poles.
142   double rxy = sqrt(mECLoc(eX)*mECLoc(eX) + mECLoc(eY)*mECLoc(eY));
143
144   // Compute the sin/cos values of the longitude
145   double sinLon, cosLon;
146   if (rxy == 0.0) {
147     sinLon = 0.0;
148     cosLon = 1.0;
149   } else {
150     sinLon = mECLoc(eY)/rxy;
151     cosLon = mECLoc(eX)/rxy;
152   }
153
154   // Compute the sin/cos values of the latitude
155   double sinLat, cosLat;
156   if (mRadius == 0.0)  {
157     sinLat = 0.0;
158     cosLat = 1.0;
159   } else {
160     sinLat = mECLoc(eZ)/mRadius;
161     cosLat = rxy/mRadius;
162   }
163
164   // Compute the longitude and latitude itself
165   if ( mECLoc( eX ) == 0.0 && mECLoc( eY ) == 0.0 )
166     mLon = 0.0;
167   else
168     mLon = atan2( mECLoc( eY ), mECLoc( eX ) );
169
170   if ( rxy == 0.0 && mECLoc( eZ ) == 0.0 )
171     mLat = 0.0;
172   else
173     mLat = atan2( mECLoc(eZ), rxy );
174
175   // Compute the transform matrices from and to the earth centered frame.
176   // see Durham Chapter 4, problem 1, page 52
177   mTec2l = FGMatrix33( -cosLon*sinLat, -sinLon*sinLat,  cosLat,
178                            -sinLon   ,     cosLon    ,    0.0 ,
179                        -cosLon*cosLat, -sinLon*cosLat, -sinLat  );
180
181   mTl2ec = mTec2l.Transposed();
182
183   // Mark the cached values as valid
184   mCacheValid = true;
185 }
186
187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188
189 void FGLocation::bind(FGPropertyManager* PropertyManager, const string& prefix) const
190 {
191   PropertyManager->Tie(prefix + "lat-gc-rad", (FGLocation*)this,
192                        &FGLocation::GetLatitude);
193   PropertyManager->Tie(prefix + "lat-gc-deg", (FGLocation*)this,
194                        &FGLocation::GetLatitudeDeg);
195   PropertyManager->Tie(prefix + "long-gc-rad", (FGLocation*)this,
196                        &FGLocation::GetLongitude);
197   PropertyManager->Tie(prefix + "long-gc-deg", (FGLocation*)this,
198                        &FGLocation::GetLongitudeDeg);
199   PropertyManager->Tie(prefix + "radius-ft", (FGLocation*)this,
200                        &FGLocation::GetRadius);
201 }
202
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205 void FGLocation::unbind(FGPropertyManager* PropertyManager, const string& prefix) const
206 {
207   PropertyManager->Untie(prefix + "lat-gc-rad");
208   PropertyManager->Untie(prefix + "lat-gc-deg");
209   PropertyManager->Untie(prefix + "long-gc-rad");
210   PropertyManager->Untie(prefix + "long-gc-deg");
211   PropertyManager->Untie(prefix + "radius-ft");
212 }
213
214 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215
216 } // namespace JSBSim