#include <config.h>
#endif
+#include <Include/compiler.h>
+
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <list>
#include <stdio.h>
#include <string.h>
-#include <string>
+#include STL_STRING
-#include <Bucket/bucketutils.h>
+#include <Bucket/newbucket.hxx>
+#include <Debug/logstream.hxx>
#include <Include/fg_constants.h>
-#include <Include/fg_zlib.h>
+#include <Misc/fgstream.hxx>
+#include <Polygon/index.hxx>
#include "area.hxx"
+#include "convex_hull.hxx"
-// process and airport + runway list
-void process_airport( string last_airport, list < string > & runway_list ) {
- list < point2d > rwy_list, apt_list;
- list < point2d > :: iterator current;
- list < point2d > :: iterator last;
+// write out airport data
+void write_airport( int p_index, list_container hull_list, FGBucket b,
+ const string& root, const bool cut_and_keep ) {
+ char tile_name[256], poly_index[256];
+
+ long int b_index = b.gen_index();
+ string base = b.gen_base_path();
+ string path = root + "/Scenery/" + base;
+ string command = "mkdir -p " + path;
+ system( command.c_str() );
+
+ sprintf(tile_name, "%ld", b_index);
+ string aptfile = path + "/" + tile_name;
+
+ sprintf( poly_index, "%d", p_index );
+ aptfile += ".";
+ aptfile += poly_index;
+ cout << "apt file = " << aptfile << endl;
+
+ FILE *fd;
+ if ( (fd = fopen(aptfile.c_str(), "a")) == NULL ) {
+ cout << "Cannot open file: " << aptfile << endl;
+ exit(-1);
+ }
- string line_str;
- double lon, lat;
- int len, width, hdg, label_hdg, elev;
- char codes[10];
- char side;
+ if ( cut_and_keep ) {
+ fprintf( fd, "AirportKeep\n" );
+ } else {
+ fprintf( fd, "AirportIgnore\n" );
+ }
+ fprintf( fd, "%d\n", hull_list.size() );
+ // write perimeter polygon
+ list_iterator current = hull_list.begin();
+ list_iterator last = hull_list.end();
+ for ( ; current != last ; ++current ) {
+ fprintf( fd, "%.7f %.7f\n", (*current).lon, (*current).lat );
+ }
+
+ fclose(fd);
+}
- printf( "(apt) %s", last_airport.c_str() );
- while ( runway_list.size() ) {
- line_str = runway_list.front();
- runway_list.pop_front();
- printf( "%s", line_str.c_str() );
+// process and airport + runway list
+void process_airport( string airport, list < string > & runway_list,
+ const string& root ) {
+ list_container rwy_list, apt_list, hull_list;
+ list_iterator current, last;
+
+ // parse main airport information
+ int elev;
+
+ cout << airport << endl;
+ string apt_type = airport.substr(0, 1);
+ string apt_code = airport.substr(2, 4);
+ string apt_lat = airport.substr(7, 10);
+ string apt_lon = airport.substr(18, 11);
+ string apt_elev = airport.substr(30, 5);
+ sscanf( apt_elev.c_str(), "%d", &elev );
+ string apt_use = airport.substr(36, 1);
+ string apt_twr = airport.substr(37, 1);
+ string apt_bldg = airport.substr(38, 1);
+ string apt_name = airport.substr(40);
- sscanf( line_str.c_str(), "%lf %lf %d %d %d %s %d %c %d\n",
- &lon, &lat, &len, &width, &hdg, codes, &label_hdg,
- &side, &elev );
+ /*
+ cout << " type = " << apt_type << endl;
+ cout << " code = " << apt_code << endl;
+ cout << " lat = " << apt_lat << endl;
+ cout << " lon = " << apt_lon << endl;
+ cout << " elev = " << apt_elev << " " << elev << endl;
+ cout << " use = " << apt_use << endl;
+ cout << " twr = " << apt_twr << endl;
+ cout << " bldg = " << apt_bldg << endl;
+ cout << " name = " << apt_name << endl;
+ */
- rwy_list = gen_runway_area( lon, lat, (double)hdg * DEG_TO_RAD,
+ // parse runways and generate the vertex list
+ string rwy_str;
+ double lon, lat, hdg;
+ int len, width;
+
+ list < string >::iterator last_runway = runway_list.end();
+ for ( list < string >::iterator current_runway = runway_list.begin();
+ current_runway != last_runway ; ++current_runway ) {
+ rwy_str = (*current_runway);
+
+ cout << rwy_str << endl;
+ string rwy_no = rwy_str.substr(2, 4);
+ string rwy_lat = rwy_str.substr(6, 10);
+ sscanf( rwy_lat.c_str(), "%lf", &lat);
+ string rwy_lon = rwy_str.substr(17, 11);
+ sscanf( rwy_lon.c_str(), "%lf", &lon);
+ string rwy_hdg = rwy_str.substr(29, 7);
+ sscanf( rwy_hdg.c_str(), "%lf", &hdg);
+ string rwy_len = rwy_str.substr(36, 7);
+ sscanf( rwy_len.c_str(), "%d", &len);
+ string rwy_width = rwy_str.substr(43, 4);
+ sscanf( rwy_width.c_str(), "%d", &width);
+ string rwy_sfc = rwy_str.substr(47, 4);
+ string rwy_end1 = rwy_str.substr(52, 6);
+ string rwy_end2 = rwy_str.substr(59, 6);
+
+ /*
+ cout << " no = " << rwy_no << endl;
+ cout << " lat = " << rwy_lat << " " << lat << endl;
+ cout << " lon = " << rwy_lon << " " << lon << endl;
+ cout << " hdg = " << rwy_hdg << " " << hdg << endl;
+ cout << " len = " << rwy_len << " " << len << endl;
+ cout << " width = " << rwy_width << " " << width << endl;
+ cout << " sfc = " << rwy_sfc << endl;
+ cout << " end1 = " << rwy_end1 << endl;
+ cout << " end2 = " << rwy_end2 << endl;
+ */
+
+ rwy_list = gen_runway_area( lon, lat, hdg * DEG_TO_RAD,
(double)len * FEET_TO_METER,
(double)width * FEET_TO_METER );
// add rwy_list to apt_list
current = rwy_list.begin();
last = rwy_list.end();
- while ( current != last ) {
+ for ( ; current != last ; ++current ) {
apt_list.push_back(*current);
- current++;
}
}
- printf("Final results in degrees\n");
- current = apt_list.begin();
- last = apt_list.end();
- while ( current != last ) {
- // printf( "(%.4f, %.4f)\n",
- printf( "%.5f %.5f\n",
- current->lon * RAD_TO_DEG,
- current->lat * RAD_TO_DEG );
- current++;
+ if ( apt_list.size() == 0 ) {
+ cout << "no runway points generated" << endl;
+ return;
+ }
+
+ // printf("Runway points in degrees\n");
+ // current = apt_list.begin();
+ // last = apt_list.end();
+ // for ( ; current != last; ++current ) {
+ // printf( "%.5f %.5f\n", current->lon, current->lat );
+ // }
+ // printf("\n");
+
+ // generate convex hull
+ hull_list = convex_hull(apt_list);
+
+ // get next polygon index
+ int index = poly_index_next();
+
+ // find average center, min, and max point of convex hull
+ point2d average, min, max;
+ double sum_x, sum_y;
+ int count = hull_list.size();
+ current = hull_list.begin();
+ last = hull_list.end();
+ sum_x = sum_y = 0.0;
+ min.x = min.y = 200.0;
+ max.x = max.y = -200.0;
+ for ( ; current != last; ++current ) {
+ // printf("return = %.6f %.6f\n", (*current).x, (*current).y);
+ sum_x += (*current).x;
+ sum_y += (*current).y;
+
+ if ( (*current).x < min.x ) { min.x = (*current).x; }
+ if ( (*current).y < min.y ) { min.y = (*current).y; }
+ if ( (*current).x > max.x ) { max.x = (*current).x; }
+ if ( (*current).y > max.y ) { max.y = (*current).y; }
+ }
+ average.x = sum_x / count;
+ average.y = sum_y / count;
+
+ // find buckets for center, min, and max points of convex hull.
+ // note to self: self, you should think about checking for runways
+ // that span the data line
+ FGBucket b(average.lon, average.lat);
+ FGBucket b_min(min.x, min.y);
+ FGBucket b_max(max.x, max.y);
+ cout << "Bucket center = " << b << endl;
+ cout << "Bucket min = " << b_min << endl;
+ cout << "Bucket max = " << b_max << endl;
+
+ if ( b_min == b_max ) {
+ write_airport( index, hull_list, b, root, true );
+ } else {
+ FGBucket b_cur;
+ int dx, dy, i, j;
+
+ fgBucketDiff(b_min, b_max, &dx, &dy);
+ cout << "airport spans tile boundaries" << endl;
+ cout << " dx = " << dx << " dy = " << dy << endl;
+
+ if ( (dx > 1) || (dy > 1) ) {
+ cout << "somethings really wrong!!!!" << endl;
+ exit(-1);
+ }
+
+ for ( j = 0; j <= dy; j++ ) {
+ for ( i = 0; i <= dx; i++ ) {
+ b_cur = fgBucketOffset(min.x, min.y, i, j);
+ if ( b_cur == b ) {
+ write_airport( index, hull_list, b_cur, root, true );
+ } else {
+ write_airport( index, hull_list, b_cur, root, false );
+ }
+ }
+ }
+ // string answer; cin >> answer;
}
- printf("\n");
}
// airport records
int main( int argc, char **argv ) {
list < string > runway_list;
- string apt_path, gz_apt_path;
string airport, last_airport;
- fgFile f;
- char line[256];
- /*
- fgBUCKET b;
- point2d nodes[4];
- char base[256], path[256], command[256], file[256], exfile[256];
- double lon, lat, elevation, heading;
- double length, width;
- long int index;
- */
+ string line;
+ char tmp[256];
+
+ fglog().setLogLevels( FG_ALL, FG_DEBUG );
if ( argc != 3 ) {
- printf("Usage %s <apt_file> <work dir>\n", argv[0]);
- exit(0);
+ FG_LOG( FG_GENERAL, FG_ALERT,
+ "Usage " << argv[0] << " <apt_file> <work_dir>" );
+ exit(-1);
}
- apt_path = argv[1];
- gz_apt_path = apt_path + ".gz";
+ // make work directory
+ string work_dir = argv[2];
+ string command = "mkdir -p " + work_dir;
+ system( command.c_str() );
- // first try "path.gz"
- if ( (f = fgopen(gz_apt_path.c_str(), "rb")) == NULL ) {
- // next try "path"
- if ( (f = fgopen(apt_path.c_str(), "rb")) == NULL ) {
- printf( "Cannot open file: %s\n", apt_path.c_str());
- }
+ // initialize persistant polygon counter
+ string counter_file = work_dir + "/polygon.counter";
+ poly_index_init( counter_file );
+
+ fg_gzifstream in( argv[1] );
+ if ( !in ) {
+ FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << argv[1] );
+ exit(-1);
}
- while ( fggets(f, line, 250) != NULL ) {
- // printf("%s", line);
- if ( strlen(line) == 0 ) {
+ // throw away the first 3 lines
+ in.getline(tmp, 256);
+ in.getline(tmp, 256);
+ in.getline(tmp, 256);
+
+ last_airport = "";
+
+ while ( ! in.eof() ) {
+ in.getline(tmp, 256);
+ line = tmp;
+ // cout << line << endl;
+
+ if ( line.length() == 0 ) {
// empty, skip
} else if ( line[0] == '#' ) {
// comment, skip
- } else if ( line[0] == '\t' ) {
- // runway entry
- runway_list.push_back(line);
- } else {
+ } else if ( (line[0] == 'A') || (line[0] == 'S') ) {
// start of airport record
airport = line;
if ( last_airport.length() ) {
// process previous record
- process_airport(last_airport, runway_list);
+ process_airport(last_airport, runway_list, argv[2]);
}
+ // clear runway list for start of next airport
+ runway_list.erase(runway_list.begin(), runway_list.end());
+
last_airport = airport;
+ } else if ( line[0] == 'R' ) {
+ // runway entry
+ runway_list.push_back(line);
+ } else if ( line == "99" ) {
+ // end of file
+ break;
+ } else {
+ FG_LOG( FG_GENERAL, FG_ALERT,
+ "Unknown line in file" << endl << line );
+ exit(-1);
}
}
if ( last_airport.length() ) {
// process previous record
- process_airport(last_airport, runway_list);
+ process_airport(last_airport, runway_list, argv[2]);
}
- fgclose(f);
-
- return(1);
+ return 0;
}
-#if 0
- // P13 (Globe, AZ)
- // lon = -110.6642442;
- // lat = 33.3528903;
- // heading = 102.0 * DEG_TO_RAD;
- // length = 1769;
- // width = 23;
-
- // KANE
- lon = -93.2113889;
- lat = 45.145;
- elevation = 912 * FEET_TO_METER;
- heading = 270.0 * DEG_TO_RAD;
- length = 1220;
- width = 23;
-
- gen_runway_area( lon * DEG_TO_RAD, lat * DEG_TO_RAD,
- heading, length, width, nodes, &count );
-
- fgBucketFind(lon, lat, &b);
- printf( "Bucket = lon,lat = %d,%d x,y index = %d,%d\n",
- b.lon, b.lat, b.x, b.y);
-
- index = fgBucketGenIndex(&b);
- fgBucketGenBasePath(&b, base);
- sprintf(path, "%s/Scenery/%s", argv[1], base);
- sprintf(command, "mkdir -p %s\n", path);
- system(command);
-
- sprintf(exfile, "%s/%ld.node.ex", path, index);
- sprintf(file, "%s/%ld.poly", path, index);
- printf( "extra node file = %s\n", exfile);
- printf( "poly file = %s\n", file);
-
- // output extra nodes
- if ( (fd = fopen(exfile, "w")) == NULL ) {
- printf("Cannot open file: %s\n", exfile);
- exit(-1);
- }
-
- fprintf(fd, "%d 2 0 0\n", count);
- for ( i = 0; i < count; i++ ) {
- fprintf( fd, "%d %.2f %.2f %.2f\n", i + 1,
- nodes[i].lon * RAD_TO_ARCSEC, nodes[i].lat * RAD_TO_ARCSEC,
- elevation);
- }
- fclose(fd);
-
- // output poly
- if ( (fd = fopen(file, "w")) == NULL ) {
- printf("Cannot open file: %s\n", file);
- exit(-1);
- }
-
- // output empty node list
- fprintf(fd, "0 2 0 0\n");
-
- // output segments
- fprintf(fd, "%d 0\n", count);
- for ( i = 0; i < count - 1; i++ ) {
- fprintf( fd, "%d %d %d\n", i + 1, i + 1, i + 2 );
- }
- fprintf( fd, "%d %d %d\n", count, count, 1 );
-
- // output hole center
- fprintf( fd, "1\n");
- fprintf( fd, "1 %.2f %.2f\n", lon * 3600.0, lat * 3600);
-
- fclose(fd);
-
-#endif
-
-
-// $Log: main.c,v
+// $Log$
+// Revision 1.7 1999/02/25 21:32:49 curt
+// Modified to adhere to new polygon naming convention, and also to read the
+// new Robin Peel aiport format.
+//
+// Revision 1.6 1999/02/11 01:10:51 curt
+// Start of scenery revamp project.
+//
+// Revision 1.5 1998/09/17 18:40:43 curt
+// Debug message tweaks.
+//
+// Revision 1.4 1998/09/09 20:59:56 curt
+// Loop construct tweaks for STL usage.
+// Output airport file to be used to generate airport scenery on the fly
+// by the run time sim.
+//
//