load2geometry.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. function [Geometry] = load2geometry
  2. %% -----===== Get CT files =====-----
  3. Geometry.data_dir = uigetdir('C:\Box Sync\Optimal Stopping\Data\CDP', 'Select folder with patient data');
  4. [IMG_in, IMG_path, filterIdx] = uigetfile([{'*.nrrd'; '*.am'}], 'Select CT image', Geometry.data_dir);
  5. % 'C:\Box Sync\Optimal Stopping\Data\CDP\CDP001\'
  6. total_num_steps = 4;
  7. hWaitbar = waitbar(0);
  8. % -- geometry explained --
  9. % geometry_file important parts:
  10. % Geometry.ROIS - actually only to get the binary mask of the target
  11. % Geometry.data - CT image in HU
  12. % Geometry.voxel_size - size of voxels in cm
  13. % Geometry.start - image coordinate origin
  14. % these are derived from above:
  15. % +Geometry.rhomw - CT image with values in relative water density
  16. % +Geometry.Smw - very similair to rhomw
  17. % +Geometry.Fmw2
  18. % +Geometry.BTV
  19. % +Geometry.ring - a ring around PTV used to pull down IMRT optimization
  20. % --------------------------
  21. switch filterIdx
  22. case 0
  23. warning('No file selected, aborting!')
  24. return
  25. case 1
  26. [Geometry.data, meta]=nrrdread([IMG_path, IMG_in]);
  27. disp('NRRD file loaded!')
  28. Geometry.rhomw = CT2dens(Geometry.data, 'pinn');
  29. Geometry.rhomw(Geometry.rhomw < 0.0012) = 0.0012;
  30. [Geometry.Smw Geometry.Fmw2] = dens2mstp(Geometry.rhomw);
  31. Geometry.voxel_size = meta.spacedirections;
  32. Geometry.start=meta.spaceorigin;
  33. case 2
  34. imgIn=am2mat([IMG_path, IMG_in]);
  35. Geometry.data = permute(imgIn.data, [2,1,3]);
  36. disp('NRRD file loaded!')
  37. Geometry.rhomw = CT2dens(Geometry.data, 'pinn');
  38. Geometry.rhomw(Geometry.rhomw < 0.0012) = 0.0012;
  39. [Geometry.Smw Geometry.Fmw2] = dens2mstp(Geometry.rhomw);
  40. Geometry.voxel_size = imgIn.voxel_size;
  41. Geometry.start=imgIn.start;
  42. case 3
  43. % something selected
  44. error('please do not select *all* in file selection')
  45. otherwise
  46. % error('You should never see this.')
  47. end
  48. % Geometry.ROIS
  49. %% -----===== Get mask/contour files =====-----
  50. [TRGT_in, TRGT_path, filterIdx2] = uigetfile([{'*.nrrd'; '*.am'}], 'Select file with Target', Geometry.data_dir);
  51. switch filterIdx2
  52. case 0
  53. warning('No file selected!')
  54. case 1
  55. [TRGT_img, meta]=nrrdread([TRGT_path, TRGT_in]);
  56. disp('NRRD file loaded!')
  57. Geometry.ROIS{1}.ind = find(TRGT_img>0);
  58. Geometry.ROIS{1}.name= 'Target';
  59. case 2
  60. TRGT_in=am2mat([TRGT_path, TRGT_in]);
  61. disp('AM file loaded!')
  62. TRGT_img = permute(TRGT_in.data, [2,1,3]);
  63. Geometry.ROIS{1}.ind = find(TRGT_img>0);
  64. Geometry.ROIS{1}.name= 'Target';
  65. case 3
  66. error('please do not select *all* in file selection')
  67. % something selected
  68. otherwise
  69. % error('You should never see this.')
  70. end
  71. %% -----===== Save geometry files =====-----
  72. patient_dir = uifile('getdir', 'Save the patient data to directory');
  73. Geometry.patient_dir = patient_dir;
  74. % patient_dir = 'C:\010-work\003_localGit\WiscPlan_v2\data\PatientData\';
  75. % make directories
  76. mkdir(Geometry.patient_dir);
  77. mkdir(fullfile(Geometry.patient_dir, 'beamlet_batch_files'));
  78. mkdir(fullfile(Geometry.patient_dir, 'geometry_files'));
  79. mkdir(fullfile(Geometry.patient_dir, 'matlab_files'));
  80. mkdir(fullfile(Geometry.patient_dir, 'opt_input'));
  81. mkdir(fullfile(Geometry.patient_dir, 'opt_output'));
  82. %% -----===== BTV and Ring creation =====-----
  83. % waitbar(1/total_num_steps, hWaitbar, 'Step 2: Assign target and BTV margin');
  84. % ROI_names = cellfun(@(c)c.name, Geometry.ROIS, 'UniformOutput', false);
  85. %
  86. % [target_idx okay] = listdlg('ListString', ROI_names, ...
  87. % 'SelectionMode', 'single', 'Name', 'Target Selection', ...
  88. % 'PromptString', 'Please select the target ROI. ');
  89. % if okay ~= 1
  90. % msgbox('Plan creation aborted');
  91. % delete(hWaitbar);
  92. % return;
  93. % end
  94. %
  95. % [BTV_margin_answer] = inputdlg({sprintf('Please enter the BTV margin (cm):\n(default 0.6 cm or 1 sigma, enter 0 to skip)')}, ...
  96. % 'BTV margin specification', 1, {'0.6'});
  97. % if isempty(BTV_margin_answer)
  98. % BTV_margin = 0.6;
  99. % else
  100. % BTV_margin = str2double(BTV_margin_answer{1});
  101. % end
  102. %
  103. % % target_idx and BTV_margin are set. Expand PTV to BTV
  104. % PTVmask = false(size(Geometry.rhomw));
  105. % % for target_idx = target_indices
  106. % PTVmask(Geometry.ROIS{target_idx}.ind) = 1;
  107. % % end
  108. % if BTV_margin > 0
  109. % if exist('BTV_margin', 'var') && BTV_margin >= min(Geometry.voxel_size)
  110. % bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  111. % Geometry.BTV = bwD <= BTV_margin;
  112. % end
  113. % end
  114. %
  115. % % Create btv
  116. % Geometry.ROIS{end+1} = Geometry.ROIS{end};
  117. % Geometry.ROIS{end}.name = 'BTV';
  118. % Geometry.ROIS{end}.num_curves = 0;
  119. % Geometry.ROIS{end}.curves = {};
  120. % Geometry.ROIS{end}.ind = find(Geometry.BTV);
  121. % Geometry.ROIS{end}.visible = false;
  122. %
  123. % % Create ring
  124. % [ring_margin_answer] = inputdlg({sprintf('Please enter the ring margin (cm):')}, ...
  125. % 'Ring margin specification', 1, {'1'});
  126. % if isempty(ring_margin_answer)
  127. % ring_margin = 1;
  128. % else
  129. % ring_margin = str2double(ring_margin_answer{1});
  130. % end
  131. % bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  132. % Geometry.Ring = bwD <= ring_margin; % default ring radius 3 cm
  133. % Geometry.Ring = xor(Geometry.Ring, PTVmask);
  134. % Geometry.ROIS{end+1} = Geometry.ROIS{end};
  135. % Geometry.ROIS{end}.name = 'Ring';
  136. % Geometry.ROIS{end}.num_curves = 0;
  137. % Geometry.ROIS{end}.curves = {};
  138. % Geometry.ROIS{end}.ind = find(Geometry.Ring);
  139. % Geometry.ROIS{end}.visible = false;
  140. %% -----===== Save the matlab files =====-----
  141. waitbar(2.33/total_num_steps, hWaitbar, 'Step 3: Save matlab geometry files');
  142. % Save matlab geometry file
  143. save(fullfile(Geometry.patient_dir, 'matlab_files', 'Geometry.mat'), 'Geometry');
  144. waitbar(2.66/total_num_steps, hWaitbar, 'Step 3: Save raw geometry files');
  145. % Write binary geometry files
  146. fid = fopen(fullfile(Geometry.patient_dir, 'geometry_files', 'rhomw.bin'), 'w');
  147. fwrite(fid, Geometry.rhomw, 'single');
  148. fclose(fid);
  149. fid = fopen(fullfile(Geometry.patient_dir, 'geometry_files', 'Smw.bin'), 'w');
  150. fwrite(fid, Geometry.Smw, 'single');
  151. fclose(fid);
  152. fid = fopen(fullfile(Geometry.patient_dir, 'geometry_files', 'Fmw2.bin'), 'w');
  153. fwrite(fid, Geometry.Fmw2, 'single');
  154. fclose(fid);
  155. delete(hWaitbar);
  156. waitfor(msgbox(['Plan geometry created successfully in ' '"' Geometry.patient_dir '"']));
  157. end