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