5bac1bec41ded693c220315fe65c325c5808568b
[ctsim.git] / include / kmath.h
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: kmath.h,v 1.10 2000/06/07 00:59:38 kevin Exp $
6 **  $Log: kmath.h,v $
7 **  Revision 1.10  2000/06/07 00:59:38  kevin
8 **  added imagefiles
9 **
10 **  Revision 1.9  2000/05/08 20:00:48  kevin
11 **  ANSI C changes
12 **
13 **  Revision 1.8  2000/05/07 12:46:19  kevin
14 **  made c++ compatible
15 **
16 **  Revision 1.7  2000/05/04 18:16:34  kevin
17 **  renamed filter definitions
18 **
19 **  Revision 1.6  2000/05/02 20:00:25  kevin
20 **  *** empty log message ***
21 **
22 **  Revision 1.5  2000/05/02 15:31:39  kevin
23 **  code cleaning
24 **
25 **  Revision 1.4  2000/04/30 19:17:35  kevin
26 **  Set up include files for conditional INTERACTIVE_GRAPHICS
27 **
28 **  Revision 1.3  2000/04/28 14:14:16  kevin
29 **  *** empty log message ***
30 **
31 **
32 **  This program is free software; you can redistribute it and/or modify
33 **  it under the terms of the GNU General Public License (version 2) as
34 **  published by the Free Software Foundation.
35 **
36 **  This program is distributed in the hope that it will be useful,
37 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
38 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39 **  GNU General Public License for more details.
40 **
41 **  You should have received a copy of the GNU General Public License
42 **  along with this program; if not, write to the Free Software
43 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
44 ******************************************************************************/
45 /******************************************************************************
46  *
47  * PURPOSE
48  *      Header file containing definitions for numerical app's
49  *      Date Started:   Nov 84
50  *
51  *****************************************************************************/
52
53 #ifndef _H_kmath
54 #define _H_kmath
55
56 #include <stdio.h>
57 #include <math.h>
58
59 #ifdef __cplusplus
60 extern "C" {
61 #endif /* __cplusplus */
62
63 #define PI      3.14159265358979323846
64 #define HALFPI  1.57079632679489661923  /* PI divided by 2 */
65 #define QUARTPI 0.78539816339744830962  /* PI divided by 4 */
66 #define I_PI    0.31830988618379067154  /* Inverse of PI */
67 #define I_PID2  0.63661977236758134308  /* Inverse of PID2 */
68  
69 #define TWOPI   6.28318530717958647692
70 #define SQRT2   1.414213562373095049
71
72 #define F_EPSILON       1.0E-6
73 #define D_EPSILON       1.0E-10
74
75 #define DEG_TO_RAD(x)   (x*(PI/180.))
76 #define RAD_TO_DEG(x)   (x*(180./PI))
77
78
79 #define ASSUMEDZERO  1E-10
80
81 /* codes for C data types */
82
83 #define DT_CHAR         1
84 #define DT_INT          2
85 #define DT_LONG         3
86 #define DT_FLOAT        4
87 #define DT_DOUBLE       5
88 #define DT_UINT32       6
89 #define DT_STRING       7
90
91 typedef char     *CMTX_1D;
92 typedef CMTX_1D  *CMTX_2D;
93 typedef CMTX_2D  *CMTX_3D;
94
95 typedef int      *IMTX_1D;
96 typedef IMTX_1D  *IMTX_2D;
97 typedef IMTX_2D  *IMTX_3D;
98
99 typedef float    *FMTX_1D;
100 typedef FMTX_1D  *FMTX_2D;
101 typedef FMTX_2D  *FMTX_3D;
102
103 typedef double   *DMTX_1D;
104 typedef DMTX_1D  *DMTX_2D;
105 typedef DMTX_2D  *DMTX_3D;
106
107 typedef double GRFMTX_2D[3][3];
108 typedef double GRFMTX_3D[4][4];
109
110 union elem_val_un {     /* holds an element value */
111         char      c;
112         int       i;
113         long int  l;
114         float     f;
115         double    d;
116 };
117
118 typedef union elem_val_un  MTX_ELEM_VAL;
119
120
121 union elem_ptr_un {     /* holds a pointer to a 1D vector of any type */
122         char      *c;
123         int       *i;
124         long int  *l;
125         float     *f;
126         double    *d;
127 };
128
129 typedef union elem_ptr_un  MTX_1D;
130 typedef MTX_1D            *MTX_2D;
131 typedef MTX_2D            *MTX_3D;
132
133 union mtx_val_ptr_un {                  /* pointer to matrix values */
134     MTX_1D  m1;
135     MTX_2D  m2;
136     MTX_3D  m3;
137 };
138
139 typedef union mtx_val_ptr_un  MTX_PTR;
140
141 struct matrix_st {
142     unsigned int order;                 /* order, or dimension, of matrix */
143     unsigned int elemtype;              /* element type */
144     unsigned int elemsize;              /* size of element in bytes */
145     unsigned int nx, ny, nz;            /* size of matrix in each dimension */
146     MTX_PTR val;                        /* pointer to matrix values */
147 };
148
149 typedef struct matrix_st MTX;
150 typedef struct matrix_st *MTXP;
151
152 /* DEFINITION IDENTIFICATION
153  *
154  *      Definitions to access a matrix element from an matrix
155  *
156
157 #define me1(mtx,x)\
158   (mtx->elemtype == DT_FLOAT ? mtx->val.m1.f[x] :\
159     (mtx->elemtype == DT_DOUBLE ? mtx->val.m1.d[x] :\
160       (mtx->elemtype == DT_INT ? mtx->val.m1.i[x] :\
161         (mtx->elemtype == DT_LONG ? mtx->val.m1.l[x] :\
162           (mtx->elemtype == DT_CHAR ? mtx->val.m1.c[x] :\
163              0\
164               )))))
165
166 #define me2(mtx,x,y)\
167   (mtx->elemtype == DT_FLOAT ? mtx->val.m2[x].f[y] :\
168     (mtx->elemtype == DT_DOUBLE ? mtx->val.m2[x].d[y] :\
169       (mtx->elemtype == DT_INT ? mtx->val.m2[x].i[y] :\
170         (mtx->elemtype == DT_LONG ? mtx->val.m2[x].l[y] :\
171           (mtx->elemtype == DT_CHAR ? mtx->val.m2[x].c[y] :\
172             0\
173               )))))
174
175
176 #define me3(mtx,x,y,z)\
177   (mtx->elemtype == DT_FLOAT ? mtx->val.m3[x][y].f[z] :\
178     (mtx->elemtype == DT_DOUBLE ? mtx->val.m3[x][y].d[z] :\
179       (mtx->elemtype == DT_INT ? mtx->val.m3[x][y].i[z] :\
180         (mtx->elemtype == DT_LONG ? mtx->val.m3[x][y].l[z] :\
181           (mtx->elemtype == DT_CHAR ? mtx->val.m3[x][y].c[z] :\
182             0\
183                )))))
184 */
185
186 /* clip.c */
187 int clip_rect(double *x1, double *y1, double *x2, double *y2, const double rect[4]);
188 int clip_segment(double *x1, double *y1, double *x2, double *y2, const double u, const double v);
189 int clip_sector(double *x1, double *y1, double *x2, double *y2, const double u, const double v);
190 int clip_circle(double *x1, double *y1, double *x2, double *y2, const double cx, const double cy, const double radius, double t1, double t2);
191 int clip_triangle(double *x1, double *y1, double *x2, double *y2, const double u, const double v, const int clip_xaxis);
192
193 /* lnearest.c */
194 long int lnearest(double x);
195
196 /* minmax.c */
197 double fmax(const double a, const double b);
198 void minmax_dvector(const double array[], const int pts, double *xmin, double *xmax);
199
200 /* mtx_disp.c */
201 void mtx_show(const MTX *mtx);
202 void mtx_prt(const MTX *mtx, FILE *fp);
203 int mtx_prt_elem(const MTX *mtx, FILE *fp, unsigned int x, unsigned int y, unsigned int z);
204
205 /* mtx_elem.c */
206 int mtx_get_elem(const MTX *mtx, MTX_ELEM_VAL *me, const int x, const int y, const int z);
207 int mtx_put_elem(MTX *mtx, const MTX_ELEM_VAL *me, unsigned int x, unsigned int y, unsigned int z);
208
209 /* mtx_inp.c */
210 int mtx_inp_elem(const char *prompt, MTX_ELEM_VAL *mev, const int dtype);
211
212 /* mtx_main.c */
213 MTX *mtx_init(const unsigned int order, const unsigned int elem_type, const unsigned int nx, const unsigned int ny, const unsigned int nz);
214 MTX *mtx_clr(MTX *mtx);
215 int mtx_free(MTX *mtx);
216 int mtx_elem_size(const int dt);
217 int mtx_check(const MTX *mtx, const char *func_name);
218
219 /* norm_ang.c */
220 double norm_ang(double theta);
221
222 /* simpson.c */
223 double simpson(const double xmin, const double xmax, const double *y, const int np);
224
225 /* xform.c */
226 void indent_mtx2(GRFMTX_2D m);
227 void xlat_mtx2(GRFMTX_2D m, const double x, const double y);
228 void scale_mtx2(GRFMTX_2D m, const double sx, const double sy);
229 void rot_mtx2(GRFMTX_2D m, const double theta);
230 void mult_mtx2(GRFMTX_2D m1, GRFMTX_2D m2, GRFMTX_2D result);
231 void xform_mtx2(GRFMTX_2D m, double *x, double *y);
232 void rotate2d(double x[], double y[], int pts, double angle);
233 void xlat2d(double x[], double y[], int pts, double xoffset, double yoffset);
234 void scale2d(double x[], double y[], int pts, double xfact, double yfact);
235
236 #ifdef __cplusplus
237 }
238 #endif /* __cplusplus */
239
240 #endif