]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment.cxx
Move viewer-related sources to separate folder.
[flightgear.git] / src / Environment / environment.cxx
index a83b7de1a257c711320de86e5e28996dab422117..95b45852ebb2ac37400c40e7500aff67dc754776 100644 (file)
@@ -18,8 +18,6 @@
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
-// $Id$
-
 
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 
 #include <boost/tuple/tuple.hpp>
 
-#include <simgear/constants.h>
-#include <simgear/debug/logstream.hxx>
-#include <simgear/math/interpolater.hxx>
 #include <simgear/props/props.hxx>
-#include <simgear/environment/visual_enviro.hxx>
 
 #include <Main/fg_props.hxx>
-#include <signal.h>
 
 #include "environment.hxx"
 #include "atmosphere.hxx"
@@ -138,8 +131,6 @@ void FGEnvironment::_init()
     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;
 #ifdef USING_TABLES
@@ -163,6 +154,13 @@ FGEnvironment::FGEnvironment (const FGEnvironment &env)
 
 FGEnvironment::~FGEnvironment()
 {
+    Untie();
+}
+
+FGEnvironment & FGEnvironment::operator = ( const FGEnvironment & other )
+{
+    copy( other );
+    return *this;
 }
 
 void
@@ -180,10 +178,15 @@ 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;
+    pressure_inhg = env.pressure_inhg;
+    density_slugft3 = env.density_slugft3;
+    density_tropo_avg_kgm3 = env.density_tropo_avg_kgm3;
+    relative_humidity = env.relative_humidity;
+    altitude_half_to_sun_m = env.altitude_half_to_sun_m;
+    altitude_tropo_top_m = env.altitude_tropo_top_m;
+    live_update = env.live_update;
 }
 
 static inline bool
@@ -209,20 +212,32 @@ FGEnvironment::read (const SGPropertyNode * node)
     maybe_copy_value(this, node, "visibility-m",
                      &FGEnvironment::set_visibility_m);
 
+    maybe_copy_value(this, node, "elevation-ft",
+                     &FGEnvironment::set_elevation_ft);
+
     if (!maybe_copy_value(this, node, "temperature-sea-level-degc",
-                          &FGEnvironment::set_temperature_sea_level_degc))
-        maybe_copy_value(this, node, "temperature-degc",
-                         &FGEnvironment::set_temperature_degc);
+                          &FGEnvironment::set_temperature_sea_level_degc)) {
+        if( maybe_copy_value(this, node, "temperature-degc",
+                         &FGEnvironment::set_temperature_degc)) {
+            _recalc_sl_temperature();
+        }
+    }
 
     if (!maybe_copy_value(this, node, "dewpoint-sea-level-degc",
-                          &FGEnvironment::set_dewpoint_sea_level_degc))
-        maybe_copy_value(this, node, "dewpoint-degc",
-                         &FGEnvironment::set_dewpoint_degc);
+                          &FGEnvironment::set_dewpoint_sea_level_degc)) {
+        if( maybe_copy_value(this, node, "dewpoint-degc",
+                         &FGEnvironment::set_dewpoint_degc)) {
+            _recalc_sl_dewpoint();
+        }
+    }
 
     if (!maybe_copy_value(this, node, "pressure-sea-level-inhg",
-                          &FGEnvironment::set_pressure_sea_level_inhg))
-        maybe_copy_value(this, node, "pressure-inhg",
-                         &FGEnvironment::set_pressure_inhg);
+                          &FGEnvironment::set_pressure_sea_level_inhg)) {
+        if( maybe_copy_value(this, node, "pressure-inhg",
+                         &FGEnvironment::set_pressure_inhg)) {
+            _recalc_sl_pressure();
+        }
+   }
 
     maybe_copy_value(this, node, "wind-from-heading-deg",
                      &FGEnvironment::set_wind_from_heading_deg);
@@ -230,14 +245,12 @@ FGEnvironment::read (const SGPropertyNode * node)
     maybe_copy_value(this, node, "wind-speed-kt",
                      &FGEnvironment::set_wind_speed_kt);
 
-    maybe_copy_value(this, node, "elevation-ft",
-                     &FGEnvironment::set_elevation_ft);
-
     maybe_copy_value(this, node, "turbulence/magnitude-norm",
                      &FGEnvironment::set_turbulence_magnitude_norm);
 
     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_pt();
@@ -248,6 +261,107 @@ FGEnvironment::read (const SGPropertyNode * node)
     set_live_update(live_update);
 }
 
