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