]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/nav.hxx
Patch from Melchior Franz:
[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( SG_HAVE_NATIVE_SGI_COMPILERS )
38 #  include <iostream.h>
39 #elif defined( __BORLANDC__ )
40 #  include <iostream>
41 #else
42 #  include <istream.h>
43 #endif
44
45 #if ! defined( SG_HAVE_NATIVE_SGI_COMPILERS )
46 SG_USING_STD(istream);
47 #endif
48
49
50 class FGNav {
51
52     char type;
53     double lon, lat;
54     double elev;
55     double x, y, z;
56     int freq;
57     int range;
58     bool has_dme;
59     string ident;               // to avoid a core dump with corrupt data
60     double magvar;              // magvar from true north (negative = W)
61
62     // for failure modeling
63     string trans_ident;         // transmitted ident
64     bool nav_failed;            // nav failed?
65     bool dme_failed;            // dme failed?
66
67 public:
68
69     inline FGNav(void);
70     inline ~FGNav(void) {}
71
72     inline char get_type() const { return type; }
73     inline double get_lon() const { return lon; }
74     inline double get_lat() const { return lat; }
75     inline double get_elev() const { return elev; }
76     inline double get_x() const { return x; }
77     inline double get_y() const { return y; }
78     inline double get_z() const { return z; }
79     inline int get_freq() const { return freq; }
80     inline int get_range() const { return range; }
81     inline bool get_has_dme() const { return has_dme; }
82     inline const char *get_ident() { return ident.c_str(); }
83     inline string get_trans_ident() { return trans_ident; }
84     inline double get_magvar () const { return magvar; }
85
86     /* inline void set_type( char t ) { type = t; }
87     inline void set_lon( double l ) { lon = l; }
88     inline void set_lat( double l ) { lat = l; }
89     inline void set_elev( double e ) { elev = e; }
90     inline void set_freq( int f ) { freq = f; }
91     inline void set_range( int r ) { range = r; }
92     inline void set_dme( bool b ) { dme = b; }
93     inline void set_ident( char *i ) { strncpy( ident, i, 5 ); } */
94
95     friend istream& operator>> ( istream&, FGNav& );
96 };
97
98
99 inline
100 FGNav::FGNav(void) :
101     type(0),
102     lon(0.0), lat(0.0),
103     elev(0.0),
104     x(0.0), y(0.0), z(0.0),
105     freq(0),
106     range(0),
107     has_dme(false),
108     ident(""),
109     magvar(0.0),
110     trans_ident(""),
111     nav_failed(false),
112     dme_failed(false)
113 {
114 }
115
116
117 inline istream&
118 operator >> ( istream& in, FGNav& n )
119 {
120     double f;
121     char c /* , magvar_dir */ ;
122     string magvar_s;
123
124     static bool first_time = true;
125     static double julian_date = 0;
126     static const double MJD0    = 2415020.0;
127     if ( first_time ) {
128         julian_date = sgTimeCurrentMJD(0,0) + MJD0;
129         first_time = false;
130     }
131
132     in >> n.type;
133     
134     if ( n.type == '[' )
135       return in >> skipeol;
136
137     in >> n.lat >> n.lon >> n.elev >> f >> n.range 
138        >> c >> n.ident >> magvar_s;
139
140     n.freq = (int)(f*100.0 + 0.5);
141     if ( c == 'Y' ) {
142         n.has_dme = true;
143     } else {
144         n.has_dme = false;
145     }
146
147     // Calculate the magvar from true north.
148     // cout << "Calculating magvar for navaid " << n.ident << endl;
149     if (magvar_s == "XXX") {
150         // default to mag var as of 1990-01-01 (Julian 2447892.5)
151         // cout << "lat = " << n.lat << " lon = " << n.lon << " elev = " 
152         //      << n.elev << " JD = " 
153         //      << julian_date << endl;
154         n.magvar = sgGetMagVar( n.lon * SGD_DEGREES_TO_RADIANS,
155                                 n.lat * SGD_DEGREES_TO_RADIANS,
156                                 n.elev * SG_FEET_TO_METER,
157                                 julian_date )
158             * SGD_RADIANS_TO_DEGREES;
159         // cout << "Default variation at " << n.lon << ',' << n.lat
160         //      << " is " << var << endl;
161 #if 0
162         // I don't know what this is for - CLO 1 Feb 2001
163         if (var - int(var) >= 0.5)
164             n.magvar = int(var) + 1;
165         else if (var - int(var) <= -0.5)
166             n.magvar = int(var) - 1;
167         else
168             n.magvar = int(var);
169 #endif
170         // cout << "Defaulted to magvar of " << n.magvar << endl;
171     } else {
172         char direction;
173         int var;
174         sscanf(magvar_s.c_str(), "%d%c", &var, &direction);
175         n.magvar = var;
176         if (direction == 'W')
177             n.magvar = -n.magvar;
178         // cout << "Explicit magvar of " << n.magvar << endl;
179     }
180     // cout << n.ident << " " << n.magvar << endl;
181
182     // generate cartesian coordinates
183     Point3D geod( n.lon * SGD_DEGREES_TO_RADIANS, n.lat * SGD_DEGREES_TO_RADIANS, n.elev );
184     Point3D cart = sgGeodToCart( geod );
185     n.x = cart.x();
186     n.y = cart.y();
187     n.z = cart.z();
188
189     n.trans_ident = n.ident;
190     n.nav_failed = n.dme_failed = false;
191
192     return in >> skipeol;
193 }
194
195
196 #endif // _FG_NAV_HXX