read_ryan_beamlets.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. function [ results ] = read_ryan_beamlets ( filename, mode, weights )
  2. %READ_RYAN_BEAMLETS Read beamlets in Ryan's format into cell array
  3. %
  4. % Example:
  5. % beamlet_cell_array = read_ryan_beamlets (beamlet_batch_filename)
  6. % beamlet_cell_array = read_ryan_beamlets (beamlet_batch_filename, 'ryan')
  7. % full_dose_grid = read_ryan_beamlets (beamlet_batch_filename, 'ryan sum', weights)
  8. %
  9. % See also open_beamlet_batch.
  10. %
  11. % Copyleft 1999-, XMO.
  12. if nargin < 2
  13. mode = 'Ryan';
  14. end
  15. fid = fopen(filename,'r');
  16. if ( fid == -1 )
  17. error(['cannot open "' filename '"']);
  18. end
  19. Nbeamlets = fread(fid,1,'int');
  20. if Nbeamlets < 0 || Nbeamlets > 327680
  21. error([num2str(Nbeamlets) ' beamlets designated']);
  22. elseif Nbeamlets == 0
  23. if strcmpi(mode, 'ryan')
  24. results = cell(0);
  25. return;
  26. elseif strcmpi(mode, 'ryan sum')
  27. results = 0;
  28. return;
  29. end
  30. end
  31. switch lower(mode)
  32. case 'info'
  33. results = Nbeamlets;
  34. % This returns an empty cell array with all meta info
  35. fprintf('Number of beamlets = %8d\n', Nbeamlets);
  36. beamlets = cell(1,Nbeamlets);
  37. for k=1:Nbeamlets
  38. beamlet.num = fread(fid,1,'int');
  39. beamlet.x_count = fread(fid,1,'int');
  40. beamlet.y_count = fread(fid,1,'int');
  41. beamlet.z_count = fread(fid,1,'int');
  42. Nind = fread(fid,1,'int'); % number of non-zero indices & values
  43. % skip beamlet data (one int32 and one single array)
  44. fseek(fid, Nind * 2 * 4, 'cof');
  45. beamlets{k} = beamlet;
  46. end
  47. case {'ryan', 'ryan sum'}
  48. beamlets = cell(1,Nbeamlets);
  49. for k=1:Nbeamlets
  50. beamlet.num = fread(fid,1,'int');
  51. beamlet.x_count = fread(fid,1,'int');
  52. beamlet.y_count = fread(fid,1,'int');
  53. beamlet.z_count = fread(fid,1,'int');
  54. Nind = fread(fid,1,'int'); % number of non-zero values
  55. beamlet.non_zero_indices = int32(fread(fid,Nind,'int'));
  56. beamlet.non_zero_values = single(fread(fid,Nind,'float'));
  57. % add unity to the indices to convert them from C to Matlab style indexing:
  58. beamlet.non_zero_indices = beamlet.non_zero_indices + 1;
  59. beamlet.num = beamlet.num + 1;
  60. if any(beamlet.non_zero_indices < 0)
  61. error('index negative');
  62. end
  63. if any(beamlet.non_zero_indices > beamlet.x_count*beamlet.y_count*beamlet.z_count)
  64. error('index out of range');
  65. end
  66. if strcmpi(mode, 'ryan') % save all beamlets in cell array
  67. beamlets{k} = beamlet;
  68. elseif strcmpi(mode, 'ryan sum')
  69. if k == 1 % allocate dose matrix in the first loop
  70. dose = zeros(beamlet.x_count, beamlet.y_count, beamlet.z_count);
  71. end
  72. dose(beamlet.non_zero_indices) = dose(beamlet.non_zero_indices) + ...
  73. beamlet.non_zero_values * weights(beamlet.num);
  74. end
  75. results = beamlets;
  76. end
  77. if strcmpi(mode, 'ryan sum')
  78. % rename for return
  79. % TODO ambiguous output value
  80. if Nbeamlets > 0
  81. results = dose;
  82. else
  83. results = 0;
  84. end
  85. end
  86. case 'sparse'
  87. nzmax = 0;
  88. nBmax = 500;
  89. for k=1:nBmax%Nbeamlets
  90. % skip 4 integers
  91. fseek(fid, 4 * 4, 'cof');
  92. % get number of non-zero values
  93. Nind = fread(fid,1,'int');
  94. nzmax = nzmax + Nind;
  95. % skip beamlet data (one int32 and one single array)
  96. fseek(fid, Nind * 2 * 4, 'cof');
  97. end
  98. % close and open again
  99. % TODO this doesn't fit the normal workflow
  100. fclose(fid);
  101. fid = fopen(filename,'r');
  102. if ( fid == -1 )
  103. error(['cannot open "' filename '"']);
  104. end
  105. Nbeamlets = fread(fid,1,'int');
  106. % Return is num_voxels x num_beamlets matrix
  107. % i === row === 1 to num_voxels
  108. % j === col === 1 to num_beamlets
  109. S = sparse([], [], [], 256*256*158, nBmax, nzmax);
  110. idx = 1;
  111. xpg = xprogressbar();
  112. for k=1:nBmax%Nbeamlets
  113. xpg.update(k/Nbeamlets);
  114. beamlet.num = fread(fid,1,'int');
  115. beamlet.x_count = fread(fid,1,'int');
  116. beamlet.y_count = fread(fid,1,'int');
  117. beamlet.z_count = fread(fid,1,'int');
  118. Nind = fread(fid,1,'int'); % number of non-zero values
  119. beamlet.non_zero_indices = int32(fread(fid,Nind,'int'));
  120. beamlet.non_zero_values = single(fread(fid,Nind,'float'));
  121. % add unity to the indices to convert them from C to Matlab style indexing:
  122. beamlet.non_zero_indices = beamlet.non_zero_indices + 1;
  123. if any(beamlet.non_zero_indices < 0)
  124. error('index negative');
  125. end
  126. if any(beamlet.non_zero_indices > beamlet.x_count*beamlet.y_count*beamlet.z_count)
  127. error('index out of range');
  128. end
  129. % TODO CRITICAL This will break if beamlet file is not in sequence
  130. S(beamlet.non_zero_indices, beamlet.num+1) = beamlet.non_zero_values;
  131. % iVector(idx:idx+Nind-1) = beamlet.non_zero_indices;
  132. % jVector(idx:idx+Nind-1) = (beamlet.num+1) * ones(Nind, 1);
  133. % sVector(idx:idx+Nind-1) = beamlet.non_zero_values;
  134. % idx = idx + Nind;
  135. end
  136. results = S;
  137. otherwise
  138. error('unknown mode');
  139. end
  140. fclose(fid);