* It now compiles.
[scdataviz] / src / 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 Thu Oct 15 21:51:34 2009 Johnny Q. Hacker
28 */
29
30 #include <stdlib.h>
31 #include <strings.h>
32 #include <gtk/gtk.h>
33 #include "config.h"
34 #include "matdb-dotcode.h"
35 #include "graphwidget.h"
36
37 #define DEBUG_SHOW_ONLY_BOWED
38
39 struct xy_properties {
40   GString *xprop;
41   GString *yprop;
42   Graph *graph;
43   struct matdb *mdb;
44   struct matdb_material *mat;
45 };
46
47 static void lookup_xy(GHashTable* propts, GString* xprop, GString* yprop, double **x, double **y) {
48   *x=g_hash_table_lookup(propts, xprop->str);
49   *y=g_hash_table_lookup(propts, yprop->str);
50 }
51
52 static void put_mat_in_graph(gpointer key, gpointer value, gpointer user_data) {
53   struct xy_properties *propmap = user_data;
54   struct matdb_material *mat = value;
55   if(!strcasecmp(mat->name->str, "vacuum")) return;
56   if(!strcasecmp(mat->name->str, "pvb")) return;
57   double *x, *y;
58   #ifdef DEBUG_ASSIGNMENT
59   fprintf(stderr, "put_mat_in_graph(%s) (x->%s, y->%s): ", (char*)key, propmap->xprop->str, propmap->yprop->str);
60   #endif
61   lookup_xy(mat->properties, propmap->xprop, propmap->yprop, &x, &y);
62   if((x != NULL) && (y != NULL)) {
63     graph_add_point(propmap->graph, *x, *y, mat->name);
64   #ifdef DEBUG_ASSIGNMENT
65     fprintf(stderr, "added (x->%s=%g, y->%s=%g)\n", propmap->xprop->str, *x, propmap->yprop->str, *y);
66   }else{
67     fprintf(stderr, "no such properties (x->%s, y->%s)\n", propmap->xprop->str, propmap->yprop->str);
68   #endif
69   }
70 }
71
72 static void inner_link_materials(gpointer key, gpointer value, gpointer user_data) {
73   struct xy_properties *propmap = user_data;
74   struct matdb_material *mat = value;
75   if(!strcasecmp(mat->name->str, "vacuum")) return;
76   if(!strcasecmp(mat->name->str, "pvb")) return;
77   if(g_string_equal(mat->name, propmap->mat->name)) return;
78   struct graph_line *l = (struct graph_line *)malloc(sizeof(struct graph_line));
79   if(l == NULL) return;
80   struct matdb_bowing *bow = NULL;
81   GHashTable *subtable;
82   double *p0_x, *p0_y, *p1_x, *p1_y, *p3_x, *p3_y;
83   lookup_xy(propmap->mat->properties, propmap->xprop, propmap->yprop, &p0_x, &p0_y);
84   lookup_xy(mat->properties, propmap->xprop, propmap->yprop, &p3_x, &p3_y);
85 #ifdef DEBUG_ASSIGNMENT
86   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);
87 #endif
88   if((p0_x != NULL) && (p0_y != NULL) && (p3_x != NULL) && (p3_y != NULL)) {
89     l->p0_x = *p0_x;
90     l->p0_y = *p0_y;
91     l->p3_x = *p3_x;
92     l->p3_y = *p3_y;
93     if((subtable = g_hash_table_lookup(propmap->mdb->bowings, propmap->mat->name->str)) == NULL) {
94       /*Try the other way 'round*/
95       if((subtable = g_hash_table_lookup(propmap->mdb->bowings, mat->name->str) )
96           != NULL) {
97         bow = g_hash_table_lookup(subtable, propmap->mat->name->str);
98       }
99     }else{
100       bow = g_hash_table_lookup(subtable, mat->name->str);
101     }
102     if(bow != NULL) {
103       if((p1_x = g_hash_table_lookup(bow->properties, propmap->xprop->str)) != NULL) {
104         l->p1_x = *p1_x/2.0;
105         graph_bezier_quadratic_to_cubic(l->p0_x, l->p3_x, &(l->p1_x), &(l->p2_x));
106       }else{
107         graph_bezier_linear_to_cubic(l->p0_x, l->p3_x, &(l->p1_x), &(l->p2_x));
108       }
109       if((p1_y = g_hash_table_lookup(bow->properties, propmap->yprop->str)) != NULL) {
110         l->p1_y = *p1_y/2.0;
111         graph_bezier_quadratic_to_cubic(l->p0_y, l->p3_y, &(l->p1_y), &(l->p2_y));
112       }else{
113         graph_bezier_linear_to_cubic(l->p0_y, l->p3_y, &(l->p1_y), &(l->p2_y));
114       }
115     }else{
116       #ifdef DEBUG_SHOW_ONLY_BOWED
117       free(l);
118       return;
119       #endif
120       graph_bezier_linear_to_cubic(l->p0_x, l->p3_x, &(l->p1_x), &(l->p2_x));
121       graph_bezier_linear_to_cubic(l->p0_y, l->p3_y, &(l->p1_y), &(l->p2_y));
122     }
123     #ifdef DEBUG_ASSIGNMENT
124     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);
125     #endif
126     graph_add_graph_line(propmap->graph, l);
127   }else{
128     free(l);
129   }
130 }
131
132 static void link_materials(gpointer key, gpointer value, gpointer user_data) {
133   struct xy_properties *propmap = user_data;
134   struct matdb_material *mat = value;
135   if(!strcasecmp(mat->name->str, "vacuum")) return;
136   if(!strcasecmp(mat->name->str, "pvb")) return;
137   propmap->mat = mat;
138   g_hash_table_foreach(propmap->mdb->materials, &inner_link_materials, propmap);
139 }
140
141 int main(int   argc, char *argv[]) 
142 {
143     GtkWidget *window;
144     GtkWidget *graph;
145     GString *file = g_string_new("matdb.txt");
146     int err=0;
147     struct matdb *mdb = read_matdb_dotcode(file, &err);
148     //fprintf(stderr, "read_matdb_dotcode(%s, %d)=%x", file->str, err,
149     //(int)mdb);
150     //fprintf(stderr, "err=%d\n", err);
151     //print_matdb(mdb);
152     
153     gtk_init (&argc, &argv);
154     
155     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
156     graph = graph_widget_new();
157
158     struct xy_properties propmap;
159     propmap.xprop = g_string_new("a_lc");
160     propmap.yprop = g_string_new("E_g_Gamma");
161     propmap.graph = graph_widget_get_graph(GRAPH_WIDGET(graph));
162     propmap.mdb = mdb;
163     g_hash_table_foreach(mdb->materials, &put_mat_in_graph, &propmap);
164     //graph_add_linear_connectors(propmap.graph);
165     g_hash_table_foreach(mdb->materials, &link_materials, &propmap);
166     graph_autoset_xaxis(propmap.graph, propmap.xprop);
167     graph_autoset_yaxis(propmap.graph, propmap.yprop);
168
169     //Connect signals
170     g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
171
172     gtk_container_add(GTK_CONTAINER(window), graph);
173     /*
174     graph_add_point(graph_widget_get_graph(graph), 5, 3);
175     graph_add_point(graph_widget_get_graph(graph), 8, 12);
176     graph_add_point(graph_widget_get_graph(graph), 11, 48);
177     */
178     
179     gtk_widget_show_all  (window);
180     
181     gtk_main ();
182     
183     return 0;
184 }