+void FGEnvironment::Tie( SGPropertyNode_ptr base, bool archivable )
+{
+  _tiedProperties.setRoot( base );
+
+  _tiedProperties.Tie( "visibility-m", this, 
+      &FGEnvironment::get_visibility_m, 
+      &FGEnvironment::set_visibility_m)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("temperature-sea-level-degc", this, 
+      &FGEnvironment::get_temperature_sea_level_degc, 
+      &FGEnvironment::set_temperature_sea_level_degc)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("temperature-degc", this, 
+      &FGEnvironment::get_temperature_degc,
+      &FGEnvironment::set_temperature_degc)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("temperature-degf", this, 
+      &FGEnvironment::get_temperature_degf);
+
+  _tiedProperties.Tie("dewpoint-sea-level-degc", this, 
+      &FGEnvironment::get_dewpoint_sea_level_degc, 
+      &FGEnvironment::set_dewpoint_sea_level_degc)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("dewpoint-degc", this, 
+      &FGEnvironment::get_dewpoint_degc,
+      &FGEnvironment::set_dewpoint_degc)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("pressure-sea-level-inhg", this, 
+      &FGEnvironment::get_pressure_sea_level_inhg, 
+      &FGEnvironment::set_pressure_sea_level_inhg)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("pressure-inhg", this, 
+      &FGEnvironment::get_pressure_inhg,
+      &FGEnvironment::set_pressure_inhg)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("density-slugft3", this, 
+      &FGEnvironment::get_density_slugft3); // read-only
+
+  _tiedProperties.Tie("relative-humidity", this, 
+      &FGEnvironment::get_relative_humidity); //ro
+
+  _tiedProperties.Tie("atmosphere/density-tropo-avg", this, 
+      &FGEnvironment::get_density_tropo_avg_kgm3); //ro
+
+  _tiedProperties.Tie("atmosphere/altitude-half-to-sun", this, 
+      &FGEnvironment::get_altitude_half_to_sun_m, 
+      &FGEnvironment::set_altitude_half_to_sun_m)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("atmosphere/altitude-troposphere-top", this, 
+      &FGEnvironment::get_altitude_tropo_top_m, 
+      &FGEnvironment::set_altitude_tropo_top_m)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("wind-from-heading-deg", this, 
+      &FGEnvironment::get_wind_from_heading_deg, 
+      &FGEnvironment::set_wind_from_heading_deg)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("wind-speed-kt", this, 
+      &FGEnvironment::get_wind_speed_kt, 
+      &FGEnvironment::set_wind_speed_kt)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("wind-from-north-fps", this, 
+      &FGEnvironment::get_wind_from_north_fps, 
+      &FGEnvironment::set_wind_from_north_fps)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("wind-from-east-fps", this, 
+      &FGEnvironment::get_wind_from_east_fps, 
+      &FGEnvironment::set_wind_from_east_fps)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("wind-from-down-fps", this, 
+      &FGEnvironment::get_wind_from_down_fps, 
+      &FGEnvironment::set_wind_from_down_fps)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("turbulence/magnitude-norm", this, 
+      &FGEnvironment::get_turbulence_magnitude_norm, 
+      &FGEnvironment::set_turbulence_magnitude_norm)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+
+  _tiedProperties.Tie("turbulence/rate-hz", this, 
+      &FGEnvironment::get_turbulence_rate_hz, 
+      &FGEnvironment::set_turbulence_rate_hz)
+      ->setAttribute( SGPropertyNode::ARCHIVE, archivable );
+}
+
+void FGEnvironment::Untie()
+{
+    _tiedProperties.Untie();
+}
 
 double
 FGEnvironment::get_visibility_m () const
@@ -357,24 +471,9 @@ 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
 {
-  if( sgEnviro.get_turbulence_enable_state() )
-    if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
-      return sgEnviro.get_cloud_turbulence();
   return turbulence_magnitude_norm;
 }
 
@@ -509,24 +608,6 @@ FGEnvironment::set_wind_from_down_fps (double d)
   }
 }
 
