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

Linux Cross Reference
Tina4/src/draw/plot/plot_procs.c

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

  1 /**@(#)
  2 **/
  3 #include <stdio.h>
  4 #include <string.h>
  5 #include <math.h>
  6 #include <tina/sys.h>
  7 #include <tina/sysfuncs.h>
  8 #include <tina/math.h>
  9 #include <tina/tv.h>
 10 #include <tina/tvfuncs.h>
 11 #include <tina/vision.h>
 12 #include <tina/draw.h>
 13 #include <tina/drawfuncs.h>
 14 #include <tina/hist_funcs.h>
 15 
 16 void    rfree();
 17 Ipos    ipos();
 18 Vec2    vec2();
 19 List   *ref_addtostart();
 20 double  pl_get_pos();
 21 Bool dofree = false;
 22 
 23 static Tv *pl_tv;
 24 static List *pl_list = NULL;
 25 static Pl_graph pl_graph = {Pl_graph_id};
 26 static Pl_ctr pl_ctr = {Pl_ctr_id};
 27 static Pl_axes pl_axes = {Pl_axes_id};
 28 
 29 static Bool pl_hscale;
 30 static double pl_hmin, pl_hmax, pl_hinc;
 31 
 32 void    pl_free(void *pl, int type)
 33 {
 34     switch (type)
 35     {
 36         case PL_GRAPH_TYPE:
 37         {
 38             Pl_graph *graph = (Pl_graph *) pl;
 39 
 40             if (graph->dofree)
 41             {
 42                 fvector_free((void *) graph->x, 0);
 43                 fvector_free((void *) graph->y, 0);
 44             }
 45             rfree((void *) graph);
 46             break;
 47         }
 48     case PL_SCATTER_TYPE:
 49         {
 50             Pl_graph *graph = (Pl_graph *) pl;
 51 
 52             if (graph->dofree)
 53             {
 54                 fvector_free((void *) graph->x, 0);
 55                 fvector_free((void *) graph->y, 0);
 56             }
 57             rfree((void *) graph);
 58             break;
 59         }
 60     case PL_CTR_TYPE:
 61         {
 62             Pl_ctr *ctr = (Pl_ctr *) pl;
 63 
 64             if (ctr->dofree)
 65             {
 66                 fvector_free((void *) ctr->x, 0);
 67                 fvector_free((void *) ctr->y, 0);
 68                 farray_free((char **) ctr->z, 0, 0, ctr->nx + 1, ctr->ny + 1);
 69             }
 70             rfree((void *) ctr);
 71             break;
 72         }
 73     }
 74 }
 75 
 76 void pl_free_set(Bool set)
 77 {
 78     dofree = set;
 79 }
 80 
 81 void    pl_init(void)
 82 {
 83     pl_graph.color = black;
 84     pl_graph.style = PL_LINE;
 85 
 86     pl_ctr.color = black;
 87     pl_ctr.cscale = true;
 88 
 89     pl_axes.color = black;
 90     (void) strcpy(pl_axes.title, "");
 91 
 92     pl_axes.xscale = true;
 93     pl_axes.xaxis = true;
 94     pl_axes.xtics = true;
 95     pl_axes.xtext = true;
 96 
 97     pl_axes.yscale = true;
 98     pl_axes.yaxis = true;
 99     pl_axes.ytics = true;
100     pl_axes.ytext = true;
101 
102     pl_hscale = true;
103 
104     list_rm(pl_list, pl_free);
105     pl_list = NULL;
106 }
107 
108 void    pl_set_tv(Tv * tv)
109 {
110     pl_tv = tv;
111 }
112 
113 void    pl_set_color(int color)
114 {
115     pl_graph.color = color;
116 }
117 
118 void    pl_set_axis_color(int color)
119 {
120     pl_axes.color = color;
121 }
122 
123 void    pl_set_style(int style)
124 {
125     pl_graph.style = style;
126 }
127 
128 void    pl_set_title(char *title)
129 {
130     (void) strcpy(pl_axes.title, title);
131 }
132 
133 void    pl_set_legend(char *legend)
134 {
135     Pl_graph *graph = pl_list->to;
136     (void) strcpy(graph->legend, legend);
137 }
138 
139 void    pl_set_x_axis(Bool set)
140 {
141     pl_axes.xaxis = set;
142 }
143 
144 void    pl_set_x_tics(Bool set)
145 {
146     pl_axes.xtics = set;
147 }
148 
149 void    pl_set_x_text(Bool set)
150 {
151     pl_axes.xtext = set;
152 }
153 
154 void    pl_set_x_range(double xmin, double xmax, double xinc)
155 {
156     pl_axes.xscale = false;
157     pl_axes.xmin = xmin;
158     pl_axes.xmax = xmax;
159     if(xinc == 0.0)
160         pl_scale(&pl_axes.xmin, &pl_axes.xmax, &pl_axes.xinc, &pl_axes.xpos);
161     else
162     {
163         pl_axes.xinc = xinc;
164         pl_axes.xpos = pl_get_pos(xmin, xmax);
165     }
166 }
167 
168 void    pl_set_y_axis(Bool set)
169 {
170     pl_axes.yaxis = set;
171 }
172 
173 void    pl_set_y_tics(Bool set)
174 {
175     pl_axes.ytics = set;
176 }
177 
178 void    pl_set_y_text(Bool set)
179 {
180     pl_axes.ytext = set;
181 }
182 
183 void    pl_set_y_range(double ymin, double ymax, double yinc)
184 {
185     pl_axes.yscale = false;
186     pl_axes.ymin = ymin;
187     pl_axes.ymax = ymax;
188     if(yinc == 0.0)
189         pl_scale(&pl_axes.ymin, &pl_axes.ymax, &pl_axes.yinc, &pl_axes.ypos);
190     else
191     {
192         pl_axes.yinc = yinc;
193         pl_axes.ypos = pl_get_pos(ymin, ymax);
194     }
195 }
196 
197 void    pl_set_hist_range(double hmin, double hmax, double hinc)
198 {
199     pl_hscale = false;
200     pl_hmin = hmin;
201     pl_hmax = hmax;
202     pl_hinc = hinc;
203 }
204 
205 void    pl_set_graph_data(int n, float *x, float *y)
206 {
207     Pl_graph *graph;
208 
209     if (n <= 0 || x == NULL || y == NULL)
210         return;
211 
212     graph = talloc(Pl_graph);
213 
214     pl_graph.dofree = dofree;
215     pl_graph.n = n;
216     pl_graph.x = x;
217     pl_graph.y = y;
218     *graph = pl_graph;
219     strcpy(graph->legend, "");
220     pl_list = ref_addtostart(pl_list, (void *) graph, PL_GRAPH_TYPE);
221 }
222 
223 void    pl_set_scatt_data(int n, float *x, float *y)
224 {
225     Pl_graph *graph = ts_ralloc(Pl_graph);
226 
227     pl_graph.dofree = dofree;
228     pl_graph.n = n;
229     pl_graph.x = x;
230     pl_graph.y = y;
231     strcpy(pl_graph.legend, "");
232     *graph = pl_graph;
233     pl_list = ref_addtostart(pl_list, (void *) graph, PL_SCATTER_TYPE);
234 }
235 
236 void    pl_set_ctr_range(double cmin, double cmax, double cinc)
237 {
238     pl_ctr.cscale = false;
239     pl_ctr.cmin = cmin;
240     pl_ctr.cmax = cmax;
241     pl_ctr.cinc = cinc;
242 }
243 
244 void    pl_set_ctr_data(int nx, float *x, int ny, float *y, float **z)
245 {
246     Pl_ctr *ctr = ts_ralloc(Pl_ctr);
247 
248     pl_ctr.dofree = dofree;
249     pl_ctr.nx = nx;
250     pl_ctr.x = x;
251     pl_ctr.ny = ny;
252     pl_ctr.y = y;
253     pl_ctr.z = z;
254 
255     *ctr = pl_ctr;
256     pl_list = ref_addtostart(pl_list, (void *) ctr, PL_CTR_TYPE);
257 }
258 
259 void    pl_set_hist_data(int nh, float *h)
260 {
261     int     i, n, *bucket;
262     float  *x, *y;
263     double  fvector_min(), fvector_max();
264     Pl_graph *graph;
265 
266     if (nh <= 0 || h == NULL)
267         return;
268 
269     graph = ts_ralloc(Pl_graph);
270 
271     if (pl_axes.xscale)
272     {
273         double  inc, pos;
274 
275         pl_axes.xmin = fvector_min(h, 0, nh);
276         pl_axes.xmax = fvector_max(h, 0, nh);
277         pl_scale(&pl_axes.xmin, &pl_axes.xmax, &inc, &pos);
278         pl_axes.xinc = (pl_axes.xmax - pl_axes.xmin) / 20;
279     }
280     if (pl_hscale)
281     {
282         pl_hmin = pl_axes.xmin;
283         pl_hmax = pl_axes.xmax;
284         pl_hinc = pl_axes.xinc;
285     }
286     if (!(pl_hinc < 0.0) && !(pl_hinc >= 0.0)) return; 
287     n = (int)((pl_hmax - pl_hmin + pl_hinc / 2) / pl_hinc);
288     bucket = ivector_alloc(0, n);
289     x = fvector_alloc(0, 4 * n);
290     y = fvector_alloc(0, 4 * n);
291 
292     for (i = 0; i < n; i++)
293         bucket[i] = 0;
294 
295     for (i = 0; i < nh; i++)
296     {
297         int     hit = (int) ((h[i] - pl_hmin) / pl_hinc);
298 
299         if (0 <= hit && hit < n)
300             bucket[hit]++;
301     }
302 
303     for (i = 0; i < n; i++)
304     {
305         x[4 * i] = x[4 * i + 1] = (float)(pl_hmin + i * pl_hinc);
306         x[4 * i + 2] = x[4 * i + 3] = (float)(x[4 * i] + pl_hinc);
307         y[4 * i] = y[4 * i + 3] = (float)0.0;
308         y[4 * i + 1] = y[4 * i + 2] = (float) bucket[i];
309     }
310 
311     pl_graph.dofree = true;
312     pl_graph.n = 4 * n;
313     pl_graph.x = x;
314     pl_graph.y = y;
315 
316     *graph = pl_graph;
317     pl_list = ref_addtostart(pl_list, (void *) graph, PL_GRAPH_TYPE);
318 
319     ivector_free((void *)bucket, 0);
320 }
321 
322 void    pl_set_hist_binned_data(void *ptr)
323 {
324     int     i, n, *bucket;
325     float  *x, *y;
326     Pl_graph *graph;
327     shistogram *h = (shistogram *)ptr;
328 
329     if (h == NULL)
330         return;
331 
332     graph = ts_ralloc(Pl_graph);
333 
334     if (pl_axes.xscale)
335     {
336         double  inc, pos;
337 
338         pl_axes.xmin = h->xmin;
339         pl_axes.xmax = h->xmax;
340         pl_scale(&pl_axes.xmin, &pl_axes.xmax, &inc, &pos);
341         pl_axes.xinc = (pl_axes.xmax - pl_axes.xmin) / 20;
342     }
343     pl_hmin = h->xmin;
344     pl_hmax = h->xmax;
345     pl_hinc = h->xincr;
346     n = h->xbins;
347     bucket = ivector_alloc(0, n);
348     x = fvector_alloc(0, 4 * n);
349     y = fvector_alloc(0, 4 * n);
350 
351     for (i = 0; i < h->xbins; i++)
352     {
353         bucket[i] = (int)h->array[0][i];
354     }
355 
356     for (i = 0; i < n; i++)
357     {
358         x[4 * i] = x[4 * i + 1] = (float)(pl_hmin + i * pl_hinc);
359         x[4 * i + 2] = x[4 * i + 3] = (float)(x[4 * i] + pl_hinc);
360         y[4 * i] = y[4 * i + 3] = (float)0.0;
361         y[4 * i + 1] = y[4 * i + 2] = (float) bucket[i];
362     }
363 
364     pl_graph.dofree = true;
365     pl_graph.n = 4 * n;
366     pl_graph.x = x;
367     pl_graph.y = y;
368 
369     *graph = pl_graph;
370     pl_list = ref_addtostart(pl_list, (void *) graph, PL_GRAPH_TYPE);
371 
372     ivector_free((void *)bucket, 0);
373 }
374 
375 void    pl_set_cumhist_data(int nh, float *h)
376 {
377     int     i, n;
378     float  *x, *y;
379     double  fvector_min(), fvector_max();
380     Pl_graph *graph;
381 
382     if (nh <= 0 || h == NULL)
383         return;
384 
385     graph = ts_ralloc(Pl_graph);
386 
387     if (pl_axes.xscale)
388     {
389         double  inc, pos;
390 
391         pl_axes.xmin = fvector_min(h, 0, nh);
392         pl_axes.xmax = fvector_max(h, 0, nh);
393         pl_scale(&pl_axes.xmin, &pl_axes.xmax, &inc, &pos);
394         pl_axes.xinc = (pl_axes.xmax - pl_axes.xmin) / 20;
395     }
396     if (pl_hscale)
397     {
398         pl_hmin = pl_axes.xmin;
399         pl_hmax = pl_axes.xmax;
400         pl_hinc = pl_axes.xinc;
401     }
402     n = (int)((pl_hmax - pl_hmin + pl_hinc / 2) / pl_hinc);
403     x = fvector_alloc(0, n);
404     y = fvector_alloc(0, n);
405 
406     for (i = 0; i < nh; i++)
407     {
408         int     hit = (int) ((h[i] - pl_hmin) / pl_hinc);
409 
410         if (0 <= hit && hit < n)
411             y[hit]++;
412     }
413     for (i = 0; i < n; i++)
414         x[i] = (float)(pl_hmin + i * pl_hinc);
415     for (i = 1; i < n; i++)
416         y[i] += y[i - 1];
417     for (i = 0; i < n; i++)
418         y[i] *= (float)(100.0 / y[n - 1]);
419 
420     pl_graph.dofree = true;
421     pl_graph.n = n;
422     pl_graph.x = x;
423     pl_graph.y = y;
424 
425     *graph = pl_graph;
426     pl_list = ref_addtostart(pl_list, (void *) graph, PL_GRAPH_TYPE);
427 }
428 
429 void    pl_set_graph_func(int n, double a, double b, double (*f) ( /* ??? */ ))
430 {
431     Pl_graph *graph = ts_ralloc(Pl_graph);
432     int     i;
433     double  dx;
434     float  *x, *y;
435 
436     x = fvector_alloc(0, n + 1);
437     y = fvector_alloc(0, n + 1);
438     dx = (b - a) / n;
439     for (i = 0; i <= n; i++)
440     {
441         x[i] = (float)(a + i * dx);
442         y[i] = (float)((*f) (x[i]));
443     }
444 
445     pl_graph.dofree = true;
446     pl_graph.n = n + 1;
447     pl_graph.x = x;
448     pl_graph.y = y;
449 
450     *graph = pl_graph;
451     pl_list = ref_addtostart(pl_list, (void *) graph, (int)NULL);
452 }
453 
454 void    pl_set_ctr_func(int nx, double ax, double bx, int ny, double ay, double by, double (*f) ( /* ??? */ ))
455 {
456     Pl_ctr *ctr = ts_ralloc(Pl_ctr);
457     int     i, j;
458     double  dx, dy;
459     float  *x, *y, **z;
460 
461     x = fvector_alloc(0, nx + 1);
462     y = fvector_alloc(0, ny + 1);
463     z = farray_alloc(0, 0, nx + 1, ny + 1);
464     dx = (bx - ax) / nx;
465     dy = (by - ay) / ny;
466 
467     for (i = 0; i <= nx; i++)
468         x[i] = (float)(ax + i * dx);
469 
470     for (j = 0; j <= ny; j++)
471         y[j] = (float)(ay + j * dy);
472 
473     for (i = 0; i <= nx; i++)
474         for (j = 0; j <= ny; j++)
475             z[i][j] = (float)((*f) (x[i], y[j]));
476 
477     pl_ctr.dofree = true;
478     pl_ctr.nx = nx + 1;
479     pl_ctr.x = x;
480     pl_ctr.ny = ny + 1;
481     pl_ctr.y = y;
482     pl_ctr.z = z;
483 
484     *ctr = pl_ctr;
485     pl_list = ref_addtostart(pl_list, (void *) ctr, PL_CTR_TYPE);
486 }
487 
488 void    pl_plot(void)
489 {
490     if (pl_list == NULL)
491         return;
492 
493     pl_scale_axes(pl_list, &pl_axes);
494     pl_graphtv(pl_tv, pl_axes.xmin, pl_axes.ymin, pl_axes.xmax, pl_axes.ymax);
495     pl_list_plot(pl_tv, pl_list, pl_axes);
496     pl_axes_plot(pl_tv, pl_axes);
497 }
498 

~ [ 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.