RDXTPS_geometry_setup_wizard.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. function [Geometry patient_dir] = RDXTPS_geometry_setup_wizard()
  2. %RDXTPS_geometry_setup_wizard Wizard to guide through plan geometry creation.
  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 change the description heading of this file.
  15. % TODO allow multiple choices in target selections
  16. %
  17. % See also readPinnGeometry.m
  18. %
  19. % Author: Xiaohu Mo
  20. total_num_steps = 4;
  21. hWaitbar = waitbar(0);
  22. set(hWaitbar, 'Unit', 'normalized');
  23. op = get(hWaitbar, 'OuterPosition');
  24. set(hWaitbar, 'OuterPosition', op + [0 0.2 0 0]);
  25. %% -----===== DicomRT Import =====-----
  26. waitbar(0/total_num_steps, hWaitbar, 'Step 1: Import DicomRT');
  27. try
  28. Geometry = dicomrt2geometry();
  29. catch EXCEPTION_DICOMRT2GEOMETRY
  30. msgbox('Failed to import DicomRT');
  31. delete(hWaitbar);
  32. rethrow(EXCEPTION_DICOMRT2GEOMETRY);
  33. end
  34. if isempty(Geometry)
  35. msgbox('Failed to import DicomRT');
  36. delete(hWaitbar);
  37. return;
  38. end
  39. %% -----===== BTV expansion =====-----
  40. waitbar(1/total_num_steps, hWaitbar, 'Step 2: Assign target and BTV margin');
  41. ROI_names = cellfun(@(c)c.name, Geometry.ROIS, 'UniformOutput', false);
  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. [BTV_margin_answer] = inputdlg({sprintf('Please enter the BTV margin (cm):\n(default 0.6 cm or 1 sigma, enter 0 to skip)')}, ...
  51. 'BTV margin specification', 1, {'0.6'});
  52. if isempty(BTV_margin_answer)
  53. BTV_margin = 0.6;
  54. else
  55. BTV_margin = str2double(BTV_margin_answer{1});
  56. end
  57. % target_idx and BTV_margin are set. Expand PTV to BTV
  58. if BTV_margin > 0
  59. Geometry.BTV = false(size(Geometry.rhomw));
  60. % for target_idx = target_indices
  61. Geometry.BTV(Geometry.ROIS{target_idx}.ind) = 1;
  62. % end
  63. if exist('BTV_margin', 'var') && BTV_margin >= min(Geometry.voxel_size)
  64. bwD = bwdistsc(Geometry.BTV, Geometry.voxel_size);
  65. Geometry.BTV = bwD <= BTV_margin;
  66. end
  67. end
  68. %% -----===== Save geometry files =====-----
  69. waitbar(2/total_num_steps, hWaitbar, 'Step 3: Save geometry files');
  70. patient_dir = uifile('getdir', 'Save the patient data to directory');
  71. % make directories
  72. mkdir(patient_dir);
  73. mkdir(fullfile(patient_dir, 'beamlet_batch_files'));
  74. mkdir(fullfile(patient_dir, 'geometry_files'));
  75. mkdir(fullfile(patient_dir, 'matlab_files'));
  76. mkdir(fullfile(patient_dir, 'opt_input'));
  77. mkdir(fullfile(patient_dir, 'opt_output'));
  78. waitbar(2.33/total_num_steps, hWaitbar, 'Step 3: Save matlab geometry files');
  79. % Save matlab geometry file
  80. save(fullfile(patient_dir, 'matlab_files', 'Geometry.mat'), 'Geometry');
  81. waitbar(2.66/total_num_steps, hWaitbar, 'Step 3: Save raw geometry files');
  82. % Write binary geometry files
  83. fid = fopen(fullfile(patient_dir, 'geometry_files', 'rhomw.bin'), 'w');
  84. fwrite(fid, Geometry.rhomw, 'single');
  85. fclose(fid);
  86. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Smw.bin'), 'w');
  87. fwrite(fid, Geometry.Smw, 'single');
  88. fclose(fid);
  89. fid = fopen(fullfile(patient_dir, 'geometry_files', 'Fmw2.bin'), 'w');
  90. fwrite(fid, Geometry.Fmw2, 'single');
  91. fclose(fid);
  92. fid = fopen(fullfile(patient_dir, 'geometry_files', 'target_mask.bin'), 'w');
  93. fwrite(fid, Geometry.BTV, 'single');
  94. fclose(fid);
  95. % hWaitbar = waitbar(1/total_num_steps, 'Step 4, Create optimization geometry');
  96. delete(hWaitbar);
  97. msgbox(['Plan geometry created successfully in ' '"' patient_dir '"']);