r309: plotfile changes
[ctsim.git] / include / ctsupport.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      File Name:      ctsupport.h
5 **      Author:         Kevin Rosenberg
6 **      Purpose:        Header file for CT support library
7 **      Date Started:   Dec. 83
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: ctsupport.h,v 1.15 2000/12/20 20:08:48 kevin Exp $
13 **
14 **
15 **  This program is free software; you can redistribute it and/or modify
16 **  it under the terms of the GNU General Public License (version 2) as
17 **  published by the Free Software Foundation.
18 **
19 **  This program is distributed in the hope that it will be useful,
20 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
21 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 **  GNU General Public License for more details.
23 **
24 **  You should have received a copy of the GNU General Public License
25 **  along with this program; if not, write to the Free Software
26 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 ******************************************************************************/
28
29 #ifndef CTSUPPORT_H
30 #define CTSUPPORT_H
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #ifdef MSVC
37 #include "msvc_compat.h"
38 #endif
39
40 #define STR_MAX_LEN 255
41 #define STR_SIZE    STR_MAX_LEN+1
42
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
46 #include <cstdarg>
47 #include <vector>\r
48 #include <algorithm>\r
49
50 #define TRUE    1
51 #define FALSE   0
52 #define OK      TRUE
53
54 /*----------------------------------------------------------------------*/
55
56 #define SHOW(var, fmt)  { cerr << "var = " << var << endl; }
57
58 /*----------------------------------------------------------------------*/
59
60 #define NEWLINE '\n'
61 #define TAB     '\t'
62 #define EOS     '\0'
63 #define BLANK   ' '
64
65 /*----------------------------------------------------------------------*/
66
67 #define ERR_WARNING     0
68 #define ERR_SEVERE      1
69 #define ERR_FATAL       2
70
71 /*----------------------------------------------------------------------*/
72
73
74 /* codes for open command */
75 #ifdef MSVC
76 #define OPEN_RDONLY  O_RDONLY                   /* other system use standard codes */
77 #define OPEN_WRONLY  O_WRONLY                   /* for binary */
78 #define OPEN_RDWR    O_RDWR
79 #else
80 #define OPEN_RDONLY  0                  /* other system use standard codes */
81 #define OPEN_WRONLY  1                  /* for binary */
82 #define OPEN_RDWR    2
83 #endif
84
85 /*----------------------------------------------------------------------*/
86
87 #if !defined(O_BINARY) && !defined(MSVC)
88 #define O_BINARY (0)
89 #endif
90
91 #ifndef S_IWRITE
92 #define S_IWRITE S_IWUSR
93 #endif
94
95 /*----------------------------------------------------------------------*/
96
97 #if defined(MSVC) || ! defined(SIZEOF_INT)
98    #define SIZEOF_INT 4
99    #define SIZEOF_LONG 4
100    #define SIZEOF_SHORT 2
101    #define SIZEOF_FLOAT 4
102    #define SIZEOF_DOUBLE 8
103 #endif
104
105 typedef signed char kint8;
106 typedef unsigned char kuint8;
107
108 #if SIZEOF_INT == 4
109     typedef int kint32;
110     typedef unsigned int kuint32;
111 #elif SIZEOF_LONG == 4
112     typedef long int kint32;
113     typedef unsigned int kuint32;
114 #endif
115
116 #if SIZEOF_SHORT == 2
117     typedef short int kint16;
118     typedef unsigned short int kuint16;
119 #elif SIZEOF_INT == 2
120     typedef int kint16;
121     typedef unsigned int kuint16;
122 #endif
123
124 #if SIZEOF_FLOAT == 4
125     typedef float kfloat32;
126 #endif
127 #if SIZEOF_DOUBLE == 8
128     typedef double kfloat64;
129 #endif
130
131
132 inline const char* 
133 fileBasename (const char* const filename)
134 {
135   const char* p = strrchr (filename, '/');
136   return (p ? p + 1 : filename);
137 }
138
139
140 /* strfuncs.cpp */
141 char* str_skip_head(const char* str, const char* const charlist);
142 char* str_skip_head(const char* str, char* charlist);
143 char *str_lower(char *s);
144 char *str_wrm_tail(char *str);
145 char *str_rm_tail(char *str, const char* const charlist);
146 char *str_upper(char *str);
147
148 /* syserror.cpp */
149 void sys_error(int severity, const char *msg, ...);
150 void sys_verror(int severity, const char *msg, va_list arg);
151 void sys_error_level(int severity);
152
153 // Math Section
154
155 #include <cmath>
156
157 #define PI      3.14159265358979323846
158 #define HALFPI  1.57079632679489661923  /* PI divided by 2 */
159 #define QUARTPI 0.78539816339744830962  /* PI divided by 4 */
160 #define I_PI    0.31830988618379067154  /* Inverse of PI */
161 #define I_PID2  0.63661977236758134308  /* Inverse of PID2 */
162  
163 #define TWOPI   6.28318530717958647692
164 #define SQRT2   1.414213562373095049
165
166 #define F_EPSILON       1.0E-6
167 #define D_EPSILON       1.0E-10
168
169 #define ASSUMEDZERO  1E-10
170
171 typedef double GRFMTX_2D[3][3];
172 typedef double GRFMTX_3D[4][4];
173
174 inline double 
175 convertDegreesToRadians (double x)
176 { return (x * (PI/180.)); }
177
178 inline double
179 convertRadiansToDegrees (double x)
180 { return (x*(180./PI)); }
181
182 template<class T>
183 inline T nearest (double x)
184 { return (x > 0 ? static_cast<T>(x+0.5) : static_cast<T>(x-0.5)); }
185
186 inline int imax (int a, int b)\r
187 { return (a >= b ? a : b); }\r
188 \r
189 inline double dmax (double a, double b)\r
190 { return (a >= b ? a : b); }\r
191 \r
192 template<class T>
193 inline T clamp (T value, T lowerBounds, T upperBounds)
194 { return (value >= upperBounds ? upperBounds : (value <= lowerBounds ? lowerBounds : value )); }
195
196 template<class T>
197 inline T lineLength (T x1, T y1, T x2, T y2)
198 { return static_cast<T>( sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) ); }
199
200 template<class T>
201 inline void minmax_array (const T* array, const int n, T& min, T& max)
202 {
203   max = min = array[0];
204
205   for (int i = 1; i < n; i++)
206     if (array[i] < min)
207       min = array[i];
208     else if (array[i] > max)
209       max = array[i];
210 }
211
212
213 //////////////////////////////////////////////////////////////
214 // FUNTION DECLARATIONS
215 //////////////////////////////////////////////////////////////
216
217 // clip.cpp 
218 bool clip_rect (double& x1, double& y1, double& x2, double& y2, const double rect[4]);
219 bool clip_segment (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
220 bool clip_sector (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
221 bool clip_circle (double& x1, double& y1, double& x2, double& y2, const double cx, const double cy, const double radius, double t1, double t2);
222 bool clip_triangle (double& x1, double& y1, double& x2, double& y2, const double u, const double v, const int clip_xaxis);
223
224
225 // xform.cpp 
226 void indent_mtx2 (GRFMTX_2D m);
227 void xlat_mtx2 (GRFMTX_2D m, const double x, const double y);
228 void scale_mtx2 (GRFMTX_2D m, const double sx, const double sy);
229 void rot_mtx2 (GRFMTX_2D m, const double theta);
230 void mult_mtx2 (const GRFMTX_2D m1, const GRFMTX_2D m2, GRFMTX_2D result);
231 void xform_mtx2 (const GRFMTX_2D m, double& x, double& y);
232 void rotate2d (double x[], double y[], int pts, double angle);
233 void xlat2d (double x[], double y[], int pts, double xoffset, double yoffset);
234 void scale2d (double x[], double y[], int pts, double xfact, double yfact);
235
236 // mathfuncs.cpp
237 double normalizeAngle (double theta);
238 double integrateSimpson (const double xmin, const double xmax, const double *y, const int np);
239 void vectorNumericStatistics (std::vector<double> vec, double& min, double& max, double& mean, double& mode, double& median, double& stddev);\r
240
241
242 /*----------------------------------------------------------------------*/
243
244 /* screen character codes */
245
246 #define SC_BKSP           8
247 #define SC_TAB            9
248 #define SC_BLANK        ' '
249
250
251 /* audio.cpp */
252 void cio_beep(void);
253 void cio_tone(double freq, double length);
254
255 /* crtput.cpp */
256 void cio_put_c(int c);
257 void cio_put_cc(int c, int count);
258 void cio_put_str(const char *str);
259
260 /* kbget.cpp */
261 unsigned int cio_kb_getc(void);
262 void cio_kb_ungetc(unsigned int c);
263 char *cio_kb_gets(char *str, int maxlen);
264 unsigned int cio_kb_waitc(const char *astr, int beep);
265
266 // Keyboard Section
267
268 #define KEY_BKSP         8
269 #define KEY_TAB          9
270 #define KEY_RETURN      13
271 #define KEY_ESCAPE      27
272
273 // ASCII Section
274
275 #define BACKSPACE  8
276 #define LF      0x0A
277 #define CR      0x0D
278 #define BELL    0x07
279
280 #define SQUOTE    '\''
281 #define DQUOTE    '\"'
282 #define BSLASH    '\\'
283 #define BACKSLASH '\\'
284 #define SHARP     '#'
285 #define SLASH     '/'
286 #define ASTERICK  '*'
287 #define COLON     ':'
288 #define LBRACE    '{'
289 #define RBRACE    '}'
290 #define LPAREN    '('
291 #define RPAREN    ')'
292 #define LBRACK    '['
293 #define RBRACK    ']'
294 #define LANBRACK  '<'
295 #define RANBRACK  '>'
296 #define SEMICOL   ';'
297 #define UNDERLIN  '_'
298 #define COMMA     ','
299 #define CARET     '^'
300 #define TILDE     '~'
301 #define ATSIGN    '@'
302 #define AMPERSAND  '&'
303 #define EXCLAM    '!'
304 #define DOLLAR    '$'
305 #define PERCENT   '%'
306 #define PLUS      '+'
307 #define HYPHEN    '-'
308 #define EQUALS    '='
309 #define QUESTION  '?'
310 #define PERIOD    '.'
311 #define VERTBAR   '|'
312
313 #endif  /* #ifndef CTSUPPORT_H */