]> Pileus Git - aweather/blobdiff - src/plugins/level2.c
Add units to isosurface slider
[aweather] / src / plugins / level2.c
index 14f9e34bdfd260b8a0941523343b969d52b6c3d4..2f595c9bd26f3ffbd7ebdd6b7189f8b9a3f409ef 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 #include <config.h>
 #include <math.h>
-#include <GL/gl.h>
 #include <glib/gstdio.h>
 #include <grits.h>
 #include <rsl.h>
 
 #include "level2.h"
 
+#include "../compat.h"
+
 #define ISO_MIN 30
 #define ISO_MAX 80
 
@@ -74,20 +75,20 @@ static void _bscan_sweep(Sweep *sweep, AWeatherColormap *colormap,
 }
 
 /* Load a sweep into an OpenGL texture */
-static void _load_sweep_gl(AWeatherLevel2 *self)
+static void _load_sweep_gl(AWeatherLevel2 *level2)
 {
        g_debug("AWeatherLevel2: _load_sweep_gl");
        guint8 *data;
        gint width, height;
-       _bscan_sweep(self->sweep, self->sweep_colors, &data, &width, &height);
+       _bscan_sweep(level2->sweep, level2->sweep_colors, &data, &width, &height);
        gint tex_width  = pow(2, ceil(log(width )/log(2)));
        gint tex_height = pow(2, ceil(log(height)/log(2)));
-       self->sweep_coords[0] = (double)width  / tex_width;
-       self->sweep_coords[1] = (double)height / tex_height;
+       level2->sweep_coords[0] = (double)width  / tex_width;
+       level2->sweep_coords[1] = (double)height / tex_height;
 
-       if (!self->sweep_tex)
-                glGenTextures(1, &self->sweep_tex);
-       glBindTexture(GL_TEXTURE_2D, self->sweep_tex);
+       if (!level2->sweep_tex)
+                glGenTextures(1, &level2->sweep_tex);
+       glBindTexture(GL_TEXTURE_2D, level2->sweep_tex);
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tex_width, tex_height, 0,
@@ -203,15 +204,15 @@ static VolGrid *_load_grid(Volume *vol)
 /*********************
  * Drawing functions *
  *********************/
-void aweather_level2_draw(GritsObject *_self, GritsOpenGL *opengl)
+void aweather_level2_draw(GritsObject *_level2, GritsOpenGL *opengl)
 {
-       AWeatherLevel2 *self = AWEATHER_LEVEL2(_self);
-       if (!self->sweep || !self->sweep_tex)
+       AWeatherLevel2 *level2 = AWEATHER_LEVEL2(_level2);
+       if (!level2->sweep || !level2->sweep_tex)
                return;
 
        /* Draw wsr88d */
-       Sweep *sweep = self->sweep;
-       glDisable(GL_ALPHA_TEST);
+       Sweep *sweep = level2->sweep;
+       //glDisable(GL_ALPHA_TEST);
        glDisable(GL_CULL_FACE);
        glDisable(GL_LIGHTING);
        glEnable(GL_TEXTURE_2D);
@@ -220,9 +221,9 @@ void aweather_level2_draw(GritsObject *_self, GritsOpenGL *opengl)
        glColor4f(1,1,1,1);
 
        /* Draw the rays */
-       gdouble xscale = self->sweep_coords[0];
-       gdouble yscale = self->sweep_coords[1];
-       glBindTexture(GL_TEXTURE_2D, self->sweep_tex);
+       gdouble xscale = level2->sweep_coords[0];
+       gdouble yscale = level2->sweep_coords[1];
+       glBindTexture(GL_TEXTURE_2D, level2->sweep_tex);
        glBegin(GL_TRIANGLE_STRIP);
        for (int ri = 0; ri <= sweep->h.nrays; ri++) {
                Ray  *ray = NULL;
@@ -265,43 +266,50 @@ void aweather_level2_draw(GritsObject *_self, GritsOpenGL *opengl)
        //glEnd();
 }
 
+void aweather_level2_hide(GritsObject *_level2, gboolean hidden)
+{
+       AWeatherLevel2 *level2 = AWEATHER_LEVEL2(_level2);
+       if (level2->volume)
+               grits_object_hide(GRITS_OBJECT(level2->volume), hidden);
+}
+
 
 /***********
  * Methods *
  ***********/
-static gboolean _set_sweep_cb(gpointer _self)
+static gboolean _set_sweep_cb(gpointer _level2)
 {
        g_debug("AWeatherLevel2: _set_sweep_cb");
-       AWeatherLevel2 *self = _self;
-       _load_sweep_gl(self);
-       grits_object_queue_draw(_self);
-       g_object_unref(self);
+       AWeatherLevel2 *level2 = _level2;
+       _load_sweep_gl(level2);
+       grits_object_queue_draw(_level2);
+       g_object_unref(level2);
        return FALSE;
 }
-void aweather_level2_set_sweep(AWeatherLevel2 *self,
+void aweather_level2_set_sweep(AWeatherLevel2 *level2,
                int type, float elev)
 {
        g_debug("AWeatherLevel2: set_sweep - %d %f", type, elev);
 
        /* Find sweep */
-       Volume *volume = RSL_get_volume(self->radar, type);
+       Volume *volume = RSL_get_volume(level2->radar, type);
        if (!volume) return;
-       self->sweep = RSL_get_closest_sweep(volume, elev, 90);
-       if (!self->sweep) return;
+       level2->sweep = RSL_get_closest_sweep(volume, elev, 90);
+       if (!level2->sweep) return;
 
        /* Find colormap */
-       self->sweep_colors = NULL;
-       for (int i = 0; self->colormap[i].file; i++)
-               if (self->colormap[i].type == type)
-                       self->sweep_colors = &self->colormap[i];
-       if (!self->sweep_colors) {
+       level2->sweep_colors = NULL;
+       for (int i = 0; level2->colormap[i].file; i++)
+               if (level2->colormap[i].type == type)
+                       level2->sweep_colors = &level2->colormap[i];
+       if (!level2->sweep_colors) {
                g_warning("AWeatherLevel2: set_sweep - missing colormap[%d]", type);
-               self->sweep_colors = &self->colormap[0];
+               level2->sweep_colors = &level2->colormap[0];
        }
 
        /* Load data */
-       g_object_ref(self);
-       g_idle_add(_set_sweep_cb, self);
+       g_object_ref(level2);
+       g_idle_add(_set_sweep_cb, level2);
 }
 
 void aweather_level2_set_iso(AWeatherLevel2 *level2, gfloat level)
@@ -317,7 +325,7 @@ void aweather_level2_set_iso(AWeatherLevel2 *level2, gfloat level)
                vol->disp = GRITS_VOLUME_SURFACE;
                GRITS_OBJECT(vol)->center = GRITS_OBJECT(level2)->center;
                grits_viewer_add(GRITS_OBJECT(level2)->viewer,
-                               GRITS_OBJECT(vol), GRITS_LEVEL_WORLD, TRUE);
+                               GRITS_OBJECT(vol), GRITS_LEVEL_WORLD+5, TRUE);
                level2->volume = vol;
        }
        if (ISO_MIN < level && level < ISO_MAX) {
@@ -327,9 +335,9 @@ void aweather_level2_set_iso(AWeatherLevel2 *level2, gfloat level)
                level2->volume->color[2] = data[2];
                level2->volume->color[3] = data[3];
                grits_volume_set_level(level2->volume, level);
-               GRITS_OBJECT(level2->volume)->hidden = FALSE;
+               grits_object_hide(GRITS_OBJECT(level2->volume), FALSE);
        } else {
-               GRITS_OBJECT(level2->volume)->hidden = TRUE;
+               grits_object_hide(GRITS_OBJECT(level2->volume), TRUE);
        }
 }
 
@@ -337,18 +345,18 @@ AWeatherLevel2 *aweather_level2_new(Radar *radar, AWeatherColormap *colormap)
 {
        g_debug("AWeatherLevel2: new - %s", radar->h.radar_name);
        RSL_sort_radar(radar);
-       AWeatherLevel2 *self = g_object_new(AWEATHER_TYPE_LEVEL2, NULL);
-       self->radar    = radar;
-       self->colormap = colormap;
-       aweather_level2_set_sweep(self, DZ_INDEX, 0);
+       AWeatherLevel2 *level2 = g_object_new(AWEATHER_TYPE_LEVEL2, NULL);
+       level2->radar    = radar;
+       level2->colormap = colormap;
+       aweather_level2_set_sweep(level2, DZ_INDEX, 0);
 
        GritsPoint center;
        Radar_header *h = &radar->h;
        center.lat  = (double)h->latd + (double)h->latm/60 + (double)h->lats/(60*60);
        center.lon  = (double)h->lond + (double)h->lonm/60 + (double)h->lons/(60*60);
        center.elev = h->height;
-       GRITS_OBJECT(self)->center = center;
-       return self;
+       GRITS_OBJECT(level2)->center = center;
+       return level2;
 }
 
 AWeatherLevel2 *aweather_level2_new_from_file(const gchar *file, const gchar *site,
@@ -372,9 +380,9 @@ AWeatherLevel2 *aweather_level2_new_from_file(const gchar *file, const gchar *si
 
        /* Load the radar file */
        RSL_read_these_sweeps("all", NULL);
-       g_message("read start");
+       g_debug("AWeatherLevel2: rsl read start");
        Radar *radar = RSL_wsr88d_to_radar(raw, (gchar*)site);
-       g_message("read done");
+       g_debug("AWeatherLevel2: rsl read done");
        g_free(raw);
        if (!radar)
                return NULL;
@@ -386,10 +394,10 @@ static void _on_sweep_clicked(GtkRadioButton *button, gpointer _level2)
 {
        AWeatherLevel2 *level2 = _level2;
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
-               gint type = (gint)g_object_get_data(G_OBJECT(button), "type");
-               gint elev = (gint)g_object_get_data(G_OBJECT(button), "elev");
+               gint type = (glong)g_object_get_data(G_OBJECT(button), "type");
+               gint elev = (glong)g_object_get_data(G_OBJECT(button), "elev");
                aweather_level2_set_sweep(level2, type, (float)elev/100);
-               //self->colormap = level2->sweep_colors;
+               //level2->colormap = level2->sweep_colors;
        }
 }
 
@@ -400,6 +408,11 @@ static void _on_iso_changed(GtkRange *range, gpointer _level2)
        aweather_level2_set_iso(level2, level);
 }
 
+static gchar *_on_format_value(GtkScale *scale, gdouble value, gpointer _level2)
+{
+       return g_strdup_printf("%.1lf dBZ ", value);
+}
+
 GtkWidget *aweather_level2_get_config(AWeatherLevel2 *level2)
 {
        Radar *radar = level2->radar;
@@ -453,7 +466,8 @@ GtkWidget *aweather_level2_get_config(AWeatherLevel2 *level2)
                                                        cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
                                }
 
-                               elev_box = gtk_hbox_new(TRUE, 0);
+                               elev_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
+                               gtk_box_set_homogeneous(GTK_BOX(elev_box), TRUE);
                                gtk_table_attach(GTK_TABLE(table), elev_box,
                                                cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
                        }
@@ -469,9 +483,9 @@ GtkWidget *aweather_level2_get_config(AWeatherLevel2 *level2)
                        g_object_set(button, "draw-indicator", FALSE, NULL);
                        gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
 
-                       g_object_set_data(G_OBJECT(button), "level2", (gpointer)level2);
-                       g_object_set_data(G_OBJECT(button), "type",   (gpointer)vi);
-                       g_object_set_data(G_OBJECT(button), "elev",   (gpointer)(int)(elev*100));
+                       g_object_set_data(G_OBJECT(button), "level2", level2);
+                       g_object_set_data(G_OBJECT(button), "type", (gpointer)(guintptr)vi);
+                       g_object_set_data(G_OBJECT(button), "elev", (gpointer)(guintptr)(elev*100));
                        g_signal_connect(button, "clicked", G_CALLBACK(_on_sweep_clicked), level2);
                }
        }
