XTPS.m 38 KB

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