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