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