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