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