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.
22 // (Log is kept at end of this file)
25 #include <Include/compiler.h>
29 #include <Bucket/newbucket.hxx>
30 #include <Debug/logstream.hxx>
32 #include <Polygon/index.hxx>
33 #include <Polygon/names.hxx>
37 #define FG_MAX_VERTICES 100000
38 static gpc_vertex_list v_list;
47 static void clip_and_write_poly( string root, int p_index, AreaType area,
48 FGBucket b, gpc_polygon *shape ) {
50 c.x = b.get_center_lon();
51 c.y = b.get_center_lat();
52 double span = bucket_span(c.y);
53 gpc_polygon base, result;
54 char tile_name[256], poly_index[256];
56 // calculate bucket dimensions
57 if ( (c.y >= -89.0) && (c.y < 89.0) ) {
58 min.x = c.x - span / 2.0;
59 max.x = c.x + span / 2.0;
60 min.y = c.y - FG_HALF_BUCKET_SPAN;
61 max.y = c.y + FG_HALF_BUCKET_SPAN;
62 } else if ( c.y < -89.0) {
67 } else if ( c.y >= 89.0) {
73 FG_LOG ( FG_GENERAL, FG_ALERT,
74 "Out of range latitude in clip_and_write_poly() = " << c.y );
77 FG_LOG( FG_GENERAL, FG_INFO, " (" << min.x << "," << min.y << ") ("
78 << max.x << "," << max.y << ")" );
80 // set up clipping tile
81 v_list.vertex[0].x = min.x;
82 v_list.vertex[0].y = min.y;
84 v_list.vertex[1].x = max.x;
85 v_list.vertex[1].y = min.y;
87 v_list.vertex[2].x = max.x;
88 v_list.vertex[2].y = max.y;
90 v_list.vertex[3].x = min.x;
91 v_list.vertex[3].y = max.y;
93 v_list.num_vertices = 4;
95 base.num_contours = 0;
97 gpc_add_contour( &base, &v_list );
99 // FG_LOG( FG_GENERAL, FG_DEBUG, "base = 4 vertices" );
102 FILE *bfp= fopen("base", "w");
103 gpc_write_polygon(bfp, &base);
107 gpc_polygon_clip(GPC_INT, &base, shape, &result);
109 if ( result.num_contours > 0 ) {
110 long int t_index = b.gen_index();
111 string path = root + "/Scenery/" + b.gen_base_path();
112 string command = "mkdir -p " + path;
113 system( command.c_str() );
115 sprintf( tile_name, "%ld", t_index );
116 string polyfile = path + "/" + tile_name;
118 sprintf( poly_index, "%d", p_index );
120 polyfile += poly_index;
122 string poly_type = get_area_name( area );
123 if ( poly_type == "Unknown" ) {
124 cout << "unknown area type in clip_and_write_poly()!" << endl;
128 FILE *rfp= fopen( polyfile.c_str(), "w" );
129 fprintf( rfp, "%s\n", poly_type.c_str() );
130 gpc_write_polygon( rfp, &result );
134 gpc_free_polygon(&base);
135 gpc_free_polygon(&result);
139 // Initialize structure we use to create polygons for the gpc library
140 bool shape_utils_init() {
141 v_list.num_vertices = 0;
142 v_list.vertex = new gpc_vertex[FG_MAX_VERTICES];;
148 // initialize a gpc_polygon
149 void init_shape(gpc_polygon *shape) {
150 shape->num_contours = 0;
151 shape->contour = NULL;
155 // make a gpc_polygon
156 void add_to_shape(int count, double *coords, gpc_polygon *shape) {
158 for ( int i = 0; i < count; i++ ) {
159 v_list.vertex[i].x = coords[i*2+0];
160 v_list.vertex[i].y = coords[i*2+1];
163 v_list.num_vertices = count;
164 gpc_add_contour( shape, &v_list );
168 // process shape (write polygon to all intersecting tiles)
169 void process_shape(string path, AreaType area, gpc_polygon *gpc_shape) {
174 min.x = min.y = 200.0;
175 max.x = max.y = -200.0;
177 // find min/max of polygon
178 for ( i = 0; i < gpc_shape->num_contours; i++ ) {
179 for ( j = 0; j < gpc_shape->contour[i].num_vertices; j++ ) {
180 double x = gpc_shape->contour[i].vertex[j].x;
181 double y = gpc_shape->contour[i].vertex[j].y;
183 if ( x < min.x ) { min.x = x; }
184 if ( y < min.y ) { min.y = y; }
185 if ( x > max.x ) { max.x = x; }
186 if ( y > max.y ) { max.y = y; }
191 FILE *sfp= fopen("shape", "w");
192 gpc_write_polygon(sfp, gpc_shape);
197 // get next polygon index
198 index = poly_index_next();
200 FG_LOG( FG_GENERAL, FG_INFO, " min = " << min.x << "," << min.y
201 << " max = " << max.x << "," << max.y );
203 // find buckets for min, and max points of convex hull.
204 // note to self: self, you should think about checking for
205 // polygons that span the date line
206 FGBucket b_min(min.x, min.y);
207 FGBucket b_max(max.x, max.y);
208 FG_LOG( FG_GENERAL, FG_INFO, " Bucket min = " << b_min );
209 FG_LOG( FG_GENERAL, FG_INFO, " Bucket max = " << b_max );
211 if ( b_min == b_max ) {
212 clip_and_write_poly( path, index, area, b_min, gpc_shape );
217 fgBucketDiff(b_min, b_max, &dx, &dy);
218 FG_LOG( FG_GENERAL, FG_INFO,
219 " polygon spans tile boundaries" );
220 FG_LOG( FG_GENERAL, FG_INFO, " dx = " << dx
223 if ( (dx > 100) || (dy > 100) ) {
224 FG_LOG( FG_GENERAL, FG_ALERT,
225 "somethings really wrong!!!!" );
229 for ( j = 0; j <= dy; j++ ) {
230 for ( i = 0; i <= dx; i++ ) {
231 b_cur = fgBucketOffset(min.x, min.y, i, j);
232 clip_and_write_poly( path, index, area, b_cur, gpc_shape );
235 // string answer; cin >> answer;
240 // free a gpc_polygon
241 void free_shape(gpc_polygon *shape) {
242 gpc_free_polygon(shape);
247 // Revision 1.2 1999/02/25 21:31:08 curt
248 // First working version???
250 // Revision 1.1 1999/02/23 01:29:06 curt
251 // Additional progress.