1 /*****************************************************************************
4 ** File Name: ctsupport.h
5 ** Author: Kevin Rosenberg
6 ** Purpose: Header file for CT support library
7 ** Date Started: Dec. 83
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: ctsupport.h,v 1.19 2000/12/29 20:04:02 kevin Exp $
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.
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.
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 ******************************************************************************/
37 #include "msvc_compat.h"
40 #define STR_MAX_LEN 255
41 #define STR_SIZE STR_MAX_LEN+1
49 #include <algorithm>
\r
55 /*----------------------------------------------------------------------*/
57 #define SHOW(var, fmt) { cerr << "var = " << var << endl; }
59 /*----------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------*/
68 #define ERR_TRACE -1
\r
73 /*----------------------------------------------------------------------*/
76 /* codes for open command */
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
82 #define OPEN_RDONLY 0 /* other system use standard codes */
83 #define OPEN_WRONLY 1 /* for binary */
87 /*----------------------------------------------------------------------*/
89 #if !defined(O_BINARY) && !defined(MSVC)
94 #define S_IWRITE S_IWUSR
97 /*----------------------------------------------------------------------*/
99 #if defined(MSVC) || ! defined(SIZEOF_INT)
101 #define SIZEOF_LONG 4
102 #define SIZEOF_SHORT 2
103 #define SIZEOF_FLOAT 4
104 #define SIZEOF_DOUBLE 8
107 typedef signed char kint8;
108 typedef unsigned char kuint8;
112 typedef unsigned int kuint32;
113 #elif SIZEOF_LONG == 4
114 typedef long int kint32;
115 typedef unsigned int kuint32;
118 #if SIZEOF_SHORT == 2
119 typedef short int kint16;
120 typedef unsigned short int kuint16;
121 #elif SIZEOF_INT == 2
123 typedef unsigned int kuint16;
126 #if SIZEOF_FLOAT == 4
127 typedef float kfloat32;
129 #if SIZEOF_DOUBLE == 8
130 typedef double kfloat64;
135 fileBasename (const char* const filename)
137 const char* p = strrchr (filename, '/');
138 return (p ? p + 1 : filename);
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);
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);
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 */
165 #define TWOPI 6.28318530717958647692
166 #define SQRT2 1.414213562373095049
168 #define F_EPSILON 1.0E-6
169 #define D_EPSILON 1.0E-10
171 #define ASSUMEDZERO 1E-10
173 typedef double GRFMTX_2D[3][3];
174 typedef double GRFMTX_3D[4][4];
177 convertDegreesToRadians (double x)
178 { return (x * (PI/180.)); }
181 convertRadiansToDegrees (double x)
182 { return (x*(180./PI)); }
185 inline T nearest (double x)
186 { return (x > 0 ? static_cast<T>(x+0.5) : static_cast<T>(x-0.5)); }
188 inline int imax (int a, int b)
\r
189 { return (a >= b ? a : b); }
\r
191 inline double dmax (double a, double b)
\r
192 { return (a >= b ? a : b); }
\r
195 inline T clamp (T value, T lowerBounds, T upperBounds)
196 { return (value >= upperBounds ? upperBounds : (value <= lowerBounds ? lowerBounds : value )); }
199 inline T lineLength (T x1, T y1, T x2, T y2)
200 { return static_cast<T>( sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) ); }
203 inline void minmax_array (const T* array, const int n, T& min, T& max)
205 max = min = array[0];
207 for (int i = 1; i < n; i++)
210 else if (array[i] > max)
215 //////////////////////////////////////////////////////////////
216 // FUNTION DECLARATIONS
217 //////////////////////////////////////////////////////////////
220 bool clip_rect (double& x1, double& y1, double& x2, double& y2, const double rect[4]);
221 bool clip_segment (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
222 bool clip_sector (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
223 bool clip_circle (double& x1, double& y1, double& x2, double& y2, const double cx, const double cy, const double radius, double t1, double t2);
224 bool clip_triangle (double& x1, double& y1, double& x2, double& y2, const double u, const double v, const int clip_xaxis);
228 void indent_mtx2 (GRFMTX_2D m);
229 void xlat_mtx2 (GRFMTX_2D m, const double x, const double y);
230 void scale_mtx2 (GRFMTX_2D m, const double sx, const double sy);
231 void rot_mtx2 (GRFMTX_2D m, const double theta);
232 void mult_mtx2 (const GRFMTX_2D m1, const GRFMTX_2D m2, GRFMTX_2D result);
233 void xform_mtx2 (const GRFMTX_2D m, double& x, double& y);
234 void rotate2d (double x[], double y[], int pts, double angle);
235 void xlat2d (double x[], double y[], int pts, double xoffset, double yoffset);
236 void scale2d (double x[], double y[], int pts, double xfact, double yfact);
239 double normalizeAngle (double theta);
240 double integrateSimpson (const double xmin, const double xmax, const double *y, const int np);
241 void vectorNumericStatistics (std::vector<double> vec, const int nPoints, double& min, double& max, double& mean, double& mode, double& median, double& stddev);
\r
244 /*----------------------------------------------------------------------*/
246 /* screen character codes */
255 void cio_tone(double freq, double length);
258 void cio_put_c(int c);
259 void cio_put_cc(int c, int count);
260 void cio_put_str(const char *str);
263 unsigned int cio_kb_getc(void);
264 void cio_kb_ungetc(unsigned int c);
265 char *cio_kb_gets(char *str, int maxlen);
266 unsigned int cio_kb_waitc(const char *astr, int beep);
272 #define KEY_RETURN 13
273 #define KEY_ESCAPE 27
285 #define BACKSLASH '\\'
304 #define AMPERSAND '&'
315 #endif /* #ifndef CTSUPPORT_H */