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