]> git.mxchange.org Git - simgear.git/blob - Bucket/bucketutils.c
0645fd69d3f6b540618c89d03d4b87741da88fd5
[simgear.git] / Bucket / 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 <Include/fg_constants.h>
31
32 #include "bucketutils.h"
33
34
35 /* Generate the unique scenery tile index containing the specified
36    lon/lat parameters.
37
38    The index is constructed as follows:
39
40    9 bits - to represent 360 degrees of longitude (-180 to 179)
41    8 bits - to represent 180 degrees of latitude (-90 to 89)
42
43    Each 1 degree by 1 degree tile is further broken down into an 8x8
44    grid.  So we also need:
45
46    3 bits - to represent x (0 to 7)
47    3 bits - to represent y (0 to 7) */
48 long int fgBucketGenIndex(fgBUCKET *p) {
49     long index = 0;
50
51     index = ((p->lon + 180) << 14) + ((p->lat + 90) << 6) + (p->y << 3) + p->x;
52     /* printf("  generated index = %ld\n", index); */
53
54     return(index);
55 }
56
57
58 /* Parse a unique scenery tile index and find the lon, lat, x, and y */
59 void fgBucketParseIndex(long int index, fgBUCKET *p) {
60     p->lon = index >> 14;
61     index -= p->lon << 14;
62     p->lon -= 180;
63
64     p->lat = index >> 6;
65     index -= p->lat << 6;
66     p->lat -= 90;
67
68     p->y = index >> 3;
69     index -= p->y << 3;
70
71     p->x = index;
72 }
73
74
75 /* Build a path name from an tile index */
76 void fgBucketGenBasePath(fgBUCKET *p, char *path) {
77     long int index;
78     int top_lon, top_lat, main_lon, main_lat;
79     char hem, pole;
80
81     index = fgBucketGenIndex(p);
82
83     path[0] = '\0';
84
85     top_lon = p->lon / 10;
86     main_lon = p->lon;
87     if ( (p->lon < 0) && (top_lon * 10 != p->lon) ) {
88         top_lon -= 1;
89     }
90     top_lon *= 10;
91     if ( top_lon >= 0 ) {
92         hem = 'e';
93     } else {
94         hem = 'w';
95         top_lon *= -1;
96     }
97     if ( main_lon < 0 ) {
98         main_lon *= -1;
99     }
100
101     top_lat = p->lat / 10;
102     main_lat = p->lat;
103     if ( (p->lat < 0) && (top_lat * 10 != p->lat) ) {
104         top_lat -= 1;
105     }
106     top_lat *= 10;
107     if ( top_lat >= 0 ) {
108         pole = 'n';
109     } else {
110         pole = 's';
111         top_lat *= -1;
112     }
113     if ( main_lat < 0 ) {
114         main_lat *= -1;
115     }
116
117     sprintf(path, "%c%03d%c%03d/%c%03d%c%03d", 
118             hem, top_lon, pole, top_lat,
119             hem, main_lon, pole, main_lat);
120 }
121
122
123 /* offset an bucket struct by the specified amounts in the X & Y direction */
124 void fgBucketOffset(fgBUCKET *in, fgBUCKET *out, int x, int y) {
125     int diff, temp;
126     int dist_lat;
127
128     out->lon = in->lon;
129     out->lat = in->lat;
130     out->x = in->x;
131     out->y = in->y;
132
133     /* do X direction */
134     diff = out->x + x;
135     /* printf("      reducing x (%d)\n", diff); */
136     if ( diff >= 0 ) {
137         temp = diff / 8;
138     } else if ( diff < -7 ) {
139         temp = (diff + 1) / 8 - 1;
140     } else {
141         temp = diff / 8 - 1;
142     }
143     out->x = ((diff % 8) + 8) % 8;
144     out->lon = ( (out->lon + 180 + 360 + temp) % 360 ) - 180;
145
146     /* do Y direction */
147     diff = out->y + y;
148     /* printf("      reducing x (%d)\n", diff); */
149     if ( diff >= 0 ) {
150         temp = diff / 8;
151     } else if ( diff < -7 ) {
152         temp = (diff + 1) / 8 - 1;
153     } else {
154         temp = diff / 8 - 1;
155     }
156     out->y = ((diff % 8) + 8) % 8;
157     out->lat = out->lat + temp;
158
159     if ( out->lat >= 90 ) {
160         dist_lat = out->lat - 90;
161         /* printf("      +lat = %d  +y = %d\n", dist_lat, out->y); */
162         
163         out->lat = 90 - (dist_lat + 1);
164         out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
165         out->y = 7 - out->y;
166     }
167
168     if ( out->lat < -90 ) {
169         dist_lat = -90 - out->lat;
170         /* printf("      +lat = %d  +y = %d\n", dist_lat, out->y); */
171         
172         out->lat = -90 + (dist_lat - 1);
173         out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
174         out->y = 7 - out->y;
175     }
176 }
177
178
179 /* Given a lat/lon in degrees, find the "bucket" or tile that it falls
180    within */
181 void fgBucketFind(double lon, double lat, fgBUCKET *p) {
182     double diff;
183
184     diff = lon - (double)(int)lon;
185     /* printf("diff = %.2f\n", diff); */
186     if ( (lon >= 0) || (fabs(diff) < FG_EPSILON) ) {
187         p->lon = (int)lon;
188     } else {
189         p->lon = (int)lon - 1;
190     }
191     /* printf("  p->lon = %d\n", p->lon); */
192
193     diff = lat - (double)(int)lat;
194     /* printf("diff = %.2f\n", diff); */
195     if ( (lat >= 0) || (fabs(diff) < FG_EPSILON) ) {
196         p->lat = (int)lat;
197     } else {
198         p->lat = (int)lat - 1;
199     }
200     /* printf("  p->lat = %d\n", p->lat); */
201
202     p->x = (int)((lon - p->lon) * 8);
203     p->y = (int)((lat - p->lat) * 8);
204     /* printf( "Bucket = lon,lat = %d,%d  x,y index = %d,%d\n", 
205             p->lon, p->lat, p->x, p->y); */
206 }
207
208
209 /* Given a lat/lon, fill in the local tile index array */
210 void fgBucketGenIdxArray(fgBUCKET *p1, fgBUCKET *tiles, int width, int height) {
211     fgBUCKET *p2;
212     int dw, dh, i, j;
213
214     dh = height / 2;
215     dw = width / 2;
216     for ( j = 0; j < height; j++ ) {
217         for ( i = 0; i < width; i++ ) {
218             fgBucketOffset(p1, &tiles[(j*width)+i], i - dw, j - dh);
219             p2 = &tiles[(j*width)+i];
220             /* printf( "  bucket = %d %d %d %d  index = %ld\n", 
221                     p2->lon, p2->lat, p2->x, p2->y, 
222                     fgBucketGenIndex(&tiles[(j*width)+i])); */
223         }
224     }
225 }
226
227
228 /* sample main for testing
229 int main() {
230     fgBUCKET p1;
231     long int tile[49];
232     char path[256];
233     double lon, lat;
234     int i, j;
235
236     p1.lon = 180;
237     p1.lat = 90;
238     p1.x = 7;
239     p1.y = 7;
240
241     printf("Max index = %ld\n", gen_index(&p1));
242
243     lon = -50.0;
244     lat = -50.0;
245     find_bucket(lon, lat, &p1);
246     gen_idx_array(&p1, tile, 7, 7);
247     for ( j = 0; j < 7; j++ ) {
248         for ( i = 0; i < 7; i++ ) {
249             gen_path(tile[(j*7)+i], path);
250             printf("  path = %s\n", path);
251         }
252     }
253
254     lon = 50.0;
255     lat = 50.0;
256     find_bucket(lon, lat, &p1);
257     gen_idx_array(&p1, tile, 7, 7);
258     for ( j = 0; j < 7; j++ ) {
259         for ( i = 0; i < 7; i++ ) {
260             gen_path(tile[(j*7)+i], path);
261             printf("  path = %s\n", path);
262         }
263     }
264
265     return(1);
266 } */
267
268
269 /* $Log$
270 /* Revision 1.3  1998/07/04 00:46:47  curt
271 /* typedef'd struct fgBUCKET.
272 /*
273  * Revision 1.2  1998/04/25 22:06:22  curt
274  * Edited cvs log messages in source files ... bad bad bad!
275  *
276  * Revision 1.1  1998/04/08 23:28:58  curt
277  * Adopted Gnu automake/autoconf system.
278  *
279  * Revision 1.6  1998/02/09 15:07:51  curt
280  * Minor tweaks.
281  *
282  * Revision 1.5  1998/01/29 00:51:38  curt
283  * First pass at tile cache, dynamic tile loading and tile unloading now works.
284  *
285  * Revision 1.4  1998/01/27 03:26:41  curt
286  * Playing with new fgPrintf command.
287  *
288  * Revision 1.3  1998/01/27 00:48:01  curt
289  * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
290  * system and commandline/config file processing code.
291  *
292  * Revision 1.2  1998/01/24 00:03:28  curt
293  * Initial revision.
294  *
295  * Revision 1.1  1998/01/23 20:06:51  curt
296  * tileutils.* renamed to bucketutils.*
297  *
298  * Revision 1.6  1998/01/19 19:27:18  curt
299  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
300  * This should simplify things tremendously.
301  *
302  * Revision 1.5  1998/01/14 02:19:04  curt
303  * Makde offset_bucket visible to outside.
304  *
305  * Revision 1.4  1998/01/13 00:23:12  curt
306  * Initial changes to support loading and management of scenery tiles.  Note,
307  * there's still a fair amount of work left to be done.
308  *
309  * Revision 1.3  1998/01/10 00:01:47  curt
310  * Misc api changes and tweaks.
311  *
312  * Revision 1.2  1998/01/08 02:22:28  curt
313  * Continue working on basic features.
314  *
315  * Revision 1.1  1998/01/07 23:50:52  curt
316  * "area" renamed to "tile"
317  *
318  * Revision 1.1  1998/01/07 23:23:40  curt
319  * Initial revision.
320  *
321  * */
322
323