]> git.mxchange.org Git - flightgear.git/blob - GenAirports/main.cxx
Loop construct tweaks for STL usage.
[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( "(%.4f, %.4f)\n", 
97         printf( "%.5f %.5f\n", current->lon, current->lat );
98     }
99     printf("\n");
100
101     // generate convex hull
102     hull_list = convex_hull(apt_list);
103
104     // find average center point of convex hull
105     count = hull_list.size();
106
107     current = hull_list.begin();
108     last = hull_list.end();
109     sum_x = sum_y = 0.0;
110     for ( ; current != last; ++current ) {
111         // printf("return = %.6f %.6f\n", (*current).x, (*current).y);
112         sum_x += (*current).x;
113         sum_y += (*current).y;
114     }
115
116     average.x = sum_x / count;
117     average.y = sum_y / count;
118
119     // find bucket based on average center point of hull list.
120     // Eventually we'll need to handle cases where the area crosses
121     // bucket boundaries.
122     fgBucketFind( average.lon, average.lat, &b);
123     printf( "Bucket = lon,lat = %d,%d  x,y index = %d,%d\n", 
124             b.lon, b.lat, b.x, b.y);
125
126     index = fgBucketGenIndex(&b);
127     fgBucketGenBasePath(&b, base);
128     path = root + "/Scenery/" + base;
129     command = "mkdir -p " + path;
130     system( command.c_str() );
131
132     sprintf(tmp, "%ld", index);
133     exfile =  path + "/" + tmp + ".node.ex";
134     file =    path + "/" + tmp + ".poly";
135     aptfile = path + "/" + tmp + ".apt";
136     printf( "extra node file = %s\n", exfile.c_str() );
137     printf( "poly file = %s\n", file.c_str() );
138     printf( "apt file = %s\n", aptfile.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     for ( ; current != last ; ++current ) {
153         // printf( "(%.4f, %.4f)\n", 
154         fprintf( fd, "%d %.2f %.2f %.2f\n", i, 
155                  (*current).lon * 3600.0, (*current).lat * 3600.0, 
156                  (double)elev * FEET_TO_METER );
157         ++i;
158     }
159     fclose(fd);
160
161     // output poly
162     printf("Output poly\n");
163     if ( (fd = fopen(file.c_str(), "w")) == NULL ) {
164         printf("Cannot open file: %s\n", file.c_str());
165         exit(-1);
166     }
167
168     // output empty node list
169     fprintf(fd, "0 2 0 0\n");
170
171     // output segments
172     fprintf( fd, "%d 0\n", count );
173     for ( i = 1; i < count; i++ ) {
174         fprintf( fd, "%d %d %d\n", i, i, i + 1 );
175     }
176     fprintf( fd, "%d %d %d\n", count, count, 1 );
177
178     // output hole center
179     fprintf( fd, "1\n");
180     fprintf( fd, "1 %.2f %.2f\n", average.x * 3600.0, average.y * 3600);
181
182     fclose(fd);
183
184     // output "apt" file
185     printf("Output airport\n");
186     if ( (fd = fopen(aptfile.c_str(), "w")) == NULL ) {
187         printf("Cannot open file: %s\n", aptfile.c_str());
188         exit(-1);
189     }
190
191     // write main airport identifier
192     fprintf(fd, "a %s", last_airport.c_str() );
193
194     // write perimeter polygon
195     current = hull_list.begin();
196     last = hull_list.end();
197     for ( ; current != last ; ++current ) {
198         fprintf( fd, "p %.7f %.7f %.2f\n", (*current).lon, (*current).lat, 
199                  (double)elev * FEET_TO_METER );
200     }
201
202     // write runway info
203     for ( list < string >::iterator current_runway = runway_list.begin();
204           current_runway != last_runway ; ++current_runway ) {
205         line_str = (*current_runway);
206         line_str = line_str.substr(1, line_str.size());
207         fprintf(fd, "r %s", line_str.c_str() );
208     }
209
210     fclose(fd);
211 }
212
213
214 // reads the apt_full file and extracts and processes the individual
215 // airport records
216 int main( int argc, char **argv ) {
217     list < string > runway_list;
218     string apt_path, gz_apt_path;
219     string airport, last_airport;
220     fgFile f;
221     char line[256];
222     /*
223     fgBUCKET b;
224     point2d nodes[4];
225     char base[256], path[256], command[256], file[256], exfile[256];
226     double lon, lat, elevation, heading;
227     double length, width;
228     long int index;
229     */
230
231     if ( argc != 3 ) {
232         printf("Usage %s <apt_file> <work dir>\n", argv[0]);
233         exit(0);
234     }
235
236     apt_path = argv[1];
237     gz_apt_path = apt_path + ".gz";
238
239     // first try "path.gz"
240     if ( (f = fgopen(gz_apt_path.c_str(), "rb")) == NULL ) {
241         // next try "path"
242         if ( (f = fgopen(apt_path.c_str(), "rb")) == NULL ) {
243             printf( "Cannot open file: %s\n", apt_path.c_str());
244         }
245     }
246
247     while ( fggets(f, line, 250) != NULL ) {
248         // printf("%s", line);
249         if ( strlen(line) == 0 ) {
250             // empty, skip
251         } else if ( line[0] == '#' ) {
252             // comment, skip
253         } else if ( line[0] == '\t' ) {
254             // runway entry
255             runway_list.push_back(line);
256         } else {
257             // start of airport record
258             airport = line;
259
260             if ( last_airport.length() ) {
261                 // process previous record
262                 process_airport(last_airport, runway_list, argv[2]);
263             }
264
265             // clear runway list for start of next airport
266             runway_list.erase(runway_list.begin(), runway_list.end());
267
268             last_airport = airport;
269         }
270     }
271
272     if ( last_airport.length() ) {
273         // process previous record
274         process_airport(last_airport, runway_list, argv[2]);
275     }
276
277     fgclose(f);
278
279     return(1);
280 }
281
282
283 // $Log$
284 // Revision 1.4  1998/09/09 20:59:56  curt
285 // Loop construct tweaks for STL usage.
286 // Output airport file to be used to generate airport scenery on the fly
287 //   by the run time sim.
288 //
289 //