1 /**********
2 *
3 * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
4 * University of Manchester, UK. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 *
9 * . Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * . Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * . Neither the name of the University of Manchester nor the names of its
17 * contributors may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
19 *
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 **********
34 *
35 * Program : TINA
36 * File : $Source: /home/tina/cvs/tina-tools/tinatool/draw/drawPlot_procs.c,v $
37 * Date : $Date: 2008/12/02 22:04:19 $
38 * Version : $Revision: 1.4 $
39 * CVS Id : $Id: drawPlot_procs.c,v 1.4 2008/12/02 22:04:19 paul Exp $
40 *
41 * Author : Legacy TINA
42 *
43 * Notes :
44 *
45 *********
46 */
47
48 #include "drawPlot_procs.h"
49
50 #if HAVE_CONFIG_H
51 #include <config.h>
52 #endif
53
54 #include <stdio.h>
55 #include <string.h>
56 #include <math.h>
57 #include <tina/sys/sysDef.h>
58 #include <tina/sys/sysPro.h>
59 #include <tina/math/mathDef.h>
60 #include <tina/geometry/geomDef.h>
61 #include <tinatool/draw/draw_TvDef.h>
62 #include <tinatool/draw/draw_TvPro.h>
63 #include <tinatool/draw/draw_PlotDef.h>
64 #include <tinatool/draw/draw_PlotPro.h>
65
66 void rfree();
67 Ipos ipos();
68 Vec2 vec2();
69 List *ref_addtostart();
70 double pl_get_pos();
71 Bool dofree = false;
72
73 static Tv *pl_tv;
74 static List *pl_list = NULL;
75 static Pl_graph pl_graph = {Pl_graph_id};
76 static Pl_ctr pl_ctr = {Pl_ctr_id};
77 static Pl_axes pl_axes = {Pl_axes_id};
78
79 static Bool pl_hscale;
80 static double pl_hmin, pl_hmax, pl_hinc;
81
82 void pl_free(void *pl, int type)
83 {
84 switch (type)
85 {
86 case PL_GRAPH_TYPE:
87 {
88 Pl_graph *graph = (Pl_graph *) pl;
89
90 if (graph->dofree)
91 {
92 fvector_free(graph->x, 0);
93 fvector_free(graph->y, 0);
94 }
95 rfree((void *) graph);
96 break;
97 }
98 case PL_SCATTER_TYPE:
99 {
100 Pl_graph *graph = (Pl_graph *) pl;
101
102 if (graph->dofree)
103 {
104 fvector_free(graph->x, 0);
105 fvector_free(graph->y, 0);
106 }
107 rfree((void *) graph);
108 break;
109 }
110 case PL_CTR_TYPE:
111 {
112 Pl_ctr *ctr = (Pl_ctr *) pl;
113
114 if (ctr->dofree)
115 {
116 fvector_free(ctr->x, 0);
117 fvector_free(ctr->y, 0);
118 farray_free(ctr->z, 0, 0, ctr->nx + 1, ctr->ny + 1);
119 }
120 rfree((void *) ctr);
121 break;
122 }
123 }
124 }
125
126 void pl_free_set(Bool set)
127 {
128 dofree = set;
129 }
130
131 void pl_init(void)
132 {
133 pl_graph.color = black;
134 pl_graph.style = PL_LINE;
135
136 pl_ctr.color = black;
137 pl_ctr.cscale = true;
138
139 pl_axes.color = black;
140 (void) strcpy(pl_axes.title, "");
141
142 pl_axes.xscale = true;
143 pl_axes.xaxis = true;
144 pl_axes.xtics = true;
145 pl_axes.xtext = true;
146
147 pl_axes.yscale = true;
148 pl_axes.yaxis = true;
149 pl_axes.ytics = true;
150 pl_axes.ytext = true;
151
152 pl_hscale = true;
153
154 list_rm(pl_list, pl_free);
155 pl_list = NULL;
156 }
157
158 void pl_set_tv(Tv * tv)
159 {
160 pl_tv = tv;
161 }
162
163 void pl_set_color(int color)
164 {
165 pl_graph.color = color;
166 }
167
168 void pl_set_axis_color(int color)
169 {
170 pl_axes.color = color;
171 }
172
173 void pl_set_style(int style)
174 {
175 pl_graph.style = style;
176 }
177
178 void pl_set_title(char *title)
179 {
180 (void) strcpy(pl_axes.title, title);
181 }
182
183 void pl_set_legend(char *legend)
184 {
185 Pl_graph *graph = pl_list->to;
186 (void) strcpy(graph->legend, legend);
187 }
188
189 void pl_set_x_axis(Bool set)
190 {
191 pl_axes.xaxis = set;
192 }
193
194 void pl_set_x_tics(Bool set)
195 {
196 pl_axes.xtics = set;
197 }
198
199 void pl_set_x_text(Bool set)
200 {
201 pl_axes.xtext = set;
202 }
203
204 void pl_set_x_range(double xmin, double xmax, double xinc)
205 {
206 pl_axes.xscale = false;
207 pl_axes.xmin = xmin;
208 pl_axes.xmax = xmax;
209 if(xinc == 0.0)
210 pl_scale(&pl_axes.xmin, &pl_axes.xmax, &pl_axes.xinc, &pl_axes.xpos);
211 else
212 {
213 pl_axes.xinc = xinc;
214 pl_axes.xpos = pl_get_pos(xmin, xmax);
215 }
216 }
217
218 void pl_set_y_axis(Bool set)
219 {
220 pl_axes.yaxis = set;
221 }
222
223 void pl_set_y_tics(Bool set)
224 {
225 pl_axes.ytics = set;
226 }
227
228 void pl_set_y_text(Bool set)
229 {
230 pl_axes.ytext = set;
231 }
232
233 void pl_set_y_range(double ymin, double ymax, double yinc)
234 {
235 pl_axes.yscale = false;
236 pl_axes.ymin = ymin;
237 pl_axes.ymax = ymax;
238 if(yinc == 0.0)
239 pl_scale(&pl_axes.ymin, &pl_axes.ymax, &pl_axes.yinc, &pl_axes.ypos);
240 else
241 {
242 pl_axes.yinc = yinc;
243 pl_axes.ypos = pl_get_pos(ymin, ymax);
244 }
245 }
246
247 void pl_set_hist_range(double hmin, double hmax, double hinc)
248 {
249 pl_hscale = false;
250 pl_hmin = hmin;
251 pl_hmax = hmax;
252 pl_hinc = hinc;
253 }
254
255 void pl_set_graph_data(int n, float *x, float *y)
256 {
257 Pl_graph *graph;
258
259 if (n <= 0 || x == NULL || y == NULL)
260 return;
261
262 graph = talloc(Pl_graph);
263
264 pl_graph.dofree = dofree;
265 pl_graph.n = n;
266 pl_graph.x = x;
267 pl_graph.y = y;
268 *graph = pl_graph;
269 strcpy(graph->legend, "");
270 pl_list = ref_addtostart(pl_list, (void *) graph, PL_GRAPH_TYPE);
271 }
272
273 void pl_set_scatt_data(int n, float *x, float *y)
274 {
275 Pl_graph *graph = ts_ralloc(Pl_graph);
276
277 pl_graph.dofree = dofree;
278 pl_graph.n = n;
279 pl_graph.x = x;
280 pl_graph.y = y;
281 strcpy(pl_graph.legend, "");
282 *graph = pl_graph;
283 pl_list = ref_addtostart(pl_list, (void *) graph, PL_SCATTER_TYPE);
284 }
285
286 void pl_set_ctr_range(double cmin, double cmax, double cinc)
287 {
288 pl_ctr.cscale = false;
289 pl_ctr.cmin = cmin;
290 pl_ctr.cmax = cmax;
291 pl_ctr.cinc = cinc;
292 }
293
294 void pl_set_ctr_data(int nx, float *x, int ny, float *y, float **z)
295 {
296 Pl_ctr *ctr = ts_ralloc(Pl_ctr);
297
298 pl_ctr.dofree = dofree;
299 pl_ctr.nx = nx;
300 pl_ctr.x = x;
301 pl_ctr.ny = ny;
302 pl_ctr.y = y;
303 pl_ctr.z = z;
304
305 *ctr = pl_ctr;
306 pl_list = ref_addtostart(pl_list, (void *) ctr, PL_CTR_TYPE);
307 }
308
309 void pl_set_hist_data(int nh, float *h)
310 {
311 int i, n, *bucket;
312 float *x, *y;
313 double fvector_min(), fvector_max();
314 Pl_graph *graph;
315
316 if (nh <= 0 || h == NULL)
317 return;
318
319 graph = ts_ralloc(Pl_graph);
320
321 if (pl_axes.xscale)
322 {
323 double inc, pos;
324
325 pl_axes.xmin = fvector_min(h, 0, nh);
326 pl_axes.xmax = fvector_max(h, 0, nh);
327 pl_scale(&pl_axes.xmin, &pl_axes.xmax, &inc, &pos);
328 pl_axes.xinc = (pl_axes.xmax - pl_axes.xmin) / 20;
329 }
330 if (pl_hscale)
331 {
332 pl_hmin = pl_axes.xmin;
333 pl_hmax = pl_axes.xmax;
334 pl_hinc = pl_axes.xinc;
335 }
336 if (!(pl_hinc < 0.0) && !(pl_hinc >= 0.0)) return;
337 n = (int)((pl_hmax - pl_hmin + pl_hinc / 2) / pl_hinc);
338 bucket = ivector_alloc(0, n);
339 x = fvector_alloc(0, 4 * n);
340 y = fvector_alloc(0, 4 * n);
341
342 for (i = 0; i < n; i++)
343 bucket[i] = 0;
344
345 for (i = 0; i < nh; i++)
346 {
347 int hit = (int) ((h[i] - pl_hmin) / pl_hinc);
348
349 if (0 <= hit && hit < n)
350 bucket[hit]++;
351 }
352
353 for (i = 0; i < n; i++)
354 {
355 x[4 * i] = x[4 * i + 1] = (float)(pl_hmin + i * pl_hinc);
356 x[4 * i + 2] = x[4 * i + 3] = (float)(x[4 * i] + pl_hinc);
357 y[4 * i] = y[4 * i + 3] = (float)0.0;
358 y[4 * i + 1] = y[4 * i + 2] = (float) bucket[i];
359 }
360
361 pl_graph.dofree = true;
362 pl_graph.n = 4 * n;
363 pl_graph.x = x;
364 pl_graph.y = y;
365
366 *graph = pl_graph;
367 pl_list = ref_addtostart(pl_list, (void *) graph, PL_GRAPH_TYPE);
368
369 ivector_free(bucket, 0);
370 }
371
372 void pl_set_hist_binned_data(void *ptr)
373 {
374 int i, n;
375 float *bucket;
376 float *x, *y;
377 Pl_graph *graph;
378 shistogram *h = (shistogram *)ptr;
379
380 if (h == NULL)
381 return;
382
383 graph = ts_ralloc(Pl_graph);
384
385 if (pl_axes.xscale)
386 {
387 double inc, pos;
388
389 pl_axes.xmin = h->xmin;
390 pl_axes.xmax = h->xmax;
391 pl_scale(&pl_axes.xmin, &pl_axes.xmax, &inc, &pos);
392 pl_axes.xinc = (pl_axes.xmax - pl_axes.xmin) / 20;
393 }
394 pl_hmin = h->xmin;
395 pl_hmax = h->xmax;
396 pl_hinc = h->xincr;
397 n = h->xbins;
398 bucket = fvector_alloc(0, n);
399 x = fvector_alloc(0, 4 * n);
400 y = fvector_alloc(0, 4 * n);
401
402 for (i = 0; i < h->xbins; i++)
403 {
404 bucket[i] = (float)h->array[0][i];
405 }
406
407 for (i = 0; i < n; i++)
408 {
409 x[4 * i] = x[4 * i + 1] = (float)(pl_hmin + i * pl_hinc);
410 x[4 * i + 2] = x[4 * i + 3] = (float)(x[4 * i] + pl_hinc);
411 y[4 * i] = y[4 * i + 3] = (float)0.0;
412 y[4 * i + 1] = y[4 * i + 2] = (float) bucket[i];
413 }
414
415 pl_graph.dofree = true;
416 pl_graph.n = 4 * n;
417 pl_graph.x = x;
418 pl_graph.y = y;
419
420 *graph = pl_graph;
421 pl_list = ref_addtostart(pl_list, (void *) graph, PL_GRAPH_TYPE);
422
423 fvector_free(bucket, 0);
424 }
425
426 void pl_set_cumhist_data(int nh, float *h)
427 {
428 int i, n;
429 float *x, *y;
430 double fvector_min(), fvector_max();
431 Pl_graph *graph;
432
433 if (nh <= 0 || h == NULL)
434 return;
435
436 graph = ts_ralloc(Pl_graph);
437
438 if (pl_axes.xscale)
439 {
440 double inc, pos;
441
442 pl_axes.xmin = fvector_min(h, 0, nh);
443 pl_axes.xmax = fvector_max(h, 0, nh);
444 pl_scale(&pl_axes.xmin, &pl_axes.xmax, &inc, &pos);
445 pl_axes.xinc = (pl_axes.xmax - pl_axes.xmin) / 20;
446 }
447 if (pl_hscale)
448 {
449 pl_hmin = pl_axes.xmin;
450 pl_hmax = pl_axes.xmax;
451 pl_hinc = pl_axes.xinc;
452 }
453 n = (int)((pl_hmax - pl_hmin + pl_hinc / 2) / pl_hinc);
454 x = fvector_alloc(0, n);
455 y = fvector_alloc(0, n);
456
457 for (i = 0; i < nh; i++)
458 {
459 int hit = (int) ((h[i] - pl_hmin) / pl_hinc);
460
461 if (0 <= hit && hit < n)
462 y[hit]++;
463 }
464 for (i = 0; i < n; i++)
465 x[i] = (float)(pl_hmin + i * pl_hinc);
466 for (i = 1; i < n; i++)
467 y[i] += y[i - 1];
468 for (i = 0; i < n; i++)
469 y[i] *= (float)(100.0 / y[n - 1]);
470
471 pl_graph.dofree = true;
472 pl_graph.n = n;
473 pl_graph.x = x;
474 pl_graph.y = y;
475
476 *graph = pl_graph;
477 pl_list = ref_addtostart(pl_list, (void *) graph, PL_GRAPH_TYPE);
478 }
479
480 void pl_set_graph_func(int n, double a, double b, double (*f) ( /* ??? */ ))
481 {
482 Pl_graph *graph = ts_ralloc(Pl_graph);
483 int i;
484 double dx;
485 float *x, *y;
486
487 x = fvector_alloc(0, n + 1);
488 y = fvector_alloc(0, n + 1);
489 dx = (b - a) / n;
490 for (i = 0; i <= n; i++)
491 {
492 x[i] = (float)(a + i * dx);
493 y[i] = (float)((*f) (x[i]));
494 }
495
496 pl_graph.dofree = true;
497 pl_graph.n = n + 1;
498 pl_graph.x = x;
499 pl_graph.y = y;
500
501 *graph = pl_graph;
502 pl_list = ref_addtostart(pl_list, (void *) graph, 0);
503 }
504
505 void pl_set_ctr_func(int nx, double ax, double bx, int ny, double ay, double by, double (*f) ( /* ??? */ ))
506 {
507 Pl_ctr *ctr = ts_ralloc(Pl_ctr);
508 int i, j;
509 double dx, dy;
510 float *x, *y, **z;
511
512 x = fvector_alloc(0, nx + 1);
513 y = fvector_alloc(0, ny + 1);
514 z = farray_alloc(0, 0, nx + 1, ny + 1);
515 dx = (bx - ax) / nx;
516 dy = (by - ay) / ny;
517
518 for (i = 0; i <= nx; i++)
519 x[i] = (float)(ax + i * dx);
520
521 for (j = 0; j <= ny; j++)
522 y[j] = (float)(ay + j * dy);
523
524 for (i = 0; i <= nx; i++)
525 for (j = 0; j <= ny; j++)
526 z[i][j] = (float)((*f) (x[i], y[j]));
527
528 pl_ctr.dofree = true;
529 pl_ctr.nx = nx + 1;
530 pl_ctr.x = x;
531 pl_ctr.ny = ny + 1;
532 pl_ctr.y = y;
533 pl_ctr.z = z;
534
535 *ctr = pl_ctr;
536 pl_list = ref_addtostart(pl_list, (void *) ctr, PL_CTR_TYPE);
537 }
538
539 void pl_plot(void)
540 {
541 if (pl_list == NULL)
542 return;
543
544 pl_scale_axes(pl_list, &pl_axes);
545 pl_graphtv(pl_tv, pl_axes.xmin, pl_axes.ymin, pl_axes.xmax, pl_axes.ymax);
546 pl_list_plot(pl_tv, pl_list, pl_axes);
547 pl_axes_plot(pl_tv, pl_axes);
548 }
549
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.