r98: finished object-orient convern of Scanner & Phantom
[ctsim.git] / include / kmath.h
index 3b4a36ad30435d0fbbef347050177971979da277..0ed4f2a3cb3de6319d8dbd75fbfa7acbb1b2c667 100644 (file)
@@ -1,30 +1,15 @@
 /*****************************************************************************
-**  This is part of the CTSim program
-**  Copyright (C) 1983-2000 Kevin Rosenberg
-**
-**  $Id: kmath.h,v 1.9 2000/05/08 20:00:48 kevin Exp $
-**  $Log: kmath.h,v $
-**  Revision 1.9  2000/05/08 20:00:48  kevin
-**  ANSI C changes
-**
-**  Revision 1.8  2000/05/07 12:46:19  kevin
-**  made c++ compatible
-**
-**  Revision 1.7  2000/05/04 18:16:34  kevin
-**  renamed filter definitions
-**
-**  Revision 1.6  2000/05/02 20:00:25  kevin
-**  *** empty log message ***
-**
-**  Revision 1.5  2000/05/02 15:31:39  kevin
-**  code cleaning
+** FILE IDENTIFICATION
 **
-**  Revision 1.4  2000/04/30 19:17:35  kevin
-**  Set up include files for conditional INTERACTIVE_GRAPHICS
+**    Name:          kmath.h
+**    Purpose:       Header file containing definitions for numerical app
+**    Programmer:    Kevin Rosenberg
+**    Date Started:  Nov 84
 **
-**  Revision 1.3  2000/04/28 14:14:16  kevin
-**  *** empty log message ***
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
 **
+**  $Id: kmath.h,v 1.15 2000/06/18 10:27:11 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
 **  along with this program; if not, write to the Free Software
 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ******************************************************************************/
-/******************************************************************************
- *
- * PURPOSE
- *     Header file containing definitions for numerical app's
- *     Date Started:   Nov 84
- *
- *****************************************************************************/
 
 #ifndef _H_kmath
 #define _H_kmath
 #include <stdio.h>
 #include <math.h>
 
