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