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