1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: ir.h,v 1.3 2000/04/29 23:24:29 kevin Exp $
7 ** Revision 1.3 2000/04/29 23:24:29 kevin
8 ** *** empty log message ***
10 ** Revision 1.2 2000/04/28 14:14:16 kevin
11 ** *** empty log message ***
13 ** Revision 1.1.1.1 2000/04/28 13:02:43 kevin
14 ** Initial CVS import for first public release
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.
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.
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
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
43 #define MPI_MAX_PROCESS 128
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];
55 extern struct mpi_ct_st mpi_ct;
56 void mpi_ct_calc_work_units(const unsigned int global_work_units);
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 */
66 typedef struct histo_st HISTOGRAM;
68 /*---------------------------------------------------------------------------*/
70 #define POINTS_PER_CIRCLE 36
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 */
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 */
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 */
101 struct object_st *next; /* pointer to next object in picture */
104 typedef struct object_st OBJECT;
106 struct pic_st { /* Picture structure */
107 OBJECT *objlist; /* object list */
109 int numobj; /* number of objects in picture */
110 double xmin, xmax, ymin, ymax; /* extent of objects in object coordinates */
111 double radius; /* " " */
114 typedef struct image_st IMAGE;
115 typedef struct pic_st PICTURE;
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 */
121 /*----------------------------------------------------------------------*/
123 /*----------------------------------------------------------------------*/
125 /* Ray sums are collected along an array of ndet detectors. The data
126 * for these detectors is stored in the structure DETECTARRAY
129 #define DETECT_TYPE float
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 */
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 */
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 */
159 struct detarray_st **view; /* Pointer to array of detarray_st pointers */
161 char remark[LENREMARK]; /* description of raysum data */
162 double calctime; /* time required to calculate raysums */
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 */
170 double det_inc; /* increment between detectors */
171 double piclen; /* Length of picture edge (pic is square) */
174 typedef struct detarray_st DETARRAY;
175 typedef struct detect_st DETECTOR;
176 typedef struct raysum_st RAYSUM;
178 /*----------------------------------------------------------------------*/
180 /*----------------------------------------------------------------------*/
182 /* codes for object types, passed to add_obj() */
184 #define O_RECTANGLE 1
190 /* Codes for Coordinate Types */
191 /* Defines coords for inside_obj() */
193 #define OBJ_COORD -1 /* Normalized Object Coordinates */
194 #define PIC_COORD -2 /* User's Picture Coordinates */
196 /* Codes for Filter types */
198 #define W_BANDLIMIT 1 /* filter types for genfilter() */
200 #define W_G_HAMMING 3
204 #define W_A_BANDLIMIT 11 /* filters times abs() of function */
206 #define W_AG_HAMMING 13
207 #define W_A_COSINE 14
211 /* function domains */
213 #define O_FREQ_STR "freq"
214 #define O_SPATIAL_STR "spatial"
216 #define D_FREQ 1 /* Domain names */
219 /* function symmetry */
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 */
225 /* interpolation methods */
227 #define I_NEAREST 1 /* Interpolation methods */
228 #define I_LINEAR 2 /* Linear interpolation */
230 #define I_1BSPLINE 3 /* 1st order B-Spline */
234 /* Constants for sizing picture */
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 */
241 #define DET_PARALLEL 1
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"
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 */
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 */
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"
269 #define O_INTERP_NEAREST_STR "nearest"
270 #define O_INTERP_LINEAR_STR "linear"
271 #define O_INTERP_BSPLINE_STR "bspline"
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"
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
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"
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
309 /*----------------------------------------------------------------------*/
310 /* GRAY SCALE STRUCTURES */
311 /*----------------------------------------------------------------------*/
313 #define GS_MAX_CELL_SIZE 4
315 typedef int GS_BITMASK[4][4];
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 */
335 typedef struct greyscale_st GREYSCALE;
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);
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);
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);
372 void usage (const char *program);
373 int main(const int argc, char * const argv[]);
378 int bspline(int samples, int zoom_factor, int spline_order, double input[], double output[]);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
484 void herm_head(PICTURE *pic);
485 void row_head(PICTURE *pic);
486 void row_bord_head(PICTURE *pic);
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);