1 /**************************************************************************
2 * bucketutils.c -- support routines to handle fgBUCKET operations
4 * Written by Curtis Olson, started January 1998.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
30 #include <Scenery/bucketutils.h>
31 #include <Include/fg_constants.h>
34 /* Generate the unique scenery tile index containing the specified
37 The index is constructed as follows:
39 9 bits - to represent 360 degrees of longitude (-180 to 179)
40 8 bits - to represent 180 degrees of latitude (-90 to 89)
42 Each 1 degree by 1 degree tile is further broken down into an 8x8
43 grid. So we also need:
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) {
50 index = ((p->lon + 180) << 14) + ((p->lat + 90) << 6) + (p->y << 3) + p->x;
51 /* printf(" generated index = %ld\n", index); */
57 /* Parse a unique scenery tile index and find the lon, lat, x, and y */
58 void fgBucketParseIndex(long int index, struct fgBUCKET *p) {
60 index -= p->lon << 14;
74 /* Build a path name from an tile index */
75 void fgBucketGenBasePath(struct fgBUCKET *p, char *path) {
77 int top_lon, top_lat, main_lon, main_lat;
80 index = fgBucketGenIndex(p);
84 top_lon = p->lon / 10;
86 if ( (p->lon < 0) && (top_lon * 10 != p->lon) ) {
100 top_lat = p->lat / 10;
102 if ( (p->lat < 0) && (top_lat * 10 != p->lat) ) {
106 if ( top_lat >= 0 ) {
112 if ( main_lat < 0 ) {
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);
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) {
134 /* printf(" reducing x (%d)\n", diff); */
137 } else if ( diff < -7 ) {
138 temp = (diff + 1) / 8 - 1;
142 out->x = ((diff % 8) + 8) % 8;
143 out->lon = ( (out->lon + 180 + 360 + temp) % 360 ) - 180;
147 /* printf(" reducing x (%d)\n", diff); */
150 } else if ( diff < -7 ) {
151 temp = (diff + 1) / 8 - 1;
155 out->y = ((diff % 8) + 8) % 8;
156 out->lat = out->lat + temp;
158 if ( out->lat >= 90 ) {
159 dist_lat = out->lat - 90;
160 /* printf(" +lat = %d +y = %d\n", dist_lat, out->y); */
162 out->lat = 90 - (dist_lat + 1);
163 out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
167 if ( out->lat < -90 ) {
168 dist_lat = -90 - out->lat;
169 /* printf(" +lat = %d +y = %d\n", dist_lat, out->y); */
171 out->lat = -90 + (dist_lat - 1);
172 out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
178 /* Given a lat/lon, find the "bucket" or tile that it falls within */
179 void fgBucketFind(double lon, double lat, struct fgBUCKET *p) {
182 diff = lon - (double)(int)lon;
183 /* printf("diff = %.2f\n", diff); */
184 if ( (lon >= 0) || (fabs(diff) < FG_EPSILON) ) {
187 p->lon = (int)lon - 1;
189 /* printf(" p->lon = %d\n", p->lon); */
191 diff = lat - (double)(int)lat;
192 /* printf("diff = %.2f\n", diff); */
193 if ( (lat >= 0) || (fabs(diff) < FG_EPSILON) ) {
196 p->lat = (int)lat - 1;
198 /* printf(" p->lat = %d\n", p->lat); */
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);
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) {
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]));
227 /* sample main for testing
240 printf("Max index = %ld\n", gen_index(&p1));
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);
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);
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.
273 * Revision 1.2 1998/01/24 00:03:28 curt
276 * Revision 1.1 1998/01/23 20:06:51 curt
277 * tileutils.* renamed to bucketutils.*
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.
283 * Revision 1.5 1998/01/14 02:19:04 curt
284 * Makde offset_bucket visible to outside.
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.
290 * Revision 1.3 1998/01/10 00:01:47 curt
291 * Misc api changes and tweaks.
293 * Revision 1.2 1998/01/08 02:22:28 curt
294 * Continue working on basic features.
296 * Revision 1.1 1998/01/07 23:50:52 curt
297 * "area" renamed to "tile"
299 * Revision 1.1 1998/01/07 23:23:40 curt