@@ -483,12 +497,13 @@ GtkWidget *aweather_level2_get_config(AWeatherLevel2 *level2)
        gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
        gtk_table_attach(GTK_TABLE(table), row_label,
                        0,1, rows,rows+1, GTK_FILL,GTK_FILL, 5,0);
-       GtkWidget *scale = gtk_hscale_new_with_range(ISO_MIN, ISO_MAX, 0.5);
+       GtkWidget *scale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, ISO_MIN, ISO_MAX, 0.5);
        gtk_widget_set_size_request(scale, -1, 26);
        gtk_scale_set_value_pos(GTK_SCALE(scale), GTK_POS_LEFT);
        gtk_range_set_inverted(GTK_RANGE(scale), TRUE);
        gtk_range_set_value(GTK_RANGE(scale), ISO_MAX);
-       g_signal_connect(scale, "value-changed", G_CALLBACK(_on_iso_changed), level2);
+       g_signal_connect(scale, "value-changed", G_CALLBACK(_on_iso_changed),  level2);
+       g_signal_connect(scale, "format-value",  G_CALLBACK(_on_format_value), level2);
        gtk_table_attach(GTK_TABLE(table), scale,
                        1,cols+1, rows,rows+1, GTK_FILL|GTK_EXPAND,GTK_FILL, 0,0);
        /* Shove all the buttons to the left, but keep the slider expanded */
