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