-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
 FGEnvironment::set_turbulence_magnitude_norm (double t)
 {
@@ -573,34 +654,15 @@ FGEnvironment::set_altitude_tropo_top_m (double alt)
 void
 FGEnvironment::_recalc_hdgspd ()
 {
-  double angle_rad;
+  wind_from_heading_deg = 
+    atan2(wind_from_east_fps, wind_from_north_fps) * SGD_RADIANS_TO_DEGREES;
 
-  if (wind_from_east_fps == 0) {
-    angle_rad = (wind_from_north_fps >= 0 ? SGD_PI_2 : -SGD_PI_2);
-  } else {
-    angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
-  }
-  wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
-  if (wind_from_east_fps >= 0)
-    wind_from_heading_deg = 90 - wind_from_heading_deg;
-  else
-    wind_from_heading_deg = 270 - wind_from_heading_deg;
+  if( wind_from_heading_deg < 0 )
+    wind_from_heading_deg += 360.0;
 
-#if 0
-  // FIXME: Windspeed can become negative with these formulas.
-  //        which can cause problems for animations that rely
-  //        on the windspeed property.
-  if (angle_rad == 0)
-    wind_speed_kt = fabs(wind_from_east_fps
-                        * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
-  else
-    wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
-      * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
-#else
   wind_speed_kt = sqrt(wind_from_north_fps * wind_from_north_fps +
                        wind_from_east_fps * wind_from_east_fps) 
                   * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
-#endif
 }
 
 void
@@ -615,12 +677,6 @@ 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 ;
-}
-
 // Intended to help with the interpretation of METAR data,
 // not for random in-flight outside-air temperatures.
 void
@@ -629,13 +685,13 @@ FGEnvironment::_recalc_sl_temperature ()
 
 #if 0
   {
-    SG_LOG(SG_GENERAL, SG_DEBUG, "recalc_sl_temperature: using "
+    SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "recalc_sl_temperature: using "
       << temperature_degc << " @ " << elevation_ft << " :: " << this);
   }
 #endif
 
-  if (elevation_ft >= ISA_def[1].height) {
-    SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_temperature: "
+  if (elevation_ft * atmodel::foot >= ISA_def[1].height) {
+    SG_LOG(SG_ENVIRONMENT, SG_ALERT, "recalc_sl_temperature: "
         << "valid only in troposphere, not " << elevation_ft);
     return;
   }
@@ -679,7 +735,7 @@ FGEnvironment::_recalc_sl_pressure ()
   using namespace atmodel;
 #if 0
   {
-    SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_pressure: using "
+    SG_LOG(SG_ENVIRONMENT, SG_ALERT, "recalc_sl_pressure: using "
       << pressure_inhg << " and "
       << temperature_degc << " @ " << elevation_ft << " :: " << this);
   }
@@ -700,17 +756,17 @@ FGEnvironment::_recalc_alt_pt ()
   {
     static int count(0);
     if (++count % 1000 == 0) {
-      SG_LOG(SG_GENERAL, SG_ALERT, 
+      SG_LOG(SG_ENVIRONMENT, SG_ALERT, 
            "recalc_alt_pt for: " << elevation_ft
         << "  using "  << pressure_sea_level_inhg 
         << "  and "  << temperature_sea_level_degc
         << " :: " << this
         << "  # " << count);
-        ///////////////////////////////////raise(SIGUSR1);
     }
   }
 #endif
-  double press, temp;
+  double press = pressure_inhg * inHg;
+  double temp = temperature_degc + freezing;
   boost::tie(press, temp) = PT_vs_hpt(elevation_ft * foot, 
         pressure_sea_level_inhg * inHg, temperature_sea_level_degc + freezing);
   temperature_degc = temp - freezing;
@@ -804,57 +860,57 @@ do_interp_deg (double a, double b, double fraction)
     return fmod(do_interp(a, b, fraction), 360);
 }
 
-void
-interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
-             double fraction, FGEnvironment * result)
+FGEnvironment &
+FGEnvironment::interpolate( const FGEnvironment & env2,
+             double fraction, FGEnvironment * result) const
 {
     // 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(),
+        (do_interp(get_visibility_m(),
+                   env2.get_visibility_m(),
                    fraction));
 
     result->set_temperature_sea_level_degc
-        (do_interp(env1->get_temperature_sea_level_degc(),
-                   env2->get_temperature_sea_level_degc(),
+        (do_interp(get_temperature_sea_level_degc(),
+                   env2.get_temperature_sea_level_degc(),
                    fraction));
 
     result->set_dewpoint_sea_level_degc
-        (do_interp(env1->get_dewpoint_sea_level_degc(),
-                   env2->get_dewpoint_sea_level_degc(),
+        (do_interp(get_dewpoint_sea_level_degc(),
+                   env2.get_dewpoint_sea_level_degc(),
                    fraction));
 
     result->set_pressure_sea_level_inhg
-        (do_interp(env1->get_pressure_sea_level_inhg(),
-                   env2->get_pressure_sea_level_inhg(),
+        (do_interp(get_pressure_sea_level_inhg(),
+                   env2.get_pressure_sea_level_inhg(),
                    fraction));
 
     result->set_wind_from_heading_deg
-        (do_interp_deg(env1->get_wind_from_heading_deg(),
-                       env2->get_wind_from_heading_deg(),
+        (do_interp_deg(get_wind_from_heading_deg(),
+                       env2.get_wind_from_heading_deg(),
                        fraction));
 
     result->set_wind_speed_kt
-        (do_interp(env1->get_wind_speed_kt(),
-                   env2->get_wind_speed_kt(),
+        (do_interp(get_wind_speed_kt(),
+                   env2.get_wind_speed_kt(),
                    fraction));
 
     result->set_elevation_ft
-        (do_interp(env1->get_elevation_ft(),
-                   env2->get_elevation_ft(),
+        (do_interp(get_elevation_ft(),
+                   env2.get_elevation_ft(),
                    fraction));
 
     result->set_turbulence_magnitude_norm
-        (do_interp(env1->get_turbulence_magnitude_norm(),
-                   env2->get_turbulence_magnitude_norm(),
+        (do_interp(get_turbulence_magnitude_norm(),
+                   env2.get_turbulence_magnitude_norm(),
                    fraction));
 
     result->set_turbulence_rate_hz
-        (do_interp(env1->get_turbulence_rate_hz(),
-                   env2->get_turbulence_rate_hz(),
+        (do_interp(get_turbulence_rate_hz(),
+                   env2.get_turbulence_rate_hz(),
                    fraction));
 
     // calculate derived properties here to avoid duplicate expensive computations
@@ -865,6 +921,8 @@ interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
     result->_recalc_relative_humidity();
 
     result->set_live_update(live_update);
+
+    return *result;
 }
 
 // end of environment.cxx