@@ -501,20 +516,33 @@ GtkWidget *aweather_level2_get_config(AWeatherLevel2 *level2)
  * GObject code *
  ****************/
 G_DEFINE_TYPE(AWeatherLevel2, aweather_level2, GRITS_TYPE_OBJECT);
-static void aweather_level2_init(AWeatherLevel2 *self)
+static void aweather_level2_init(AWeatherLevel2 *level2)
 {
 }
-static void aweather_level2_finalize(GObject *_self)
+static void aweather_level2_dispose(GObject *_level2)
+{
+       AWeatherLevel2 *level2 = AWEATHER_LEVEL2(_level2);
+       g_debug("AWeatherLevel2: dispose - %p", _level2);
+       if (level2->volume) {
+               grits_viewer_remove(GRITS_OBJECT(level2->volume)->viewer,
+                               GRITS_OBJECT(level2->volume));
+               level2->volume = NULL;
+       }
+       G_OBJECT_CLASS(aweather_level2_parent_class)->dispose(_level2);
+}
+static void aweather_level2_finalize(GObject *_level2)
 {
-       AWeatherLevel2 *self = AWEATHER_LEVEL2(_self);
-       g_debug("AWeatherLevel2: finalize - %p", _self);
-       RSL_free_radar(self->radar);
-       if (self->sweep_tex)
-               glDeleteTextures(1, &self->sweep_tex);
-       G_OBJECT_CLASS(aweather_level2_parent_class)->finalize(_self);
+       AWeatherLevel2 *level2 = AWEATHER_LEVEL2(_level2);
+       g_debug("AWeatherLevel2: finalize - %p", _level2);
+       RSL_free_radar(level2->radar);
+       if (level2->sweep_tex)
+               glDeleteTextures(1, &level2->sweep_tex);
+       G_OBJECT_CLASS(aweather_level2_parent_class)->finalize(_level2);
 }
 static void aweather_level2_class_init(AWeatherLevel2Class *klass)
 {
+       G_OBJECT_CLASS(klass)->dispose  = aweather_level2_dispose;
        G_OBJECT_CLASS(klass)->finalize = aweather_level2_finalize;
-       GRITS_OBJECT_CLASS(klass)->draw   = aweather_level2_draw;
+       GRITS_OBJECT_CLASS(klass)->draw = aweather_level2_draw;
+       GRITS_OBJECT_CLASS(klass)->hide = aweather_level2_hide;
 }