]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment.cxx
calculate internal properties only once on read()
[flightgear.git] / src / Environment / environment.cxx
index 026ebdcb8c3a7634f73252dacd873ceb155e9772..a75d6ebb3e3d8c399e1c14f270c5f7b83e837b5f 100644 (file)
 #  include <config.h>
 #endif
 
-#ifdef HAVE_WINDOWS_H
-#  include <windows.h>                     
-#endif
-
 #include <math.h>
 
-#include <plib/sg.h>
-
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/interpolater.hxx>
@@ -122,33 +116,41 @@ _setup_tables ()
 // Implementation of FGEnvironment.
 ////////////////////////////////////////////////////////////////////////
 
+void FGEnvironment::_init()
+{
+    elevation_ft = 0;
+    visibility_m = 32000;
+    temperature_sea_level_degc = 15;
+    temperature_degc = 15;
+    dewpoint_sea_level_degc = 5; // guess
+    dewpoint_degc = 5;
+    pressure_sea_level_inhg = 29.92;
+    pressure_inhg = 29.92;
+    turbulence_magnitude_norm = 0;
+    turbulence_rate_hz = 1;
+    wind_from_heading_deg = 0;
+    wind_speed_kt = 0;
+    wind_from_north_fps = 0;
+    wind_from_east_fps = 0;
+    wind_from_down_fps = 0;
+    thermal_lift_fps = 0;
+    ridge_lift_fps= 0;
+    altitude_half_to_sun_m = 1000;
+    altitude_tropo_top_m = 10000;
+    _setup_tables();
+    _recalc_density();
+    _recalc_relative_humidity();
+    live_update = true;
+}
+
 FGEnvironment::FGEnvironment()
-  : elevation_ft(0),
-    visibility_m(32000),
-    temperature_sea_level_degc(15),
-    temperature_degc(15),
-    dewpoint_sea_level_degc(5), // guess
-    dewpoint_degc(5),
-    pressure_sea_level_inhg(29.92),
-    pressure_inhg(29.92),
-    turbulence_magnitude_norm(0),
-    turbulence_rate_hz(1),
-    wind_from_heading_deg(0),
-    wind_speed_kt(0),
-    wind_from_north_fps(0),
-    wind_from_east_fps(0),
-    wind_from_down_fps(0),
-    altitude_half_to_sun_m(1000),
-    altitude_tropo_top_m(10000)
-{
-  _setup_tables();
-  _recalc_density();
-  _recalc_relative_humidity();
+{
+    _init();
 }
 
 FGEnvironment::FGEnvironment (const FGEnvironment &env)
 {
-    FGEnvironment();
+    _init();
     copy(env);
 }
 
@@ -171,6 +173,8 @@ FGEnvironment::copy (const FGEnvironment &env)
     wind_from_north_fps = env.wind_from_north_fps;
     wind_from_east_fps = env.wind_from_east_fps;
     wind_from_down_fps = env.wind_from_down_fps;
+    thermal_lift_fps = env.thermal_lift_fps;
+    ridge_lift_fps= env.ridge_lift_fps;
     turbulence_magnitude_norm = env.turbulence_magnitude_norm;
     turbulence_rate_hz = env.turbulence_rate_hz;
 }
@@ -194,6 +198,7 @@ maybe_copy_value (FGEnvironment * env, const SGPropertyNode * node,
 void
 FGEnvironment::read (const SGPropertyNode * node)
 {
+    bool live_update = set_live_update( false );
     maybe_copy_value(this, node, "visibility-m",
                      &FGEnvironment::set_visibility_m);
 
@@ -226,6 +231,15 @@ FGEnvironment::read (const SGPropertyNode * node)
 
     maybe_copy_value(this, node, "turbulence/rate-hz",
                      &FGEnvironment::set_turbulence_rate_hz);
+    // calculate derived properties here to avoid duplicate expensive computations
+    _recalc_ne();
+    _recalc_alt_temperature();
+    _recalc_alt_dewpoint();
+    _recalc_alt_pressure();
+    _recalc_density();
+    _recalc_relative_humidity();
+
+    set_live_update(live_update);
 }
 
 
@@ -337,6 +351,18 @@ FGEnvironment::get_wind_from_down_fps () const
   return wind_from_down_fps;
 }
 
