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