]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/nav.hxx
84bd8132ee28f67cf3c9bf18d44c9c9bdfcf36b7
[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
34 #ifdef FG_HAVE_STD_INCLUDES
35 #  include <istream>
36 #elif defined( FG_HAVE_NATIVE_SGI_COMPILERS )
37 #  include <iostream.h>
38 #elif defined( __BORLANDC__ )
39 #  include <iostream>
40 #else
41 #  include <istream.h>
42 #endif
43
44 #if ! defined( FG_HAVE_NATIVE_SGI_COMPILERS )
45 FG_USING_STD(istream);
46 #endif
47
48
49 class FGNav {
50
51     char type;
52     double lon, lat;
53     double elev;
54     double x, y, z;
55     int freq;
56     int range;
57     bool has_dme;
58     string ident;               // to avoid a core dump with corrupt data
59     double magvar;              // magvar from true north (negative = W)
60
61 public:
62
63     inline FGNav(void) {}
64     inline ~FGNav(void) {}
65
66     inline char get_type() const { return type; }
67     inline double get_lon() const { return lon; }
68     inline double get_lat() const { return lat; }
69     inline double get_elev() const { return elev; }
70     inline double get_x() const { return x; }
71     inline double get_y() const { return y; }
72     inline double get_z() const { return z; }
73     inline int get_freq() const { return freq; }
74     inline int get_range() const { return range; }
75     inline bool get_has_dme() const { return has_dme; }
76     inline const char *get_ident() { return ident.c_str(); }
77     inline double get_magvar () const { return magvar; }
78
79     /* inline void set_type( char t ) { type = t; }
80     inline void set_lon( double l ) { lon = l; }
81     inline void set_lat( double l ) { lat = l; }
82     inline void set_elev( double e ) { elev = e; }
83     inline void set_freq( int f ) { freq = f; }
84     inline void set_range( int r ) { range = r; }
85     inline void set_dme( bool b ) { dme = b; }
86     inline void set_ident( char *i ) { strncpy( ident, i, 5 ); } */
87
88     friend istream& operator>> ( istream&, FGNav& );
89 };
90
91
92 inline istream&
93 operator >> ( istream& in, FGNav& n )
94 {
95     double f;
96     char c /* , magvar_dir */ ;
97     string magvar_s;
98     
99     in >> n.type >> n.lat >> n.lon >> n.elev >> f >> n.range 
100        >> c >> n.ident >> magvar_s;
101
102     n.freq = (int)(f*100.0 + 0.5);
103     if ( c == 'Y' ) {
104         n.has_dme = true;
105     } else {
106         n.has_dme = false;
107     }
108
109     // Calculate the magvar from true north.
110     // cout << "Calculating magvar for navaid " << n.ident << endl;
111     if (magvar_s == "XXX") {
112         // default to mag var as of 1990-01-01 (Julian 2447892.5)
113         n.magvar = -sgGetMagVar(n.lon * DEG_TO_RAD, n.lat * DEG_TO_RAD,
114                                 n.elev * FEET_TO_METER,
115                                 2447892.5) * RAD_TO_DEG;
116         // cout << "Default variation at " << n.lon << ',' << n.lat
117         //      << " is " << var << endl;
118 #if 0
119         // I don't know what this is for - CLO 1 Feb 2001
120         if (var - int(var) >= 0.5)
121             n.magvar = int(var) + 1;
122         else if (var - int(var) <= -0.5)
123             n.magvar = int(var) - 1;
124         else
125             n.magvar = int(var);
126 #endif
127         // cout << "Defaulted to magvar of " << n.magvar << endl;
128     } else {
129         char direction;
130         int var;
131         sscanf(magvar_s.c_str(), "%d%c", &var, &direction);
132         n.magvar = var;
133         if (direction == 'E')
134             n.magvar = 0 - n.magvar;
135         // cout << "Explicit magvar of " << n.magvar << endl;
136     }
137     cout << n.ident << " " << n.magvar << endl;
138
139     // generate cartesian coordinates
140     Point3D geod( n.lon * DEG_TO_RAD, n.lat * DEG_TO_RAD, n.elev );
141     Point3D cart = sgGeodToCart( geod );
142     n.x = cart.x();
143     n.y = cart.y();
144     n.z = cart.z();
145
146     return in >> skipeol;
147 }
148
149
150 #endif // _FG_NAV_HXX