]> git.mxchange.org Git - flightgear.git/blob - GenAirports/main.cxx
Start of scenery revamp project.
[flightgear.git] / GenAirports / main.cxx
1 // main.cxx -- main loop
2 //
3 // Written by Curtis Olson, started March 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU 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 // (Log is kept at end of this file)
23 //
24
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <Include/compiler.h>
31
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35
36 #include <list>
37 #include <stdio.h>
38 #include <string.h>
39 #include STL_STRING
40
41 #include <Bucket/newbucket.hxx>
42 #include <Debug/logstream.hxx>
43 #include <Include/fg_constants.h>
44 #include <Misc/fgstream.hxx>
45
46 #include "area.hxx"
47 #include "convex_hull.hxx"
48
49
50 // write out airport data
51 void write_airport( list_container hull_list, FGBucket b, const string& root,
52                     const bool cut_and_keep ) {
53     char tmp[256];
54
55     long int index = b.gen_index();
56     string base = b.gen_base_path();
57     string path = root + "/Scenery/" + base;
58     string command = "mkdir -p " + path;
59     system( command.c_str() );
60
61     sprintf(tmp, "%ld", index);
62     string aptfile = path + "/" + tmp + ".apt";
63     cout << "apt file = " << aptfile << endl;
64
65     FILE *fd;
66     if ( (fd = fopen(aptfile.c_str(), "a")) == NULL ) {
67         cout << "Cannot open file: " << aptfile << endl;
68         exit(-1);
69     }
70
71     if ( cut_and_keep ) {
72         fprintf( fd, "cut_and_keep\n" );
73     } else {
74         fprintf( fd, "cut_and_ignore\n" );
75     }
76     fprintf( fd, "%d\n", hull_list.size() );
77     // write perimeter polygon
78     list_iterator current = hull_list.begin();
79     list_iterator last = hull_list.end();
80     for ( ; current != last ; ++current ) {
81         fprintf( fd, "%.7f %.7f\n", (*current).lon, (*current).lat );
82     }
83
84     fclose(fd);
85 }
86
87
88 // process and airport + runway list
89 void process_airport( string last_airport, list < string > & runway_list,
90                       const string& root ) {
91     list_container rwy_list, apt_list, hull_list;
92     list_iterator current, last;
93
94     string line_str;
95     double lon, lat;
96     // int i;
97     int count;
98
99     // generate the vertices of all the runways
100     int len, width, hdg, label_hdg, elev;
101     char codes[10];
102     char side;
103     cout << "(apt) " << last_airport;
104     list < string >::iterator last_runway = runway_list.end();
105     for ( list < string >::iterator current_runway = runway_list.begin();
106           current_runway != last_runway ; ++current_runway ) {
107         line_str = (*current_runway);
108         cout << line_str;
109
110         sscanf( line_str.c_str(), "%lf %lf %d %d %d %s %d %c %d\n",
111                 &lon, &lat, &len, &width, &hdg, codes, &label_hdg, 
112                 &side, &elev );
113
114         rwy_list = gen_runway_area( lon, lat, (double)hdg * DEG_TO_RAD, 
115                                     (double)len * FEET_TO_METER,
116                                     (double)width * FEET_TO_METER );
117
118         // add rwy_list to apt_list
119         current = rwy_list.begin();
120         last = rwy_list.end();
121         for ( ; current != last ; ++current ) {
122             apt_list.push_back(*current);
123         }
124     }
125
126     // printf("Runway points in degrees\n");
127     // current = apt_list.begin();
128     // last = apt_list.end();
129     // for ( ; current != last; ++current ) {
130     //   printf( "%.5f %.5f\n", current->lon, current->lat );
131     // }
132     // printf("\n");
133
134     // generate convex hull
135     hull_list = convex_hull(apt_list);
136
137     // find average center, min, and max point of convex hull
138     point2d average, min, max;
139     double sum_x, sum_y;
140     count = hull_list.size();
141     current = hull_list.begin();
142     last = hull_list.end();
143     sum_x = sum_y = 0.0;
144     min.x = min.y = 200.0;
145     max.x = max.y = -200.0;
146     for ( ; current != last; ++current ) {
147         // printf("return = %.6f %.6f\n", (*current).x, (*current).y);
148         sum_x += (*current).x;
149         sum_y += (*current).y;
150
151         if ( (*current).x < min.x ) { min.x = (*current).x; }
152         if ( (*current).y < min.y ) { min.y = (*current).y; }
153         if ( (*current).x > max.x ) { max.x = (*current).x; }
154         if ( (*current).y > max.y ) { max.y = (*current).y; }
155     }
156     average.x = sum_x / count;
157     average.y = sum_y / count;
158
159     // find buckets for center, min, and max points of convex hull.
160     // note to self: self, you should think about checking for runways
161     // that span the data line
162     FGBucket b(average.lon, average.lat);
163     FGBucket b_min(min.x, min.y);
164     FGBucket b_max(max.x, max.y);
165     cout << "Bucket center = " << b << endl;
166     cout << "Bucket min = " << b_min << endl;
167     cout << "Bucket max = " << b_max << endl;
168     
169     if ( b_min == b_max ) {
170         write_airport( hull_list, b, root, true );
171     } else {
172         FGBucket b_cur;
173         int dx, dy, i, j;
174
175         fgBucketDiff(b_min, b_max, &dx, &dy);
176         cout << "airport spans tile boundaries" << endl;
177         cout << "  dx = " << dx << "  dy = " << dy << endl;
178
179         if ( (dx > 1) || (dy > 1) ) {
180             cout << "somethings really wrong!!!!" << endl;
181             exit(-1);
182         }
183
184         for ( j = 0; j <= dy; j++ ) {
185             for ( i = 0; i <= dx; i++ ) {
186                 b_cur = fgBucketOffset(min.x, min.y, i, j);
187                 if ( b_cur == b ) {
188                     write_airport( hull_list, b_cur, root, true );
189                 } else {
190                     write_airport( hull_list, b_cur, root, false );
191                 }
192             }
193         }
194         // string answer; cin >> answer;
195     }
196 }
197
198
199 // reads the apt_full file and extracts and processes the individual
200 // airport records
201 int main( int argc, char **argv ) {
202     list < string > runway_list;
203     string airport, last_airport;
204     string line;
205     char tmp[256];
206
207     fglog().setLogLevels( FG_ALL, FG_DEBUG );
208
209     if ( argc != 3 ) {
210         FG_LOG( FG_GENERAL, FG_ALERT, 
211                 "Usage " << argv[0] << " <apt_file> <work dir>" );
212         exit(-1);
213     }
214
215     fg_gzifstream in( argv[1] );
216     if ( !in ) {
217         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << argv[1] );
218         exit(-1);
219     }
220
221     while ( ! in.eof() ) {
222         in.getline(tmp, 256);
223         line = tmp;
224         // cout << line << endl;
225
226         if ( line.length() == 0 ) {
227             // empty, skip
228         } else if ( line[0] == '#' ) {
229             // comment, skip
230         } else if ( line[0] == '\t' ) {
231             // runway entry
232             runway_list.push_back(line);
233         } else {
234             // start of airport record
235             airport = line;
236
237             if ( last_airport.length() ) {
238                 // process previous record
239                 process_airport(last_airport, runway_list, argv[2]);
240             }
241
242             // clear runway list for start of next airport
243             runway_list.erase(runway_list.begin(), runway_list.end());
244
245             last_airport = airport;
246         }
247     }
248
249     if ( last_airport.length() ) {
250         // process previous record
251         process_airport(last_airport, runway_list, argv[2]);
252     }
253
254     return 0;
255 }
256
257
258 // $Log$
259 // Revision 1.6  1999/02/11 01:10:51  curt
260 // Start of scenery revamp project.
261 //
262 // Revision 1.5  1998/09/17 18:40:43  curt
263 // Debug message tweaks.
264 //
265 // Revision 1.4  1998/09/09 20:59:56  curt
266 // Loop construct tweaks for STL usage.
267 // Output airport file to be used to generate airport scenery on the fly
268 //   by the run time sim.
269 //
270 //