callbacks.cpp
Go to the documentation of this file.
1 #include <gtk/gtk.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <list>
7 #include "callbacks.h"
8 #include "interface.h"
9 #include "../../svm.h"
10 using namespace std;
11 
12 #define DEFAULT_PARAM "-t 2 -c 100"
13 #define XLEN 500
14 #define YLEN 500
15 
16 GdkColor colors[] =
17 {
18  {0,0,0,0},
19  {0,0,120<<8,120<<8},
20  {0,120<<8,120<<8,0},
21  {0,120<<8,0,120<<8},
22  {0,0,200<<8,200<<8},
23  {0,200<<8,200<<8,0},
24  {0,200<<8,0,200<<8},
25 };
26 
27 GdkGC *gc;
28 GdkPixmap *pixmap;
29 extern "C" GtkWidget *draw_main;
30 GtkWidget *draw_main;
31 extern "C" GtkWidget *entry_option;
32 GtkWidget *entry_option;
33 
34 typedef struct {
35  double x, y;
36  signed char value;
37 } point;
38 
39 list<point> point_list;
40 int current_value = 1;
41 
42 extern "C" void svm_toy_initialize()
43 {
44  gboolean success[7];
45 
46  gdk_colormap_alloc_colors(
47  gdk_colormap_get_system(),
48  colors,
49  7,
50  FALSE,
51  TRUE,
52  success);
53 
54  gc = gdk_gc_new(draw_main->window);
55  pixmap = gdk_pixmap_new(draw_main->window,XLEN,YLEN,-1);
56  gdk_gc_set_foreground(gc,&colors[0]);
57  gdk_draw_rectangle(pixmap,gc,TRUE,0,0,XLEN,YLEN);
58  gtk_entry_set_text(GTK_ENTRY(entry_option),DEFAULT_PARAM);
59 }
60 
61 void redraw_area(GtkWidget* widget, int x, int y, int w, int h)
62 {
63  gdk_draw_pixmap(widget->window,
64  gc,
65  pixmap,
66  x,y,x,y,w,h);
67 }
68 
69 void draw_point(const point& p)
70 {
71  gdk_gc_set_foreground(gc,&colors[p.value+3]);
72  gdk_draw_rectangle(pixmap, gc, TRUE,int(p.x*XLEN),int(p.y*YLEN),4,4);
73  gdk_draw_rectangle(draw_main->window, gc, TRUE,int(p.x*XLEN),int(p.y*YLEN),4,4);
74 }
75 
77 {
78  for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
79  draw_point(*p);
80 }
81 
82 void clear_all()
83 {
84  point_list.clear();
85  gdk_gc_set_foreground(gc,&colors[0]);
86  gdk_draw_rectangle(pixmap,gc,TRUE,0,0,XLEN,YLEN);
88 }
89 
90 void
91 on_button_change_clicked (GtkButton *button,
92  gpointer user_data)
93 {
94  ++current_value;
95  if(current_value > 3) current_value = 1;
96 }
97 
98 void
99 on_button_run_clicked (GtkButton *button,
100  gpointer user_data)
101 {
102  // guard
103  if(point_list.empty()) return;
104 
106  int i,j;
107 
108  // default values
109  param.svm_type = C_SVC;
110  param.kernel_type = RBF;
111  param.degree = 3;
112  param.gamma = 0;
113  param.coef0 = 0;
114  param.nu = 0.5;
115  param.cache_size = 100;
116  param.C = 1;
117  param.eps = 1e-3;
118  param.p = 0.1;
119  param.shrinking = 1;
120  param.probability = 0;
121  param.nr_weight = 0;
122  param.weight_label = NULL;
123  param.weight = NULL;
124 
125  // parse options
126  const char *p = gtk_entry_get_text(GTK_ENTRY(entry_option));
127 
128  while (1) {
129  while (*p && *p != '-')
130  p++;
131 
132  if (*p == '\0')
133  break;
134 
135  p++;
136  switch (*p++) {
137  case 's':
138  param.svm_type = atoi(p);
139  break;
140  case 't':
141  param.kernel_type = atoi(p);
142  break;
143  case 'd':
144  param.degree = atoi(p);
145  break;
146  case 'g':
147  param.gamma = atof(p);
148  break;
149  case 'r':
150  param.coef0 = atof(p);
151  break;
152  case 'n':
153  param.nu = atof(p);
154  break;
155  case 'm':
156  param.cache_size = atof(p);
157  break;
158  case 'c':
159  param.C = atof(p);
160  break;
161  case 'e':
162  param.eps = atof(p);
163  break;
164  case 'p':
165  param.p = atof(p);
166  break;
167  case 'h':
168  param.shrinking = atoi(p);
169  break;
170  case 'b':
171  param.probability = atoi(p);
172  break;
173  case 'w':
174  ++param.nr_weight;
175  param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight);
176  param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight);
177  param.weight_label[param.nr_weight-1] = atoi(p);
178  while(*p && !isspace(*p)) ++p;
179  param.weight[param.nr_weight-1] = atof(p);
180  break;
181  }
182  }
183 
184  // build problem
186 
187  prob.l = point_list.size();
188  prob.y = new double[prob.l];
189 
190  if(param.kernel_type == PRECOMPUTED)
191  {
192  }
193  else if(param.svm_type == EPSILON_SVR ||
194  param.svm_type == NU_SVR)
195  {
196  if(param.gamma == 0) param.gamma = 1;
197  svm_node *x_space = new svm_node[2 * prob.l];
198  prob.x = new svm_node *[prob.l];
199 
200  i = 0;
201  for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
202  {
203  x_space[2 * i].index = 1;
204  x_space[2 * i].value = q->x;
205  x_space[2 * i + 1].index = -1;
206  prob.x[i] = &x_space[2 * i];
207  prob.y[i] = q->y;
208  }
209 
210  // build model & classify
211  svm_model *model = svm_train(&prob, &param);
212  svm_node x[2];
213  x[0].index = 1;
214  x[1].index = -1;
215  int *j = new int[XLEN];
216 
217  for (i = 0; i < XLEN; i++)
218  {
219  x[0].value = (double) i / XLEN;
220  j[i] = (int)(YLEN*svm_predict(model, x));
221  }
222 
223  gdk_gc_set_foreground(gc,&colors[0]);
224  gdk_draw_line(pixmap,gc,0,0,0,YLEN-1);
225  gdk_draw_line(draw_main->window,gc,0,0,0,YLEN-1);
226 
227  int p = (int)(param.p * YLEN);
228  for(i = 1; i < XLEN; i++)
229  {
230  gdk_gc_set_foreground(gc,&colors[0]);
231  gdk_draw_line(pixmap,gc,i,0,i,YLEN-1);
232  gdk_draw_line(draw_main->window,gc,i,0,i,YLEN-1);
233 
234  gdk_gc_set_foreground(gc,&colors[5]);
235  gdk_draw_line(pixmap,gc,i-1,j[i-1],i,j[i]);
236  gdk_draw_line(draw_main->window,gc,i-1,j[i-1],i,j[i]);
237 
238  if(param.svm_type == EPSILON_SVR)
239  {
240  gdk_gc_set_foreground(gc,&colors[2]);
241  gdk_draw_line(pixmap,gc,i-1,j[i-1]+p,i,j[i]+p);
242  gdk_draw_line(draw_main->window,gc,i-1,j[i-1]+p,i,j[i]+p);
243 
244  gdk_gc_set_foreground(gc,&colors[2]);
245  gdk_draw_line(pixmap,gc,i-1,j[i-1]-p,i,j[i]-p);
246  gdk_draw_line(draw_main->window,gc,i-1,j[i-1]-p,i,j[i]-p);
247  }
248  }
249 
251  delete[] j;
252  delete[] x_space;
253  delete[] prob.x;
254  delete[] prob.y;
255  }
256  else
257  {
258  if(param.gamma == 0) param.gamma = 0.5;
259  svm_node *x_space = new svm_node[3 * prob.l];
260  prob.x = new svm_node *[prob.l];
261 
262  i = 0;
263  for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
264  {
265  x_space[3 * i].index = 1;
266  x_space[3 * i].value = q->x;
267  x_space[3 * i + 1].index = 2;
268  x_space[3 * i + 1].value = q->y;
269  x_space[3 * i + 2].index = -1;
270  prob.x[i] = &x_space[3 * i];
271  prob.y[i] = q->value;
272  }
273 
274  // build model & classify
275  svm_model *model = svm_train(&prob, &param);
276  svm_node x[3];
277  x[0].index = 1;
278  x[1].index = 2;
279  x[2].index = -1;
280 
281  for (i = 0; i < XLEN; i++)
282  for (j = 0; j < YLEN; j++) {
283  x[0].value = (double) i / XLEN;
284  x[1].value = (double) j / YLEN;
285  double d = svm_predict(model, x);
286  if (param.svm_type == ONE_CLASS && d<0) d=2;
287  gdk_gc_set_foreground(gc,&colors[(int)d]);
288  gdk_draw_point(pixmap,gc,i,j);
289  gdk_draw_point(draw_main->window,gc,i,j);
290  }
291 
293  delete[] x_space;
294  delete[] prob.x;
295  delete[] prob.y;
296  }
297  free(param.weight_label);
298  free(param.weight);
299  draw_all_points();
300 }
301 
302 void
303 on_button_clear_clicked (GtkButton *button,
304  gpointer user_data)
305 {
306  clear_all();
307 }
308 
309 void
310 on_window1_destroy (GtkObject *object,
311  gpointer user_data)
312 {
313  gtk_exit(0);
314 }
315 
316 gboolean
318  GdkEventButton *event,
319  gpointer user_data)
320 {
321  point p = {(double)event->x/XLEN, (double)event->y/YLEN, current_value};
322  point_list.push_back(p);
323  draw_point(p);
324  return FALSE;
325 }
326 
327 gboolean
328 on_draw_main_expose_event (GtkWidget *widget,
329  GdkEventExpose *event,
330  gpointer user_data)
331 {
332  redraw_area(widget,
333  event->area.x, event->area.y,
334  event->area.width, event->area.height);
335  return FALSE;
336 }
337 
338 GtkWidget *fileselection;
339 static enum { SAVE, LOAD } fileselection_flag;
340 
342 {
344  gtk_signal_connect_object(
345  GTK_OBJECT(GTK_FILE_SELECTION(fileselection)->ok_button),
346  "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
347  (GtkObject *) fileselection);
348 
349  gtk_signal_connect_object (GTK_OBJECT
350  (GTK_FILE_SELECTION(fileselection)->cancel_button),
351  "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
352  (GtkObject *) fileselection);
353 
354  gtk_widget_show(fileselection);
355 }
356 
357 void
358 on_button_save_clicked (GtkButton *button,
359  gpointer user_data)
360 {
363 }
364 
365 
366 void
367 on_button_load_clicked (GtkButton *button,
368  gpointer user_data)
369 {
372 }
373 
374 void
375 on_filesel_ok_clicked (GtkButton *button,
376  gpointer user_data)
377 {
378  gtk_widget_hide(fileselection);
379  const char *filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(fileselection));
380 
381  if(fileselection_flag == SAVE)
382  {
383  FILE *fp = fopen(filename,"w");
384 
385  const char *p = gtk_entry_get_text(GTK_ENTRY(entry_option));
386  const char* svm_type_str = strstr(p, "-s ");
387  int svm_type = C_SVC;
388  if(svm_type_str != NULL)
389  sscanf(svm_type_str, "-s %d", &svm_type);
390 
391  if(fp)
392  {
393  if(svm_type == EPSILON_SVR || svm_type == NU_SVR)
394  {
395  for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
396  fprintf(fp,"%f 1:%f\n", p->y, p->x);
397  }
398  else
399  {
400  for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
401  fprintf(fp,"%d 1:%f 2:%f\n", p->value, p->x, p->y);
402  }
403  fclose(fp);
404  }
405 
406  }
407  else if(fileselection_flag == LOAD)
408  {
409  FILE *fp = fopen(filename,"r");
410  if(fp)
411  {
412  clear_all();
413  char buf[4096];
414  while(fgets(buf,sizeof(buf),fp))
415  {
416  int v;
417  double x,y;
418  if(sscanf(buf,"%d%*d:%lf%*d:%lf",&v,&x,&y)==3)
419  {
420  point p = {x,y,v};
421  point_list.push_back(p);
422  }
423  else if(sscanf(buf,"%lf%*d:%lf",&y,&x)==2)
424  {
425  point p = {x,y,current_value};
426  point_list.push_back(p);
427  }
428  else
429  break;
430  }
431  fclose(fp);
432  draw_all_points();
433  }
434  }
435 }
436 
437 void
438 on_fileselection_destroy (GtkObject *object,
439  gpointer user_data)
440 {
441 }
442 
443 void
444 on_filesel_cancel_clicked (GtkButton *button,
445  gpointer user_data)
446 {
447 }
d
GtkWidget * fileselection
Definition: callbacks.cpp:338
void svm_toy_initialize()
Definition: callbacks.cpp:42
void show_fileselection()
Definition: callbacks.cpp:341
filename
#define DEFAULT_PARAM
Definition: callbacks.cpp:12
struct svm_problem prob
Definition: svmtrain.c:57
struct svm_parameter param
Definition: svmtrain.c:56
gboolean on_draw_main_button_press_event(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
Definition: callbacks.cpp:317
GdkGC * gc
Definition: callbacks.cpp:27
def svm_train(arg1, arg2=None, arg3=None)
Definition: svmutil.py:77
Definition: svm.h:25
void on_button_load_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:367
double value
Definition: svm.h:15
int nr_weight
Definition: svm.h:40
void on_fileselection_destroy(GtkObject *object, gpointer user_data)
Definition: callbacks.cpp:438
int * weight_label
Definition: svm.h:41
Definition: svm.h:25
TFSIMD_FORCE_INLINE const tfScalar & y() const
void on_button_clear_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:303
Definition: svm.h:52
GdkPixmap * pixmap
Definition: callbacks.cpp:28
double p
Definition: svm.h:44
void on_filesel_ok_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:375
static enum @0 fileselection_flag
def svm_predict(y, x, m, options="")
Definition: svmutil.py:164
struct svm_node * x
Definition: svm-predict.c:8
double cache_size
Definition: svm.h:37
void on_button_change_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:91
void redraw_area(GtkWidget *widget, int x, int y, int w, int h)
Definition: callbacks.cpp:61
double y
Definition: callbacks.cpp:35
double eps
Definition: svm.h:38
int shrinking
Definition: svm.h:45
void clear_all()
Definition: callbacks.cpp:82
struct svm_node ** x
Definition: svm.h:22
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
Definition: svm.cpp:2961
GtkWidget * draw_main
Definition: callbacks.cpp:29
GtkWidget * create_fileselection(void)
Definition: interface.c:131
double x
Definition: callbacks.cpp:35
void draw_point(const point &p)
Definition: callbacks.cpp:69
void on_button_run_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:99
Definition: svm.h:26
int index
Definition: svm.h:14
#define YLEN
Definition: callbacks.cpp:14
list< point > point_list
Definition: callbacks.cpp:39
GtkWidget * entry_option
Definition: callbacks.cpp:31
int current_value
Definition: callbacks.cpp:40
void on_button_save_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:358
void on_filesel_cancel_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:444
signed char value
Definition: callbacks.cpp:36
int probability
Definition: svm.h:46
int degree
Definition: svm.h:32
Definition: svm.h:25
Definition: svm.h:12
double * y
Definition: svm.h:21
double gamma
Definition: svm.h:33
int l
Definition: svm.h:20
double * weight
Definition: svm.h:42
GdkColor colors[]
Definition: callbacks.cpp:16
void draw_all_points()
Definition: callbacks.cpp:76
double C
Definition: svm.h:39
#define XLEN
Definition: callbacks.cpp:13
int svm_type
Definition: svm.h:30
double nu
Definition: svm.h:43
struct svm_model * model
Definition: svmtrain.c:58
gboolean on_draw_main_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
Definition: callbacks.cpp:328
double coef0
Definition: svm.h:34
int kernel_type
Definition: svm.h:31
struct svm_node * x_space
Definition: svmtrain.c:59
void on_window1_destroy(GtkObject *object, gpointer user_data)
Definition: callbacks.cpp:310


haf_grasping
Author(s): David Fischinger
autogenerated on Mon Jun 10 2019 13:28:43