+double
+FGEnvironment::get_thermal_lift_fps () const
+{
+  return thermal_lift_fps;
+}
+
+double
+FGEnvironment::get_ridge_lift_fps () const
+{
+  return ridge_lift_fps;
+}
+
 double
 FGEnvironment::get_turbulence_magnitude_norm () const
 {
@@ -370,17 +396,21 @@ FGEnvironment::set_temperature_sea_level_degc (double t)
   temperature_sea_level_degc = t;
   if (dewpoint_sea_level_degc > t)
       dewpoint_sea_level_degc = t;
-  _recalc_alt_temperature();
-  _recalc_density();
+  if( live_update ) {
+    _recalc_alt_temperature();
+    _recalc_density();
+  }
 }
 
 void
 FGEnvironment::set_temperature_degc (double t)
 {
   temperature_degc = t;
-  _recalc_sl_temperature();
-  _recalc_density();
-  _recalc_relative_humidity();
+  if( live_update ) {
+    _recalc_sl_temperature();
+    _recalc_density();
+    _recalc_relative_humidity();
+  }
 }
 
 void
@@ -389,68 +419,104 @@ FGEnvironment::set_dewpoint_sea_level_degc (double t)
   dewpoint_sea_level_degc = t;
   if (temperature_sea_level_degc < t)
       temperature_sea_level_degc = t;
-  _recalc_alt_dewpoint();
-  _recalc_density();
+  if( live_update ) {
+    _recalc_alt_dewpoint();
+    _recalc_density();
+  }
 }
 
 void
 FGEnvironment::set_dewpoint_degc (double t)
 {
   dewpoint_degc = t;
-  _recalc_sl_dewpoint();
-  _recalc_density();
-  _recalc_relative_humidity();
+  if( live_update ) {
+    _recalc_sl_dewpoint();
+    _recalc_density();
+    _recalc_relative_humidity();
+  }
 }
 
 void
 FGEnvironment::set_pressure_sea_level_inhg (double p)
 {
   pressure_sea_level_inhg = p;
-  _recalc_alt_pressure();
-  _recalc_density();
+  if( live_update ) {
+    _recalc_alt_pressure();
+    _recalc_density();
+  }
 }
 
 void
 FGEnvironment::set_pressure_inhg (double p)
 {
   pressure_inhg = p;
-  _recalc_sl_pressure();
-  _recalc_density();
+  if( live_update ) {
+    _recalc_sl_pressure();
+    _recalc_density();
+  }
 }
 
 void
 FGEnvironment::set_wind_from_heading_deg (double h)
 {
   wind_from_heading_deg = h;
-  _recalc_ne();
+  if( live_update ) {
+    _recalc_ne();
+  }
 }
 
 void
 FGEnvironment::set_wind_speed_kt (double s)
 {
   wind_speed_kt = s;
-  _recalc_ne();
+  if( live_update ) {
+    _recalc_ne();
+  }
 }
 
 void
 FGEnvironment::set_wind_from_north_fps (double n)
 {
   wind_from_north_fps = n;
-  _recalc_hdgspd();
+  if( live_update ) {
+    _recalc_hdgspd();
+  }
 }
 
 void
 FGEnvironment::set_wind_from_east_fps (double e)
 {
   wind_from_east_fps = e;
-  _recalc_hdgspd();
+  if( live_update ) {
+    _recalc_hdgspd();
+  }
 }
 
 void
 FGEnvironment::set_wind_from_down_fps (double d)
 {
   wind_from_down_fps = d;
-  _recalc_hdgspd();
+  if( live_update ) {
+    _recalc_hdgspd();
+  }
+}
+
+void
+FGEnvironment::set_thermal_lift_fps (double th)
+{
+  thermal_lift_fps = th;
+  if( live_update ) {
+    _recalc_updraft();
+  }
+}
+
+void
+FGEnvironment::set_ridge_lift_fps (double ri)
+{
+  ridge_lift_fps = ri;
+  if( live_update ) {
+    _recalc_updraft();
+}
 }
 
 void
