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