1 // bucketutils.c -- support routines to handle fgBUCKET operations
3 // Written by Curtis Olson, started January 1998.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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)
28 #include <Include/fg_constants.h>
30 #include "bucketutils.h"
33 /* Generate the unique scenery tile index containing the specified
36 The index is constructed as follows:
38 9 bits - to represent 360 degrees of longitude (-180 to 179)
39 8 bits - to represent 180 degrees of latitude (-90 to 89)
41 Each 1 degree by 1 degree tile is further broken down into an 8x8
42 grid. So we also need:
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) {
49 index = ((p->lon + 180) << 14) + ((p->lat + 90) << 6) + (p->y << 3) + p->x;
50 // printf(" generated index = %ld\n", index);
56 // Parse a unique scenery tile index and find the lon, lat, x, and y
57 void fgBucketParseIndex(long int index, fgBUCKET *p) {
59 index -= p->lon << 14;
73 // Build a path name from an tile index
74 void fgBucketGenBasePath( const fgBUCKET *p, char *path) {
76 int top_lon, top_lat, main_lon, main_lat;
79 index = fgBucketGenIndex(p);
83 top_lon = p->lon / 10;
85 if ( (p->lon < 0) && (top_lon * 10 != p->lon) ) {
99 top_lat = p->lat / 10;
101 if ( (p->lat < 0) && (top_lat * 10 != p->lat) ) {
105 if ( top_lat >= 0 ) {
111 if ( main_lat < 0 ) {
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);
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) {
133 // printf(" reducing x (%d)\n", diff);
136 } else if ( diff < -7 ) {
137 temp = (diff + 1) / 8 - 1;
141 out->x = ((diff % 8) + 8) % 8;
142 out->lon = ( (out->lon + 180 + 360 + temp) % 360 ) - 180;
146 // printf(" reducing x (%d)\n", diff);
149 } else if ( diff < -7 ) {
150 temp = (diff + 1) / 8 - 1;
154 out->y = ((diff % 8) + 8) % 8;
155 out->lat = out->lat + temp;
157 if ( out->lat >= 90 ) {
158 dist_lat = out->lat - 90;
159 // printf(" +lat = %d +y = %d\n", dist_lat, out->y);
161 out->lat = 90 - (dist_lat + 1);
162 out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
166 if ( out->lat < -90 ) {
167 dist_lat = -90 - out->lat;
168 // printf(" +lat = %d +y = %d\n", dist_lat, out->y);
170 out->lat = -90 + (dist_lat - 1);
171 out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
177 // Given a lat/lon in degrees, find the "bucket" or tile that it falls
179 void fgBucketFind(double lon, double lat, 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 = (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);
207 // Given a lat/lon, fill in the local tile index array
208 void fgBucketGenIdxArray(fgBUCKET *p1, fgBUCKET *tiles, int width, int height) {
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])); */
226 /* sample main for testing
239 printf("Max index = %ld\n", gen_index(&p1));
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);
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);
268 // Revision 1.5 1998/12/09 18:48:08 curt
269 // Use C++ style comments.
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.
274 // Revision 1.3 1998/07/04 00:46:47 curt
275 // typedef'd struct fgBUCKET.
277 // Revision 1.2 1998/04/25 22:06:22 curt
278 // Edited cvs log messages in source files ... bad bad bad!
280 // Revision 1.1 1998/04/08 23:28:58 curt
281 // Adopted Gnu automake/autoconf system.
283 // Revision 1.6 1998/02/09 15:07:51 curt
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.
289 // Revision 1.4 1998/01/27 03:26:41 curt
290 // Playing with new fgPrintf command.
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.
296 // Revision 1.2 1998/01/24 00:03:28 curt
299 // Revision 1.1 1998/01/23 20:06:51 curt
300 // tileutils.* renamed to bucketutils.*
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.
306 // Revision 1.5 1998/01/14 02:19:04 curt
307 // Makde offset_bucket visible to outside.
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.
313 // Revision 1.3 1998/01/10 00:01:47 curt
314 // Misc api changes and tweaks.
316 // Revision 1.2 1998/01/08 02:22:28 curt
317 // Continue working on basic features.
319 // Revision 1.1 1998/01/07 23:50:52 curt
320 // "area" renamed to "tile"
322 // Revision 1.1 1998/01/07 23:23:40 curt