STIR  6.2.0
SPECTUB_Tools.h
1 /*
2  Copyright (c) 2013, Biomedical Image Group (GIB), Universitat de Barcelona, Barcelona, Spain.
3  Copyright (c) 2013, University College London
4  This file is part of STIR.
5 
6  SPDX-License-Identifier: Apache-2.0
7 
8  See STIR/LICENSE.txt for details
9 
10  \author Carles Falcon
11 */
12 
13 #ifndef _WM_SPECTUB_H
14 #define _WM_SPECTUB_H
15 
16 #include <iostream>
17 #include <vector>
18 
19 #include <string>
20 
21 namespace SPECTUB
22 {
23 
24 //::: srtuctures ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
25 
27 typedef struct
28 {
29  int num; // number of collimator (see weight_64.cpp for options)
30  bool do_fb; // true: fanbeam collimator || false: parallel collimator
31 
32  //... parallel collimator parameters .....................
33 
34  float A; // linear factor for dependency of sigma on distance: sigma=A*dist+B (parallel, fanbeam-vertical)
35  float B; // independent factor for dependency of sigma on distance: sigma=A*dist+B (parallel, fanbeam-vertical)
36 
37  //... fanbeam collimator parameters ......................
38 
39  float F; // Focal length (fanbeam)
40  float L; // collimator to detector distance (?) (fanbeam)
41  float A_h; // linear factor for dependency of sigma on distance (fanbeam horizontal)
42  float A_v; // linear factor for dependency of sigma on distance (fanbeam horizontal)
43  float D; // collimator thicknes?? (fanbeam)
44  float w; // collimator thickness (?) (fanbeam)
45  float insgm; // intrinsic sigma (cristal resolution) (fanbeam)
46 
47 } collim_type;
48 
50 typedef struct
51 {
52  int Nrow; // number of rows
53  int Ncol; // number of columns
54  int Nsli; // number of slices
55  int Npix; // number of pixels (axial planes)
56  int Nvox; // number of voxels (the whole volume)
57 
58  int first_sl; // first slice to reconstruct (0->Nslic-1)
59  int last_sl; // last slice to reconstruct + 1 (end of the 'for' loop) (1->Nslic)
60 
61  float Nrowd2; // half of Nrow
62  float Ncold2; // half of Ncol
63  float Nslid2; // half of Nsli
64 
65  float Xcmd2; // Half of the size of the volume, dimension x (cm);
66  float Ycmd2; // Half of the size of the volume, dimension y (cm);
67  float Zcmd2; // Half of the size of the volume, dimension z (cm);
68 
69  float szcm; // voxel size (side length in cm)
70  float thcm; // voxel thickness (cm)
71 
72  float x0; // x coordinate (cm, ref center of volume) of the first voxel
73  float y0; // y coordinate (cm, ref center of volume) of the first voxel
74  float z0; // z coordinate (cm, ref center of volume) of the first voxel
75 
76  float* val; // array of values
77 
78 } volume_type;
79 
81 typedef struct
82 {
83  int Nbin; // length of the detection line in bins (number of bins per line)
84  float lngcm; // length of the detection line in cm.
85  float szcm; // bin size in cm
86 
87  int Nsli; // number of slices
88  float thcm; // slice thickness in cm
89 
90  int Nang; // number of projection angles
91  float ang0; // initial projection angle. degrees from upper detection plane (parallel to table). Negative for CW rotacions (see
92  // manual)
93  float incr; // angle increment between two consecutive projection angles. Degrees. Negative for CW, Positive for CCW
94 
95  int NOS; // number of subsets in which to split the matrix
96  int NangOS; // Number of angles in each subset = Nang/NOS
97  int Nbp; // number of bins in a 2D projection=lng*Nsli
98  int Nbt; // total number of bins= Nbp*Nang
99  int NbOS; // total number of bins per subset= Nbp*NangOS = Nbt/NOS
100  int* order; // order of the angles of projection (array formed by indexs of angles belonging to consecutive subsets)
101 
102  float Nbind2; // length of the detection line (in bins) divided by 2
103  float lngcmd2; // length of the detection line in cm divided by 2
104  float Nslid2; // number of slices divided by 2
105 
106 } proj_type;
107 
109 typedef struct
110 {
111  int subset_ind; // subset index of this matrix (-1: all subsets in a single file)
112  int* index; // included angles into this subset (index. Multiply by increm to get corresponding angles in degrees)
113 
114  float* Rrad; // Rotation radius (one value for each projection angle)
115  float min_w; // minimum weight to be taken into account
116  float psfres; // spatial resolution of continous distributions in PSF calculation
117  float maxsigm; // maximum number of sigmas in PSF calculation
118 
119  bool fixed_Rrad; // true: fixed radius of projection || false: variable radius of projection
120  bool do_psf; // true: to correct for PSF || false: do not correct for PSF
121  bool do_psf_3d; // true: 3d correction for PSF || false: 2d correction for PSF
122  bool predef_col; // true: predefined collimator || false: user defined PSF parametres
123  bool do_att; // true: to correct for attenuation || false: do not correct for attenuation
124  bool do_full_att; // true: diff att for each PSF bin || false: the whole PSF has the same att. factor (central line)
125  bool do_msk; // true: weights just inside msk || false: weights for the whole FOV
126  bool do_msk_slc; // true: weights for several slices || false: weights for all slices
127  bool do_msk_cyl; // true: to use cylinder as a mask || false: not to use cylinder as a mask
128  bool do_msk_att; // true: to use att map as a mask || false: not to use att map as a mask
129  bool do_msk_file; // true: explicit mask || false: not to use explicit mask
130 
131  std::string att_fn; // attenuation map filename
132  std::string msk_fn; // explicit mask filename
133  std::string col_fn; // collimator parameters filename
134  std::string Rrad_fn; // rotation radius file name
135 
136  volume_type vol; //
137  proj_type prj; //
138  collim_type COL; // collimator structure (see weight3d_64b.cpp for options)
139 
140 } wmh_type;
141 
143 typedef struct
144 {
145  // weight matrix dimensions
146 
147  int ne; // nonzero elements
148  int NbOS; // dimension 1 (rows) of the weight matrix (NbOS or NBt)
149  int Nvox; // dimension 2 (columns) of the weight matrix (Nvox)
150 
151  // weight matrix values
152 
153  float* ar; // array of nonzero elements of weight matrix (by rows)
154  int* ja; // array of the column index of the above elements
155  int* ia; // array containing the indexes of the previous vector where a row change happens
156 
157  bool do_save_wmh; // to save or not to save weight_mat header info into weight_mat file
158 
159 } wm_type;
160 
162 typedef struct
163 {
164  int NbOS; // dimension 1 (rows) of the weight matrix (NbOS or NBt)
165  int Nvox; // dimension 2 (columns) of the weight matrix (Nvox)
166  float** val; // double array to store weights (index of the projection element, number of weight for that element)
167  int** col; // double array to store column indexs of the above element (index of the projection element, number of weight for
168  // that element)
169  int* ne; // array indicating how many elements has been stored for each element of projection
170 
171  //... filename .............................................
172 
173  std::string fn; // matrix base name (filename without extension index)
174  std::string OSfn; // matrix filename
175  std::string fn_hdr; // matrix header file name
176 
177  //... indexs for STIR format ...............................
178 
179  int *na, *nb, *ns; // indexs for projection elements (angle, bin, slice respec.)
180  short int *nx, *ny, *nz; // indexs for image elements (x,y,z)
181 
182  //... format ...............................................
183 
184  bool do_save_wmh; // to save or not to save weight_mat header info into weight_mat file
185  bool do_save_STIR; // to save weight matrix with STIR format
186 
187 } wm_da_type;
188 
190 typedef struct
191 {
192  int lng; // length (in discretization intervals) (odd number)
193  int lngd2; // half of the length (in discretization intervals) (lng-1)/2
194  float res; // spatial resolution of distfunc (discretization interval)
195  float* val; // array of values
196  float* acu; // distribution function values (cumulative sum)
197 
198 } discrf_type;
199 
201 typedef struct
202 {
203  int maxszb; // maximum size in bins (for allocation purposes)
204 
205  int di; // discretization interval (to reduce spatial resolution to bin resolution). (int: #points)
206  int* ind; // projection indexs for the bins of the PSF (horizontal)
207  int Nib; // actual number of bins forming the PSF (length of PSF in bins)
208 
209  float sgmcm; // sigma of the PSF in cm
210  float lngcm; // length of PSF (in cm)
211  float lngcmd2; // half of the length of PSF (in cm)
212  float* val; // array of values
213  float efres; // effective resolution (psfres rescaled to real psf length)
214 
215 } psf1d_type;
216 
218 typedef struct
219 {
220  int maxszb_h; // maximum size in bins horizontal (for allocation purposes)
221  int maxszb_v; // maximum size in bins vertical (for allocation purposes)
222  int maxszb_t; // maximum size in bins total (for allocation purposes)
223 
224  int* ib; // projection indexs for the bins of the PSF (horizontal)
225  int* jb; // projection indexs for the bins of the PSF (vertical)
226  int Nib; // actual number of bins forming the PSF (length of PSF in bins)
227 
228  float* val; // array of values
229 
230 } psf2da_type;
231 
233 typedef struct
234 {
235  int ind; // index of angle considering the whole set of projections (sequential order: 0->Nang-1)
236  int indOS; // index of angle considering the subjet
237  int iOS_proj; // index of the first bin for this angle (in subset of projections)
238 
239  float cos; // coninus of the angle
240  float sin; // sinus of the angle
241 
242  // parametres for describng the trapezoidal projection of a square voxel
243 
244  float p; // plateau higness
245  float m; // slope of the trapezoid
246  float n; // independent term of the slope
247  int N1; // index of the first vertice (end of plateau) in DX units
248  int N2; // index of the second vertice (end of the slope) in DX units
249  discrf_type vxprj; // projection of a square voxel in this direction (for no PSF)
250 
251  // variable rotation radius
252 
253  float Rrad; // rotation radius for this angle
254 
255  // first bin position and increments
256 
257  float xbin0; // x coordinate for the first bin of the detection line corresponding to this angle
258  float ybin0; // y coordinate for the first bin of the detection line corresponding to this angle
259  float incx; // increment in x to the following bin in detection line
260  float incy; // increment in y to the following bin in detection line
261 
262 } angle_type;
263 
265 typedef struct
266 {
267  float szcm; // voxel size (side length in cm)
268  float thcm; // voxel thickness (cm)
269 
270  int irow; // row index
271  int icol; // column index
272  int islc; // slice index
273  int ip; // in plane index (considering the slice as an array) of the voxel
274  int iv; // volume index (considering the volume as an array) of the voxel
275 
276  float x; // x coordinate (cm, ref center of volume)
277  float y; // y coordinate (cm, ref center of volume)
278  float z; // z coordinate (cm, ref center of volume)
279  float x1; // x coordinade in rotated framework
280 
281  float dv2dp; // distance from voxel to detection plane
282  float costhe; // cosinus of theta (angle between focal-voxel line and line perpendicular to detection plane) (fanbeam)
283  float xdc; // distance (cm over detection line) from projected voxel to the center of the detection line
284  float xd0; // distance (cm over detection line) from projected voxel to the begin of the detection line
285  float zd0; // distance (cm) to the lowest plane of the volume
286 
287 } voxel_type;
288 
290 typedef struct
291 {
292  float szcm; // bin size (cm)
293  float szcmd2; // half of the above value
294  float thcm; // bin thickness (cm)
295  float thcmd2; // bin thickness (cm)
296 
297  float x; // x coordinate (cm, ref center of volume)
298  float y; // y coordinate (cm, ref center of volume)
299  float z; // z coordinate (cm, ref center of volume)
300 
301  float szdx; // bin size in resolution units
302  float thdx; // bin thickness in resolution units
303 
304 } bin_type;
305 
307 typedef struct
308 {
309  float* dl; // distance of attenuation path on each crossed voxel of the attenuation map
310  int* iv; // in-plane index (considering slices of attmap as an array) of any crossed voxel of the attenuation map
311  int lng; // number of elements in the attenuation path
312  int maxlng; // maximum number of elements in the attenuation path (for allocation)
313 
314 } attpth_type;
315 
316 //::: functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
317 
318 //... functions from wmtools_SPECT.cpp .........................................
319 
320 void write_wm_FC(wm_da_type& wm); // to write double array weight matrix
321 
322 void write_wm_hdr(wm_da_type& wm, wmh_type& wmh); // to write header of a matrix
323 
324 void write_wm_STIR(SPECTUB::wm_da_type& wm); // to write matrix in STIR format
325 
326 void index_calc(int* indexs, wmh_type& wmh); // to calculate projection index order in subsets
327 
328 void read_Rrad(float* Rrad, wmh_type& wmh); // to read variable rotation radius from a text file (1 radius per line)
329 
330 //
331 // void col_params ( collim_type *COL ); // to fill collimator structure
332 //
333 // void read_col_params ( collim_type *COL); // to read collimator parameters from a file
334 
335 void fill_ang(angle_type* ang, wmh_type& wmh, const float* Rrad); // to fill angle structure
336 
337 void generate_msk(bool* msk_3d,
338  bool* msk_2d,
339  float* att,
340  volume_type* vol,
341  wmh_type& wmh); // to create a boolean mask for wm (no weights outside the msk)
342 
343 void read_msk_file(bool* msk, wmh_type& wmh); // to read mask from a file
344 
345 void read_att_map(float* attmap, wmh_type& wmh); // to read attenuation map from a file
346 
347 int max_psf_szb(angle_type* ang, wmh_type& wmh);
348 
349 float calc_sigma_h(voxel_type vox, collim_type COL);
350 
351 float calc_sigma_v(voxel_type vox, collim_type COL, wmh_type& wmh);
352 
353 char* itoa(int n, char* s); // to conver integer to ascii
354 
355 void free_wm(wm_type* f); // to free weight_mat
356 
357 void free_wm_da(wm_da_type* f); // to free weight_mat_da
358 
359 void error_wmtools_SPECT(int nerr, std::string txt); // error messages in wm_SPECT
360 
361 //... functions from wm_SPECT.2.0............................
362 
363 // int wm_SPECT( std::string inputFile);
364 
365 // void error_wm_SPECT( int nerr, std::string txt); //list of error messages
366 
368 // void wm_inputs(char **argv,
369 // int argc,
370 // proj_type *prj,
371 // volume_type *vol,
372 // voxel_type *vox,
373 // bin_type *bin);
374 //
375 //
377 // void read_inputs(vector<std::string> param,
378 // proj_type *prj,
379 // volume_type *vol,
380 // voxel_type *vox,
382 //
383 // extern wmh_type wmh; // weight matrix header. Global variable
384 //
385 // extern wm_da_type wm; // double array weight matrix structure. Global variable
386 //
387 // extern float * Rrad; // variable projection radius (in acquisition order)
388 
389 } // namespace SPECTUB
390 
391 #endif //_WM_SPECT_H
structure for voxel information
Definition: SPECTUB_Tools.h:265
Namespace for the SPECT library from University of Barcelona.
Definition: SPECTUB_Tools.h:21
complementary information (matrix header)
Definition: SPECTUB_Tools.h:109
structure for distribution function information
Definition: SPECTUB_Tools.h:218
float calc_sigma_v(voxel_type vox, collim_type COL, wmh_type &wmh)
=============================================================================
Definition: SPECTUB_Tools.cxx:580
structure for projection information
Definition: SPECTUB_Tools.h:81
structure for bin information
Definition: SPECTUB_Tools.h:50
structure for attenuation calculus
Definition: SPECTUB_Tools.h:307
structure to store angles values, indices and ratios
Definition: SPECTUB_Tools.h:233
weight_mat structure definition. Structure for reading weight matrix
Definition: SPECTUB_Tools.h:143
weight_mat_da structure definition. Structure for generating weight matrix
Definition: SPECTUB_Tools.h:162
structure for PSF information
Definition: SPECTUB_Tools.h:201
structure for distribution function information
Definition: SPECTUB_Tools.h:190
collimator parameters structure
Definition: SPECTUB_Tools.h:27
structure for bin information
Definition: SPECTUB_Tools.h:290