]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/nav.hxx
Added some sanity checking on max number of bottons and axes.
[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/fgstream.hxx>
32 #include <simgear/magvar/magvar.hxx>
33 #include <simgear/timing/sg_time.hxx>
34
35 #ifdef FG_HAVE_STD_INCLUDES
36 #  include <istream>
37 #elif defined( FG_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( FG_HAVE_NATIVE_SGI_COMPILERS )
46 FG_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 public:
63
64     inline FGNav(void) {}
65     inline ~FGNav(void) {}
66
67     inline char get_type() const { return type; }
68     inline double get_lon() const { return lon; }
69     inline double get_lat() const { return lat; }
70     inline double get_elev() const { return elev; }
71     inline double get_x() const { return x; }
72     inline double get_y() const { return y; }
73     inline double get_z() const { return z; }
74     inline int get_freq() const { return freq; }
75     inline int get_range() const { return range; }
76     inline bool get_has_dme() const { return has_dme; }
77     inline const char *get_ident() { return ident.c_str(); }
78     inline double get_magvar () const { return magvar; }
79
80     /* inline void set_type( char t ) { type = t; }
81     inline void set_lon( double l ) { lon = l; }
82     inline void set_lat( double l ) { lat = l; }
83     inline void set_elev( double e ) { elev = e; }
84     inline void set_freq( int f ) { freq = f; }
85     inline void set_range( int r ) { range = r; }
86     inline void set_dme( bool b ) { dme = b; }
87     inline void set_ident( char *i ) { strncpy( ident, i, 5 ); } */
88
89     friend istream& operator>> ( istream&, FGNav& );
90 };
91
92
93 inline istream&
94 operator >> ( istream& in, FGNav& n )
95 {
96     double f;
97     char c /* , magvar_dir */ ;
98     string magvar_s;
99
100     static SGTime time_params;
101     static bool first_time = true;
102     static double julian_date = 0;
103     if ( first_time ) {
104         time_params.update( 0.0, 0.0, 0 );
105         julian_date = time_params.getJD();
106         first_time = false;
107     }
108
109     in >> n.type >> n.lat >> n.lon >> n.elev >> f >> n.range 
110        >> c >> n.ident >> magvar_s;
111
112     n.freq = (int)(f*100.0 + 0.5);
113     if ( c == 'Y' ) {
114         n.has_dme = true;
115     } else {
116         n.has_dme = false;
117     }
118
119     // Calculate the magvar from true north.
120     // cout << "Calculating magvar for navaid " << n.ident << endl;
121     if (magvar_s == "XXX") {
122         // default to mag var as of 1990-01-01 (Julian 2447892.5)
123         // cout << "lat = " << n.lat << " lon = " << n.lon << " elev = " 
124         //      << n.elev << " JD = " 
125         //      << julian_date << endl;
126         n.magvar = sgGetMagVar(n.lon * DEG_TO_RAD, n.lat * DEG_TO_RAD,
127                                 n.elev * FEET_TO_METER,
128                                 julian_date) * RAD_TO_DEG;
129         // cout << "Default variation at " << n.lon << ',' << n.lat
130         //      << " is " << var << endl;
131 #if 0
132         // I don't know what this is for - CLO 1 Feb 2001
133         if (var - int(var) >= 0.5)
134             n.magvar = int(var) + 1;
135         else if (var - int(var) <= -0.5)
136             n.magvar = int(var) - 1;
137         else
138             n.magvar = int(var);
139 #endif
140         // cout << "Defaulted to magvar of " << n.magvar << endl;
141     } else {
142         char direction;
143         int var;
144         sscanf(magvar_s.c_str(), "%d%c", &var, &direction);
145         n.magvar = var;
146         if (direction == 'W')
147             n.magvar = -n.magvar;
148         // cout << "Explicit magvar of " << n.magvar << endl;
149     }
150     // cout << n.ident << " " << n.magvar << endl;
151
152     // generate cartesian coordinates
153     Point3D geod( n.lon * DEG_TO_RAD, n.lat * DEG_TO_RAD, n.elev );
154     Point3D cart = sgGeodToCart( geod );
155     n.x = cart.x();
156     n.y = cart.y();
157     n.z = cart.z();
158
159     return in >> skipeol;
160 }
161
162
163 #endif // _FG_NAV_HXX