]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Merge branch 'mathias/math' into next
[flightgear.git] / src / Environment / environment_mgr.cxx
1 // environment-mgr.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/constants.h>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/scene/sky/sky.hxx>
30 #include <simgear/environment/visual_enviro.hxx>
31 #include <simgear/scene/model/particles.hxx>
32
33 #include <Main/main.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Aircraft/aircraft.hxx>
36 #include <FDM/flight.hxx>
37
38 #include "environment.hxx"
39 #include "environment_mgr.hxx"
40 #include "environment_ctrl.hxx"
41 #include "fgclouds.hxx"
42 #include "precipitation_mgr.hxx"
43
44 class SGSky;
45 extern SGSky *thesky;
46
47
48
49 FGEnvironmentMgr::FGEnvironmentMgr ()
50   : _environment(new FGEnvironment)
51 {
52
53   _controller = new FGInterpolateEnvironmentCtrl;
54   _controller->setEnvironment(_environment);
55   set_subsystem("controller", _controller, 0.1 );
56
57   fgClouds = new FGClouds();
58
59   _metarcontroller = new FGMetarCtrl(_controller );
60   set_subsystem("metarcontroller", _metarcontroller, 0.1 );
61
62   _metarfetcher = new FGMetarFetcher();
63   set_subsystem("metarfetcher", _metarfetcher, 1.0 );
64
65   _precipitationManager = new FGPrecipitationMgr;
66   set_subsystem("precipitation", _precipitationManager);
67 }
68
69 FGEnvironmentMgr::~FGEnvironmentMgr ()
70 {
71   remove_subsystem("precipitation");
72   delete _precipitationManager;
73
74   remove_subsystem("metarcontroller");
75   delete _metarfetcher;
76
77   remove_subsystem("metarfetcher");
78   delete _metarcontroller;
79
80   delete fgClouds;
81
82   remove_subsystem("controller");
83   delete _controller;
84
85   delete _environment;
86 }
87
88 void
89 FGEnvironmentMgr::init ()
90 {
91   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
92   SGSubsystemGroup::init();
93   _update_fdm();
94 }
95
96 void
97 FGEnvironmentMgr::reinit ()
98 {
99   SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
100   SGSubsystemGroup::reinit();
101   _update_fdm();
102 }
103
104 void
105 FGEnvironmentMgr::bind ()
106 {
107   SGSubsystemGroup::bind();
108   fgTie("/environment/visibility-m", _environment,
109         &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
110   fgSetArchivable("/environment/visibility-m");
111   fgTie("/environment/effective-visibility-m", thesky,
112        &SGSky::get_visibility );
113   fgTie("/environment/temperature-sea-level-degc", _environment,
114         &FGEnvironment::get_temperature_sea_level_degc,
115         &FGEnvironment::set_temperature_sea_level_degc);
116   fgSetArchivable("/environment/temperature-sea-level-degc");
117   fgTie("/environment/temperature-degc", _environment,
118         &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
119   fgTie("/environment/temperature-degf", _environment,
120         &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
121   fgTie("/environment/dewpoint-sea-level-degc", _environment,
122         &FGEnvironment::get_dewpoint_sea_level_degc,
123         &FGEnvironment::set_dewpoint_sea_level_degc);
124   fgSetArchivable("/environment/dewpoint-sea-level-degc");
125   fgTie("/environment/dewpoint-degc", _environment,
126         &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
127   fgTie("/environment/pressure-sea-level-inhg", _environment,
128         &FGEnvironment::get_pressure_sea_level_inhg,
129         &FGEnvironment::set_pressure_sea_level_inhg);
130   fgSetArchivable("/environment/pressure-sea-level-inhg");
131   fgTie("/environment/pressure-inhg", _environment,
132         &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
133   fgTie("/environment/density-slugft3", _environment,
134         &FGEnvironment::get_density_slugft3); // read-only
135   fgTie("/environment/relative-humidity", _environment,
136         &FGEnvironment::get_relative_humidity); //ro
137   fgTie("/environment/atmosphere/density-tropo-avg", _environment,
138         &FGEnvironment::get_density_tropo_avg_kgm3); //ro
139   fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
140         &FGEnvironment::get_altitude_half_to_sun_m, 
141         &FGEnvironment::set_altitude_half_to_sun_m);
142   fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
143         &FGEnvironment::get_altitude_tropo_top_m,
144         &FGEnvironment::set_altitude_tropo_top_m);
145   fgTie("/environment/wind-from-heading-deg", _environment,
146         &FGEnvironment::get_wind_from_heading_deg,
147         &FGEnvironment::set_wind_from_heading_deg);
148   fgTie("/environment/wind-speed-kt", _environment,
149         &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
150   fgTie("/environment/wind-from-north-fps", _environment,
151         &FGEnvironment::get_wind_from_north_fps,
152         &FGEnvironment::set_wind_from_north_fps);
153   fgSetArchivable("/environment/wind-from-north-fps");
154   fgTie("/environment/wind-from-east-fps", _environment,
155         &FGEnvironment::get_wind_from_east_fps,
156         &FGEnvironment::set_wind_from_east_fps);
157   fgSetArchivable("/environment/wind-from-east-fps");
158   fgTie("/environment/wind-from-down-fps", _environment,
159         &FGEnvironment::get_wind_from_down_fps,
160         &FGEnvironment::set_wind_from_down_fps);
161   fgSetArchivable("/environment/wind-from-down-fps");
162
163   fgTie("/environment/thermal-lift-fps", _environment,
164         &FGEnvironment::get_thermal_lift_fps,
165         &FGEnvironment::set_thermal_lift_fps);
166   fgSetArchivable("/environment/thermal-lift-fps");
167   fgTie("/environment/ridge-lift-fps", _environment,
168         &FGEnvironment::get_ridge_lift_fps,
169         &FGEnvironment::set_ridge_lift_fps);    
170   fgSetArchivable("/environment/ridge-lift-fps");
171   
172     fgTie("/environment/local-weather-lift", _environment,
173         &FGEnvironment::get_local_weather_lift_fps); //read-only
174      
175   fgTie("/environment/turbulence/magnitude-norm", _environment,
176         &FGEnvironment::get_turbulence_magnitude_norm,
177         &FGEnvironment::set_turbulence_magnitude_norm);
178   fgSetArchivable("/environment/turbulence/magnitude-norm");
179   fgTie("/environment/turbulence/rate-hz", _environment,
180         &FGEnvironment::get_turbulence_rate_hz,
181         &FGEnvironment::set_turbulence_rate_hz);
182   fgSetArchivable("/environment/turbulence/rate-hz");
183
184   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
185     char buf[128];
186     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
187     fgTie(buf, this, i,
188           &FGEnvironmentMgr::get_cloud_layer_span_m,
189           &FGEnvironmentMgr::set_cloud_layer_span_m);
190     fgSetArchivable(buf);
191     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
192     fgTie(buf, this, i,
193           &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
194           &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
195     fgSetArchivable(buf);
196     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
197     fgTie(buf, this, i,
198           &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
199           &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
200     fgSetArchivable(buf);
201     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
202     fgTie(buf, this, i,
203           &FGEnvironmentMgr::get_cloud_layer_transition_ft,
204           &FGEnvironmentMgr::set_cloud_layer_transition_ft);
205     fgSetArchivable(buf);
206     sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
207     fgTie(buf, this, i,
208           &FGEnvironmentMgr::get_cloud_layer_coverage,
209           &FGEnvironmentMgr::set_cloud_layer_coverage);
210     fgSetArchivable(buf);
211   }
212
213   fgTie("/environment/metar/data", _metarcontroller,
214           &FGMetarCtrl::get_metar, &FGMetarCtrl::set_metar );
215
216   fgTie("/sim/rendering/clouds3d-enable", fgClouds,
217           &FGClouds::get_3dClouds,
218           &FGClouds::set_3dClouds);
219   fgTie("/sim/rendering/clouds3d-density", thesky,
220           &SGSky::get_3dCloudDensity,
221           &SGSky::set_3dCloudDensity);
222   fgTie("/sim/rendering/clouds3d-vis-range", thesky,
223         &SGSky::get_3dCloudVisRange,
224         &SGSky::set_3dCloudVisRange);
225   
226   fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
227           &SGEnviro::get_precipitation_enable_state, 
228           &SGEnviro::set_precipitation_enable_state);
229   fgTie("/environment/rebuild-layers", fgClouds,
230       &FGClouds::get_update_event,
231       &FGClouds::set_update_event);
232   fgTie("/sim/rendering/lightning-enable", &sgEnviro,
233       &SGEnviro::get_lightning_enable_state,
234       &SGEnviro::set_lightning_enable_state);
235   fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
236       &SGEnviro::get_turbulence_enable_state,
237       &SGEnviro::set_turbulence_enable_state);
238   sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
239 }
240
241 void
242 FGEnvironmentMgr::unbind ()
243 {
244   fgUntie("/environment/visibility-m");
245   fgUntie("/environment/effective-visibility-m");
246   fgUntie("/environment/temperature-sea-level-degc");
247   fgUntie("/environment/temperature-degc");
248   fgUntie("/environment/dewpoint-sea-level-degc");
249   fgUntie("/environment/dewpoint-degc");
250   fgUntie("/environment/pressure-sea-level-inhg");
251   fgUntie("/environment/pressure-inhg");
252   fgUntie("/environment/density-inhg");
253   fgUntie("/environment/relative-humidity");
254   fgUntie("/environment/atmosphere/density-tropo-avg");
255   fgUntie("/environment/wind-from-north-fps");
256   fgUntie("/environment/wind-from-east-fps");
257   fgUntie("/environment/wind-from-down-fps");
258
259   fgUntie("/environment/thermal-lift-fps");
260   fgUntie("/environment/ridge-lift-fps");
261   fgUntie("/environment/local-weather-lift");
262
263   fgUntie("/environment/atmosphere/altitude-half-to-sun");
264   fgUntie("/environment/atmosphere/altitude-troposphere-top");
265   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
266     char buf[128];
267     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
268     fgUntie(buf);
269     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
270     fgUntie(buf);
271     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
272     fgUntie(buf);
273     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
274     fgUntie(buf);
275     sprintf(buf, "/environment/clouds/layer[%d]/type", i);
276     fgUntie(buf);
277   }
278   fgUntie("/sim/rendering/clouds3d-enable");
279   fgUntie("/sim/rendering/clouds3d-vis-range");
280   fgUntie("/sim/rendering/clouds3d-density");
281   fgUntie("/sim/rendering/precipitation-enable");
282   fgUntie("/environment/rebuild-layers");
283   fgUntie("/environment/weather-scenario");
284   fgUntie("/sim/rendering/lightning-enable");
285   fgUntie("/environment/turbulence/use-cloud-turbulence");
286   SGSubsystemGroup::unbind();
287 }
288
289 void
290 FGEnvironmentMgr::update (double dt)
291 {
292   SGSubsystemGroup::update(dt);
293
294                                 // FIXME: the FDMs should update themselves
295   current_aircraft.fdm_state
296     ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
297                                    _environment->get_wind_from_east_fps(),
298                                    _environment->get_wind_from_down_fps());
299   _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
300   _environment->set_local_weather_lift_fps(fgGetDouble("/local-weather/current/thermal-lift"));
301   osg::Vec3 windVec(-_environment->get_wind_from_north_fps(),
302                     -_environment->get_wind_from_east_fps(),
303                     _environment->get_wind_from_down_fps());
304   simgear::Particles::setWindVector(windVec * SG_FEET_TO_METER);
305
306   _update_fdm();
307 }
308
309 FGEnvironment
310 FGEnvironmentMgr::getEnvironment () const
311 {
312   return *_environment;
313 }
314
315 FGEnvironment
316 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
317 {
318                                 // Always returns the same environment
319                                 // for now; we'll make it interesting
320                                 // later.
321   FGEnvironment env = *_environment;
322   env.set_elevation_ft(alt);
323   return env;
324 }
325
326 FGEnvironment
327 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
328 {
329   // Always returns the same environment
330   // for now; we'll make it interesting
331   // later.
332   FGEnvironment env = *_environment;
333   env.set_elevation_ft(aPos.getElevationFt());
334   return env;
335
336 }
337
338 void
339 FGEnvironmentMgr::_update_fdm () const
340 {
341   //
342   // Pass atmosphere on to FDM
343   // FIXME: have FDMs read properties directly.
344   //
345   if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
346                                 // convert from Rankine to Celsius
347     cur_fdm_state
348       ->set_Static_temperature((9.0/5.0)
349                                * (_environment->get_temperature_degc() + 273.15));
350                                 // convert from inHG to PSF
351     cur_fdm_state
352       ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
353                                 // keep in slugs/ft^3
354     cur_fdm_state
355       ->set_Density(_environment->get_density_slugft3());
356   }
357 }
358
359 double
360 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
361 {
362   return thesky->get_cloud_layer(index)->getSpan_m();
363 }
364
365 void
366 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
367 {
368   thesky->get_cloud_layer(index)->setSpan_m(span_m);
369 }
370
371 double
372 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
373 {
374   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
375 }
376
377 void
378 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
379 {
380   FGEnvironment env = *_environment;
381   env.set_elevation_ft(elevation_ft);
382
383   thesky->get_cloud_layer(index)
384     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
385
386   thesky->get_cloud_layer(index)
387     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
388
389   thesky->get_cloud_layer(index)
390     ->setDirection(env.get_wind_from_heading_deg());
391 }
392
393 double
394 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
395 {
396   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
397 }
398
399 void
400 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
401 {
402   thesky->get_cloud_layer(index)
403     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
404 }
405
406 double
407 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
408 {
409   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
410 }
411
412 void
413 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
414                                                  double transition_ft)
415 {
416   thesky->get_cloud_layer(index)
417     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
418 }
419
420 const char *
421 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
422 {
423   switch (thesky->get_cloud_layer(index)->getCoverage()) {
424   case SGCloudLayer::SG_CLOUD_OVERCAST:
425     return "overcast";
426   case SGCloudLayer::SG_CLOUD_BROKEN:
427     return "broken";
428   case SGCloudLayer::SG_CLOUD_SCATTERED:
429     return "scattered";
430   case SGCloudLayer::SG_CLOUD_FEW:
431     return "few";
432   case SGCloudLayer::SG_CLOUD_CIRRUS:
433     return "cirrus";
434   case SGCloudLayer::SG_CLOUD_CLEAR:
435     return "clear";
436   default:
437     return "unknown";
438   }
439 }
440
441 void
442 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
443                                             const char * coverage_name)
444 {
445   SGCloudLayer::Coverage coverage;
446   if (!strcmp(coverage_name, "overcast"))
447     coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
448   else if (!strcmp(coverage_name, "broken"))
449     coverage = SGCloudLayer::SG_CLOUD_BROKEN;
450   else if (!strcmp(coverage_name, "scattered"))
451     coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
452   else if (!strcmp(coverage_name, "few"))
453     coverage = SGCloudLayer::SG_CLOUD_FEW;
454   else if (!strcmp(coverage_name, "cirrus"))
455     coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
456   else if (!strcmp(coverage_name, "clear"))
457     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
458   else {
459     SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
460     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
461   }
462   thesky->get_cloud_layer(index)->setCoverage(coverage);
463 }
464
465
466
467 // end of environment-mgr.cxx