9df0c68db548251c05b63e4e97bc40ad6987d468
[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-2001 Kevin Rosenberg
11 **
12 **  $Id: ctsupport.h,v 1.25 2003/01/22 22:15:10 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 <string>
48 #include <vector>
49 #include <algorithm>
50
51 #if defined(MSVC) || HAVE_SSTREAM
52 #include <sstream>
53 #else
54 #include <sstream_subst>
55 #endif
56
57 #ifndef TRUE
58 #define TRUE    1
59 #endif
60 #ifndef FALSE
61 #define FALSE   0
62 #endif
63 #define OK      TRUE
64
65 /*----------------------------------------------------------------------*/
66
67 #define SHOW(var, fmt)  { cerr << "var = " << var << endl; }
68
69 /*----------------------------------------------------------------------*/
70
71 #define NEWLINE '\n'
72 #define TAB     '\t'
73 #define EOS     '\0'
74 #define BLANK   ' '
75
76 /*----------------------------------------------------------------------*/
77
78 #define ERR_TRACE -1
79 #define ERR_WARNING     0
80 #define ERR_SEVERE      1
81 #define ERR_FATAL       2
82
83 /*----------------------------------------------------------------------*/
84
85
86 /* codes for open command */
87 #ifdef MSVC
88 #define OPEN_RDONLY  O_RDONLY                   /* other system use standard codes */
89 #define OPEN_WRONLY  O_WRONLY                   /* for binary */
90 #define OPEN_RDWR    O_RDWR
91 #else
92 #define OPEN_RDONLY  0                  /* other system use standard codes */
93 #define OPEN_WRONLY  1                  /* for binary */
94 #define OPEN_RDWR    2
95 #endif
96
97 /*----------------------------------------------------------------------*/
98
99 #if !defined(O_BINARY) && !defined(MSVC)
100 #define O_BINARY (0)
101 #endif
102
103 #ifndef S_IWRITE
104 #define S_IWRITE S_IWUSR
105 #endif
106
107 /*----------------------------------------------------------------------*/
108
109 #if defined(MSVC) || ! defined(SIZEOF_INT)
110    #define SIZEOF_INT 4
111    #define SIZEOF_LONG 4
112    #define SIZEOF_SHORT 2
113    #define SIZEOF_FLOAT 4
114    #define SIZEOF_DOUBLE 8
115 #endif
116
117 typedef signed char kint8;
118 typedef unsigned char kuint8;
119
120 #if SIZEOF_INT == 4
121     typedef int kint32;
122     typedef unsigned int kuint32;
123 #elif SIZEOF_LONG == 4
124     typedef long int kint32;
125     typedef unsigned int kuint32;
126 #endif
127
128 #if SIZEOF_SHORT == 2
129     typedef short int kint16;
130     typedef unsigned short int kuint16;
131 #elif SIZEOF_INT == 2
132     typedef int kint16;
133     typedef unsigned int kuint16;
134 #endif
135
136 #if SIZEOF_FLOAT == 4
137     typedef float kfloat32;
138 #endif
139 #if SIZEOF_DOUBLE == 8
140     typedef double kfloat64;
141 #endif
142
143
144 inline const char* 
145 fileBasename (const char* const filename)
146 {
147   const char* p = strrchr (filename, '/');
148   return (p ? p + 1 : filename);
149 }
150
151
152 /* strfuncs.cpp */
153 char* str_skip_head(const char* str, const char* const charlist);
154 char* str_skip_head(const char* str, char* charlist);
155 char *str_lower(char *s);
156 char *str_wrm_tail(char *str);
157 char *str_rm_tail(char *str, const char* const charlist);
158 char *str_upper(char *str);
159
160 /* syserror.cpp */
161 void sys_error(int severity, const char *msg, ...);
162 void sys_verror (std::string& strOutput, int severity, const char *msg, va_list arg);
163 void sys_error_level(int severity);
164
165 // Math Section
166
167 #include <cmath>
168
169 #define PI      3.14159265358979323846
170 #define HALFPI  1.57079632679489661923  /* PI divided by 2 */
171 #define QUARTPI 0.78539816339744830962  /* PI divided by 4 */
172 #define I_PI    0.31830988618379067154  /* Inverse of PI */
173 #define I_PID2  0.63661977236758134308  /* Inverse of PID2 */
174  
175 #define TWOPI   6.28318530717958647692
176 #define SQRT2   1.414213562373095049
177
178 #define F_EPSILON       1.0E-6
179 #define D_EPSILON       1.0E-10
180
181 #define ASSUMEDZERO  1E-10
182
183 typedef double GRFMTX_2D[3][3];
184 typedef double GRFMTX_3D[4][4];
185
186 inline double 
187 convertDegreesToRadians (double x)
188 { return (x * (PI/180.)); }
189
190 inline double
191 convertRadiansToDegrees (double x)
192 { return (x*(180./PI)); }
193
194 template<class T>
195 inline T nearest (double x)
196 { return (x > 0 ? static_cast<T>(x+0.5) : static_cast<T>(x-0.5)); }
197
198 template<class T>
199 inline T maxValue (T x, T y)
200 { return (x > y ? x : y); }
201
202 inline bool isEven (int n)
203 { return (n % 2) == 0; }
204
205 inline bool isOdd (int n)
206 { return (n % 2) != 0; }
207
208 #if 0
209 inline bool isEven (long n)
210 { return (n % 2) == 0; }
211
212 inline bool isOdd (long n)
213 { return (n % 2) != 0; }
214 #endif
215
216 inline int imax (int a, int b)
217 { return (a >= b ? a : b); }
218
219 inline double dmax (double a, double b)
220 { return (a >= b ? a : b); }
221
222 template<class T>
223 inline T clamp (T value, T lowerBounds, T upperBounds)
224 { return (value >= upperBounds ? upperBounds : (value <= lowerBounds ? lowerBounds : value )); }
225
226 template<class T>
227 inline T lineLength (T x1, T y1, T x2, T y2)
228 { return static_cast<T>( sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) ); }
229
230 template<class T>
231 inline void minmax_array (const T* array, const int n, T& min, T& max)
232 {
233   max = min = array[0];
234
235   for (int i = 1; i < n; i++)
236     if (array[i] < min)
237       min = array[i];
238     else if (array[i] > max)
239       max = array[i];
240 }
241
242
243 //////////////////////////////////////////////////////////////
244 // FUNTION DECLARATIONS
245 //////////////////////////////////////////////////////////////
246
247 // clip.cpp 
248 bool clip_rect (double& x1, double& y1, double& x2, double& y2, const double rect[4]);
249 bool clip_segment (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
250 bool clip_sector (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
251 bool clip_circle (double& x1, double& y1, double& x2, double& y2, const double cx, const double cy, const double radius, double t1, double t2);
252 bool clip_triangle (double& x1, double& y1, double& x2, double& y2, const double u, const double v, const int clip_xaxis);
253
254
255 // xform.cpp 
256 void indent_mtx2 (GRFMTX_2D m);
257 void xlat_mtx2 (GRFMTX_2D m, const double x, const double y);
258 void scale_mtx2 (GRFMTX_2D m, const double sx, const double sy);
259 void rot_mtx2 (GRFMTX_2D m, const double theta);
260 void mult_mtx2 (const GRFMTX_2D m1, const GRFMTX_2D m2, GRFMTX_2D result);
261 void xform_mtx2 (const GRFMTX_2D m, double& x, double& y);
262 void rotate2d (double x[], double y[], int pts, double angle);
263 void xlat2d (double x[], double y[], int pts, double xoffset, double yoffset);
264 void scale2d (double x[], double y[], int pts, double xfact, double yfact);
265
266 // mathfuncs.cpp
267 double normalizeAngle (double theta);
268 double integrateSimpson (const double xmin, const double xmax, const double *y, const int np);
269 void vectorNumericStatistics (std::vector<double> vec, const int nPoints, double& min, double& max, double& mean, double& mode, double& median, double& stddev);
270
271
272 /*----------------------------------------------------------------------*/
273
274 /* screen character codes */
275
276 #define SC_BKSP           8
277 #define SC_TAB            9
278 #define SC_BLANK        ' '
279
280
281 /* audio.cpp */
282 void cio_beep(void);
283 void cio_tone(double freq, double length);
284
285 /* crtput.cpp */
286 void cio_put_c(int c);
287 void cio_put_cc(int c, int count);
288 void cio_put_str(const char *str);
289
290 /* kbget.cpp */
291 unsigned int cio_kb_getc(void);
292 void cio_kb_ungetc(unsigned int c);
293 char *cio_kb_gets(char *str, int maxlen);
294 unsigned int cio_kb_waitc(const char *astr, int beep);
295
296 // Keyboard Section
297
298 #define KEY_BKSP         8
299 #define KEY_TAB          9
300 #define KEY_RETURN      13
301 #define KEY_ESCAPE      27
302
303 // ASCII Section
304
305 #define BACKSPACE  8
306 // #define LF   0x0A
307 // #define CR   0x0D
308 #define BELL    0x07
309
310 #define SQUOTE    '\''
311 #define DQUOTE    '\"'
312 #define BSLASH    '\\'
313 #define BACKSLASH '\\'
314 #define SHARP     '#'
315 #define SLASH     '/'
316 #define ASTERICK  '*'
317 #define COLON     ':'
318 #define LBRACE    '{'
319 #define RBRACE    '}'
320 #define LPAREN    '('
321 #define RPAREN    ')'
322 #define LBRACK    '['
323 #define RBRACK    ']'
324 #define LANBRACK  '<'
325 #define RANBRACK  '>'
326 #define SEMICOL   ';'
327 #define UNDERLIN  '_'
328 #define COMMA     ','
329 #define CARET     '^'
330 #define TILDE     '~'
331 #define ATSIGN    '@'
332 #define AMPERSAND  '&'
333 #define EXCLAM    '!'
334 #define DOLLAR    '$'
335 #define PERCENT   '%'
336 #define PLUS      '+'
337 #define HYPHEN    '-'
338 #define EQUALS    '='
339 #define QUESTION  '?'
340 #define PERIOD    '.'
341 #define VERTBAR   '|'
342
343 #endif  /* #ifndef CTSUPPORT_H */