calc_dose.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* calc_dose.cpp */
  2. /* The dose at all voxels in the grid dose_mask is calculated using a convolution
  3. method that uses a polyenergetic kernel. Inhomogeneities are accounted for by kernel
  4. scaling, and an inverse square correction is applied after the convolution of the terma
  5. grid with the kernel, rather than being applied directly to the terma grid before
  6. the convolution. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <malloc.h>
  10. #include <math.h>
  11. #include <string.h>
  12. #include "defs.h"
  13. extern char errstr[200]; // error string that all routines have access to
  14. int calc_dose(FLOAT_GRID *density, FLOAT_GRID *terma,FLOAT_GRID *dose, KERNEL *kern,
  15. BEAM *bm, FLOAT_GRID *dose_mask)
  16. {
  17. int i, j, k, l;
  18. int p, q, r;
  19. int M, N, Q; // dimensions of CT array
  20. int inter_i, inter_j, inter_k;
  21. int r_index;
  22. int baseindex;
  23. float SAD;
  24. float del_x, del_y, del_z;
  25. float current_x, current_y, current_z;
  26. float inter_x, inter_y, inter_z;
  27. float r_eff, delr_eff, inter_r_eff;
  28. float kval;
  29. float cumval, last_cumval;
  30. float length;
  31. float *ip, *jp, *kp;
  32. float dx, dy, dz;
  33. float delr; // convolution step size
  34. float t, u, v, f;
  35. float *x, *y, *z; // vectors of CT coordinates
  36. float *phi, *theta, *sinphi, *cosphi, *sintheta, *costheta;
  37. float one;
  38. float rho;
  39. one = (float)1.0;
  40. // copy CT dimensions and voxel sizes for shorter references later
  41. M = density->x_count;
  42. N = density->y_count;
  43. Q = density->z_count;
  44. dx = density->inc.x;
  45. dy = density->inc.y;
  46. dz = density->inc.z;
  47. // copy vectors describing the beam's eye view coordinate system as well
  48. ip = fvector(0,2); // ip and jp span the beam's eye view
  49. jp = fvector(0,2);
  50. kp = fvector(0,2); // beam direction
  51. // create the unit vector describing the beam direction
  52. for (j=0;j<3;j++) ip[j] = bm->ip[j];
  53. for (j=0;j<3;j++) jp[j] = bm->jp[j];
  54. for (j=0;j<3;j++) kp[j] = bm->kp[j];
  55. // vectors describing the location of each voxel
  56. x = fvector(0,M-1);
  57. y = fvector(0,N-1);
  58. z = fvector(0,Q-1);
  59. // lookup table for vectors in polar coordinates
  60. phi = fvector(0,NPHI-1);
  61. theta = fvector(0,NTHETA-1);
  62. sinphi = fvector(0,NPHI-1);
  63. cosphi = fvector(0,NPHI-1);
  64. sintheta = fvector(0,NTHETA-1);
  65. costheta = fvector(0,NTHETA-1);
  66. //kernel with fewer elements for faster calculation
  67. //see defs.h
  68. KERNEL smallkern;
  69. smallkern.radial_boundary = (float *)malloc(kern->nradii*sizeof(float));
  70. smallkern.angular_boundary = (float *)malloc(kern->ntheta*sizeof(float));
  71. //small kernel dimensions
  72. smallkern.nradii = N_KERNEL_RADII; //same as original kernel
  73. smallkern.ntheta = NTHETA; //must divide evenly into N_KERNEL_ANGLES
  74. SAD = bm->SAD;
  75. for (i=0;i<N_KERNEL_CATEGORIES;i++)
  76. if ( (smallkern.matrix[i] =
  77. (float *) calloc(smallkern.ntheta*smallkern.nradii,sizeof(float))) == NULL)
  78. {
  79. sprintf(errstr,"Cannot allocate space for matrix %d\n",i);
  80. return(FAILURE);
  81. }
  82. if ( (smallkern.total_matrix =
  83. (float *) calloc(smallkern.ntheta*smallkern.nradii,sizeof(float))) == NULL)
  84. {
  85. sprintf(errstr,"Cannot allocate space for total matrix\n");
  86. return(FAILURE);
  87. }
  88. //set up boundaries
  89. for (i=0;i<smallkern.ntheta;i++)
  90. smallkern.angular_boundary[i] = ( (float) i + 1) * (float)180.0/(float) smallkern.ntheta;
  91. for (i=0;i<smallkern.nradii;i++)
  92. smallkern.radial_boundary[i] = kern->radial_boundary[i];
  93. //initialise
  94. for (i=0;i<smallkern.nradii;i++)
  95. for (j=0;j<smallkern.ntheta;j++)
  96. {
  97. KERNEL_TOTAL_VALUE(&smallkern,i,j) = (float)0.0;
  98. for (l=0;l<N_KERNEL_CATEGORIES;l++)
  99. KERNEL_VALUE(&smallkern,l,i,j) = (float)0.0;
  100. }
  101. //create kernel values
  102. for (i=0;i<smallkern.nradii;i++)
  103. for (j=0;j<smallkern.ntheta;j++)
  104. {
  105. //first angular index in original kernel for this element
  106. baseindex = j*N_KERNEL_ANGLES/NTHETA;
  107. //for each category, sum values from original kernel
  108. for (l=0;l<N_KERNEL_CATEGORIES;l++)
  109. for (k=0;k<N_KERNEL_ANGLES/NTHETA;k++)
  110. KERNEL_VALUE(&smallkern,l,i,j) += KERNEL_VALUE(kern,l,i,baseindex+k);
  111. //and for total kernel
  112. for (k=0;k<N_KERNEL_ANGLES/NTHETA;k++)
  113. KERNEL_TOTAL_VALUE(&smallkern,i,j) += KERNEL_TOTAL_VALUE(kern,i,baseindex+k);
  114. }
  115. //Make cumulative kernel (with radius)
  116. //this is what is used for the dose calculation
  117. for (p=0;p<smallkern.ntheta;p++)
  118. for (r=0;r<smallkern.nradii;r++)
  119. {
  120. for (i=0;i<N_KERNEL_CATEGORIES;i++)
  121. if (r > 0)
  122. KERNEL_VALUE(&smallkern,i,r,p)
  123. = KERNEL_VALUE(&smallkern,i,r-1,p) + KERNEL_VALUE(&smallkern,i,r,p);
  124. if (r > 0)
  125. KERNEL_TOTAL_VALUE(&smallkern,r,p)
  126. = KERNEL_TOTAL_VALUE(&smallkern,r-1,p) + KERNEL_TOTAL_VALUE(&smallkern,r,p);
  127. }
  128. // fill the coordinate vectors
  129. for (i=0;i<M;i++) x[i] = density->start.x + (float)i*dx;
  130. for (j=0;j<N;j++) y[j] = density->start.y + (float)j*dy;
  131. for (k=0;k<Q;k++) z[k] = density->start.z + (float)k*dz;
  132. // fill in the polar coordinates vectors
  133. for (q=0;q<NPHI;q++)
  134. {
  135. phi[q] = ((float)q + (float)0.5)*(float)2.0*(float)PI/(float)NPHI;
  136. sinphi[q] = (float)sin(phi[q]);
  137. cosphi[q] = (float)cos(phi[q]);
  138. }
  139. // Theta is subtracted from PI is because direction of travel along kernel ray is actually opposite of
  140. // direction along which energy is radiating, so need to use a source voxel direction that
  141. // is reflected about horizontal. This can be thought of as the kernel inversion line.
  142. for (p=0;p<smallkern.ntheta;p++)
  143. if (p == 0)
  144. {
  145. theta[p] = (float)0.5*smallkern.angular_boundary[0]*(float)PI/(float)180.0;
  146. sintheta[p] = (float)sin((float)PI - theta[p]);
  147. costheta[p] = (float)cos((float)PI - theta[p]);
  148. }
  149. else
  150. {
  151. theta[p] = (float)0.5*(smallkern.angular_boundary[p-1] + smallkern.angular_boundary[p])*(float)PI/(float)180.0;
  152. sintheta[p] = (float)sin((float)PI - theta[p]);
  153. costheta[p] = (float)cos((float)PI - theta[p]);
  154. }
  155. // store the sines and cosines in a lookup table
  156. // the step size for the convolution integration is the smallest voxel side length
  157. if (dx <= dy && dx <= dz)
  158. delr = (float)2.0*dx;
  159. else if (dy <= dx && dy <= dz)
  160. delr = (float)2.0*dy;
  161. else
  162. delr = (float)2.0*dz;
  163. //calculate dose at each point
  164. //done from deposition (catcher's) point of view
  165. for (k=0;k<Q; k++)
  166. for (j=0;j<N; j++)
  167. for (i=0;i<M; i++)
  168. if (GRID_VALUE(dose_mask,i,j,k) > 0) // only calculate dose inside dose mask
  169. {
  170. // do the integral for the point in the ROI
  171. for (p=0;p<smallkern.ntheta;p++) //polar
  172. for (q=0;q<NPHI;q++) //azimuthal
  173. {
  174. //initialise position of current voxel
  175. current_x = x[i];
  176. current_y = y[j];
  177. current_z = z[k];
  178. //initialise effective radius along kernel direction
  179. r_eff = 0.0;
  180. //initialise cumulative kernel value for this direction
  181. last_cumval = 0.0;
  182. //Using reciprocity technique where dose at point A due to point B
  183. //is dose at point B due to point A
  184. //x ,y, z increments along ray
  185. del_x = delr*(ip[0]*cosphi[q]*sintheta[p] + jp[0]*sinphi[q]*sintheta[p]
  186. + kp[0]*costheta[p]);
  187. del_y = delr*(ip[1]*cosphi[q]*sintheta[p] + jp[1]*sinphi[q]*sintheta[p]
  188. + kp[1]*costheta[p]);
  189. del_z = delr*(ip[2]*cosphi[q]*sintheta[p] + jp[2]*sinphi[q]*sintheta[p]
  190. + kp[2]*costheta[p]);
  191. //initialise physical radius
  192. r = 0;
  193. do
  194. {
  195. //interaction point is at mid-point of curent increment
  196. inter_x = current_x + (float)0.5*del_x;
  197. inter_y = current_y + (float)0.5*del_y;
  198. inter_z = current_z + (float)0.5*del_z;
  199. //voxel containing interaction point
  200. inter_i = (int) ((inter_x - density->start.x)/dx);
  201. inter_j = (int) ((inter_y - density->start.y)/dy);
  202. inter_k = (int) ((inter_z - density->start.z)/dz);
  203. // stop the integral if interaction point is outside the dose calculation limits
  204. if ( (inter_i < 0) || (inter_i + 1 >= M)
  205. || (inter_j < 0) || (inter_j + 1 >= N)
  206. || (inter_k < 0) || (inter_k + 1 >= Q)
  207. || (GRID_VALUE(dose_mask,inter_i,inter_j,inter_k) <= 0.0))
  208. break;
  209. // Position of the end of the increment. Interaction point is at the
  210. // midpoint.
  211. current_x += del_x;
  212. current_y += del_y;
  213. current_z += del_z;
  214. //effective distance increment
  215. delr_eff = delr*GRID_VALUE(density,inter_i,inter_j,inter_k);
  216. //effective radius of interaction point
  217. inter_r_eff = r_eff + (float)0.5*delr_eff;
  218. r_eff += delr_eff;
  219. // trilinear interpolation method of the terma contribution, f
  220. // relative differences between the interaction point and the lower voxel bound
  221. t = (inter_x - x[inter_i])/dx;
  222. u = (inter_y - y[inter_j])/dy;
  223. v = (inter_z - z[inter_k])/dz;
  224. f = GRID_VALUE(terma,inter_i,inter_j,inter_k)*(one-t)*(one-u)*(one-v)
  225. + GRID_VALUE(terma,inter_i,inter_j,inter_k+1)*(one-t)*(one-u)*v
  226. + GRID_VALUE(terma,inter_i,inter_j+1,inter_k+1)*(one-t)*u*v
  227. + GRID_VALUE(terma,inter_i+1,inter_j+1,inter_k+1)*t*u*v
  228. + GRID_VALUE(terma,inter_i,inter_j+1,inter_k)*(one-t)*u*(one-v)
  229. + GRID_VALUE(terma,inter_i+1,inter_j+1,inter_k)*t*u*(one-v)
  230. + GRID_VALUE(terma,inter_i+1,inter_j,inter_k+1)*t*(one-u)*v
  231. + GRID_VALUE(terma,inter_i+1,inter_j,inter_k)*t*(one-u)*(one-v);
  232. /*
  233. // interpolate density at the interaction point
  234. rho = GRID_VALUE(density,inter_i,inter_j,inter_k)*(one-t)*(one-u)*(one-v)
  235. + GRID_VALUE(density,inter_i,inter_j,inter_k+1)*(one-t)*(one-u)*v
  236. + GRID_VALUE(density,inter_i,inter_j+1,inter_k+1)*(one-t)*u*v
  237. + GRID_VALUE(density,inter_i+1,inter_j+1,inter_k+1)*t*u*v
  238. + GRID_VALUE(density,inter_i,inter_j+1,inter_k)*(one-t)*u*(one-v)
  239. + GRID_VALUE(density,inter_i+1,inter_j+1,inter_k)*t*u*(one-v)
  240. + GRID_VALUE(density,inter_i+1,inter_j,inter_k+1)*t*(one-u)*v
  241. + GRID_VALUE(density,inter_i+1,inter_j,inter_k)*t*(one-u)*(one-v); */
  242. // perform kernel lookup for r_eff, r_index is the kernel index of the first
  243. // bin boundary below the effective radius of the voxel
  244. r_index = binSearch(smallkern.radial_boundary,inter_r_eff,smallkern.nradii);
  245. // interpolate to obtain the effective cumulative kernel value
  246. if (r_index == -1) // radius is between zero and the first bin boundary
  247. {
  248. // fractional difference between inter_r_eff and the first bin boundary
  249. t = inter_r_eff/smallkern.radial_boundary[0];
  250. cumval = (1-t)*KERNEL_TOTAL_VALUE(&smallkern,0,p);
  251. }
  252. else if (r_index >= smallkern.nradii-1) // overshot the kernel bin boundaries
  253. {
  254. cumval = KERNEL_TOTAL_VALUE(&smallkern,smallkern.nradii-1,p);
  255. }
  256. else // inter_r_eff is between the first upper bin boundary and the last
  257. {
  258. t = (inter_r_eff - smallkern.radial_boundary[r_index])
  259. /(smallkern.radial_boundary[r_index + 1] - smallkern.radial_boundary[r_index]);
  260. cumval = (1-t)*KERNEL_TOTAL_VALUE(&smallkern,r_index,p)
  261. + t*KERNEL_TOTAL_VALUE(&smallkern,r_index+1,p);
  262. }
  263. kval = cumval - last_cumval;
  264. last_cumval = cumval;
  265. // Kernel value to use is current increment in cumulative value
  266. // Note that this is the fractional dose deposited at i,j,k due to
  267. // terma in an effective increment (increment*density) along the kernel ray at the
  268. // interaction point. The value comes from the fractional dose deposited in an
  269. // effective increment along the ray at i,j,k due to terma at the current
  270. // interaction point.
  271. //Increment dose value.
  272. //Note that to include the effect of density at i,j,k on ENERGY deposited at i,j,k,
  273. //there should be a density term, but this cancels on conversion of energy to dose as indicated below
  274. // if (GRID_VALUE(terma,inter_i,inter_j,inter_k) > 0.001)
  275. GRID_VALUE(dose,i,j,k) += f*kval;
  276. r++;
  277. }
  278. while (r<10000);
  279. } //p,q
  280. GRID_VALUE(dose,i,j,k)/= NPHI;
  281. }
  282. //Inverse square correction to dose
  283. //This works better than applying the inverse square correction to terma
  284. //See Papanikolaou and Mackie 1993
  285. for (k=0;k<Q;k++)
  286. for (j=0;j<N;j++)
  287. for (i=0;i<M;i++)
  288. if (GRID_VALUE(dose,i,j,k) > 0.0)
  289. {
  290. // squared difference between the source and the voxel
  291. length = (float)pow(x[i] - bm->y_vec[0],2.0f) + (float)pow(y[j] - bm->y_vec[1],2.0f)
  292. + (float)pow(z[k] - bm->y_vec[2],2.0f);
  293. if (length > 0.0)
  294. GRID_VALUE(dose,i,j,k) *= SAD*SAD/length;
  295. }
  296. // free vectors
  297. free_fvector(ip,0,2);
  298. free_fvector(jp,0,2);
  299. free_fvector(kp,0,2);
  300. free_fvector(x,0,M-1);
  301. free_fvector(y,0,N-1);
  302. free_fvector(z,0,Q-1);
  303. free(smallkern.angular_boundary);
  304. free(smallkern.radial_boundary);
  305. // free(poly_kernel.total_matrix);
  306. for (j=0;j<N_KERNEL_CATEGORIES;j++)
  307. free(smallkern.matrix[j]);
  308. return(SUCCESS);
  309. }