RDXTPS_geometry_setup_wizard_ASP.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. function [Geometry patient_dir] = RDXTPS_geometry_setup_wizard_ASP()
  2. %PLAN_SETUP_WIZARD Wizard to guide through plan creating
  3. % Usage:
  4. % [ Geometry ] = dicomrt2geometry ( [input_dir] )
  5. % Input:
  6. % input_dir = directory contains DicomRT data
  7. % [] = prompt to select input_dir
  8. % Output:
  9. % Geometry = Ryan's Geometry struct used with RDX TPS
  10. %
  11. % This function imports DicomRT data set and create RDX TPS compatible Geometry
  12. % struct.
  13. %
  14. % TODO: Switch from filename based Dicom identification to StudyID based.
  15. %
  16. % See also readPinnGeometry.m
  17. %
  18. % Author: Xiaohu Mo
  19. total_num_steps = 4;
  20. hWaitbar = waitbar(0);
  21. set(hWaitbar, 'Unit', 'normalized');
  22. op = get(hWaitbar, 'OuterPosition');
  23. set(hWaitbar, 'OuterPosition', op + [0 0.2 0 0]);
  24. %% -----===== DicomRT Import =====-----
  25. waitbar(0/total_num_steps, hWaitbar, 'Step 1: Import DicomRT');
  26. % try
  27. Geometry = dicomrt2geometry();
  28. % catch EXCEPTION_DICOMRT2GEOMETRY
  29. % msgbox('Failed to import DicomRT');
  30. % delete(hWaitbar);
  31. % rethrow(EXCEPTION_DICOMRT2GEOMETRY);
  32. % end
  33. if isempty(Geometry)
  34. msgbox('Failed to import DicomRT');
  35. delete(hWaitbar);
  36. return;
  37. end
  38. %% -----===== BTV and Ring creation =====-----
  39. waitbar(1/total_num_steps, hWaitbar, 'Step 2: Assign target and BTV margin');
  40. ROI_names = cellfun(@(c)c.name, Geometry.ROIS, 'UniformOutput', false);
  41. [target_idx okay] = listdlg('ListString', ROI_names, ...
  42. 'SelectionMode', 'single', 'Name', 'Target Selection', ...
  43. 'PromptString', 'Please select the target ROI. ');
  44. if okay ~= 1
  45. msgbox('Plan creation aborted');
  46. delete(hWaitbar);
  47. return;
  48. end
  49. [BTV_margin_answer] = inputdlg({sprintf('Please enter the BTV margin (cm):\n(default 0.6 cm or 1 sigma, enter 0 to skip)')}, ...
  50. 'BTV margin specification', 1, {'0.6'});
  51. if isempty(BTV_margin_answer)
  52. BTV_margin = 0.6;
  53. else
  54. BTV_margin = str2double(BTV_margin_answer{1});
  55. end
  56. % target_idx and BTV_margin are set. Expand PTV to BTV
  57. PTVmask = false(size(Geometry.rhomw));
  58. % for target_idx = target_indices
  59. PTVmask(Geometry.ROIS{target_idx}.ind) = 1;
  60. % end
  61. if BTV_margin > 0
  62. if exist('BTV_margin', 'var') && BTV_margin >= min(Geometry.voxel_size)
  63. bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  64. Geometry.BTV = bwD <= BTV_margin;
  65. end
  66. end
  67. % Create btv
  68. Geometry.ROIS{end+1} = Geometry.ROIS{end};
  69. Geometry.ROIS{end}.name = 'BTV';
  70. Geometry.ROIS{end}.num_curves = 0;
  71. Geometry.ROIS{end}.curves = {};
  72. Geometry.ROIS{end}.ind = find(Geometry.BTV);
  73. Geometry.ROIS{end}.visible = false;
  74. % Create ring
  75. [ring_margin_answer] = inputdlg({sprintf('Please enter the ring margin (cm):')}, ...
  76. 'Ring margin specification', 1, {'1'});
  77. if isempty(ring_margin_answer)
  78. ring_margin = 1;
  79. else
  80. ring_margin = str2double(ring_margin_answer{1});
  81. end
  82. bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  83. Geometry.Ring = bwD <= ring_margin; % default ring radius 3 cm
  84. Geometry.Ring = xor(Geometry.Ring, PTVmask);
  85. Geometry.ROIS{end+1} = Geometry.ROIS{end};
  86. Geometry.ROIS{end}.name = 'Ring';
  87. Geometry.ROIS{end}.num_curves = 0;
  88. Geometry.ROIS{end}.curves = {};
  89. Geometry.ROIS{end}.ind = find(Geometry.Ring);
  90. Geometry.ROIS{end}.visible = false;
  91. % Create high comformity ring structure for adaptive step size
  92. [HCRing_margin_answer] = inputdlg({sprintf('Please enter the HC ring margin (cm):\n(default 1.2 cm or 2 sigma, enter 0 to skip)')}, ...
  93. 'HCRing margin specification', 1, {'1.2'});
  94. if isempty(HCRing_margin_answer)
  95. HCRing_margin = 1.2;
  96. else
  97. HCRing_margin = str2double(HCRing_margin_answer{1});
  98. end
  99. if HCRing_margin > 0
  100. bwD = bwdistsc(~Geometry.BTV, Geometry.voxel_size);
  101. % shrinked BTV from original BTV, such that original_BTV > target_VOI > shrinked_BTV
  102. Geometry.SBTV = bwD >= HCRing_margin;
  103. Geometry.HCRing = xor(Geometry.BTV, Geometry.SBTV);
  104. end
  105. %% -----===== Save geometry files =====-----
  106. waitbar(2/total_num_steps, hWaitbar, 'Step 3: Save geometry files');
  107. load('WiscPlan_preferences.mat')
  108. patient_dir = uigetdir(WiscPlan_preferences.patientDataPath, 'Save the patient data to directory');
  109. Geometry.data_dir = patient_dir;
  110. WiscPlan_preferences.patientDataPath = patient_dir;
  111. thisDir = mfilename('fullpath');
  112. idcs = strfind(thisDir,'\');
  113. prefsdir = thisDir(1:idcs(end-1)-1);
  114. save([prefsdir '\WiscPlan_preferences.mat'], 'WiscPlan_preferences');
  115. % patient_dir = uifile('getdir', 'Save the patient data to directory');
  116. % !!! Grozomah
  117. % patient_dir = 'C:\010-work\003_localGit\WiscPlan_v2\data\PatientData';
  118. % make directories
  119. mkdir(patient_dir);
  120. mkdir(fullfile(patient_dir, 'beamlet_batch_files'));
  121. mkdir(fullfile(patient_dir, 'geometry_files'));
  122. mkdir(fullfile(patient_dir, 'matlab_files'));
  123. mkdir(fullfile(patient_dir, 'opt_input'));
  124. mkdir(fullfile(patient_dir, 'opt_output'));
  125. Geometry.patient_dir = patient_dir;
  126. waitbar(2.33/total_num_steps, hWaitbar, 'Step 3: Save matlab geometry files');
  127. % Save matlab geometry file
  128. save(fullfile(patient_dir, 'matlab_files', 'Geometry.mat'), 'Geometry');
  129. waitbar(2.66/total_num_steps, hWaitbar, 'Step 3: Save raw geometry files');
  130. % Write binary geometry files
  131. fid = fopen(fullfile(patient_dir, 'geometry_files', 'rhomw.bin'), 'w');
  132. fwrite(fid, Geometry.rhomw, 'single');
  133. fclose(fid);
  134. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Smw.bin'), 'w');
  135. fwrite(fid, Geometry.Smw, 'single');
  136. fclose(fid);
  137. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Fmw2.bin'), 'w');
  138. fwrite(fid, Geometry.Fmw2, 'single');
  139. fclose(fid);
  140. % fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask.bin'), 'w');
  141. % fwrite(fid, Geometry.BTV, 'single');
  142. % fclose(fid);
  143. % fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask_SBTV.bin'), 'w');
  144. % fwrite(fid, Geometry.SBTV, 'single');
  145. % fclose(fid);
  146. %
  147. % fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask_HCRing.bin'), 'w');
  148. % fwrite(fid, Geometry.HCRing, 'single');
  149. % fclose(fid);
  150. % hWaitbar = waitbar(1/total_num_steps, 'Step 4, Create optimization geometry');
  151. delete(hWaitbar);
  152. msgbox(['Plan geometry created successfully in ' '"' patient_dir '"']);