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