1999-03-14 00:03:24 +00:00
|
|
|
// main.cxx -- top level construction routines
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started March 1999.
|
|
|
|
//
|
|
|
|
// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
// (Log is kept at end of this file)
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h> // for directory reading
|
|
|
|
#include <dirent.h> // for directory reading
|
|
|
|
|
|
|
|
#include <Bucket/newbucket.hxx>
|
|
|
|
|
|
|
|
#include <Array/array.hxx>
|
|
|
|
#include <Clipper/clipper.hxx>
|
1999-03-22 23:48:20 +00:00
|
|
|
#include <GenOutput/genobj.hxx>
|
1999-03-17 23:49:51 +00:00
|
|
|
#include <Triangulate/triangle.hxx>
|
1999-03-14 00:03:24 +00:00
|
|
|
|
|
|
|
|
1999-03-20 20:32:51 +00:00
|
|
|
// load regular grid of elevation data (dem based), return list of
|
|
|
|
// fitted nodes
|
1999-03-14 00:03:24 +00:00
|
|
|
int load_dem(const string& work_base, FGBucket& b, FGArray& array) {
|
1999-03-20 20:32:51 +00:00
|
|
|
fitnode_list result;
|
1999-03-14 00:03:24 +00:00
|
|
|
string base = b.gen_base_path();
|
|
|
|
|
|
|
|
string dem_path = work_base + ".dem" + "/Scenery/" + base
|
1999-03-25 19:04:31 +00:00
|
|
|
+ "/" + b.gen_index_str() + ".dem";
|
1999-03-14 00:03:24 +00:00
|
|
|
cout << "dem_path = " << dem_path << endl;
|
|
|
|
|
|
|
|
if ( ! array.open(dem_path) ) {
|
1999-03-20 20:32:51 +00:00
|
|
|
cout << "ERROR: cannot open " << dem_path << endl;
|
1999-03-14 00:03:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1999-03-20 20:32:51 +00:00
|
|
|
|
1999-03-14 00:03:24 +00:00
|
|
|
array.parse();
|
|
|
|
array.fit( 100 );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-17 23:49:51 +00:00
|
|
|
// do actual scan of directory and loading of files
|
|
|
|
int actual_load_polys( const string& dir, FGBucket& b, FGClipper& clipper ) {
|
|
|
|
int counter = 0;
|
1999-03-14 00:03:24 +00:00
|
|
|
string base = b.gen_base_path();
|
1999-03-25 19:04:31 +00:00
|
|
|
string tile_str = b.gen_index_str();
|
1999-03-17 23:49:51 +00:00
|
|
|
string ext;
|
1999-03-14 00:03:24 +00:00
|
|
|
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *de;
|
|
|
|
|
1999-03-17 23:49:51 +00:00
|
|
|
if ( (d = opendir( dir.c_str() )) == NULL ) {
|
|
|
|
cout << "cannot open directory " << dir << "\n";
|
1999-03-14 00:03:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load all matching polygon files
|
|
|
|
string file, f_index, full_path;
|
|
|
|
int pos;
|
|
|
|
while ( (de = readdir(d)) != NULL ) {
|
|
|
|
file = de->d_name;
|
|
|
|
pos = file.find(".");
|
|
|
|
f_index = file.substr(0, pos);
|
|
|
|
|
|
|
|
if ( tile_str == f_index ) {
|
1999-03-17 23:49:51 +00:00
|
|
|
ext = file.substr(pos + 1);
|
|
|
|
cout << file << " " << f_index << " '" << ext << "'" << endl;
|
|
|
|
full_path = dir + "/" + file;
|
|
|
|
if ( (ext == "dem") || (ext == "dem.gz") ) {
|
|
|
|
// skip
|
|
|
|
} else {
|
|
|
|
cout << "ext = '" << ext << "'" << endl;
|
|
|
|
clipper.load_polys( full_path );
|
|
|
|
++counter;
|
|
|
|
}
|
1999-03-14 00:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-17 23:49:51 +00:00
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// load all 2d polygons matching the specified base path and clip
|
|
|
|
// against each other to resolve any overlaps
|
|
|
|
int load_polys( const string& work_base, FGBucket& b, FGClipper& clipper) {
|
|
|
|
string base = b.gen_base_path();
|
|
|
|
int result;
|
|
|
|
|
|
|
|
// initialize clipper
|
|
|
|
clipper.init();
|
|
|
|
|
|
|
|
// load airports
|
|
|
|
string poly_path = work_base + ".apt" + "/Scenery/" + base;
|
|
|
|
cout << "poly_path = " << poly_path << endl;
|
|
|
|
result = actual_load_polys( poly_path, b, clipper );
|
|
|
|
cout << " loaded " << result << " polys" << endl;
|
|
|
|
|
|
|
|
// load hydro
|
|
|
|
poly_path = work_base + ".hydro" + "/Scenery/" + base;
|
|
|
|
cout << "poly_path = " << poly_path << endl;
|
|
|
|
result = actual_load_polys( poly_path, b, clipper );
|
|
|
|
cout << " loaded " << result << " polys" << endl;
|
|
|
|
|
1999-03-14 00:03:24 +00:00
|
|
|
point2d min, max;
|
|
|
|
min.x = b.get_center_lon() - 0.5 * b.get_width();
|
|
|
|
min.y = b.get_center_lat() - 0.5 * b.get_height();
|
|
|
|
max.x = b.get_center_lon() + 0.5 * b.get_width();
|
|
|
|
max.y = b.get_center_lat() + 0.5 * b.get_height();
|
|
|
|
|
|
|
|
// do clipping
|
1999-03-17 23:49:51 +00:00
|
|
|
cout << "clipping polygons" << endl;
|
1999-03-14 00:03:24 +00:00
|
|
|
clipper.clip_all(min, max);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-17 23:49:51 +00:00
|
|
|
// triangulate the data for each polygon
|
1999-03-20 20:32:51 +00:00
|
|
|
void do_triangulate( const FGArray& array, const FGClipper& clipper,
|
1999-03-17 23:49:51 +00:00
|
|
|
FGTriangle& t ) {
|
1999-03-20 20:32:51 +00:00
|
|
|
// first we need to consolidate the points of the DEM fit list and
|
|
|
|
// all the polygons into a more "Triangle" friendly format
|
1999-03-17 23:49:51 +00:00
|
|
|
|
1999-03-20 20:32:51 +00:00
|
|
|
fitnode_list fit_list = array.get_fit_node_list();
|
|
|
|
FGgpcPolyList gpc_polys = clipper.get_polys_clipped();
|
1999-03-17 23:49:51 +00:00
|
|
|
|
1999-03-19 00:26:51 +00:00
|
|
|
cout << "ready to build node list and polygons" << endl;
|
1999-03-20 20:32:51 +00:00
|
|
|
t.build( fit_list, gpc_polys );
|
1999-03-19 00:26:51 +00:00
|
|
|
cout << "done building node list and polygons" << endl;
|
1999-03-20 20:32:51 +00:00
|
|
|
|
|
|
|
cout << "ready to do triangulation" << endl;
|
|
|
|
t.run_triangulate();
|
|
|
|
cout << "finished triangulation" << endl;
|
1999-03-17 23:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-23 22:02:03 +00:00
|
|
|
// generate the flight gear scenery file
|
1999-03-25 19:04:31 +00:00
|
|
|
void do_output( const FGBucket &b, const FGTriangle& t, FGGenOutput& output ) {
|
1999-03-23 22:02:03 +00:00
|
|
|
output.build( t );
|
1999-03-25 19:04:31 +00:00
|
|
|
output.write( b, "output" );
|
1999-03-23 22:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-14 00:03:24 +00:00
|
|
|
main(int argc, char **argv) {
|
1999-03-20 20:32:51 +00:00
|
|
|
fitnode_list fit_list;
|
1999-03-14 00:03:24 +00:00
|
|
|
double lon, lat;
|
|
|
|
|
|
|
|
if ( argc != 2 ) {
|
|
|
|
cout << "Usage: " << argv[0] << " work_base" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
string work_base = argv[1];
|
|
|
|
|
1999-03-21 14:02:05 +00:00
|
|
|
// lon = -146.248360; lat = 61.133950; // PAVD (Valdez, AK)
|
|
|
|
// lon = -110.664244; lat = 33.352890; // P13
|
|
|
|
// lon = -93.211389; lat = 45.145000; // KANE
|
1999-03-21 15:48:00 +00:00
|
|
|
// lon = -92.486188; lat = 44.590190; // KRGK
|
|
|
|
lon = -89.744682312011719; lat= 29.314495086669922;
|
1999-03-19 00:26:51 +00:00
|
|
|
|
1999-03-14 00:03:24 +00:00
|
|
|
FGBucket b( lon, lat );
|
|
|
|
|
|
|
|
// load and fit grid of elevation data
|
|
|
|
FGArray array;
|
|
|
|
load_dem( work_base, b, array );
|
|
|
|
|
|
|
|
// load and clip 2d polygon data
|
|
|
|
FGClipper clipper;
|
|
|
|
load_polys( work_base, b, clipper );
|
1999-03-17 23:49:51 +00:00
|
|
|
|
|
|
|
// triangulate the data for each polygon
|
|
|
|
FGTriangle t;
|
1999-03-20 20:32:51 +00:00
|
|
|
do_triangulate( array, clipper, t );
|
1999-03-22 23:48:20 +00:00
|
|
|
|
|
|
|
// generate the output
|
1999-03-23 22:02:03 +00:00
|
|
|
FGGenOutput output;
|
1999-03-25 19:04:31 +00:00
|
|
|
do_output( b, t, output );
|
1999-03-14 00:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// $Log$
|
1999-03-25 19:04:31 +00:00
|
|
|
// Revision 1.9 1999/03/25 19:04:31 curt
|
|
|
|
// Preparations for outputing scenery file to correct location.
|
|
|
|
// Minor tweaks related to FGBucket usage.
|
|
|
|
//
|
1999-03-23 22:02:03 +00:00
|
|
|
// Revision 1.8 1999/03/23 22:02:17 curt
|
|
|
|
// Worked on creating data to output ... normals, bounding spheres, etc.
|
|
|
|
//
|
1999-03-22 23:48:20 +00:00
|
|
|
// Revision 1.7 1999/03/22 23:48:29 curt
|
|
|
|
// Added GenOutput/
|
|
|
|
//
|
1999-03-21 15:48:00 +00:00
|
|
|
// Revision 1.6 1999/03/21 15:48:01 curt
|
|
|
|
// Removed Dem2node from the Tools fold.
|
|
|
|
// Tweaked the triangulator options to add quality mesh refinement.
|
|
|
|
//
|
1999-03-21 14:02:05 +00:00
|
|
|
// Revision 1.5 1999/03/21 14:02:05 curt
|
|
|
|
// Added a mechanism to dump out the triangle structures for viewing.
|
|
|
|
// Fixed a couple bugs in first pass at triangulation.
|
|
|
|
// - needed to explicitely initialize the polygon accumulator in triangle.cxx
|
|
|
|
// before each polygon rather than depending on the default behavior.
|
|
|
|
// - Fixed a problem with region attribute propagation where I wasn't generating
|
|
|
|
// the hole points correctly.
|
|
|
|
//
|
1999-03-20 20:32:51 +00:00
|
|
|
// Revision 1.4 1999/03/20 20:32:54 curt
|
|
|
|
// First mostly successful tile triangulation works. There's plenty of tweaking
|
|
|
|
// to do, but we are marching in the right direction.
|
|
|
|
//
|
1999-03-19 00:26:51 +00:00
|
|
|
// Revision 1.3 1999/03/19 00:26:52 curt
|
|
|
|
// Minor tweaks ...
|
|
|
|
//
|
1999-03-17 23:49:51 +00:00
|
|
|
// Revision 1.2 1999/03/17 23:49:52 curt
|
|
|
|
// Started work on Triangulate/ section.
|
|
|
|
//
|
1999-03-14 00:03:24 +00:00
|
|
|
// Revision 1.1 1999/03/14 00:03:24 curt
|
|
|
|
// Initial revision.
|
|
|
|
//
|
|
|
|
|
|
|
|
|