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