r13: *** 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.3 2000/04/29 23:24:29 kevin Exp $
6 **  $Log: ir.h,v $
7 **  Revision 1.3  2000/04/29 23:24:29  kevin
8 **  *** empty log message ***
9 **
10 **  Revision 1.2  2000/04/28 14:14:16  kevin
11 **  *** empty log message ***
12 **
13 **  Revision 1.1.1.1  2000/04/28 13:02:43  kevin
14 **  Initial CVS import for first public release
15 **
16 **
17 **
18 **  This program is free software; you can redistribute it and/or modify
19 **  it under the terms of the GNU General Public License (version 2) as
20 **  published by the Free Software Foundation.
21 **
22 **  This program is distributed in the hope that it will be useful,
23 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
24 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 **  GNU General Public License for more details.
26 **
27 **  You should have received a copy of the GNU General Public License
28 **  along with this program; if not, write to the Free Software
29 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30 ******************************************************************************/
31 /* FILE IDENTIFICATION
32  *
33  *      Name:         ir.h          Header File for Image Reconstruction System
34  *      Programmer:   Kevin Rosenberg
35  *      Date Started: 7-1-84
36  *      Last Change:  1-20-85
37  */
38
39 #ifndef IR_H
40 #define IR_H
41
42 #ifdef MPI_CT
43 #define MPI_MAX_PROCESS 128
44 struct mpi_ct_st
45 {
46   int my_rank;
47   int nproc;
48   int base_local_work_units;
49   int remainder_work_units;
50   int local_work_units[MPI_MAX_PROCESS];
51   int start_work_unit[MPI_MAX_PROCESS];
52   MPI_Comm comm;
53 };
54
55 extern struct mpi_ct_st mpi_ct;
56 void mpi_ct_calc_work_units(const unsigned int global_work_units);
57 #endif
58
59
60 struct histo_st {
61     int *b;                     /* Histogram array (# of elements in each bin) */
62     int nbin;                   /* Number of histogram bins */
63     double xmin, xmax, xinc;    /* Limits of histogram boundaries */
64 };
65
66 typedef struct histo_st HISTOGRAM;
67
68 /*---------------------------------------------------------------------------*/
69
70 #define POINTS_PER_CIRCLE  36
71
72 #define MAXREMARK       99
73 #define LENREMARK       (MAXREMARK+1)
74 #define IMAGE_VAL       FMTX_2D         /* use 2d floating point matrix */
75 #define IMAGE_ELEM_TYPE DT_FLOAT        /* use floats for images */
76 typedef float IMAGE_ELEM_VAL;           /* use floats for image storing */
77
78 struct image_st {
79     IMAGE_VAL v;                        /* values of voxels in matrix form */
80     SDF_2D *dfp_2d;                     /* Pointer to disk image file */
81     int nx, ny;                         /* size of voxel matrix */
82     double xmin, xmax, ymin, ymax;      /* extent of voxel matrix in pic coord */
83     char remark[LENREMARK];             /* description of voxel data */
84     float calctime;                     /* time to calculate voxels in seconds */
85 };
86
87 struct object_st {
88     int type;                      /* object type (box, ellipse, etc) */
89     double atten;                  /* X-ray attenuation coefficient */
90     double cx,cy;                  /* center of object */
91     double u,v;                    /* size of object */
92     double rot;                    /* object rotation angle (in radians) */
93     double *x, *y;                 /* ptr to array of points in obj world coord */
94     int pts;                       /* number of points in outline arrays */
95     double xmin, xmax, ymin, ymax; /* object limits */
96     double radius;                 /*   "   */
97     struct {                       /* transform matrices        */
98         GRFMTX_2D p_to_o;          /* map from pic to standard obj coords */
99         GRFMTX_2D o_to_p;          /* map from std object coords to pic coords */
100     } xform;
101     struct object_st *next;       /* pointer to next object in picture */
102 };
103
104 typedef struct object_st OBJECT;
105
106 struct pic_st {                    /* Picture structure */
107     OBJECT *objlist;               /* object list */
108     int type;
109     int numobj;                    /* number of objects in picture */
110     double xmin, xmax, ymin, ymax; /* extent of objects in object coordinates */
111     double radius;                 /*     " "    */
112 };
113
114 typedef struct image_st  IMAGE;
115 typedef struct pic_st    PICTURE;
116
117 #define P_OBJECTS       0       /* Picture made of objects */
118 #define P_UNIT_PULSE    1       /* Special picture, not made of objects */
119 #define P_FILTER        9       /* defined only by a type */
120
121 /*----------------------------------------------------------------------*/
122 /*                              RAYSUM SYMBOLS                          */
123 /*----------------------------------------------------------------------*/
124
125 /* Ray sums are collected along an array of ndet detectors.  The data
126  * for these detectors is stored in the structure DETECTARRAY
127  */
128
129 #define DETECT_TYPE float
130
131 struct detarray_st {
132   DETECT_TYPE *detval;  /* Pointer to array of values recorded by detector */
133   int ndet;                     /* Number of detectors in array */
134   double view_angle;    /* View angle in radians */
135 };
136
137 struct detect_st {
138   int ndet;                     /* Number of detectors in array */
139   int nview;                    /* Number of rotated views */
140   int nsample;                  /* Number of rays per detector */
141   double detlen;                /* Total length of detector array */
142   double rotlen;                /* Rotation angle length in radians (norm 2PI) */
143   double det_inc;               /* Increment between centers of detectors */
144   double rot_inc;               /* Increment in rotation angle between views */
145   double radius;                /* Radius of rotation.  Distance from */
146                                 /*   center of pic to center of det */
147   double piclen;                /* Maximum Length of picture or area of interest */
148   struct {
149     double xd1,yd1,xd2,yd2;     /* Coordinates of detector endpoints */
150     double xs1,ys1,xs2,ys2;     /* Coordinates of source endpoints */
151     double angle;               /* Starting angle */
152   } init;
153 };
154
155 struct raysum_st {
156   int fd;
157   int file_mode;
158   int header_size;
159   struct detarray_st **view;    /* Pointer to array of detarray_st pointers */
160
161   char remark[LENREMARK];       /* description of raysum data */
162   double calctime;              /* time required to calculate raysums */
163
164   int ndet;                     /* number of detectors in array */
165   int nview;                    /* number of rotated views */
166   double rot_start;             /* starting view rotation */
167   double rot_inc;               /* angle between rotations */
168   double det_start;             /* distance of beginning detector to center */
169                                 /*    of picture */
170   double det_inc;               /* increment between detectors */
171   double piclen;                /* Length of picture edge (pic is square) */
172 };
173
174 typedef struct detarray_st DETARRAY;
175 typedef struct detect_st   DETECTOR;
176 typedef struct raysum_st   RAYSUM;
177
178 /*----------------------------------------------------------------------*/
179 /*                              USER SYMBOLS                            */
180 /*----------------------------------------------------------------------*/
181
182 /* codes for object types, passed to add_obj() */
183
184 #define O_RECTANGLE  1
185 #define O_TRIANGLE   2
186 #define O_ELLIPSE    3
187 #define O_SECTOR     4
188 #define O_SEGMENT    5
189
190 /* Codes for Coordinate Types      */
191 /* Defines coords for inside_obj() */
192
193 #define OBJ_COORD -1            /* Normalized Object Coordinates */
194 #define PIC_COORD -2            /* User's Picture Coordinates */
195
196 /* Codes for Filter types */
197
198 #define W_BANDLIMIT     1       /* filter types for genfilter() */
199 #define W_SINC          2
200 #define W_G_HAMMING     3
201 #define W_COSINE        4
202 #define W_TRIANGLE      5
203
204 #define W_A_BANDLIMIT   11      /* filters times abs() of function */
205 #define W_A_SINC        12
206 #define W_AG_HAMMING    13
207 #define W_A_COSINE      14
208
209 #define W_SHEPP         21
210
211 /* function domains */
212
213 #define O_FREQ_STR      "freq"
214 #define O_SPATIAL_STR   "spatial"
215  
216 #define D_FREQ          1       /* Domain names */
217 #define D_SPATIAL       2
218
219 /* function symmetry */
220
221 #define FUNC_EVEN       1       /* function types, f[-n] = f[n] */
222 #define FUNC_ODD        2       /* f[-n] = -f[n] */
223 #define FUNC_BOTH       3       /* function has both odd & even components */
224
225 /* interpolation methods */
226
227 #define I_NEAREST       1       /* Interpolation methods */
228 #define I_LINEAR        2       /* Linear interpolation */
229 #define I_BSPLINE       3
230 #define I_1BSPLINE      3       /* 1st order B-Spline */
231 #define I_2BSPLINE      4
232 #define I_3BSPLINE      5
233
234 /* Constants for sizing picture */
235
236 #define PERCENT_PIC_SIZE_INCR   0.0     /* Fractional increase in picture */
237                                         /* limits compared to object size */
238 #define N_EXTRA_DETECTORS         4     /* Number of extra detectors */
239                                         /* widths when calculating detlen */
240
241 #define DET_PARALLEL 1
242 #define DET_FAN      2
243
244 #define O_TRACE_NONE_STR     "none"
245 #define O_TRACE_TEXT_STR     "text"
246 #define O_TRACE_PIC_STR      "pic"
247 #define O_TRACE_RAYS_STR     "rays"
248 #define O_TRACE_PLOT_STR     "plot"
249 #define O_TRACE_CLIPPING_STR "clipping"
250
251
252 #define TRACE_NONE     0                /* No tracing */
253 #define TRACE_TEXT     1                /* Minimal status */
254 #define TRACE_PIC      2                /* Show picture */
255 #define TRACE_RAYS     3                /* Show all rays */
256 #define TRACE_PLOT     4                /* Plot raysums */
257 #define TRACE_CLIPPING 5                /* Plot clipping */
258
259 #define O_PIC_HERMAN    1               /* Herman head phantom */
260 #define O_PIC_ROWLAND   2               /* Rowland head phantom */
261 #define O_PIC_BROWLAND  3               /* Bordered Rowland head phantom */
262 #define O_PIC_UNITPULSE 4               /* Unit pulse phantom */
263
264 #define O_PIC_HERMAN_STR    "herman"
265 #define O_PIC_ROWLAND_STR   "rowland"
266 #define O_PIC_BROWLAND_STR  "browland"
267 #define O_PIC_UNITPULSE_STR "unitpulse"
268
269 #define O_INTERP_NEAREST_STR  "nearest"
270 #define O_INTERP_LINEAR_STR   "linear"
271 #define O_INTERP_BSPLINE_STR  "bspline"
272
273 #define O_FILTER_ABS_BANDLIMIT_STR "abs_bandlimit"
274 #define O_FILTER_ABS_SINC_STR      "abs_sinc"
275 #define O_FILTER_ABS_COS_STR       "abs_cos"
276 #define O_FILTER_ABS_HAMMING_STR   "abs_hamming"
277 #define O_FILTER_SHEPP_STR         "shepp"
278 #define O_FILTER_BANDLIMIT_STR     "bandlimit"
279 #define O_FILTER_SINC_STR          "sinc"
280 #define O_FILTER_COS_STR           "cos"
281 #define O_FILTER_HAMMING_STR       "hamming"
282 #define O_FILTER_TRIANGLE_STR      "triangle"
283
284 #define O_BPROJ_TRIG   1 
285 #define O_BPROJ_TABLE  2
286 #define O_BPROJ_DIFF   3
287 #define O_BPROJ_DIFF2  4
288 #define O_BPROJ_IDIFF2 5
289
290 #define O_BPROJ_TRIG_STR     "trig"
291 #define O_BPROJ_TABLE_STR    "table"
292 #define O_BPROJ_DIFF_STR     "diff"
293 #define O_BPROJ_DIFF2_STR    "diff2"
294 #define O_BPROJ_IDIFF2_STR   "idiff2"
295
296 #define RS_TRACE_ROW_TITLE      1
297 #define RS_TRACE_ROW_TITLE2     2
298 #define RS_TRACE_ROW_PHANT_ID   4
299 #define RS_TRACE_ROW_CHROMATIC  7
300 #define RS_TRACE_ROW_SCATTER    8
301 #define RS_TRACE_ROW_PHOT_STAT  9
302 #define RS_TRACE_ROW_NDET       12
303 #define RS_TRACE_ROW_NVIEW      13
304 #define RS_TRACE_ROW_SAMPLES    14
305 #define RS_TRACE_ROW_CURR_VIEW  17
306 #define RS_TRACE_ROW_ATTEN      18
307
308
309 /*----------------------------------------------------------------------*/
310 /*                      GRAY SCALE STRUCTURES                           */
311 /*----------------------------------------------------------------------*/
312
313 #define GS_MAX_CELL_SIZE   4
314
315 typedef int GS_BITMASK[4][4];
316
317 struct greyscale_st {
318     int dev;                            /* Device to output to */
319     int (*dotfunc)(int x, int y, int color);                    /* Pointer to dot function for device */
320     int cur_x, cur_y;                   /* Current cell location */
321     int nxcell, nycell;                 /* size of cell in pixels */
322     int xmin, ymin;                     /* starting position of grey scale */
323     int num_color;                      /* Number of primary colors available */
324     int num_intens;                     /* Number of intensities available */
325     int max_level;                      /* gs levels range from 0 to max_level */
326     char *fg_color_tbl;                 /* Hold foreground color for each level */
327     char *bg_color_tbl;                 /* Holds background color */
328     char *level_sub_tbl;                /* Holds value to subtract for level */
329                                         /* before accessing bit mask */
330     GS_BITMASK *bm;                     /* Holds grey-scale bit mask */
331     struct greyscale_st *next_dev;      /* Pointer to next open device */
332                                         /* == NULL when no more devices */
333 };
334
335 typedef struct greyscale_st GREYSCALE;
336
337
338 /* From reconstr.c */
339 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);
340
341 /* From bproj.c */
342 void backproj_init (const RAYSUM *rs, IMAGE *im, const int bproj_method);
343 int  backproj_calc (const RAYSUM *rs, IMAGE *im, const double *t, const double view_angle, 
344                     const int interp_type, const int bproj_method);
345 void backproj_term (const RAYSUM *rs, IMAGE *im, const int bproj_method);
346
347 void backproj_init_trig (const RAYSUM *rs, IMAGE *im);
348 int  backproj_calc_trig (const RAYSUM *rs, IMAGE *im, const double *t, 
349                          const double view_angle, const int interp_type);
350 void backproj_term_trig (const RAYSUM *rs, IMAGE *im);
351 void backproj_init_table (const RAYSUM *rs, IMAGE *im);
352 int  backproj_calc_table (const RAYSUM *rs, IMAGE *im, const double *t, 
353                           const double view_angle, const int interp_type);
354 void backproj_term_table (const RAYSUM *rs, IMAGE *im);
355 void backproj_init_d (const RAYSUM *rs, IMAGE *im);
356 int  backproj_calc_d (const RAYSUM *rs, IMAGE *im, const double *t, 
357                       const double view_angle, const int interp_type);
358 void backproj_term_d (const RAYSUM *rs, IMAGE *im);
359 void backproj_init_d2 (const RAYSUM *rs, IMAGE *im);
360 int  backproj_calc_d2 (const RAYSUM *rs, IMAGE *im, const double *t, 
361                        const double view_angle, const int interp_type);
362 void backproj_term_d2 (const RAYSUM *rs, IMAGE *im);
363 void backproj_init_id (const RAYSUM *rs, IMAGE *im);
364 int  backproj_calc_id (const RAYSUM *rs, IMAGE *im, const double *t, 
365                        const double view_angle, const int interp_type);
366 void backproj_term_id (const RAYSUM *rs, IMAGE *im);
367 void backproj_init_id2 (const RAYSUM *rs, IMAGE *im);
368 int  backproj_calc_id2 (const RAYSUM *rs, IMAGE *im, const double *t, 
369                         const double view_angle, const int interp_type);
370 void backproj_term_id2 (const RAYSUM *rs, IMAGE *im);
371
372 void usage (const char *program);
373 int main(const int argc, char * const argv[]);
374
375
376
377 /* bspline.c */
378 int bspline(int samples, int zoom_factor, int spline_order, double input[], double output[]);
379 /* clip.c */
380 int inside_obj(const OBJECT *obj, double x, double y, const int coord_type);
381 int clipsegment(double *x1, double *y1, double *x2, double *y2, const double u, const double v);
382 int clipsector(double *x1, double *y1, double *x2, double *y2, const double u, const double v);
383 int clipcircle(double *x1, double *y1, double *x2, double *y2, const double cx, const double cy, const double radius, double t1, double t2);
384 int cliptriangle(double *x1, double *y1, double *x2, double *y2, const double u, const double v, const int clip_xaxis);
385 /* convolve.c */
386 double convolve(const double f1[], const double f2[], const double dx, const int n, const int np, const int func_type);
387 double convolve_both(const double f1[], const double f2[], const double dx, const int n, const int np);
388 /* ctsub.c */
389 OBJECT *alloc_obj(int n);
390 void dminmax(const double array[], const int pts, double *xmin, double *xmax);
391 void rotate2d(double x[], double y[], int pts, double angle);
392 void xlat2d(double x[], double y[], int pts, double xoffset, double yoffset);
393 void scale2d(double x[], double y[], int pts, double xfact, double yfact);
394 int circle_pts(double theta);
395 /* filt.c */
396 const char *interp_name_of(int interp_type);
397 const char *filter_name_of(int filt_type);
398 int interp_select(void);
399 int filter_select(double *filt_param);
400 double d_filtfunc(int filt_type, double x, double bw, double param, int n);
401 double filter_freq(int filt_type, double u, double bw, double param);
402 double a_filtfunc(int filt_type, double x, double bw, double param);
403 double sinc(double x, double mult);
404 double abscos_int(double u, double w);
405 double *filter_generate(int filt_type, double bw, double xmin, double xmax, int n, double param, int domain, int numint);
406 void image_filter_init(IMAGE *im, int domain, double bw, int filt_type, double filt_param, int opt_trace);
407 /* im_draw.c */
408 void image_show(IMAGE *im, int nxcell, int nycell, double densmin, double densmax, bool disp_scale);
409 int image_paint(int dev, IMAGE *im, int xmin, int ymin, int nxcell, int nycell, double densmin, double densmax, bool disp_scale);
410 int gs_image(GREYSCALE *gs, IMAGE *im, double densmin, double densmax, bool disp_scale);
411 void paint_cscale(GREYSCALE *gs, int nx, int ny);
412 GREYSCALE *gs_init(int dev, int xmin, int ymin, int nxcell, int nycell);
413 void gs_pixel(GREYSCALE *gs, int x, int y, int gs_level);
414 void gs_set_pos(GREYSCALE *gs, int x, int y);
415 void gs_set_at_addr(GREYSCALE *gs, int gs_level);
416 void gs_set_color_scale(void);
417 /* image.c */
418 IMAGE *image_create(const char *fname, const int nx, const int ny);
419 int image_clear(IMAGE *im);
420 int image_save(IMAGE *im);
421 IMAGE *image_load(const char *fname);
422 /* ir_vars.c */
423 /* options.c */
424 int opt_set_trace(const char *optarg, const char *program);
425 const char *name_of_picture(const int picnum);
426 int opt_set_picture(const char *optarg, const char *program);
427 int opt_set_interpolation(const char *optarg, const char *program);
428 int opt_set_filter(const char *optarg, const char *program);
429 const char *name_of_filter(const int filter);
430 int opt_set_filter_domain(const char *optarg, const char *program);
431 const char *name_of_filter_domain(const int domain);
432 int opt_set_backproj(const char *optarg, const char *program);
433 const char *name_of_backproj(const int backproj);
434 /* pic.c */
435 PICTURE *select_pic(void);
436 PICTURE *create_pic_from_file(const char *fname);
437 PICTURE *create_pic(const int picnum);
438 PICTURE *init_pic(void);
439 int add_objs_kb(PICTURE *pic);
440 int add_objs_file(PICTURE *pic, const char *fname);
441 void addobject(PICTURE *pic, const int type, const double cx, const double cy, 
442                const double u, const double v, const double rot, const double atten);
443 int makeobjpts(OBJECT *obj);
444 void makeobjxform(OBJECT *obj);
445 void calc_arc(double x[], double y[], const int pts, const double xcent, const double ycent, 
446               const double r, const double start, const double stop);
447 void calc_ellipse(double x[], double y[], const int pts, const double u, const double v);
448 void prt_pic(PICTURE *pic);
449 void show_pic(const PICTURE *pic);
450 void draw_pic(const PICTURE *pic);
451 /* pic2image.c */
452 void pic_to_image(const PICTURE *pic, IMAGE *im, const int col_start, const int col_count,
453                   const int nsample, const int trace);
454 /* ray.c */
455 void rs_trace_showprm (const char *label, const char *fmt, int row, int color, ...);
456 DETECTOR *detect_create(const PICTURE *pic, int ndet, int nview, int nsample, const double rot_anglen);
457 void detect_free(DETECTOR *det);
458 int raysum_collect(RAYSUM *rs, const DETECTOR *det, const PICTURE *pic, const int start_view, 
459                    const int trace, const int unit_pulse);
460 void rayview(const PICTURE *pic, DETARRAY *darray, const DETECTOR *det, 
461              const double xd1, const double yd1, const double xd2, const double yd2, 
462              const double xs1, const double ys1, const double xs2, const double ys2, const int unit_pulse);
463 double calc_rsum(const PICTURE *pic, const double x1, const double y1, const double x2, const double y2);
464 double calc_objsum(const OBJECT *obj, const double x1, const double y1, const double x2, const double y2);
465 int clipobj(const OBJECT *obj, double *x1, double *y1, double *x2, double *y2);
466 /* rayio.c */
467 RAYSUM *raysum_create(const char *fname, const int nview, const int ndet);
468 RAYSUM *raysum_create_from_det(const char *fname, const DETECTOR *det);
469 RAYSUM *raysum_open(const char *filename);
470 void raysum_alloc_views(RAYSUM *rs);
471 void raysum_free(RAYSUM *rs);
472 int raysum_is_open(RAYSUM *rs);
473 int raysum_close(RAYSUM *rs);
474 int raysum_read_header(RAYSUM *rs);
475 int raysum_write_header(RAYSUM *rs);
476 int raysum_read(RAYSUM *rs);
477 int raysum_write(RAYSUM *rs);
478 DETARRAY *detarray_alloc(const int n);
479 void detarray_free(DETARRAY *darray);
480 int detarray_read(RAYSUM *rs, DETARRAY *darray, const int view_num);
481 int detarray_write(RAYSUM *rs, const DETARRAY *darray, const int view_num);
482 int raysum_print(const RAYSUM *rs);
483 /* phantom.c */
484 void herm_head(PICTURE *pic);
485 void row_head(PICTURE *pic);
486 void row_bord_head(PICTURE *pic);
487 /* xform.c */
488 void indent_mtx2(GRFMTX_2D m);
489 void xlat_mtx2(GRFMTX_2D m, const double x, const double y);
490 void scale_mtx2(GRFMTX_2D m, const double sx, const double sy);
491 void rot_mtx2(GRFMTX_2D m, const double theta);
492 void mult_mtx2(GRFMTX_2D m1, GRFMTX_2D m2, GRFMTX_2D result);
493 void xform_mtx2(GRFMTX_2D m, double *x, double *y);
494
495 #endif