]> git.mxchange.org Git - flightgear.git/blob - Main/construct.cxx
Started work on Triangulate/ section.
[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)
36 int load_dem(const string& work_base, FGBucket& b, FGArray& array) {
37     char tile_name[256];
38     string base = b.gen_base_path();
39     long int b_index = b.gen_index();
40     sprintf(tile_name, "%ld", b_index);
41
42     string dem_path = work_base + ".dem" + "/Scenery/" + base 
43         + "/" + tile_name + ".dem";
44     cout << "dem_path = " << dem_path << endl;
45
46     if ( ! array.open(dem_path) ) {
47         return 0;
48     }
49     array.parse();
50     array.fit( 100 );
51
52     return 1;
53 }
54
55
56 // do actual scan of directory and loading of files
57 int actual_load_polys( const string& dir, FGBucket& b, FGClipper& clipper ) {
58     int counter = 0;
59     char tile_char[256];
60     string base = b.gen_base_path();
61     long int b_index = b.gen_index();
62     sprintf(tile_char, "%ld", b_index);
63     string tile_str = tile_char;
64     string ext;
65
66     DIR *d;
67     struct dirent *de;
68
69     if ( (d = opendir( dir.c_str() )) == NULL ) {
70         cout << "cannot open directory " << dir << "\n";
71         return 0;
72     }
73
74     // load all matching polygon files
75     string file, f_index, full_path;
76     int pos;
77     while ( (de = readdir(d)) != NULL ) {
78         file = de->d_name;
79         pos = file.find(".");
80         f_index = file.substr(0, pos);
81
82         if ( tile_str == f_index ) {
83             ext = file.substr(pos + 1);
84             cout << file << "  " << f_index << "  '" << ext << "'" << endl;
85             full_path = dir + "/" + file;
86             if ( (ext == "dem") || (ext == "dem.gz") ) {
87                 // skip
88             } else {
89                 cout << "ext = '" << ext << "'" << endl;
90                 clipper.load_polys( full_path );
91                 ++counter;
92             }
93         }
94     }
95
96     return counter;
97 }
98
99
100 // load all 2d polygons matching the specified base path and clip
101 // against each other to resolve any overlaps
102 int load_polys( const string& work_base, FGBucket& b, FGClipper& clipper) {
103     string base = b.gen_base_path();
104     int result;
105
106     // initialize clipper
107     clipper.init();
108
109     // load airports
110     string poly_path = work_base + ".apt" + "/Scenery/" + base;
111     cout << "poly_path = " << poly_path << endl;
112     result = actual_load_polys( poly_path, b, clipper );
113     cout << "  loaded " << result << " polys" << endl;
114
115     // load hydro
116     poly_path = work_base + ".hydro" + "/Scenery/" + base;
117     cout << "poly_path = " << poly_path << endl;
118     result = actual_load_polys( poly_path, b, clipper );
119     cout << "  loaded " << result << " polys" << endl;
120
121     point2d min, max;
122     min.x = b.get_center_lon() - 0.5 * b.get_width();
123     min.y = b.get_center_lat() - 0.5 * b.get_height();
124     max.x = b.get_center_lon() + 0.5 * b.get_width();
125     max.y = b.get_center_lat() + 0.5 * b.get_height();
126
127     // do clipping
128     cout << "clipping polygons" << endl;
129     clipper.clip_all(min, max);
130
131     return 1;
132 }
133
134
135 // triangulate the data for each polygon
136 void triangulate( const FGArray& array, const FGClipper& clipper,
137                   FGTriangle& t ) {
138     // first we need to consolidate the points of all the polygons
139     // into a more "Triangle" friendly format
140     FGgpcPolyList gpc_polys;
141
142     gpc_polys = clipper.get_polys_clipped();
143
144     t.build( gpc_polys );
145 }
146
147
148 main(int argc, char **argv) {
149     double lon, lat;
150
151     if ( argc != 2 ) {
152         cout << "Usage: " << argv[0] << " work_base" << endl;
153         exit(-1);
154     }
155
156     string work_base = argv[1];
157    
158     lon = -146.248360; lat = 61.133950;  // PAVD (Valdez, AK)
159     // lon = -110.664244; lat = 33.352890;  // P13
160     FGBucket b( lon, lat );
161
162     // load and fit grid of elevation data
163     FGArray array;
164     load_dem( work_base, b, array );
165
166     // load and clip 2d polygon data
167     FGClipper clipper;
168     load_polys( work_base, b, clipper );
169
170     // triangulate the data for each polygon
171     FGTriangle t;
172     triangulate( array, clipper, t );
173 }
174
175
176 // $Log$
177 // Revision 1.2  1999/03/17 23:49:52  curt
178 // Started work on Triangulate/ section.
179 //
180 // Revision 1.1  1999/03/14 00:03:24  curt
181 // Initial revision.
182 //
183
184