]> Pileus Git - grits/blob - src/objects/gis-tile.c
24214ade125a4434a4871661879c6652c6a27cb0
[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-tile.h"
37
38 gchar *gis_tile_path_table[2][2] = {
39         {"00.", "01."},
40         {"10.", "11."},
41 };
42
43 /**
44  * gis_tile_new:
45  * @parent: the parent for the tile, or NULL
46  * @n:      the northern border of the tile
47  * @s:      the southern border of the tile
48  * @e:      the eastern border of the tile
49  * @w:      the western border of the tile
50  *
51  * Create a tile associated with a particular latitude/longitude box.
52  *
53  * Returns: the new #GisTile
54  */
55 GisTile *gis_tile_new(GisTile *parent,
56         gdouble n, gdouble s, gdouble e, gdouble w)
57 {
58         GisTile *tile = g_object_new(GIS_TYPE_TILE, NULL);
59         tile->parent = parent;
60         tile->atime  = time(NULL);
61         gis_bbox_set_bounds(&tile->edge, n, s, e, w);
62         return tile;
63 }
64
65 /**
66  * gis_tile_get_path:
67  * @child: the tile to generate a path for
68  *
69  * Generate a string representation of a tiles location in a group of nested
70  * tiles. The string returned consists of groups of two digits separated by a
71  * delimiter. Each group of digits the tiles location with respect to it's
72  * parent tile.
73  *
74  * Returns: the path representing the tiles's location
75  */
76 gchar *gis_tile_get_path(GisTile *child)
77 {
78         /* This could be easily cached if necessary */
79         int x, y;
80         GList *parts = NULL;
81         for (GisTile *parent = child->parent; parent; child = parent, parent = child->parent)
82                 gis_tile_foreach_index(child, x, y)
83                         if (parent->children[x][y] == child)
84                                 parts = g_list_prepend(parts, gis_tile_path_table[x][y]);
85         GString *path = g_string_new("");
86         for (GList *cur = parts; cur; cur = cur->next)
87                 g_string_append(path, cur->data);
88         g_list_free(parts);
89         return g_string_free(path, FALSE);
90 }
91
92 static gdouble _gis_tile_get_min_dist(GisPoint *eye, GisBBox *bounds)
93 {
94         GisPoint pos = {};
95         pos.lat = eye->lat > bounds->n ? bounds->n :
96                   eye->lat < bounds->s ? bounds->s : eye->lat;
97         pos.lon = eye->lon > bounds->e ? bounds->e :
98                   eye->lon < bounds->w ? bounds->w : eye->lon;
99         //if (eye->lat == pos.lat && eye->lon == pos.lon)
100         //      return elev; /* Shortcut? */
101         gdouble a[3], b[3];
102         lle2xyz(eye->lat, eye->lon, eye->elev, a+0, a+1, a+2);
103         lle2xyz(pos.lat,  pos.lon,  pos.elev,  b+0, b+1, b+2);
104         return distd(a, b);
105 }
106
107 static gboolean _gis_tile_precise(GisPoint *eye, GisBBox *bounds,
108                 gdouble max_res, gint width, gint height)
109 {
110         gdouble min_dist  = _gis_tile_get_min_dist(eye, bounds);
111         gdouble view_res  = MPPX(min_dist);
112
113         gdouble lat_point = bounds->n < 0 ? bounds->n :
114                             bounds->s > 0 ? bounds->s : 0;
115         gdouble lon_dist  = bounds->e - bounds->w;
116         gdouble tile_res  = ll2m(lon_dist, lat_point)/width;
117
118         /* This isn't really right, but it helps with memory since we don't (yet?) test if the tile
119          * would be drawn */
120         gdouble scale = eye->elev / min_dist;
121         view_res /= scale;
122         //view_res /= 1.4; /* make it a little nicer, not sure why this is needed */
123         //g_message("tile=(%7.2f %7.2f %7.2f %7.2f) "
124         //          "eye=(%9.1f %9.1f %9.1f) "
125         //          "elev=%9.1f / dist=%9.1f = %f",
126         //              tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w,
127         //              lat, lon, elev,
128         //              elev, min_dist, scale);
129
130         return tile_res < max_res ||
131                tile_res < view_res;
132 }
133
134 /**
135  * gis_tile_update:
136  * @root:      the root tile to split
137  * @eye:       the point the tile is viewed from, for calculating distances
138  * @res:       a maximum resolution in meters per pixel to split tiles to
139  * @width:     width in pixels of the image associated with the tile
140  * @height:    height in pixels of the image associated with the tile
141  * @load_func: function used to load the image when a new tile is created
142  * @user_data: user data to past to the load function
143  *
144  * Recursively split a tile into children of appropriate detail. The resolution
145  * of the tile in pixels per meter is compared to the resolution which the tile
146  * is being drawn at on the screen. If the screen resolution is insufficient
147  * the tile is recursively subdivided until a sufficient resolution is
148  * achieved.
149  */
150 void gis_tile_update(GisTile *root, GisPoint *eye,
151                 gdouble res, gint width, gint height,
152                 GisTileLoadFunc load_func, gpointer user_data)
153 {
154         root->atime = time(NULL);
155         //g_debug("GisTile: update - %p->atime = %u",
156         //              root, (guint)root->atime);
157         const gdouble rows = G_N_ELEMENTS(root->children);
158         const gdouble cols = G_N_ELEMENTS(root->children[0]);
159         const gdouble lat_dist = root->edge.n - root->edge.s;
160         const gdouble lon_dist = root->edge.e - root->edge.w;
161         const gdouble lat_step = lat_dist / rows;
162         const gdouble lon_step = lon_dist / cols;
163         int row, col;
164         gis_tile_foreach_index(root, row, col) {
165                 GisTile **child = &root->children[row][col];
166                 GisBBox edge;
167                 edge.n = root->edge.n-(lat_step*(row+0));
168                 edge.s = root->edge.n-(lat_step*(row+1));
169                 edge.e = root->edge.w+(lon_step*(col+1));
170                 edge.w = root->edge.w+(lon_step*(col+0));
171                 if (!_gis_tile_precise(eye, &edge, res,
172                                         width/cols, height/rows)) {
173                         if (!*child) {
174                                 *child = gis_tile_new(root, edge.n, edge.s,
175                                                 edge.e, edge.w);
176                                 load_func(*child, user_data);
177                         }
178                         gis_tile_update(*child, eye,
179                                         res, width, height,
180                                         load_func, user_data);
181                 }
182         }
183 }
184
185 /**
186  * gis_tile_find:
187  * @root: the root tile to search from
188  * @lat:  target latitude
189  * @lon:  target longitude
190  *
191  * Locate the subtile with the highest resolution which contains the given
192  * lat/lon point.
193  * 
194  * Returns: the child tile
195  */
196 GisTile *gis_tile_find(GisTile *root, gdouble lat, gdouble lon)
197 {
198         gint    rows = G_N_ELEMENTS(root->children);
199         gint    cols = G_N_ELEMENTS(root->children[0]);
200
201         gdouble lat_step = (root->edge.n - root->edge.s) / rows;
202         gdouble lon_step = (root->edge.e - root->edge.w) / cols;
203
204         gdouble lat_offset = root->edge.n - lat;;
205         gdouble lon_offset = lon - root->edge.w;
206
207         gint    row = lat_offset / lat_step;
208         gint    col = lon_offset / lon_step;
209
210         if (lon == 180) col--;
211         if (lat == -90) row--;
212
213         //if (lon == 180 || lon == -180)
214         //      g_message("lat=%f,lon=%f step=%f,%f off=%f,%f row=%d/%d,col=%d/%d",
215         //              lat,lon, lat_step,lon_step, lat_offset,lon_offset, row,rows,col,cols);
216
217         if (row < 0 || row >= rows || col < 0 || col >= cols)
218                 return NULL;
219         else if (root->children[row][col] && root->children[row][col]->data)
220                 return gis_tile_find(root->children[row][col], lat, lon);
221         else
222                 return root;
223 }
224
225 /**
226  * gis_tile_gc:
227  * @root:      the root tile to start garbage collection at
228  * @atime:     most recent time at which tiles will be kept
229  * @free_func: function used to free the image when a new tile is collected
230  * @user_data: user data to past to the free function
231  *
232  * Garbage collect old tiles. This removes and deallocate tiles that have not
233  * been used since before @atime.
234  *
235  * Returns: a pointer to the original tile, or NULL if it was garbage collected
236  */
237 GisTile *gis_tile_gc(GisTile *root, time_t atime,
238                 GisTileFreeFunc free_func, gpointer user_data)
239 {
240         if (!root)
241                 return NULL;
242         gboolean has_children = FALSE;
243         int x, y;
244         gis_tile_foreach_index(root, x, y) {
245                 root->children[x][y] = gis_tile_gc(
246                                 root->children[x][y], atime,
247                                 free_func, user_data);
248                 if (root->children[x][y])
249                         has_children = TRUE;
250         }
251         //g_debug("GisTile: gc - %p->atime=%u < atime=%u",
252         //              root, (guint)root->atime, (guint)atime);
253         if (!has_children && root->atime < atime && root->data) {
254                 free_func(root, user_data);
255                 g_object_unref(root);
256                 return NULL;
257         }
258         return root;
259 }
260
261 /* Use GObject for this */
262 /**
263  * gis_tile_free:
264  * @root:      the root tile to free
265  * @free_func: function used to free the image when a new tile is collected
266  * @user_data: user data to past to the free function
267  *
268  * Recursively free a tile and all it's children.
269  */
270 void gis_tile_free(GisTile *root, GisTileFreeFunc free_func, gpointer user_data)
271 {
272         if (!root)
273                 return;
274         GisTile *child;
275         gis_tile_foreach(root, child)
276                 gis_tile_free(child, free_func, user_data);
277         if (free_func)
278                 free_func(root, user_data);
279         g_object_unref(root);
280 }
281
282 /* GObject code */
283 G_DEFINE_TYPE(GisTile, gis_tile, GIS_TYPE_OBJECT);
284 static void gis_tile_init(GisTile *tile)
285 {
286 }
287
288 static void gis_tile_class_init(GisTileClass *klass)
289 {
290 }