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