XTPS.m 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. classdef XTPS < handle
  2. %XTPS Main GUI for RDXTPS front end.
  3. %
  4. % Default to contain three SliceViewerPanels for viewing.
  5. %
  6. % TODO:
  7. % Move Geometry and optResults into SliceViewerPanelSource completely so that
  8. % only one copy exist.
  9. % Move color stuff into Geometry so that can be saved.
  10. %
  11. % See also SliceViewerPanel
  12. %
  13. % Author Xiaohu Mo
  14. % Robust optimization by Peter Ferjancic
  15. properties
  16. handles
  17. patient_dir
  18. dosecalcSetup
  19. end
  20. methods
  21. %-------------------------------------------------------------------------------
  22. function obj = XTPS() % XTPS constructor
  23. hMainFigure = figure('MenuBar', 'none', 'Name', 'RDXTPS: No Patient', 'Position', [100 100 800 600], 'Renderer', 'painters');
  24. obj.handles = guihandles(hMainFigure);
  25. obj.handles.hMainFigure = hMainFigure;
  26. % Adding widgets
  27. obj.handles.hROIPanel = uipanel('Parent', obj.handles.hMainFigure, 'Title', 'ROI display');
  28. obj.handles.hROITable = uitable('Parent', obj.handles.hROIPanel);
  29. obj.handles.hDosePanel = uipanel('Parent', obj.handles.hMainFigure, 'Title', 'Dose display');
  30. obj.handles.hDoseTable = uitable('Parent', obj.handles.hDosePanel);
  31. obj.handles.hDoseModeDropdown = uicontrol('Parent', obj.handles.hDosePanel, 'Style', 'popupmenu');
  32. obj.handles.hDoseModeLabel = uicontrol('Parent', obj.handles.hDosePanel, 'Style', 'text');
  33. obj.handles.hMiscPanel = uipanel('Parent', obj.handles.hMainFigure, 'Title', 'Misc settings');
  34. obj.handles.hTCSShow = uicontrol('Parent', obj.handles.hMiscPanel, 'Style', 'checkbox');
  35. obj.handles.hWWLabel = uicontrol('Parent', obj.handles.hMiscPanel, 'Style', 'edit');
  36. obj.handles.hWWSlider = uicontrol('Parent', obj.handles.hMiscPanel, 'Style', 'slider');
  37. obj.handles.hWWEdit = uicontrol('Parent', obj.handles.hMiscPanel, 'Style', 'edit');
  38. obj.handles.hWLLabel = uicontrol('Parent', obj.handles.hMiscPanel, 'Style', 'edit');
  39. obj.handles.hWLSlider = uicontrol('Parent', obj.handles.hMiscPanel, 'Style', 'slider');
  40. obj.handles.hWLEdit = uicontrol('Parent', obj.handles.hMiscPanel, 'Style', 'edit');
  41. obj.handles.hSVPS = SliceViewerPanelSource();
  42. obj.handles.hSliceViewerPanel(1) = SliceViewerPanel(obj.handles.hSVPS, 3, obj.handles.hMainFigure);
  43. obj.handles.hSliceViewerPanel(2) = SliceViewerPanel(obj.handles.hSVPS, 2, obj.handles.hMainFigure);
  44. obj.handles.hSliceViewerPanel(3) = SliceViewerPanel(obj.handles.hSVPS, 1, obj.handles.hMainFigure);
  45. % File menu
  46. obj.handles.hFileMenu = uimenu(obj.handles.hMainFigure, 'Label','File');
  47. obj.handles.hImportDicomRTMenu = uimenu(obj.handles.hFileMenu, 'Label', '1a. Import DicomRT Geometry');
  48. obj.handles.hNRRD1 = uimenu(obj.handles.hFileMenu, 'Label', '1b. Geometry wizard (am/nrrd)');
  49. obj.handles.hNRRD2 = uimenu(obj.handles.hFileMenu, 'Label', '1b. Add Geometry ROI');
  50. obj.handles.hImgDownsample = uimenu(obj.handles.hFileMenu, 'Label', '1(c). Downsample data');
  51. obj.handles.hDosecalcMenu = uimenu(obj.handles.hFileMenu, 'Label', '2.1 Beamlet Dose Calculation');
  52. obj.handles.hMergeBeamlets = uimenu(obj.handles.hFileMenu, 'Label', '2.2 Merge Beamlets');
  53. obj.handles.hOptimMenu = uimenu(obj.handles.hFileMenu, 'Label', '3a.1 Classical Optimization');
  54. obj.handles.hSaveOptimResultsMenu = uimenu(obj.handles.hFileMenu, 'Label', '3a.2 Save Optimization Results');
  55. obj.handles.hGetFullDose = uimenu(obj.handles.hFileMenu, 'Label', '3a.3 Get Full Dose');
  56. obj.handles.hRO_GoalSpec = uimenu(obj.handles.hFileMenu, 'Label', '3a. RO Goal Spec');
  57. obj.handles.hRO_Optimize = uimenu(obj.handles.hFileMenu, 'Label', '3b. RO Optimization');
  58. obj.handles.hRO_fullDose = uimenu(obj.handles.hFileMenu, 'Label', '3c. Full dose calc');
  59. obj.handles.hLoadGeometryMenu = uimenu(obj.handles.hFileMenu, 'Label', 'Load Geometry', 'Separator','on');
  60. obj.handles.hLoadOptresultsMenu = uimenu(obj.handles.hFileMenu, 'Label', 'Load OptResults');
  61. obj.handles.hSaveAsGeometryMenu = uimenu(obj.handles.hFileMenu, 'Label', 'Save Geometry As...', 'Separator','on');
  62. obj.handles.hSaveAsOptResultsMenu = uimenu(obj.handles.hFileMenu, 'Label', 'Save OptResults As...');
  63. obj.handles.hExportPinnacleMenu = uimenu(obj.handles.hFileMenu, 'Label', 'Export Pinnacle Files', 'Separator','on');
  64. % Full RO menu
  65. obj.handles.hFullROMenu = uimenu(obj.handles.hMainFigure, 'Label', 'Full RO');
  66. obj.handles.hFullRO_beamCalc = uimenu(obj.handles.hFullROMenu, 'Label', 'FRO beam calc');
  67. obj.handles.hFullRO_mergeBeams = uimenu(obj.handles.hFullROMenu, 'Label', 'FRO mergeBeams');
  68. obj.handles.hFullRO_Opt = uimenu(obj.handles.hFullROMenu, 'Label', 'FRO optimization');
  69. % Tools menu
  70. obj.handles.hToolsMenu = uimenu(obj.handles.hMainFigure, 'Label', 'Tools');
  71. obj.handles.hPlotDVHMenu = uimenu(obj.handles.hToolsMenu, 'Label', 'Plot DVH');
  72. obj.handles.hResampleGeometry = uimenu(obj.handles.hToolsMenu, 'Label', 'Resample Geometry');
  73. % Associate callbacks
  74. set(obj.handles.hMainFigure, 'ResizeFcn', @obj.set_layout);
  75. set(obj.handles.hROITable, 'CellSelectionCallback', @obj.ROITable_CellSelectionCallback);
  76. set(obj.handles.hDoseTable, 'CellSelectionCallback', @obj.DoseTable_CellSelectionCallback);
  77. set(obj.handles.hDoseTable, 'CellEditCallback', @obj.DoseTable_CellEditCallback);
  78. set(obj.handles.hImportDicomRTMenu, 'Callback', @obj.ImportDicomRTMenu_Callback);
  79. set(obj.handles.hDosecalcMenu, 'Callback', @obj.DosecalcMenu_Callback);
  80. % Grozomah
  81. set(obj.handles.hNRRD1, 'Callback', @obj.NRRD1_Callback);
  82. set(obj.handles.hNRRD2, 'Callback', @obj.NRRD2_Callback);
  83. set(obj.handles.hImgDownsample, 'Callback', @obj.ImgDownsample_Callback);
  84. set(obj.handles.hRO_GoalSpec, 'Callback', @obj.RO_GoalSpec);
  85. set(obj.handles.hRO_Optimize, 'Callback', @obj.RO_Optimize);
  86. set(obj.handles.hRO_fullDose, 'Callback', @obj.RO_fullDose);
  87. % Grozomah
  88. set(obj.handles.hMergeBeamlets, 'Callback', @obj.MergeBeamlets_Callback);
  89. set(obj.handles.hGetFullDose, 'Callback', @obj.GetFullDose_Callback);
  90. set(obj.handles.hSaveOptimResultsMenu, 'Callback', @obj.SaveOptimResultsMenu_Callback);
  91. set(obj.handles.hOptimMenu, 'Callback', @obj.OptimMenu_Callback);
  92. set(obj.handles.hLoadGeometryMenu, 'Callback', @obj.LoadGeometryMenu_Callback);
  93. set(obj.handles.hLoadOptresultsMenu, 'Callback', @obj.LoadOptresultsMenu_Callback);
  94. set(obj.handles.hSaveAsGeometryMenu, 'Callback', @obj.SaveAsGeometryMenu_Callback);
  95. set(obj.handles.hSaveAsOptResultsMenu, 'Callback', @obj.SaveAsOptResultsMenu_Callback);
  96. set(obj.handles.hExportPinnacleMenu, 'Callback', @obj.ExportPinnacleMenu_Callback);
  97. set(obj.handles.hPlotDVHMenu, 'Callback', @obj.PlotDVHMenu_Callback);
  98. set(obj.handles.hResampleGeometry, 'Callback', @obj.ResampleGeometry_Callback);
  99. set(obj.handles.hFullRO_beamCalc, 'Callback', @obj.FullRO_beamCalc_Callback);
  100. set(obj.handles.hFullRO_mergeBeams, 'Callback', @obj.hFullRO_mergeBeams_Callback);
  101. set(obj.handles.hFullRO_Opt, 'Callback', @obj.FullRO_Opt_Callback);
  102. set(obj.handles.hDoseModeDropdown, 'Callback', @obj.DoseModeDropdown_Callback);
  103. set(obj.handles.hWWSlider, 'Callback', @obj.WWSlider_Callback);
  104. set(obj.handles.hWWEdit, 'Callback', @obj.WWEdit_Callback);
  105. set(obj.handles.hWLSlider, 'Callback', @obj.WLSlider_Callback);
  106. set(obj.handles.hWLEdit, 'Callback', @obj.WLEdit_Callback);
  107. set(obj.handles.hTCSShow, 'Callback', @obj.TCSShow_Callback);
  108. obj.set_widget_properties();
  109. obj.set_layout();
  110. obj.updateROITable();
  111. obj.updateDoseTable();
  112. obj.updateCTWindowControls();
  113. end % XTPS constructor
  114. %-------------------------------------------------------------------------------
  115. function set_widget_properties(obj)
  116. %% Set widget default properties, excluding those related to data
  117. h = obj.handles; % shortcut
  118. set(h.hROITable, 'ColumnEditable', logical([1 0 0]));
  119. set(h.hROITable, 'ColumnFormat', {'logical', 'char', 'char'});
  120. set(h.hROITable, 'ColumnName', {'V' 'C' 'ROI'});
  121. set(h.hROITable, 'ColumnWidth',{20 15 'auto'});
  122. set(h.hROITable, 'RowName', {'numbered'});
  123. set(h.hROITable, 'RowStriping', 'off');
  124. set(h.hROITable, 'ToolTipString', 'click to set color');
  125. set(h.hDoseTable, 'ColumnEditable', logical([0 1 1]));
  126. set(h.hDoseTable, 'ColumnFormat', {'char' 'numeric' 'numeric'});
  127. set(h.hDoseTable, 'ColumnName', {'C' 'Dose' 'Alpha'});
  128. set(h.hDoseTable, 'ColumnWidth',{15 'auto' 'auto'});
  129. set(h.hDoseTable, 'RowName', []);
  130. set(h.hDoseTable, 'RowStriping', 'off');
  131. set(h.hDoseTable, 'ToolTipString', 'click to set color');
  132. set(h.hDoseModeLabel, 'String', 'display mode');
  133. set(h.hDoseModeDropdown, 'String', {'None' 'Isodose' 'IsodoseLine' 'Colorwash'});
  134. set(h.hWWLabel, 'String', 'Win. Width');
  135. set(h.hWWSlider, 'Min', 0, 'Max', 2048, 'SliderStep', [10/2048 100/2048]);
  136. set(h.hWLLabel, 'String', 'Win. Level');
  137. set(h.hWLSlider, 'Min', 0, 'Max', 2048, 'SliderStep', [10/2048 100/2048]);
  138. set(h.hTCSShow, 'String', 'Show TCS planes', 'Value', get(h.hTCSShow, 'Max'));
  139. % BackgroundColor (get all ui object handles except uitable)
  140. set(findobj(h.hMainFigure, '-property', 'BackgroundColor', '-not', 'Type', 'uitable'), ...
  141. 'BackgroundColor', get(h.hMainFigure, 'Color'));
  142. end
  143. %-------------------------------------------------------------------------------
  144. function set_layout(obj, src, evt)
  145. % Set widget positions and size. Also as ResizeFcn.
  146. h = obj.handles; % shortcut
  147. set(findobj(h.hMainFigure, '-property', 'Units'), 'Units', 'Normalized');
  148. padding = [0.005 0.01];
  149. setPositionXY(h.hDosePanel, [0 0.2], [0.2 0.5], padding);
  150. setPositionXY(h.hROIPanel, [0 0.2], [0.5 1], padding);
  151. setPositionXY(h.hMiscPanel, [0 0.2], [0 0.2], padding);
  152. setPositionXY(h.hSliceViewerPanel(1).hPanel, [0.2 0.7], [0 1], padding);
  153. setPositionXY(h.hSliceViewerPanel(2).hPanel, [0.7 1], [0.5 1], padding);
  154. setPositionXY(h.hSliceViewerPanel(3).hPanel, [0.7 1], [0 0.5], padding);
  155. setPositionXY(h.hROITable, [0 1], [0 1], padding);
  156. setPositionXY(h.hDoseModeDropdown, [0.5 1], [0.9 1], padding);
  157. setPositionXY(h.hDoseModeLabel, [0 0.5], [0.9 1], padding);
  158. setPositionXY(h.hDoseTable, [0 1], [0 0.9], padding);
  159. setPositionXY(h.hWWLabel, [0 0.3], [0.67 1], padding, 1.5, 'characters');
  160. setPositionXY(h.hWWSlider, [0.3 0.85], [0.67 1], padding, 1.5, 'characters');
  161. setPositionXY(h.hWWEdit, [0.85 1], [0.67 1], padding, 1.5, 'characters');
  162. setPositionXY(h.hWLLabel, [0 0.3], [0.33 0.67], padding, 1.5, 'characters');
  163. setPositionXY(h.hWLSlider, [0.3 0.85], [0.33 0.67], padding, 1.5, 'characters');
  164. setPositionXY(h.hWLEdit, [0.85 1], [0.33 0.67], padding, 1.5, 'characters');
  165. setPositionXY(h.hTCSShow, [0 1], [0.0 0.33], padding, 1.5, 'characters');
  166. table_width = getWidthPixel(obj.handles.hROITable);
  167. set(obj.handles.hROITable, 'ColumnWidth', {20 15 table_width-20-15-35});
  168. table_width = getWidthPixel(obj.handles.hDoseTable);
  169. set(obj.handles.hDoseTable, 'ColumnWidth', {15 (table_width-15)/2-2 (table_width-15)/2-2});
  170. % Panel ResizeFcn not executed after init. Has to manually call.
  171. for i = 1:numel(h.hSliceViewerPanel)
  172. h.hSliceViewerPanel(i).set_layout();
  173. end
  174. end
  175. %-------------------------------------------------------------------------------
  176. function load_geometry(obj, geometry_filename)
  177. %% load geometry
  178. if nargin == 2 % load the geometry file if supplied
  179. load(geometry_filename);
  180. elseif nargin == 1 % Geometry should be loaded already
  181. Geometry = obj.handles.hSVPS.Geometry;
  182. if isempty(obj.handles.hSVPS.Geometry)
  183. % TODO appropriate handling
  184. return;
  185. end
  186. end
  187. % add x,y,z axis for plotting purposes
  188. Geometry.x = Geometry.start(1) + Geometry.voxel_size(1) * (0:size(Geometry.rhomw, 1)-1);
  189. Geometry.y = Geometry.start(2) + Geometry.voxel_size(2) * (0:size(Geometry.rhomw, 2)-1);
  190. Geometry.z = Geometry.start(3) + Geometry.voxel_size(3) * (0:size(Geometry.rhomw, 3)-1);
  191. for i = 1:numel(Geometry.ROIS)
  192. % TODO unify curves as N x 3 cell array so we don't need Xcurves & Ycurves,
  193. % but that way will break back-compatibility.
  194. if isfield(Geometry.ROIS{i}, 'Xcurves')
  195. Xcurves_coordinate = cellfun(@(c)c(1,1), Geometry.ROIS{i}.Xcurves);
  196. Geometry.ROIS{i}.Xcurves_slices = ...
  197. round((Xcurves_coordinate - Geometry.start(1)) / Geometry.voxel_size(1)) + 1;
  198. else
  199. % Old version doesn't have XYcurves. Create an empty index to skip plotting.
  200. Geometry.ROIS{i}.Xcurves_slices = [];
  201. end
  202. if isfield(Geometry.ROIS{i}, 'Ycurves')
  203. Ycurves_coordinate = cellfun(@(c)c(1,2), Geometry.ROIS{i}.Ycurves);
  204. Geometry.ROIS{i}.Ycurves_slices = ...
  205. round((Ycurves_coordinate - Geometry.start(2)) / Geometry.voxel_size(2)) + 1;
  206. else
  207. % Old version doesn't have XYcurves. Create an empty index to skip plotting.
  208. Geometry.ROIS{i}.Ycurves_slices = [];
  209. end
  210. if isfield(Geometry.ROIS{i}, 'curves')
  211. Zcurves_coordinate = cellfun(@(c)c(1,3), Geometry.ROIS{i}.curves);
  212. Geometry.ROIS{i}.Zcurves_slices = ...
  213. round((Zcurves_coordinate - Geometry.start(3)) / Geometry.voxel_size(3)) + 1;
  214. else
  215. Geometry.ROIS{i}.Zcurves_slices = [];
  216. end
  217. end
  218. % Convert data into float
  219. if isinteger(Geometry.data)
  220. Geometry.data = single(Geometry.data);
  221. end
  222. % Init/set display settings if absent
  223. num_ROI = numel(Geometry.ROIS);
  224. for i = 1:num_ROI
  225. if ~isfield(Geometry.ROIS{i}, 'visible')
  226. Geometry.ROIS{i}.visible = false;
  227. end
  228. if ~isfield(Geometry.ROIS{i}, 'color')
  229. Geometry.ROIS{i}.color = rand(1,3);
  230. end
  231. end
  232. % Set/save to SliceViewerPanelSource
  233. obj.handles.hSVPS.Geometry = Geometry;
  234. obj.handles.hSVPS.TCSVisible = ...
  235. (get(obj.handles.hTCSShow, 'Value') == get(obj.handles.hTCSShow, 'Max'));
  236. % updates
  237. obj.handles.hSliceViewerPanel(1).load_data();
  238. obj.handles.hSliceViewerPanel(2).load_data();
  239. obj.handles.hSliceViewerPanel(3).load_data();
  240. obj.updateROITable();
  241. end
  242. %-------------------------------------------------------------------------------
  243. function load_optResults(obj, optresults_filename)
  244. if nargin == 2 % load the optResults file if supplied
  245. load(optresults_filename, 'optResults');
  246. obj.handles.hSVPS.optResults = optResults;
  247. elseif nargin == 1 % optResults should exist already
  248. % Do nothing for now
  249. end
  250. obj.updateDoseTable();
  251. end
  252. %-------------------------------------------------------------------------------
  253. function updateROITable(obj)
  254. % Update ROITable using values in Geometry.ROIS
  255. if isempty(obj.handles.hSVPS.Geometry); return; end
  256. tdata = get(obj.handles.hROITable, 'Data');
  257. % Update table data according to display settings
  258. for i = 1:numel(obj.handles.hSVPS.Geometry.ROIS)
  259. tdata{i, 1} = obj.handles.hSVPS.Geometry.ROIS{i}.visible;
  260. c = obj.handles.hSVPS.Geometry.ROIS{i}.color;
  261. tdata{i, 2} = sprintf('<html><span style="color:%s">&#x25A0</span></html>', rgb2str(c));
  262. tdata{i, 3} = obj.handles.hSVPS.Geometry.ROIS{i}.name;
  263. end
  264. set(obj.handles.hROITable, 'Data', tdata);
  265. drawnow;
  266. obj.set_layout();
  267. end
  268. %-------------------------------------------------------------------------------
  269. function updateDoseTable(obj)
  270. % Update DoseTable using data in SliceViewerPanelSource.dosedisp
  271. % if isempty(obj.handles.hSVPS.Geometry) && isempty(obj.handles.hSVPS.optResults); return; end
  272. tdata = cell(0);
  273. % Update table data according to display settings
  274. for i = 1:numel(obj.handles.hSVPS.dosedisp.level)
  275. tdata{i, 1} = sprintf('<html><span style="color:%s">&#x25A0</span></html>', rgb2str(obj.handles.hSVPS.dosedisp.color(i,:)));
  276. tdata{i, 2} = obj.handles.hSVPS.dosedisp.level(i);
  277. tdata{i, 3} = obj.handles.hSVPS.dosedisp.alpha(i);
  278. end
  279. % disable the callback to prevent recursion
  280. set(obj.handles.hDoseTable, 'CellSelectionCallback', []);
  281. set(obj.handles.hDoseTable, 'Data', tdata);
  282. drawnow; % Flush //IMPORTANT
  283. set(obj.handles.hDoseTable, 'CellSelectionCallback', @obj.DoseTable_CellSelectionCallback);
  284. end
  285. %-------------------------------------------------------------------------------
  286. function updateCTWindowControls(obj)
  287. set(obj.handles.hWWSlider, 'Value', obj.handles.hSVPS.ctWindow.ww);
  288. set(obj.handles.hWWEdit, 'String', num2str(obj.handles.hSVPS.ctWindow.ww));
  289. set(obj.handles.hWLSlider, 'Value', obj.handles.hSVPS.ctWindow.wl);
  290. set(obj.handles.hWLEdit, 'String', num2str(obj.handles.hSVPS.ctWindow.wl));
  291. end
  292. %-------------------------------------------------------------------------------
  293. function ROITable_CellSelectionCallback(obj, hObject, eventdata)
  294. % eventdata structure with the following fields (see UITABLE)
  295. % Indices: row and column indices of the cell(s) currently selecteds
  296. % Avoid "fake" event during initialization
  297. if ~isempty(eventdata.Indices)
  298. roi_idx = eventdata.Indices(1);
  299. if eventdata.Indices(2) == 1
  300. % toggle ROI display in display settings
  301. tdata = get(hObject, 'Data');
  302. obj.handles.hSVPS.Geometry.ROIS{roi_idx}.visible = ~tdata{roi_idx, 1};
  303. else % eventdata.Indices(2) ~= 1
  304. % pick and save color
  305. c = uisetcolor;
  306. if ~(isscalar(c) && c == 0)
  307. obj.handles.hSVPS.Geometry.ROIS{roi_idx}.color = c;
  308. % also make it visible
  309. obj.handles.hSVPS.Geometry.ROIS{roi_idx}.visible = true;
  310. end
  311. end
  312. obj.updateROITable();
  313. end
  314. end
  315. %-------------------------------------------------------------------------------
  316. function DoseTable_CellSelectionCallback(obj, tilde, eventdata)
  317. % eventdata structure with the following fields (see UITABLE)
  318. % Indices: row and column indices of the cell(s) currently selecteds
  319. % Avoid fake event during initialization
  320. if ~isempty(eventdata.Indices)
  321. if eventdata.Indices(2) == 1
  322. % pick and save color
  323. c = uisetcolor;
  324. if ~(isscalar(c) && c == 0)
  325. obj.handles.hSVPS.dosedisp.color(eventdata.Indices(1), :) = c;
  326. obj.updateDoseTable();
  327. end
  328. end
  329. end
  330. end
  331. %-------------------------------------------------------------------------------
  332. function DoseTable_CellEditCallback(obj, tilde, eventdata)
  333. % Update handles.hSVPS.dosedisp
  334. if eventdata.Indices(2) == 2
  335. obj.handles.hSVPS.dosedisp.level(eventdata.Indices(1)) = eventdata.NewData;
  336. elseif eventdata.Indices(2) == 3
  337. obj.handles.hSVPS.dosedisp.alpha(eventdata.Indices(1)) = eventdata.NewData;
  338. end
  339. obj.updateDoseTable();
  340. end
  341. %-------------------------------------------------------------------------------
  342. function WWSlider_Callback(obj, src, evt)
  343. % set window width property of SliceViewerPancelSource
  344. value = round(get(src, 'Value'));
  345. obj.handles.hSVPS.ctWindow.ww = value;
  346. obj.updateCTWindowControls();
  347. end
  348. %-------------------------------------------------------------------------------
  349. function WWEdit_Callback(obj, src, evt)
  350. % set window width property of SliceViewerPancelSource
  351. value = round(str2double(get(src, 'String')));
  352. % clipping to valid value
  353. value = max(value, get(obj.handles.hWWSlider, 'Min'));
  354. value = min(value, get(obj.handles.hWWSlider, 'Max'));
  355. obj.handles.hSVPS.ctWindow.ww = value;
  356. obj.updateCTWindowControls();
  357. end
  358. %-------------------------------------------------------------------------------
  359. function WLSlider_Callback(obj, src, evt)
  360. % set window width property of SliceViewerPancelSource
  361. value = round(get(src, 'Value'));
  362. obj.handles.hSVPS.ctWindow.wl = value;
  363. obj.updateCTWindowControls();
  364. end
  365. %-------------------------------------------------------------------------------
  366. function WLEdit_Callback(obj, src, evt)
  367. % set window width property of SliceViewerPancelSource
  368. value = round(str2double(get(src, 'String')));
  369. % clipping to valid value
  370. value = max(value, get(obj.handles.hWLSlider, 'Min'));
  371. value = min(value, get(obj.handles.hWLSlider, 'Max'));
  372. obj.handles.hSVPS.ctWindow.wl = value;
  373. obj.updateCTWindowControls();
  374. end
  375. %-------------------------------------------------------------------------------
  376. function ImportDicomRTMenu_Callback(obj, src, evt)
  377. [obj.handles.hSVPS.Geometry obj.patient_dir] = RDXTPS_geometry_setup_wizard_ASP();
  378. obj.load_geometry();
  379. obj.handles.hSVPS.Geometry.patient_dir
  380. a=obj.handles.hSVPS.Geometry;
  381. disp(1)
  382. end
  383. %-------------------------------------------------------------------------------
  384. % Grozomah
  385. function NRRD1_Callback(obj, src, evt)
  386. [obj.handles.hSVPS.Geometry] = load2geometry();
  387. obj.load_geometry();
  388. [obj.handles.hSVPS.Geometry] = nrrd2ROI(obj.handles.hSVPS.Geometry);
  389. obj.load_geometry();
  390. disp('New ROI loaded!')
  391. end
  392. %-------------------------------------------------------------------------------
  393. % Grozomah
  394. function NRRD2_Callback(obj, src, evt)
  395. [obj.handles.hSVPS.Geometry] = nrrd2ROI(obj.handles.hSVPS.Geometry);
  396. obj.load_geometry();
  397. disp('New ROI loaded!')
  398. end
  399. %-------------------------------------------------------------------------------
  400. % Grozomah
  401. function ImgDownsample_Callback(obj, src, evt)
  402. disp('Making downsampled geometry')
  403. make_downsampled_Geometry(obj.handles.hSVPS.Geometry)
  404. end
  405. %-------------------------------------------------------------------------------
  406. function DosecalcMenu_Callback(obj, src, evt)
  407. % TODO fix patient_dir system
  408. % [obj.dosecalcSetup obj.patient_dir] = RDXTPS_dosecalcSetup(obj.dosecalcSetup, obj.patient_dir, obj.handles.hSVPS.Geometry);
  409. % [obj.dosecalcSetup obj.patient_dir] = RDXTPS_dosecalcSetup(obj.dosecalcSetup, [], obj.handles.hSVPS.Geometry);
  410. %
  411. % % Temporary fix for Gustavo asking energy limit
  412. % answer = inputdlg('Machine energy limit: (MeV)', 'Energy limit', 1, {'250'});
  413. % maxMachineEnergyStr = answer{1};
  414. %
  415. % % run beamlet dose calculation
  416. % cmdline = ['.' filesep 'RDXdosecalc.exe "' fullfile(obj.patient_dir, 'dosecalc_input.txt') '"' ' ' maxMachineEnergyStr];
  417. % % Grozomah ##
  418. % % cmdline = ['.' filesep 'RDXoptimizer.exe "' fullfile(obj.patient_dir, 'dosecalc_input.txt') '"'];
  419. %
  420. % if ispc
  421. % execline = ['cmd /c ' cmdline ' &'];
  422. % elseif isunix
  423. % % xterm -e "export LD_LIBRARY_PATH=. ; ./RDXdosecalc.exe '../patients/Knew/dosecalc_input.txt' ; read" &
  424. % % TODO fix single quote
  425. % cmdline = ['xterm -e "export LD_LIBRARY_PATH=. ; ./RDXdosecalc.exe ' fullfile(obj.patient_dir, 'dosecalc_input.txt') ' ; read" &'];
  426. % end
  427. % msgbox(sprintf('Start beamlet dose calculation.\nRun this if not started automatically:\n%s', cmdline));
  428. % system(execline);
  429. obj.handles.hSVPS.Geometry.num_batches = helicalDosecalcSetup7(obj.handles.hSVPS.Geometry.patient_dir)
  430. end
  431. %-------------------------------------------------------------------------------
  432. % Grozomah
  433. function MergeBeamlets_Callback(obj, src, evt)
  434. if isfield(obj.handles.hSVPS.Geometry, 'num_batches')
  435. merge_beamlets(obj.handles.hSVPS.Geometry.num_batches, obj.handles.hSVPS.Geometry.patient_dir);
  436. else
  437. % num_batches = 1;
  438. str = input('Enter num of beam batches: ','s');
  439. merge_beamlets(str2double(str), obj.handles.hSVPS.Geometry.patient_dir);
  440. end
  441. disp('Beamlets merged!')
  442. end
  443. %-------------------------------------------------------------------------------
  444. function OptimMenu_Callback(obj, src, evt)
  445. % TODO fix patient_dir system
  446. % if isempty(obj.patient_dir)
  447. load('WiscPlan_preferences.mat')
  448. obj.patient_dir = uigetdir([WiscPlan_preferences.patientDataPath], 'Select the patient directory');
  449. % end
  450. source_filename = fullfile(obj.patient_dir, 'batch_dose.bin');
  451. target_filename = fullfile(obj.patient_dir, 'beamlet_batch_files', 'beamletbatch0.bin');
  452. mkdir(fullfile(obj.patient_dir, 'beamlet_batch_files'));
  453. try
  454. if ispc % MATLAB movefile() super slow on windows
  455. % TODO confirm overwrite
  456. % system(['move "' source_filename '" "' target_filename '"']);
  457. system(['copy "' source_filename '" "' target_filename '"']);
  458. else
  459. movefile(source_filename, target_filename);
  460. end
  461. catch
  462. msgbox('Error moving the beamlet file. Please do so manually.');
  463. end
  464. Nbeamlets = read_ryan_beamlets(target_filename, 'info');
  465. RDXTPS_optimSetup(Nbeamlets, obj.patient_dir, obj.handles.hSVPS.Geometry);
  466. % run optimizer
  467. %---
  468. cd('C:\010-work\003_localGit\WiscPlan_v2\WiscPlanPhotonkV125\WiscPlanEXE')
  469. cmdline = ['.' filesep 'RDXoptimizer.exe "' fullfile(obj.patient_dir, 'optInput.txt') '"'];
  470. % msgbox(sprintf('Start optimization\nRun this if not started automatically:\n%s', cmdline));
  471. if ispc
  472. execline = ['cmd /c ' cmdline ' &'];
  473. elseif isunix
  474. % TODO fix single quote
  475. execline = [cmdline];
  476. end
  477. system(execline);
  478. end
  479. %-------------------------------------------------------------------------------
  480. function SaveOptimResultsMenu_Callback(obj, src, evt)
  481. % TODO fix patient_dir system
  482. % if isempty(obj.patient_dir)
  483. load('WiscPlan_preferences.mat')
  484. obj.patient_dir = uigetdir([WiscPlan_preferences.patientDataPath], 'Select the patient directory');
  485. % obj.patient_dir = uifile('getdir', 'Select the patient directory');
  486. % end
  487. [tilde, obj.handles.hSVPS.optResults] = loadOptResults(obj.patient_dir, 'optInput.txt');
  488. obj.load_optResults();
  489. end
  490. %-------------------------------------------------------------------------------
  491. % Grozomah
  492. function GetFullDose_Callback(obj, src, evt)
  493. get_full_dose([obj.patient_dir])
  494. disp('Full dose calculated.')
  495. end
  496. %-------------------------------------------------------------------------------
  497. % Grozomah
  498. function RO_GoalSpec(obj, src, evt)
  499. disp('RO Goal spec called!')
  500. goal_def_UI
  501. end
  502. %-------------------------------------------------------------------------------
  503. % Grozomah
  504. function RO_Optimize(obj, src, evt)
  505. disp('RO Optimization called!')
  506. Pat_path = [obj.handles.hSVPS.Geometry.patient_dir];
  507. [Obj_file,Obj_path,indx] = uigetfile([obj.handles.hSVPS.Geometry.patient_dir '\matlab_files\*.mat'], 'Select OptGoal file' );
  508. path2goal = [Obj_path, Obj_file];
  509. [D_full, w_fin, Geometry, optGoal] = NLP_optimizer_v3(Pat_path, path2goal);
  510. % This part needs work still
  511. % obj.handles.hSVPS.optResults = optResults;
  512. end
  513. function RO_fullDose(obj, src, evt)
  514. disp('Full dose calculation called ...')
  515. Pat_path = [obj.handles.hSVPS.Geometry.patient_dir];
  516. [NLP_file,NLP_path,indx] = uigetfile([obj.handles.hSVPS.Geometry.patient_dir '\matlab_files\*.mat'], 'Select NLP file' );
  517. NLP_getFullDose(NLP_path, NLP_file);
  518. disp('Full dose calculation completed!')
  519. end
  520. %-------------------------------------------------------------------------------
  521. function LoadGeometryMenu_Callback(obj, src, evt)
  522. [filename pathname] = uifile('get', '*.mat', 'Choose Geometry file to load');
  523. % do nothing if cancelled
  524. if isscalar(filename) && filename == 0; return; end
  525. obj.load_geometry(fullfile(pathname, filename));
  526. end
  527. %-------------------------------------------------------------------------------
  528. function LoadOptresultsMenu_Callback(obj, src, evt)
  529. load('WiscPlan_preferences.mat')
  530. [FileName,PathName,FilterIndex] = uigetfile([WiscPlan_preferences.patientDataPath '\*.mat'], 'Choose OptResults file to load');
  531. % [filename pathname] = uifile('get', '*.mat', 'Choose OptResults file to load');
  532. % do nothing if cancelled
  533. if isscalar(FileName) && FileName == 0; return; end
  534. obj.load_optResults(fullfile(PathName, FileName));
  535. % % Extra dose scaling. Temporary code for comparison with Tomo plans
  536. % answer = inputdlg({'Dose', '% Volume'}, 'Scale final dose');
  537. % if ~isempty(answer)
  538. % % choose target
  539. % ROI_names = cellfun(@(c)c.name, obj.handles.hSVPS.Geometry.ROIS, 'UniformOutput', false);
  540. % [target_idx] = listdlg('ListString', ROI_names, ...
  541. % 'SelectionMode', 'single', 'Name', 'Target Selection', ...
  542. % 'PromptString', 'Please select the target ROI.');
  543. % opt_dose = prctile(obj.handles.hSVPS.optResults.dose{end}(obj.handles.hSVPS.Geometry.ROIS{target_idx}.ind), 100 - str2double(answer{2}));
  544. % obj.handles.hSVPS.optResults.dose{end} = obj.handles.hSVPS.optResults.dose{end} * str2double(answer{1}) / opt_dose;
  545. % end
  546. % % END OF Extra dose scaling
  547. msgbox('Load optResults successfully.')
  548. end
  549. %-------------------------------------------------------------------------------
  550. function ExportPinnacleMenu_Callback(obj, src, evt)
  551. if isempty(obj.handles.hSVPS.Geometry)
  552. obj.LoadGeometryMenu_Callback([], [], obj);
  553. end
  554. if isempty(obj.handles.hSVPS.optResults)
  555. obj.LoadOptresultsMenu_Callback([], [], obj);
  556. end
  557. RDXTPS_exportPinnGrid4Tomo(obj.handles.hSVPS.Geometry, obj.handles.hSVPS.optResults);
  558. end
  559. %-------------------------------------------------------------------------------
  560. function PlotDVHMenu_Callback(obj, src, evt)
  561. answer = inputdlg({'Enter figure number:', 'Enter line style'}, 'Choose figure');
  562. if isempty(answer{1})
  563. return;
  564. else
  565. fig_idx = str2double(answer{1});
  566. end
  567. if isempty(answer{2})
  568. line_style = '-';
  569. else
  570. line_style = answer{2};
  571. end
  572. figure(fig_idx); hold on;
  573. % temp solution for karthik dosimetry data
  574. answer = inputdlg('number of fractions');
  575. nfrac = str2double(answer{1});
  576. for roi_idx = 1:numel(obj.handles.hSVPS.Geometry.ROIS)
  577. if obj.handles.hSVPS.Geometry.ROIS{roi_idx}.visible == true % display == ON
  578. fprintf('%s\n', obj.handles.hSVPS.Geometry.ROIS{roi_idx}.name);
  579. % [dvh dosebins] = dvhist(obj.handles.hSVPS.optResults.dose{end}, obj.handles.hSVPS.Geometry.ROIS{roi_idx}.ind);
  580. % temp solution for karthik dosimetry data
  581. [dvh dosebins] = dvhist(obj.handles.hSVPS.optResults.dose{end}, ...
  582. obj.handles.hSVPS.Geometry, ...
  583. roi_idx, ...
  584. nfrac);
  585. plot(dosebins, dvh, ...
  586. 'Color', obj.handles.hSVPS.Geometry.ROIS{roi_idx}.color, ...
  587. 'LineStyle', line_style, ...
  588. 'DisplayName', obj.handles.hSVPS.Geometry.ROIS{roi_idx}.name);
  589. end
  590. end
  591. set(gca, 'XMinorTick', 'on', 'YMinorTick', 'on');
  592. grid(gca, 'on');
  593. box(gca, 'on');
  594. legend('show');
  595. xlabel('Dose (Gy)');
  596. ylabel('% volume');
  597. hold off;
  598. end
  599. %-------------------------------------------------------------------------------
  600. function ResampleGeometry_Callback(obj, src, evt)
  601. % call the function to create a resampled geometry and save it
  602. % in a separate folder
  603. resample_geometry(obj.handles.hSVPS.Geometry)
  604. disp('Resampled geometry created!')
  605. end
  606. %-------------------------------------------------------------------------------
  607. function FullRO_beamCalc_Callback(obj, src, evt)
  608. % call the function to start beamlet calculations for all
  609. % scenarios
  610. disp('Full RO beamcalc called!')
  611. fullRO_beamCalc(obj.handles.hSVPS.Geometry)
  612. end
  613. %-------------------------------------------------------------------------------
  614. function hFullRO_mergeBeams_Callback(obj, src, evt)
  615. % loop through the scenario folders and merge and shift
  616. % beamlets
  617. disp('Merging full RO beamlets')
  618. fullRO_mergeBeams(obj.handles.hSVPS.Geometry)
  619. end
  620. %-------------------------------------------------------------------------------
  621. function FullRO_Opt_Callback(obj, src, evt)
  622. % call function to do full RO optimization based on multiple
  623. % beamlets
  624. disp('Full RO optimization called')
  625. Pat_path = [obj.handles.hSVPS.Geometry.patient_dir];
  626. [Obj_file,Obj_path,indx] = uigetfile([obj.handles.hSVPS.Geometry.patient_dir '\matlab_files\*.mat'], 'Select OptGoal file' );
  627. path2goal = [Obj_path, Obj_file];
  628. path2scBeams = uigetdir([obj.handles.hSVPS.Geometry.patient_dir ], 'Select folder with scenario beamlets');
  629. [D_full, w_fin, Geometry, optGoal] = FullRO_NLP_optimizer_v3(Pat_path, path2goal, path2scBeams);
  630. end
  631. %-------------------------------------------------------------------------------
  632. function DoseModeDropdown_Callback(obj, src, evt)
  633. str = get(obj.handles.hDoseModeDropdown, 'String');
  634. obj.handles.hSVPS.dosedisp.mode = str{get(obj.handles.hDoseModeDropdown, 'Value')};
  635. end
  636. %-------------------------------------------------------------------------------
  637. function TCSShow_Callback(obj, src, evt)
  638. obj.handles.hSVPS.TCSVisible = ...
  639. get(src, 'Value') == get(src, 'Max');
  640. end
  641. %-------------------------------------------------------------------------------
  642. function SaveAsGeometryMenu_Callback(obj, src, evt)
  643. [filename pathname] = uifile('put', '*.mat', 'Save Geometry as...');
  644. % do nothing if cancelled
  645. if isscalar(filename) && filename == 0; return; end
  646. Geometry = obj.handles.hSVPS.Geometry;
  647. for i = 1:numel(Geometry.ROIS)
  648. if isfield(Geometry.ROIS{i}, 'BW')
  649. Geometry.ROIS{i}.BW = [];
  650. end
  651. end
  652. save(fullfile(pathname, filename), 'Geometry');
  653. end
  654. %-------------------------------------------------------------------------------
  655. function SaveAsOptResultsMenu_Callback(obj, src, evt)
  656. [filename pathname] = uifile('put', '*.mat', 'Save OptResults as...');
  657. % do nothing if cancelled
  658. if isscalar(filename) && filename == 0; return; end
  659. optResults = obj.handles.hSVPS.optResults;
  660. save(fullfile(pathname, filename), 'optResults');
  661. end
  662. %-------------------------------------------------------------------------------
  663. end % methods
  664. end