XTPS.m 30 KB

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