-#ifdef __cplusplus
-extern "C" {
-#endif /* __cplusplus */
-
 #define PI      3.14159265358979323846
 #define HALFPI  1.57079632679489661923 /* PI divided by 2 */
 #define QUARTPI 0.78539816339744830962 /* PI divided by 4 */
@@ -69,156 +43,54 @@ extern "C" {
 #define F_EPSILON      1.0E-6
 #define D_EPSILON      1.0E-10
 
-#define DEG_TO_RAD(x)  (x*(PI/180.))
-#define RAD_TO_DEG(x)  (x*(180./PI))
-
-
 #define ASSUMEDZERO  1E-10
 
-/* codes for C data types */
+typedef double GRFMTX_2D[3][3];
+typedef double GRFMTX_3D[4][4];
+
+inline double 
+convertDegreesToRadians (double x)
+{ return (x * (PI/180.)); }
 
-#define DT_CHAR                1
-#define DT_INT         2
-#define DT_LONG                3
-#define DT_FLOAT       4
-#define DT_DOUBLE      5
-#define DT_STRING  6
+inline double
+convertRadiansToDegrees (double x)
+{ return (x*(180./PI)); }
 
-typedef char    *CMTX_1D;
-typedef CMTX_1D  *CMTX_2D;
-typedef CMTX_2D  *CMTX_3D;
+template<class T>
+inline T nearest (double x)
+{ return (x > 0 ? static_cast<T>(x+0.5) : static_cast<T>(x-0.5)); }
 
-typedef int     *IMTX_1D;
-typedef IMTX_1D  *IMTX_2D;
-typedef IMTX_2D  *IMTX_3D;
+template<class T>
+inline T clamp (T value, T lowerBounds, T upperBounds)
+{ return (value >= upperBounds ? upperBounds : (value <= lowerBounds ? lowerBounds : value )); }
 
-typedef float   *FMTX_1D;
-typedef FMTX_1D  *FMTX_2D;
-typedef FMTX_2D  *FMTX_3D;
+template<class T>
+inline T lineLength (T x1, T y1, T x2, T y2)
+{ return static_cast<T>( sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) ); }
 
-typedef double   *DMTX_1D;
-typedef DMTX_1D  *DMTX_2D;
-typedef DMTX_2D  *DMTX_3D;
+template<class T>
+inline void minmax_array (const T* array, const int n, T& min, T& max)
+{
+  max = min = array[0];
 
-typedef double GRFMTX_2D[3][3];
-typedef double GRFMTX_3D[4][4];
+  for (int i = 1; i < n; i++)
+    if (array[i] < min)
+      min = array[i];
+    else if (array[i] > max)
+      max = array[i];
+}
 
-union elem_val_un {    /* holds an element value */
-       char      c;
-       int       i;
-       long int  l;
-       float     f;
-       double    d;
-};
-
-typedef union elem_val_un  MTX_ELEM_VAL;
-
-
-union elem_ptr_un {    /* holds a pointer to a 1D vector of any type */
-       char      *c;
-       int       *i;
-       long int  *l;
-       float     *f;
-       double    *d;
-};
-
-typedef union elem_ptr_un  MTX_1D;
-typedef MTX_1D           *MTX_2D;
-typedef MTX_2D           *MTX_3D;
-
-union mtx_val_ptr_un {                 /* pointer to matrix values */
-    MTX_1D  m1;
-    MTX_2D  m2;
-    MTX_3D  m3;
-};
-
-typedef union mtx_val_ptr_un  MTX_PTR;
-
-struct matrix_st {
-    unsigned int order;                        /* order, or dimension, of matrix */
-    unsigned int elemtype;             /* element type */
-    unsigned int elemsize;             /* size of element in bytes */
-    unsigned int nx, ny, nz;           /* size of matrix in each dimension */
-    MTX_PTR val;                       /* pointer to matrix values */
-};
-
-typedef struct matrix_st MTX;
-typedef struct matrix_st *MTXP;
-
-/* DEFINITION IDENTIFICATION
- *
- *     Definitions to access a matrix element from an matrix
- *
-
-#define me1(mtx,x)\
-  (mtx->elemtype == DT_FLOAT ? mtx->val.m1.f[x] :\
-    (mtx->elemtype == DT_DOUBLE ? mtx->val.m1.d[x] :\
-      (mtx->elemtype == DT_INT ? mtx->val.m1.i[x] :\
-       (mtx->elemtype == DT_LONG ? mtx->val.m1.l[x] :\
-         (mtx->elemtype == DT_CHAR ? mtx->val.m1.c[x] :\
-            0\
-             )))))
-
-#define me2(mtx,x,y)\
-  (mtx->elemtype == DT_FLOAT ? mtx->val.m2[x].f[y] :\
-    (mtx->elemtype == DT_DOUBLE ? mtx->val.m2[x].d[y] :\
-      (mtx->elemtype == DT_INT ? mtx->val.m2[x].i[y] :\
-       (mtx->elemtype == DT_LONG ? mtx->val.m2[x].l[y] :\
-         (mtx->elemtype == DT_CHAR ? mtx->val.m2[x].c[y] :\
-           0\
-             )))))
-
-
-#define me3(mtx,x,y,z)\
-  (mtx->elemtype == DT_FLOAT ? mtx->val.m3[x][y].f[z] :\
-    (mtx->elemtype == DT_DOUBLE ? mtx->val.m3[x][y].d[z] :\
-      (mtx->elemtype == DT_INT ? mtx->val.m3[x][y].i[z] :\
-       (mtx->elemtype == DT_LONG ? mtx->val.m3[x][y].l[z] :\
-         (mtx->elemtype == DT_CHAR ? mtx->val.m3[x][y].c[z] :\
-           0\
-              )))))
-*/
-
-/* clip.c */
+/* clip.cpp */
 int clip_rect(double *x1, double *y1, double *x2, double *y2, const double rect[4]);
 int clip_segment(double *x1, double *y1, double *x2, double *y2, const double u, const double v);
 int clip_sector(double *x1, double *y1, double *x2, double *y2, const double u, const double v);
 int clip_circle(double *x1, double *y1, double *x2, double *y2, const double cx, const double cy, const double radius, double t1, double t2);
 int clip_triangle(double *x1, double *y1, double *x2, double *y2, const double u, const double v, const int clip_xaxis);
 
-/* lnearest.c */
-long int lnearest(double x);
-
-/* minmax.c */
-double fmax(const double a, const double b);
-void minmax_dvector(const double array[], const int pts, double *xmin, double *xmax);
-
-/* mtx_disp.c */
-void mtx_show(const MTX *mtx);
-void mtx_prt(const MTX *mtx, FILE *fp);
-int mtx_prt_elem(const MTX *mtx, FILE *fp, unsigned int x, unsigned int y, unsigned int z);
-
-/* mtx_elem.c */
-int mtx_get_elem(const MTX *mtx, MTX_ELEM_VAL *me, const int x, const int y, const int z);
-int mtx_put_elem(MTX *mtx, const MTX_ELEM_VAL *me, unsigned int x, unsigned int y, unsigned int z);
-
-/* mtx_inp.c */
-int mtx_inp_elem(const char *prompt, MTX_ELEM_VAL *mev, const int dtype);
-
-/* mtx_main.c */
-MTX *mtx_init(const unsigned int order, const unsigned int elem_type, const unsigned int nx, const unsigned int ny, const unsigned int nz);
-MTX *mtx_clr(MTX *mtx);
-int mtx_free(MTX *mtx);
-int mtx_elem_size(const int dt);
-int mtx_check(const MTX *mtx, const char *func_name);
-
-/* norm_ang.c */
+/* norm_ang.cpp */
 double norm_ang(double theta);
 
-/* simpson.c */
-double simpson(const double xmin, const double xmax, const double *y, const int np);
-
-/* xform.c */
+/* xform.cpp */
 void indent_mtx2(GRFMTX_2D m);
 void xlat_mtx2(GRFMTX_2D m, const double x, const double y);
 void scale_mtx2(GRFMTX_2D m, const double sx, const double sy);
@@ -229,8 +101,7 @@ void rotate2d(double x[], double y[], int pts, double angle);
 void xlat2d(double x[], double y[], int pts, double xoffset, double yoffset);
 void scale2d(double x[], double y[], int pts, double xfact, double yfact);
 
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
+/* simpson.cpp */
+double simpson(const double xmin, const double xmax, const double *y, const int np);
 
 #endif