get_beamlets.m 3.5 KB

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