r248: msvc changes
[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.17 2000/12/06 15:17:51 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 <cstdio>
31 #include <cctype>
32 #include <cmath>
33 #include <stddef.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     int 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     double m_dVP_xmin, m_dVP_ymin;
197     double m_dVP_xmax, m_dVP_ymax;
198     double m_dVP_xscale, m_dVP_yscale;
199     double m_xWorldScale, m_yWorldScale;
200
201     void drawAxes(void);
202     void symbol (int sym, double symwidth, double symheight);
203     void make_numfmt(char *fmtstr, int *fldwid, int *nfrac, double min, double max, int nint);
204     int axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp);
205
206     SGP& rSGP;
207
208     void clearCurves ();
209
210     bool ezcmd (char *comm);
211     bool do_cmd(int lx);
212     void bad_option(char *opt);
213     void initPlotSettings();
214
215     static void initkw(void);
216     
217     static bool ezset_initialized;
218
219     double convertWorldToNDC_X (double x)
220         { return xgn_min + (x - xgw_min) * m_xWorldScale; }
221
222     double convertWorldToNDC_Y (double y)
223         { return ygn_min + (y - ygw_min) * m_yWorldScale; }
224
225  public:
226     EZPlot (SGP& sgp);
227     ~EZPlot ();
228
229     int ezset (char *command);
230
231     void addCurve (const float* x, const double* y, int num);
232     void addCurve (const double* x, const float* y, int num);
233     void addCurve (const double* x, const double* y, int num);
234     void addCurve (const double* y, int n);
235     void addCurve (const float* y, int n);
236
237     void plot ();
238 };      
239
240 //----------------------------------------------------------------------
241 //                      Codes from LEX                                  
242 //----------------------------------------------------------------------
243
244 #define S_DATA          2
245 #define S_HELP          3
246 #define S_EXIT          4
247
248 #define S_SOLID         -10
249 #define S_DASH          -11
250 #define S_NOLINE        -12
251 #define S_BLACK         -13
252 #define S_RED           -14
253 #define S_BLUE          -15
254 #define S_GREEN         -16
255 #define S_PEN           -17
256 #define S_SYMBOL        -18     
257 #define S_EVERY         -19
258 #define S_NONE          -20
259 #define S_LEGEND        -24
260 #define S_XLEGEND       -25
261 #define S_YLEGEND       -26
262 #define S_XLIN          -27
263 #define S_YLIN          -28
264 #define S_XLOG          -29
265 #define S_YLOG          -30
266 #define S_XLABEL        -31
267 #define S_YLABEL        -32
268 #define S_XLENGTH       -33
269 #define S_YLENGTH       -34
270 #define S_XTICKS        -35
271 #define S_YTICKS        -36
272 #define S_ABOVE         -37
273 #define S_LABEL         -38
274 #define S_BELOW         -39
275 #define S_NOLABEL       -40
276 #define S_RIGHT         -41
277 #define S_LEFT          -42
278 #define S_XAUTOSCALE    -43
279 #define S_YAUTOSCALE    -44
280 #define S_XMIN          -45
281 #define S_YMIN          -46
282 #define S_XMAX          -47
283 #define S_YMAX          -48
284 #define S_LXFRAC        -49
285 #define S_LYFRAC        -50
286 #define S_XCROSS        -51
287 #define S_YCROSS        -52
288 #define S_NOXAXIS       -53
289 #define S_NOYAXIS       -54
290 #define S_XPORIGIN      -55
291 #define S_YPORIGIN      -56
292 #define S_TITLE         -57
293 #define S_XTITLE        -58
294 #define S_YTITLE        -59
295 #define S_REPLOT        -60
296 #define S_CLEAR         -61
297 #define S_STORE         -62
298 #define S_RESTORE       -63
299 #define S_AMARK         -66
300 #define S_NO            -67
301 #define S_INTERACTIVE   -68
302 #define S_UNITS         -69
303 #define S_INCHES        -70     
304 #define S_USER          -71
305
306 #define S_BOX           -100
307 #define S_NOBOX         -101
308 #define S_GRID          -102
309 #define S_NOGRID        -103
310 #define S_MAJOR         -104
311 #define S_MINOR         -105
312 #define S_COLOR         -106
313 #define S_LEGENDBOX     -107
314 #define S_TAG           -108
315
316 #define S_TEXTSIZE      -120
317
318
319 #endif