1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 04/04/2004
6 Purpose: Store an arbitrary location on the globe
8 ------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) ------------------
9 ------- (C) 2004 Mathias Froehlich (Mathias.Froehlich@web.de) ----
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
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
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.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
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 ....
34 ------------------------------------------------------------------------------
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 # include <simgear/compiler.h>
43 # ifdef SG_HAVE_STD_INCLUDES
49 # if defined(sgi) && !defined(__GNUC__)
56 #include "FGLocation.h"
57 #include "FGPropertyManager.h"
61 static const char *IdSrc = "$Id$";
62 static const char *IdHdr = ID_LOCATION;
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 FGLocation::FGLocation(double lon, double lat, double radius)
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,
81 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 void FGLocation::SetLongitude(double longitude)
85 double rtmp = sqrt(mECLoc(eX)*mECLoc(eX) + mECLoc(eY)*mECLoc(eY));
86 // Fast return if we are on the north or south pole ...
92 mECLoc(eX) = rtmp*sin(longitude);
93 mECLoc(eY) = rtmp*cos(longitude);
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 void FGLocation::SetLatitude(double latitude)
102 double r = mECLoc.Magnitude();
108 double rtmp = sqrt(mECLoc(eX)*mECLoc(eX) + mECLoc(eY)*mECLoc(eY));
110 double fac = r/rtmp*cos(latitude);
114 mECLoc(eX) = r*cos(latitude);
117 mECLoc(eZ) = r*sin(latitude);
120 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 void FGLocation::SetRadius(double radius)
126 double rold = mECLoc.Magnitude();
130 mECLoc *= radius/rold;
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 void FGLocation::ComputeDerivedUnconditional(void) const
137 // The radius is just the Euclidean norm of the vector.
138 mRadius = mECLoc.Magnitude();
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));
144 // Compute the sin/cos values of the longitude
145 double sinLon, cosLon;
150 sinLon = mECLoc(eY)/rxy;
151 cosLon = mECLoc(eX)/rxy;
154 // Compute the sin/cos values of the latitude
155 double sinLat, cosLat;
156 if (mRadius == 0.0) {
160 sinLat = mECLoc(eZ)/mRadius;
161 cosLat = rxy/mRadius;
164 // Compute the longitude and latitude itself
165 if ( mECLoc( eX ) == 0.0 && mECLoc( eY ) == 0.0 )
168 mLon = atan2( mECLoc( eY ), mECLoc( eX ) );
170 if ( rxy == 0.0 && mECLoc( eZ ) == 0.0 )
173 mLat = atan2( mECLoc(eZ), rxy );
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 );
181 mTl2ec = mTec2l.Transposed();
183 // Mark the cached values as valid
187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 void FGLocation::bind(FGPropertyManager* PropertyManager, const string& prefix) const
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);
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 void FGLocation::unbind(FGPropertyManager* PropertyManager, const string& prefix) const
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");
214 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 } // namespace JSBSim