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