]> Pileus Git - grits/blob - src/objects/gis-tile.c
fe3ff6ed8d3b2db94f0814620400b4baf949a62b
[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(GisTile *tile,
97                 gdouble lat, gdouble lon, gdouble elev)
98 {
99         gdouble tlat  = lat > tile->edge.n ? tile->edge.n :
100                         lat < tile->edge.s ? tile->edge.s : lat;
101         gdouble tlon  = lon > tile->edge.e ? tile->edge.e :
102                         lon < tile->edge.w ? tile->edge.w : lon;
103         gdouble telev = 0; // TODO: elevation at rlat,rlon
104         //if (lat == tlat && lon == tlon)
105         //      return elev; /* Shortcut? */
106         gdouble a[3], b[3];
107         lle2xyz( lat,  lon,  elev, a+0, a+1, a+2);
108         lle2xyz(tlat, tlon, telev, b+0, b+1, b+2);
109         return distd(a, b);
110 }
111
112 static gboolean _gis_tile_needs_split(GisTile *tile,
113                 gdouble max_res, gint width, gint height,
114                 gdouble lat, gdouble lon, gdouble elev)
115 {
116         gdouble lat_point = tile->edge.n < 0 ? tile->edge.n :
117                             tile->edge.s > 0 ? tile->edge.s : 0;
118         gdouble min_dist  = _gis_tile_get_min_dist(tile, lat, lon, elev);
119         gdouble view_res  = MPPX(min_dist);
120         gdouble lon_dist  = tile->edge.e - tile->edge.w;
121         gdouble tile_res  = ll2m(lon_dist, lat_point)/width;
122
123         /* This isn't really right, but it helps with memory since we don't (yet?) test if the tile
124          * would be drawn */
125         gdouble scale = elev / min_dist;
126         view_res /= scale;
127         //g_message("tile=(%7.2f %7.2f %7.2f %7.2f) "
128         //          "eye=(%9.1f %9.1f %9.1f) "
129         //          "elev=%9.1f / dist=%9.1f = %f",
130         //              tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w,
131         //              lat, lon, elev,
132         //              elev, min_dist, scale);
133
134         if (tile_res < max_res)
135                 return FALSE;
136         return view_res < tile_res;
137 }
138
139 /**
140  * gis_tile_update:
141  * @root:      the root tile to split
142  * @res:       a maximum resolution in meters per pixel to split tiles to
143  * @width:     width in pixels of the image associated with the tile
144  * @height:    height in pixels of the image associated with the tile
145  * @lat:       latitude of the eye point
146  * @lon:       longitude of the eye point
147  * @elev:      elevation of the eye point
148  * @load_func: function used to load the image when a new tile is created
149  * @user_data: user data to past to the load function
150  *
151  * Recursively split a tile into children of appropriate detail. The resolution
152  * of the tile in pixels per meter is compared to the resolution which the tile
153  * is being drawn at on the screen. If the screen resolution is insufficient
154  * the tile is recursively subdivided until a sufficient resolution is
155  * achieved.
156  */
157 void gis_tile_update(GisTile *root,
158                 gdouble res, gint width, gint height,
159                 gdouble lat, gdouble lon, gdouble elev,
160                 GisTileLoadFunc load_func, gpointer user_data)
161 {
162         root->atime = time(NULL);
163         //g_debug("GisTile: update - %p->atime = %u", root, (guint)root->atime);
164         gdouble lat_dist = root->edge.n - root->edge.s;
165         gdouble lon_dist = root->edge.e - root->edge.w;
166         if (_gis_tile_needs_split(root, res, width, height, lat, lon, elev)) {
167                 gdouble lat_step = lat_dist / G_N_ELEMENTS(root->children);
168                 gdouble lon_step = lon_dist / G_N_ELEMENTS(root->children[0]);
169                 int x, y;
170                 gis_tile_foreach_index(root, x, y) {
171                         if (!root->children[x][y]) {
172                                 root->children[x][y] = gis_tile_new(root,
173                                                 root->edge.n-(lat_step*(x+0)),
174                                                 root->edge.n-(lat_step*(x+1)),
175                                                 root->edge.w+(lon_step*(y+1)),
176                                                 root->edge.w+(lon_step*(y+0)));
177                                 load_func(root->children[x][y], user_data);
178                         }
179                         gis_tile_update(root->children[x][y],
180                                         res, width, height,
181                                         lat, lon, elev,
182                                         load_func, user_data);
183                 }
184         }
185 }
186
187 /**
188  * gis_tile_find:
189  * @root: the root tile to search from
190  * @lat:  target latitude
191  * @lon:  target longitude
192  *
193  * Locate the subtile with the highest resolution which contains the given
194  * lat/lon point.
195  * 
196  * Returns: the child tile
197  */
198 GisTile *gis_tile_find(GisTile *root, gdouble lat, gdouble lon)
199 {
200         gint    rows = G_N_ELEMENTS(root->children);
201         gint    cols = G_N_ELEMENTS(root->children[0]);
202
203         gdouble lat_step = (root->edge.n - root->edge.s) / rows;
204         gdouble lon_step = (root->edge.e - root->edge.w) / cols;
205
206         gdouble lat_offset = root->edge.n - lat;;
207         gdouble lon_offset = lon - root->edge.w;
208
209         gint    row = lat_offset / lat_step;
210         gint    col = lon_offset / lon_step;
211
212         if (lon == 180) col--;
213         if (lat == -90) row--;
214
215         //if (lon == 180 || lon == -180)
216         //      g_message("lat=%f,lon=%f step=%f,%f off=%f,%f row=%d/%d,col=%d/%d",
217         //              lat,lon, lat_step,lon_step, lat_offset,lon_offset, row,rows,col,cols);
218
219         if (row < 0 || row >= rows || col < 0 || col >= cols)
220                 return NULL;
221         else if (root->children[row][col] && root->children[row][col]->data)
222                 return gis_tile_find(root->children[row][col], lat, lon);
223         else
224                 return root;
225 }
226
227 /**
228  * gis_tile_gc:
229  * @root:      the root tile to start garbage collection at
230  * @atime:     most recent time at which tiles will be kept
231  * @free_func: function used to free the image when a new tile is collected
232  * @user_data: user data to past to the free function
233  *
234  * Garbage collect old tiles. This removes and deallocate tiles that have not
235  * been used since before @atime.
236  *
237  * Returns: a pointer to the original tile, or NULL if it was garbage collected
238  */
239 GisTile *gis_tile_gc(GisTile *root, time_t atime,
240                 GisTileFreeFunc free_func, gpointer user_data)
241 {
242         if (!root)
243                 return NULL;
244         gboolean has_children = FALSE;
245         int x, y;
246         gis_tile_foreach_index(root, x, y) {
247                 root->children[x][y] = gis_tile_gc(
248                                 root->children[x][y], atime,
249                                 free_func, user_data);
250                 if (root->children[x][y])
251                         has_children = TRUE;
252         }
253         //g_debug("GisTile: gc - %p->atime=%u < atime=%u",
254         //              root, (guint)root->atime, (guint)atime);
255         if (!has_children && root->atime < atime && root->data) {
256                 free_func(root, user_data);
257                 g_object_unref(root);
258                 return NULL;
259         }
260         return root;
261 }
262
263 /* Use GObject for this */
264 /**
265  * gis_tile_free:
266  * @root:      the root tile to free
267  * @free_func: function used to free the image when a new tile is collected
268  * @user_data: user data to past to the free function
269  *
270  * Recursively free a tile and all it's children.
271  */
272 void gis_tile_free(GisTile *root, GisTileFreeFunc free_func, gpointer user_data)
273 {
274         if (!root)
275                 return;
276         GisTile *child;
277         gis_tile_foreach(root, child)
278                 gis_tile_free(child, free_func, user_data);
279         if (free_func)
280                 free_func(root, user_data);
281         g_object_unref(root);
282 }
283
284 /* GObject code */
285 G_DEFINE_TYPE(GisTile, gis_tile, GIS_TYPE_OBJECT);
286 static void gis_tile_init(GisTile *tile)
287 {
288 }
289
290 static void gis_tile_class_init(GisTileClass *klass)
291 {
292 }