get_beam_lets.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. function [beamlets, beamlets_joined, numBeamlet, numBeam, beam_i_list] = get_beam_lets(Geometry, patient_dir)
  2. % this function loads and returns beamlets and joined beams
  3. beamlet_batch_filename = [patient_dir '\' 'batch_dose.bin'];
  4. beamlet_cell_array = read_ryan_beamlets(beamlet_batch_filename, 'ryan');
  5. numVox = numel(Geometry.data);
  6. numBeamlet = size(beamlet_cell_array,2);
  7. beamlets = get_beamlets(beamlet_cell_array, numVox);
  8. % join beamlets into beams
  9. load([patient_dir '\all_beams.mat'])
  10. beamletOrigin=[0 0 0];
  11. beam_i=0;
  12. beam_i_list=[];
  13. for beamlet_i = 1:numel(all_beams)
  14. newBeamletOrigin = all_beams{beamlet_i}.ip;
  15. if any(newBeamletOrigin ~= beamletOrigin)
  16. beam_i = beam_i+1;
  17. beamletOrigin = newBeamletOrigin;
  18. end
  19. beam_i_list=[beam_i_list, beam_i];
  20. end
  21. beamlets_joined=[];
  22. target_joined=[];
  23. wbar2 = waitbar(0, 'merging beamlets into beams');
  24. numBeam=numel(unique(beam_i_list));
  25. for beam_i = unique(beam_i_list)
  26. beam_full = sum(beamlets(:,beam_i_list == beam_i), 2);
  27. beamlets_joined(:,end+1) = beam_full;
  28. waitbar(beam_i/numBeam, wbar2)
  29. end
  30. close(wbar2)
  31. end
  32. % ---- GET BEAMLETS ----
  33. function beamlets = get_beamlets(beamlet_cell_array, numVox);
  34. wbar1 = waitbar(0, 'Creating beamlet array');
  35. numBeam = size(beamlet_cell_array,2);
  36. batchSize=100;
  37. beamlets = sparse(0, 0);
  38. for beam_i=1:numBeam
  39. % for each beam define how much dose it delivers on each voxel
  40. idx=beamlet_cell_array{1, beam_i}.non_zero_indices;
  41. % break the beamlets into multiple batches
  42. if rem(beam_i, batchSize)==1;
  43. beamlet_batch = sparse(numVox, batchSize);
  44. beam_i_temp=1;
  45. end
  46. beamlet_batch(idx, beam_i_temp) = 1000*beamlet_cell_array{1, beam_i}.non_zero_values;
  47. waitbar(beam_i/numBeam, wbar1, ['Adding beamlet array: #', num2str(beam_i)])
  48. % add the batch to full set when filled
  49. if rem(beam_i, batchSize)==0;
  50. beamlets =[beamlets, beamlet_batch];
  51. end
  52. % crop and add the batch to full set when completed
  53. if beam_i==numBeam;
  54. beamlet_batch=beamlet_batch(:, 1:beam_i_temp);
  55. beamlets =[beamlets, beamlet_batch];
  56. end
  57. beam_i_temp=beam_i_temp+1;
  58. end
  59. close(wbar1)
  60. end
  61. function show_joint_beamlets(beamlets, IMGsize, Beam_list)
  62. % this function overlays and plots multiple beamlets. The goal is to
  63. % check whether some beamlets are part of the same beam manually.
  64. beam=sum(beamlets(:, Beam_list), 2);
  65. beamImg=reshape(full(beam), IMGsize);
  66. orthoslice(beamImg)
  67. end