calc_dose.cpp 12 KB

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