]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/mkrbeacons.cxx
Oops ...
[flightgear.git] / src / Navaids / mkrbeacons.cxx
1 // mkrbeacons.cxx -- marker beacon management class
2 //
3 // Written by Curtis Olson, started March 2001.
4 //
5 // Copyright (C) 2001  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 #include "mkrbeacons.hxx"
25
26
27 // constructor
28 FGBeacon::FGBeacon() {
29     FGBeacon( 0, 0, 0, NOBEACON );
30 }
31
32 FGBeacon::FGBeacon( double _lon, double _lat, double _elev,
33                     fgMkrBeacType _type ) {
34     lon = _lon;
35     lat = _lat;
36     elev = _elev;
37     type = _type;
38
39     Point3D pos = sgGeodToCart(Point3D(lon * DEG_TO_RAD, lat * DEG_TO_RAD, 0));
40     // cout << "pos = " << pos << endl;
41     x = pos.x();
42     y = pos.y();
43     z = pos.z();
44 }
45
46 // destructor
47 FGBeacon::~FGBeacon() {
48 }
49
50
51 // constructor
52 FGMarkerBeacons::FGMarkerBeacons() {
53 }
54
55 // destructor
56 FGMarkerBeacons::~FGMarkerBeacons() {
57 }
58
59
60 // initialize the structures
61 bool FGMarkerBeacons::init() {
62     // erase the beacon map
63     beacon_map.clear();
64
65     return true;
66 }
67
68
69 // real add a marker beacon
70 bool FGMarkerBeacons::real_add( const int master_index, const FGBeacon& b ) {
71     // cout << "Master index = " << master_index << endl;
72     beacon_map[master_index].push_back( b );
73     
74     return true;
75 }
76
77
78 // front end for add a marker beacon
79 bool FGMarkerBeacons::add( double lon, double lat, double elev,
80                            FGBeacon::fgMkrBeacType type ) {
81     double diff;
82
83     int lonidx = (int)lon;
84     diff = lon - (double)lonidx;
85     if ( (lon < 0.0) && (fabs(diff) > FG_EPSILON) ) {
86         lonidx -= 1;
87     }
88     double lonfrac = lon - (double)lonidx;
89     lonidx += 180;
90
91     int latidx = (int)lat;
92     diff = lat - (double)latidx;
93     if ( (lat < 0.0) && (fabs(diff) > FG_EPSILON) ) {
94         latidx -= 1;
95     }
96     double latfrac = lat - (double)latidx;
97     latidx += 90;
98
99     int master_index = lonidx * 1000 + latidx;
100     FGBeacon b( lon, lat, elev, type );
101
102     // add to the actual bucket
103     real_add( master_index, b );
104
105     // if we are close to the edge, add to adjacent buckets so we only
106     // have to search one bucket at run time
107
108     // there are 8 cases since there are 8 adjacent tiles
109
110     if ( lonfrac < 0.2 ) {
111         real_add( master_index - 1000, b );
112         if ( latfrac < 0.2 ) {
113             real_add( master_index - 1000 - 1, b );
114         } else if ( latfrac > 0.8 ) {
115             real_add( master_index - 1000 + 1, b );
116         }
117     } else if ( lonfrac > 0.8 ) {
118         real_add( master_index + 1000, b );
119         if ( latfrac < 0.2 ) {
120             real_add( master_index + 1000 - 1, b );
121         } else if ( latfrac > 0.8 ) {
122             real_add( master_index+- 1000 + 1, b );
123         }
124     } else if ( latfrac < 0.2 ) {
125         real_add( master_index - 1, b );
126     } else if ( latfrac > 0.8 ) {
127         real_add( master_index + 1, b );
128     }
129
130     return true;
131 }
132
133
134 // returns marker beacon type if we are over a marker beacon, NOBEACON
135 // otherwise
136 FGBeacon::fgMkrBeacType FGMarkerBeacons::query( double lon, double lat,
137                                                 double elev ) {
138     double diff;
139
140     int lonidx = (int)lon;
141     diff = lon - (double)lonidx;
142     if ( (lon < 0.0) && (fabs(diff) > FG_EPSILON) ) {
143         lonidx -= 1;
144     }
145     lonidx += 180;
146
147     int latidx = (int)lat;
148     diff = lat - (double)latidx;
149     if ( (lat < 0.0) && (fabs(diff) > FG_EPSILON) ) {
150         latidx -= 1;
151     }
152     latidx += 90;
153
154     int master_index = lonidx * 1000 + latidx;
155
156     beacon_list_type beacons = beacon_map[ master_index ];
157     // cout << "Master index = " << master_index << endl;
158     // cout << "beacon search length = " << beacons.size() << endl;
159
160     beacon_list_iterator current = beacons.begin();
161     beacon_list_iterator last = beacons.end();
162
163     Point3D aircraft = sgGeodToCart(Point3D(lon*DEG_TO_RAD, lat*DEG_TO_RAD, 0));
164
165     double min_dist = 999999999.0;
166
167     for ( ; current != last ; ++current ) {
168         // cout << "  testing " << current->get_locident() << endl;
169         Point3D station = Point3D( current->get_x(),
170                                    current->get_y(),
171                                    current->get_z() );
172         // cout << "    aircraft = " << aircraft << " station = " << station 
173         //      << endl;
174
175         double d = aircraft.distance3Dsquared( station );
176         // cout << "  distance = " << d << " (" 
177         //      << FG_ILS_DEFAULT_RANGE * NM_TO_METER 
178         //         * FG_ILS_DEFAULT_RANGE * NM_TO_METER
179         //      << ")" << endl;
180
181         // cout << "  range = " << sqrt(d) << endl;
182
183         if ( d < min_dist ) {
184             min_dist = d;
185         }
186
187         // cout << "elev = " << elev * METER_TO_FEET
188         //      << " current->get_elev() = " << current->get_elev() << endl;
189         double delev = elev * METER_TO_FEET - current->get_elev();
190         double maxrange = 4200 * delev / 1000;
191         // cout << "delev = " << delev << " maxrange = " << maxrange << endl;
192
193         // match up to twice the published range so we can model
194         // reduced signal strength
195         if ( d < maxrange * maxrange ) {
196             // cout << "lon = " << lon << " lat = " << lat
197             //      << "  closest beacon = " << sqrt( min_dist ) << endl;
198             return current->get_type();
199         }
200     }
201
202     // cout << "lon = " << lon << " lat = " << lat
203     //      << "  closest beacon = " << sqrt( min_dist ) << endl;
204
205     return FGBeacon::NOBEACON;
206 }
207
208
209 FGMarkerBeacons *current_beacons;