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;
68 printf( "(apt) %s", last_airport.c_str() );
70 while ( runway_list.size() ) {
71 line_str = runway_list.front();
72 runway_list.pop_front();
73 printf( "%s", line_str.c_str() );
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,
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 );
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);
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 );
102 // generate convex hull
103 hull_list = convex_hull(apt_list);
105 // find average center point of convex hull
106 count = hull_list.size();
108 current = hull_list.begin();
109 last = hull_list.end();
111 while ( current != last ) {
112 sum_x += (*current).x;
113 sum_y += (*current).y;
118 average.x = sum_x / count;
119 average.y = sum_y / count;
121 // find bucket based on first point in hull list. Eventually
122 // we'll need to handle cases where the area crosses bucket
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);
128 index = fgBucketGenIndex(&b);
129 fgBucketGenBasePath(&b, base);
130 path = root + "/Scenery/" + base;
131 command = "mkdir -p " + path;
132 system( command.c_str() );
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() );
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 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);
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);
185 // reads the apt_full file and extracts and processes the individual
187 int main( int argc, char **argv ) {
188 list < string > runway_list;
189 string apt_path, gz_apt_path;
190 string airport, last_airport;
196 char base[256], path[256], command[256], file[256], exfile[256];
197 double lon, lat, elevation, heading;
198 double length, width;
203 printf("Usage %s <apt_file> <work dir>\n", argv[0]);
208 gz_apt_path = apt_path + ".gz";
210 // first try "path.gz"
211 if ( (f = fgopen(gz_apt_path.c_str(), "rb")) == NULL ) {
213 if ( (f = fgopen(apt_path.c_str(), "rb")) == NULL ) {
214 printf( "Cannot open file: %s\n", apt_path.c_str());
218 while ( fggets(f, line, 250) != NULL ) {
219 // printf("%s", line);
220 if ( strlen(line) == 0 ) {
222 } else if ( line[0] == '#' ) {
224 } else if ( line[0] == '\t' ) {
226 runway_list.push_back(line);
228 // start of airport record
231 if ( last_airport.length() ) {
232 // process previous record
233 process_airport(last_airport, runway_list, argv[2]);
236 last_airport = airport;
240 if ( last_airport.length() ) {
241 // process previous record
242 process_airport(last_airport, runway_list, argv[2]);
253 // lon = -110.6642442;
255 // heading = 102.0 * DEG_TO_RAD;
262 elevation = 912 * FEET_TO_METER;
263 heading = 270.0 * DEG_TO_RAD;
267 gen_runway_area( lon * DEG_TO_RAD, lat * DEG_TO_RAD,
268 heading, length, width, nodes, &count );
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);
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);
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);
285 // output extra nodes
286 if ( (fd = fopen(exfile, "w")) == NULL ) {
287 printf("Cannot open file: %s\n", exfile);
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,
300 if ( (fd = fopen(file, "w")) == NULL ) {
301 printf("Cannot open file: %s\n", file);
305 // output empty node list
306 fprintf(fd, "0 2 0 0\n");
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 );
313 fprintf( fd, "%d %d %d\n", count, count, 1 );
315 // output hole center
317 fprintf( fd, "1 %.2f %.2f\n", lon * 3600.0, lat * 3600);