RDXTPS_geometry_setup_wizard_ASP.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. %
  100. % if HCRing_margin > 0
  101. % bwD = bwdistsc(~Geometry.BTV, Geometry.voxel_size);
  102. % % shrinked BTV from original BTV, such that original_BTV > target_VOI > shrinked_BTV
  103. % Geometry.SBTV = bwD >= HCRing_margin;
  104. % Geometry.HCRing = xor(Geometry.BTV, Geometry.SBTV);
  105. % end
  106. %% -----===== Save geometry files =====-----
  107. waitbar(2/total_num_steps, hWaitbar, 'Step 3: Save geometry files');
  108. patient_dir = uifile('getdir', 'Save the patient data to directory');
  109. % make directories
  110. mkdir(patient_dir);
  111. mkdir(fullfile(patient_dir, 'beamlet_batch_files'));
  112. mkdir(fullfile(patient_dir, 'geometry_files'));
  113. mkdir(fullfile(patient_dir, 'matlab_files'));
  114. mkdir(fullfile(patient_dir, 'opt_input'));
  115. mkdir(fullfile(patient_dir, 'opt_output'));
  116. waitbar(2.33/total_num_steps, hWaitbar, 'Step 3: Save matlab geometry files');
  117. % Save matlab geometry file
  118. save(fullfile(patient_dir, 'matlab_files', 'Geometry.mat'), 'Geometry');
  119. waitbar(2.66/total_num_steps, hWaitbar, 'Step 3: Save raw geometry files');
  120. % Write binary geometry files
  121. fid = fopen(fullfile(patient_dir, 'geometry_files', 'rhomw.bin'), 'w');
  122. fwrite(fid, Geometry.rhomw, 'single');
  123. fclose(fid);
  124. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Smw.bin'), 'w');
  125. fwrite(fid, Geometry.Smw, 'single');
  126. fclose(fid);
  127. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Fmw2.bin'), 'w');
  128. fwrite(fid, Geometry.Fmw2, 'single');
  129. fclose(fid);
  130. fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask.bin'), 'w');
  131. fwrite(fid, Geometry.BTV, 'single');
  132. fclose(fid);
  133. % fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask_SBTV.bin'), 'w');
  134. % fwrite(fid, Geometry.SBTV, 'single');
  135. % fclose(fid);
  136. %
  137. % fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask_HCRing.bin'), 'w');
  138. % fwrite(fid, Geometry.HCRing, 'single');
  139. % fclose(fid);
  140. % hWaitbar = waitbar(1/total_num_steps, 'Step 4, Create optimization geometry');
  141. delete(hWaitbar);
  142. msgbox(['Plan geometry created successfully in ' '"' patient_dir '"']);