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

Linux Cross Reference
Tina6/tina-tools/tinatool/draw/drawPlot_graph.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_graph.c,v $
 37  * Date    :  $Date: 2003/10/01 16:02:47 $
 38  * Version :  $Revision: 1.2 $
 39  * CVS Id  :  $Id: drawPlot_graph.c,v 1.2 2003/10/01 16:02:47 tony Exp $
 40  *
 41  * Author  : Legacy TINA
 42  *
 43  * Notes :
 44  *
 45  *********
 46 */
 47 
 48 #include "drawPlot_graph.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/math/mathDef.h>
 59 #include <tina/geometry/geomDef.h>
 60 #include <tinatool/draw/draw_TvDef.h>
 61 #include <tinatool/draw/draw_TvPro.h>
 62 #include <tinatool/draw/draw_PlotDef.h>
 63 #include <tinatool/draw/draw_PlotPro.h>
 64 
 65 Vec2    vec2(double , double);
 66 Ipos    ipos(int, int);
 67 
 68 
 69 static int ylegend;
 70 
 71 static char xfmt[16] = "%5.2f";
 72 void pl_xfmt_set(char *newxfmt)
 73 {
 74     strcpy(xfmt, newxfmt);
 75 }
 76 
 77 static char yfmt[16] = "%5.2f";
 78 void pl_yfmt_set(char *newyfmt)
 79 {
 80     strcpy(yfmt, newyfmt);
 81 }
 82 
 83 static Vec2 ll, ur;
 84 Vec2 pl_ll_get()
 85 {
 86     return(ll);
 87 }
 88 
 89 Vec2 pl_ur_get()
 90 {
 91     return(ur);
 92 }
 93 
 94 
 95 void    pl_graphtv(Tv * tv, double xmin, double ymin, double xmax, double ymax)
 96 {
 97     double  cx = (xmax + xmin) / 2, wx = (xmax - xmin) / 2;
 98     double  cy = (ymax + ymin) / 2, wy = (ymax - ymin) / 2;
 99 
100     if (tv == NULL)
101         return;
102     wx = (wx == 0)? 1: wx;
103     wy = (wy == 0)? 1: wy;
104     tv_camera2_rect(tv, vec2(cx - 1.3 * wx, cy - 1.3 * wy),
105                     vec2(cx + 1.3 * wx, cy + 1.3 * wy));
106 }
107 
108 void    tv_xtick(Tv * tv, Ipos p, int n)
109 {
110     Ipos    q = {Ipos_id};
111 
112     if (tv == NULL)
113         return;
114     q.x = p.x;
115     q.y = p.y + n;
116     tv_line(tv, p, q);
117 }
118 
119 void    tv_xtick2(Tv * tv, Vec2 p, int n)
120 {
121     if (tv == NULL)
122         return;
123     tv_xtick(tv, tv_proj2(tv, p), n);
124 }
125 
126 void    tv_xtick3(Tv * tv, Vec3 p, int n)
127 {
128     if (tv == NULL)
129         return;
130     tv_xtick(tv, tv_proj3(tv, p), n);
131 }
132 
133 void    tv_ytick(Tv * tv, Ipos p, int n)
134 {
135     Ipos    q = {Ipos_id};
136 
137     if (tv == NULL)
138         return;
139     q.x = p.x - n;
140     q.y = p.y;
141     tv_line(tv, p, q);
142 }
143 
144 void    tv_ytick2(Tv * tv, Vec2 p, int n)
145 {
146     if (tv == NULL)
147         return;
148     tv_ytick(tv, tv_proj2(tv, p), n);
149 }
150 
151 void    tv_ytick3(Tv * tv, Vec3 p, int n)
152 {
153     if (tv == NULL)
154         return;
155     tv_ytick(tv, tv_proj3(tv, p), n);
156 }
157 
158 void    tv_cctext(Tv * tv, char *string, Ipos p)
159 {
160     int     xmin, xmax, ymin, ymax;
161     int     offx, offy;
162 
163     if (tv == NULL)
164         return;
165 
166     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
167     offx = -(xmax + xmin) / 2;
168     offy = -(ymax + ymin) / 2;
169     tv_offset_text(tv, string, p, offx, offy);
170 }
171 
172 void    tv_uctext(Tv * tv, char *string, Ipos p)
173 {
174     int     xmin, xmax, ymin, ymax;
175     int     offx, offy;
176 
177     if (tv == NULL)
178         return;
179 
180     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
181     offx = -(xmax + xmin) / 2;
182     offy = -ymin + 5;
183     tv_offset_text(tv, string, p, offx, offy);
184 }
185 
186 void    tv_uctext2(Tv * tv, char *string, Vec2 p)
187 {
188     if (tv == NULL)
189         return;
190     tv_uctext(tv, string, tv_proj2(tv, p));
191 }
192 
193 void    tv_uctext3(Tv * tv, char *string, Vec3 p)
194 {
195     if (tv == NULL)
196         return;
197     tv_uctext(tv, string, tv_proj3(tv, p));
198 }
199 
200 void    tv_lctext(Tv * tv, char *string, Ipos p)
201 {
202     int     xmin, xmax, ymin, ymax;
203     int     offx, offy;
204 
205     if (tv == NULL)
206         return;
207 
208     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
209     offx = -(xmax + xmin) / 2;
210     offy = -5;
211     tv_offset_text(tv, string, p, offx, offy);
212 }
213 
214 void    tv_lctext2(Tv * tv, char *string, Vec2 p)
215 {
216     if (tv == NULL)
217         return;
218     tv_lctext(tv, string, tv_proj2(tv, p));
219 }
220 
221 void    tv_lctext3(Tv * tv, char *string, Vec3 p)
222 {
223     if (tv == NULL)
224         return;
225     tv_lctext(tv, string, tv_proj3(tv, p));
226 }
227 
228 void    tv_crtext(Tv * tv, char *string, Ipos p)
229 {
230     int     xmin, xmax, ymin, ymax;
231     int     offx, offy;
232 
233     if (tv == NULL)
234         return;
235 
236     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
237     offx = -xmax - 5;
238     offy = -(ymax + ymin) / 2;
239     tv_offset_text(tv, string, p, offx, offy);
240 }
241 
242 void    tv_crtext2(Tv * tv, char *string, Vec2 p)
243 {
244     if (tv == NULL)
245         return;
246     tv_crtext(tv, string, tv_proj2(tv, p));
247 }
248 
249 void    tv_crtext3(Tv * tv, char *string, Vec3 p)
250 {
251     if (tv == NULL)
252         return;
253     tv_crtext(tv, string, tv_proj3(tv, p));
254 }
255 
256 void    tv_cltext(Tv * tv, char *string, Ipos p)
257 {
258     int     xmin, xmax, ymin, ymax;
259     int     offx, offy;
260 
261     if (tv == NULL)
262         return;
263 
264     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
265     offx = 5;
266     offy = -(ymax + ymin) / 2;
267     tv_offset_text(tv, string, p, offx, offy);
268 }
269 
270 void    tv_cltext2(Tv * tv, char *string, Vec2 p)
271 {
272     if (tv == NULL)
273         return;
274     tv_cltext(tv, string, tv_proj2(tv, p));
275 }
276 
277 void    tv_cltext3(Tv * tv, char *string, Vec3 p)
278 {
279     if (tv == NULL)
280         return;
281     tv_cltext(tv, string, tv_proj3(tv, p));
282 }
283 
284 /* ARGSUSED quieten lint */
285 void    pl_xaxis_plot(Tv * tv, double xmin, double xmax, double xinc, double xpos, double ypos)
286 {
287     if (tv == NULL)
288         return;
289     tv_line2(tv, vec2(xmin, ypos), vec2(xmax, ypos));
290 }
291 
292 /* ARGSUSED quieten lint */
293 void    pl_xtics_plot(Tv * tv, double xmin, double xmax, double xinc, double xpos, double ypos)
294 {
295     double  x;
296 
297     if (tv == NULL)
298         return;
299     for (x = xmin; x <= xmax + xinc / 2; x += xinc)
300         tv_xtick2(tv, vec2(x, ypos), 3);
301 }
302 
303 void    pl_xtext_plot(Tv * tv, double xmin, double xmax, double xinc, double xpos, double ypos)
304 {
305     double  x;
306 
307     if (tv == NULL)
308         return;
309     for (x = xmin; x <= xmax + xinc / 2; x += xinc)
310     {
311         if (fabs(x - xpos) >= xinc / 2)
312         {
313             char    string[32];
314 
315             (void) sprintf(string, xfmt, x);
316             tv_uctext2(tv, string, vec2(x, ypos));
317         }
318     }
319 }
320 
321 /* ARGSUSED quieten lint */
322 void    pl_yaxis_plot(Tv * tv, double ymin, double ymax, double yinc, double xpos, double ypos)
323 {
324     if (tv == NULL)
325         return;
326     tv_line2(tv, vec2(xpos, ymin), vec2(xpos, ymax));
327 }
328 
329 /* ARGSUSED quieten lint */
330 void    pl_ytics_plot(Tv * tv, double ymin, double ymax, double yinc, double xpos, double ypos)
331 {
332     double  y;
333 
334     if (tv == NULL)
335         return;
336     for (y = ymin; y <= ymax + yinc / 2; y += yinc)
337         tv_ytick2(tv, vec2(xpos, y), 3);
338 }
339 
340 void    pl_ytext_plot(Tv * tv, double ymin, double ymax, double yinc, double ypos, double xpos)
341 {
342     double  y;
343 
344     if (tv == NULL)
345         return;
346     for (y = ymin; y <= ymax + yinc / 2; y += yinc)
347     {
348         char    string[32];
349 
350         if (fabs(y - ypos) >= yinc / 2)
351         {
352             (void) sprintf(string, yfmt, y);
353             tv_crtext2(tv, string, vec2(xpos, y));
354         }
355     }
356 }
357 
358 void    pl_axes_plot(Tv * tv, Pl_axes axes)
359 {
360     tv_set_color(tv, axes.color);
361     tv_uctext(tv, axes.title, ipos(tv->width / 2, 10));
362 
363     if (axes.xaxis)
364         pl_xaxis_plot(tv,
365                axes.xmin, axes.xmax, axes.xinc, axes.xpos, axes.ypos);
366 
367     if (axes.xtics)
368         pl_xtics_plot(tv,
369                axes.xmin, axes.xmax, axes.xinc, axes.xpos, axes.ypos);
370 
371 /* NAT positioning of text modified to put text below x axis 3/3/13 */
372     if (axes.xtext)
373         pl_xtext_plot(tv,
374                axes.xmin, axes.xmax, axes.xinc, axes.xpos, axes.ypos - (axes.ymax-axes.ymin)/10.0);
375 
376     if (axes.yaxis)
377         pl_yaxis_plot(tv,
378                axes.ymin, axes.ymax, axes.yinc, axes.xpos, axes.ypos);
379 
380     if (axes.ytics)
381         pl_ytics_plot(tv,
382                axes.ymin, axes.ymax, axes.yinc, axes.xpos, axes.ypos);
383 
384     if (axes.ytext)
385         pl_ytext_plot(tv,
386                axes.ymin, axes.ymax, axes.yinc, axes.ypos, axes.xpos);
387     
388     ll = vec2(axes.xmin, axes.ymin);
389     ur = vec2(axes.xmax, axes.ymax);
390 }
391 
392 static void legend(Tv *tv, int color, char *legend)
393 {
394     int xmin, xmax, ymin, ymax, w, h;
395     tv_get_text_extent(tv, legend, &xmin, &xmax, &ymin, &ymax);
396     w = xmin+xmax;
397     h = ymin+ymax;
398     tv_set_color(tv, black);
399     tv_text(tv, legend, ipos(tv->width-35-w, ylegend-h/2));
400     tv_set_color(tv, color);
401     tv_linexy(tv, tv->width-30, ylegend, tv->width-10, ylegend);
402     h = -ymin+ymax;
403     ylegend += h+5;
404 }
405 
406 void    pl_graph_plot(Tv * tv, Pl_graph * graph, Pl_axes axes)
407 {
408     int     i, n = graph->n;
409     float  *x, *y;
410     float xmin = (float)axes.xmin;
411     float xmax = (float)axes.xmax;
412     float ymin = (float)axes.ymin;
413     float ymax = (float)axes.ymax;
414     void *mask;
415 
416     if (tv == NULL || graph == NULL)
417         return;
418 
419     x = graph->x;
420     y = graph->y;
421     if(strcmp(graph->legend, "") != 0)
422         legend(tv, graph->color, graph->legend);
423     tv_set_color(tv, graph->color);
424     mask = tv_clip_mask2_get(tv, vec2(xmin, ymin), vec2(xmax, ymax));
425     tv_mask_use(tv, mask);
426     switch(graph->style)
427     {
428         case PL_LINE:
429             for (i = 1; i < n; i++)
430                 tv_line2(tv, vec2(x[i - 1], y[i - 1]), vec2(x[i], y[i]));
431             break;
432         case PL_CROSS:
433             for (i = 0; i < n; i++)
434                 tv_cross2(tv, vec2(x[i], y[i]), 5);
435             break;
436         case PL_DOT:
437             for (i = 0; i < n; i++)
438                 tv_dot2(tv, vec2(x[i], y[i]));
439             break;
440         case PL_POINT:
441             for (i = 0; i < n; i++)
442                 tv_point2(tv, vec2(x[i], y[i]));
443             break;
444     }
445     tv_mask_free(tv, mask);
446 }
447 
448 void    pl_scatter_plot(Tv * tv, Pl_graph * graph)
449 {
450     int     i, n = graph->n;
451     float  *x, *y;
452 
453     if (tv == NULL || graph == NULL)
454         return;
455 
456     x = graph->x;
457     y = graph->y;
458     tv_set_color(tv, graph->color);
459     for (i = 0; i < n; i++)
460         tv_cross2(tv, vec2(x[i], y[i]), 5);
461 }
462 
463 
464 void    pl_list_plot(Tv * tv, List * list, Pl_axes axes)
465 {
466     List   *ptr;
467 
468     ylegend = 10;
469 
470     if (tv == NULL || list == NULL)
471         return;
472 
473     for (ptr = list; ptr != NULL; ptr = ptr->next)
474     {
475         switch (ptr->type)
476         {
477         case PL_GRAPH_TYPE:
478             {
479                 Pl_graph *graph = (Pl_graph *) ptr->to;
480 
481                 pl_graph_plot(tv, graph, axes);
482                 break;
483             }
484         case PL_CTR_TYPE:
485             {
486                 Pl_ctr *ctr = (Pl_ctr *) ptr->to;
487 
488                 pl_ctr_plot(tv, ctr);
489                 break;
490             }
491         case PL_SCATTER_TYPE:
492             {
493                 Pl_graph *graph = (Pl_graph *) ptr->to;
494 
495                 pl_scatter_plot(tv, graph);
496                 break;
497             }
498         }
499     }
500 }
501 

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