RDXTPS_dosecalcSetup.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. function [dosecalcSetup patient_dir] = RDXTPS_dosecalcSetup(dosecalcSetup, patient_dir, Geometry)
  2. if nargin < 1
  3. dosecalcSetup = [];
  4. elseif ~isempty(dosecalcSetup)
  5. inpAns = {...
  6. dosecalcSetup.treat_mode, ...
  7. num2str(dosecalcSetup.det_margin), ...
  8. num2str(dosecalcSetup.spread_parameters), ...
  9. num2str(dosecalcSetup.spacing_parameters(:)'), ...
  10. num2str(dosecalcSetup.aperture_size), ...
  11. num2str(dosecalcSetup.beam_angles)};
  12. end
  13. if nargin < 2
  14. patient_dir = [];
  15. end
  16. %%
  17. prompt = {...
  18. sprintf('All unit in cm\n\nTreatment mode (SS or DET):'), ...
  19. sprintf('DET margin (default 0.1):'), ...
  20. sprintf('Beam width (1 Gaussian sigma), typically 0.3 to 1.0:'), ...
  21. sprintf('Spot spacing, typically <= 1 sigma:\n(3 numbers seperated by space, the last one along beam direction)'), ...
  22. sprintf('Cutoff distance (aperture size) of the dose calculation grid:'), ...
  23. sprintf('Gantry angles (any scalar or vector in MATLAB syntax):\ne.g. -30 0 30 or -180:10:180\n0 is PA') ...
  24. sprintf('Couch angles (any scalar or vector in MATLAB syntax):\ne.g. -30 0 30 or -180:10:180\n0 for coplanar beam') ...
  25. };
  26. defAns = {...
  27. 'SS', ...
  28. '0.1', ...
  29. '0.6', ...
  30. '0.5 0.5 0.3', ...
  31. '4', ...
  32. '-90 90', ...
  33. '0 0'};
  34. %% Prompt input dialog
  35. if isempty(dosecalcSetup)
  36. answer = inputdlg(prompt, 'Dose calculation parameters', 1, defAns);
  37. else
  38. answer = inputdlg(prompt, 'Dose calculation parameters', 1, inpAns);
  39. end
  40. %% TODO if hit cancel
  41. %% Convert answer to struct
  42. dosecalcSetup.treat_mode = answer{1};
  43. dosecalcSetup.det_margin = str2double(answer{2});
  44. dosecalcSetup.spread_parameters = str2double(answer{3});
  45. dosecalcSetup.spacing_parameters = sscanf(answer{4}, '%f%f%f');
  46. dosecalcSetup.aperture_size = str2double(answer{5});
  47. dosecalcSetup.beam_angles = eval(['[' answer{6} ']']);
  48. dosecalcSetup.couch_angles = eval(['[' answer{7} ']']);
  49. %% Check input data integrity
  50. dosecalcSetup.treat_mode = strtrim(dosecalcSetup.treat_mode);
  51. if ~strcmpi(dosecalcSetup.treat_mode, 'SS') && ~strcmpi(dosecalcSetup.treat_mode, 'DET')
  52. msgbox('Unknow treatment mode specified');
  53. end
  54. if isempty(dosecalcSetup.det_margin) || dosecalcSetup.det_margin < 0 || dosecalcSetup.det_margin > 10
  55. msgbox('DET margin out of range');
  56. end
  57. if isempty(dosecalcSetup.spread_parameters) || dosecalcSetup.spread_parameters < 0 || dosecalcSetup.spread_parameters > 10
  58. msgbox('Beam width out of range');
  59. end
  60. if isempty(dosecalcSetup.spacing_parameters) || any(dosecalcSetup.spacing_parameters < 0) || any(dosecalcSetup.spacing_parameters > 10)
  61. msgbox('Spot spacing out of range');
  62. end
  63. if isempty(dosecalcSetup.aperture_size) || dosecalcSetup.aperture_size < 0 || dosecalcSetup.aperture_size > 100
  64. msgbox('Aperture out of range');
  65. end
  66. if isempty(dosecalcSetup.beam_angles) || any(~isnumeric(dosecalcSetup.beam_angles))
  67. msgbox('Gantry angle out of range');
  68. end
  69. %% Choose output directory
  70. if isempty(patient_dir)
  71. patient_dir = uifile('getdir', 'Select the patient directory');
  72. end
  73. try
  74. load(fullfile(patient_dir, 'matlab_files', 'Geometry.mat'));
  75. catch
  76. msgbox('Error loading Geometry file');
  77. return;
  78. end
  79. %% prepare variables for input file
  80. geometry_rhomw_filename = fullfile(patient_dir, 'geometry_files', 'rhomw.bin');
  81. geometry_Smw_filename = fullfile(patient_dir, 'geometry_files', 'Smw.bin');
  82. geometry_tumormask_filename = fullfile(patient_dir, 'geometry_files', 'target_mask.bin');
  83. geometry_Fmw2_filename = fullfile(patient_dir, 'geometry_files', 'Fmw2.bin');
  84. batch_dose = fullfile(patient_dir, 'batch_dose.bin');
  85. beam_spec = fullfile(patient_dir, 'beam_spec_DAVE.txt');
  86. % size of the CT data grid
  87. [M,N,Q] = size(Geometry.rhomw);
  88. % voxel size
  89. dx = double(Geometry.voxel_size(1));
  90. dy = double(Geometry.voxel_size(2));
  91. dz = double(Geometry.voxel_size(3));
  92. % grid start location
  93. x0 = double(Geometry.start(1));
  94. y0 = double(Geometry.start(2));
  95. z0 = double(Geometry.start(3));
  96. % tumor zstart, zend. NOT used any more
  97. tumor_zstart = 0;
  98. tumor_zend = 1;
  99. read_beamspec = 0;
  100. write_beamspec = 1;
  101. use_scale2D = 1;
  102. Mxp = 512;
  103. Nyp = 512;
  104. SAD = 100;
  105. num_energies = 576;
  106. pencil_beam_filenames_file = 'pencil_beam_files/pencil_beam_filenames.txt';
  107. pencil_beam_path = 'pencil_beam_files';
  108. sig_xp = dosecalcSetup.spread_parameters;
  109. sig_yp = dosecalcSetup.spread_parameters;
  110. xSpace = dosecalcSetup.spacing_parameters(1);
  111. ySpace = dosecalcSetup.spacing_parameters(2);
  112. zSpace = dosecalcSetup.spacing_parameters(3);
  113. del_xp = dosecalcSetup.aperture_size;
  114. del_yp = dosecalcSetup.aperture_size;
  115. num_angles = numel(dosecalcSetup.beam_angles);
  116. phi = dosecalcSetup.beam_angles;
  117. couch_phi = dosecalcSetup.couch_angles;
  118. %% write input file
  119. fprintf('Generating input file.\n');
  120. fid = fopen(fullfile(patient_dir, 'dosecalc_input.txt'), 'w');
  121. fprintf(fid,'#######################################################################\n');
  122. fprintf(fid,['## Proton dose calculator input file for: ' patient_dir '\n']);
  123. fprintf(fid,'#######################################################################\n\n');
  124. fprintf(fid,'# Text entries not preceeded by comments must not be changed!!\n\n');
  125. fprintf(fid,'# GEOMETRY DATA\n\n');
  126. fprintf(fid,'# CT density grid file\n');
  127. fprintf(fid,'rhomw_grid_file\n');
  128. fprintf(fid,'%s\n\n',geometry_rhomw_filename);
  129. fprintf(fid,'# Stopping power ratio grid file\n');
  130. fprintf(fid,'Smw_grid_file\n');
  131. fprintf(fid,'%s\n\n',geometry_Smw_filename);
  132. fprintf(fid,'# Tumor mask file\n');
  133. fprintf(fid,'mask_file\n');
  134. fprintf(fid,'%s\n\n',geometry_tumormask_filename);
  135. fprintf(fid,'# Grid containing 2D scaling F^2 factors\n');
  136. fprintf(fid,'Fmw2_grid_file\n');
  137. fprintf(fid,'%s\n\n',geometry_Fmw2_filename);
  138. fprintf(fid,'# Output dose file\n');
  139. fprintf(fid,'batch_dose_file\n');
  140. fprintf(fid,'%s\n\n',batch_dose);
  141. fprintf(fid,'# Beam specification file\n');
  142. fprintf(fid,'beam_spec_file\n');
  143. fprintf(fid,'%s\n\n',beam_spec);
  144. fprintf(fid,'# Data grid dimensions (Xdim,Ydim,Zdim)\n');
  145. fprintf(fid,'grid_dimensions\n');
  146. fprintf(fid,'%d %d %d\n\n',M,N,Q);
  147. fprintf(fid,'# Voxel dimensions (dx,dy,dz) [cm]\n');
  148. fprintf(fid,'voxel_dimensions\n');
  149. fprintf(fid,'%f %f %f\n\n',dx,dy,dz);
  150. fprintf(fid,'# Grid start coordinates (shifted) (x0,y0,z0) [cm]\n');
  151. fprintf(fid,'start_coordinates\n');
  152. fprintf(fid,'%f %f %f\n\n',x0,y0,z0);
  153. fprintf(fid,'# Tumor Z coordinates (tumor_zstart, tumor_zend) [cm]\n');
  154. fprintf(fid,'tumor_coordinates\n');
  155. fprintf(fid,'%f %f\n\n',tumor_zstart,tumor_zend);
  156. fprintf(fid,'# USER DEFINED BEAM DATA\n\n');
  157. fprintf(fid,'# Calculation mode\n');
  158. fprintf(fid,'treat_mode\n');
  159. fprintf(fid,'%s\n\n', dosecalcSetup.treat_mode);
  160. fprintf(fid,'# DET margin\n');
  161. fprintf(fid,'det_margin\n');
  162. fprintf(fid,'%f\n\n', dosecalcSetup.det_margin);
  163. fprintf(fid,'# Load beam specification file [1 for yes, 0 for no]\n');
  164. fprintf(fid,'read_beam_spec_file\n');
  165. fprintf(fid,'%d\n\n',read_beamspec);
  166. fprintf(fid,'# Write beam specification file [1 for yes, 0 for no]\n');
  167. fprintf(fid,'write_beam_spec_file\n');
  168. fprintf(fid,'%d\n\n',write_beamspec);
  169. fprintf(fid,'# Use 2D scaling method [1 for yes, 0 for no]\n');
  170. fprintf(fid,'use_2D_scaling\n');
  171. fprintf(fid,'%d\n\n',use_scale2D);
  172. fprintf(fid,'# Gaussian spread parameters (sig_xp,sig_yp) [cm]\n');
  173. fprintf(fid,'spread_parameters\n');
  174. fprintf(fid,'%f %f\n\n',sig_xp,sig_yp);
  175. fprintf(fid,'# Gaussian spacing parameters (xSpace,ySpace,zSpace) [cm]\n');
  176. fprintf(fid,'spacing_parameters\n');
  177. fprintf(fid,'%f %f %f\n\n',xSpace,ySpace,zSpace);
  178. fprintf(fid,'# Rectangular aperture size in beams eye view (del_xp,del_yp) [cm]\n');
  179. fprintf(fid,'aperture_size\n');
  180. fprintf(fid,'%f %f\n\n',del_xp,del_yp);
  181. fprintf(fid,'# Number of beam angles\n');
  182. fprintf(fid,'num_beam_angles\n');
  183. fprintf(fid,'%d\n\n',num_angles);
  184. fprintf(fid,'# Beam angles for calculation [degrees]\n');
  185. fprintf(fid,'gantry_angles\n');
  186. fprintf(fid,'%f\n',phi);
  187. fprintf(fid,'\n');
  188. fprintf(fid,'# Couch angles for calculation [degrees]\n');
  189. fprintf(fid,'couch_angles\n');
  190. fprintf(fid,'%f\n',couch_phi);
  191. fprintf(fid,'\n');
  192. fprintf(fid,'# Number of beamlets filling each beam angle aperture (Mxp,Nyp)\n');
  193. fprintf(fid,'number_beamlets\n');
  194. fprintf(fid,'%d %d\n\n',Mxp,Nyp);
  195. fprintf(fid,'# Source to axis distance (SAD) [cm]\n');
  196. fprintf(fid,'SAD\n');
  197. fprintf(fid,'%f\n\n',SAD);
  198. fprintf(fid,'# PENCIL BEAM DATA\n\n');
  199. fprintf(fid,'# Number of pencil beam energies\n');
  200. fprintf(fid,'number_energies\n');
  201. fprintf(fid,'%d\n\n',num_energies);
  202. fprintf(fid,'# Pencil beam filenames file\n');
  203. fprintf(fid,'pencil_beam_filenames\n');
  204. fprintf(fid,'%s\n\n',pencil_beam_filenames_file);
  205. fprintf(fid,'# Location of pencil beam files\n');
  206. fprintf(fid,'pencil_beam_path\n');
  207. fprintf(fid,'%s\n',pencil_beam_path);
  208. fclose(fid);
  209. % save data to matlab structure
  210. save(fullfile(patient_dir, 'matlab_files', 'dosecalcSetup.mat'), 'dosecalcSetup');