get_beamlets.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. function [beamlets, numBeamlet] = get_beamlets(Geometry, patient_dir, OptGoals)
  2. % this function loads and returns beamlets and joined beams
  3. % ROI_idxList = [];
  4. % for i = 1:numel(OptGoals.data)
  5. % ROI_idxList = [ROI_idxList; OptGoals.data{i}.ROI_idx];
  6. % end
  7. % ROI_idxList = unique(ROI_idxList);
  8. tabula = zeros(size(Geometry.data));
  9. for i = 1:numel(OptGoals.data)
  10. tabula(OptGoals.data{i}.ROI_idx) = 1;
  11. end
  12. ROI_idxList = find(tabula>0);
  13. for i_sss = OptGoals.sss_scene_list
  14. tabula2 = imtranslate(tabula, i_sss{1});
  15. ROI_idxList2 = find(tabula2>0);
  16. ROI_idxList = [ROI_idxList; ROI_idxList2];
  17. end
  18. ROI_idxList = unique(ROI_idxList);
  19. %% add idx lists for each goal!
  20. beamlet_batch_filename = [patient_dir '\' 'batch_dose.bin'];
  21. % beamlet_cell_array = read_ryan_beamlets(beamlet_batch_filename, 'ryan');
  22. beamlet_cell_array = read_ryan_beamlets(beamlet_batch_filename, 'peter', ROI_idxList);
  23. numVox = numel(Geometry.data);
  24. numBeamlet = size(beamlet_cell_array,2);
  25. batchSize = 200;
  26. beamlets = get_beamlets2(beamlet_cell_array, numVox, batchSize, ROI_idxList);
  27. % beamlets = beamlet_cell_array;
  28. % --- join beamlets into beams
  29. % load([patient_dir '\all_beams.mat'])
  30. % beamletOrigin=[0 0 0];
  31. % beam_i=0;
  32. % beam_i_list=[];
  33. % for beamlet_i = 1:numel(all_beams)
  34. % newBeamletOrigin = all_beams{beamlet_i}.ip;
  35. % if any(newBeamletOrigin ~= beamletOrigin)
  36. % beam_i = beam_i+1;
  37. % beamletOrigin = newBeamletOrigin;
  38. % end
  39. % beam_i_list=[beam_i_list, beam_i];
  40. % end
  41. %
  42. %
  43. % wbar2 = waitbar(0, 'merging beamlets into beams');
  44. % numBeam=numel(unique(beam_i_list));
  45. % beamlets_joined=sparse(size(beamlets,1), numBeam);
  46. % for beam_i = 1:numel(unique(beam_i_list))
  47. % beam_full = sum(beamlets(:,beam_i_list == beam_i), 2);
  48. % beamlets_joined(:,beam_i) = beam_full;
  49. % waitbar(beam_i/numBeam, wbar2)
  50. % end
  51. %
  52. % close(wbar2)
  53. end
  54. % ---- GET BEAMLETS ----
  55. function beamlets = get_beamlets2(beamlet_cell_array, numVox, batchSize, ROI_idxList);
  56. wbar1 = waitbar(0, 'Creating beamlet array');
  57. numBeam = size(beamlet_cell_array,2);
  58. beamlets = sparse(0, 0);
  59. for beam_i=1:numBeam
  60. % for each beam define how much dose it delivers on each voxel
  61. idx=beamlet_cell_array{1, beam_i}.non_zero_indices;
  62. [ROI_idxIntersect, ia, ~] = intersect (idx, ROI_idxList);
  63. % if ~isequal(ROI_idxIntersect,idx)
  64. % warning('this')
  65. % end
  66. % ROI_idxIntersect = beamlet_cell_array{1, beam_i}.non_zero_indices;
  67. if isempty(ROI_idxIntersect)
  68. warning(['Beamlet ' num2str(beam_i) ' is empty!'])
  69. end
  70. % break the beamlets into multiple batches
  71. if rem(beam_i, batchSize)==1;
  72. beamlet_batch = sparse(numVox, batchSize);
  73. beam_i_temp=1;
  74. end
  75. % beamlet_batch(idx, beam_i_temp) = 1000*beamlet_cell_array{1, beam_i}.non_zero_values;
  76. beamlet_batch(ROI_idxIntersect, beam_i_temp) = beamlet_cell_array{1, beam_i}.non_zero_values(ia);
  77. waitbar(beam_i/numBeam, wbar1, ['Adding beamlet array: #', num2str(beam_i)])
  78. % add the batch to full set when filled
  79. if rem(beam_i, batchSize)==0;
  80. beamlets =[beamlets, beamlet_batch];
  81. end
  82. % crop and add the batch to full set when completed
  83. if beam_i==numBeam;
  84. beamlet_batch=beamlet_batch(:, 1:beam_i_temp);
  85. beamlets =[beamlets, beamlet_batch];
  86. end
  87. beam_i_temp=beam_i_temp+1;
  88. end
  89. close(wbar1)
  90. end
  91. function show_joint_beamlets(beamlets, IMGsize, Beam_list)
  92. % this function overlays and plots multiple beamlets. The goal is to
  93. % check whether some beamlets are part of the same beam manually.
  94. beam=sum(beamlets(:, Beam_list), 2);
  95. beamImg=reshape(full(beam), IMGsize);
  96. orthoslice(beamImg)
  97. end