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