1 // shape.cxx -- shape/gpc utils
3 // Written by Curtis Olson, started February 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
24 #include <Include/compiler.h>
28 #include <Bucket/newbucket.hxx>
29 #include <Debug/logstream.hxx>
31 #include <Polygon/index.hxx>
32 #include <Polygon/names.hxx>
36 #define FG_MAX_VERTICES 100000
37 static gpc_vertex_list v_list;
46 static void clip_and_write_poly( string root, long int p_index, AreaType area,
47 FGBucket b, gpc_polygon *shape ) {
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];
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) {
66 } else if ( c.y >= 89.0) {
72 FG_LOG ( FG_GENERAL, FG_ALERT,
73 "Out of range latitude in clip_and_write_poly() = " << c.y );
76 FG_LOG( FG_GENERAL, FG_INFO, " (" << min.x << "," << min.y << ") ("
77 << max.x << "," << max.y << ")" );
79 // set up clipping tile
80 v_list.vertex[0].x = min.x;
81 v_list.vertex[0].y = min.y;
83 v_list.vertex[1].x = max.x;
84 v_list.vertex[1].y = min.y;
86 v_list.vertex[2].x = max.x;
87 v_list.vertex[2].y = max.y;
89 v_list.vertex[3].x = min.x;
90 v_list.vertex[3].y = max.y;
92 v_list.num_vertices = 4;
94 base.num_contours = 0;
96 gpc_add_contour( &base, &v_list, 0 );
98 // FG_LOG( FG_GENERAL, FG_DEBUG, "base = 4 vertices" );
101 FILE *bfp= fopen("base", "w");
102 gpc_write_polygon(bfp, &base);
106 gpc_polygon_clip(GPC_INT, &base, shape, &result);
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() );
114 sprintf( tile_name, "%ld", t_index );
115 string polyfile = path + "/" + tile_name;
117 sprintf( poly_index, "%ld", p_index );
119 polyfile += poly_index;
121 string poly_type = get_area_name( area );
122 if ( poly_type == "Unknown" ) {
123 cout << "unknown area type in clip_and_write_poly()!" << endl;
127 FILE *rfp= fopen( polyfile.c_str(), "w" );
128 fprintf( rfp, "%s\n", poly_type.c_str() );
129 gpc_write_polygon( rfp, 1, &result );
133 gpc_free_polygon(&base);
134 gpc_free_polygon(&result);
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];;
147 // initialize a gpc_polygon
148 void init_shape(gpc_polygon *shape) {
149 shape->num_contours = 0;
150 shape->contour = NULL;
154 // make a gpc_polygon
155 void add_to_shape(int count, double *coords, gpc_polygon *shape) {
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];
162 v_list.num_vertices = count;
163 gpc_add_contour( shape, &v_list, 0 );
167 // process shape (write polygon to all intersecting tiles)
168 void process_shape(string path, AreaType area, gpc_polygon *gpc_shape) {
173 min.x = min.y = 200.0;
174 max.x = max.y = -200.0;
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;
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; }
190 FILE *sfp= fopen("shape", "w");
191 gpc_write_polygon(sfp, gpc_shape);
196 // get next polygon index
197 index = poly_index_next();
199 FG_LOG( FG_GENERAL, FG_INFO, " min = " << min.x << "," << min.y
200 << " max = " << max.x << "," << max.y );
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 );
210 if ( b_min == b_max ) {
211 clip_and_write_poly( path, index, area, b_min, gpc_shape );
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
222 if ( (dx > 100) || (dy > 100) ) {
223 FG_LOG( FG_GENERAL, FG_ALERT,
224 "somethings really wrong!!!!" );
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 );
234 // string answer; cin >> answer;
239 // free a gpc_polygon
240 void free_shape(gpc_polygon *shape) {
241 gpc_free_polygon(shape);