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