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