gtk_viewer1.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <goocanvas.h>
3 #include "goo_laser_data.h"
4 #include "gtk_viewer.h"
5 
6 static gboolean on_rect_button_press (GooCanvasItem *view,
7  GooCanvasItem *target,
8  GdkEventButton *event,
9  gpointer data);
10 
11 static gboolean on_delete_event (GtkWidget *window,
12  GdkEvent *event,
13  gpointer unused_data);
14 
15 void* reading_thread(void *data);
16 
17 GooCanvasItem* text_item;
18 GooCanvasItem* rect_item;
19 
20 int main (int argc, char **argv)
21 {
22  sm_set_program_name(basename(argv[0]));
23 
24  viewer_params *p = (viewer_params*) malloc(sizeof(viewer_params));
25  lds_set_defaults(&(p->laser));
27 
28  p->laser.rays.draw = 0;
29  p->laser.points.draw = 0;
30  p->laser.normals.draw = 0;
31  p->laser.countour.width = 0.1;
32  p->pose_path.width = 0.1;
33  p->pose_path.color = "#f00";
34 
35  struct option * ops = options_allocate(100);
36  options_string(ops, "in", &(p->input_filename), "stdin", "input file (Carmen or JSON)");
37  options_string(ops, "use", &(p->use), "estimate", "One in 'odometry','estimate','true_pose'");
38 
39  lds_add_options(&(p->laser), ops, "laser_", "");
40  ls_add_options(&(p->pose_path), ops, "path_", "");
41 
42  if(!options_parse_args(ops, argc, argv)) {
43  fprintf(stderr, "A simple experimental GTK viewer.\n\nUsage:\n");
44  options_print_help(ops, stderr);
45  return -1;
46  }
47 
48  /* init threads */
49  g_thread_init(NULL);
50  gdk_threads_init();
51 
52 
53  GtkWidget *window, *scrolled_win, *canvas;
54 GooCanvasItem *root;
55 
56  /* Initialize GTK+. */
57  gtk_set_locale ();
58  gtk_init (&argc, &argv);
59 
60  /* Create the window and widgets. */
61  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
62  gtk_window_set_default_size (GTK_WINDOW (window), 640, 600);
63  gtk_widget_show (window);
64  g_signal_connect (window, "delete_event", (GtkSignalFunc) on_delete_event,
65  NULL);
66 
67  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
68  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
69  GTK_SHADOW_IN);
70  gtk_widget_show (scrolled_win);
71  gtk_container_add (GTK_CONTAINER (window), scrolled_win);
72 
73  canvas = goo_canvas_new ();
74 
75  root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
76 
77  p->device_size[0] = 800;
78  p->device_size[1] = 600;
79 
80  gtk_widget_set_size_request (canvas, p->device_size[0], p->device_size[1]);
81  goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, p->device_size[0], p->device_size[1]);
82  gtk_widget_show (canvas);
83  gtk_container_add (GTK_CONTAINER (scrolled_win), canvas);
84 
85  p->root = root;
86  p->canvas = canvas;
87 
88 
89  /* Add a few simple items. */
90 rect_item = goo_canvas_rect_new (root, 0, 0, 50, 50,
91  "line-width", 10.0,
92  "radius-x", 20.0,
93  "radius-y", 10.0,
94  "stroke-color", "yellow",
95  "fill-color", "red",
96  NULL);
97 
98 /*text_item = goo_canvas_text_new (root, "Hello World", 300, 300, -1,
99  GTK_ANCHOR_CENTER,
100  "font", "Sans 1",
101  NULL);
102  goo_canvas_item_rotate (text_item, 45, 300, 300);
103  */
104 
105 
106  GError * error;
107  if (!g_thread_create(reading_thread, p, FALSE, &error)) {
108  g_printerr ("Failed to create YES thread: %s\n", error->message);
109  return 1;
110  }
111  /* Pass control to the GTK+ main event loop. */
112  gtk_main ();
113 
114 }
115 
116 void* reading_thread(void *data) {
117  viewer_params * p = (viewer_params*) data;
118 
119  FILE * input = open_file_for_reading(p->input_filename);
120  if(!input) return 0;
121 
122  p->scans = malloc(1);
123  p->scans_items = malloc(1);
124  p->scans_size = 0;
125  p->scans_num = 0;
126 
127  LDP ld;
128  while( (ld = ld_read_smart(input)) ) {
129 
130  if(p->scans_num >= p->scans_size) {
131  p->scans_size = 2* p->scans_size +10;
132  p->scans = realloc(p->scans, sizeof(LDP) * p->scans_size);
133  p->scans_items = realloc(p->scans_items, sizeof(LDP) * p->scans_size);
134  }
135 
136  gdk_threads_enter();
137 
138  if(1) {
139  GooCanvasItem * gld = goo_laser_data_new (p->root, p, ld);
140  g_signal_connect (gld, "button_press_event",
141  (GtkSignalFunc) on_rect_button_press, NULL);
142  p->scans[p->scans_num] = ld;
143  p->scans_items[p->scans_num] = gld;
144  p->scans_num++;
145  } else {
146 
147  }
149  goo_canvas_update(p->canvas);
150 /* gdk_flush ();*/
151 
152  gdk_threads_leave();
153  /* sleep a while */
154  usleep(20);
155 /* sleep(g_random_int_range (1, 4));*/
156  }
157 
158  return 0;
159 }
160 
162  const oriented_bbox*obbox, const double device_size[2], cairo_matrix_t*m) {
163 
164  cairo_matrix_init_identity(m);
165 
166  double scale[2] = {
167  device_size[0] / obbox->size[0],
168  device_size[1] / obbox->size[1]};
169 
170  double s = scale[0] < scale[1] ? scale[0] : scale[1];
171  cairo_matrix_scale(m, s, s);
172  cairo_matrix_scale(m, 1, -1);
173  cairo_matrix_rotate(m, -obbox->pose[2]);
174  cairo_matrix_translate(m, -obbox->pose[0], -obbox->pose[1]);
175 
176 }
177 
178 void item_to_world(const double pose[3], cairo_matrix_t*m) {
179  cairo_matrix_init_identity(m);
180  cairo_matrix_translate(m, pose[0], pose[1]);
181  cairo_matrix_rotate(m, pose[2]);
182 }
183 
185  int k;
186  oriented_bbox global;
187 
188  if(0) {
189  bbfind * bbf = bbfind_new();
190  int n = p->scans_num;
191 /* if(n>20) n = 20;*/
192 
193  for(k=0;k<n;k++) {
194  GooLaserData * gld = (GooLaserData*) p->scans_items[k];
195  bbfind_add_bbox(bbf, &gld->obbox);
196  }
197 
198 
199  if(bbfind_compute(bbf, &global)) {
200  sm_debug("%d Global: %s size %f %f\n", p->scans_num, friendly_pose(global.pose),
201  global.size[0], global.size[1]);
202  } else {
203  sm_error("%d Could not compute global bounding box.\n", p->scans_num);
204  }
205  bbfind_free(bbf);
206  }else{
207 
208  global.pose[0] = -22;
209  global.pose[1] = -41;
210  global.pose[2] = M_PI/2;
211  global.size[0] = 106;
212  global.size[1] = 46;
213 }
214  cairo_matrix_t m_world_to_viewport;
215  world_to_viewport(&global, p->device_size, &(m_world_to_viewport));
216 
217  cairo_matrix_t *m = &m_world_to_viewport;
218  sm_info("Matrix: %f %f %f %f %f %f\n", m->xx,m->yx,m->xy,m->yy, m->x0, m->y0);
219 
220  cairo_matrix_t mm;
221  cairo_matrix_init_identity(&mm);
222 /* cairo_matrix_translate(&mm, 300, 600);*/
223  cairo_matrix_translate(&mm, 300, 600);
224  cairo_matrix_rotate(&mm, deg2rad(p->scans_num));
225  goo_canvas_item_set_transform(rect_item, &mm);
226 
227  /*
228  goo_canvas_item_set_transform(text_item, &m_world_to_viewport);
229 */
230  for(k=0;k<p->scans_num;k++) {
231  GooCanvasItem * gld = p->scans_items[k];
232 
233  double * pose = p->scans[k]->estimate;
234  cairo_matrix_t m_item_to_world;
235  item_to_world(pose, &m_item_to_world);
236 
237  cairo_matrix_t transform;
238  cairo_matrix_multiply(&transform, &m_item_to_world, &m_world_to_viewport);
239  goo_canvas_item_set_transform(gld, &transform);
240  }
241 }
242 
243 /* This handles button presses in item views. We simply output a message to
244  the console. */
245 static gboolean
246 on_rect_button_press (GooCanvasItem *item,
247  GooCanvasItem *target,
248  GdkEventButton *event,
249  gpointer data)
250 {
251  g_print ("rect item received button press event\n");
252  return TRUE;
253 }
254 
255 
256 /* This is our handler for the "delete-event" signal of the window, which
257  is emitted when the 'x' close button is clicked. We just exit here. */
258 static gboolean
259 on_delete_event (GtkWidget *window,
260  GdkEvent *event,
261  gpointer unused_data)
262 {
263  exit (0);
264 }
void * reading_thread(void *data)
Definition: gtk_viewer1.c:116
line_style points
#define deg2rad(x)
Definition: gpc_test.c:22
void sm_set_program_name(const char *name)
Definition: logging.c:21
void ls_add_options(line_style *ls, struct option *ops, const char *prefix, const char *desc_prefix)
void lds_add_options(ld_style *lds, struct option *ops, const char *prefix, const char *desc_prefix)
const char * input_filename
Definition: gtk_viewer.h:15
#define TRUE
Definition: json_object.h:21
line_style countour
static gboolean on_delete_event(GtkWidget *window, GdkEvent *event, gpointer unused_data)
Definition: gtk_viewer1.c:259
const char * friendly_pose(const double *pose)
Definition: math_utils.c:266
line_style rays
void compute_transformations(viewer_params *p)
Definition: gtk_viewer1.c:184
double device_size[2]
Definition: gtk_viewer.h:24
const char * color
struct option * ops
Definition: rb_sm.c:31
int main(int argc, char **argv)
Definition: gtk_viewer1.c:20
LDP ld_read_smart(FILE *)
#define M_PI
Definition: math_utils.h:7
void item_to_world(const double pose[3], cairo_matrix_t *m)
Definition: gtk_viewer1.c:178
Definition: options.h:49
ld_style laser
Definition: gtk_viewer.h:10
#define m(v1, v2)
Definition: egsl_macros.h:13
line_style normals
int bbfind_add_bbox(bbfind *bbf, const BB2 bbox)
#define FALSE
Definition: json_object.h:18
void bbfind_free(bbfind *bbf)
struct option * options_allocate(int n)
struct @0 p
bbfind * bbfind_new(void)
struct GooLaserData ** scans_items
Definition: gtk_viewer.h:20
void world_to_viewport(const oriented_bbox *obbox, const double device_size[2], cairo_matrix_t *m)
Definition: gtk_viewer1.c:161
oriented_bbox obbox
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
GooCanvasItem * text_item
Definition: gtk_viewer1.c:17
GooCanvas * canvas
Definition: gtk_viewer.h:27
GooCanvasItem * rect_item
Definition: gtk_viewer1.c:18
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
void ls_set_defaults(line_style *ls)
double estimate[3]
Definition: laser_data.h:40
void sm_debug(const char *msg,...)
Definition: logging.c:88
void transform(const gsl_vector *p, const gsl_vector *x, gsl_vector *res)
Definition: math_utils_gsl.c:3
const char * use
Definition: gtk_viewer.h:13
void sm_info(const char *msg,...)
Definition: logging.c:71
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
line_style pose_path
Definition: gtk_viewer.h:11
int bbfind_compute(bbfind *bbf, BB2 bbox)
static gboolean on_rect_button_press(GooCanvasItem *view, GooCanvasItem *target, GdkEventButton *event, gpointer data)
Definition: gtk_viewer1.c:246
void sm_error(const char *msg,...)
Definition: logging.c:49
GooCanvasItem * root
Definition: gtk_viewer.h:26
void lds_set_defaults(ld_style *lds)
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66
GooCanvasItem * goo_laser_data_new(GooCanvasItem *parent, viewer_params *p, LDP ld)


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23