]> git.mxchange.org Git - flightgear.git/blob - Main/construct.cxx
Added GenOutput/
[flightgear.git] / Main / construct.cxx
1 // main.cxx -- top level construction routines
2 //
3 // Written by Curtis Olson, started March 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
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.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #include <sys/types.h>  // for directory reading
26 #include <dirent.h>     // for directory reading
27
28 #include <Bucket/newbucket.hxx>
29
30 #include <Array/array.hxx>
31 #include <Clipper/clipper.hxx>
32 #include <GenOutput/genobj.hxx>
33 #include <Triangulate/triangle.hxx>
34
35
36 // load regular grid of elevation data (dem based), return list of
37 // fitted nodes
38 int load_dem(const string& work_base, FGBucket& b, FGArray& array) {
39     fitnode_list result;
40     char tile_name[256];
41     string base = b.gen_base_path();
42     long int b_index = b.gen_index();
43     sprintf(tile_name, "%ld", b_index);
44
45     string dem_path = work_base + ".dem" + "/Scenery/" + base 
46         + "/" + tile_name + ".dem";
47     cout << "dem_path = " << dem_path << endl;
48
49     if ( ! array.open(dem_path) ) {
50         cout << "ERROR: cannot open " << dem_path << endl;
51         return 0;
52     }
53
54     array.parse();
55     array.fit( 100 );
56
57     return 1;
58 }
59
60
61 // do actual scan of directory and loading of files
62 int actual_load_polys( const string& dir, FGBucket& b, FGClipper& clipper ) {
63     int counter = 0;
64     char tile_char[256];
65     string base = b.gen_base_path();
66     long int b_index = b.gen_index();
67     sprintf(tile_char, "%ld", b_index);
68     string tile_str = tile_char;
69     string ext;
70
71     DIR *d;
72     struct dirent *de;
73
74     if ( (d = opendir( dir.c_str() )) == NULL ) {
75         cout << "cannot open directory " << dir << "\n";
76         return 0;
77     }
78
79     // load all matching polygon files
80     string file, f_index, full_path;
81     int pos;
82     while ( (de = readdir(d)) != NULL ) {
83         file = de->d_name;
84         pos = file.find(".");
85         f_index = file.substr(0, pos);
86
87         if ( tile_str == f_index ) {
88             ext = file.substr(pos + 1);
89             cout << file << "  " << f_index << "  '" << ext << "'" << endl;
90             full_path = dir + "/" + file;
91             if ( (ext == "dem") || (ext == "dem.gz") ) {
92                 // skip
93             } else {
94                 cout << "ext = '" << ext << "'" << endl;
95                 clipper.load_polys( full_path );
96                 ++counter;
97             }
98         }
99     }
100
101     return counter;
102 }
103
104
105 // load all 2d polygons matching the specified base path and clip
106 // against each other to resolve any overlaps
107 int load_polys( const string& work_base, FGBucket& b, FGClipper& clipper) {
108     string base = b.gen_base_path();
109     int result;
110
111     // initialize clipper
112     clipper.init();
113
114     // load airports
115     string poly_path = work_base + ".apt" + "/Scenery/" + base;
116     cout << "poly_path = " << poly_path << endl;
117     result = actual_load_polys( poly_path, b, clipper );
118     cout << "  loaded " << result << " polys" << endl;
119
120     // load hydro
121     poly_path = work_base + ".hydro" + "/Scenery/" + base;
122     cout << "poly_path = " << poly_path << endl;
123     result = actual_load_polys( poly_path, b, clipper );
124     cout << "  loaded " << result << " polys" << endl;
125
126     point2d min, max;
127     min.x = b.get_center_lon() - 0.5 * b.get_width();
128     min.y = b.get_center_lat() - 0.5 * b.get_height();
129     max.x = b.get_center_lon() + 0.5 * b.get_width();
130     max.y = b.get_center_lat() + 0.5 * b.get_height();
131
132     // do clipping
133     cout << "clipping polygons" << endl;
134     clipper.clip_all(min, max);
135
136     return 1;
137 }
138
139
140 // triangulate the data for each polygon
141 void do_triangulate( const FGArray& array, const FGClipper& clipper,
142                   FGTriangle& t ) {
143     // first we need to consolidate the points of the DEM fit list and
144     // all the polygons into a more "Triangle" friendly format
145
146     fitnode_list fit_list = array.get_fit_node_list();
147     FGgpcPolyList gpc_polys = clipper.get_polys_clipped();
148
149     cout << "ready to build node list and polygons" << endl;
150     t.build( fit_list, gpc_polys );
151     cout << "done building node list and polygons" << endl;
152
153     cout << "ready to do triangulation" << endl;
154     t.run_triangulate();
155     cout << "finished triangulation" << endl;
156 }
157
158
159 main(int argc, char **argv) {
160     fitnode_list fit_list;
161     double lon, lat;
162
163     if ( argc != 2 ) {
164         cout << "Usage: " << argv[0] << " work_base" << endl;
165         exit(-1);
166     }
167
168     string work_base = argv[1];
169    
170     // lon = -146.248360; lat = 61.133950;     // PAVD (Valdez, AK)
171     // lon = -110.664244; lat = 33.352890;     // P13
172     // lon = -93.211389; lat = 45.145000;      // KANE
173     // lon = -92.486188; lat = 44.590190;      // KRGK
174     lon = -89.744682312011719; lat= 29.314495086669922;
175
176     FGBucket b( lon, lat );
177
178     // load and fit grid of elevation data
179     FGArray array;
180     load_dem( work_base, b, array );
181
182     // load and clip 2d polygon data
183     FGClipper clipper;
184     load_polys( work_base, b, clipper );
185
186     // triangulate the data for each polygon
187     FGTriangle t;
188     do_triangulate( array, clipper, t );
189
190     // generate the output
191     fgGenOutput( t );
192 }
193
194
195 // $Log$
196 // Revision 1.7  1999/03/22 23:48:29  curt
197 // Added GenOutput/
198 //
199 // Revision 1.6  1999/03/21 15:48:01  curt
200 // Removed Dem2node from the Tools fold.
201 // Tweaked the triangulator options to add quality mesh refinement.
202 //
203 // Revision 1.5  1999/03/21 14:02:05  curt
204 // Added a mechanism to dump out the triangle structures for viewing.
205 // Fixed a couple bugs in first pass at triangulation.
206 // - needed to explicitely initialize the polygon accumulator in triangle.cxx
207 //   before each polygon rather than depending on the default behavior.
208 // - Fixed a problem with region attribute propagation where I wasn't generating
209 //   the hole points correctly.
210 //
211 // Revision 1.4  1999/03/20 20:32:54  curt
212 // First mostly successful tile triangulation works.  There's plenty of tweaking
213 // to do, but we are marching in the right direction.
214 //
215 // Revision 1.3  1999/03/19 00:26:52  curt
216 // Minor tweaks ...
217 //
218 // Revision 1.2  1999/03/17 23:49:52  curt
219 // Started work on Triangulate/ section.
220 //
221 // Revision 1.1  1999/03/14 00:03:24  curt
222 // Initial revision.
223 //
224
225