* OK. Now *this* seems to work great.
[scdataviz] / scdataviz.c
1 /*
2 ** scdataviz.c
3 ** 
4 ** Made by (Johnny Q. Hacker)
5 ** Login   <solarion@johnathan>
6 ** 
7 ** Holds the SC graphing widget.
8
9
10
11     Copyright (C) 2008 Joseph Pingenot
12
13     This program is free software: you can redistribute it and/or modify
14     it under the terms of the GNU Affero General Public License as published by
15     the Free Software Foundation, either version 3 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU Affero General Public License for more details.
22
23     You should have received a copy of the GNU Affero General Public License
24     along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
26 ** Started on  Thu Jul 17 11:03:27 2008 Johnny Q. Hacker
27 ** Last update Sun May 12 01:17:25 2002 Speed Blue
28 */
29
30 #include <gtk/gtk.h>
31 #include <graphwidget.h>
32 #include <matdb-dotcode.h>
33 #include <stdlib.h>
34 #include <strings.h>
35
36 #define DEBUG_SHOW_ONLY_BOWED
37
38 struct xy_properties {
39   GString *xprop;
40   GString *yprop;
41   Graph *graph;
42   struct matdb *mdb;
43   struct matdb_material *mat;
44 };
45
46 static void lookup_xy(GHashTable* propts, GString* xprop, GString* yprop, double **x, double **y) {
47   *x=g_hash_table_lookup(propts, xprop->str);
48   *y=g_hash_table_lookup(propts, yprop->str);
49 }
50
51 static void put_mat_in_graph(gpointer key, gpointer value, gpointer user_data) {
52   struct xy_properties *propmap = user_data;
53   struct matdb_material *mat = value;
54   if(!strcasecmp(mat->name->str, "vacuum")) return;
55   if(!strcasecmp(mat->name->str, "pvb")) return;
56   double *x, *y;
57   #ifdef DEBUG_ASSIGNMENT
58   fprintf(stderr, "put_mat_in_graph(%s) (x->%s, y->%s): ", (char*)key, propmap->xprop->str, propmap->yprop->str);
59   #endif
60   lookup_xy(mat->properties, propmap->xprop, propmap->yprop, &x, &y);
61   if((x != NULL) && (y != NULL)) {
62     graph_add_point(propmap->graph, *x, *y, mat->name);
63   #ifdef DEBUG_ASSIGNMENT
64     fprintf(stderr, "added (x->%s=%g, y->%s=%g)\n", propmap->xprop->str, *x, propmap->yprop->str, *y);
65   }else{
66     fprintf(stderr, "no such properties (x->%s, y->%s)\n", propmap->xprop->str, propmap->yprop->str);
67   #endif
68   }
69 }
70
71 static void inner_link_materials(gpointer key, gpointer value, gpointer user_data) {
72   struct xy_properties *propmap = user_data;
73   struct matdb_material *mat = value;
74   if(!strcasecmp(mat->name->str, "vacuum")) return;
75   if(!strcasecmp(mat->name->str, "pvb")) return;
76   if(g_string_equal(mat->name, propmap->mat->name)) return;
77   struct graph_line *l = (struct graph_line *)malloc(sizeof(struct graph_line));
78   if(l == NULL) return;
79   struct matdb_bowing *bow = NULL;
80   GHashTable *subtable;
81   double *p0_x, *p0_y, *p1_x, *p1_y, *p3_x, *p3_y;
82   lookup_xy(propmap->mat->properties, propmap->xprop, propmap->yprop, &p0_x, &p0_y);
83   lookup_xy(mat->properties, propmap->xprop, propmap->yprop, &p3_x, &p3_y);
84 #ifdef DEBUG_ASSIGNMENT
85   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);
86 #endif
87   if((p0_x != NULL) && (p0_y != NULL) && (p3_x != NULL) && (p3_y != NULL)) {
88     l->p0_x = *p0_x;
89     l->p0_y = *p0_y;
90     l->p3_x = *p3_x;
91     l->p3_y = *p3_y;
92     if((subtable = g_hash_table_lookup(propmap->mdb->bowings, propmap->mat->name->str)) == NULL) {
93       /*Try the other way 'round*/
94       if((subtable = g_hash_table_lookup(propmap->mdb->bowings, mat->name->str) )
95           != NULL) {
96         bow = g_hash_table_lookup(subtable, propmap->mat->name->str);
97       }
98     }else{
99       bow = g_hash_table_lookup(subtable, mat->name->str);
100     }
101     if(bow != NULL) {
102       if((p1_x = g_hash_table_lookup(bow->properties, propmap->xprop->str)) != NULL) {
103         l->p1_x = *p1_x/2.0;
104         graph_bezier_quadratic_to_cubic(l->p0_x, l->p3_x, &(l->p1_x), &(l->p2_x));
105       }else{
106         graph_bezier_linear_to_cubic(l->p0_x, l->p3_x, &(l->p1_x), &(l->p2_x));
107       }
108       if((p1_y = g_hash_table_lookup(bow->properties, propmap->yprop->str)) != NULL) {
109         l->p1_y = *p1_y/2.0;
110         graph_bezier_quadratic_to_cubic(l->p0_y, l->p3_y, &(l->p1_y), &(l->p2_y));
111       }else{
112         graph_bezier_linear_to_cubic(l->p0_y, l->p3_y, &(l->p1_y), &(l->p2_y));
113       }
114     }else{
115       #ifdef DEBUG_SHOW_ONLY_BOWED
116       free(l);
117       return;
118       #endif
119       graph_bezier_linear_to_cubic(l->p0_x, l->p3_x, &(l->p1_x), &(l->p2_x));
120       graph_bezier_linear_to_cubic(l->p0_y, l->p3_y, &(l->p1_y), &(l->p2_y));
121     }
122     #ifdef DEBUG_ASSIGNMENT
123     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);
124     #endif
125     graph_add_graph_line(propmap->graph, l);
126   }else{
127     free(l);
128   }
129 }
130
131 static void link_materials(gpointer key, gpointer value, gpointer user_data) {
132   struct xy_properties *propmap = user_data;
133   struct matdb_material *mat = value;
134   if(!strcasecmp(mat->name->str, "vacuum")) return;
135   if(!strcasecmp(mat->name->str, "pvb")) return;
136   propmap->mat = mat;
137   g_hash_table_foreach(propmap->mdb->materials, &inner_link_materials, propmap);
138 }
139
140 int main(int   argc, char *argv[]) 
141 {
142     GtkWidget *window;
143     GtkWidget *graph;
144     GString *file = g_string_new("../matdb");
145     int err=0;
146     struct matdb *mdb = read_matdb_dotcode(file, &err);
147     //fprintf(stderr, "read_matdb_dotcode(%s, %d)=%x", file->str, err,
148     //(int)mdb);
149     fprintf(stderr, "err=%d\n", err);
150     //print_matdb(mdb);
151     
152     gtk_init (&argc, &argv);
153     
154     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
155     graph = graph_widget_new();
156
157     struct xy_properties propmap;
158     propmap.xprop = g_string_new("a_lc");
159     propmap.yprop = g_string_new("E_g_Gamma");
160     propmap.graph = graph_widget_get_graph(GRAPH_WIDGET(graph));
161     propmap.mdb = mdb;
162     g_hash_table_foreach(mdb->materials, &put_mat_in_graph, &propmap);
163     //graph_add_linear_connectors(propmap.graph);
164     g_hash_table_foreach(mdb->materials, &link_materials, &propmap);
165     graph_autoset_xaxis(propmap.graph, propmap.xprop);
166     graph_autoset_yaxis(propmap.graph, propmap.yprop);
167
168     //Connect signals
169     g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
170
171     gtk_container_add(GTK_CONTAINER(window), graph);
172     /*
173     graph_add_point(graph_widget_get_graph(graph), 5, 3);
174     graph_add_point(graph_widget_get_graph(graph), 8, 12);
175     graph_add_point(graph_widget_get_graph(graph), 11, 48);
176     */
177     
178     gtk_widget_show_all  (window);
179     
180     gtk_main ();
181     
182     return 0;
183 }