]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_ctrl.cxx
Irix/MipsPro compiler fixes
[flightgear.git] / src / Environment / environment_ctrl.cxx
1 // environment_ctrl.cxx -- manager for natural environment information.
2 //
3 // Written by David Megginson, started February 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #include <simgear/debug/logstream.hxx>
24
25 #include <stdlib.h>
26 #include <Main/fg_props.hxx>
27
28 #include "environment_ctrl.hxx"
29
30 SG_USING_STD(sort);
31
32
33 \f
34 ////////////////////////////////////////////////////////////////////////
35 // Implementation of FGEnvironmentCtrl abstract base class.
36 ////////////////////////////////////////////////////////////////////////
37
38 FGEnvironmentCtrl::FGEnvironmentCtrl ()
39   : _environment(0),
40     _lon_deg(0),
41     _lat_deg(0),
42     _elev_ft(0)
43 {
44 }
45
46 FGEnvironmentCtrl::~FGEnvironmentCtrl ()
47 {
48 }
49
50 void
51 FGEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
52 {
53   _environment = environment;
54 }
55
56 void
57 FGEnvironmentCtrl::setLongitudeDeg (double lon_deg)
58 {
59   _lon_deg = lon_deg;
60 }
61
62 void
63 FGEnvironmentCtrl::setLatitudeDeg (double lat_deg)
64 {
65   _lat_deg = lat_deg;
66 }
67
68 void
69 FGEnvironmentCtrl::setElevationFt (double elev_ft)
70 {
71   _elev_ft = elev_ft;
72 }
73
74 void
75 FGEnvironmentCtrl::setPosition (double lon_deg, double lat_deg, double elev_ft)
76 {
77   _lon_deg = lon_deg;
78   _lat_deg = lat_deg;
79   _elev_ft = elev_ft;
80 }
81
82
83 \f
84 ////////////////////////////////////////////////////////////////////////
85 // Implementation of FGUserDefEnvironmentCtrl.
86 ////////////////////////////////////////////////////////////////////////
87
88 FGUserDefEnvironmentCtrl::FGUserDefEnvironmentCtrl ()
89   : _base_wind_speed_node(0),
90     _gust_wind_speed_node(0),
91     _current_wind_speed_kt(0),
92     _delta_wind_speed_kt(0)
93 {
94 }
95
96 FGUserDefEnvironmentCtrl::~FGUserDefEnvironmentCtrl ()
97 {
98 }
99
100 void
101 FGUserDefEnvironmentCtrl::init ()
102 {
103                                 // Fill in some defaults.
104   if (!fgHasNode("/environment/params/base-wind-speed-kt"))
105     fgSetDouble("/environment/params/base-wind-speed-kt",
106                 fgGetDouble("/environment/wind-speed-kt"));
107   if (!fgHasNode("/environment/params/gust-wind-speed-kt"))
108     fgSetDouble("/environment/params/gust-wind-speed-kt",
109                 fgGetDouble("/environment/params/base-wind-speed-kt"));
110
111   _base_wind_speed_node =
112     fgGetNode("/environment/params/base-wind-speed-kt", true);
113   _gust_wind_speed_node =
114     fgGetNode("/environment/params/gust-wind-speed-kt", true);
115
116   _current_wind_speed_kt = _base_wind_speed_node->getDoubleValue();
117   _delta_wind_speed_kt = 0.1;
118 }
119
120 void
121 FGUserDefEnvironmentCtrl::update (double dt)
122 {
123   double base_wind_speed = _base_wind_speed_node->getDoubleValue();
124   double gust_wind_speed = _gust_wind_speed_node->getDoubleValue();
125
126   if (gust_wind_speed < base_wind_speed) {
127       gust_wind_speed = base_wind_speed;
128       _gust_wind_speed_node->setDoubleValue(gust_wind_speed);
129   }
130
131   if (base_wind_speed == gust_wind_speed) {
132     _current_wind_speed_kt = base_wind_speed;
133   } else {
134     int rn = rand() % 128;
135     int sign = (_delta_wind_speed_kt < 0 ? -1 : 1);
136     double gust = _current_wind_speed_kt - base_wind_speed;
137     double incr = gust / 50;
138
139     if (rn == 0)
140       _delta_wind_speed_kt = - _delta_wind_speed_kt;
141     else if (rn < 4)
142       _delta_wind_speed_kt -= incr * sign;
143     else if (rn < 16)
144       _delta_wind_speed_kt += incr * sign;
145
146     _current_wind_speed_kt += _delta_wind_speed_kt;
147
148     if (_current_wind_speed_kt < base_wind_speed) {
149       _current_wind_speed_kt = base_wind_speed;
150       _delta_wind_speed_kt = 0.01;
151     } else if (_current_wind_speed_kt > gust_wind_speed) {
152       _current_wind_speed_kt = gust_wind_speed;
153       _delta_wind_speed_kt = -0.01;
154     }
155   }
156   
157   if (_environment != 0)
158     _environment->set_wind_speed_kt(_current_wind_speed_kt);
159 }
160
161
162 \f
163 ////////////////////////////////////////////////////////////////////////
164 // Implementation of FGInterpolateEnvironmentCtrl.
165 ////////////////////////////////////////////////////////////////////////
166
167 FGInterpolateEnvironmentCtrl::FGInterpolateEnvironmentCtrl ()
168 {
169 }
170
171 FGInterpolateEnvironmentCtrl::~FGInterpolateEnvironmentCtrl ()
172 {
173     int i;
174     for (i = 0; i < _boundary_table.size(); i++)
175         delete _boundary_table[i];
176     for (i = 0; i < _aloft_table.size(); i++)
177         delete _aloft_table[i];
178 }
179
180
181
182 void
183 FGInterpolateEnvironmentCtrl::init ()
184 {
185     read_table(fgGetNode("/environment/config/boundary", true),
186                _boundary_table);
187     read_table(fgGetNode("/environment/config/aloft", true),
188                _aloft_table);
189 }
190
191 void
192 FGInterpolateEnvironmentCtrl::reinit ()
193 {
194     int i;
195     for (i = 0; i < _boundary_table.size(); i++)
196         delete _boundary_table[i];
197     for (i = 0; i < _aloft_table.size(); i++)
198         delete _aloft_table[i];
199     _boundary_table.clear();
200     _aloft_table.clear();
201     init();
202 }
203
204 void
205 FGInterpolateEnvironmentCtrl::read_table (const SGPropertyNode * node,
206                                           vector<bucket *> &table)
207 {
208     for (int i = 0; i < node->nChildren(); i++) {
209         const SGPropertyNode * child = node->getChild(i);
210         if (!strcmp(child->getName(), "entry") &&
211             child->getStringValue("elevation-ft", "")[0] != '\0') {
212             bucket * b = new bucket;
213             if (i > 0)
214                 b->environment.copy(table[i-1]->environment);
215             b->environment.read(child);
216             b->altitude_ft = b->environment.get_elevation_ft();
217             table.push_back(b);
218         }
219     }
220     sort(table.begin(), table.end());
221 }
222
223 void
224 FGInterpolateEnvironmentCtrl::update (double delta_time_sec)
225 {
226                                 // FIXME
227     double altitude_ft = fgGetDouble("/position/altitude-ft");
228     double altitude_agl_ft = fgGetDouble("/position/altitude-agl-ft");
229     double boundary_transition =
230         fgGetDouble("/environment/config/boundary-transition-ft", 500);
231
232     double ground_elevation_ft = altitude_ft - altitude_agl_ft;
233
234     int length = _boundary_table.size();
235
236     if (length > 0) {
237                                 // boundary table
238         double boundary_limit = _boundary_table[length-1]->altitude_ft;
239         if (boundary_limit >= altitude_agl_ft) {
240             do_interpolate(_boundary_table, altitude_agl_ft,
241                            _environment);
242             return;
243         } else if ((boundary_limit + boundary_transition) >= altitude_agl_ft) {
244                                 // both tables
245             do_interpolate(_boundary_table, altitude_agl_ft, &env1);
246             do_interpolate(_aloft_table, altitude_ft, &env2);
247             double fraction =
248                 (altitude_agl_ft - boundary_limit) / boundary_transition;
249             interpolate(&env1, &env2, fraction, _environment);
250             return;
251         }
252     }
253
254                                 // aloft table
255     do_interpolate(_aloft_table, altitude_ft, _environment);
256 }
257
258 void
259 FGInterpolateEnvironmentCtrl::do_interpolate (vector<bucket *> &table,
260                                               double altitude_ft,
261                                               FGEnvironment * environment)
262 {
263     int length = table.size();
264     if (length == 0)
265         return;
266
267                                 // Boundary conditions
268     if ((length == 1) || (table[0]->altitude_ft >= altitude_ft)) {
269         environment->copy(table[0]->environment);
270         return;
271     } else if (table[length-1]->altitude_ft <= altitude_ft) {
272         environment->copy(table[length-1]->environment);
273         return;
274     }
275         
276                                 // Search the interpolation table
277     for (int i = 0; i < length - 1; i++) {
278         if ((i == length - 1) || (table[i]->altitude_ft <= altitude_ft)) {
279                 FGEnvironment * env1 = &(table[i]->environment);
280                 FGEnvironment * env2 = &(table[i+1]->environment);
281                 double fraction;
282                 if (table[i]->altitude_ft == table[i+1]->altitude_ft)
283                     fraction = 1.0;
284                 else
285                     fraction =
286                         ((altitude_ft - table[i]->altitude_ft) /
287                          (table[i+1]->altitude_ft - table[i]->altitude_ft));
288                 interpolate(env1, env2, fraction, environment);
289
290                 return;
291         }
292     }
293 }
294
295 bool
296 FGInterpolateEnvironmentCtrl::bucket::operator< (const bucket &b) const
297 {
298     return (altitude_ft < b.altitude_ft);
299 }
300
301
302 // end of environment_ctrl.cxx