]> Pileus Git - grits/blob - src/objects/gis-tile.c
Lazy tile splitting
[grits] / src / objects / gis-tile.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /**
19  * SECTION:gis-tile
20  * @short_description: Latitude/longitude overlays
21  *
22  * Each #GisTile corresponds to a latitude/longitude box on the surface of the
23  * earth. When drawn, the #GisTile renders an images associated with it to the
24  * surface of the earth. This is primarily used to draw ground overlays.
25  *
26  * Each GisTile can be split into subtiles in order to draw higher resolution
27  * overlays. Pointers to subtitles are stored in the parent tile and a parent
28  * pointer is stored in each child.
29  *
30  * Each #GisTile has a data filed which must be set by the user in order for
31  * the tile to be drawn. When used with GisOpenGL the data must be an integer
32  * representing the OpenGL texture to use when drawing the tile.
33  */
34
35 #include <config.h>
36 #include "gis-util.h"
37 #include "gis-tile.h"
38
39 gchar *gis_tile_path_table[2][2] = {
40         {"00.", "01."},
41         {"10.", "11."},
42 };
43
44 /**
45  * gis_tile_new:
46  * @parent: the parent for the tile, or NULL
47  * @n:      the northern border of the tile
48  * @s:      the southern border of the tile
49  * @e:      the eastern border of the tile
50  * @w:      the western border of the tile
51  *
52  * Create a tile associated with a particular latitude/longitude box.
53  *
54  * Returns: the new #GisTile
55  */
56 GisTile *gis_tile_new(GisTile *parent,
57         gdouble n, gdouble s, gdouble e, gdouble w)
58 {
59         GisTile *tile = g_object_new(GIS_TYPE_TILE, NULL);
60         tile->parent = parent;
61         tile->edge.n = n;
62         tile->edge.s = s;
63         tile->edge.e = e;
64         tile->edge.w = w;
65         tile->atime  = time(NULL);
66         return tile;
67 }
68
69 /**
70  * gis_tile_get_path:
71  * @child: the tile to generate a path for
72  *
73  * Generate a string representation of a tiles location in a group of nested
74  * tiles. The string returned consists of groups of two digits separated by a
75  * delimiter. Each group of digits the tiles location with respect to it's
76  * parent tile.
77  *
78  * Returns: the path representing the tiles's location
79  */
80 gchar *gis_tile_get_path(GisTile *child)
81 {
82         /* This could be easily cached if necessary */
83         int x, y;
84         GList *parts = NULL;
85         for (GisTile *parent = child->parent; parent; child = parent, parent = child->parent)
86                 gis_tile_foreach_index(child, x, y)
87                         if (parent->children[x][y] == child)
88                                 parts = g_list_prepend(parts, gis_tile_path_table[x][y]);
89         GString *path = g_string_new("");
90         for (GList *cur = parts; cur; cur = cur->next)
91                 g_string_append(path, cur->data);
92         g_list_free(parts);
93         return g_string_free(path, FALSE);
94 }
95
96 static gdouble _gis_tile_get_min_dist(
97                 gdouble lat, gdouble lon, gdouble elev,
98                 gdouble n, gdouble s, gdouble e, gdouble w)
99 {
100         gdouble tlat  = lat > n ? n :
101                         lat < s ? s : lat;
102         gdouble tlon  = lon > e ? e :
103                         lon < w ? w : lon;
104         gdouble telev = 0; // TODO: elevation at rlat,rlon
105         //if (lat == tlat && lon == tlon)
106         //      return elev; /* Shortcut? */
107         gdouble a[3], b[3];
108         lle2xyz( lat,  lon,  elev, a+0, a+1, a+2);
109         lle2xyz(tlat, tlon, telev, b+0, b+1, b+2);
110         return distd(a, b);
111 }
112
113 static gboolean _gis_tile_precise(
114                 gdouble lat, gdouble lon, gdouble elev,
115                 gdouble n, gdouble s, gdouble e, gdouble w,
116                 gdouble max_res, gint width, gint height)
117 {
118         gdouble min_dist  = _gis_tile_get_min_dist(lat, lon, elev, n, s, e, w);
119         gdouble view_res  = MPPX(min_dist);
120
121         gdouble lat_point = n < 0 ? n : s > 0 ? s : 0;
122         gdouble lon_dist  = e - w;
123         gdouble tile_res  = ll2m(lon_dist, lat_point)/width;
124
125         /* This isn't really right, but it helps with memory since we don't (yet?) test if the tile
126          * would be drawn */
127         gdouble scale = elev / min_dist;
128         view_res /= scale;
129         //view_res /= 1.4; /* make it a little nicer, not sure why this is needed */
130         //g_message("tile=(%7.2f %7.2f %7.2f %7.2f) "
131         //          "eye=(%9.1f %9.1f %9.1f) "
132         //          "elev=%9.1f / dist=%9.1f = %f",
133         //              tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w,
134         //              lat, lon, elev,
135         //              elev, min_dist, scale);
136
137         return tile_res < max_res ||
138                tile_res < view_res;
139 }
140
141 /**
142  * gis_tile_update:
143  * @root:      the root tile to split
144  * @res:       a maximum resolution in meters per pixel to split tiles to
145  * @width:     width in pixels of the image associated with the tile
146  * @height:    height in pixels of the image associated with the tile
147  * @lat:       latitude of the eye point
148  * @lon:       longitude of the eye point
149  * @elev:      elevation of the eye point
150  * @load_func: function used to load the image when a new tile is created
151  * @user_data: user data to past to the load function
152  *
153  * Recursively split a tile into children of appropriate detail. The resolution
154  * of the tile in pixels per meter is compared to the resolution which the tile
155  * is being drawn at on the screen. If the screen resolution is insufficient
156  * the tile is recursively subdivided until a sufficient resolution is
157  * achieved.
158  */
159 void gis_tile_update(GisTile *root,
160                 gdouble res, gint width, gint height,
161                 gdouble lat, gdouble lon, gdouble elev,
162                 GisTileLoadFunc load_func, gpointer user_data)
163 {
164         root->atime = time(NULL);
165         //g_debug("GisTile: update - %p->atime = %u",
166         //              root, (guint)root->atime);
167         const gdouble rows = G_N_ELEMENTS(root->children);
168         const gdouble cols = G_N_ELEMENTS(root->children[0]);
169         const gdouble lat_dist = root->edge.n - root->edge.s;
170         const gdouble lon_dist = root->edge.e - root->edge.w;
171         const gdouble lat_step = lat_dist / rows;
172         const gdouble lon_step = lon_dist / cols;
173         int row, col;
174         gis_tile_foreach_index(root, row, col) {
175                 GisTile **child = &root->children[row][col];
176                 const gdouble n = root->edge.n-(lat_step*(row+0));
177                 const gdouble s = root->edge.n-(lat_step*(row+1));
178                 const gdouble e = root->edge.w+(lon_step*(col+1));
179                 const gdouble w = root->edge.w+(lon_step*(col+0));
180                 if (!_gis_tile_precise(lat,lon,elev, n,s,e,w,
181                                         res,width/cols,height/rows)) {
182                         if (!*child) {
183                                 *child = gis_tile_new(root, n,s,e,w);
184                                 load_func(*child, user_data);
185                         }
186                         gis_tile_update(*child,
187                                         res, width, height,
188                                         lat, lon, elev,
189                                         load_func, user_data);
190                 }
191         }
192 }
193
194 /**
195  * gis_tile_find:
196  * @root: the root tile to search from
197  * @lat:  target latitude
198  * @lon:  target longitude
199  *
200  * Locate the subtile with the highest resolution which contains the given
201  * lat/lon point.
202  * 
203  * Returns: the child tile
204  */
205 GisTile *gis_tile_find(GisTile *root, gdouble lat, gdouble lon)
206 {
207         gint    rows = G_N_ELEMENTS(root->children);
208         gint    cols = G_N_ELEMENTS(root->children[0]);
209
210         gdouble lat_step = (root->edge.n - root->edge.s) / rows;
211         gdouble lon_step = (root->edge.e - root->edge.w) / cols;
212
213         gdouble lat_offset = root->edge.n - lat;;
214         gdouble lon_offset = lon - root->edge.w;
215
216         gint    row = lat_offset / lat_step;
217         gint    col = lon_offset / lon_step;
218
219         if (lon == 180) col--;
220         if (lat == -90) row--;
221
222         //if (lon == 180 || lon == -180)
223         //      g_message("lat=%f,lon=%f step=%f,%f off=%f,%f row=%d/%d,col=%d/%d",
224         //              lat,lon, lat_step,lon_step, lat_offset,lon_offset, row,rows,col,cols);
225
226         if (row < 0 || row >= rows || col < 0 || col >= cols)
227                 return NULL;
228         else if (root->children[row][col] && root->children[row][col]->data)
229                 return gis_tile_find(root->children[row][col], lat, lon);
230         else
231                 return root;
232 }
233
234 /**
235  * gis_tile_gc:
236  * @root:      the root tile to start garbage collection at
237  * @atime:     most recent time at which tiles will be kept
238  * @free_func: function used to free the image when a new tile is collected
239  * @user_data: user data to past to the free function
240  *
241  * Garbage collect old tiles. This removes and deallocate tiles that have not
242  * been used since before @atime.
243  *
244  * Returns: a pointer to the original tile, or NULL if it was garbage collected
245  */
246 GisTile *gis_tile_gc(GisTile *root, time_t atime,
247                 GisTileFreeFunc free_func, gpointer user_data)
248 {
249         if (!root)
250                 return NULL;
251         gboolean has_children = FALSE;
252         int x, y;
253         gis_tile_foreach_index(root, x, y) {
254                 root->children[x][y] = gis_tile_gc(
255                                 root->children[x][y], atime,
256                                 free_func, user_data);
257                 if (root->children[x][y])
258                         has_children = TRUE;
259         }
260         //g_debug("GisTile: gc - %p->atime=%u < atime=%u",
261         //              root, (guint)root->atime, (guint)atime);
262         if (!has_children && root->atime < atime && root->data) {
263                 free_func(root, user_data);
264                 g_object_unref(root);
265                 return NULL;
266         }
267         return root;
268 }
269
270 /* Use GObject for this */
271 /**
272  * gis_tile_free:
273  * @root:      the root tile to free
274  * @free_func: function used to free the image when a new tile is collected
275  * @user_data: user data to past to the free function
276  *
277  * Recursively free a tile and all it's children.
278  */
279 void gis_tile_free(GisTile *root, GisTileFreeFunc free_func, gpointer user_data)
280 {
281         if (!root)
282                 return;
283         GisTile *child;
284         gis_tile_foreach(root, child)
285                 gis_tile_free(child, free_func, user_data);
286         if (free_func)
287                 free_func(root, user_data);
288         g_object_unref(root);
289 }
290
291 /* GObject code */
292 G_DEFINE_TYPE(GisTile, gis_tile, GIS_TYPE_OBJECT);
293 static void gis_tile_init(GisTile *tile)
294 {
295 }
296
297 static void gis_tile_class_init(GisTileClass *klass)
298 {
299 }