c7f63a04b87053081ddb20a2fc547506fb7f337c
[ctsim.git] / include / ir.h
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: ir.h,v 1.5 2000/05/02 20:00:25 kevin Exp $
6 **  $Log: ir.h,v $
7 **  Revision 1.5  2000/05/02 20:00:25  kevin
8 **  *** empty log message ***
9 **
10 **  Revision 1.4  2000/05/02 15:31:39  kevin
11 **  code cleaning
12 **
13 **  Revision 1.3  2000/04/29 23:24:29  kevin
14 **  *** empty log message ***
15 **
16 **  Revision 1.2  2000/04/28 14:14:16  kevin
17 **  *** empty log message ***
18 **
19 **  Revision 1.1.1.1  2000/04/28 13:02:43  kevin
20 **  Initial CVS import for first public release
21 **
22 **
23 **
24 **  This program is free software; you can redistribute it and/or modify
25 **  it under the terms of the GNU General Public License (version 2) as
26 **  published by the Free Software Foundation.
27 **
28 **  This program is distributed in the hope that it will be useful,
29 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
30 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31 **  GNU General Public License for more details.
32 **
33 **  You should have received a copy of the GNU General Public License
34 **  along with this program; if not, write to the Free Software
35 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
36 ******************************************************************************/
37 /* FILE IDENTIFICATION
38  *
39  *      Name:         ir.h          Header File for Image Reconstruction System
40  *      Programmer:   Kevin Rosenberg
41  *      Date Started: 7-1-84
42  *      Last Change:  1-20-85
43  */
44
45 #ifndef IR_H
46 #define IR_H
47
48 #ifdef MPI_CT
49 #define MPI_MAX_PROCESS 128
50 struct mpi_ct_st
51 {
52   int my_rank;
53   int nproc;
54   int base_local_work_units;
55   int remainder_work_units;
56   int local_work_units[MPI_MAX_PROCESS];
57   int start_work_unit[MPI_MAX_PROCESS];
58   MPI_Comm comm;
59 };
60
61 extern struct mpi_ct_st mpi_ct;
62 void mpi_ct_calc_work_units(const unsigned int global_work_units);
63 #endif
64
65
66 struct histo_st {
67     int *b;                     /* Histogram array (# of elements in each bin) */
68     int nbin;                   /* Number of histogram bins */
69     double xmin, xmax, xinc;    /* Limits of histogram boundaries */
70 };
71
72 typedef struct histo_st HISTOGRAM;
73
74 /*---------------------------------------------------------------------------*/
75
76 #define POINTS_PER_CIRCLE  36
77
78 #define MAXREMARK       99
79 #define LENREMARK       (MAXREMARK+1)
80 #define IMAGE_VAL       FMTX_2D         /* use 2d floating point matrix */
81 #define IMAGE_ELEM_TYPE DT_FLOAT        /* use floats for images */
82 typedef float IMAGE_ELEM_VAL;           /* use floats for image storing */
83
84 struct image_st {
85     IMAGE_VAL v;                        /* values of voxels in matrix form */
86     SDF_2D *dfp_2d;                     /* Pointer to disk image file */
87     int nx, ny;                         /* size of voxel matrix */
88     double xmin, xmax, ymin, ymax;      /* extent of voxel matrix in pic coord */
89     char remark[LENREMARK];             /* description of voxel data */
90     float calctime;                     /* time to calculate voxels in seconds */
91 };
92
93 struct object_st {
94     int type;                      /* object type (box, ellipse, etc) */
95     double atten;                  /* X-ray attenuation coefficient */
96     double cx,cy;                  /* center of object */
97     double u,v;                    /* size of object */
98     double rot;                    /* object rotation angle (in radians) */
99     double *x, *y;                 /* ptr to array of points in obj world coord */
100     int pts;                       /* number of points in outline arrays */
101     double xmin, xmax, ymin, ymax; /* object limits */
102     double radius;                 /*   "   */
103     struct {                       /* transform matrices        */
104         GRFMTX_2D p_to_o;          /* map from pic to standard obj coords */
105         GRFMTX_2D o_to_p;          /* map from std object coords to pic coords */
106     } xform;
107     struct object_st *next;       /* pointer to next object in picture */
108 };
109
110 typedef struct object_st OBJECT;
111
112 struct pic_st {                    /* Picture structure */
113     OBJECT *objlist;               /* object list */
114     int type;
115     int numobj;                    /* number of objects in picture */
116     double xmin, xmax, ymin, ymax; /* extent of objects in object coordinates */
117     double radius;                 /*     " "    */
118 };
119
120 typedef struct image_st  IMAGE;
121 typedef struct pic_st    PICTURE;
122
123 #define P_OBJECTS       0       /* Picture made of objects */
124 #define P_UNIT_PULSE    1       /* Special picture, not made of objects */
125 #define P_FILTER        9       /* defined only by a type */
126
127 /*----------------------------------------------------------------------*/
128 /*                              RAYSUM SYMBOLS                          */
129 /*----------------------------------------------------------------------*/
130
131 /* Ray sums are collected along an array of ndet detectors.  The data
132  * for these detectors is stored in the structure DETECTARRAY
133  */
134
135 #define DETECT_TYPE float
136
137 struct detarray_st {
138   DETECT_TYPE *detval;  /* Pointer to array of values recorded by detector */
139   int ndet;                     /* Number of detectors in array */
140   double view_angle;    /* View angle in radians */
141 };
142
143 struct detect_st {
144   int ndet;                     /* Number of detectors in array */
145   int nview;                    /* Number of rotated views */
146   int nsample;                  /* Number of rays per detector */
147   double detlen;                /* Total length of detector array */
148   double rotlen;                /* Rotation angle length in radians (norm 2PI) */
149   double det_inc;               /* Increment between centers of detectors */
150   double rot_inc;               /* Increment in rotation angle between views */
151   double radius;                /* Radius of rotation.  Distance from */
152                                 /*   center of pic to center of det */
153   double piclen;                /* Maximum Length of picture or area of interest */
154   struct {
155     double xd1,yd1,xd2,yd2;     /* Coordinates of detector endpoints */
156     double xs1,ys1,xs2,ys2;     /* Coordinates of source endpoints */
157     double angle;               /* Starting angle */
158   } init;
159 };
160
161 struct raysum_st {
162   int fd;
163   int file_mode;
164   int header_size;
165   struct detarray_st **view;    /* Pointer to array of detarray_st pointers */
166
167   char remark[LENREMARK];       /* description of raysum data */
168   double calctime;              /* time required to calculate raysums */
169
170   int ndet;                     /* number of detectors in array */
171   int nview;                    /* number of rotated views */
172   double rot_start;             /* starting view rotation */
173   double rot_inc;               /* angle between rotations */
174   double det_start;             /* distance of beginning detector to center */
175                                 /*    of picture */
176   double det_inc;               /* increment between detectors */
177   double piclen;                /* Length of picture edge (pic is square) */
178 };
179
180 typedef struct detarray_st DETARRAY;
181 typedef struct detect_st   DETECTOR;
182 typedef struct raysum_st   RAYSUM;
183
184 /*----------------------------------------------------------------------*/
185 /*                              USER SYMBOLS                            */
186 /*----------------------------------------------------------------------*/
187
188 /* codes for object types, passed to add_obj() */
189
190 #define O_RECTANGLE  1
191 #define O_TRIANGLE   2
192 #define O_ELLIPSE    3
193 #define O_SECTOR     4
194 #define O_SEGMENT    5
195
196 /* Codes for Coordinate Types      */
197 /* Defines coords for inside_obj() */
198
199 #define OBJ_COORD -1            /* Normalized Object Coordinates */
200 #define PIC_COORD -2            /* User's Picture Coordinates */
201
202 /* Codes for Filter types */
203
204 #define W_BANDLIMIT     1       /* filter types for genfilter() */
205 #define W_SINC          2
206 #define W_G_HAMMING     3
207 #define W_COSINE        4
208 #define W_TRIANGLE      5
209
210 #define W_A_BANDLIMIT   11      /* filters times abs() of function */
211 #define W_A_SINC        12
212 #define W_AG_HAMMING    13
213 #define W_A_COSINE      14
214
215 #define W_SHEPP         21
216
217 /* function domains */
218
219 #define O_FREQ_STR      "freq"
220 #define O_SPATIAL_STR   "spatial"
221  
222 #define D_FREQ          1       /* Domain names */
223 #define D_SPATIAL       2
224
225 /* function symmetry */
226
227 #define FUNC_EVEN       1       /* function types, f[-n] = f[n] */
228 #define FUNC_ODD        2       /* f[-n] = -f[n] */
229 #define FUNC_BOTH       3       /* function has both odd & even components */
230
231 /* interpolation methods */
232
233 #define I_NEAREST       1       /* Interpolation methods */
234 #define I_LINEAR        2       /* Linear interpolation */
235 #define I_BSPLINE       3
236 #define I_1BSPLINE      3       /* 1st order B-Spline */
237 #define I_2BSPLINE      4
238 #define I_3BSPLINE      5
239
240 /* Constants for sizing picture */
241
242 #define PERCENT_PIC_SIZE_INCR   0.0     /* Fractional increase in picture */
243                                         /* limits compared to object size */
244 #define N_EXTRA_DETECTORS         4     /* Number of extra detectors */
245                                         /* widths when calculating detlen */
246
247 #define DET_PARALLEL 1
248 #define DET_FAN      2
249
250 #define O_TRACE_NONE_STR     "none"
251 #define O_TRACE_TEXT_STR     "text"
252 #define O_TRACE_PIC_STR      "pic"
253 #define O_TRACE_RAYS_STR     "rays"
254 #define O_TRACE_PLOT_STR     "plot"
255 #define O_TRACE_CLIPPING_STR "clipping"
256
257
258 #define TRACE_NONE     0                /* No tracing */
259 #define TRACE_TEXT     1                /* Minimal status */
260 #define TRACE_PIC      2                /* Show picture */
261 #define TRACE_RAYS     3                /* Show all rays */
262 #define TRACE_PLOT     4                /* Plot raysums */
263 #define TRACE_CLIPPING 5                /* Plot clipping */
264
265 #define O_PIC_HERMAN    1               /* Herman head phantom */
266 #define O_PIC_ROWLAND   2               /* Rowland head phantom */
267 #define O_PIC_BROWLAND  3               /* Bordered Rowland head phantom */
268 #define O_PIC_UNITPULSE 4               /* Unit pulse phantom */
269
270 #define O_PIC_HERMAN_STR    "herman"
271 #define O_PIC_ROWLAND_STR   "rowland"
272 #define O_PIC_BROWLAND_STR  "browland"
273 #define O_PIC_UNITPULSE_STR "unitpulse"
274
275 #define O_INTERP_NEAREST_STR  "nearest"
276 #define O_INTERP_LINEAR_STR   "linear"
277 #define O_INTERP_BSPLINE_STR  "bspline"
278
279 #define O_FILTER_ABS_BANDLIMIT_STR "abs_bandlimit"
280 #define O_FILTER_ABS_SINC_STR      "abs_sinc"
281 #define O_FILTER_ABS_COS_STR       "abs_cos"
282 #define O_FILTER_ABS_HAMMING_STR   "abs_hamming"
283 #define O_FILTER_SHEPP_STR         "shepp"
284 #define O_FILTER_BANDLIMIT_STR     "bandlimit"
285 #define O_FILTER_SINC_STR          "sinc"
286 #define O_FILTER_COS_STR           "cos"
287 #define O_FILTER_HAMMING_STR       "hamming"
288 #define O_FILTER_TRIANGLE_STR      "triangle"
289
290 #define O_BPROJ_TRIG   1 
291 #define O_BPROJ_TABLE  2
292 #define O_BPROJ_DIFF   3
293 #define O_BPROJ_DIFF2  4
294 #define O_BPROJ_IDIFF2 5
295
296 #define O_BPROJ_TRIG_STR     "trig"
297 #define O_BPROJ_TABLE_STR    "table"
298 #define O_BPROJ_DIFF_STR     "diff"
299 #define O_BPROJ_DIFF2_STR    "diff2"
300 #define O_BPROJ_IDIFF2_STR   "idiff2"
301
302 #define RS_TRACE_ROW_TITLE      1
303 #define RS_TRACE_ROW_TITLE2     2
304 #define RS_TRACE_ROW_PHANT_ID   4
305 #define RS_TRACE_ROW_CHROMATIC  7
306 #define RS_TRACE_ROW_SCATTER    8
307 #define RS_TRACE_ROW_PHOT_STAT  9
308 #define RS_TRACE_ROW_NDET       12
309 #define RS_TRACE_ROW_NVIEW      13
310 #define RS_TRACE_ROW_SAMPLES    14
311 #define RS_TRACE_ROW_CURR_VIEW  17
312 #define RS_TRACE_ROW_ATTEN      18
313
314
315 /*----------------------------------------------------------------------*/
316 /*                      GRAY SCALE STRUCTURES                           */
317 /*----------------------------------------------------------------------*/
318
319 #define GS_MAX_CELL_SIZE   4
320
321 typedef int GS_BITMASK[4][4];
322
323 struct greyscale_st {
324     int dev;                            /* Device to output to */
325     int (*dotfunc)(int x, int y, int color);                    /* Pointer to dot function for device */
326     int cur_x, cur_y;                   /* Current cell location */
327     int nxcell, nycell;                 /* size of cell in pixels */
328     int xmin, ymin;                     /* starting position of grey scale */
329     int num_color;                      /* Number of primary colors available */
330     int num_intens;                     /* Number of intensities available */
331     int max_level;                      /* gs levels range from 0 to max_level */
332     char *fg_color_tbl;                 /* Hold foreground color for each level */
333     char *bg_color_tbl;                 /* Holds background color */
334     char *level_sub_tbl;                /* Holds value to subtract for level */
335                                         /* before accessing bit mask */
336     GS_BITMASK *bm;                     /* Holds grey-scale bit mask */
337     struct greyscale_st *next_dev;      /* Pointer to next open device */
338                                         /* == NULL when no more devices */
339 };
340
341 typedef struct greyscale_st GREYSCALE;
342
343
344 /* From reconstr.c */
345 IMAGE *image_reconst (IMAGE *im, RAYSUM *rs, int filt_type, double filt_param, int interp_type, int interp_param, const int backproj_type, int ir_trace);
346
347 /* From bproj.c */
348 void backproj_init (const RAYSUM *rs, IMAGE *im, const int bproj_method);
349 int  backproj_calc (const RAYSUM *rs, IMAGE *im, const double *t, const double view_angle, 
350                     const int interp_type, const int bproj_method);
351 void backproj_term (const RAYSUM *rs, IMAGE *im, const int bproj_method);
352
353 void backproj_init_trig (const RAYSUM *rs, IMAGE *im);
354 int  backproj_calc_trig (const RAYSUM *rs, IMAGE *im, const double *t, 
355                          const double view_angle, const int interp_type);
356 void backproj_term_trig (const RAYSUM *rs, IMAGE *im);
357 void backproj_init_table (const RAYSUM *rs, IMAGE *im);
358 int  backproj_calc_table (const RAYSUM *rs, IMAGE *im, const double *t, 
359                           const double view_angle, const int interp_type);
360 void backproj_term_table (const RAYSUM *rs, IMAGE *im);
361 void backproj_init_d (const RAYSUM *rs, IMAGE *im);
362 int  backproj_calc_d (const RAYSUM *rs, IMAGE *im, const double *t, 
363                       const double view_angle, const int interp_type);
364 void backproj_term_d (const RAYSUM *rs, IMAGE *im);
365 void backproj_init_d2 (const RAYSUM *rs, IMAGE *im);
366 int  backproj_calc_d2 (const RAYSUM *rs, IMAGE *im, const double *t, 
367                        const double view_angle, const int interp_type);
368 void backproj_term_d2 (const RAYSUM *rs, IMAGE *im);
369 void backproj_init_id (const RAYSUM *rs, IMAGE *im);
370 int  backproj_calc_id (const RAYSUM *rs, IMAGE *im, const double *t, 
371                        const double view_angle, const int interp_type);
372 void backproj_term_id (const RAYSUM *rs, IMAGE *im);
373 void backproj_init_id2 (const RAYSUM *rs, IMAGE *im);
374 int  backproj_calc_id2 (const RAYSUM *rs, IMAGE *im, const double *t, 
375                         const double view_angle, const int interp_type);
376 void backproj_term_id2 (const RAYSUM *rs, IMAGE *im);
377
378 void usage (const char *program);
379 int main(const int argc, char * const argv[]);
380
381
382
383 /* bspline.c */
384 int bspline(int samples, int zoom_factor, int spline_order, double input[], double output[]);
385 /* convolve.c */
386 double convolve(const double f1[], const double f2[], const double dx, const int n, const int np, const int func_type);
387 double convolve_both(const double f1[], const double f2[], const double dx, const int n, const int np);
388 void rotate2d(double x[], double y[], int pts, double angle);
389 void xlat2d(double x[], double y[], int pts, double xoffset, double yoffset);
390 void scale2d(double x[], double y[], int pts, double xfact, double yfact);
391 int circle_pts(double theta);
392 /* filt.c */
393 double d_filtfunc(int filt_type, double x, double bw, double param, int n);
394 double filter_freq(int filt_type, double u, double bw, double param);
395 double a_filtfunc(int filt_type, double x, double bw, double param);
396 double sinc(double x, double mult);
397 double abscos_int(double u, double w);
398 double *filter_generate(int filt_type, double bw, double xmin, double xmax, int n, double param, int domain, int numint);
399 void image_filter_init(IMAGE *im, int domain, double bw, int filt_type, double filt_param, int opt_trace);
400 /* im_draw.c */
401 void image_show(IMAGE *im, int nxcell, int nycell, double densmin, double densmax, bool disp_scale);
402 int image_paint(int dev, IMAGE *im, int xmin, int ymin, int nxcell, int nycell, double densmin, double densmax, bool disp_scale);
403 int gs_image(GREYSCALE *gs, IMAGE *im, double densmin, double densmax, bool disp_scale);
404 void paint_cscale(GREYSCALE *gs, int nx, int ny);
405 GREYSCALE *gs_init(int dev, int xmin, int ymin, int nxcell, int nycell);
406 void gs_pixel(GREYSCALE *gs, int x, int y, int gs_level);
407 void gs_set_pos(GREYSCALE *gs, int x, int y);
408 void gs_set_at_addr(GREYSCALE *gs, int gs_level);
409 void gs_set_color_scale(void);
410 /* image.c */
411 IMAGE *image_create(const char *fname, const int nx, const int ny);
412 int image_clear(IMAGE *im);
413 int image_save(IMAGE *im);
414 IMAGE *image_load(const char *fname);
415 /* ir_vars.c */
416 /* options.c */
417 int opt_set_trace(const char *optarg, const char *program);
418 const char *name_of_picture(const int picnum);
419 int opt_set_picture(const char *optarg, const char *program);
420 int opt_set_interpolation(const char *optarg, const char *program);
421 const char *name_of_interpolation(int interp_type);
422 int opt_set_filter(const char *optarg, const char *program);
423 const char *name_of_filter(const int filter);
424 int opt_set_filter_domain(const char *optarg, const char *program);
425 const char *name_of_filter_domain(const int domain);
426 int opt_set_backproj(const char *optarg, const char *program);
427 const char *name_of_backproj(const int backproj);
428 /* phm.c */
429 PICTURE *select_pic(void);
430 PICTURE *create_pic_from_file(const char *fname);
431 PICTURE *create_pic(const int picnum);
432 PICTURE *init_pic(void);
433 int add_objs_kb(PICTURE *pic);
434 int add_objs_file(PICTURE *pic, const char *fname);
435 void addobject(PICTURE *pic, const int type, const double cx, const double cy, 
436                const double u, const double v, const double rot, const double atten);
437 int makeobjpts(OBJECT *obj);
438 void makeobjxform(OBJECT *obj);
439 void calc_arc(double x[], double y[], const int pts, const double xcent, const double ycent, 
440               const double r, const double start, const double stop);
441 void calc_ellipse(double x[], double y[], const int pts, const double u, const double v);
442 OBJECT *alloc_obj(void);
443 void prt_pic(PICTURE *pic);
444 void show_pic(const PICTURE *pic);
445 void draw_pic(const PICTURE *pic);
446 /* phm2image.c */
447 void pic_to_image(const PICTURE *pic, IMAGE *im, const int col_start, const int col_count,
448                   const int nsample, const int trace);
449 int inside_obj(const OBJECT *obj, double x, double y, const int coord_type);
450 /* ray.c */
451 DETECTOR *detect_create(const PICTURE *pic, int ndet, int nview, int nsample, const double rot_anglen);
452 void detect_free(DETECTOR *det);
453 double calc_rsum(const PICTURE *pic, const double x1, const double y1, const double x2, const double y2);
454 double calc_objsum(const OBJECT *obj, const double x1, const double y1, const double x2, const double y2);
455 int clipobj(const OBJECT *obj, double *x1, double *y1, double *x2, double *y2);
456 /* raycollect.c */
457 int raysum_collect(RAYSUM *rs, const DETECTOR *det, const PICTURE *pic, const int start_view, 
458                    const int trace, const int unit_pulse);
459 void rayview(const PICTURE *pic, DETARRAY *darray, const DETECTOR *det, 
460              const double xd1, const double yd1, const double xd2, const double yd2, 
461              const double xs1, const double ys1, const double xs2, const double ys2, const int unit_pulse);
462 void rs_trace_showprm (const char *label, const char *fmt, int row, int color, ...);
463 /* rayio.c */
464 RAYSUM *raysum_create(const char *fname, const int nview, const int ndet);
465 RAYSUM *raysum_create_from_det(const char *fname, const DETECTOR *det);
466 RAYSUM *raysum_open(const char *filename);
467 void raysum_alloc_views(RAYSUM *rs);
468 void raysum_free(RAYSUM *rs);
469 int raysum_is_open(RAYSUM *rs);
470 int raysum_close(RAYSUM *rs);
471 int raysum_read_header(RAYSUM *rs);
472 int raysum_write_header(RAYSUM *rs);
473 int raysum_read(RAYSUM *rs);
474 int raysum_write(RAYSUM *rs);
475 DETARRAY *detarray_alloc(const int n);
476 void detarray_free(DETARRAY *darray);
477 int detarray_read(RAYSUM *rs, DETARRAY *darray, const int view_num);
478 int detarray_write(RAYSUM *rs, const DETARRAY *darray, const int view_num);
479 int raysum_print(const RAYSUM *rs);
480 /* phmstd.c */
481 void herm_head(PICTURE *pic);
482 void row_head(PICTURE *pic);
483 void row_bord_head(PICTURE *pic);
484
485 #endif