]> git.mxchange.org Git - flightgear.git/commitdiff
Lazy caching of magvar on waypoints. Not used yet, but soon.
authorJames Turner <zakalawe@mac.com>
Thu, 20 Jan 2011 08:49:09 +0000 (08:49 +0000)
committerJames Turner <zakalawe@mac.com>
Thu, 20 Jan 2011 08:49:09 +0000 (08:49 +0000)
src/Navaids/route.cxx
src/Navaids/route.hxx
src/Navaids/waypoint.hxx

index cfe25de40b40ac0753d9197cf09519ff9f962164..0dc9b2c0fb734d7a471bcc1adc4dc276ab272c2c 100644 (file)
 #include <simgear/structure/exception.hxx>
 #include <simgear/xml/easyxml.hxx>
 #include <simgear/misc/sg_path.hxx>
+#include <simgear/magvar/magvar.hxx>
+#include <simgear/timing/sg_time.hxx>
 
 // FlightGear
+#include <Main/globals.hxx>
 #include <Navaids/procedure.hxx>
 #include <Navaids/waypoint.hxx>
 #include <Airports/simple.hxx>
@@ -49,13 +52,16 @@ using std::fstream;
 
 namespace flightgear {
 
+const double NO_MAG_VAR = -1000.0; // an impossible mag-var value
+
 Waypt::Waypt(Route* aOwner) :
   _altitudeFt(0.0),
   _speed(0.0),
   _altRestrict(RESTRICT_NONE),
   _speedRestrict(RESTRICT_NONE),
   _owner(aOwner),
-  _flags(0)
+  _flags(0),
+  _magVarDeg(NO_MAG_VAR)
 {
 }
 
@@ -127,6 +133,19 @@ Waypt::courseAndDistanceFrom(const SGGeod& aPos) const
   SGGeodesy::inverse(aPos, position(), course, az2, distance);
   return std::make_pair(course, distance);
 }
+
+double Waypt::magvarDeg() const
+{
+  if (_magVarDeg == NO_MAG_VAR) {
+    // derived classes with a default pos must override this method
+    assert(!(position() == SGGeod()));
+    
+    double jd = globals->get_time_params()->getJD();
+    _magVarDeg = sgGetMagVar(position(), jd);
+  }
+  
+  return _magVarDeg;
+}
   
 ///////////////////////////////////////////////////////////////////////////
 // persistence
index b0375d15c980c68d00b76ce255bf5ee4516d6dd4..cd2114f6d55ffe8590db50aa30d5cd50d097ce0a 100644 (file)
@@ -156,6 +156,12 @@ public:
   bool matches(const SGGeod& aPos) const;
   
   virtual std::string type() const = 0;
+  
+  /**
+   * Magentic variation at/in the vicinity of the waypoint.
+   * For some waypoint types this will always return 0.
+   */
+  virtual double magvarDeg() const;
 protected:
   friend class NavdataVisitor;
   
@@ -187,7 +193,7 @@ private:
 
        Route* _owner;
        unsigned short _flags;
-       
+  mutable double _magVarDeg; 
 };
 
 typedef std::vector<WayptRef> WayptVec;
index 7b5f30f09e228372a6e24a57119bb57b60552f49..0b2ae7d952eeca6e88fec85224b53e9bb4fc7cb0 100644 (file)
@@ -206,6 +206,9 @@ public:
   double headingDegMagnetic() const
     { return _magHeading; }
   
+  virtual double magvarDeg() const
+    { return 0.0; }
+  
 private:
   std::string _ident;
   double _magHeading;