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( "(%.4f, %.4f)\n",
97 printf( "%.5f %.5f\n", current->lon, current->lat );
101 // generate convex hull
102 hull_list = convex_hull(apt_list);
104 // find average center point of convex hull
105 count = hull_list.size();
107 current = hull_list.begin();
108 last = hull_list.end();
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;
116 average.x = sum_x / count;
117 average.y = sum_y / count;
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);
126 index = fgBucketGenIndex(&b);
127 fgBucketGenBasePath(&b, base);
128 path = root + "/Scenery/" + base;
129 command = "mkdir -p " + path;
130 system( command.c_str() );
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() );
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());
147 fprintf( fd, "%d 2 0 0\n", count );
149 current = hull_list.begin();
150 last = hull_list.end();
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 );
162 printf("Output poly\n");
163 if ( (fd = fopen(file.c_str(), "w")) == NULL ) {
164 printf("Cannot open file: %s\n", file.c_str());
168 // output empty node list
169 fprintf(fd, "0 2 0 0\n");
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 );
176 fprintf( fd, "%d %d %d\n", count, count, 1 );
178 // output hole center
180 fprintf( fd, "1 %.2f %.2f\n", average.x * 3600.0, average.y * 3600);
185 printf("Output airport\n");
186 if ( (fd = fopen(aptfile.c_str(), "w")) == NULL ) {
187 printf("Cannot open file: %s\n", aptfile.c_str());
191 // write main airport identifier
192 fprintf(fd, "a %s", last_airport.c_str() );
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 );
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() );
214 // reads the apt_full file and extracts and processes the individual
216 int main( int argc, char **argv ) {
217 list < string > runway_list;
218 string apt_path, gz_apt_path;
219 string airport, last_airport;
225 char base[256], path[256], command[256], file[256], exfile[256];
226 double lon, lat, elevation, heading;
227 double length, width;
232 printf("Usage %s <apt_file> <work dir>\n", argv[0]);
237 gz_apt_path = apt_path + ".gz";
239 // first try "path.gz"
240 if ( (f = fgopen(gz_apt_path.c_str(), "rb")) == NULL ) {
242 if ( (f = fgopen(apt_path.c_str(), "rb")) == NULL ) {
243 printf( "Cannot open file: %s\n", apt_path.c_str());
247 while ( fggets(f, line, 250) != NULL ) {
248 // printf("%s", line);
249 if ( strlen(line) == 0 ) {
251 } else if ( line[0] == '#' ) {
253 } else if ( line[0] == '\t' ) {
255 runway_list.push_back(line);
257 // start of airport record
260 if ( last_airport.length() ) {
261 // process previous record
262 process_airport(last_airport, runway_list, argv[2]);
265 // clear runway list for start of next airport
266 runway_list.erase(runway_list.begin(), runway_list.end());
268 last_airport = airport;
272 if ( last_airport.length() ) {
273 // process previous record
274 process_airport(last_airport, runway_list, argv[2]);
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.