~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina5/tina-tools/tinatool/draw/drawPlot_procs.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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, *bucket;
375     float  *x, *y;
376     Pl_graph *graph;
377     shistogram *h = (shistogram *)ptr;
378 
379     if (h == NULL)
380         return;
381 
382     graph = ts_ralloc(Pl_graph);
383 
384     if (pl_axes.xscale)
385     {
386         double  inc, pos;
387 
388         pl_axes.xmin = h->xmin;
389         pl_axes.xmax = h->xmax;
390         pl_scale(&pl_axes.xmin, &pl_axes.xmax, &inc, &pos);
391         pl_axes.xinc = (pl_axes.xmax - pl_axes.xmin) / 20;
392     }
393     pl_hmin = h->xmin;
394     pl_hmax = h->xmax;
395     pl_hinc = h->xincr;
396     n = h->xbins;
397     bucket = ivector_alloc(0, n);
398     x = fvector_alloc(0, 4 * n);
399     y = fvector_alloc(0, 4 * n);
400 
401     for (i = 0; i < h->xbins; i++)
402     {
403         bucket[i] = (int)h->array[0][i];
404     }
405 
406     for (i = 0; i < n; i++)
407     {
408         x[4 * i] = x[4 * i + 1] = (float)(pl_hmin + i * pl_hinc);
409         x[4 * i + 2] = x[4 * i + 3] = (float)(x[4 * i] + pl_hinc);
410         y[4 * i] = y[4 * i + 3] = (float)0.0;
411         y[4 * i + 1] = y[4 * i + 2] = (float) bucket[i];
412     }
413 
414     pl_graph.dofree = true;
415     pl_graph.n = 4 * n;
416     pl_graph.x = x;
417     pl_graph.y = y;
418 
419     *graph = pl_graph;
420     pl_list = ref_addtostart(pl_list, (void *) graph, PL_GRAPH_TYPE);
421 
422     ivector_free(bucket, 0);
423 }
424 
425 void    pl_set_cumhist_data(int nh, float *h)
426 {
427     int     i, n;
428     float  *x, *y;
429     double  fvector_min(), fvector_max();
430     Pl_graph *graph;
431 
432     if (nh <= 0 || h == NULL)
433         return;
434 
435     graph = ts_ralloc(Pl_graph);
436 
437     if (pl_axes.xscale)
438     {
439         double  inc, pos;
440 
441         pl_axes.xmin = fvector_min(h, 0, nh);
442         pl_axes.xmax = fvector_max(h, 0, nh);
443         pl_scale(&pl_axes.xmin, &pl_axes.xmax, &inc, &pos);
444         pl_axes.xinc = (pl_axes.xmax - pl_axes.xmin) / 20;
445     }
446     if (pl_hscale)
447     {
448         pl_hmin = pl_axes.xmin;
449         pl_hmax = pl_axes.xmax;
450         pl_hinc = pl_axes.xinc;
451     }
452     n = (int)((pl_hmax - pl_hmin + pl_hinc / 2) / pl_hinc);
453     x = fvector_alloc(0, n);
454     y = fvector_alloc(0, n);
455 
456     for (i = 0; i < nh; i++)
457     {
458         int     hit = (int) ((h[i] - pl_hmin) / pl_hinc);
459 
460         if (0 <= hit && hit < n)
461             y[hit]++;
462     }
463     for (i = 0; i < n; i++)
464         x[i] = (float)(pl_hmin + i * pl_hinc);
465     for (i = 1; i < n; i++)
466         y[i] += y[i - 1];
467     for (i = 0; i < n; i++)
468         y[i] *= (float)(100.0 / y[n - 1]);
469 
470     pl_graph.dofree = true;
471     pl_graph.n = n;
472     pl_graph.x = x;
473     pl_graph.y = y;
474 
475     *graph = pl_graph;
476     pl_list = ref_addtostart(pl_list, (void *) graph, PL_GRAPH_TYPE);
477 }
478 
479 void    pl_set_graph_func(int n, double a, double b, double (*f) ( /* ??? */ ))
480 {
481     Pl_graph *graph = ts_ralloc(Pl_graph);
482     int     i;
483     double  dx;
484     float  *x, *y;
485 
486     x = fvector_alloc(0, n + 1);
487     y = fvector_alloc(0, n + 1);
488     dx = (b - a) / n;
489     for (i = 0; i <= n; i++)
490     {
491         x[i] = (float)(a + i * dx);
492         y[i] = (float)((*f) (x[i]));
493     }
494 
495     pl_graph.dofree = true;
496     pl_graph.n = n + 1;
497     pl_graph.x = x;
498     pl_graph.y = y;
499 
500     *graph = pl_graph;
501     pl_list = ref_addtostart(pl_list, (void *) graph, 0);
502 }
503 
504 void    pl_set_ctr_func(int nx, double ax, double bx, int ny, double ay, double by, double (*f) ( /* ??? */ ))
505 {
506     Pl_ctr *ctr = ts_ralloc(Pl_ctr);
507     int     i, j;
508     double  dx, dy;
509     float  *x, *y, **z;
510 
511     x = fvector_alloc(0, nx + 1);
512     y = fvector_alloc(0, ny + 1);
513     z = farray_alloc(0, 0, nx + 1, ny + 1);
514     dx = (bx - ax) / nx;
515     dy = (by - ay) / ny;
516 
517     for (i = 0; i <= nx; i++)
518         x[i] = (float)(ax + i * dx);
519 
520     for (j = 0; j <= ny; j++)
521         y[j] = (float)(ay + j * dy);
522 
523     for (i = 0; i <= nx; i++)
524         for (j = 0; j <= ny; j++)
525             z[i][j] = (float)((*f) (x[i], y[j]));
526 
527     pl_ctr.dofree = true;
528     pl_ctr.nx = nx + 1;
529     pl_ctr.x = x;
530     pl_ctr.ny = ny + 1;
531     pl_ctr.y = y;
532     pl_ctr.z = z;
533 
534     *ctr = pl_ctr;
535     pl_list = ref_addtostart(pl_list, (void *) ctr, PL_CTR_TYPE);
536 }
537 
538 void    pl_plot(void)
539 {
540     if (pl_list == NULL)
541         return;
542 
543     pl_scale_axes(pl_list, &pl_axes);
544     pl_graphtv(pl_tv, pl_axes.xmin, pl_axes.ymin, pl_axes.xmax, pl_axes.ymax);
545     pl_list_plot(pl_tv, pl_list, pl_axes);
546     pl_axes_plot(pl_tv, pl_axes);
547 }
548 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.