XTPS.m 35 KB

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