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