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