bugrepair: drag and drop
[gpiv.git] / src / display_scalars.c
blob33f62804be25478fd1b0fa3a7a17a60833bae569
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*----------------------------------------------------------------------
5 gpiv - Graphic program for Particle Image Velocimetry, based on gtk/gnome
6 libraries.
8 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
9 Gerber van der Graaf
11 This file is part of gpiv.
13 Gpiv is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
16 any later version.
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 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software Foundation,
25 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ----------------------------------------------------------------------*/
29 #include "gpiv_gui.h"
30 #include "display_scalars.h"
32 #ifdef CANVAS_AA
33 static guint
34 create_scalar_color (gint flag,
35 gfloat sc_min,
36 gfloat sc_max,
37 gfloat sc,
38 gfloat scale_factor
41 #else /* CANVAS_AA */
43 static guint
44 create_scalar_color (gint flag,
45 gfloat sc_min,
46 gfloat sc_max,
47 gfloat sc,
48 gfloat scale_factor
50 #endif /* CANVAS_AA */
53 static void
54 destroy_filledrect (GnomeCanvasItem *filled_rect
57 static void
58 show_filledrect (GnomeCanvasItem *filled_rect
61 static void
62 hide_filledrect (GnomeCanvasItem *filled_rect
65 static void
66 create_vor (Display *disp,
67 gint i,
68 gint j,
69 guint col_val
72 static void
73 create_sstrain (Display *disp,
74 gint i,
75 gint j,
76 guint col_val
79 static void
80 create_nstrain (Display *disp,
81 gint i,
82 gint j,
83 guint col_val
87 * Public functions
89 void
90 create_all_scalars(Display *disp,
91 gint type
93 /* ----------------------------------------------------------------------------
94 * Displays all scalar gnome canvas items
97 int i, j;
98 int nx = 0, ny = 0, **flag = NULL;
100 float **sc = NULL;
101 float sc_min = 10000.0, sc_max = -10000.0;
102 guint col_val = 0;
103 GpivScalarData *scalar_data;
104 GtkWidget *view_piv_display0 =
105 gtk_object_get_data(GTK_OBJECT(disp->mwin),
106 "view_piv_display0");
107 GtkWidget * view_piv_display1 =
108 gtk_object_get_data(GTK_OBJECT(disp->mwin),
109 "view_piv_display1");
110 float scale_factor = 0.0;
113 if (type == GPIV_VORTICITY) {
114 scalar_data = disp->pida->vor_data;
115 sc = scalar_data->scalar;
116 nx = disp->pida->vor_data->nx;
117 ny = disp->pida->vor_data->ny;
118 flag = disp->pida->vor_data->flag;
119 /* scalar_colval(disp); */
121 } else if (type == GPIV_S_STRAIN) {
122 scalar_data = disp->pida->sstrain_data;
123 sc = scalar_data->scalar;
124 nx = disp->pida->sstrain_data->nx;
125 ny = disp->pida->sstrain_data->ny;
126 flag = disp->pida->sstrain_data->flag;
128 } else if (type == GPIV_N_STRAIN) {
129 scalar_data = disp->pida->nstrain_data;
130 sc = scalar_data->scalar;
131 nx = disp->pida->nstrain_data->nx;
132 ny = disp->pida->nstrain_data->ny;
133 flag = disp->pida->nstrain_data->flag;
134 } else {
135 g_warning(_("create_all_scalars: unexisting type"));
139 * normalizing data between 0 and 1 to define color value col_val
141 /* scalar_colval(Display * disp) { */
142 for (i = 0; i < ny; i++) {
143 for (j = 0; j < nx; j++) {
144 if (flag[i][j] != -1) {
145 if (sc[i][j] < sc_min) sc_min = sc[i][j];
146 if (sc[i][j] > sc_max) sc_max = sc[i][j];
151 if (sc_min < 0) {
152 if (-sc_min >= sc_max) {
153 scale_factor = 1.0 / -sc_min;
154 } else {
155 scale_factor = 1.0 / sc_max;
157 } else {
158 scale_factor = 1.0 / sc_max;
162 for (i = 0; i < ny; i++) {
163 for (j = 0; j < nx; j++) {
164 col_val = create_scalar_color(flag[i][j], sc_min, sc_max,
165 sc[i][j], scale_factor);
166 if (type == GPIV_VORTICITY) {
167 create_vor(disp, i, j, col_val);
168 } else if (type == GPIV_S_STRAIN) {
169 create_sstrain(disp, i, j, col_val);
170 } else if (type == GPIV_N_STRAIN) {
171 create_nstrain(disp, i, j, col_val);
172 } else {
173 g_warning(_("create_all_scalars: unexisting type"));
178 if (GTK_CHECK_MENU_ITEM(view_piv_display0)->active) {
179 for (i = 0; i < ny; i++) {
180 for (j = 0; j < nx; j++) {
181 gnome_canvas_item_raise_to_top
182 (disp->intreg->gci_intreg1[i][j]);
183 gnome_canvas_item_raise_to_top
184 (display_act->intreg->gci_intreg2[i][j]);
189 if (GTK_CHECK_MENU_ITEM(view_piv_display1)->active) {
190 if (disp->pida->exist_piv && disp->display_piv) {
191 for (i = 0; i < ny; i++) {
192 for (j = 0; j < nx; j++) {
193 gnome_canvas_item_raise_to_top
194 (disp->pida->gci_vector[i][j]);
204 void
205 show_all_scalars(Display *disp,
206 gint type
208 /* ----------------------------------------------------------------------------
209 * Shows scalar gnome canvas items
212 guint i = 0, j = 0;
213 guint nx = 0, ny = 0;
215 if (disp->pida->vor_data == NULL) return;
217 if (type == GPIV_VORTICITY
218 && disp->pida->vor_data != NULL) {
219 nx = disp->pida->vor_data->nx;
220 ny = disp->pida->vor_data->ny;
221 for (i = 0; i < ny; i++) {
222 for (j = 0; j < nx; j++) {
223 /* if (i ==0 && j==0) */
224 show_filledrect(display_act->pida->gci_scalar_vor[i][j]);
227 display_act->display_scalar = SHOW_SC_VORTICITY;
230 if (type == GPIV_S_STRAIN
231 && disp->pida->sstrain_data != NULL) {
232 nx = disp->pida->sstrain_data->nx;
233 ny = disp->pida->sstrain_data->ny;
234 for (i = 0; i < ny; i++) {
235 for (j = 0; j < nx; j++) {
236 show_filledrect(display_act->pida->gci_scalar_sstrain[i][j]);
239 display_act->display_scalar = SHOW_SC_SSTRAIN;
242 if (type == GPIV_N_STRAIN
243 && disp->pida->nstrain_data != NULL) {
244 nx = disp->pida->nstrain_data->nx;
245 ny = disp->pida->nstrain_data->ny;
246 for (i = 0; i < ny; i++) {
247 for (j = 0; j < nx; j++) {
248 show_filledrect(display_act->pida->gci_scalar_nstrain[i][j]);
251 display_act->display_scalar = SHOW_SC_NSTRAIN;
258 void
259 hide_all_scalars (Display *disp,
260 gint type
262 /* ----------------------------------------------------------------------------
263 * Hides all scalar gnome canvas items
266 guint i, j;
267 /* int nx = disp->pida->scalar_data->nx, ny = disp->pida->scalar_data->ny; */
270 if (disp->pida->vor_data == NULL) return;
272 if (type == GPIV_VORTICITY
273 && disp->pida->vor_data != NULL) {
274 for (i = 0; i < disp->pida->vor_data->ny; i++) {
275 for (j = 0; j < disp->pida->vor_data->nx; j++) {
276 /* if (i ==0 && j==0) */
277 hide_filledrect(disp->pida->gci_scalar_vor[i][j]);
280 disp->display_scalar = SHOW_SC_NONE;
283 if (type == GPIV_S_STRAIN
284 && disp->pida->sstrain_data != NULL) {
285 for (i = 0; i < disp->pida->sstrain_data->ny; i++) {
286 for (j = 0; j < disp->pida->sstrain_data->nx; j++) {
287 hide_filledrect(disp->pida->gci_scalar_sstrain[i][j]);
290 disp->display_scalar = SHOW_SC_NONE;
293 if (type == GPIV_N_STRAIN
294 && disp->pida->nstrain_data != NULL) {
295 for (i = 0; i < disp->pida->nstrain_data->ny; i++) {
296 for (j = 0; j < disp->pida->nstrain_data->nx; j++) {
297 hide_filledrect(disp->pida->gci_scalar_nstrain[i][j]);
300 disp->display_scalar = SHOW_SC_NONE;
308 void
309 destroy_all_scalars(Display *disp,
310 gint type
312 /* ----------------------------------------------------------------------------
313 * Destroys scalar canvas items
316 guint i, j;
319 if (disp->pida->vor_data == NULL) return;
321 if (type == GPIV_VORTICITY
322 && disp->pida->vor_data != NULL) {
323 for (i = 0; i < disp->pida->vor_data->ny; i++) {
324 for (j = 0; j < disp->pida->vor_data->nx; j++) {
325 /* if (i ==0 && j==0) */
326 destroy_filledrect(disp->pida->gci_scalar_vor[i][j]);
330 } else if (type == GPIV_S_STRAIN
331 && disp->pida->sstrain_data != NULL) {
332 for (i = 0; i < disp->pida->sstrain_data->ny; i++) {
333 for (j = 0; j < disp->pida->sstrain_data->nx; j++) {
334 destroy_filledrect(disp->pida->gci_scalar_sstrain[i][j]);
338 } else if (type == GPIV_N_STRAIN
339 && disp->pida->nstrain_data != NULL) {
340 for (i = 0; i < disp->pida->nstrain_data->ny; i++) {
341 for (j = 0; j < disp->pida->nstrain_data->nx; j++) {
342 destroy_filledrect(disp->pida->gci_scalar_nstrain[i][j]);
345 } else {
346 g_warning(_("destroy_all_scalars: unexisting type"));
354 * Local functions
356 #ifdef CANVAS_AA
358 * BUGFIX repair color representation for canvas_aa
361 static guint
362 create_scalar_color (gint flag,
363 gfloat sc_min,
364 gfloat sc_max,
365 gfloat sc,
366 gfloat scale_factor
368 /* ----------------------------------------------------------------------------
369 * Create scalar color for in canvas
372 guint color = 0;
373 GdkColor *color_val = NULL;
375 color_val = (GdkColor *)g_malloc(sizeof(GdkColor));
376 if (flag != -1) {
377 if (sc_min < 0) {
378 if (sc <0) {
379 color_val->red = (gint) (-sc * BYTEVAL * scale_factor);
380 color_val->green = 0;
381 color_val->blue = 0;
382 } else {
383 color_val->red = 0;
384 color_val->green = 0;
385 color_val->blue = (int) (sc * BYTEVAL * scale_factor);
387 } else {
388 color_val->red = 0;
389 color_val->green = 0;
390 color_val->blue = (int) (sc * BYTEVAL * scale_factor);
392 } else {
393 color_val->red = 0;
394 color_val->green = BYTEVAL / 2;
395 color_val->blue = 0;
398 color = GNOME_CANVAS_COLOR(color_val->red,
399 color_val->green,
400 color_val->blue);
401 g_free(color_val);
402 return color;
406 #else /* CANVAS_AA */
408 static guint
409 create_scalar_color (gint flag,
410 gfloat sc_min,
411 gfloat sc_max,
412 gfloat sc,
413 gfloat scale_factor
415 /* ----------------------------------------------------------------------------
416 * Create scalar color for in canvas
419 guint color_val = 0;
421 if (flag != -1) {
422 if (sc_min < 0) {
423 if (sc <0) {
424 color_val = (int) (-sc * BYTEVAL * scale_factor);
425 color_val = (color_val << BITSHIFT_RED);
426 } else {
427 color_val = (int) (sc * BYTEVAL * scale_factor);
428 color_val = (color_val << BITSHIFT_BLUE);
430 } else {
431 color_val = (int) (sc * BYTEVAL * scale_factor);
432 color_val = (color_val << BITSHIFT_BLUE);
434 } else {
435 color_val = 128 << BITSHIFT_GREEN;
439 return color_val;
443 #endif /* CANVAS_AA */
446 static void
447 destroy_filledrect(GnomeCanvasItem *filled_rect
449 /* ----------------------------------------------------------------------------
450 * Destroys a single filled rectangular canvas item
453 if (filled_rect != NULL) {
454 gtk_object_destroy(GTK_OBJECT(filled_rect));
455 filled_rect = NULL;
461 static void
462 show_filledrect(GnomeCanvasItem *filled_rect
464 /* ----------------------------------------------------------------------------
465 * Shows a single filled rectangular canvas item
468 if (filled_rect != NULL) {
469 gnome_canvas_item_show
470 (GNOME_CANVAS_ITEM(filled_rect));
476 static void
477 hide_filledrect(GnomeCanvasItem *filled_rect
479 /* ----------------------------------------------------------------------------
480 * Hides a single filled rectangular canvas item
483 if (filled_rect != NULL) {
484 gnome_canvas_item_hide
485 (GNOME_CANVAS_ITEM(filled_rect));
491 static void
492 create_vor (Display *disp,
493 gint i,
494 gint j,
495 guint col_val
497 /* ----------------------------------------------------------------------------
498 * Creates vorticity gnome canvas item by coloring the interrogation area
501 float **x, **y;
502 int start_x, start_y, end_x, end_y;
504 GnomeCanvasPoints *points;
505 points = gnome_canvas_points_new(5);
509 * Using centre points of interr regs
511 x = disp->pida->vor_data->point_x;
512 y = disp->pida->vor_data->point_y;
513 start_x = (int) x[i][j] - disp->pida->piv_par->int_size_f / 2;
514 start_y = (int) y[i][j] - disp->pida->piv_par->int_size_f / 2;
515 end_x = (int) x[i][j] + disp->pida->piv_par->int_size_f / 2;
516 end_y = (int) y[i][j] + disp->pida->piv_par->int_size_f / 2;
518 disp->pida->gci_scalar_vor[i][j] =
519 gnome_canvas_item_new(gnome_canvas_root
520 (GNOME_CANVAS(display_act->canvas)),
521 gnome_canvas_rect_get_type(),
522 "x1", (double) start_x,
523 "y1", (double) start_y,
524 "x2", (double) end_x,
525 "y2", (double) end_y,
526 "fill_color_rgba", col_val,
527 NULL);
529 gnome_canvas_points_free(points);
534 static void
535 create_sstrain (Display *disp,
536 gint i,
537 gint j,
538 guint col_val
540 /* ----------------------------------------------------------------------------
541 * Creates shear strain gnome canvas item by coloring the interrogation area
544 float **x, **y;
545 int start_x, start_y, end_x, end_y;
547 GnomeCanvasPoints *points;
548 points = gnome_canvas_points_new(5);
552 * Using centre points of interr regs
554 x = disp->pida->sstrain_data->point_x;
555 y = disp->pida->sstrain_data->point_y;
556 start_x = (int) x[i][j] - disp->pida->piv_par->int_size_f / 2;
557 start_y = (int) y[i][j] - disp->pida->piv_par->int_size_f / 2;
558 end_x = (int) x[i][j] + disp->pida->piv_par->int_size_f / 2;
559 end_y = (int) y[i][j] + disp->pida->piv_par->int_size_f / 2;
561 disp->pida->gci_scalar_sstrain[i][j] =
562 gnome_canvas_item_new(gnome_canvas_root
563 (GNOME_CANVAS(display_act->canvas)),
564 gnome_canvas_rect_get_type(),
565 "x1", (double) start_x,
566 "y1", (double) start_y,
567 "x2", (double) end_x,
568 "y2", (double) end_y,
569 "fill_color_rgba", col_val,
570 NULL);
572 gnome_canvas_points_free(points);
577 static void
578 create_nstrain (Display *disp,
579 gint i,
580 gint j,
581 guint col_val
583 /* ----------------------------------------------------------------------------
584 * Creates normal strain gnome canvas item by coloring the interrogation area
587 float **x, **y;
588 int start_x, start_y, end_x, end_y;
590 GnomeCanvasPoints *points;
591 points = gnome_canvas_points_new(5);
595 * Using centre points of interr regs
598 x = disp->pida->nstrain_data->point_x;
599 y = disp->pida->nstrain_data->point_y;
600 start_x = (int) x[i][j] - disp->pida->piv_par->int_size_f / 2;
601 start_y = (int) y[i][j] - disp->pida->piv_par->int_size_f / 2;
602 end_x = (int) x[i][j] + disp->pida->piv_par->int_size_f / 2;
603 end_y = (int) y[i][j] + disp->pida->piv_par->int_size_f / 2;
605 disp->pida->gci_scalar_nstrain[i][j] =
606 gnome_canvas_item_new(gnome_canvas_root
607 (GNOME_CANVAS(display_act->canvas)),
608 gnome_canvas_rect_get_type(),
609 "x1", (double) start_x,
610 "y1", (double) start_y,
611 "x2", (double) end_x,
612 "y2", (double) end_y,
613 "fill_color_rgba", col_val,
614 NULL);
616 gnome_canvas_points_free(points);