get_beamlets.m 3.3 KB

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