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