1 // main.cxx -- main loop
3 // Written by Curtis Olson, started March 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
22 // (Log is kept at end of this file)
39 #include <Bucket/bucketutils.h>
40 #include <Include/fg_constants.h>
41 #include <Include/fg_zlib.h>
44 #include "convex_hull.hxx"
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;
55 int len, width, hdg, label_hdg, elev;
64 char base[256], tmp[256];
65 string path, command, exfile, file, aptfile;
68 printf( "(apt) %s", last_airport.c_str() );
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() );
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,
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 );
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);
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 );
100 // generate convex hull
101 hull_list = convex_hull(apt_list);
103 // find average center point of convex hull
104 count = hull_list.size();
106 current = hull_list.begin();
107 last = hull_list.end();
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;
115 average.x = sum_x / count;
116 average.y = sum_y / count;
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);
125 index = fgBucketGenIndex(&b);
126 fgBucketGenBasePath(&b, base);
127 path = root + "/Scenery/" + base;
128 command = "mkdir -p " + path;
129 system( command.c_str() );
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() );
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());
146 fprintf( fd, "%d 2 0 0\n", count );
148 current = hull_list.begin();
149 last = hull_list.end();
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 );
161 printf("Output poly\n");
162 if ( (fd = fopen(file.c_str(), "w")) == NULL ) {
163 printf("Cannot open file: %s\n", file.c_str());
167 // output empty node list
168 fprintf(fd, "0 2 0 0\n");
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 );
175 fprintf( fd, "%d %d %d\n", count, count, 1 );
177 // output hole center
179 fprintf( fd, "1 %.2f %.2f\n", average.x * 3600.0, average.y * 3600);
184 printf("Output airport\n");
185 if ( (fd = fopen(aptfile.c_str(), "w")) == NULL ) {
186 printf("Cannot open file: %s\n", aptfile.c_str());
190 // write main airport identifier
191 fprintf(fd, "a %s", last_airport.c_str() );
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 );
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() );
213 // reads the apt_full file and extracts and processes the individual
215 int main( int argc, char **argv ) {
216 list < string > runway_list;
217 string apt_path, gz_apt_path;
218 string airport, last_airport;
224 char base[256], path[256], command[256], file[256], exfile[256];
225 double lon, lat, elevation, heading;
226 double length, width;
231 printf("Usage %s <apt_file> <work dir>\n", argv[0]);
236 gz_apt_path = apt_path + ".gz";
238 // first try "path.gz"
239 if ( (f = fgopen(gz_apt_path.c_str(), "rb")) == NULL ) {
241 if ( (f = fgopen(apt_path.c_str(), "rb")) == NULL ) {
242 printf( "Cannot open file: %s\n", apt_path.c_str());
246 while ( fggets(f, line, 250) != NULL ) {
247 // printf("%s", line);
248 if ( strlen(line) == 0 ) {
250 } else if ( line[0] == '#' ) {
252 } else if ( line[0] == '\t' ) {
254 runway_list.push_back(line);
256 // start of airport record
259 if ( last_airport.length() ) {
260 // process previous record
261 process_airport(last_airport, runway_list, argv[2]);
264 // clear runway list for start of next airport
265 runway_list.erase(runway_list.begin(), runway_list.end());
267 last_airport = airport;
271 if ( last_airport.length() ) {
272 // process previous record
273 process_airport(last_airport, runway_list, argv[2]);
283 // Revision 1.5 1998/09/17 18:40:43 curt
284 // Debug message tweaks.
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.