1 // location.cxx -- class for determining model location in the flightgear world.
3 // Written by Jim Wilson, David Megginson, started April 2002.
4 // Based largely on code by Curtis Olson and Norman Vine.
6 // Copyright (C) 2002 Curtis L. Olson - curt@flightgear.org
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/compiler.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/constants.h>
33 #include <simgear/math/point3d.hxx>
34 #include <simgear/math/polar3d.hxx>
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/math/vector.hxx>
38 #include <Scenery/scenery.hxx>
39 #include "globals.hxx"
41 #include "location.hxx"
45 * make model transformation Matrix - based on optimizations by NHV
47 static void MakeTRANS( sgMat4 dst, const double Theta,
48 const double Phi, const double Psi,
51 SGfloat cosTheta = (SGfloat) cos(Theta);
52 SGfloat sinTheta = (SGfloat) sin(Theta);
53 SGfloat cosPhi = (SGfloat) cos(Phi);
54 SGfloat sinPhi = (SGfloat) sin(Phi);
55 SGfloat sinPsi = (SGfloat) sin(Psi) ;
56 SGfloat cosPsi = (SGfloat) cos(Psi) ;
60 tmp[0][0] = cosPhi * cosTheta;
61 tmp[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
62 tmp[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
64 tmp[1][0] = -sinPhi * cosTheta;
65 tmp[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
66 tmp[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
69 tmp[2][1] = cosTheta * -sinPsi;
70 tmp[2][2] = cosTheta * cosPsi;
75 dst[2][0] = a*tmp[0][0] + b*tmp[0][1] + c*tmp[0][2] ;
76 dst[1][0] = a*tmp[1][0] + b*tmp[1][1] + c*tmp[1][2] ;
77 dst[0][0] = -(a*tmp[2][0] + b*tmp[2][1] + c*tmp[2][2]) ;
83 dst[2][1] = a*tmp[0][0] + b*tmp[0][1] + c*tmp[0][2] ;
84 dst[1][1] = a*tmp[1][0] + b*tmp[1][1] + c*tmp[1][2] ;
85 dst[0][1] = -(a*tmp[2][0] + b*tmp[2][1] + c*tmp[2][2]) ;
90 dst[2][2] = a*tmp[0][0] + c*tmp[0][2] ;
91 dst[1][2] = a*tmp[1][0] + c*tmp[1][2] ;
92 dst[0][2] = -(a*tmp[2][0] + c*tmp[2][2]) ;
103 ////////////////////////////////////////////////////////////////////////
104 // Implementation of FGLocation.
105 ////////////////////////////////////////////////////////////////////////
108 FGLocation::FGLocation( void ):
117 sgdZeroVec3(_absolute_view_pos);
122 FGLocation::~FGLocation( void ) {
136 FGLocation::unbind ()
141 FGLocation::setPosition (double lon_deg, double lat_deg, double alt_ft)
150 FGLocation::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
153 _roll_deg = roll_deg;
154 _pitch_deg = pitch_deg;
155 _heading_deg = heading_deg;
159 FGLocation::get_absolute_view_pos ()
163 return _absolute_view_pos;
167 FGLocation::getRelativeViewPos ()
171 return _relative_view_pos;
175 FGLocation::getZeroElevViewPos ()
179 return _zero_elev_view_pos;
183 // recalc() is done every time one of the setters is called (making the
184 // cached data "dirty") on the next "get". It calculates all the outputs
187 FGLocation::recalc ()
190 recalcPosition( _lon_deg, _lat_deg, _alt_ft );
192 // Make the world up rotation matrix for eye positioin...
193 sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
196 // get the world up radial vector from planet center for output
197 sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
199 // Creat local matrix with current geodetic position. Converting
200 // the orientation (pitch/roll/heading) to vectors.
201 MakeTRANS( TRANS, _pitch_deg * SG_DEGREES_TO_RADIANS,
202 _roll_deg * SG_DEGREES_TO_RADIANS,
203 -_heading_deg * SG_DEGREES_TO_RADIANS,
206 // Given a vector pointing straight down (-Z), map into onto the
207 // local plane representing "horizontal". This should give us the
208 // local direction for moving "south".
210 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
212 sgmap_vec_onto_cur_surface_plane(_world_up, _relative_view_pos, minus_z,
214 sgNormalizeVec3(_surface_south);
216 // now calculate the surface east vector
218 sgNegateVec3(world_down, _world_up);
219 sgVectorProductVec3(_surface_east, _surface_south, world_down);
225 FGLocation::recalcPosition (double lon_deg, double lat_deg, double alt_ft) const
227 double sea_level_radius_m;
231 // Convert from geodetic to geocentric
233 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
234 alt_ft * SG_FEET_TO_METER,
238 // Calculate the cartesian coordinates
239 // of point directly below at sea level.
240 // aka Zero Elevation Position
241 Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
244 Point3D tmp = sgPolarToCart3d(p) - globals->get_scenery()->get_next_center();
245 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
247 // Calculate the absolute view position
248 // in fgfs coordinates.
249 // aka Absolute View Position
250 p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
251 tmp = sgPolarToCart3d(p);
252 sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
254 // Calculate the relative view position
255 // from the scenery center.
256 // aka Relative View Position
257 sgdVec3 scenery_center;
258 sgdSetVec3(scenery_center,
259 globals->get_scenery()->get_next_center().x(),
260 globals->get_scenery()->get_next_center().y(),
261 globals->get_scenery()->get_next_center().z());
263 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
264 sgSetVec3(_relative_view_pos, view_pos);
269 FGLocation::update (int dt)