@@ -469,25 +535,31 @@ void
 FGEnvironment::set_elevation_ft (double e)
 {
   elevation_ft = e;
-  _recalc_alt_temperature();
-  _recalc_alt_dewpoint();
-  _recalc_alt_pressure();
-  _recalc_density();
-  _recalc_relative_humidity();
+  if( live_update ) {
+    _recalc_alt_temperature();
+    _recalc_alt_dewpoint();
+    _recalc_alt_pressure();
+    _recalc_density();
+    _recalc_relative_humidity();
+  }
 }
 
 void
 FGEnvironment::set_altitude_half_to_sun_m (double alt)
 {
- altitude_half_to_sun_m = alt;
- _recalc_density_tropo_avg_kgm3();
+  altitude_half_to_sun_m = alt;
+  if( live_update ) {
+    _recalc_density_tropo_avg_kgm3();
+  }
 }
 
 void
 FGEnvironment::set_altitude_tropo_top_m (double alt)
 {
- altitude_tropo_top_m = alt;
- _recalc_density_tropo_avg_kgm3();
+  altitude_tropo_top_m = alt;
+  if( live_update ) {
+    _recalc_density_tropo_avg_kgm3();
+  }
 }
 
 
@@ -536,6 +608,12 @@ FGEnvironment::_recalc_ne ()
     sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
 }
 
+void
+FGEnvironment::_recalc_updraft ()
+{
+  wind_from_down_fps = thermal_lift_fps + ridge_lift_fps ;
+}
+
 void
 FGEnvironment::_recalc_sl_temperature ()
 {
@@ -634,12 +712,26 @@ FGEnvironment::_recalc_density_tropo_avg_kgm3 ()
 void
 FGEnvironment::_recalc_relative_humidity ()
 {
+/*
   double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc)));
   double sat_vaporpressure = 6.11 * pow(10.0, ((7.5 * temperature_degc)
       / ( 237.7 + temperature_degc)) );
   relative_humidity = 100 * vaporpressure / sat_vaporpressure ;
+
+  with a little algebra, this gets the same result and spares two multiplications and one pow()
+*/
+  double a = (7.5 * dewpoint_degc)    / ( 237.7 + dewpoint_degc);
+  double b = (7.5 * temperature_degc) / ( 237.7 + temperature_degc);
+  relative_humidity = 100 * pow(10,a-b); 
 }
 
+bool
+FGEnvironment::set_live_update( bool _live_update )
+{
+  bool b = live_update;
+  live_update = _live_update;
+  return b;
+}
 
 
 ////////////////////////////////////////////////////////////////////////
@@ -671,6 +763,10 @@ void
 interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
              double fraction, FGEnvironment * result)
 {
+    // don't calculate each internal property every time we set a single value
+    // we trigger that at the end of the interpolation process
+    bool live_update = result->set_live_update( false );
+
     result->set_visibility_m
         (do_interp(env1->get_visibility_m(),
                    env2->get_visibility_m(),
@@ -715,6 +811,16 @@ interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
         (do_interp(env1->get_turbulence_rate_hz(),
                    env2->get_turbulence_rate_hz(),
                    fraction));
+
+    // calculate derived properties here to avoid duplicate expensive computations
+    result->_recalc_ne();
+    result->_recalc_alt_temperature();
+    result->_recalc_alt_dewpoint();
+    result->_recalc_alt_pressure();
+    result->_recalc_density();
+    result->_recalc_relative_humidity();
+
+    result->set_live_update(live_update);
 }
 
 // end of environment.cxx