XTPS.m 33 KB

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