1 /**************************************************************************
2 * tileutils.c -- support routines to handle dynamic management of scenery tiles
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 "tileutils.h"
31 #include "../Include/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 static long gen_index(struct bucket *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 static void parse_index(long int index, struct bucket *p) {
60 index -= p->lon << 14;
74 /* Build a path name from an tile index */
75 void gen_path(long int index, char *path) {
77 int top_lon, top_lat, main_lon, main_lat;
80 parse_index(index, &p);
86 if ( (p.lon < 0) && (top_lon * 10 != p.lon) ) {
101 top_lat = p.lat / 10;
103 if ( (p.lat < 0) && (top_lat * 10 != p.lat) ) {
108 if ( top_lat >= 0 ) {
114 if ( main_lat < 0 ) {
118 sprintf(path, "%c%03d%c%03d/%c%03d%c%03d/%ld.ter",
119 hem, top_lon, pole, top_lat,
120 hem, main_lon, pole, main_lat,
125 /* offset an bucket struct by the specified amounts in the X & Y direction */
126 static void offset_bucket(struct bucket *in, struct bucket *out, int x, int y) {
137 /* printf(" reducing x (%d)\n", diff); */
140 } else if ( diff < -7 ) {
141 temp = (diff + 1) / 8 - 1;
145 out->x = ((diff % 8) + 8) % 8;
146 out->lon = ( (out->lon + 180 + 360 + temp) % 360 ) - 180;
150 /* printf(" reducing x (%d)\n", diff); */
153 } else if ( diff < -7 ) {
154 temp = (diff + 1) / 8 - 1;
158 out->y = ((diff % 8) + 8) % 8;
159 out->lat = out->lat + temp;
161 if ( out->lat >= 90 ) {
162 dist_lat = out->lat - 90;
163 /* printf(" +lat = %d +y = %d\n", dist_lat, out->y); */
165 out->lat = 90 - (dist_lat + 1);
166 out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
170 if ( out->lat < -90 ) {
171 dist_lat = -90 - out->lat;
172 /* printf(" +lat = %d +y = %d\n", dist_lat, out->y); */
174 out->lat = -90 + (dist_lat - 1);
175 out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
181 /* Given a lat/lon, find the "bucket" or tile that it falls within */
182 void find_bucket(double lon, double lat, struct bucket *p) {
185 diff = lon - (double)(int)lon;
186 /* printf("diff = %.2f\n", diff); */
187 if ( (lon >= 0) || (fabs(diff) < FG_EPSILON) ) {
190 p->lon = (int)lon - 1;
192 /* printf(" p->lon = %d\n", p->lon); */
194 diff = lat - (double)(int)lat;
195 /* printf("diff = %.2f\n", diff); */
196 if ( (lat >= 0) || (fabs(diff) < FG_EPSILON) ) {
199 p->lat = (int)lat - 1;
201 /* printf(" p->lat = %d\n", p->lat); */
203 p->x = (lon - p->lon) * 8;
204 p->y = (lat - p->lat) * 8;
205 printf("Bucket = lon,lat = %d,%d x,y index = %d,%d\n",
206 p->lon, p->lat, p->x, p->y);
210 /* Given a lat/lon, fill in the local tile index array */
211 void gen_idx_array(struct bucket *p1, long int *tile,
212 int width, int height) {
218 for ( j = 0; j < height; j++ ) {
219 for ( i = 0; i < width; i++ ) {
220 offset_bucket(p1, &p2, i - dw, j - dh);
221 tile[(j*width)+i] = gen_index(&p2);
222 printf(" bucket = %d %d %d %d index = %ld\n",
223 p2.lon, p2.lat, p2.x, p2.y, tile[(j*width)+i]);
229 /* sample main for testing
242 printf("Max index = %ld\n", gen_index(&p1));
246 find_bucket(lon, lat, &p1);
247 gen_idx_array(&p1, tile, 7, 7);
248 for ( j = 0; j < 7; j++ ) {
249 for ( i = 0; i < 7; i++ ) {
250 gen_path(tile[(j*7)+i], path);
251 printf(" path = %s\n", path);
257 find_bucket(lon, lat, &p1);
258 gen_idx_array(&p1, tile, 7, 7);
259 for ( j = 0; j < 7; j++ ) {
260 for ( i = 0; i < 7; i++ ) {
261 gen_path(tile[(j*7)+i], path);
262 printf(" path = %s\n", path);
271 /* Revision 1.1 1998/01/07 23:50:52 curt
272 /* "area" renamed to "tile"
274 * Revision 1.1 1998/01/07 23:23:40 curt