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