]> git.mxchange.org Git - flightgear.git/blob - GenAirports/main.cxx
Continued progress in implementing the convex hull algorithm.
[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 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33
34 #include <list>
35 #include <stdio.h>
36 #include <string.h>
37 #include <string>
38
39 #include <Bucket/bucketutils.h>
40 #include <Include/fg_constants.h>
41 #include <Include/fg_zlib.h>
42
43 #include "area.hxx"
44 #include "convex_hull.hxx"
45
46
47 // process and airport + runway list
48 void process_airport( string last_airport, list < string > & runway_list,
49                       const string& root ) {
50     list_container rwy_list, apt_list, hull_list;
51     list_iterator current, last;
52
53     string line_str;
54     double lon, lat;
55     int len, width, hdg, label_hdg, elev;
56     char codes[10];
57     char side;
58     point2d average;
59     double sum_x, sum_y;
60
61     FILE *fd;
62     fgBUCKET b;
63     long int index;
64     char base[256], tmp[256];
65     string path, command, exfile, file;
66     int i, count;
67
68     printf( "(apt) %s", last_airport.c_str() );
69
70     while ( runway_list.size() ) {
71         line_str = runway_list.front();
72         runway_list.pop_front();
73         printf( "%s", line_str.c_str() );
74
75         sscanf( line_str.c_str(), "%lf %lf %d %d %d %s %d %c %d\n",
76                 &lon, &lat, &len, &width, &hdg, codes, &label_hdg, 
77                 &side, &elev );
78
79         rwy_list = gen_runway_area( lon, lat, (double)hdg * DEG_TO_RAD, 
80                                     (double)len * FEET_TO_METER,
81                                     (double)width * FEET_TO_METER );
82
83         // add rwy_list to apt_list
84         current = rwy_list.begin();
85         last = rwy_list.end();
86         while ( current != last ) {
87             apt_list.push_back(*current);
88             ++current;
89         }
90     }
91
92     printf("Runway points in degrees\n");
93     current = apt_list.begin();
94     last = apt_list.end();
95     while ( current != last ) {
96         // printf( "(%.4f, %.4f)\n", 
97         printf( "%.5f %.5f\n", current->lon, current->lat );
98         ++current;
99     }
100     printf("\n");
101
102     // generate convex hull
103     hull_list = convex_hull(apt_list);
104
105     // find average center point of convex hull
106     count = hull_list.size();
107
108     current = hull_list.begin();
109     last = hull_list.end();
110     sum_x = sum_y = 0.0;
111     while ( current != last ) {
112         sum_x += (*current).x;
113         sum_y += (*current).y;
114
115         ++current;
116     }
117
118     average.x = sum_x / count;
119     average.y = sum_y / count;
120
121     // find bucket based on first point in hull list.  Eventually
122     // we'll need to handle cases where the area crosses bucket
123     // boundaries.
124     fgBucketFind( (*current).lon, (*current).lat, &b);
125     printf( "Bucket = lon,lat = %d,%d  x,y index = %d,%d\n", 
126             b.lon, b.lat, b.x, b.y);
127
128     index = fgBucketGenIndex(&b);
129     fgBucketGenBasePath(&b, base);
130     path = root + "/Scenery/" + base;
131     command = "mkdir -p " + path;
132     system( command.c_str() );
133
134     sprintf(tmp, "%ld", index);
135     exfile = path + "/" + tmp + ".node.ex";
136     file =   path + "/" + tmp + ".poly";
137     printf( "extra node file = %s\n", exfile.c_str() );
138     printf( "poly file = %s\n", file.c_str() );
139
140     // output exclude nodes
141     printf("Output exclude nodes\n");
142     if ( (fd = fopen(exfile.c_str(), "w")) == NULL ) {
143         printf("Cannot open file: %s\n", exfile.c_str());
144         exit(-1);
145     }
146
147     fprintf( fd, "%d 2 0 0\n", count );
148
149     current = hull_list.begin();
150     last = hull_list.end();
151     i = 1;
152     while ( current != last ) {
153         // printf( "(%.4f, %.4f)\n", 
154         fprintf( fd, "%d %.2f %.2f %.2f\n", i, 
155                  (*current).lon * 3600.0, (*current).lat * 3600.0, elev);
156         ++current;
157         ++i;
158     }
159     fclose(fd);
160
161     // output poly
162     if ( (fd = fopen(file.c_str(), "w")) == NULL ) {
163         printf("Cannot open file: %s\n", file.c_str());
164         exit(-1);
165     }
166
167     // output empty node list
168     fprintf(fd, "0 2 0 0\n");
169
170     // output segments
171     fprintf( fd, "%d 0\n", count );
172     for ( i = 1; i < count; i++ ) {
173         fprintf( fd, "%d %d %d\n", i, i, i + 1 );
174     }
175     fprintf( fd, "%d %d %d\n", count, count, 1 );
176
177     // output hole center
178     fprintf( fd, "1\n");
179     fprintf( fd, "1 %.2f %.2f\n", average.x * 3600.0, average.y * 3600);
180
181     fclose(fd);
182 }
183
184
185 // reads the apt_full file and extracts and processes the individual
186 // airport records
187 int main( int argc, char **argv ) {
188     list < string > runway_list;
189     string apt_path, gz_apt_path;
190     string airport, last_airport;
191     fgFile f;
192     char line[256];
193     /*
194     fgBUCKET b;
195     point2d nodes[4];
196     char base[256], path[256], command[256], file[256], exfile[256];
197     double lon, lat, elevation, heading;
198     double length, width;
199     long int index;
200     */
201
202     if ( argc != 3 ) {
203         printf("Usage %s <apt_file> <work dir>\n", argv[0]);
204         exit(0);
205     }
206
207     apt_path = argv[1];
208     gz_apt_path = apt_path + ".gz";
209
210     // first try "path.gz"
211     if ( (f = fgopen(gz_apt_path.c_str(), "rb")) == NULL ) {
212         // next try "path"
213         if ( (f = fgopen(apt_path.c_str(), "rb")) == NULL ) {
214             printf( "Cannot open file: %s\n", apt_path.c_str());
215         }
216     }
217
218     while ( fggets(f, line, 250) != NULL ) {
219         // printf("%s", line);
220         if ( strlen(line) == 0 ) {
221             // empty, skip
222         } else if ( line[0] == '#' ) {
223             // comment, skip
224         } else if ( line[0] == '\t' ) {
225             // runway entry
226             runway_list.push_back(line);
227         } else {
228             // start of airport record
229             airport = line;
230
231             if ( last_airport.length() ) {
232                 // process previous record
233                 process_airport(last_airport, runway_list, argv[2]);
234             }
235
236             last_airport = airport;
237         }
238     }
239
240     if ( last_airport.length() ) {
241         // process previous record
242         process_airport(last_airport, runway_list, argv[2]);
243     }
244
245     fgclose(f);
246
247     return(1);
248 }
249
250
251 #if 0
252     // P13 (Globe, AZ)
253     // lon = -110.6642442;
254     // lat = 33.3528903;
255     // heading = 102.0 * DEG_TO_RAD;
256     // length = 1769;
257     // width = 23;
258
259     // KANE
260     lon = -93.2113889;
261     lat = 45.145;
262     elevation = 912 * FEET_TO_METER;
263     heading = 270.0 * DEG_TO_RAD;
264     length = 1220;
265     width = 23;
266
267     gen_runway_area( lon * DEG_TO_RAD, lat * DEG_TO_RAD, 
268                      heading, length, width, nodes, &count );
269
270     fgBucketFind(lon, lat, &b);
271     printf( "Bucket = lon,lat = %d,%d  x,y index = %d,%d\n", 
272             b.lon, b.lat, b.x, b.y);
273
274     index = fgBucketGenIndex(&b);
275     fgBucketGenBasePath(&b, base);
276     sprintf(path, "%s/Scenery/%s", argv[1], base);
277     sprintf(command, "mkdir -p %s\n", path);
278     system(command);
279     
280     sprintf(exfile, "%s/%ld.node.ex", path, index);
281     sprintf(file, "%s/%ld.poly", path, index);
282     printf( "extra node file = %s\n", exfile);
283     printf( "poly file = %s\n", file);
284
285     // output extra nodes
286     if ( (fd = fopen(exfile, "w")) == NULL ) {
287         printf("Cannot open file: %s\n", exfile);
288         exit(-1);
289     }
290
291     fprintf(fd, "%d 2 0 0\n", count);
292     for ( i = 0; i < count; i++ ) {
293         fprintf( fd, "%d %.2f %.2f %.2f\n", i + 1, 
294                  nodes[i].lon * RAD_TO_ARCSEC, nodes[i].lat * RAD_TO_ARCSEC, 
295                  elevation);
296     }
297     fclose(fd);
298
299     // output poly
300     if ( (fd = fopen(file, "w")) == NULL ) {
301         printf("Cannot open file: %s\n", file);
302         exit(-1);
303     }
304
305     // output empty node list
306     fprintf(fd, "0 2 0 0\n");
307
308     // output segments
309     fprintf(fd, "%d 0\n", count);
310     for ( i = 0; i < count - 1; i++ ) {
311         fprintf( fd, "%d %d %d\n", i + 1, i + 1, i + 2 );
312     }
313     fprintf( fd, "%d %d %d\n", count, count, 1 );
314
315     // output hole center
316     fprintf( fd, "1\n");
317     fprintf( fd, "1 %.2f %.2f\n", lon * 3600.0, lat * 3600);
318
319     fclose(fd);
320
321 #endif
322
323
324 // $Log: main.c,v
325 //