]> git.mxchange.org Git - flightgear.git/blob - GenAirports/main.cxx
475ae10ea606ffb6ff1b780e34115bba184795b6
[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, aptfile;
66     int i, count;
67
68     printf( "(apt) %s", last_airport.c_str() );
69
70     list < string >::iterator last_runway = runway_list.end();
71     for ( list < string >::iterator current_runway = runway_list.begin();
72           current_runway != last_runway ; ++current_runway ) {
73         line_str = (*current_runway);
74         printf( "%s", line_str.c_str() );
75
76         sscanf( line_str.c_str(), "%lf %lf %d %d %d %s %d %c %d\n",
77                 &lon, &lat, &len, &width, &hdg, codes, &label_hdg, 
78                 &side, &elev );
79
80         rwy_list = gen_runway_area( lon, lat, (double)hdg * DEG_TO_RAD, 
81                                     (double)len * FEET_TO_METER,
82                                     (double)width * FEET_TO_METER );
83
84         // add rwy_list to apt_list
85         current = rwy_list.begin();
86         last = rwy_list.end();
87         for ( ; current != last ; ++current ) {
88             apt_list.push_back(*current);
89         }
90     }
91
92     // printf("Runway points in degrees\n");
93     current = apt_list.begin();
94     last = apt_list.end();
95     for ( ; current != last; ++current ) {
96         // printf( "%.5f %.5f\n", current->lon, current->lat );
97     }
98     // printf("\n");
99
100     // generate convex hull
101     hull_list = convex_hull(apt_list);
102
103     // find average center point of convex hull
104     count = hull_list.size();
105
106     current = hull_list.begin();
107     last = hull_list.end();
108     sum_x = sum_y = 0.0;
109     for ( ; current != last; ++current ) {
110         // printf("return = %.6f %.6f\n", (*current).x, (*current).y);
111         sum_x += (*current).x;
112         sum_y += (*current).y;
113     }
114
115     average.x = sum_x / count;
116     average.y = sum_y / count;
117
118     // find bucket based on average center point of hull list.
119     // Eventually we'll need to handle cases where the area crosses
120     // bucket boundaries.
121     fgBucketFind( average.lon, average.lat, &b);
122     printf( "Bucket = lon,lat = %d,%d  x,y index = %d,%d\n", 
123             b.lon, b.lat, b.x, b.y);
124
125     index = fgBucketGenIndex(&b);
126     fgBucketGenBasePath(&b, base);
127     path = root + "/Scenery/" + base;
128     command = "mkdir -p " + path;
129     system( command.c_str() );
130
131     sprintf(tmp, "%ld", index);
132     exfile =  path + "/" + tmp + ".node.ex";
133     file =    path + "/" + tmp + ".poly";
134     aptfile = path + "/" + tmp + ".apt";
135     printf( "extra node file = %s\n", exfile.c_str() );
136     printf( "poly file = %s\n", file.c_str() );
137     printf( "apt file = %s\n", aptfile.c_str() );
138
139     // output exclude nodes
140     printf("Output exclude nodes\n");
141     if ( (fd = fopen(exfile.c_str(), "w")) == NULL ) {
142         printf("Cannot open file: %s\n", exfile.c_str());
143         exit(-1);
144     }
145
146     fprintf( fd, "%d 2 0 0\n", count );
147
148     current = hull_list.begin();
149     last = hull_list.end();
150     i = 1;
151     for ( ; current != last ; ++current ) {
152         // printf( "(%.4f, %.4f)\n", 
153         fprintf( fd, "%d %.2f %.2f %.2f\n", i, 
154                  (*current).lon * 3600.0, (*current).lat * 3600.0, 
155                  (double)elev * FEET_TO_METER );
156         ++i;
157     }
158     fclose(fd);
159
160     // output poly
161     printf("Output poly\n");
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     // output "apt" file
184     printf("Output airport\n");
185     if ( (fd = fopen(aptfile.c_str(), "w")) == NULL ) {
186         printf("Cannot open file: %s\n", aptfile.c_str());
187         exit(-1);
188     }
189
190     // write main airport identifier
191     fprintf(fd, "a %s", last_airport.c_str() );
192
193     // write perimeter polygon
194     current = hull_list.begin();
195     last = hull_list.end();
196     for ( ; current != last ; ++current ) {
197         fprintf( fd, "p %.7f %.7f %.2f\n", (*current).lon, (*current).lat, 
198                  (double)elev * FEET_TO_METER );
199     }
200
201     // write runway info
202     for ( list < string >::iterator current_runway = runway_list.begin();
203           current_runway != last_runway ; ++current_runway ) {
204         line_str = (*current_runway);
205         line_str = line_str.substr(1, line_str.size());
206         fprintf(fd, "r %s", line_str.c_str() );
207     }
208
209     fclose(fd);
210 }
211
212
213 // reads the apt_full file and extracts and processes the individual
214 // airport records
215 int main( int argc, char **argv ) {
216     list < string > runway_list;
217     string apt_path, gz_apt_path;
218     string airport, last_airport;
219     fgFile f;
220     char line[256];
221     /*
222     fgBUCKET b;
223     point2d nodes[4];
224     char base[256], path[256], command[256], file[256], exfile[256];
225     double lon, lat, elevation, heading;
226     double length, width;
227     long int index;
228     */
229
230     if ( argc != 3 ) {
231         printf("Usage %s <apt_file> <work dir>\n", argv[0]);
232         exit(0);
233     }
234
235     apt_path = argv[1];
236     gz_apt_path = apt_path + ".gz";
237
238     // first try "path.gz"
239     if ( (f = fgopen(gz_apt_path.c_str(), "rb")) == NULL ) {
240         // next try "path"
241         if ( (f = fgopen(apt_path.c_str(), "rb")) == NULL ) {
242             printf( "Cannot open file: %s\n", apt_path.c_str());
243         }
244     }
245
246     while ( fggets(f, line, 250) != NULL ) {
247         // printf("%s", line);
248         if ( strlen(line) == 0 ) {
249             // empty, skip
250         } else if ( line[0] == '#' ) {
251             // comment, skip
252         } else if ( line[0] == '\t' ) {
253             // runway entry
254             runway_list.push_back(line);
255         } else {
256             // start of airport record
257             airport = line;
258
259             if ( last_airport.length() ) {
260                 // process previous record
261                 process_airport(last_airport, runway_list, argv[2]);
262             }
263
264             // clear runway list for start of next airport
265             runway_list.erase(runway_list.begin(), runway_list.end());
266
267             last_airport = airport;
268         }
269     }
270
271     if ( last_airport.length() ) {
272         // process previous record
273         process_airport(last_airport, runway_list, argv[2]);
274     }
275
276     fgclose(f);
277
278     return(1);
279 }
280
281
282 // $Log$
283 // Revision 1.5  1998/09/17 18:40:43  curt
284 // Debug message tweaks.
285 //
286 // Revision 1.4  1998/09/09 20:59:56  curt
287 // Loop construct tweaks for STL usage.
288 // Output airport file to be used to generate airport scenery on the fly
289 //   by the run time sim.
290 //
291 //