]> git.mxchange.org Git - flightgear.git/blob - Scenery/bucketutils.c
Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
[flightgear.git] / Scenery / bucketutils.c
1 /**************************************************************************
2  * bucketutils.c -- support routines to handle fgBUCKET operations
3  *
4  * Written by Curtis Olson, started January 1998.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #include <math.h>
28 #include <stdio.h>
29
30 #include <Scenery/bucketutils.h>
31 #include <Include/fg_constants.h>
32
33
34 /* Generate the unique scenery tile index containing the specified
35    lon/lat parameters.
36
37    The index is constructed as follows:
38
39    9 bits - to represent 360 degrees of longitude (-180 to 179)
40    8 bits - to represent 180 degrees of latitude (-90 to 89)
41
42    Each 1 degree by 1 degree tile is further broken down into an 8x8
43    grid.  So we also need:
44
45    3 bits - to represent x (0 to 7)
46    3 bits - to represent y (0 to 7) */
47 long int fgBucketGenIndex(struct fgBUCKET *p) {
48     long index = 0;
49
50     index = ((p->lon + 180) << 14) + ((p->lat + 90) << 6) + (p->y << 3) + p->x;
51     /* printf("  generated index = %ld\n", index); */
52
53     return(index);
54 }
55
56
57 /* Parse a unique scenery tile index and find the lon, lat, x, and y */
58 void fgBucketParseIndex(long int index, struct fgBUCKET *p) {
59     p->lon = index >> 14;
60     index -= p->lon << 14;
61     p->lon -= 180;
62
63     p->lat = index >> 6;
64     index -= p->lat << 6;
65     p->lat -= 90;
66
67     p->y = index >> 3;
68     index -= p->y << 3;
69
70     p->x = index;
71 }
72
73
74 /* Build a path name from an tile index */
75 void fgBucketGenBasePath(struct fgBUCKET *p, char *path) {
76     long int index;
77     int top_lon, top_lat, main_lon, main_lat;
78     char hem, pole;
79
80     index = fgBucketGenIndex(p);
81
82     path[0] = '\0';
83
84     top_lon = p->lon / 10;
85     main_lon = p->lon;
86     if ( (p->lon < 0) && (top_lon * 10 != p->lon) ) {
87         top_lon -= 1;
88     }
89     top_lon *= 10;
90     if ( top_lon >= 0 ) {
91         hem = 'e';
92     } else {
93         hem = 'w';
94         top_lon *= -1;
95     }
96     if ( main_lon < 0 ) {
97         main_lon *= -1;
98     }
99
100     top_lat = p->lat / 10;
101     main_lat = p->lat;
102     if ( (p->lat < 0) && (top_lat * 10 != p->lat) ) {
103         top_lat -= 1;
104     }
105     top_lat *= 10;
106     if ( top_lat >= 0 ) {
107         pole = 'n';
108     } else {
109         pole = 's';
110         top_lat *= -1;
111     }
112     if ( main_lat < 0 ) {
113         main_lat *= -1;
114     }
115
116     sprintf(path, "%c%03d%c%03d/%c%03d%c%03d", 
117             hem, top_lon, pole, top_lat,
118             hem, main_lon, pole, main_lat);
119 }
120
121
122 /* offset an bucket struct by the specified amounts in the X & Y direction */
123 void fgBucketOffset(struct fgBUCKET *in, struct fgBUCKET *out, int x, int y) {
124     int diff, temp;
125     int dist_lat;
126
127     out->lon = in->lon;
128     out->lat = in->lat;
129     out->x = in->x;
130     out->y = in->y;
131
132     /* do X direction */
133     diff = out->x + x;
134     /* printf("      reducing x (%d)\n", diff); */
135     if ( diff >= 0 ) {
136         temp = diff / 8;
137     } else if ( diff < -7 ) {
138         temp = (diff + 1) / 8 - 1;
139     } else {
140         temp = diff / 8 - 1;
141     }
142     out->x = ((diff % 8) + 8) % 8;
143     out->lon = ( (out->lon + 180 + 360 + temp) % 360 ) - 180;
144
145     /* do Y direction */
146     diff = out->y + y;
147     /* printf("      reducing x (%d)\n", diff); */
148     if ( diff >= 0 ) {
149         temp = diff / 8;
150     } else if ( diff < -7 ) {
151         temp = (diff + 1) / 8 - 1;
152     } else {
153         temp = diff / 8 - 1;
154     }
155     out->y = ((diff % 8) + 8) % 8;
156     out->lat = out->lat + temp;
157
158     if ( out->lat >= 90 ) {
159         dist_lat = out->lat - 90;
160         /* printf("      +lat = %d  +y = %d\n", dist_lat, out->y); */
161         
162         out->lat = 90 - (dist_lat + 1);
163         out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
164         out->y = 7 - out->y;
165     }
166
167     if ( out->lat < -90 ) {
168         dist_lat = -90 - out->lat;
169         /* printf("      +lat = %d  +y = %d\n", dist_lat, out->y); */
170         
171         out->lat = -90 + (dist_lat - 1);
172         out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
173         out->y = 7 - out->y;
174     }
175 }
176
177
178 /* Given a lat/lon, find the "bucket" or tile that it falls within */
179 void fgBucketFind(double lon, double lat, struct fgBUCKET *p) {
180     double diff;
181
182     diff = lon - (double)(int)lon;
183     /* printf("diff = %.2f\n", diff); */
184     if ( (lon >= 0) || (fabs(diff) < FG_EPSILON) ) {
185         p->lon = (int)lon;
186     } else {
187         p->lon = (int)lon - 1;
188     }
189     /* printf("  p->lon = %d\n", p->lon); */
190
191     diff = lat - (double)(int)lat;
192     /* printf("diff = %.2f\n", diff); */
193     if ( (lat >= 0) || (fabs(diff) < FG_EPSILON) ) {
194         p->lat = (int)lat;
195     } else {
196         p->lat = (int)lat - 1;
197     }
198     /* printf("  p->lat = %d\n", p->lat); */
199
200     p->x = (lon - p->lon) * 8;
201     p->y = (lat - p->lat) * 8;
202     printf("Bucket = lon,lat = %d,%d  x,y index = %d,%d\n", 
203            p->lon, p->lat, p->x, p->y);
204 }
205
206
207 /* Given a lat/lon, fill in the local tile index array */
208 void fgBucketGenIdxArray(struct fgBUCKET *p1, struct fgBUCKET *tiles, 
209                          int width, int height) {
210     struct fgBUCKET *p2;
211     int dw, dh, i, j;
212
213     dh = height / 2;
214     dw = width / 2;
215     for ( j = 0; j < height; j++ ) {
216         for ( i = 0; i < width; i++ ) {
217             fgBucketOffset(p1, &tiles[(j*width)+i], i - dw, j - dh);
218             p2 = &tiles[(j*width)+i];
219             printf("  bucket = %d %d %d %d  index = %ld\n", 
220                    p2->lon, p2->lat, p2->x, p2->y, 
221                    fgBucketGenIndex(&tiles[(j*width)+i]));
222         }
223     }
224 }
225
226
227 /* sample main for testing
228 int main() {
229     struct fgBUCKET p1;
230     long int tile[49];
231     char path[256];
232     double lon, lat;
233     int i, j;
234
235     p1.lon = 180;
236     p1.lat = 90;
237     p1.x = 7;
238     p1.y = 7;
239
240     printf("Max index = %ld\n", gen_index(&p1));
241
242     lon = -50.0;
243     lat = -50.0;
244     find_bucket(lon, lat, &p1);
245     gen_idx_array(&p1, tile, 7, 7);
246     for ( j = 0; j < 7; j++ ) {
247         for ( i = 0; i < 7; i++ ) {
248             gen_path(tile[(j*7)+i], path);
249             printf("  path = %s\n", path);
250         }
251     }
252
253     lon = 50.0;
254     lat = 50.0;
255     find_bucket(lon, lat, &p1);
256     gen_idx_array(&p1, tile, 7, 7);
257     for ( j = 0; j < 7; j++ ) {
258         for ( i = 0; i < 7; i++ ) {
259             gen_path(tile[(j*7)+i], path);
260             printf("  path = %s\n", path);
261         }
262     }
263
264     return(1);
265 } */
266
267
268 /* $Log$
269 /* Revision 1.3  1998/01/27 00:48:01  curt
270 /* Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
271 /* system and commandline/config file processing code.
272 /*
273  * Revision 1.2  1998/01/24 00:03:28  curt
274  * Initial revision.
275  *
276  * Revision 1.1  1998/01/23 20:06:51  curt
277  * tileutils.* renamed to bucketutils.*
278  *
279  * Revision 1.6  1998/01/19 19:27:18  curt
280  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
281  * This should simplify things tremendously.
282  *
283  * Revision 1.5  1998/01/14 02:19:04  curt
284  * Makde offset_bucket visible to outside.
285  *
286  * Revision 1.4  1998/01/13 00:23:12  curt
287  * Initial changes to support loading and management of scenery tiles.  Note,
288  * there's still a fair amount of work left to be done.
289  *
290  * Revision 1.3  1998/01/10 00:01:47  curt
291  * Misc api changes and tweaks.
292  *
293  * Revision 1.2  1998/01/08 02:22:28  curt
294  * Continue working on basic features.
295  *
296  * Revision 1.1  1998/01/07 23:50:52  curt
297  * "area" renamed to "tile"
298  *
299  * Revision 1.1  1998/01/07 23:23:40  curt
300  * Initial revision.
301  *
302  * */
303
304