get_beam_lets.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. wbar2 = waitbar(0, 'merging beamlets into beams');
  22. numBeam=numel(unique(beam_i_list));
  23. beamlets_joined=sparse(size(beamlets,1), numBeam);
  24. for beam_i = 1:numel(unique(beam_i_list))
  25. beam_full = sum(beamlets(:,beam_i_list == beam_i), 2);
  26. beamlets_joined(:,beam_i) = beam_full;
  27. waitbar(beam_i/numBeam, wbar2)
  28. end
  29. close(wbar2)
  30. end
  31. % ---- GET BEAMLETS ----
  32. function beamlets = get_beamlets(beamlet_cell_array, numVox);
  33. wbar1 = waitbar(0, 'Creating beamlet array');
  34. numBeam = size(beamlet_cell_array,2);
  35. batchSize=400;
  36. beamlets = sparse(0, 0);
  37. for beam_i=1:numBeam
  38. % for each beam define how much dose it delivers on each voxel
  39. idx=beamlet_cell_array{1, beam_i}.non_zero_indices;
  40. % break the beamlets into multiple batches
  41. if rem(beam_i, batchSize)==1;
  42. beamlet_batch = sparse(numVox, batchSize);
  43. beam_i_temp=1;
  44. end
  45. beamlet_batch(idx, beam_i_temp) = 1000*beamlet_cell_array{1, beam_i}.non_zero_values;
  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