]> Pileus Git - grits/blob - src/plugins/radar.c
Lots of work on libGIS
[grits] / 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         GisOpenGL *opengl = self->opengl;
76         gis_opengl_begin(opengl);
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         gis_opengl_redraw(opengl);
92         gis_opengl_end(opengl);
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("_gis_plugin_radar_expose: 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("_gis_plugin_radar_expose: 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_message("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                 g_warning("failed to decompress WSR88D data: %s",
385                                 error->message);
386                 g_error_free(error);
387         }
388         g_child_watch_add(pid, decompressed_cb, udata);
389         self->soup = NULL;
390 }
391
392
393 /*************
394  * Callbacks *
395  *************/
396 static void on_sweep_clicked(GtkRadioButton *button, gpointer _self)
397 {
398         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
399         load_colormap(self, g_object_get_data(G_OBJECT(button), "type" ));
400         load_sweep   (self, g_object_get_data(G_OBJECT(button), "sweep"));
401 }
402
403 static void on_time_changed(GisView *view, const char *time, gpointer _self)
404 {
405         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
406         g_debug("GisPluginRadar: on_time_changed - setting time=%s", time);
407         // format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323
408         char *site = gis_view_get_site(view);
409         char *path = g_strdup_printf("nexrd2/raw/%s/%s_%s", site, site, time);
410
411         /* Set up progress bar */
412         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
413         if (child) gtk_widget_destroy(child);
414
415         GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
416         gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
417         self->progress_bar   = gtk_progress_bar_new();
418         self->progress_label = gtk_label_new("Loading radar...");
419         gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar,   FALSE, FALSE, 0);
420         gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
421         gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
422         gtk_widget_show_all(self->config_body);
423
424         /* Clear radar */
425         if (self->cur_radar)
426                 RSL_free_radar(self->cur_radar);
427         self->cur_radar = NULL;
428         self->cur_sweep = NULL;
429         gis_opengl_redraw(self->opengl);
430
431         /* Start loading the new radar */
432         if (self->soup) {
433                 soup_session_abort(self->soup);
434                 self->soup = NULL;
435         }
436         gchar *base = gis_prefs_get_string(self->prefs, "aweather/nexrad_url");
437         if (gis_world_get_offline(self->world)) 
438                 self->soup = cache_file(base, path, GIS_ONCE,
439                                 cache_chunk_cb, cache_done_cb, self);
440         else 
441                 self->soup = cache_file(base, path, GIS_UPDATE,
442                                 cache_chunk_cb, cache_done_cb, self);
443         g_free(path);
444 }
445
446
447 /***********
448  * Methods *
449  ***********/
450 GisPluginRadar *gis_plugin_radar_new(GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs)
451 {
452         /* TODO: move to constructor if possible */
453         g_debug("GisPluginRadar: new");
454         GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
455         self->world  = world;
456         self->view   = view;
457         self->opengl = opengl;
458         self->prefs  = prefs;
459         self->time_changed_id = g_signal_connect(view, "time-changed",
460                         G_CALLBACK(on_time_changed), self);
461         return self;
462 }
463
464 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
465 {
466         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
467         return self->config_body;
468 }
469
470 static void gis_plugin_radar_expose(GisPlugin *_self)
471 {
472         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
473         g_debug("GisPluginRadar: expose");
474         if (self->cur_sweep == NULL)
475                 return;
476         Sweep *sweep = self->cur_sweep;
477
478 #ifdef MARCHING
479         /* Draw the surface */
480         glMatrixMode(GL_MODELVIEW);
481         glPushMatrix();
482         glDisable(GL_TEXTURE_2D);
483         float light_ambient[]  = {0.1f, 0.1f, 0.0f};
484         float light_diffuse[]  = {0.9f, 0.9f, 0.9f};
485         float light_position[] = {-300000.0f, 500000.0f, 400000.0f, 1.0f};
486         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
487         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
488         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
489         glEnable(GL_LIGHT0);
490         glEnable(GL_LIGHTING);
491         glEnable(GL_COLOR_MATERIAL);
492         glColor4f(1,1,1,0.75);
493         g_debug("ntri=%d", self->cur_num_triangles);
494         glBegin(GL_TRIANGLES);
495         for (int i = 0; i < self->cur_num_triangles; i++) {
496                 TRIANGLE t = self->cur_triangles[i];
497                 do_normal(t.p[0].x, t.p[0].y, t.p[0].z,
498                           t.p[1].x, t.p[1].y, t.p[1].z,
499                           t.p[2].x, t.p[2].y, t.p[2].z);
500                 glVertex3f(t.p[0].x, t.p[0].y, t.p[0].z);
501                 glVertex3f(t.p[1].x, t.p[1].y, t.p[1].z);
502                 glVertex3f(t.p[2].x, t.p[2].y, t.p[2].z);
503         }
504         glEnd();
505         glPopMatrix();
506 #endif
507
508         g_message("set camera");
509         Radar_header *h = &self->cur_radar->h;
510         gdouble lat  = (double)h->latd + (double)h->latm/60 + (double)h->lats/(60*60);
511         gdouble lon  = (double)h->lond + (double)h->lonm/60 + (double)h->lons/(60*60);
512         gdouble elev = h->height;
513         gis_opengl_center_position(self->opengl, lat, lon, elev);
514
515         glDisable(GL_ALPHA_TEST);
516         glDisable(GL_CULL_FACE);
517         glDisable(GL_DEPTH_TEST);
518         glDisable(GL_LIGHTING);
519         glEnable(GL_TEXTURE_2D);
520         glColor4f(1,1,1,1);
521
522         /* Draw the rays */
523         glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
524         glBegin(GL_QUAD_STRIP);
525         for (int ri = 0; ri <= sweep->h.nrays; ri++) {
526                 Ray  *ray = NULL;
527                 double angle = 0;
528                 if (ri < sweep->h.nrays) {
529                         ray = sweep->ray[ri];
530                         angle = deg2rad(ray->h.azimuth - ((double)ray->h.beam_width/2.));
531                 } else {
532                         /* Do the right side of the last sweep */
533                         ray = sweep->ray[ri-1];
534                         angle = deg2rad(ray->h.azimuth + ((double)ray->h.beam_width/2.));
535                 }
536
537                 double lx = sin(angle);
538                 double ly = cos(angle);
539
540                 double near_dist = ray->h.range_bin1;
541                 double far_dist  = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
542
543                 /* (find middle of bin) / scale for opengl */
544                 // near left
545                 glTexCoord2f(0.0, (double)ri/sweep->h.nrays-0.01);
546                 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
547
548                 // far  left
549                 // todo: correct range-height function
550                 double height = sin(deg2rad(ray->h.elev)) * far_dist;
551                 glTexCoord2f(1.0, (double)ri/sweep->h.nrays-0.01);
552                 glVertex3f(lx*far_dist,  ly*far_dist, height);
553         }
554         glEnd();
555         //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
556
557         /* Texture debug */
558         //glBegin(GL_QUADS);
559         //glTexCoord2d( 0.,  0.); glVertex3f(-500.,   0., 0.); // bot left
560         //glTexCoord2d( 0.,  1.); glVertex3f(-500., 500., 0.); // top left
561         //glTexCoord2d( 1.,  1.); glVertex3f( 0.,   500., 3.); // top right
562         //glTexCoord2d( 1.,  0.); glVertex3f( 0.,     0., 3.); // bot right
563         //glEnd();
564
565         /* Print the color table */
566         glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
567         glMatrixMode(GL_PROJECTION); glLoadIdentity();
568         glDisable(GL_TEXTURE_2D);
569         glEnable(GL_COLOR_MATERIAL);
570         glBegin(GL_QUADS);
571         int i;
572         for (i = 0; i < 256; i++) {
573                 glColor4ubv(self->cur_colormap->data[i]);
574                 glVertex3f(-1.0, (float)((i  ) - 256/2)/(256/2), 0.0); // bot left
575                 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
576                 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
577                 glVertex3f(-0.9, (float)((i  ) - 256/2)/(256/2), 0.0); // bot right
578         }
579         glEnd();
580 }
581
582
583 /****************
584  * GObject code *
585  ****************/
586 /* Plugin init */
587 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
588 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
589                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
590                         gis_plugin_radar_plugin_init));
591 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
592 {
593         g_debug("GisPluginRadar: plugin_init");
594         /* Add methods to the interface */
595         iface->expose     = gis_plugin_radar_expose;
596         iface->get_config = gis_plugin_radar_get_config;
597 }
598 /* Class/Object init */
599 static void gis_plugin_radar_init(GisPluginRadar *self)
600 {
601         g_debug("GisPluginRadar: class_init");
602         /* Set defaults */
603         self->soup          = NULL;
604         self->cur_triangles = NULL;
605         self->cur_num_triangles = 0;
606
607         self->config_body = gtk_alignment_new(0, 0, 1, 1);
608         gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
609         gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
610 }
611 static void gis_plugin_radar_dispose(GObject *gobject)
612 {
613         g_debug("GisPluginRadar: dispose");
614         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
615         g_signal_handler_disconnect(self->view, self->time_changed_id);
616         /* Drop references */
617         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
618 }
619 static void gis_plugin_radar_finalize(GObject *gobject)
620 {
621         g_debug("GisPluginRadar: finalize");
622         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
623         /* Free data */
624         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
625
626 }
627 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
628 {
629         g_debug("GisPluginRadar: class_init");
630         GObjectClass *gobject_class = (GObjectClass*)klass;
631         gobject_class->dispose  = gis_plugin_radar_dispose;
632         gobject_class->finalize = gis_plugin_radar_finalize;
633 }
634