r11859: Canonicalize whitespace
[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-2001 Kevin Rosenberg
9 **
10 **  $Id$
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 #include "pol.h"
37
38
39 class EZPlotCurve {
40 public:
41   double *x;
42   double *y;
43   int m_iPointCount;
44
45   EZPlotCurve (const double* x, const double* y, int n);
46
47   ~EZPlotCurve();
48 };
49
50 //----------------------------------------------------------------------
51 //                             GLOBAL VARIABLES
52 //----------------------------------------------------------------------
53
54 // axis definitions
55 enum {
56   LINEAR =      1,              // linear axis
57   LOG,          // logrithmic axis
58   NOAXIS,       // don't plot axis
59 };
60
61 // tick definitions
62 enum {
63   ABOVE = 1,
64  BELOW,
65  RIGHT,
66  LEFT,
67 };
68
69 // line types
70 enum {
71  NOLINE =       0,
72  SOLID,
73  DASH,
74  DASH1,
75  DASH2,
76  DASH3,
77  DASH4,
78 };
79
80 // symbol definitions
81 enum {
82  SB_CROSS = 1,
83  SB_PLUS,
84  SB_BOX,
85  SB_CIRCLE,
86  SB_ERRORBAR,
87  SB_POINT,
88  MAXSYMBOL,
89 };
90
91 enum {
92  INSIDE = 1,            // values of o_legendbox
93  OUTSIDE,
94  NOLEGEND,
95 };
96
97
98 struct KeywordCodeTable {
99   char* keyword;
100   int code;
101 };
102
103 /*-----------------------------------------------------------------------------
104 *                               GLOBAL VARIABLES
105 *
106 * Naming Convention:
107 *       i_   Internal variable
108 *               Not user changable
109 *       o_   Option variable
110 *               Normal variable that is user modifiable
111 *               These variables must always have a valid value
112 *       d_   Device variable
113 *               Variables controlling devices
114 *      clr_ Color variable
115 *               Holds a color value
116 *       c_   Character string variable
117 *               Contains a character string
118 *       v_   Value variable
119 *               User modifiable variable associated with the set variable (s_)
120 *               These variables do not always have a valid value
121 *               These variables change assumption EZPLOT makes about the plot
122 *       s_   Set variable.
123 *               TRUE if associated value variable (v_) has been set by the user
124 *---------------------------------------------------------------------------*/
125
126 #include <vector>
127
128 typedef std::vector<EZPlotCurve*>::iterator EZPlotCurveIterator;
129 typedef std::vector<EZPlotCurve*>::const_iterator EZPlotCurveConstIterator;
130
131 class SGP;
132 class EZPlot {
133 private:
134   //----------------------------------------------------------------------
135   //                    POL Codes
136   //----------------------------------------------------------------------
137
138   enum {
139     S_DATA = 2,
140       S_HELP,
141       S_EXIT,
142       S_CURVE,
143       S_SOLID,
144       S_DASH,
145       S_NOLINE,
146       S_BLACK,
147       S_RED,
148       S_GREEN,
149       S_BLUE,
150       S_SYMBOL,
151       S_PEN,
152       S_EVERY,
153       S_NONE,
154       S_LEGEND,
155       S_XLEGEND,
156       S_YLEGEND,
157       S_XLIN,
158       S_YLIN,
159       S_XLOG,
160       S_YLOG,
161       S_XLABEL,
162       S_YLABEL,
163       S_XLENGTH,
164       S_YLENGTH,
165       S_XTICKS,
166       S_YTICKS,
167       S_ABOVE,
168       S_LABEL,
169       S_BELOW,
170       S_NOLABEL,
171       S_RIGHT,
172       S_LEFT,
173       S_XAUTOSCALE,
174       S_YAUTOSCALE,
175       S_XMIN,
176       S_YMIN,
177       S_XMAX,
178       S_YMAX,
179       S_LXFRAC,
180       S_LYFRAC,
181       S_XCROSS,
182       S_YCROSS,
183       S_NOXAXIS,
184       S_NOYAXIS,
185       S_XPORIGIN,
186       S_YPORIGIN,
187       S_TITLE,
188       S_XTITLE,
189       S_YTITLE,
190       S_REPLOT,
191       S_CLEAR,
192       S_STORE,
193       S_RESTORE,
194       S_AMARK,
195       S_NO,
196       S_INTERACTIVE,
197       S_UNITS,
198       S_INCHES,
199       S_USER,
200       S_BOX,
201       S_NOBOX,
202       S_GRID,
203       S_NOGRID,
204       S_MAJOR,
205       S_MINOR,
206       S_COLOR,
207       S_LEGENDBOX,
208       S_TAG,
209       S_TEXTSIZE,
210   };
211
212   static const struct KeywordCodeTable m_sKeywords[];
213   static const int NKEYS;
214
215   std::vector<class EZPlotCurve*> m_vecCurves;
216   std::vector<int> m_veciColor;
217   std::vector<bool> m_vecbColorSet;
218   std::vector<int> m_veciSymbol;
219   std::vector<bool> m_vecbSymbolSet;
220   std::vector<int> m_veciSymbolFreq;
221   std::vector<bool> m_vecbSymbolFreqSet;
222   std::vector<int> m_veciLinestyle;
223   std::vector<bool> m_vecbLinestyleSet;
224   std::vector<std::string> m_vecsLegend;
225   std::vector<bool> m_vecbLegendSet;
226
227   int getColor (unsigned int iCurve) const;
228   int getSymbol (unsigned int iCurve) const;
229   const std::string* getLegend (unsigned int iCurve) const;
230   int getSymbolFreq (unsigned int iCurve) const;
231   int getLinestyle (unsigned int iCurve) const;
232
233   void setColor (unsigned int iCurve, int iColor);
234   void setSymbol (unsigned int iCurve, int iSymbol);
235   void setSymbolFreq (unsigned int iCurve, int iSymbolFreq);
236   void setLinestyle (unsigned int iCurve, int iLinestyle);
237   void setLegend (unsigned int iCurve, const std::string& strLegend);
238   void setLegend (unsigned int iCurve, const char* const pszLegend);
239
240   int m_iCurrentCurve;
241
242   // Colors
243   int clr_axis;                 // color of all axis lines
244   int clr_title;                        // color of main title
245   int clr_label;                        // color of axis labels
246   int clr_legend;                       // color of legend box
247   int clr_grid;                 // color of grid lines
248   int clr_number;                       // color of axis number labels
249
250   // Options
251   double o_xporigin, o_yporigin;        // origin of plot frame in NDC
252   double o_xlength, o_ylength;  // length of plot frame in NDC
253
254   std::string c_xlabel; // label for x axis
255   std::string c_ylabel; // label for y axis
256   std::string c_title;          // title to print above graph
257
258   int o_linestyle, o_color;     // style to use for curves all subsequent curves to EZPLOT
259   int o_xaxis, o_yaxis;         // Specifies where axis & labels are drawn
260   bool o_grid;                  // Flag to draw a grid at major ticks
261   bool o_box;                   // Flag to draw a box around the graph
262
263   int o_xticks, o_yticks;               // direction to draw tick marks
264   bool o_xtlabel, o_ytlabel;    // TRUE if label tick marks
265
266   int o_xmajortick, o_ymajortick;       // number of major ticks to draw
267   int o_xminortick, o_yminortick;       // number of minor ticks between major ticks
268
269   int o_symbol;                 // Symbol type, (0 = no symbol)
270   int o_symfreq;                        // frequency to draw symbols at curve points
271
272   int o_legendbox;              // controls whether legend is inside or outside of the axis extents
273   int o_tag;                    // controls whether to draw tag at end of axes
274
275   // VALUE & SET variables
276   double v_xmin, v_xmax, v_ymin, v_ymax;        // user supplied axis endpoints
277   bool   s_xmin, s_xmax, s_ymin, s_ymax;        // TRUE is endpoint has been set
278   double v_xtitle, v_ytitle;    // NDC position to plot title
279   bool   s_xtitle, s_ytitle;    // TRUE if set position for title
280   double v_xcross, v_ycross;    // position that axes cross
281   bool   s_xcross, s_ycross;    // TRUE if set axes cross position
282   double v_xlegend, v_ylegend;  // upper-left position of legend box in NDC
283   bool   s_xlegend, s_ylegend;  // TRUE if set position of legend box
284   int  v_lxfrac, v_lyfrac;      // number of digits to right of decimal place
285   bool s_lxfrac, s_lyfrac;      // TRUE if set number of fractional digits
286   double v_textsize;            // size of text in NDC
287   bool   s_textsize;            // TRUE if user set size of text
288
289   // Global variables
290   double charheight;    // Height of characters in NDC
291   double charwidth;     // Height of characters in NDC
292   double  xp_min, xp_max, yp_min, yp_max;       // boundry of plot frame in NDC
293   double  xa_min, xa_max, ya_min, ya_max;       // extent of axes in NDC
294   double  xgw_min, xgw_max, ygw_min, ygw_max;   // boundary of graph in input coords
295   double  xgn_min, xgn_max, ygn_min, ygn_max;   // boundy of graph in NDC
296   double xt_min, xt_max, yt_min, yt_max;        // boundary of axis ticks
297   double  xl_min, xl_max, yl_min, yl_max;       // boundary of legend box
298   double title_row;     // y-coord of title row
299   double xtl_ofs;               // Offset y-coord of x tick labels from axis
300   double ytl_ofs;               // Offset x-coord of y tick labels from axis
301   double xlbl_row;      // row of x label in world coord
302   double ylbl_col;      // column of y label in world coord
303   double xw_tickinc, yw_tickinc;        // increment between major ticks in WC
304   double xn_tickinc, yn_tickinc;        // increment between major ticks in NDC
305   int x_nint, y_nint;   // number of intervals along x & y axes
306   int x_fldwid, x_frac; // numeric field sizes & number of digits
307   int y_fldwid, y_frac; // in fraction of number, used for printf()
308   double xtl_wid, ytl_wid;      // length of ticks labels in NDC
309   double tl_height;     // height of tick labels in NDC
310   char x_numfmt[20];    // format to print x tick labels
311   char y_numfmt[20];    // format to print y tick labels
312
313   double m_dVP_xmin, m_dVP_ymin;
314   double m_dVP_xmax, m_dVP_ymax;
315   double m_dVP_xscale, m_dVP_yscale;
316   double m_xWorldScale, m_yWorldScale;
317
318   void drawAxes();
319   void symbol (int sym, double symwidth, double symheight);
320   void make_numfmt(char *fmtstr, int *fldwid, int *nfrac, double min, double max, int nint);
321   int axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp);
322
323   SGP* m_pSGP;
324   POL m_pol;
325
326   void clearCurves ();
327
328   bool ezcmd (const char* const comm);
329   bool do_cmd(int lx);
330   void bad_option(char *opt);
331   void initPlotSettings();
332
333   void initKeywords ();
334
335   double convertWorldToNDC_X (double x)
336   { return xgn_min + (x - xgw_min) * m_xWorldScale; }
337
338   double convertWorldToNDC_Y (double y)
339   { return ygn_min + (y - ygw_min) * m_yWorldScale; }
340
341  public:
342    EZPlot ();
343    ~EZPlot ();
344
345    bool ezset (const std::string& command);
346    bool ezset (const char* const command);
347
348    void addCurve (const float* x, const double* y, int num);
349    void addCurve (const double* x, const float* y, int num);
350    void addCurve (const double* x, const double* y, int num);
351    void addCurve (const double* y, int n);
352    void addCurve (const float* y, int n);
353
354    void plot (SGP* pSGP);
355 };
356
357 #endif