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)
30 #include <Include/compiler.h>
41 #include <Bucket/newbucket.hxx>
42 #include <Debug/logstream.hxx>
43 #include <Include/fg_constants.h>
44 #include <Misc/fgstream.hxx>
47 #include "convex_hull.hxx"
50 // write out airport data
51 void write_airport( list_container hull_list, FGBucket b, const string& root,
52 const bool cut_and_keep ) {
55 long int index = b.gen_index();
56 string base = b.gen_base_path();
57 string path = root + "/Scenery/" + base;
58 string command = "mkdir -p " + path;
59 system( command.c_str() );
61 sprintf(tmp, "%ld", index);
62 string aptfile = path + "/" + tmp + ".apt";
63 cout << "apt file = " << aptfile << endl;
66 if ( (fd = fopen(aptfile.c_str(), "a")) == NULL ) {
67 cout << "Cannot open file: " << aptfile << endl;
72 fprintf( fd, "cut_and_keep\n" );
74 fprintf( fd, "cut_and_ignore\n" );
76 fprintf( fd, "%d\n", hull_list.size() );
77 // write perimeter polygon
78 list_iterator current = hull_list.begin();
79 list_iterator last = hull_list.end();
80 for ( ; current != last ; ++current ) {
81 fprintf( fd, "%.7f %.7f\n", (*current).lon, (*current).lat );
88 // process and airport + runway list
89 void process_airport( string last_airport, list < string > & runway_list,
90 const string& root ) {
91 list_container rwy_list, apt_list, hull_list;
92 list_iterator current, last;
99 // generate the vertices of all the runways
100 int len, width, hdg, label_hdg, elev;
103 cout << "(apt) " << last_airport;
104 list < string >::iterator last_runway = runway_list.end();
105 for ( list < string >::iterator current_runway = runway_list.begin();
106 current_runway != last_runway ; ++current_runway ) {
107 line_str = (*current_runway);
110 sscanf( line_str.c_str(), "%lf %lf %d %d %d %s %d %c %d\n",
111 &lon, &lat, &len, &width, &hdg, codes, &label_hdg,
114 rwy_list = gen_runway_area( lon, lat, (double)hdg * DEG_TO_RAD,
115 (double)len * FEET_TO_METER,
116 (double)width * FEET_TO_METER );
118 // add rwy_list to apt_list
119 current = rwy_list.begin();
120 last = rwy_list.end();
121 for ( ; current != last ; ++current ) {
122 apt_list.push_back(*current);
126 // printf("Runway points in degrees\n");
127 // current = apt_list.begin();
128 // last = apt_list.end();
129 // for ( ; current != last; ++current ) {
130 // printf( "%.5f %.5f\n", current->lon, current->lat );
134 // generate convex hull
135 hull_list = convex_hull(apt_list);
137 // find average center, min, and max point of convex hull
138 point2d average, min, max;
140 count = hull_list.size();
141 current = hull_list.begin();
142 last = hull_list.end();
144 min.x = min.y = 200.0;
145 max.x = max.y = -200.0;
146 for ( ; current != last; ++current ) {
147 // printf("return = %.6f %.6f\n", (*current).x, (*current).y);
148 sum_x += (*current).x;
149 sum_y += (*current).y;
151 if ( (*current).x < min.x ) { min.x = (*current).x; }
152 if ( (*current).y < min.y ) { min.y = (*current).y; }
153 if ( (*current).x > max.x ) { max.x = (*current).x; }
154 if ( (*current).y > max.y ) { max.y = (*current).y; }
156 average.x = sum_x / count;
157 average.y = sum_y / count;
159 // find buckets for center, min, and max points of convex hull.
160 // note to self: self, you should think about checking for runways
161 // that span the data line
162 FGBucket b(average.lon, average.lat);
163 FGBucket b_min(min.x, min.y);
164 FGBucket b_max(max.x, max.y);
165 cout << "Bucket center = " << b << endl;
166 cout << "Bucket min = " << b_min << endl;
167 cout << "Bucket max = " << b_max << endl;
169 if ( b_min == b_max ) {
170 write_airport( hull_list, b, root, true );
175 fgBucketDiff(b_min, b_max, &dx, &dy);
176 cout << "airport spans tile boundaries" << endl;
177 cout << " dx = " << dx << " dy = " << dy << endl;
179 if ( (dx > 1) || (dy > 1) ) {
180 cout << "somethings really wrong!!!!" << endl;
184 for ( j = 0; j <= dy; j++ ) {
185 for ( i = 0; i <= dx; i++ ) {
186 b_cur = fgBucketOffset(min.x, min.y, i, j);
188 write_airport( hull_list, b_cur, root, true );
190 write_airport( hull_list, b_cur, root, false );
194 // string answer; cin >> answer;
199 // reads the apt_full file and extracts and processes the individual
201 int main( int argc, char **argv ) {
202 list < string > runway_list;
203 string airport, last_airport;
207 fglog().setLogLevels( FG_ALL, FG_DEBUG );
210 FG_LOG( FG_GENERAL, FG_ALERT,
211 "Usage " << argv[0] << " <apt_file> <work dir>" );
215 fg_gzifstream in( argv[1] );
217 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << argv[1] );
221 while ( ! in.eof() ) {
222 in.getline(tmp, 256);
224 // cout << line << endl;
226 if ( line.length() == 0 ) {
228 } else if ( line[0] == '#' ) {
230 } else if ( line[0] == '\t' ) {
232 runway_list.push_back(line);
234 // start of airport record
237 if ( last_airport.length() ) {
238 // process previous record
239 process_airport(last_airport, runway_list, argv[2]);
242 // clear runway list for start of next airport
243 runway_list.erase(runway_list.begin(), runway_list.end());
245 last_airport = airport;
249 if ( last_airport.length() ) {
250 // process previous record
251 process_airport(last_airport, runway_list, argv[2]);
259 // Revision 1.6 1999/02/11 01:10:51 curt
260 // Start of scenery revamp project.
262 // Revision 1.5 1998/09/17 18:40:43 curt
263 // Debug message tweaks.
265 // Revision 1.4 1998/09/09 20:59:56 curt
266 // Loop construct tweaks for STL usage.
267 // Output airport file to be used to generate airport scenery on the fly
268 // by the run time sim.