]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/nav.hxx
Removed FGEnvironmentMgr as a special case in globals, initialization,
[flightgear.git] / src / Navaids / nav.hxx
1 // nav.hxx -- vor/dme/ndb class
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _FG_NAV_HXX
25 #define _FG_NAV_HXX
26
27 #include <stdio.h>
28
29 #include <simgear/compiler.h>
30 #include <simgear/math/sg_geodesy.hxx>
31 #include <simgear/misc/sgstream.hxx>
32 #include <simgear/magvar/magvar.hxx>
33 #include <simgear/timing/sg_time.hxx>
34
35 #ifdef SG_HAVE_STD_INCLUDES
36 #  include <istream>
37 #elif defined( __BORLANDC__ ) || (__APPLE__)
38 #  include <iostream>
39 #else
40 #  include <istream.h>
41 #endif
42
43 SG_USING_STD(istream);
44
45
46 class FGNav {
47
48     char type;
49     double lon, lat;
50     double elev;
51     double x, y, z;
52     int freq;
53     int range;
54     bool has_dme;
55     string ident;               // to avoid a core dump with corrupt data
56     double magvar;              // magvar from true north (negative = W)
57
58     // for failure modeling
59     string trans_ident;         // transmitted ident
60     bool nav_failed;            // nav failed?
61     bool dme_failed;            // dme failed?
62
63 public:
64
65     inline FGNav(void);
66     inline ~FGNav(void) {}
67
68     inline char get_type() const { return type; }
69     inline double get_lon() const { return lon; }
70     inline double get_lat() const { return lat; }
71     inline double get_elev() const { return elev; }
72     inline double get_x() const { return x; }
73     inline double get_y() const { return y; }
74     inline double get_z() const { return z; }
75     inline int get_freq() const { return freq; }
76     inline int get_range() const { return range; }
77     inline bool get_has_dme() const { return has_dme; }
78     inline const char *get_ident() { return ident.c_str(); }
79     inline string get_trans_ident() { return trans_ident; }
80     inline double get_magvar () const { return magvar; }
81
82     friend istream& operator>> ( istream&, FGNav& );
83 };
84
85
86 inline
87 FGNav::FGNav(void) :
88     type(0),
89     lon(0.0), lat(0.0),
90     elev(0.0),
91     x(0.0), y(0.0), z(0.0),
92     freq(0),
93     range(0),
94     has_dme(false),
95     ident(""),
96     magvar(0.0),
97     trans_ident(""),
98     nav_failed(false),
99     dme_failed(false)
100 {
101 }
102
103
104 inline istream&
105 operator >> ( istream& in, FGNav& n )
106 {
107     double f;
108     char c /* , magvar_dir */ ;
109     string magvar_s;
110
111     static bool first_time = true;
112     static double julian_date = 0;
113     static const double MJD0    = 2415020.0;
114     if ( first_time ) {
115         julian_date = sgTimeCurrentMJD(0,0) + MJD0;
116         first_time = false;
117     }
118
119     in >> n.type;
120     
121     if ( n.type == '[' )
122       return in >> skipeol;
123
124     in >> n.lat >> n.lon >> n.elev >> f >> n.range 
125        >> c >> n.ident >> magvar_s;
126
127     n.freq = (int)(f*100.0 + 0.5);
128     if ( c == 'Y' ) {
129         n.has_dme = true;
130     } else {
131         n.has_dme = false;
132     }
133
134     // Calculate the magvar from true north.
135     // cout << "Calculating magvar for navaid " << n.ident << endl;
136     if (magvar_s == "XXX") {
137         // default to mag var as of 1990-01-01 (Julian 2447892.5)
138         // cout << "lat = " << n.lat << " lon = " << n.lon << " elev = " 
139         //      << n.elev << " JD = " 
140         //      << julian_date << endl;
141         n.magvar = sgGetMagVar( n.lon * SGD_DEGREES_TO_RADIANS,
142                                 n.lat * SGD_DEGREES_TO_RADIANS,
143                                 n.elev * SG_FEET_TO_METER,
144                                 julian_date )
145             * SGD_RADIANS_TO_DEGREES;
146         // cout << "Default variation at " << n.lon << ',' << n.lat
147         //      << " is " << var << endl;
148 #if 0
149         // I don't know what this is for - CLO 1 Feb 2001
150         if (var - int(var) >= 0.5)
151             n.magvar = int(var) + 1;
152         else if (var - int(var) <= -0.5)
153             n.magvar = int(var) - 1;
154         else
155             n.magvar = int(var);
156 #endif
157         // cout << "Defaulted to magvar of " << n.magvar << endl;
158     } else {
159         char direction;
160         int var;
161         sscanf(magvar_s.c_str(), "%d%c", &var, &direction);
162         n.magvar = var;
163         if (direction == 'W')
164             n.magvar = -n.magvar;
165         // cout << "Explicit magvar of " << n.magvar << endl;
166     }
167     // cout << n.ident << " " << n.magvar << endl;
168
169     // generate cartesian coordinates
170     Point3D geod( n.lon * SGD_DEGREES_TO_RADIANS, n.lat * SGD_DEGREES_TO_RADIANS, n.elev );
171     Point3D cart = sgGeodToCart( geod );
172     n.x = cart.x();
173     n.y = cart.y();
174     n.z = cart.z();
175
176     n.trans_ident = n.ident;
177     n.nav_failed = n.dme_failed = false;
178
179     return in >> skipeol;
180 }
181
182
183 #endif // _FG_NAV_HXX