* graphwidget.c: fixed a bug where we'd divide by 0 if min and max x and y were the...
[scdataviz] / scdataviz.c
index 9597ec9..812cd26 100644 (file)
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 ** Started on  Thu Jul 17 11:03:27 2008 Johnny Q. Hacker
-** Last update Sun May 12 01:17:25 2002 Speed Blue
+** Last update Thu Oct  8 19:13:57 2009 Johnny Q. Hacker
 */
 
 #include <gtk/gtk.h>
 #include <graphwidget.h>
 #include <matdb-dotcode.h>
+#include <stdlib.h>
+#include <strings.h>
+
+#define DEBUG_SHOW_ONLY_BOWED
+
+struct xy_properties {
+  GString *xprop;
+  GString *yprop;
+  Graph *graph;
+  struct matdb *mdb;
+  struct matdb_material *mat;
+};
+
+static void lookup_xy(GHashTable* propts, GString* xprop, GString* yprop, double **x, double **y) {
+  *x=g_hash_table_lookup(propts, xprop->str);
+  *y=g_hash_table_lookup(propts, yprop->str);
+}
+
+static void put_mat_in_graph(gpointer key, gpointer value, gpointer user_data) {
+  struct xy_properties *propmap = user_data;
+  struct matdb_material *mat = value;
+  if(!strcasecmp(mat->name->str, "vacuum")) return;
+  if(!strcasecmp(mat->name->str, "pvb")) return;
+  double *x, *y;
+  #ifdef DEBUG_ASSIGNMENT
+  fprintf(stderr, "put_mat_in_graph(%s) (x->%s, y->%s): ", (char*)key, propmap->xprop->str, propmap->yprop->str);
+  #endif
+  lookup_xy(mat->properties, propmap->xprop, propmap->yprop, &x, &y);
+  if((x != NULL) && (y != NULL)) {
+    graph_add_point(propmap->graph, *x, *y, mat->name);
+  #ifdef DEBUG_ASSIGNMENT
+    fprintf(stderr, "added (x->%s=%g, y->%s=%g)\n", propmap->xprop->str, *x, propmap->yprop->str, *y);
+  }else{
+    fprintf(stderr, "no such properties (x->%s, y->%s)\n", propmap->xprop->str, propmap->yprop->str);
+  #endif
+  }
+}
+
+static void inner_link_materials(gpointer key, gpointer value, gpointer user_data) {
+  struct xy_properties *propmap = user_data;
+  struct matdb_material *mat = value;
+  if(!strcasecmp(mat->name->str, "vacuum")) return;
+  if(!strcasecmp(mat->name->str, "pvb")) return;
+  if(g_string_equal(mat->name, propmap->mat->name)) return;
+  struct graph_line *l = (struct graph_line *)malloc(sizeof(struct graph_line));
+  if(l == NULL) return;
+  struct matdb_bowing *bow = NULL;
+  GHashTable *subtable;
+  double *p0_x, *p0_y, *p1_x, *p1_y, *p3_x, *p3_y;
+  lookup_xy(propmap->mat->properties, propmap->xprop, propmap->yprop, &p0_x, &p0_y);
+  lookup_xy(mat->properties, propmap->xprop, propmap->yprop, &p3_x, &p3_y);
+#ifdef DEBUG_ASSIGNMENT
+  fprintf(stderr, "%s:%s x=%s y=%s p0(%g,%g) p3(%g,%g)\n", propmap->mat->name->str, mat->name->str, propmap->xprop->str, propmap->yprop->str, (p0_x)?*p0_x:0, (p0_y)?*p0_y:0, (p3_x)?*p3_x:0, (p3_y)?*p3_y:0);
+#endif
+  if((p0_x != NULL) && (p0_y != NULL) && (p3_x != NULL) && (p3_y != NULL)) {
+    l->p0_x = *p0_x;
+    l->p0_y = *p0_y;
+    l->p3_x = *p3_x;
+    l->p3_y = *p3_y;
+    if((subtable = g_hash_table_lookup(propmap->mdb->bowings, propmap->mat->name->str)) == NULL) {
+      /*Try the other way 'round*/
+      if((subtable = g_hash_table_lookup(propmap->mdb->bowings, mat->name->str) )
+         != NULL) {
+       bow = g_hash_table_lookup(subtable, propmap->mat->name->str);
+      }
+    }else{
+      bow = g_hash_table_lookup(subtable, mat->name->str);
+    }
+    if(bow != NULL) {
+      if((p1_x = g_hash_table_lookup(bow->properties, propmap->xprop->str)) != NULL) {
+       l->p1_x = *p1_x/2.0;
+       graph_bezier_quadratic_to_cubic(l->p0_x, l->p3_x, &(l->p1_x), &(l->p2_x));
+      }else{
+       graph_bezier_linear_to_cubic(l->p0_x, l->p3_x, &(l->p1_x), &(l->p2_x));
+      }
+      if((p1_y = g_hash_table_lookup(bow->properties, propmap->yprop->str)) != NULL) {
+       l->p1_y = *p1_y/2.0;
+       graph_bezier_quadratic_to_cubic(l->p0_y, l->p3_y, &(l->p1_y), &(l->p2_y));
+      }else{
+       graph_bezier_linear_to_cubic(l->p0_y, l->p3_y, &(l->p1_y), &(l->p2_y));
+      }
+    }else{
+      #ifdef DEBUG_SHOW_ONLY_BOWED
+      free(l);
+      return;
+      #endif
+      graph_bezier_linear_to_cubic(l->p0_x, l->p3_x, &(l->p1_x), &(l->p2_x));
+      graph_bezier_linear_to_cubic(l->p0_y, l->p3_y, &(l->p1_y), &(l->p2_y));
+    }
+    #ifdef DEBUG_ASSIGNMENT
+    fprintf(stderr, "%s:%s p0(%g,%g) p1(%g,%g) p2(%g,%g), p3(%g,%g)\n", propmap->mat->name->str, mat->name->str, l->p0_x, l->p0_y, l->p1_x, l->p1_y, l->p2_x, l->p2_y, l->p3_x, l->p3_y);
+    #endif
+    graph_add_graph_line(propmap->graph, l);
+  }else{
+    free(l);
+  }
+}
+
+static void link_materials(gpointer key, gpointer value, gpointer user_data) {
+  struct xy_properties *propmap = user_data;
+  struct matdb_material *mat = value;
+  if(!strcasecmp(mat->name->str, "vacuum")) return;
+  if(!strcasecmp(mat->name->str, "pvb")) return;
+  propmap->mat = mat;
+  g_hash_table_foreach(propmap->mdb->materials, &inner_link_materials, propmap);
+}
 
 int main(int   argc, char *argv[]) 
 {
     GtkWidget *window;
-    GtkWidget *button;
     GtkWidget *graph;
-    GString *file = g_string_new("../matdb");
+    GString *file = g_string_new("matdb.txt");
     int err=0;
     struct matdb *mdb = read_matdb_dotcode(file, &err);
     //fprintf(stderr, "read_matdb_dotcode(%s, %d)=%x", file->str, err,
     //(int)mdb);
-    fprintf(stderr, "err=%d\n", err);
-    print_matdb(mdb);
-    
+    //fprintf(stderr, "err=%d\n", err);
+    //print_matdb(mdb);
     
     gtk_init (&argc, &argv);
-
-    
     
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
     graph = graph_widget_new();
-    
+
+    struct xy_properties propmap;
+    propmap.xprop = g_string_new("a_lc");
+    propmap.yprop = g_string_new("E_g_Gamma");
+    propmap.graph = graph_widget_get_graph(GRAPH_WIDGET(graph));
+    propmap.mdb = mdb;
+    g_hash_table_foreach(mdb->materials, &put_mat_in_graph, &propmap);
+    //graph_add_linear_connectors(propmap.graph);
+    g_hash_table_foreach(mdb->materials, &link_materials, &propmap);
+    graph_autoset_xaxis(propmap.graph, propmap.xprop);
+    graph_autoset_yaxis(propmap.graph, propmap.yprop);
 
     //Connect signals
     g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
 
     gtk_container_add(GTK_CONTAINER(window), graph);
+    /*
     graph_add_point(graph_widget_get_graph(graph), 5, 3);
     graph_add_point(graph_widget_get_graph(graph), 8, 12);
     graph_add_point(graph_widget_get_graph(graph), 11, 48);
+    */
     
     gtk_widget_show_all  (window);