1
0

read_ryan_beamlets.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. % dose(beamlet.non_zero_indices) = dose(beamlet.non_zero_indices) + ...
  75. % beamlet.non_zero_values * weights(k);
  76. end
  77. results = beamlets;
  78. end
  79. if strcmpi(mode, 'ryan sum')
  80. % rename for return
  81. % TODO ambiguous output value
  82. if Nbeamlets > 0
  83. results = dose;
  84. else
  85. results = 0;
  86. end
  87. end
  88. case 'peter'
  89. beamlets = cell(1,Nbeamlets);
  90. ROI_idxList = weights;
  91. wbar1 = waitbar(0, 'Creating beamlet array');
  92. for k=1:Nbeamlets
  93. waitbar(k/Nbeamlets, wbar1, ['Extracting beamlet array: #', num2str(k)])
  94. beamlet.num = fread(fid,1,'int');
  95. beamlet.x_count = fread(fid,1,'int');
  96. beamlet.y_count = fread(fid,1,'int');
  97. beamlet.z_count = fread(fid,1,'int');
  98. Nind = fread(fid,1,'int'); % number of non-zero values
  99. non_zero_indices = int32(fread(fid,Nind,'int'));
  100. non_zero_values = single(fread(fid,Nind,'float'));
  101. % add unity to the indices to convert them from C to Matlab style indexing:
  102. non_zero_indices = non_zero_indices + 1;
  103. beamlet.num = beamlet.num + 1;
  104. if any(non_zero_indices < 0)
  105. error('index negative');
  106. end
  107. if any(non_zero_indices > beamlet.x_count*beamlet.y_count*beamlet.z_count)
  108. error('index out of range');
  109. end
  110. % load only the voxels within provided ROI
  111. [ROI_idxIntersect, ia, ~] = intersect (non_zero_indices, ROI_idxList);
  112. beamlet.non_zero_indices = ROI_idxIntersect;
  113. beamlet.non_zero_values = non_zero_values(ia);
  114. beamlets{k} = beamlet;
  115. end
  116. results = beamlets;
  117. close(wbar1)
  118. case 'sparse'
  119. nzmax = 0;
  120. nBmax = 500;
  121. for k=1:nBmax%Nbeamlets
  122. % skip 4 integers
  123. fseek(fid, 4 * 4, 'cof');
  124. % get number of non-zero values
  125. Nind = fread(fid,1,'int');
  126. nzmax = nzmax + Nind;
  127. % skip beamlet data (one int32 and one single array)
  128. fseek(fid, Nind * 2 * 4, 'cof');
  129. end
  130. % close and open again
  131. % TODO this doesn't fit the normal workflow
  132. fclose(fid);
  133. fid = fopen(filename,'r');
  134. if ( fid == -1 )
  135. error(['cannot open "' filename '"']);
  136. end
  137. Nbeamlets = fread(fid,1,'int');
  138. % Return is num_voxels x num_beamlets matrix
  139. % i === row === 1 to num_voxels
  140. % j === col === 1 to num_beamlets
  141. S = sparse([], [], [], 256*256*158, nBmax, nzmax);
  142. idx = 1;
  143. xpg = xprogressbar();
  144. for k=1:nBmax%Nbeamlets
  145. xpg.update(k/Nbeamlets);
  146. beamlet.num = fread(fid,1,'int')
  147. beamlet.x_count = fread(fid,1,'int');
  148. beamlet.y_count = fread(fid,1,'int');
  149. beamlet.z_count = fread(fid,1,'int');
  150. Nind = fread(fid,1,'int'); % number of non-zero values
  151. beamlet.non_zero_indices = int32(fread(fid,Nind,'int'));
  152. beamlet.non_zero_values = single(fread(fid,Nind,'float'));
  153. % add unity to the indices to convert them from C to Matlab style indexing:
  154. beamlet.non_zero_indices = beamlet.non_zero_indices + 1;
  155. if any(beamlet.non_zero_indices < 0)
  156. error('index negative');
  157. end
  158. if any(beamlet.non_zero_indices > beamlet.x_count*beamlet.y_count*beamlet.z_count)
  159. error('index out of range');
  160. end
  161. % TODO CRITICAL This will break if beamlet file is not in sequence
  162. S(beamlet.non_zero_indices, beamlet.num+1) = beamlet.non_zero_values;
  163. % iVector(idx:idx+Nind-1) = beamlet.non_zero_indices;
  164. % jVector(idx:idx+Nind-1) = (beamlet.num+1) * ones(Nind, 1);
  165. % sVector(idx:idx+Nind-1) = beamlet.non_zero_values;
  166. % idx = idx + Nind;
  167. end
  168. results = S;
  169. otherwise
  170. error('unknown mode');
  171. end
  172. fclose(fid);
  173. end