updated copyright statement
[gpiv.git] / src / display_piv.c
blob7ae888b5799cd01b8161e7c8b0d6b1a9ddb432b4
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_piv.h"
33 static guint32
34 create_vector_color (gint peak_no,
35 gfloat snr,
36 gfloat dl
40 gfloat
41 dxdy_min (GpivPivData *piv_data
43 /*-----------------------------------------------------------------------------
44 * Calculates maximum particle displacement from dx and dy of a piv data-set
47 guint i, j;
48 gfloat dl_abs = 0.0, dl_min = 10.0e+9;
51 /* assert (piv_data->nx != 0); */
52 /* assert (piv_data->ny != 0); */
54 if (piv_data == NULL) {
55 gpiv_error ("dxdy_min: piv_data == NULL");
58 for (i = 0; i < piv_data->ny; i++) {
59 for (j = 0; j < piv_data->nx; j++) {
60 if (piv_data->peak_no[i][j] >= 1) {
61 dl_abs = sqrt (piv_data->dx[i][j] * piv_data->dx[i][j] +
62 piv_data->dy[i][j] * piv_data->dy[i][j]);
63 if (dl_abs < dl_min) {
64 dl_min = dl_abs;
71 return dl_min;
76 gfloat
77 dxdy_max (GpivPivData *piv_data
79 /*-----------------------------------------------------------------------------
80 * Calculates maximum particle displacement from dx and dy of a piv data-set
83 guint i, j;
84 gfloat dl_abs = 0.0, dl_max = 0.0;
86 /* g_message ("dxdy_max:: 0 nx = %d ny = %d", */
87 /* piv_data->nx, piv_data->ny); */
88 if (piv_data == NULL) {
89 gpiv_error ("dxdy_max: piv_data == NULL");
92 for (i = 0; i < piv_data->ny; i++) {
93 for (j = 0; j < piv_data->nx; j++) {
94 if (piv_data->peak_no[i][j] >= 1) {
95 dl_abs = sqrt (piv_data->dx[i][j] * piv_data->dx[i][j] +
96 piv_data->dy[i][j] * piv_data->dy[i][j]);
97 if (dl_abs > dl_max) {
98 dl_max = dl_abs;
104 return dl_max;
109 void
110 create_vector (PivData *pida,
111 guint i,
112 guint j
114 /* ----------------------------------------------------------------------------
115 * Displays a single PIV vector on a Gnome canvas
118 GnomeCanvasPoints *points = NULL;
119 Display *disp = display_act;
121 gfloat **point_x = NULL;
122 gfloat **point_y = NULL;
123 gfloat **dx = NULL, **dy = NULL;
124 gint **peak_no = NULL;
125 gfloat **snr = NULL;
126 gfloat dl = 0.0;
127 guint32 color_val = 0;
130 if (pida->piv_data == NULL) return;
131 point_x = pida->piv_data->point_x;
132 point_y = pida->piv_data->point_y;
133 dx = pida->piv_data->dx;
134 dy = pida->piv_data->dy;
135 peak_no = pida->piv_data->peak_no;
136 snr = pida->piv_data->snr;
138 dl = sqrt (dx[i][j] * dx[i][j] + dy[i][j] * dy[i][j]);
139 points = gnome_canvas_points_new (2);
143 * Fill out the points
146 points->coords[0] = point_x[i][j];
147 points->coords[1] = point_y[i][j];
148 points->coords[2] = point_x[i][j] + dx[i][j] * gpiv_par->display__vector_scale;
149 points->coords[3] = point_y[i][j] + dy[i][j] * gpiv_par->display__vector_scale;
152 color_val = create_vector_color (peak_no[i][j], snr[i][j], dl);
154 if (pida->gci_vector[i][j] != NULL) {
155 /* g_warning ("create_vector:: gci_vector[%d][%d] != NULL ==> destroying", */
156 /* i, j); */
157 destroy_vector (pida, i, j);
160 pida->gci_vector[i][j] =
161 gnome_canvas_item_new (gnome_canvas_root
162 (GNOME_CANVAS (disp->canvas)),
163 gnome_canvas_line_get_type (),
164 "points", points,
165 "fill_color_rgba", color_val,
166 "width_units", (double) THICKNESS,
167 "last_arrowhead", TRUE,
168 "arrow_shape_a", (double) ARROW_LENGTH *
169 ARROW_FACT * dl * gpiv_par->display__vector_scale +
170 ARROW_ADD,
171 "arrow_shape_b", (double) ARROW_EDGE *
172 ARROW_FACT * dl * gpiv_par->display__vector_scale +
173 ARROW_ADD,
174 "arrow_shape_c", (double) ARROW_WIDTH *
175 ARROW_FACT * dl * gpiv_par->display__vector_scale +
176 ARROW_ADD,
177 NULL);
179 gnome_canvas_points_free (points);
185 void
186 update_vector (PivData *pida,
187 guint i,
188 guint j
190 /* ----------------------------------------------------------------------------
191 * Updates a single PIV vector on a Gnome canvas
194 GnomeCanvasPoints *points;
196 gfloat **point_x = NULL, **point_y = NULL;
197 gfloat **dx = NULL, **dy = NULL;
198 gfloat **snr = NULL;
199 gint **peak_no = NULL;
200 gfloat dl = 0.0;
201 guint32 color_val = 0;
204 if (pida->piv_data == NULL) return;
205 point_x = pida->piv_data->point_x;
206 point_y = pida->piv_data->point_y;
207 dx = pida->piv_data->dx;
208 dy = pida->piv_data->dy;
209 peak_no = pida->piv_data->peak_no;
210 snr = pida->piv_data->snr;
212 dl = sqrt (dx[i][j] * dx[i][j] + dy[i][j] * dy[i][j]);
213 points = gnome_canvas_points_new (2);
216 * Fill out the points
218 points->coords[0] = point_x[i][j];
219 points->coords[1] = point_y[i][j];
220 points->coords[2] = point_x[i][j] + dx[i][j] * gpiv_par->display__vector_scale;
221 points->coords[3] = point_y[i][j] + dy[i][j] * gpiv_par->display__vector_scale;
223 color_val = create_vector_color (peak_no[i][j], snr[i][j], dl);
225 if (pida->gci_vector[i][j] != NULL) {
226 gnome_canvas_item_set (GNOME_CANVAS_ITEM (pida->gci_vector[i][j]),
227 "points", points,
228 "fill_color_rgba", color_val,
229 "width_units", (double) THICKNESS,
230 "last_arrowhead", TRUE,
231 "arrow_shape_a", (double) ARROW_LENGTH *
232 ARROW_FACT * dl * gpiv_par->display__vector_scale
233 + ARROW_ADD,
234 "arrow_shape_b", (double) ARROW_EDGE *
235 ARROW_FACT * dl * gpiv_par->display__vector_scale
236 + ARROW_ADD,
237 "arrow_shape_c", (double) ARROW_WIDTH *
238 ARROW_FACT * dl * gpiv_par->display__vector_scale
239 + ARROW_ADD,
240 NULL);
243 gnome_canvas_points_free (points);
248 void
249 destroy_vector (PivData *pida,
250 guint i,
251 guint j
253 /* ----------------------------------------------------------------------------
254 * Detroys a single PIV vector on a Gnome canvas
257 if (pida->gci_vector[i][j] != NULL) {
258 gtk_object_destroy (GTK_OBJECT (pida->gci_vector[i][j]));
259 pida->gci_vector[i][j] = NULL;
265 void
266 create_all_vectors (PivData *pida
268 /* ---------------------------------------------------------------------------
269 * Displays all PIV vectors on a Gnome canvas
272 guint i, j;
273 guint nx = 0, ny = 0;
276 if (pida->piv_data == NULL) return;
278 nx = pida->piv_data->nx;
279 ny = pida->piv_data->ny;
281 if (pida->exist_vec) {
282 destroy_all_vectors (pida);
285 gpiv_var->dl_max = dxdy_max (pida->piv_data);
286 gpiv_var->dl_min = dxdy_min (pida->piv_data);
287 for (i = 0; i < ny; i++) {
288 for (j = 0; j < nx; j++) {
289 create_vector (pida, i, j);
294 pida->exist_vec = TRUE;
299 void
300 show_all_vectors (PivData *pida
302 /* ----------------------------------------------------------------------------
303 * Shows all PIV vectors on a Gnome canvas
306 guint i, j;
307 guint nx = 0, ny = 0;
310 if (pida->piv_data == NULL) return;
311 nx = pida->piv_data->nx;
312 ny = pida->piv_data->ny;
314 for (i = 0; i < ny; i++) {
315 for (j = 0; j < nx; j++) {
316 if (pida->gci_vector[i][j] != NULL) {
317 gnome_canvas_item_show (GNOME_CANVAS_ITEM
318 (pida->gci_vector[i][j]));
326 void
327 hide_all_vectors (PivData *pida
329 /* ----------------------------------------------------------------------------
330 * Hides all PIV vectors on a Gnome canvas
333 guint i, j;
334 guint nx = 0, ny = 0;
337 if (pida->piv_data == NULL) return;
338 nx = pida->piv_data->nx;
339 ny = pida->piv_data->ny;
342 for (i = 0; i < ny; i++) {
343 for (j = 0; j < nx; j++) {
344 if (pida->gci_vector[i][j] != NULL) {
345 gnome_canvas_item_hide (GNOME_CANVAS_ITEM
346 (pida->gci_vector[i][j]));
354 void
355 update_all_vectors (PivData *pida
357 /* ----------------------------------------------------------------------------
358 * Scales PIV vectors for Gnome canvas
361 guint i, j;
362 guint nx = 0, ny = 0;
365 if (pida->piv_data == NULL) return;
366 nx = pida->piv_data->nx;
367 ny = pida->piv_data->ny;
369 gpiv_var->dl_max = dxdy_max (pida->piv_data);
370 gpiv_var->dl_min = dxdy_min (pida->piv_data);
371 for (i = 0; i < ny; i++) {
372 for (j = 0; j < nx; j++) {
373 update_vector (pida, i, j);
380 void
381 destroy_all_vectors (PivData *pida
383 /* ----------------------------------------------------------------------------
384 * Destroys all PIV vectors on a Gnome canvas
387 guint i, j;
388 guint nx = 0, ny = 0;
391 if (pida->piv_data == NULL) return;
392 nx = pida->piv_data->nx;
393 ny = pida->piv_data->ny;
395 if (pida->exist_vec) {
396 for (i = 0; i < ny; i++) {
397 for (j = 0; j < nx; j++) {
398 destroy_vector (pida, i, j);
401 /* } else { */
402 /* g_warning ("destroy_all_vectors: exist_vec = FALSE"); */
404 pida->exist_vec = FALSE;
409 * Local functions
411 #ifdef CANVAS_AA
412 static guint32
413 create_vector_color (gint peak_no,
414 gfloat snr,
415 gfloat dl
417 /* ----------------------------------------------------------------------------
418 * Create vector color for in canvas
421 guint32 color = 0;
422 GdkColor *color_val = NULL;
423 guint shift_factor;
424 Display *disp = display_act;
426 color_val = (GdkColor *)g_malloc (sizeof (GdkColor));
428 if (gpiv_par->display__vector_color == SHOW_PEAKNR) {
429 if (peak_no == -1) {
430 color_val->red = BYTEVAL;
431 color_val->green = 0;
432 color_val->blue = 0;
434 } else if (peak_no == 0) {
435 color_val->red = 0;
436 color_val->green = 0;
437 color_val->blue = BYTEVAL;
439 } else if (peak_no == 1) {
440 color_val->red = 0;
441 color_val->green = BYTEVAL;
442 color_val->blue = 0;
444 } else if (peak_no == 2) {
445 color_val->red = 0;
446 color_val->green = BYTEVAL;
447 color_val->blue = BYTEVAL;
449 } else {
450 /* if (peak_no[i][j] == 3) */
451 color_val->red = BYTEVAL / 2;
452 color_val->green = BYTEVAL / 2;
453 color_val->blue = BYTEVAL / 2;
457 color = GNOME_CANVAS_COLOR (color_val->red,
458 color_val->green,
459 color_val->blue);
461 } else if (gpiv_par->display__vector_color == SHOW_SNR) {
462 if (snr >= disp->pida->valid_par->residu_max) {
463 /* color = "red"; */
464 color_val->red = BYTEVAL;
465 color_val->green = 0;
466 color_val->blue = 0;
467 } else {
468 /* color = "green"; */
469 color_val->red = 0;
470 color_val->green = BYTEVAL;
471 color_val->blue = 0;
474 color = GNOME_CANVAS_COLOR (color_val->red,
475 color_val->green,
476 color_val->blue);
478 } else if (gpiv_par->display__vector_color == SHOW_MAGNITUDE_GRAY) {
479 if (peak_no >= 1) {
480 color_val->red = (gint) (BYTEVAL * (dl - gpiv_var->dl_min) /
481 (gpiv_var->dl_max - gpiv_var->dl_min));
482 color_val->green = (gint) (BYTEVAL * (dl - gpiv_var->dl_min) /
483 (gpiv_var->dl_max - gpiv_var->dl_min));
484 color_val->blue = (gint) (BYTEVAL * (dl - gpiv_var->dl_min) /
485 (gpiv_var->dl_max - gpiv_var->dl_min));
486 } else {
487 color_val->red = 0;
488 color_val->green = 0;
489 color_val->blue = 0;
492 color = GNOME_CANVAS_COLOR (color_val->red,
493 color_val->green,
494 color_val->blue);
496 } else if (gpiv_par->display__vector_color == SHOW_MAGNITUDE) {
497 if (peak_no >= 1) {
498 shift_factor = (gint) (28.0 * (dl - gpiv_var->dl_min) /
499 (gpiv_var->dl_max - gpiv_var->dl_min));
500 color = (( 0xFFFF << shift_factor) | 0xFF);
503 } else {
504 color = GNOME_CANVAS_COLOR (0, 0, 0);
508 g_free (color_val);
509 return color;
513 #else /* CANVAS_AA */
514 static guint32
515 create_vector_color (gint peak_no,
516 gfloat snr,
517 gfloat dl
519 /* ----------------------------------------------------------------------------
520 * Create vector color for in canvas
523 guint32 color_val = 0;
524 gint shift_factor;
526 if (gpiv_par->display__vector_color == SHOW_PEAKNR) {
527 if (peak_no == -1) {
528 /* color = "red"; */
529 color_val = (gint) (BYTEVAL);
530 color_val = (color_val << BITSHIFT_RED);
531 } else if (peak_no == 0) {
532 /* color = "lightblue"; */
533 color_val = (gint) (BYTEVAL);
534 color_val = (color_val << BITSHIFT_BLUE);
535 } else if (peak_no == 1) {
536 /* color = "green"; */
537 color_val = (gint) (BYTEVAL);
538 color_val = (color_val << BITSHIFT_GREEN);
539 } else if (peak_no == 2) {
540 /* color = "yellow"; */
541 color_val = (gint) (BYTEVAL);
542 color_val = (color_val << BITSHIFT_GREEN)
543 + (color_val << BITSHIFT_BLUE);
544 } else {
545 /* if (peak_no == 3) */
546 /* color = "gray"; */
547 color_val = (gint) (127);
548 color_val = (color_val << BITSHIFT_RED)
549 + (color_val << BITSHIFT_GREEN)
550 + (color_val << BITSHIFT_BLUE);
554 } else if (gpiv_par->display__vector_color == SHOW_SNR) {
555 if (snr >= gl_valid_par->residu_max) {
556 /* color = "red"; */
557 color_val = (gint) (BYTEVAL);
558 color_val = (color_val << BITSHIFT_RED);
559 } else {
560 /* color = "green"; */
561 color_val = (gint) (BYTEVAL);
562 color_val = (color_val << BITSHIFT_GREEN);
565 } else if (gpiv_par->display__vector_color == SHOW_MAGNITUDE_GRAY) {
566 if (peak_no >= 1) {
567 color_val = (gint) (BYTEVAL * (dl - gpiv_var->dl_min) /
568 (gpiv_var->dl_max - gpiv_var->dl_min));
569 color_val = (color_val << BITSHIFT_RED) +
570 (color_val << BITSHIFT_GREEN) + (color_val << BITSHIFT_BLUE);
571 } else {
572 color_val = 0;
575 } else if (gpiv_par->display__vector_color == SHOW_MAGNITUDE) {
576 if (peak_no >= 1) {
577 shift_factor = (gint) (28.0 * (dl - gpiv_var->dl_min) /
578 (gpiv_var->dl_max - gpiv_var->dl_min));
579 color_val = ( 0xFFFF << shift_factor);
580 } else {
581 color_val = 0;
586 return color_val;
589 #endif /* CANVAS_AA */