]> git.mxchange.org Git - flightgear.git/blob - Scenery/tileutils.c
Continue working on basic features.
[flightgear.git] / Scenery / tileutils.c
1 /**************************************************************************
2  * tileutils.c -- support routines to handle dynamic management of scenery tiles
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 "tileutils.h"
31 #include "../Include/constants.h"
32
33
34 /* Generate the unique scenery tile index containing the specified
35    lon/lat parameters.
36
37    The index is constructed as follows:
38
39    9 bits - to represent 360 degrees of longitude (-180 to 179)
40    8 bits - to represent 180 degrees of latitude (-90 to 89)
41
42    Each 1 degree by 1 degree tile is further broken down into an 8x8
43    grid.  So we also need:
44
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) {
48     long index = 0;
49
50     index = ((p->lon + 180) << 14) + ((p->lat + 90) << 6) + (p->y << 3) + p->x;
51     /* printf("  generated index = %ld\n", index); */
52
53     return(index);
54 }
55
56
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) {
59     p->lon = index >> 14;
60     index -= p->lon << 14;
61     p->lon -= 180;
62
63     p->lat = index >> 6;
64     index -= p->lat << 6;
65     p->lat -= 90;
66
67     p->y = index >> 3;
68     index -= p->y << 3;
69
70     p->x = index;
71 }
72
73
74 /* Build a path name from an tile index */
75 void gen_path(long int index, char *path) {
76     struct bucket p;
77     int top_lon, top_lat, main_lon, main_lat;
78     char hem, pole;
79
80     parse_index(index, &p);
81
82     path[0] = '\0';
83
84     top_lon = p.lon / 10;
85     main_lon = p.lon;
86     if ( (p.lon < 0) && (top_lon * 10 != p.lon) ) {
87         top_lon -= 1;
88         main_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         main_lat -= 1;
106     }
107     top_lat *= 10;
108     if ( top_lat >= 0 ) {
109         pole = 'n';
110     } else {
111         pole = 's';
112         top_lat *= -1;
113     }
114     if ( main_lat < 0 ) {
115         main_lat *= -1;
116     }
117
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,
121             index);
122 }
123
124
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) {
127     int diff, temp;
128     int dist_lat;
129
130     out->lon = in->lon;
131     out->lat = in->lat;
132     out->x = in->x;
133     out->y = in->y;
134
135     /* do X direction */
136     diff = out->x + x;
137     /* printf("      reducing x (%d)\n", diff); */
138     if ( diff >= 0 ) {
139         temp = diff / 8;
140     } else if ( diff < -7 ) {
141         temp = (diff + 1) / 8 - 1;
142     } else {
143         temp = diff / 8 - 1;
144     }
145     out->x = ((diff % 8) + 8) % 8;
146     out->lon = ( (out->lon + 180 + 360 + temp) % 360 ) - 180;
147
148     /* do Y direction */
149     diff = out->y + y;
150     /* printf("      reducing x (%d)\n", diff); */
151     if ( diff >= 0 ) {
152         temp = diff / 8;
153     } else if ( diff < -7 ) {
154         temp = (diff + 1) / 8 - 1;
155     } else {
156         temp = diff / 8 - 1;
157     }
158     out->y = ((diff % 8) + 8) % 8;
159     out->lat = out->lat + temp;
160
161     if ( out->lat >= 90 ) {
162         dist_lat = out->lat - 90;
163         /* printf("      +lat = %d  +y = %d\n", dist_lat, out->y); */
164         
165         out->lat = 90 - (dist_lat + 1);
166         out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
167         out->y = 7 - out->y;
168     }
169
170     if ( out->lat < -90 ) {
171         dist_lat = -90 - out->lat;
172         /* printf("      +lat = %d  +y = %d\n", dist_lat, out->y); */
173         
174         out->lat = -90 + (dist_lat - 1);
175         out->lon = ( (out->lon + 180 + 180) % 360 ) - 180;
176         out->y = 7 - out->y;
177     }
178 }
179
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) {
183     double diff;
184
185     diff = lon - (double)(int)lon;
186     /* printf("diff = %.2f\n", diff); */
187     if ( (lon >= 0) || (fabs(diff) < FG_EPSILON) ) {
188         p->lon = (int)lon;
189     } else {
190         p->lon = (int)lon - 1;
191     }
192     /* printf("  p->lon = %d\n", p->lon); */
193
194     diff = lat - (double)(int)lat;
195     /* printf("diff = %.2f\n", diff); */
196     if ( (lat >= 0) || (fabs(diff) < FG_EPSILON) ) {
197         p->lat = (int)lat;
198     } else {
199         p->lat = (int)lat - 1;
200     }
201     /* printf("  p->lat = %d\n", p->lat); */
202
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);
207 }
208
209
210 /* Given a lat/lon, fill in the local tile index array */
211 void gen_idx_array(struct bucket *p1, long int *tiles, 
212                           int width, int height) {
213     struct bucket p2;
214     int dw, dh, i, j;
215
216     dh = height / 2;
217     dw = width / 2;
218     for ( j = 0; j < height; j++ ) {
219         for ( i = 0; i < width; i++ ) {
220             offset_bucket(p1, &p2, i - dw, j - dh);
221             tiles[(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, tiles[(j*width)+i]);
224         }
225     }
226 }
227
228
229 /* sample main for testing
230 int main() {
231     struct bucket p1;
232     long int tile[49];
233     char path[256];
234     double lon, lat;
235     int i, j;
236
237     p1.lon = 180;
238     p1.lat = 90;
239     p1.x = 7;
240     p1.y = 7;
241
242     printf("Max index = %ld\n", gen_index(&p1));
243
244     lon = -50.0;
245     lat = -50.0;
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);
252         }
253     }
254
255     lon = 50.0;
256     lat = 50.0;
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);
263         }
264     }
265
266     return(1);
267 } */
268
269
270 /* $Log$
271 /* Revision 1.2  1998/01/08 02:22:28  curt
272 /* Continue working on basic features.
273 /*
274  * Revision 1.1  1998/01/07 23:50:52  curt
275  * "area" renamed to "tile"
276  *
277  * Revision 1.1  1998/01/07 23:23:40  curt
278  * Initial revision.
279  *
280  * */
281
282