RDXTPS_geometry_setup_wizard_ASP.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. %
  42. % [target_idx okay] = listdlg('ListString', ROI_names, ...
  43. % 'SelectionMode', 'single', 'Name', 'Target Selection', ...
  44. % 'PromptString', 'Please select the target ROI. ');
  45. % if okay ~= 1
  46. % msgbox('Plan creation aborted');
  47. % delete(hWaitbar);
  48. % return;
  49. % end
  50. %
  51. % [BTV_margin_answer] = inputdlg({sprintf('Please enter the BTV margin (cm):\n(default 0.6 cm or 1 sigma, enter 0 to skip)')}, ...
  52. % 'BTV margin specification', 1, {'0.6'});
  53. % if isempty(BTV_margin_answer)
  54. % BTV_margin = 0.6;
  55. % else
  56. % BTV_margin = str2double(BTV_margin_answer{1});
  57. % end
  58. %
  59. % % target_idx and BTV_margin are set. Expand PTV to BTV
  60. % PTVmask = false(size(Geometry.rhomw));
  61. % % for target_idx = target_indices
  62. % PTVmask(Geometry.ROIS{target_idx}.ind) = 1;
  63. % % end
  64. % if BTV_margin > 0
  65. % if exist('BTV_margin', 'var') && BTV_margin >= min(Geometry.voxel_size)
  66. % bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  67. % Geometry.BTV = bwD <= BTV_margin;
  68. % end
  69. % end
  70. %
  71. % % Create btv
  72. % Geometry.ROIS{end+1} = Geometry.ROIS{end};
  73. % Geometry.ROIS{end}.name = 'BTV';
  74. % Geometry.ROIS{end}.num_curves = 0;
  75. % Geometry.ROIS{end}.curves = {};
  76. % Geometry.ROIS{end}.ind = find(Geometry.BTV);
  77. % Geometry.ROIS{end}.visible = false;
  78. %
  79. % % Create ring
  80. % [ring_margin_answer] = inputdlg({sprintf('Please enter the ring margin (cm):')}, ...
  81. % 'Ring margin specification', 1, {'1'});
  82. % if isempty(ring_margin_answer)
  83. % ring_margin = 1;
  84. % else
  85. % ring_margin = str2double(ring_margin_answer{1});
  86. % end
  87. % bwD = bwdistsc(PTVmask, Geometry.voxel_size);
  88. % Geometry.Ring = bwD <= ring_margin; % default ring radius 3 cm
  89. % Geometry.Ring = xor(Geometry.Ring, PTVmask);
  90. % Geometry.ROIS{end+1} = Geometry.ROIS{end};
  91. % Geometry.ROIS{end}.name = 'Ring';
  92. % Geometry.ROIS{end}.num_curves = 0;
  93. % Geometry.ROIS{end}.curves = {};
  94. % Geometry.ROIS{end}.ind = find(Geometry.Ring);
  95. % Geometry.ROIS{end}.visible = false;
  96. % % Create high comformity ring structure for adaptive step size
  97. % [HCRing_margin_answer] = inputdlg({sprintf('Please enter the HC ring margin (cm):\n(default 1.2 cm or 2 sigma, enter 0 to skip)')}, ...
  98. % 'HCRing margin specification', 1, {'1.2'});
  99. % if isempty(HCRing_margin_answer)
  100. % HCRing_margin = 1.2;
  101. % else
  102. % HCRing_margin = str2double(HCRing_margin_answer{1});
  103. % end
  104. %
  105. % if HCRing_margin > 0
  106. % bwD = bwdistsc(~Geometry.BTV, Geometry.voxel_size);
  107. % % shrinked BTV from original BTV, such that original_BTV > target_VOI > shrinked_BTV
  108. % Geometry.SBTV = bwD >= HCRing_margin;
  109. % Geometry.HCRing = xor(Geometry.BTV, Geometry.SBTV);
  110. % end
  111. %% -----===== Save geometry files =====-----
  112. waitbar(2/total_num_steps, hWaitbar, 'Step 3: Save geometry files');
  113. patient_dir = uifile('getdir', 'Save the patient data to directory');
  114. % !!! Grozomah
  115. % patient_dir = 'C:\010-work\003_localGit\WiscPlan_v2\data\PatientData';
  116. % make directories
  117. mkdir(patient_dir);
  118. mkdir(fullfile(patient_dir, 'beamlet_batch_files'));
  119. mkdir(fullfile(patient_dir, 'geometry_files'));
  120. mkdir(fullfile(patient_dir, 'matlab_files'));
  121. mkdir(fullfile(patient_dir, 'opt_input'));
  122. mkdir(fullfile(patient_dir, 'opt_output'));
  123. Geometry.patient_dir = patient_dir;
  124. waitbar(2.33/total_num_steps, hWaitbar, 'Step 3: Save matlab geometry files');
  125. % Save matlab geometry file
  126. save(fullfile(patient_dir, 'matlab_files', 'Geometry.mat'), 'Geometry');
  127. waitbar(2.66/total_num_steps, hWaitbar, 'Step 3: Save raw geometry files');
  128. % Write binary geometry files
  129. fid = fopen(fullfile(patient_dir, 'geometry_files', 'rhomw.bin'), 'w');
  130. fwrite(fid, Geometry.rhomw, 'single');
  131. fclose(fid);
  132. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Smw.bin'), 'w');
  133. fwrite(fid, Geometry.Smw, 'single');
  134. fclose(fid);
  135. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Fmw2.bin'), 'w');
  136. fwrite(fid, Geometry.Fmw2, 'single');
  137. fclose(fid);
  138. % fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask.bin'), 'w');
  139. % fwrite(fid, Geometry.BTV, 'single');
  140. % fclose(fid);
  141. % fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask_SBTV.bin'), 'w');
  142. % fwrite(fid, Geometry.SBTV, 'single');
  143. % fclose(fid);
  144. %
  145. % fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask_HCRing.bin'), 'w');
  146. % fwrite(fid, Geometry.HCRing, 'single');
  147. % fclose(fid);
  148. % hWaitbar = waitbar(1/total_num_steps, 'Step 4, Create optimization geometry');
  149. delete(hWaitbar);
  150. msgbox(['Plan geometry created successfully in ' '"' patient_dir '"']);