]> Pileus Git - aweather/blob - src/plugins/radar.c
Update for Gis{World,OpenGL,View,Viewer} refactoring
[aweather] / src / plugins / radar.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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 #include <config.h>
19 #include <gtk/gtk.h>
20 #include <gtk/gtkgl.h>
21 #include <gio/gio.h>
22 #include <GL/gl.h>
23 #include <math.h>
24 #include <rsl.h>
25
26 #include <gis/gis.h>
27
28 #include "radar.h"
29 #include "marching.h"
30
31
32 /**************************
33  * Data loading functions *
34  **************************/
35 /* Convert a sweep to an 2d array of data points */
36 static void _bscan_sweep(GisPluginRadar *self, Sweep *sweep, colormap_t *colormap,
37                 guint8 **data, int *width, int *height)
38 {
39         /* Calculate max number of bins */
40         int max_bins = 0;
41         for (int i = 0; i < sweep->h.nrays; i++)
42                 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
43
44         /* Allocate buffer using max number of bins for each ray */
45         guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
46
47         /* Fill the data */
48         for (int ri = 0; ri < sweep->h.nrays; ri++) {
49                 Ray *ray  = sweep->ray[ri];
50                 for (int bi = 0; bi < ray->h.nbins; bi++) {
51                         /* copy RGBA into buffer */
52                         //guint val   = dz_f(ray->range[bi]);
53                         guint8 val   = (guint8)ray->h.f(ray->range[bi]);
54                         guint  buf_i = (ri*max_bins+bi)*4;
55                         buf[buf_i+0] = colormap->data[val][0];
56                         buf[buf_i+1] = colormap->data[val][1];
57                         buf[buf_i+2] = colormap->data[val][2];
58                         buf[buf_i+3] = colormap->data[val][3]; // TESTING
59                         if (val == BADVAL     || val == RFVAL      || val == APFLAG ||
60                             val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO) {
61                                 buf[buf_i+3] = 0x00; // transparent
62                         }
63                 }
64         }
65
66         /* set output */
67         *width  = max_bins;
68         *height = sweep->h.nrays;
69         *data   = buf;
70 }
71
72 /* Load a sweep as the active texture */
73 static void _load_sweep(GisPluginRadar *self, Sweep *sweep)
74 {
75         GisViewer *viewer = self->viewer;
76         gis_viewer_begin(viewer);
77         self->cur_sweep = sweep;
78         int height, width;
79         guint8 *data;
80         _bscan_sweep(self, sweep, self->cur_colormap, &data, &width, &height);
81         glDeleteTextures(1, &self->cur_sweep_tex);
82         glGenTextures(1, &self->cur_sweep_tex);
83         glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
84         glPixelStorei(GL_PACK_ALIGNMENT, 1);
85         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
86         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
87         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
88         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
89                         GL_RGBA, GL_UNSIGNED_BYTE, data);
90         g_free(data);
91         gtk_widget_queue_draw(GTK_WIDGET(viewer));
92         gis_viewer_end(viewer);
93 }
94
95 static void _load_colormap(GisPluginRadar *self, gchar *table)
96 {
97         /* Set colormap so we can draw it on expose */
98         for (int i = 0; colormaps[i].name; i++)
99                 if (g_str_equal(colormaps[i].name, table))
100                         self->cur_colormap = &colormaps[i];
101 }
102
103 /* Add selectors to the config area for the sweeps */
104 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self);
105 static void _load_radar_gui(GisPluginRadar *self, Radar *radar)
106 {
107         /* Clear existing items */
108         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
109         if (child)
110                 gtk_widget_destroy(child);
111
112         gdouble elev;
113         guint rows = 1, cols = 1, cur_cols;
114         gchar row_label_str[64], col_label_str[64], button_str[64];
115         GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
116         GtkWidget *table = gtk_table_new(rows, cols, FALSE);
117
118         for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
119                 Volume *vol = radar->v[vi];
120                 if (vol == NULL) continue;
121                 rows++; cols = 1; elev = 0;
122
123                 /* Row label */
124                 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
125                 row_label = gtk_label_new(row_label_str);
126                 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
127                 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
128                 gtk_table_attach(GTK_TABLE(table), row_label,
129                                 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
130
131                 for (guint si = 0; si < vol->h.nsweeps; si++) {
132                         Sweep *sweep = vol->sweep[si];
133                         if (sweep == NULL || sweep->h.elev == 0) continue;
134                         if (sweep->h.elev != elev) {
135                                 cols++;
136                                 elev = sweep->h.elev;
137
138                                 /* Column label */
139                                 g_object_get(table, "n-columns", &cur_cols, NULL);
140                                 if (cols >  cur_cols) {
141                                         g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
142                                         col_label = gtk_label_new(col_label_str);
143                                         gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
144                                         gtk_widget_set_size_request(col_label, 50, -1);
145                                         gtk_table_attach(GTK_TABLE(table), col_label,
146                                                         cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
147                                 }
148
149                                 elev_box = gtk_hbox_new(TRUE, 0);
150                                 gtk_table_attach(GTK_TABLE(table), elev_box,
151                                                 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
152                         }
153
154
155                         /* Button */
156                         g_snprintf(button_str, 64, "%3.2f", elev);
157                         button = gtk_radio_button_new_with_label_from_widget(
158                                         GTK_RADIO_BUTTON(button), button_str);
159                         gtk_widget_set_size_request(button, -1, 26);
160                         //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
161                         //gtk_widget_set_size_request(button, -1, 22);
162                         g_object_set(button, "draw-indicator", FALSE, NULL);
163                         gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
164
165                         g_object_set_data(G_OBJECT(button), "type",  vol->h.type_str);
166                         g_object_set_data(G_OBJECT(button), "sweep", sweep);
167                         g_signal_connect(button, "clicked", G_CALLBACK(_on_sweep_clicked), self);
168                 }
169         }
170         gtk_container_add(GTK_CONTAINER(self->config_body), table);
171         gtk_widget_show_all(table);
172 }
173
174 static void _gis_plugin_radar_grid_set(GRIDCELL *grid, int gi, Ray *ray, int bi)
175 {
176         Range range = ray->range[bi];
177
178         double angle = deg2rad(ray->h.azimuth);
179         double tilt  = deg2rad(ray->h.elev);
180
181         double lx    = sin(angle);
182         double ly    = cos(angle);
183         double lz    = sin(tilt);
184
185         double dist   = bi*ray->h.gate_size + ray->h.range_bin1;
186
187         grid->p[gi].x = lx*dist;
188         grid->p[gi].y = ly*dist;
189         grid->p[gi].z = lz*dist;
190
191         guint8 val = (guint8)ray->h.f(ray->range[bi]);
192         if (val == BADVAL     || val == RFVAL      || val == APFLAG ||
193             val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO ||
194             val > 80)
195                 val = 0;
196         grid->val[gi] = (float)val;
197         //g_debug("(%.2f,%.2f,%.2f) - (%.0f,%.0f,%.0f) = %d",
198         //      angle, tilt, dist,
199         //      grid->p[gi].x,
200         //      grid->p[gi].y,
201         //      grid->p[gi].z,
202         //      val);
203 }
204
205 /* Load a radar from a decompressed file */
206 static void _load_radar(GisPluginRadar *self, gchar *radar_file)
207 {
208         char *dir  = g_path_get_dirname(radar_file);
209         char *site = g_path_get_basename(dir);
210         g_free(dir);
211         g_debug("GisPluginRadar: load_radar - Loading new radar");
212         RSL_read_these_sweeps("all", NULL);
213         Radar *radar = self->cur_radar = RSL_wsr88d_to_radar(radar_file, site);
214         if (radar == NULL) {
215                 g_warning("fail to load radar: path=%s, site=%s", radar_file, site);
216                 g_free(site);
217                 return;
218         }
219         g_free(site);
220
221 #ifdef MARCHING
222         /* Load the surface */
223         if (self->cur_triangles) {
224                 g_free(self->cur_triangles);
225                 self->cur_triangles = NULL;
226         }
227         self->cur_num_triangles = 0;
228         int x = 1;
229         for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
230                 if (radar->v[vi] == NULL) continue;
231
232                 for (guint si = 0; si+1 < radar->v[vi]->h.nsweeps; si++) {
233                         Sweep *sweep0 = radar->v[vi]->sweep[si+0];
234                         Sweep *sweep1 = radar->v[vi]->sweep[si+1];
235
236                         //g_debug("GisPluginRadar: load_radar: sweep[%3d-%3d] -- nrays = %d, %d",
237                         //      si, si+1,sweep0->h.nrays, sweep1->h.nrays);
238
239                         /* Skip super->regular resolution switch for now */
240                         if (sweep0 == NULL || sweep0->h.elev == 0 ||
241                             sweep1 == NULL || sweep1->h.elev == 0 ||
242                             sweep0->h.nrays != sweep1->h.nrays)
243                                 continue;
244
245                         /* We repack the arrays so that raysX[0] is always north, etc */
246                         Ray **rays0 = g_malloc0(sizeof(Ray*)*sweep0->h.nrays);
247                         Ray **rays1 = g_malloc0(sizeof(Ray*)*sweep1->h.nrays);
248
249                         for (guint ri = 0; ri < sweep0->h.nrays; ri++)
250                                 rays0[(guint)(sweep0->ray[ri]->h.azimuth * sweep0->h.nrays / 360)] =
251                                         sweep0->ray[ri];
252                         for (guint ri = 0; ri < sweep1->h.nrays; ri++)
253                                 rays1[(guint)(sweep1->ray[ri]->h.azimuth * sweep1->h.nrays / 360)] =
254                                         sweep1->ray[ri];
255
256                         for (guint ri = 0; ri+x < sweep0->h.nrays; ri+=x) {
257                                 //g_debug("GisPluginRadar: load_radar - ray[%3d-%3d] -- nbins = %d, %d, %d, %d",
258                                 //      ri, ri+x,
259                                 //      rays0[ri  ]->h.nbins,
260                                 //      rays0[ri+1]->h.nbins,
261                                 //      rays1[ri  ]->h.nbins,
262                                 //      rays1[ri+1]->h.nbins);
263
264                                 for (guint bi = 0; bi+x < rays1[ri]->h.nbins; bi+=x) {
265                                         GRIDCELL grid = {};
266                                         _gis_plugin_radar_grid_set(&grid, 7, rays0[(ri  )%sweep0->h.nrays], bi+x);
267                                         _gis_plugin_radar_grid_set(&grid, 6, rays0[(ri+x)%sweep0->h.nrays], bi+x);
268                                         _gis_plugin_radar_grid_set(&grid, 5, rays0[(ri+x)%sweep0->h.nrays], bi  );
269                                         _gis_plugin_radar_grid_set(&grid, 4, rays0[(ri  )%sweep0->h.nrays], bi  );
270                                         _gis_plugin_radar_grid_set(&grid, 3, rays1[(ri  )%sweep0->h.nrays], bi+x);
271                                         _gis_plugin_radar_grid_set(&grid, 2, rays1[(ri+x)%sweep0->h.nrays], bi+x);
272                                         _gis_plugin_radar_grid_set(&grid, 1, rays1[(ri+x)%sweep0->h.nrays], bi  );
273                                         _gis_plugin_radar_grid_set(&grid, 0, rays1[(ri  )%sweep0->h.nrays], bi  );
274                                         
275                                         TRIANGLE tris[10];
276                                         int n = march_one_cube(grid, 40, tris);
277
278                                         self->cur_triangles = g_realloc(self->cur_triangles,
279                                                 (self->cur_num_triangles+n)*sizeof(TRIANGLE));
280                                         for (int i = 0; i < n; i++) {
281                                                 //g_debug("triangle: ");
282                                                 //g_debug("\t(%f,%f,%f)", tris[i].p[0].x, tris[i].p[0].y, tris[i].p[0].z);
283                                                 //g_debug("\t(%f,%f,%f)", tris[i].p[1].x, tris[i].p[1].y, tris[i].p[1].z);
284                                                 //g_debug("\t(%f,%f,%f)", tris[i].p[2].x, tris[i].p[2].y, tris[i].p[2].z);
285                                                 self->cur_triangles[self->cur_num_triangles+i] = tris[i];
286                                         }
287                                         self->cur_num_triangles += n;
288                                         //g_debug(" ");
289                                 }
290                         }
291                 }
292                 break; // Exit after first volume (reflectivity)
293         }
294 #endif
295
296         /* Load the first sweep by default */
297         if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1) {
298                 g_warning("No sweeps found\n");
299         } else {
300                 /* load first available sweep */
301                 for (int vi = 0; vi < radar->h.nvolumes; vi++) {
302                         if (radar->v[vi]== NULL) continue;
303                         for (int si = 0; si < radar->v[vi]->h.nsweeps; si++) {
304                                 if (radar->v[vi]->sweep[si]== NULL) continue;
305                                 _load_colormap(self, radar->v[vi]->h.type_str);
306                                 _load_sweep(self, radar->v[vi]->sweep[si]);
307                                 break;
308                         }
309                         break;
310                 }
311         }
312
313         _load_radar_gui(self, radar);
314 }
315
316
317 /*****************
318  * ASync helpers *
319  *****************/
320 typedef struct {
321         GisPluginRadar *self;
322         gchar *radar_file;
323 } decompressed_t;
324
325 static void _decompressed_cb(GPid pid, gint status, gpointer _udata)
326 {
327         g_debug("GisPluginRadar: decompressed_cb");
328         decompressed_t *udata = _udata;
329         if (status != 0) {
330                 g_warning("wsr88ddec exited with status %d", status);
331                 return;
332         }
333         _load_radar(udata->self, udata->radar_file);
334         g_spawn_close_pid(pid);
335         g_free(udata->radar_file);
336         g_free(udata);
337 }
338
339 static void _cache_chunk_cb(char *path, goffset cur, goffset total, gpointer _self)
340 {
341         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
342         double percent = (double)cur/total;
343
344         //g_debug("GisPluginRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
345         //              cur, total, percent*100);
346
347         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
348
349         gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)",
350                         percent*100, (double)cur/1000000, (double)total/1000000);
351         gtk_label_set_text(GTK_LABEL(self->progress_label), msg);
352         g_free(msg);
353 }
354
355 static void _cache_done_cb(char *path, gboolean updated, gpointer _self)
356 {
357         g_debug("GisPluginRadar: cache_done_cb - updated = %d", updated);
358         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
359         char *decompressed = g_strconcat(path, ".raw", NULL);
360         if (!updated && g_file_test(decompressed, G_FILE_TEST_EXISTS)) {
361                 _load_radar(self, decompressed);
362                 return;
363         }
364
365         decompressed_t *udata = g_malloc(sizeof(decompressed_t));
366         udata->self       = self;
367         udata->radar_file = decompressed;
368         g_debug("GisPluginRadar: cache_done_cb - File updated, decompressing..");
369         char *argv[] = {"wsr88ddec", path, decompressed, NULL};
370         GPid pid;
371         GError *error = NULL;
372         g_spawn_async(
373                 NULL,    // const gchar *working_directory,
374                 argv,    // gchar **argv,
375                 NULL,    // gchar **envp,
376                 G_SPAWN_SEARCH_PATH|
377                 G_SPAWN_DO_NOT_REAP_CHILD,
378                          // GSpawnFlags flags,
379                 NULL,    // GSpawnChildSetupFunc child_setup,
380                 NULL,    // gpointer user_data,
381                 &pid,    // GPid *child_pid,
382                 &error); // GError **error
383         if (error) {
384                 gchar *message = g_strdup_printf("Unable to decompress WSR88D data: %s",
385                                 error->message);
386                 g_warning("%s", message);
387                 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
388                 if (child)
389                         gtk_widget_destroy(child);
390                 gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new(message));
391                 gtk_widget_show_all(self->config_body);
392                 g_error_free(error);
393                 g_free(message);
394         }
395         g_child_watch_add(pid, _decompressed_cb, udata);
396         self->soup = NULL;
397 }
398
399
400 /*************
401  * Callbacks *
402  *************/
403 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self)
404 {
405         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
406         _load_colormap(self, g_object_get_data(G_OBJECT(button), "type" ));
407         _load_sweep   (self, g_object_get_data(G_OBJECT(button), "sweep"));
408 }
409
410 static void _on_time_changed(GisViewer *viewer, const char *time, gpointer _self)
411 {
412         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
413         g_debug("GisPluginRadar: on_time_changed - setting time=%s", time);
414         // format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323
415         char *site = gis_viewer_get_site(viewer);
416         char *path = g_strdup_printf("nexrd2/raw/%s/%s_%s", site, site, time);
417
418         /* Set up progress bar */
419         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
420         if (child) gtk_widget_destroy(child);
421
422         GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
423         gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
424         self->progress_bar   = gtk_progress_bar_new();
425         self->progress_label = gtk_label_new("Loading radar...");
426         gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar,   FALSE, FALSE, 0);
427         gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
428         gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
429         gtk_widget_show_all(self->config_body);
430
431         /* Clear radar */
432         if (self->cur_radar)
433                 RSL_free_radar(self->cur_radar);
434         self->cur_radar = NULL;
435         self->cur_sweep = NULL;
436         gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
437
438         /* Start loading the new radar */
439         if (self->soup) {
440                 soup_session_abort(self->soup);
441                 self->soup = NULL;
442         }
443         gchar *base = gis_prefs_get_string(self->prefs, "aweather/nexrad_url", NULL);
444         if (gis_viewer_get_offline(self->viewer))
445                 self->soup = cache_file(base, path, GIS_ONCE,
446                                 _cache_chunk_cb, _cache_done_cb, self);
447         else
448                 self->soup = cache_file(base, path, GIS_UPDATE,
449                                 _cache_chunk_cb, _cache_done_cb, self);
450         g_free(path);
451 }
452
453
454 /***********
455  * Methods *
456  ***********/
457 GisPluginRadar *gis_plugin_radar_new(GisViewer *viewer, GisPrefs *prefs)
458 {
459         /* TODO: move to constructor if possible */
460         g_debug("GisPluginRadar: new");
461         GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
462         self->viewer = viewer;
463         self->prefs  = prefs;
464         self->time_changed_id = g_signal_connect(viewer, "time-changed",
465                         G_CALLBACK(_on_time_changed), self);
466         return self;
467 }
468
469 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
470 {
471         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
472         return self->config_body;
473 }
474
475 static void gis_plugin_radar_expose(GisPlugin *_self)
476 {
477         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
478         g_debug("GisPluginRadar: expose");
479         if (self->cur_sweep == NULL)
480                 return;
481         Sweep *sweep = self->cur_sweep;
482
483 #ifdef MARCHING
484         /* Draw the surface */
485         glMatrixMode(GL_MODELVIEW);
486         glPushMatrix();
487         glDisable(GL_TEXTURE_2D);
488         float light_ambient[]  = {0.1f, 0.1f, 0.0f};
489         float light_diffuse[]  = {0.9f, 0.9f, 0.9f};
490         float light_position[] = {-300000.0f, 500000.0f, 400000.0f, 1.0f};
491         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
492         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
493         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
494         glEnable(GL_LIGHT0);
495         glEnable(GL_LIGHTING);
496         glEnable(GL_COLOR_MATERIAL);
497         glColor4f(1,1,1,0.75);
498         g_debug("ntri=%d", self->cur_num_triangles);
499         glBegin(GL_TRIANGLES);
500         for (int i = 0; i < self->cur_num_triangles; i++) {
501                 TRIANGLE t = self->cur_triangles[i];
502                 do_normal(t.p[0].x, t.p[0].y, t.p[0].z,
503                           t.p[1].x, t.p[1].y, t.p[1].z,
504                           t.p[2].x, t.p[2].y, t.p[2].z);
505                 glVertex3f(t.p[0].x, t.p[0].y, t.p[0].z);
506                 glVertex3f(t.p[1].x, t.p[1].y, t.p[1].z);
507                 glVertex3f(t.p[2].x, t.p[2].y, t.p[2].z);
508         }
509         glEnd();
510         glPopMatrix();
511 #endif
512
513         g_debug("GisPluginRadar: expose - setting camera");
514         Radar_header *h = &self->cur_radar->h;
515         gdouble lat  = (double)h->latd + (double)h->latm/60 + (double)h->lats/(60*60);
516         gdouble lon  = (double)h->lond + (double)h->lonm/60 + (double)h->lons/(60*60);
517         gdouble elev = h->height;
518         gis_viewer_center_position(self->viewer, lat, lon, elev);
519
520         glDisable(GL_ALPHA_TEST);
521         glDisable(GL_CULL_FACE);
522         glDisable(GL_DEPTH_TEST);
523         glDisable(GL_LIGHTING);
524         glEnable(GL_TEXTURE_2D);
525         glColor4f(1,1,1,1);
526
527         /* Draw the rays */
528         glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
529         glBegin(GL_QUAD_STRIP);
530         for (int ri = 0; ri <= sweep->h.nrays; ri++) {
531                 Ray  *ray = NULL;
532                 double angle = 0;
533                 if (ri < sweep->h.nrays) {
534                         ray = sweep->ray[ri];
535                         angle = deg2rad(ray->h.azimuth - ((double)ray->h.beam_width/2.));
536                 } else {
537                         /* Do the right side of the last sweep */
538                         ray = sweep->ray[ri-1];
539                         angle = deg2rad(ray->h.azimuth + ((double)ray->h.beam_width/2.));
540                 }
541
542                 double lx = sin(angle);
543                 double ly = cos(angle);
544
545                 double near_dist = ray->h.range_bin1;
546                 double far_dist  = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
547
548                 /* (find middle of bin) / scale for opengl */
549                 // near left
550                 glTexCoord2f(0.0, (double)ri/sweep->h.nrays-0.01);
551                 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
552
553                 // far  left
554                 // todo: correct range-height function
555                 double height = sin(deg2rad(ray->h.elev)) * far_dist;
556                 glTexCoord2f(1.0, (double)ri/sweep->h.nrays-0.01);
557                 glVertex3f(lx*far_dist,  ly*far_dist, height);
558         }
559         glEnd();
560         //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
561
562         /* Texture debug */
563         //glBegin(GL_QUADS);
564         //glTexCoord2d( 0.,  0.); glVertex3f(-500.,   0., 0.); // bot left
565         //glTexCoord2d( 0.,  1.); glVertex3f(-500., 500., 0.); // top left
566         //glTexCoord2d( 1.,  1.); glVertex3f( 0.,   500., 3.); // top right
567         //glTexCoord2d( 1.,  0.); glVertex3f( 0.,     0., 3.); // bot right
568         //glEnd();
569
570         /* Print the color table */
571         glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
572         glMatrixMode(GL_PROJECTION); glLoadIdentity();
573         glDisable(GL_TEXTURE_2D);
574         glEnable(GL_COLOR_MATERIAL);
575         glBegin(GL_QUADS);
576         int i;
577         for (i = 0; i < 256; i++) {
578                 glColor4ubv(self->cur_colormap->data[i]);
579                 glVertex3f(-1.0, (float)((i  ) - 256/2)/(256/2), 0.0); // bot left
580                 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
581                 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
582                 glVertex3f(-0.9, (float)((i  ) - 256/2)/(256/2), 0.0); // bot right
583         }
584         glEnd();
585 }
586
587
588 /****************
589  * GObject code *
590  ****************/
591 /* Plugin init */
592 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
593 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
594                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
595                         gis_plugin_radar_plugin_init));
596 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
597 {
598         g_debug("GisPluginRadar: plugin_init");
599         /* Add methods to the interface */
600         iface->expose     = gis_plugin_radar_expose;
601         iface->get_config = gis_plugin_radar_get_config;
602 }
603 /* Class/Object init */
604 static void gis_plugin_radar_init(GisPluginRadar *self)
605 {
606         g_debug("GisPluginRadar: class_init");
607         /* Set defaults */
608         self->soup          = NULL;
609         self->cur_triangles = NULL;
610         self->cur_num_triangles = 0;
611
612         self->config_body = gtk_alignment_new(0, 0, 1, 1);
613         gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
614         gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
615 }
616 static void gis_plugin_radar_dispose(GObject *gobject)
617 {
618         g_debug("GisPluginRadar: dispose");
619         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
620         g_signal_handler_disconnect(self->viewer, self->time_changed_id);
621         /* Drop references */
622         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
623 }
624 static void gis_plugin_radar_finalize(GObject *gobject)
625 {
626         g_debug("GisPluginRadar: finalize");
627         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
628         /* Free data */
629         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
630
631 }
632 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
633 {
634         g_debug("GisPluginRadar: class_init");
635         GObjectClass *gobject_class = (GObjectClass*)klass;
636         gobject_class->dispose  = gis_plugin_radar_dispose;
637         gobject_class->finalize = gis_plugin_radar_finalize;
638 }
639