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