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.3 2000/06/19 19:10:08 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 #define snprintf _snprintf
40 #define STR_MAX_LEN 255
41 #define STR_SIZE STR_MAX_LEN+1
55 /*----------------------------------------------------------------------*/
57 #define SHOW(var, fmt) { cerr << "var = " << var << endl; }
59 /*----------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------*/
69 int hour, minute, second, ms;
73 int year, month, date, dow;
81 typedef struct time_st TIME;
82 typedef struct date_st DATE;
83 typedef struct timedate_st TIMEDATE;
85 /*----------------------------------------------------------------------*/
91 /*----------------------------------------------------------------------*/
94 /* codes for open command */
96 #define OPEN_RDONLY O_RDONLY /* other system use standard codes */
97 #define OPEN_WRONLY O_WRONLY /* for binary */
98 #define OPEN_RDWR O_RDWR
100 #define OPEN_RDONLY 0 /* other system use standard codes */
101 #define OPEN_WRONLY 1 /* for binary */
105 /*----------------------------------------------------------------------*/
112 #define S_IWRITE S_IWUSR
115 /*----------------------------------------------------------------------*/
117 #if defined(MICROSOFT) || ! defined(SIZEOF_INT)
119 #define SIZEOF_LONG 4
120 #define SIZEOF_SHORT 2
121 #define SIZEOF_FLOAT 4
122 #define SIZEOF_DOUBLE 8
125 typedef signed char kint8;
126 typedef unsigned char kuint8;
130 typedef unsigned int kuint32;
131 #elif SIZEOF_LONG == 4
132 typedef long int kint32;
133 typedef unsigned int kuint32;
136 #if SIZEOF_SHORT == 2
137 typedef short int kint16;
138 typedef unsigned short int kuint16;
139 #elif SIZEOF_INT == 2
141 typedef unsigned int kuint16;
144 #if SIZEOF_FLOAT == 4
145 typedef float kfloat32;
147 #if SIZEOF_DOUBLE == 8
148 typedef double kfloat64;
153 bool file_exists(const char* fname);
154 const char* fileBasename(const char* filename);
155 FILE *sys_fopen(const char *filename, const char *mode, const char *progname);
158 char* str_skip_head(const char* str, const char* const charlist);
159 char* str_skip_head(const char* str, char* charlist);
160 char *str_lower(char *s);
161 char *str_wrm_tail(char *str);
162 char *str_rm_tail(char *str, const char* const charlist);
163 char *str_upper(char *str);
166 void sys_error(int severity, const char *msg, ...);
167 void sys_verror(int severity, const char *msg, va_list arg);
168 void sys_error_level(int severity);
171 DATE *td_get_date(DATE *d);
172 TIME *td_get_time(TIME *t);
173 double td_current_sec(void);
174 double td_time_to_sec(TIME *t);
175 TIME *td_time_sub(const TIME *t1, const TIME *t2, TIME *tdiff);
176 TIME *td_time_add(const TIME *t1, const TIME *t2, TIME *tsum);
177 TIME *td_time_copy(TIME *to, const TIME *from);
178 TIME *td_time_norm(TIME *t);
179 void td_get_tmdt(TIMEDATE *td);
180 const char *td_str_tmdt(const TIMEDATE *td);
181 const char *td_str_time(const TIME *t);
182 const char *td_str_stime(const TIME *t);
183 const char *td_str_date(const DATE *d);
184 char *td_str_cdate(DATE *d);
185 char *td_month_name(int n);
186 char *td_day_name(int n);
193 #define PI 3.14159265358979323846
194 #define HALFPI 1.57079632679489661923 /* PI divided by 2 */
195 #define QUARTPI 0.78539816339744830962 /* PI divided by 4 */
196 #define I_PI 0.31830988618379067154 /* Inverse of PI */
197 #define I_PID2 0.63661977236758134308 /* Inverse of PID2 */
199 #define TWOPI 6.28318530717958647692
200 #define SQRT2 1.414213562373095049
202 #define F_EPSILON 1.0E-6
203 #define D_EPSILON 1.0E-10
205 #define ASSUMEDZERO 1E-10
207 typedef double GRFMTX_2D[3][3];
208 typedef double GRFMTX_3D[4][4];
211 convertDegreesToRadians (double x)
212 { return (x * (PI/180.)); }
215 convertRadiansToDegrees (double x)
216 { return (x*(180./PI)); }
219 inline T nearest (double x)
220 { return (x > 0 ? static_cast<T>(x+0.5) : static_cast<T>(x-0.5)); }
223 inline T clamp (T value, T lowerBounds, T upperBounds)
224 { return (value >= upperBounds ? upperBounds : (value <= lowerBounds ? lowerBounds : value )); }
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)) ); }
231 inline void minmax_array (const T* array, const int n, T& min, T& max)
233 max = min = array[0];
235 for (int i = 1; i < n; i++)
238 else if (array[i] > max)
243 //////////////////////////////////////////////////////////////
244 // FUNTION DECLARATIONS
245 //////////////////////////////////////////////////////////////
248 int clip_rect (double& x1, double& y1, double& x2, double& y2, const double rect[4]);
249 int clip_segment (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
250 int clip_sector (double& x1, double& y1, double& x2, double& y2, const double u, const double v);
251 int clip_circle (double& x1, double& y1, double& x2, double& y2, const double cx, const double cy, const double radius, double t1, double t2);
252 int clip_triangle (double& x1, double& y1, double& x2, double& y2, const double u, const double v, const int clip_xaxis);
255 double norm_ang (double theta);
258 void indent_mtx2 (GRFMTX_2D m);
259 void xlat_mtx2 (GRFMTX_2D m, const double x, const double y);
260 void scale_mtx2 (GRFMTX_2D m, const double sx, const double sy);
261 void rot_mtx2 (GRFMTX_2D m, const double theta);
262 void mult_mtx2 (const GRFMTX_2D m1, const GRFMTX_2D m2, GRFMTX_2D result);
263 void xform_mtx2 (const GRFMTX_2D m, double& x, double& y);
264 void rotate2d (double x[], double y[], int pts, double angle);
265 void xlat2d (double x[], double y[], int pts, double xoffset, double yoffset);
266 void scale2d (double x[], double y[], int pts, double xfact, double yfact);
269 double integrateSimpson (const double xmin, const double xmax, const double *y, const int np);
272 // Console I/O Section
274 #define C_BLACK 0 /* color codes */
287 #define C_LTMAGENTA 13
291 /*----------------------------------------------------------------------*/
293 /* screen character codes */
302 void cio_tone(double freq, double length);
305 void cio_put_c(int c);
306 void cio_put_cc(int c, int count);
307 void cio_put_str(const char *str);
310 unsigned int cio_kb_getc(void);
311 void cio_kb_ungetc(unsigned int c);
312 char *cio_kb_gets(char *str, int maxlen);
313 unsigned int cio_kb_waitc(const char *astr, int beep);
319 #define KEY_RETURN 13
320 #define KEY_ESCAPE 27
332 #define BACKSLASH '\\'
351 #define AMPERSAND '&'
362 #endif /* #ifndef ASCII_H */