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