13e35e9b9f1f679c85c0e2bf255e1e6f94e32972
[ctsim.git] / include / ezplot.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **         Name:  ezplot.h
5 **         Purpose: Header file for EZplot library
6 **
7 **  This is part of the CTSim program
8 **  Copyright (C) 1983-2000 Kevin Rosenberg
9 **
10 **  $Id: ezplot.h,v 1.11 2000/07/28 10:51:31 kevin Exp $
11 **
12 **  This program is free software; you can redistribute it and/or modify
13 **  it under the terms of the GNU General Public License (version 2) as
14 **  published by the Free Software Foundation.
15 **
16 **  This program is distributed in the hope that it will be useful,
17 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 **  GNU General Public License for more details.
20 **
21 **  You should have received a copy of the GNU General Public License
22 **  along with this program; if not, write to the Free Software
23 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 ******************************************************************************/
25
26
27 #ifndef __H_EZPLOT
28 #define __H_EZPLOT
29
30 #include <stdio.h>
31 #include <stddef.h>
32 #include <ctype.h>
33 #include <math.h>
34 #include "ctsupport.h"
35 #include "sgp.h"
36
37
38 class EZPlotCurve {
39  public:
40     double *x;
41     double *y;
42     int m_iPointCount;
43     int m_iLineStyle;
44     int m_iSymbol;
45     int m_iSymbolFreq;
46     int m_iColor;
47     string m_sLegend;
48
49     EZPlotCurve (const double* x, const double* y, int n, int color, int linestyle, int symbol, int sumbolFreq, const string& legend);
50
51     ~EZPlotCurve();
52 };
53
54 /*----------------------------------------------------------------------*/
55 /*                             GLOBAL VARIABLES                         */
56 /*----------------------------------------------------------------------*/
57
58 /* axis definitions */
59 #define LINEAR  1               /* linear axis */
60 #define LOG     2               /* logrithmic axis */
61 #define NOAXIS  3               /* don't plot axis */
62
63 /* tick definitions */
64 #define ABOVE    1
65 #define BELOW    2
66 #define RIGHT    4
67 #define LEFT     8
68
69 /* line types */
70 #define NOLINE  0
71 #define SOLID   1
72 #define DASH    2
73 #define DASH1   10
74 #define DASH2   11
75 #define DASH3   12
76 #define DASH4   13
77
78 /* symbol definitions */
79 #define SB_CROSS    1
80 #define SB_PLUS     2
81 #define SB_BOX      3
82 #define SB_CIRCLE   4
83 #define SB_ERRORBAR 5
84 #define MAXSYMBOL   5
85
86 #define INSIDE   1              /* values of o_legendbox */
87 #define OUTSIDE  2
88 #define NOLEGEND 3
89
90 /*-----------------------------------------------------------------------------
91  *                              GLOBAL VARIABLES
92  *
93  * Naming Convention:
94  *      i_   Internal variable
95  *              Not user changable
96  *      o_   Option variable
97  *              Normal variable that is user modifiable
98  *              These variables must always have a valid value
99  *      d_   Device variable
100  *              Variables controlling devices
101  *      clr_ Color variable
102  *              Holds a color value
103  *      c_   Character string variable
104  *              Contains a character string
105  *      v_   Value variable
106  *              User modifiable variable associated with the set variable (s_)
107  *              These variables do not always have a valid value
108  *              These variables change assumption EZPLOT makes about the plot
109  *      s_   Set variable.
110  *              TRUE if associated value variable (v_) has been set by the user
111  *---------------------------------------------------------------------------*/
112
113 #include <vector>
114 using namespace std;
115
116 typedef vector<EZPlotCurve*>::iterator EZPlotCurveIterator;
117 typedef vector<EZPlotCurve*>::const_iterator EZPlotCurveConstIterator;
118
119 class SGP;
120 class EZPlot {
121  private:
122     vector<class EZPlotCurve*> m_vecCurves;
123
124     // Colors
125     int clr_axis;                       /* color of all axis lines */
126     int clr_title;                      /* color of main title */
127     int clr_label;                      /* color of axis labels */
128     int clr_legend;                     /* color of legend box */
129     int clr_grid;                       /* color of grid lines */
130     int clr_number;                     /* color of axis number labels */
131
132     // Options
133     double o_xporigin, o_yporigin;      /* origin of plot frame in NDC */
134     double o_xlength, o_ylength;        /* length of plot frame in NDC */
135
136     string c_xlabel;    /* label for x axis */
137     string c_ylabel;    /* label for y axis */
138     string c_title;             /* title to print above graph */
139     string c_legend;;   /* current legend specified */
140     
141     int o_linestyle, o_color;   /* style to use for curves all subsequent curves to EZPLOT */
142     bool o_xaxis, o_yaxis;              /* Specifies where axis & labels are drawn */
143     bool o_grid;                        /* Flag to draw a grid at major ticks */
144     bool o_box;                 /* Flag to draw a box around the graph */
145     
146     int o_xticks, o_yticks;             /* direction to draw tick marks */
147     bool o_xtlabel, o_ytlabel;  /* TRUE if label tick marks */
148     
149     int o_xmajortick, o_ymajortick;     /* number of major ticks to draw */
150     int o_xminortick, o_yminortick;     /* number of minor ticks between major ticks */
151     
152     int o_symbol;                       /* Symbol type, (0 = no symbol) */
153     int o_symfreq;                      /* frequency to draw symbols at curve points */
154     
155     int o_legendbox;            /* controls whether legend is inside or outside of the axis extents */
156     int o_tag;                  /* controls whether to draw tag at end of axes */
157
158     // VALUE & SET variables 
159     double v_xmin, v_xmax, v_ymin, v_ymax;      /* user supplied axis endpoints */
160     bool   s_xmin, s_xmax, s_ymin, s_ymax;      /* TRUE is endpoint has been set */
161     double v_xtitle, v_ytitle;  /* NDC position to plot title */
162     bool   s_xtitle, s_ytitle;  /* TRUE if set position for title */
163     double v_xcross, v_ycross;  /* position that axes cross */
164     bool   s_xcross, s_ycross;  /* TRUE if set axes cross position */
165     double v_xlegend, v_ylegend;        /* upper-left position of legend box in NDC */
166     bool   s_xlegend, s_ylegend;        /* TRUE if set position of legend box */
167     int  v_lxfrac, v_lyfrac;    /* number of digits to right of decimal place */
168     bool s_lxfrac, s_lyfrac;    /* TRUE if set number of fractional digits */
169     double v_textsize;          /* size of text in NDC */
170     bool   s_textsize;          /* TRUE if user set size of text */
171
172     // Global variables
173     double charheight;  /* Height of characters in NDC */
174     double charwidth;   /* Height of characters in NDC */
175     double  xp_min, xp_max, yp_min, yp_max;             /* boundry of plot frame in NDC */
176     double  xa_min, xa_max, ya_min, ya_max;             /* extent of axes in NDC */
177     double  xgw_min, xgw_max, ygw_min, ygw_max; /* boundary of graph in input coords */
178     double  xgn_min, xgn_max, ygn_min, ygn_max; /* boundy of graph in NDC */
179     double xt_min, xt_max, yt_min, yt_max;              /* boundary of axis ticks */
180     double  xl_min, xl_max, yl_min, yl_max;             /* boundary of legend box */
181     double title_row;   /* y-coord of title row */
182     double xtl_ofs;             /* Offset y-coord of x tick labels from axis */
183     double ytl_ofs;             /* Offset x-coord of y tick labels from axis */
184     double xlbl_row;    /* row of x label in world coord */
185     double ylbl_col;    /* column of y label in world coord */
186     double xw_tickinc, yw_tickinc;      /* increment between major ticks in WC */
187     double xn_tickinc, yn_tickinc;      /* increment between major ticks in NDC */
188     int x_nint, y_nint; /* number of intervals along x & y axes */
189     int x_fldwid, x_frac;       /* numeric field sizes & number of digits   */
190     int y_fldwid, y_frac;       /* in fraction of number, used for printf() */
191     double xtl_wid, ytl_wid;    /* length of ticks labels in NDC */
192     double tl_height;   /* height of tick labels in NDC */
193     char x_numfmt[20];  /* format to print x tick labels */
194     char y_numfmt[20];  /* format to print y tick labels */
195
196     void drawAxes(void);
197     void symbol (int sym, double symwidth, double symheight);
198     void make_numfmt(char *fmtstr, int *fldwid, int *nfrac, double min, double max, int nint);
199     int axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp);
200
201     SGP& rSGP;
202
203     void clearCurves ();
204
205     bool ezcmd (char *comm);
206     int do_cmd(int lx);
207     void bad_option(char *opt);
208     void initPlotSettings();
209
210     static void initkw(void);
211     
212     int curveinteract;
213     static bool ezset_initialized;
214
215  public:
216     EZPlot (SGP& sgp);
217     ~EZPlot ();
218
219     int ezset (char *command);
220
221     void addCurve (const float* x, const double* y, int num);
222     void addCurve (const double* x, const float* y, int num);
223     void addCurve (const double* x, const double* y, int num);
224     void addCurve (const double* y, int n);
225
226     void plot ();
227 };      
228
229 /*----------------------------------------------------------------------*/
230 /*                      Codes from LEX                                  */
231 /*----------------------------------------------------------------------*/
232
233 #define S_DATA          2
234 #define S_HELP          3
235 #define S_EXIT          4
236
237 #define S_SOLID         -10
238 #define S_DASH          -11
239 #define S_NOLINE        -12
240 #define S_BLACK         -13
241 #define S_RED           -14
242 #define S_BLUE          -15
243 #define S_GREEN         -16
244 #define S_PEN           -17
245 #define S_SYMBOL        -18     
246 #define S_EVERY         -19
247 #define S_NONE          -20
248 #define S_CURVES        -21
249 #define S_UNKNOWN       -22
250 #define S_END           -23
251 #define S_LEGEND        -24
252 #define S_XLEGEND       -25
253 #define S_YLEGEND       -26
254 #define S_XLIN          -27
255 #define S_YLIN          -28
256 #define S_XLOG          -29
257 #define S_YLOG          -30
258 #define S_XLABEL        -31
259 #define S_YLABEL        -32
260 #define S_XLENGTH       -33
261 #define S_YLENGTH       -34
262 #define S_XTICKS        -35
263 #define S_YTICKS        -36
264 #define S_ABOVE         -37
265 #define S_LABEL         -38
266 #define S_BELOW         -39
267 #define S_NOLABEL       -40
268 #define S_RIGHT         -41
269 #define S_LEFT          -42
270 #define S_XAUTOSCALE    -43
271 #define S_YAUTOSCALE    -44
272 #define S_XMIN          -45
273 #define S_YMIN          -46
274 #define S_XMAX          -47
275 #define S_YMAX          -48
276 #define S_LXFRAC        -49
277 #define S_LYFRAC        -50
278 #define S_XCROSS        -51
279 #define S_YCROSS        -52
280 #define S_NOXAXIS       -53
281 #define S_NOYAXIS       -54
282 #define S_XPORIGIN      -55
283 #define S_YPORIGIN      -56
284 #define S_TITLE         -57
285 #define S_XTITLE        -58
286 #define S_YTITLE        -59
287 #define S_REPLOT        -60
288 #define S_CLEAR         -61
289 #define S_STORE         -62
290 #define S_RESTORE       -63
291 #define S_AMARK         -66
292 #define S_NO            -67
293 #define S_INTERACTIVE   -68
294 #define S_UNITS         -69
295 #define S_INCHES        -70     
296 #define S_USER          -71
297
298 #define S_BOX           -100
299 #define S_NOBOX         -101
300 #define S_GRID          -102
301 #define S_NOGRID        -103
302 #define S_MAJOR         -104
303 #define S_MINOR         -105
304 #define S_COLOR         -106
305 #define S_LEGENDBOX     -107
306 #define S_TAG           -108
307
308 #define S_TEXTSIZE      -120
309
310
311 #endif