get_beamlets_fullRO.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. function [beamlets, numBeamlet] = get_beamlets_fullRO(imgDim, patient_dir, optGoal)
  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(imgDim);
  9. for i = 1:numel(optGoal)
  10. tabula(optGoal{i}.ROI_idx) = 1;
  11. end
  12. ROI_idxList = find(tabula>0);
  13. %% add idx lists for each goal!
  14. beamlet_batch_filename = [patient_dir '\' 'batch_dose.bin'];
  15. % beamlet_cell_array = read_ryan_beamlets(beamlet_batch_filename, 'ryan');
  16. beamlet_cell_array = read_ryan_beamlets(beamlet_batch_filename, 'peter', ROI_idxList);
  17. numVox = prod(imgDim);
  18. numBeamlet = size(beamlet_cell_array,2);
  19. batchSize = 200;
  20. beamlets = get_beamlets2(beamlet_cell_array, numVox, batchSize, ROI_idxList);
  21. % beamlets = beamlet_cell_array;
  22. end
  23. % ---- GET BEAMLETS ----
  24. function beamlets = get_beamlets2(beamlet_cell_array, numVox, batchSize, ROI_idxList);
  25. wbar1 = waitbar(0, 'Creating beamlet array');
  26. numBeam = size(beamlet_cell_array,2);
  27. beamlets = sparse(0, 0);
  28. for beam_i=1:numBeam
  29. % for each beam define how much dose it delivers on each voxel
  30. idx=beamlet_cell_array{1, beam_i}.non_zero_indices;
  31. [ROI_idxIntersect, ia, ~] = intersect (idx, ROI_idxList);
  32. % if ~isequal(ROI_idxIntersect,idx)
  33. % warning('this')
  34. % end
  35. % ROI_idxIntersect = beamlet_cell_array{1, beam_i}.non_zero_indices;
  36. if isempty(ROI_idxIntersect)
  37. warning(['Beamlet ' num2str(beam_i) ' is empty!'])
  38. end
  39. % break the beamlets into multiple batches
  40. if rem(beam_i, batchSize)==1;
  41. beamlet_batch = sparse(numVox, batchSize);
  42. beam_i_temp=1;
  43. end
  44. % beamlet_batch(idx, beam_i_temp) = 1000*beamlet_cell_array{1, beam_i}.non_zero_values;
  45. beamlet_batch(ROI_idxIntersect, beam_i_temp) = beamlet_cell_array{1, beam_i}.non_zero_values(ia);
  46. waitbar(beam_i/numBeam, wbar1, ['Adding beamlet array: #', num2str(beam_i)])
  47. % add the batch to full set when filled
  48. if rem(beam_i, batchSize)==0;
  49. beamlets =[beamlets, beamlet_batch];
  50. end
  51. % crop and add the batch to full set when completed
  52. if beam_i==numBeam;
  53. beamlet_batch=beamlet_batch(:, 1:beam_i_temp);
  54. beamlets =[beamlets, beamlet_batch];
  55. end
  56. beam_i_temp=beam_i_temp+1;
  57. end
  58. close(wbar1)
  59. end
  60. function show_joint_beamlets(beamlets, IMGsize, Beam_list)
  61. % this function overlays and plots multiple beamlets. The goal is to
  62. % check whether some beamlets are part of the same beam manually.
  63. beam=sum(beamlets(:, Beam_list), 2);
  64. beamImg=reshape(full(beam), IMGsize);
  65. orthoslice(beamImg)
  66. end