]> git.mxchange.org Git - flightgear.git/blob - Tools/Prep/ShapeFile/shape.cxx
Changes to support new gpc api with hole tracking support.
[flightgear.git] / Tools / Prep / ShapeFile / shape.cxx
1 // shape.cxx -- shape/gpc utils
2 //
3 // Written by Curtis Olson, started February 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 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.
11 //
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.
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
23
24 #include <Include/compiler.h>
25
26 #include STL_STRING
27
28 #include <Bucket/newbucket.hxx>
29 #include <Debug/logstream.hxx>
30
31 #include <Polygon/index.hxx>
32 #include <Polygon/names.hxx>
33 #include "shape.hxx"
34
35
36 #define FG_MAX_VERTICES 100000
37 static gpc_vertex_list v_list;
38
39
40 class point2d {
41 public:
42     double x, y;
43 };
44
45
46 static void clip_and_write_poly( string root, long int p_index, AreaType area, 
47                                  FGBucket b, gpc_polygon *shape ) {
48     point2d c, min, max;
49     c.x = b.get_center_lon();
50     c.y = b.get_center_lat();
51     double span = bucket_span(c.y);
52     gpc_polygon base, result;
53     char tile_name[256], poly_index[256];
54
55     // calculate bucket dimensions
56     if ( (c.y >= -89.0) && (c.y < 89.0) ) {
57         min.x = c.x - span / 2.0;
58         max.x = c.x + span / 2.0;
59         min.y = c.y - FG_HALF_BUCKET_SPAN;
60         max.y = c.y + FG_HALF_BUCKET_SPAN;
61     } else if ( c.y < -89.0) {
62         min.x = -90.0;
63         max.x = -89.0;
64         min.y = -180.0;
65         max.y = 180.0;
66     } else if ( c.y >= 89.0) {
67         min.x = 89.0;
68         max.x = 90.0;
69         min.y = -180.0;
70         max.y = 180.0;
71     } else {
72         FG_LOG ( FG_GENERAL, FG_ALERT, 
73                  "Out of range latitude in clip_and_write_poly() = " << c.y );
74     }
75
76     FG_LOG( FG_GENERAL, FG_INFO, "  (" << min.x << "," << min.y << ") ("
77             << max.x << "," << max.y << ")" );
78
79     // set up clipping tile
80     v_list.vertex[0].x = min.x;
81     v_list.vertex[0].y = min.y;
82
83     v_list.vertex[1].x = max.x;
84     v_list.vertex[1].y = min.y;
85
86     v_list.vertex[2].x = max.x;
87     v_list.vertex[2].y = max.y;
88
89     v_list.vertex[3].x = min.x;
90     v_list.vertex[3].y = max.y;
91
92     v_list.num_vertices = 4;
93
94     base.num_contours = 0;
95     base.contour = NULL;
96     gpc_add_contour( &base, &v_list, 0 );
97
98     // FG_LOG( FG_GENERAL, FG_DEBUG, "base = 4 vertices" );
99
100     /*
101     FILE *bfp= fopen("base", "w");
102     gpc_write_polygon(bfp, &base);
103     fclose(bfp);
104     */
105
106     gpc_polygon_clip(GPC_INT, &base, shape, &result);
107
108     if ( result.num_contours > 0 ) {
109         long int t_index = b.gen_index();
110         string path = root + "/Scenery/" + b.gen_base_path();
111         string command = "mkdir -p " + path;
112         system( command.c_str() );
113
114         sprintf( tile_name, "%ld", t_index );
115         string polyfile = path + "/" + tile_name;
116
117         sprintf( poly_index, "%ld", p_index );
118         polyfile += ".";
119         polyfile += poly_index;
120
121         string poly_type = get_area_name( area );
122         if ( poly_type == "Unknown" ) {
123             cout << "unknown area type in clip_and_write_poly()!" << endl;
124             exit(-1);
125         }
126         
127         FILE *rfp= fopen( polyfile.c_str(), "w" );
128         fprintf( rfp, "%s\n", poly_type.c_str() );
129         gpc_write_polygon( rfp, 1, &result );
130         fclose( rfp );
131     }
132
133     gpc_free_polygon(&base);
134     gpc_free_polygon(&result);
135 }
136
137
138 // Initialize structure we use to create polygons for the gpc library
139 bool shape_utils_init() {
140     v_list.num_vertices = 0;
141     v_list.vertex = new gpc_vertex[FG_MAX_VERTICES];;
142
143     return true;
144 }
145
146
147 // initialize a gpc_polygon
148 void init_shape(gpc_polygon *shape) {
149     shape->num_contours = 0;
150     shape->contour = NULL;
151 }
152
153
154 // make a gpc_polygon
155 void add_to_shape(int count, double *coords, gpc_polygon *shape) {
156     
157     for ( int i = 0; i < count; i++ ) {
158         v_list.vertex[i].x = coords[i*2+0];
159         v_list.vertex[i].y = coords[i*2+1];
160     }
161
162     v_list.num_vertices = count;
163     gpc_add_contour( shape, &v_list, 0 );
164 }
165
166
167 // process shape (write polygon to all intersecting tiles)
168 void process_shape(string path, AreaType area, gpc_polygon *gpc_shape) {
169     point2d min, max;
170     long int index;
171     int i, j;
172
173     min.x = min.y = 200.0;
174     max.x = max.y = -200.0;
175
176     // find min/max of polygon
177     for ( i = 0; i < gpc_shape->num_contours; i++ ) {
178         for ( j = 0; j < gpc_shape->contour[i].num_vertices; j++ ) {
179             double x = gpc_shape->contour[i].vertex[j].x;
180             double y = gpc_shape->contour[i].vertex[j].y;
181
182             if ( x < min.x ) { min.x = x; }
183             if ( y < min.y ) { min.y = y; }
184             if ( x > max.x ) { max.x = x; }
185             if ( y > max.y ) { max.y = y; }
186         }
187     }
188
189     /*
190     FILE *sfp= fopen("shape", "w");
191     gpc_write_polygon(sfp, gpc_shape);
192     fclose(sfp);
193     exit(-1);
194     */
195         
196     // get next polygon index
197     index = poly_index_next();
198
199     FG_LOG( FG_GENERAL, FG_INFO, "  min = " << min.x << "," << min.y
200             << " max = " << max.x << "," << max.y );
201
202     // find buckets for min, and max points of convex hull.
203     // note to self: self, you should think about checking for
204     // polygons that span the date line
205     FGBucket b_min(min.x, min.y);
206     FGBucket b_max(max.x, max.y);
207     FG_LOG( FG_GENERAL, FG_INFO, "  Bucket min = " << b_min );
208     FG_LOG( FG_GENERAL, FG_INFO, "  Bucket max = " << b_max );
209             
210     if ( b_min == b_max ) {
211         clip_and_write_poly( path, index, area, b_min, gpc_shape );
212     } else {
213         FGBucket b_cur;
214         int dx, dy, i, j;
215             
216         fgBucketDiff(b_min, b_max, &dx, &dy);
217         FG_LOG( FG_GENERAL, FG_INFO, 
218                 "  polygon spans tile boundaries" );
219         FG_LOG( FG_GENERAL, FG_INFO, "  dx = " << dx 
220                 << "  dy = " << dy );
221
222         if ( (dx > 100) || (dy > 100) ) {
223             FG_LOG( FG_GENERAL, FG_ALERT, 
224                     "somethings really wrong!!!!" );
225             exit(-1);
226         }
227
228         for ( j = 0; j <= dy; j++ ) {
229             for ( i = 0; i <= dx; i++ ) {
230                 b_cur = fgBucketOffset(min.x, min.y, i, j);
231                 clip_and_write_poly( path, index, area, b_cur, gpc_shape );
232             }
233         }
234         // string answer; cin >> answer;
235     }
236 }
237
238
239 // free a gpc_polygon
240 void free_shape(gpc_polygon *shape) {
241     gpc_free_polygon(shape);
242 }
243
244