1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: ir.h,v 1.4 2000/05/02 15:31:39 kevin Exp $
7 ** Revision 1.4 2000/05/02 15:31:39 kevin
10 ** Revision 1.3 2000/04/29 23:24:29 kevin
11 ** *** empty log message ***
13 ** Revision 1.2 2000/04/28 14:14:16 kevin
14 ** *** empty log message ***
16 ** Revision 1.1.1.1 2000/04/28 13:02:43 kevin
17 ** Initial CVS import for first public release
21 ** This program is free software; you can redistribute it and/or modify
22 ** it under the terms of the GNU General Public License (version 2) as
23 ** published by the Free Software Foundation.
25 ** This program is distributed in the hope that it will be useful,
26 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ** GNU General Public License for more details.
30 ** You should have received a copy of the GNU General Public License
31 ** along with this program; if not, write to the Free Software
32 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 ******************************************************************************/
34 /* FILE IDENTIFICATION
36 * Name: ir.h Header File for Image Reconstruction System
37 * Programmer: Kevin Rosenberg
38 * Date Started: 7-1-84
39 * Last Change: 1-20-85
46 #define MPI_MAX_PROCESS 128
51 int base_local_work_units;
52 int remainder_work_units;
53 int local_work_units[MPI_MAX_PROCESS];
54 int start_work_unit[MPI_MAX_PROCESS];
58 extern struct mpi_ct_st mpi_ct;
59 void mpi_ct_calc_work_units(const unsigned int global_work_units);
64 int *b; /* Histogram array (# of elements in each bin) */
65 int nbin; /* Number of histogram bins */
66 double xmin, xmax, xinc; /* Limits of histogram boundaries */
69 typedef struct histo_st HISTOGRAM;
71 /*---------------------------------------------------------------------------*/
73 #define POINTS_PER_CIRCLE 36
76 #define LENREMARK (MAXREMARK+1)
77 #define IMAGE_VAL FMTX_2D /* use 2d floating point matrix */
78 #define IMAGE_ELEM_TYPE DT_FLOAT /* use floats for images */
79 typedef float IMAGE_ELEM_VAL; /* use floats for image storing */
82 IMAGE_VAL v; /* values of voxels in matrix form */
83 SDF_2D *dfp_2d; /* Pointer to disk image file */
84 int nx, ny; /* size of voxel matrix */
85 double xmin, xmax, ymin, ymax; /* extent of voxel matrix in pic coord */
86 char remark[LENREMARK]; /* description of voxel data */
87 float calctime; /* time to calculate voxels in seconds */
91 int type; /* object type (box, ellipse, etc) */
92 double atten; /* X-ray attenuation coefficient */
93 double cx,cy; /* center of object */
94 double u,v; /* size of object */
95 double rot; /* object rotation angle (in radians) */
96 double *x, *y; /* ptr to array of points in obj world coord */
97 int pts; /* number of points in outline arrays */
98 double xmin, xmax, ymin, ymax; /* object limits */
99 double radius; /* " */
100 struct { /* transform matrices */
101 GRFMTX_2D p_to_o; /* map from pic to standard obj coords */
102 GRFMTX_2D o_to_p; /* map from std object coords to pic coords */
104 struct object_st *next; /* pointer to next object in picture */
107 typedef struct object_st OBJECT;
109 struct pic_st { /* Picture structure */
110 OBJECT *objlist; /* object list */
112 int numobj; /* number of objects in picture */
113 double xmin, xmax, ymin, ymax; /* extent of objects in object coordinates */
114 double radius; /* " " */
117 typedef struct image_st IMAGE;
118 typedef struct pic_st PICTURE;
120 #define P_OBJECTS 0 /* Picture made of objects */
121 #define P_UNIT_PULSE 1 /* Special picture, not made of objects */
122 #define P_FILTER 9 /* defined only by a type */
124 /*----------------------------------------------------------------------*/
126 /*----------------------------------------------------------------------*/
128 /* Ray sums are collected along an array of ndet detectors. The data
129 * for these detectors is stored in the structure DETECTARRAY
132 #define DETECT_TYPE float
135 DETECT_TYPE *detval; /* Pointer to array of values recorded by detector */
136 int ndet; /* Number of detectors in array */
137 double view_angle; /* View angle in radians */
141 int ndet; /* Number of detectors in array */
142 int nview; /* Number of rotated views */
143 int nsample; /* Number of rays per detector */
144 double detlen; /* Total length of detector array */
145 double rotlen; /* Rotation angle length in radians (norm 2PI) */
146 double det_inc; /* Increment between centers of detectors */
147 double rot_inc; /* Increment in rotation angle between views */
148 double radius; /* Radius of rotation. Distance from */
149 /* center of pic to center of det */
150 double piclen; /* Maximum Length of picture or area of interest */
152 double xd1,yd1,xd2,yd2; /* Coordinates of detector endpoints */
153 double xs1,ys1,xs2,ys2; /* Coordinates of source endpoints */
154 double angle; /* Starting angle */
162 struct detarray_st **view; /* Pointer to array of detarray_st pointers */
164 char remark[LENREMARK]; /* description of raysum data */
165 double calctime; /* time required to calculate raysums */
167 int ndet; /* number of detectors in array */
168 int nview; /* number of rotated views */
169 double rot_start; /* starting view rotation */
170 double rot_inc; /* angle between rotations */
171 double det_start; /* distance of beginning detector to center */
173 double det_inc; /* increment between detectors */
174 double piclen; /* Length of picture edge (pic is square) */
177 typedef struct detarray_st DETARRAY;
178 typedef struct detect_st DETECTOR;
179 typedef struct raysum_st RAYSUM;
181 /*----------------------------------------------------------------------*/
183 /*----------------------------------------------------------------------*/
185 /* codes for object types, passed to add_obj() */
187 #define O_RECTANGLE 1
193 /* Codes for Coordinate Types */
194 /* Defines coords for inside_obj() */
196 #define OBJ_COORD -1 /* Normalized Object Coordinates */
197 #define PIC_COORD -2 /* User's Picture Coordinates */
199 /* Codes for Filter types */
201 #define W_BANDLIMIT 1 /* filter types for genfilter() */
203 #define W_G_HAMMING 3
207 #define W_A_BANDLIMIT 11 /* filters times abs() of function */
209 #define W_AG_HAMMING 13
210 #define W_A_COSINE 14
214 /* function domains */
216 #define O_FREQ_STR "freq"
217 #define O_SPATIAL_STR "spatial"
219 #define D_FREQ 1 /* Domain names */
222 /* function symmetry */
224 #define FUNC_EVEN 1 /* function types, f[-n] = f[n] */
225 #define FUNC_ODD 2 /* f[-n] = -f[n] */
226 #define FUNC_BOTH 3 /* function has both odd & even components */
228 /* interpolation methods */
230 #define I_NEAREST 1 /* Interpolation methods */
231 #define I_LINEAR 2 /* Linear interpolation */
233 #define I_1BSPLINE 3 /* 1st order B-Spline */
237 /* Constants for sizing picture */
239 #define PERCENT_PIC_SIZE_INCR 0.0 /* Fractional increase in picture */
240 /* limits compared to object size */
241 #define N_EXTRA_DETECTORS 4 /* Number of extra detectors */
242 /* widths when calculating detlen */
244 #define DET_PARALLEL 1
247 #define O_TRACE_NONE_STR "none"
248 #define O_TRACE_TEXT_STR "text"
249 #define O_TRACE_PIC_STR "pic"
250 #define O_TRACE_RAYS_STR "rays"
251 #define O_TRACE_PLOT_STR "plot"
252 #define O_TRACE_CLIPPING_STR "clipping"
255 #define TRACE_NONE 0 /* No tracing */
256 #define TRACE_TEXT 1 /* Minimal status */
257 #define TRACE_PIC 2 /* Show picture */
258 #define TRACE_RAYS 3 /* Show all rays */
259 #define TRACE_PLOT 4 /* Plot raysums */
260 #define TRACE_CLIPPING 5 /* Plot clipping */
262 #define O_PIC_HERMAN 1 /* Herman head phantom */
263 #define O_PIC_ROWLAND 2 /* Rowland head phantom */
264 #define O_PIC_BROWLAND 3 /* Bordered Rowland head phantom */
265 #define O_PIC_UNITPULSE 4 /* Unit pulse phantom */
267 #define O_PIC_HERMAN_STR "herman"
268 #define O_PIC_ROWLAND_STR "rowland"
269 #define O_PIC_BROWLAND_STR "browland"
270 #define O_PIC_UNITPULSE_STR "unitpulse"
272 #define O_INTERP_NEAREST_STR "nearest"
273 #define O_INTERP_LINEAR_STR "linear"
274 #define O_INTERP_BSPLINE_STR "bspline"
276 #define O_FILTER_ABS_BANDLIMIT_STR "abs_bandlimit"
277 #define O_FILTER_ABS_SINC_STR "abs_sinc"
278 #define O_FILTER_ABS_COS_STR "abs_cos"
279 #define O_FILTER_ABS_HAMMING_STR "abs_hamming"
280 #define O_FILTER_SHEPP_STR "shepp"
281 #define O_FILTER_BANDLIMIT_STR "bandlimit"
282 #define O_FILTER_SINC_STR "sinc"
283 #define O_FILTER_COS_STR "cos"
284 #define O_FILTER_HAMMING_STR "hamming"
285 #define O_FILTER_TRIANGLE_STR "triangle"
287 #define O_BPROJ_TRIG 1
288 #define O_BPROJ_TABLE 2
289 #define O_BPROJ_DIFF 3
290 #define O_BPROJ_DIFF2 4
291 #define O_BPROJ_IDIFF2 5
293 #define O_BPROJ_TRIG_STR "trig"
294 #define O_BPROJ_TABLE_STR "table"
295 #define O_BPROJ_DIFF_STR "diff"
296 #define O_BPROJ_DIFF2_STR "diff2"
297 #define O_BPROJ_IDIFF2_STR "idiff2"
299 #define RS_TRACE_ROW_TITLE 1
300 #define RS_TRACE_ROW_TITLE2 2
301 #define RS_TRACE_ROW_PHANT_ID 4
302 #define RS_TRACE_ROW_CHROMATIC 7
303 #define RS_TRACE_ROW_SCATTER 8
304 #define RS_TRACE_ROW_PHOT_STAT 9
305 #define RS_TRACE_ROW_NDET 12
306 #define RS_TRACE_ROW_NVIEW 13
307 #define RS_TRACE_ROW_SAMPLES 14
308 #define RS_TRACE_ROW_CURR_VIEW 17
309 #define RS_TRACE_ROW_ATTEN 18
312 /*----------------------------------------------------------------------*/
313 /* GRAY SCALE STRUCTURES */
314 /*----------------------------------------------------------------------*/
316 #define GS_MAX_CELL_SIZE 4
318 typedef int GS_BITMASK[4][4];
320 struct greyscale_st {
321 int dev; /* Device to output to */
322 int (*dotfunc)(int x, int y, int color); /* Pointer to dot function for device */
323 int cur_x, cur_y; /* Current cell location */
324 int nxcell, nycell; /* size of cell in pixels */
325 int xmin, ymin; /* starting position of grey scale */
326 int num_color; /* Number of primary colors available */
327 int num_intens; /* Number of intensities available */
328 int max_level; /* gs levels range from 0 to max_level */
329 char *fg_color_tbl; /* Hold foreground color for each level */
330 char *bg_color_tbl; /* Holds background color */
331 char *level_sub_tbl; /* Holds value to subtract for level */
332 /* before accessing bit mask */
333 GS_BITMASK *bm; /* Holds grey-scale bit mask */
334 struct greyscale_st *next_dev; /* Pointer to next open device */
335 /* == NULL when no more devices */
338 typedef struct greyscale_st GREYSCALE;
341 /* From reconstr.c */
342 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);
345 void backproj_init (const RAYSUM *rs, IMAGE *im, const int bproj_method);
346 int backproj_calc (const RAYSUM *rs, IMAGE *im, const double *t, const double view_angle,
347 const int interp_type, const int bproj_method);
348 void backproj_term (const RAYSUM *rs, IMAGE *im, const int bproj_method);
350 void backproj_init_trig (const RAYSUM *rs, IMAGE *im);
351 int backproj_calc_trig (const RAYSUM *rs, IMAGE *im, const double *t,
352 const double view_angle, const int interp_type);
353 void backproj_term_trig (const RAYSUM *rs, IMAGE *im);
354 void backproj_init_table (const RAYSUM *rs, IMAGE *im);
355 int backproj_calc_table (const RAYSUM *rs, IMAGE *im, const double *t,
356 const double view_angle, const int interp_type);
357 void backproj_term_table (const RAYSUM *rs, IMAGE *im);
358 void backproj_init_d (const RAYSUM *rs, IMAGE *im);
359 int backproj_calc_d (const RAYSUM *rs, IMAGE *im, const double *t,
360 const double view_angle, const int interp_type);
361 void backproj_term_d (const RAYSUM *rs, IMAGE *im);
362 void backproj_init_d2 (const RAYSUM *rs, IMAGE *im);
363 int backproj_calc_d2 (const RAYSUM *rs, IMAGE *im, const double *t,
364 const double view_angle, const int interp_type);
365 void backproj_term_d2 (const RAYSUM *rs, IMAGE *im);
366 void backproj_init_id (const RAYSUM *rs, IMAGE *im);
367 int backproj_calc_id (const RAYSUM *rs, IMAGE *im, const double *t,
368 const double view_angle, const int interp_type);
369 void backproj_term_id (const RAYSUM *rs, IMAGE *im);
370 void backproj_init_id2 (const RAYSUM *rs, IMAGE *im);
371 int backproj_calc_id2 (const RAYSUM *rs, IMAGE *im, const double *t,
372 const double view_angle, const int interp_type);
373 void backproj_term_id2 (const RAYSUM *rs, IMAGE *im);
375 void usage (const char *program);
376 int main(const int argc, char * const argv[]);
381 int bspline(int samples, int zoom_factor, int spline_order, double input[], double output[]);
383 int inside_obj(const OBJECT *obj, double x, double y, const int coord_type);
384 int clipsegment(double *x1, double *y1, double *x2, double *y2, const double u, const double v);
385 int clipsector(double *x1, double *y1, double *x2, double *y2, const double u, const double v);
386 int clipcircle(double *x1, double *y1, double *x2, double *y2, const double cx, const double cy, const double radius, double t1, double t2);
387 int cliptriangle(double *x1, double *y1, double *x2, double *y2, const double u, const double v, const int clip_xaxis);
389 double convolve(const double f1[], const double f2[], const double dx, const int n, const int np, const int func_type);
390 double convolve_both(const double f1[], const double f2[], const double dx, const int n, const int np);
392 void dminmax(const double array[], const int pts, double *xmin, double *xmax);
393 void rotate2d(double x[], double y[], int pts, double angle);
394 void xlat2d(double x[], double y[], int pts, double xoffset, double yoffset);
395 void scale2d(double x[], double y[], int pts, double xfact, double yfact);
396 int circle_pts(double theta);
398 double d_filtfunc(int filt_type, double x, double bw, double param, int n);
399 double filter_freq(int filt_type, double u, double bw, double param);
400 double a_filtfunc(int filt_type, double x, double bw, double param);
401 double sinc(double x, double mult);
402 double abscos_int(double u, double w);
403 double *filter_generate(int filt_type, double bw, double xmin, double xmax, int n, double param, int domain, int numint);
404 void image_filter_init(IMAGE *im, int domain, double bw, int filt_type, double filt_param, int opt_trace);
406 void image_show(IMAGE *im, int nxcell, int nycell, double densmin, double densmax, bool disp_scale);
407 int image_paint(int dev, IMAGE *im, int xmin, int ymin, int nxcell, int nycell, double densmin, double densmax, bool disp_scale);
408 int gs_image(GREYSCALE *gs, IMAGE *im, double densmin, double densmax, bool disp_scale);
409 void paint_cscale(GREYSCALE *gs, int nx, int ny);
410 GREYSCALE *gs_init(int dev, int xmin, int ymin, int nxcell, int nycell);
411 void gs_pixel(GREYSCALE *gs, int x, int y, int gs_level);
412 void gs_set_pos(GREYSCALE *gs, int x, int y);
413 void gs_set_at_addr(GREYSCALE *gs, int gs_level);
414 void gs_set_color_scale(void);
416 IMAGE *image_create(const char *fname, const int nx, const int ny);
417 int image_clear(IMAGE *im);
418 int image_save(IMAGE *im);
419 IMAGE *image_load(const char *fname);
422 int opt_set_trace(const char *optarg, const char *program);
423 const char *name_of_picture(const int picnum);
424 int opt_set_picture(const char *optarg, const char *program);
425 int opt_set_interpolation(const char *optarg, const char *program);
426 const char *name_of_interpolation(int interp_type);
427 int opt_set_filter(const char *optarg, const char *program);
428 const char *name_of_filter(const int filter);
429 int opt_set_filter_domain(const char *optarg, const char *program);
430 const char *name_of_filter_domain(const int domain);
431 int opt_set_backproj(const char *optarg, const char *program);
432 const char *name_of_backproj(const int backproj);
434 PICTURE *select_pic(void);
435 PICTURE *create_pic_from_file(const char *fname);
436 PICTURE *create_pic(const int picnum);
437 PICTURE *init_pic(void);
438 int add_objs_kb(PICTURE *pic);
439 int add_objs_file(PICTURE *pic, const char *fname);
440 void addobject(PICTURE *pic, const int type, const double cx, const double cy,
441 const double u, const double v, const double rot, const double atten);
442 int makeobjpts(OBJECT *obj);
443 void makeobjxform(OBJECT *obj);
444 void calc_arc(double x[], double y[], const int pts, const double xcent, const double ycent,
445 const double r, const double start, const double stop);
446 void calc_ellipse(double x[], double y[], const int pts, const double u, const double v);
447 OBJECT *alloc_obj(void);
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(const GRFMTX_2D m, double *x, double *y);