]> Pileus Git - grits/blob - src/plugins/elev.c
More reference counting updates
[grits] / src / plugins / elev.c
1 /*
2  * Copyright (C) 2009-2011 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:elev
20  * @short_description: Elevation plugin
21  *
22  * #GritsPluginElev provides access to ground elevation. It does this in two ways:
23  * First, it provides a height function used by the viewer when drawing the
24  * world. Second, it can load the elevation data into an image and draw a
25  * greyscale elevation overlay on the planets surface.
26  */
27
28 #include <time.h>
29 #include <glib/gstdio.h>
30
31 #include <grits.h>
32
33 #include "elev.h"
34
35 /* Configuration */
36 #define LOAD_BIL       TRUE
37 #define LOAD_TEX       FALSE
38
39 /* Tile size constnats */
40 #define MAX_RESOLUTION 50
41 #define TILE_WIDTH     1024
42 #define TILE_HEIGHT    512
43 #define TILE_CHANNELS  4
44 #define TILE_SIZE      (TILE_WIDTH*TILE_HEIGHT*sizeof(guint16))
45
46 struct _TileData {
47         /* OpenGL has to be first to make grits_tile_draw happy */
48         guint    tex;
49         guint16 *bil;
50 };
51
52 static gdouble _height_func(gdouble lat, gdouble lon, gpointer _elev)
53 {
54         GritsPluginElev *elev = _elev;
55         if (!elev) return 0;
56
57         GritsTile *tile = grits_tile_find(elev->tiles, lat, lon);
58         if (!tile) return 0;
59
60         struct _TileData *data = tile->data;
61         if (!data) return 0;
62
63         guint16 *bil  = data->bil;
64         if (!bil)  return 0;
65
66         gint w = TILE_WIDTH;
67         gint h = TILE_HEIGHT;
68
69         gdouble ymin  = tile->edge.s;
70         gdouble ymax  = tile->edge.n;
71         gdouble xmin  = tile->edge.w;
72         gdouble xmax  = tile->edge.e;
73
74         gdouble xdist = xmax - xmin;
75         gdouble ydist = ymax - ymin;
76
77         gdouble x =    (lon-xmin)/xdist  * w;
78         gdouble y = (1-(lat-ymin)/ydist) * h;
79
80         gdouble x_rem = x - (int)x;
81         gdouble y_rem = y - (int)y;
82         guint x_flr = (int)x;
83         guint y_flr = (int)y;
84
85         //if (lon == 180 || lon == -180)
86         //      g_message("lon=%f w=%d min=%f max=%f dist=%f x=%f rem=%f flr=%d",
87         //                 lon,   w,  xmin,  xmax,  xdist,   x, x_rem, x_flr);
88
89         /* TODO: Fix interpolation at edges:
90          *   - Pad these at the edges instead of wrapping/truncating
91          *   - Figure out which pixels to index (is 0,0 edge, center, etc) */
92         gint16 px00 = bil[MIN((y_flr  ),h-1)*w + MIN((x_flr  ),w-1)];
93         gint16 px10 = bil[MIN((y_flr  ),h-1)*w + MIN((x_flr+1),w-1)];
94         gint16 px01 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr  ),w-1)];
95         gint16 px11 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr+1),w-1)];
96
97         return px00 * (1-x_rem) * (1-y_rem) +
98                px10 * (  x_rem) * (1-y_rem) +
99                px01 * (1-x_rem) * (  y_rem) +
100                px11 * (  x_rem) * (  y_rem);
101 }
102
103 /**********************
104  * Loader and Freeers *
105  **********************/
106
107 struct _LoadTileData {
108         GritsPluginElev  *elev;
109         GritsTile        *tile;
110         guint8           *pixels;
111         struct _TileData *tdata;
112 };
113
114 static guint16 *_load_bil(gchar *path)
115 {
116         gsize len;
117         gchar *data = NULL;
118         g_file_get_contents(path, &data, &len, NULL);
119         g_debug("GritsPluginElev: load_bil %p", data);
120         if (len != TILE_SIZE) {
121                 g_warning("GritsPluginElev: _load_bil - unexpected tile size %ld, != %ld",
122                                 (glong)len, (glong)TILE_SIZE);
123                 g_free(data);
124                 return NULL;
125         }
126         return (guint16*)data;
127 }
128
129 static guchar *_load_pixels(guint16 *bil)
130 {
131         g_assert(TILE_CHANNELS == 4);
132
133         guchar (*pixels)[TILE_WIDTH][TILE_CHANNELS]
134                 = g_malloc0(TILE_HEIGHT * TILE_WIDTH * TILE_CHANNELS);
135
136         for (int r = 0; r < TILE_HEIGHT; r++) {
137                 for (int c = 0; c < TILE_WIDTH; c++) {
138                         gint16 value = bil[r*TILE_WIDTH + c];
139                         guchar color = (float)value/8848 * 255;
140                         //guchar color = (float)(MAX(value,0))/8848 * 255;
141                         pixels[r][c][0] = color;
142                         pixels[r][c][1] = color;
143                         pixels[r][c][2] = color;
144                         pixels[r][c][3] = 0xff;
145                 }
146         }
147
148         g_debug("GritsPluginElev: load_pixels %p", pixels);
149         return (guchar*)pixels;
150 }
151
152 static gboolean _load_tile_cb(gpointer _data)
153 {
154         struct _LoadTileData *data  = _data;
155         struct _TileData     *tdata = data->tdata;
156         g_debug("GritsPluginElev: _load_tile_cb start");
157         if (data->elev->aborted) {
158                 g_debug("GritsPluginElev: _load_tile - aborted");
159                 return FALSE;
160         }
161
162         /* Load OpenGL texture (from main thread) */
163         if (data->pixels) {
164                 glGenTextures(1, &tdata->tex);
165                 glBindTexture(GL_TEXTURE_2D, tdata->tex);
166
167                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
168                 glPixelStorei(GL_PACK_ALIGNMENT, 1);
169                 glTexImage2D(GL_TEXTURE_2D, 0, TILE_CHANNELS, TILE_WIDTH, TILE_HEIGHT, 0,
170                                 GL_RGBA, GL_UNSIGNED_BYTE, data->pixels);
171                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
172                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
173                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
174                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
175                 glFlush();
176         }
177
178         /* Set hight function (from main thread) */
179         if (tdata->bil) {
180                 grits_viewer_set_height_func(data->elev->viewer, &data->tile->edge,
181                                 _height_func, data->elev, TRUE);
182         }
183
184         /* Queue tiles for drawing */
185         data->tile->data = tdata;
186         gtk_widget_queue_draw(GTK_WIDGET(data->elev->viewer));
187
188         /* Cleanup */
189         g_free(data->pixels);
190         g_free(data);
191         return FALSE;
192 }
193
194 static void _load_tile(GritsTile *tile, gpointer _elev)
195 {
196         GritsPluginElev *elev = _elev;
197         guint16 *bil    = NULL;
198         guchar  *pixels = NULL;
199
200         g_debug("GritsPluginElev: _load_tile start %p", g_thread_self());
201         if (elev->aborted) {
202                 g_debug("GritsPluginElev: _load_tile - aborted");
203                 return;
204         }
205
206         /* Download tile */
207         gchar *path = grits_wms_fetch(elev->wms, tile, GRITS_ONCE, NULL, NULL);
208         if (!path) return;
209
210         /* Load bil */
211         bil = _load_bil(path);
212         g_free(path);
213         if (!bil) return;
214
215         /* Load pixels */
216         if (LOAD_TEX)
217                 pixels = _load_pixels(bil);
218         if (!LOAD_BIL)
219                 g_free(bil);
220
221         /* Copy pixbuf data for callback */
222         struct _LoadTileData *data  = g_new0(struct _LoadTileData, 1);
223         struct _TileData     *tdata = g_new0(struct _TileData,     1);
224         data->elev   = elev;
225         data->tile   = tile;
226         data->pixels = pixels;
227         data->tdata  = tdata;
228         tdata->tex   = 0;
229         tdata->bil   = bil;
230
231         /* Load the GL texture from the main thread */
232         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
233         g_debug("GritsPluginElev: _load_tile end %p", g_thread_self());
234 }
235
236 static gboolean _free_tile_cb(gpointer _data)
237 {
238         struct _TileData *data = _data;
239         if (LOAD_BIL)
240                 g_free(data->bil);
241         if (LOAD_TEX)
242                 glDeleteTextures(1, &data->tex);
243         g_free(data);
244         return FALSE;
245 }
246 static void _free_tile(GritsTile *tile, gpointer _elev)
247 {
248         g_debug("GritsPluginElev: _free_tile: %p", tile->data);
249         if (tile->data)
250                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
251 }
252
253 static void _update_tiles(gpointer _, gpointer _elev)
254 {
255         g_debug("GritsPluginElev: _update_tiles");
256         GritsPluginElev *elev = _elev;
257         GritsPoint eye;
258         grits_viewer_get_location(elev->viewer, &eye.lat, &eye.lon, &eye.elev);
259         grits_tile_update(elev->tiles, &eye,
260                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
261                         _load_tile, elev);
262         grits_tile_gc(elev->tiles, time(NULL)-10,
263                         _free_tile, elev);
264 }
265
266 /*************
267  * Callbacks *
268  *************/
269 static void _on_location_changed(GritsViewer *viewer,
270                 gdouble lat, gdouble lon, gdouble elevation, GritsPluginElev *elev)
271 {
272         g_thread_pool_push(elev->threads, NULL+1, NULL);
273 }
274
275 /***********
276  * Methods *
277  ***********/
278 /**
279  * grits_plugin_elev_new:
280  * @viewer: the #GritsViewer to use for drawing
281  *
282  * Create a new instance of the elevation plugin.
283  *
284  * Returns: the new #GritsPluginElev
285  */
286 GritsPluginElev *grits_plugin_elev_new(GritsViewer *viewer)
287 {
288         g_debug("GritsPluginElev: new");
289         GritsPluginElev *elev = g_object_new(GRITS_TYPE_PLUGIN_ELEV, NULL);
290         elev->viewer = g_object_ref(viewer);
291
292         /* Load initial tiles */
293         _update_tiles(NULL, elev);
294
295         /* Connect signals */
296         elev->sigid = g_signal_connect(elev->viewer, "location-changed",
297                         G_CALLBACK(_on_location_changed), elev);
298
299         /* Add renderers */
300         if (LOAD_TEX)
301                 grits_viewer_add(viewer, GRITS_OBJECT(elev->tiles), GRITS_LEVEL_WORLD, FALSE);
302
303         return elev;
304 }
305
306
307 /****************
308  * GObject code *
309  ****************/
310 /* Plugin init */
311 static void grits_plugin_elev_plugin_init(GritsPluginInterface *iface);
312 G_DEFINE_TYPE_WITH_CODE(GritsPluginElev, grits_plugin_elev, G_TYPE_OBJECT,
313                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
314                         grits_plugin_elev_plugin_init));
315 static void grits_plugin_elev_plugin_init(GritsPluginInterface *iface)
316 {
317         g_debug("GritsPluginElev: plugin_init");
318         /* Add methods to the interface */
319 }
320 /* Class/Object init */
321 static void grits_plugin_elev_init(GritsPluginElev *elev)
322 {
323         g_debug("GritsPluginElev: init");
324         /* Set defaults */
325         elev->threads = g_thread_pool_new(_update_tiles, elev, 1, FALSE, NULL);
326         elev->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
327         elev->wms   = grits_wms_new(
328                 "http://www.nasa.network.com/elev", "mergedSrtm", "application/bil",
329                 "srtm/", "bil", TILE_WIDTH, TILE_HEIGHT);
330         g_object_ref(elev->tiles);
331 }
332 static void grits_plugin_elev_dispose(GObject *gobject)
333 {
334         g_debug("GritsPluginElev: dispose");
335         GritsPluginElev *elev = GRITS_PLUGIN_ELEV(gobject);
336         elev->aborted = TRUE;
337         /* Drop references */
338         if (elev->viewer) {
339                 GritsViewer *viewer = elev->viewer;
340                 elev->viewer = NULL;
341                 g_signal_handler_disconnect(viewer, elev->sigid);
342                 if (LOAD_BIL)
343                         grits_viewer_clear_height_func(viewer);
344                 if (LOAD_TEX)
345                         grits_viewer_remove(viewer, GRITS_OBJECT(elev->tiles));
346                 g_object_unref(elev->tiles);
347                 soup_session_abort(elev->wms->http->soup);
348                 g_thread_pool_free(elev->threads, TRUE, TRUE);
349                 while (gtk_events_pending())
350                         gtk_main_iteration();
351                 g_object_unref(viewer);
352         }
353         G_OBJECT_CLASS(grits_plugin_elev_parent_class)->dispose(gobject);
354 }
355 static void grits_plugin_elev_finalize(GObject *gobject)
356 {
357         g_debug("GritsPluginElev: finalize");
358         GritsPluginElev *elev = GRITS_PLUGIN_ELEV(gobject);
359         /* Free data */
360         grits_wms_free(elev->wms);
361         grits_tile_free(elev->tiles, _free_tile, elev);
362         G_OBJECT_CLASS(grits_plugin_elev_parent_class)->finalize(gobject);
363
364 }
365 static void grits_plugin_elev_class_init(GritsPluginElevClass *klass)
366 {
367         g_debug("GritsPluginElev: class_init");
368         GObjectClass *gobject_class = (GObjectClass*)klass;
369         gobject_class->dispose  = grits_plugin_elev_dispose;
370         gobject_class->finalize = grits_plugin_elev_finalize;
371 }