SliceViewerPanel.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. classdef SliceViewerPanel < handle
  2. %SliceViewerPanel uipanel viewer to SliceViewerPanelSource
  3. %
  4. % SliceViewerPanel plots the content from SliceViewerPanelSource (SVPS).
  5. %
  6. % Coordinate/Orientation in detail:
  7. % zOrient = 1: Coronal, abscissa = dim3 = zmesh, ordinate = dim2 = ymesh
  8. % zOrient = 2: Sagittal, abscissa = dim3 = zmesh, ordinate = dim1 = xmesh
  9. % zOrient = 3: Transverse, abscissa = dim1 = xmesh, ordinate = dim2 = ymesh
  10. % or think as this:
  11. % zOrient = 1: image(zmesh, ymesh, Geometry.rhomw(slice,:,:))
  12. % zOrient = 2: image(zmesh, xmesh, Geometry.rhomw(:,slice,:))
  13. % zOrient = 3: image(xmesh, ymesh, Geometry.rhomw(:,:,slice))
  14. %
  15. % See also XTPS
  16. %
  17. % Author: Xiaohu Mo
  18. %===============================================================================
  19. properties
  20. hSVPS % handle to SliceViewerPanelSource
  21. zOrien % scroll along which dimension: 1=S; 2=C; 3=T
  22. % TODO should be private
  23. % handles to gui objects
  24. hPanel
  25. hAxis
  26. hSlider
  27. hEdit
  28. hLineH % horizontal TCS line
  29. hLineV % vertical TCS line
  30. hImage % image has CT and dose overlayed manually
  31. hCbar % colorbar handle
  32. hROIs % handle vector of overlay ROIs
  33. hDoseContours % handle vector of overlay isodose lines
  34. hDoseLegend % handle of legend of isodose lines
  35. end
  36. %===============================================================================
  37. %===============================================================================
  38. methods
  39. %-------------------------------------------------------------------------------
  40. function obj = SliceViewerPanel(hSVPS, zOrien, hPanelParent) % Constructor returns object
  41. obj.hSVPS = hSVPS;
  42. obj.zOrien = zOrien;
  43. obj.hPanel = uipanel('Parent', hPanelParent);
  44. obj.hAxis = axes('Parent', obj.hPanel);
  45. obj.hSlider = uicontrol(obj.hPanel, 'Style','slider');
  46. obj.hEdit = uicontrol(obj.hPanel, 'Style','edit');
  47. obj.hImage = image('Parent', obj.hAxis);
  48. obj.hLineH = line('Parent', obj.hAxis, 'XData', [], 'YData', []);
  49. obj.hLineV = line('Parent', obj.hAxis, 'XData', [], 'YData', []);
  50. obj.hCbar = [];
  51. obj.hDoseContours = [];
  52. obj.hDoseLegend = [];
  53. % Callback passes 3 args: (obj, src, evt)
  54. set(obj.hSlider, 'Callback', @obj.Slider_Callback);
  55. set(obj.hEdit, 'Callback', @obj.Edit_Callback);
  56. % Properties
  57. axis(obj.hAxis, 'off');
  58. set(obj.hImage, 'CData', [], 'CDataMapping', 'scaled');
  59. set(obj.hLineH, 'HitTest', 'off', 'Color', 'red');
  60. set(obj.hLineV, 'HitTest', 'off', 'Color', 'red');
  61. set([obj.hPanel obj.hSlider obj.hEdit], ...
  62. 'Background', get(get(obj.hPanel, 'Parent'), 'Color'));
  63. % Add Listener
  64. addlistener(obj.hSVPS, 'POI', 'PostSet', @obj.handleEvent);
  65. addlistener(obj.hSVPS, 'Geometry', 'PostSet', @obj.handleEvent);
  66. addlistener(obj.hSVPS, 'dosedisp', 'PostSet', @obj.handleEvent);
  67. addlistener(obj.hSVPS, 'ctWindow', 'PostSet', @obj.handleEvent);
  68. addlistener(obj.hSVPS, 'TCSVisible', 'PostSet', @obj.handleEvent);
  69. end % function SliceViewerPanel
  70. %-------------------------------------------------------------------------------
  71. function set_layout(obj, src, evt)
  72. % Set widget positions and sizes
  73. set([obj.hPanel obj.hAxis obj.hSlider obj.hEdit], 'Unit', 'normalized');
  74. padding = [0.01 0.01];
  75. setPositionXY(obj.hAxis, [0 1], [0.1 1], padding);
  76. setPositionXY(obj.hSlider, [0 0.8], [0 0.1], padding, 1.5, 'characters');
  77. setPositionXY(obj.hEdit, [0.8 1], [0 0.1], padding, 1.5, 'characters');
  78. end
  79. %-------------------------------------------------------------------------------
  80. function handleEvent(obj, src, evt)
  81. switch src.Name
  82. case 'POI'
  83. obj.redraw_lines();
  84. % redraw only changed dimension
  85. if obj.hSVPS.POI(obj.zOrien) ~= obj.hSVPS.prev_POI(obj.zOrien)
  86. obj.redraw_image();
  87. obj.redraw_contour();
  88. end
  89. case {'Geometry' 'ctWindow' 'ROIdisp' 'dosedisp'}
  90. % redraw all
  91. obj.redraw_image();
  92. obj.redraw_contour();
  93. case 'TCSVisible'
  94. % redraw line
  95. obj.redraw_lines();
  96. end
  97. end
  98. %-------------------------------------------------------------------------------
  99. function Slider_Callback(obj, src, evt)
  100. % check value, update Edit and Axis
  101. value = round(get(obj.hSlider, 'Value'));
  102. set(obj.hSlider, 'Value', value);
  103. set(obj.hEdit, 'String', num2str(value));
  104. obj.hSVPS.POI(obj.zOrien) = value;
  105. end % function Slider_Callback
  106. %-------------------------------------------------------------------------------
  107. function Edit_Callback(obj, src, evt)
  108. % check value, update Slider and Axis
  109. value = round(str2double(get(obj.hEdit, 'String')));
  110. if value >= get(obj.hSlider, 'Min') && value <= get(obj.hSlider, 'Max')
  111. set(obj.hSlider, 'Value', value);
  112. set(obj.hEdit, 'String', num2str(value));
  113. obj.hSVPS.POI(obj.zOrien) = value;
  114. else % invalid input, set back to slider value
  115. value = get(obj.hSlider, 'Value');
  116. set(obj.hEdit, 'String', num2str(value));
  117. end
  118. end % function Edit_Callback
  119. %-------------------------------------------------------------------------------
  120. function load_data(obj)
  121. set(obj.hSlider, 'Min', 1, 'Max', size(obj.hSVPS.Geometry.rhomw, obj.zOrien), 'Value', 1);
  122. set(obj.hSlider, 'SliderStep', [1 10] / size(obj.hSVPS.Geometry.rhomw, obj.zOrien));
  123. % set image coordinates, axis limits, DataAspectRatio
  124. switch obj.zOrien
  125. case 1
  126. set(obj.hImage, 'XData', obj.hSVPS.Geometry.z, 'YData', obj.hSVPS.Geometry.y);
  127. set(obj.hAxis, 'XLim', [min(obj.hSVPS.Geometry.z) max(obj.hSVPS.Geometry.z)]);
  128. set(obj.hAxis, 'YLim', [min(obj.hSVPS.Geometry.y) max(obj.hSVPS.Geometry.y)]);
  129. zoom(obj.hAxis, 'reset');
  130. daspect(obj.hAxis, [obj.hSVPS.Geometry.voxel_size(3) obj.hSVPS.Geometry.voxel_size(2) 1]);
  131. case 2
  132. set(obj.hImage, 'XData', obj.hSVPS.Geometry.z, 'YData', obj.hSVPS.Geometry.x);
  133. set(obj.hAxis, 'XLim', [min(obj.hSVPS.Geometry.z) max(obj.hSVPS.Geometry.z)]);
  134. set(obj.hAxis, 'YLim', [min(obj.hSVPS.Geometry.x) max(obj.hSVPS.Geometry.x)]);
  135. zoom(obj.hAxis, 'reset');
  136. daspect(obj.hAxis, [obj.hSVPS.Geometry.voxel_size(3) obj.hSVPS.Geometry.voxel_size(1) 1]);
  137. set(obj.hAxis, 'YDir', 'reverse');
  138. case 3
  139. set(obj.hImage, 'XData', obj.hSVPS.Geometry.x, 'YData', obj.hSVPS.Geometry.y);
  140. set(obj.hAxis, 'XLim', [min(obj.hSVPS.Geometry.x) max(obj.hSVPS.Geometry.x)]);
  141. set(obj.hAxis, 'YLim', [min(obj.hSVPS.Geometry.y) max(obj.hSVPS.Geometry.y)]);
  142. zoom(obj.hAxis, 'reset');
  143. daspect(obj.hAxis, [obj.hSVPS.Geometry.voxel_size(1) obj.hSVPS.Geometry.voxel_size(2) 1]);
  144. set(obj.hAxis, 'YDir', 'reverse');
  145. end
  146. % default to volume center
  147. obj.hSVPS.POI = round(size(obj.hSVPS.Geometry.rhomw)/2);
  148. % set slider and edit control when manually set focus point
  149. slice = obj.hSVPS.POI(obj.zOrien);
  150. set(obj.hSlider, 'Value', slice);
  151. set(obj.hEdit, 'String', slice);
  152. end % function load_data
  153. %-------------------------------------------------------------------------------
  154. function updateColorbar(obj)
  155. % only plot color on transvers axis
  156. if obj.zOrien ~= 3; return; end
  157. % delete old one
  158. delete(obj.hCbar); obj.hCbar = [];
  159. switch lower(obj.hSVPS.dosedisp.mode)
  160. case {'isodose' 'isodoseline'}
  161. % get a sorted, valid dose level and colors
  162. valid_indices = find(obj.hSVPS.dosedisp.level > 0);
  163. if isempty(valid_indices); return; end
  164. dose_levels = obj.hSVPS.dosedisp.level(valid_indices);
  165. dose_colors = obj.hSVPS.dosedisp.color(valid_indices,:);
  166. [dose_levels iX] = sort(dose_levels);
  167. dose_labels = arrayfun(@(d)num2str(d), dose_levels, 'UniformOutput', false)';
  168. cmap = dose_colors(iX, :);
  169. colormap(cmap);
  170. set(obj.hAxis, 'CLim', [0 1]);
  171. obj.hCbar = colorbar('Peer', obj.hAxis, 'Location', 'SouthOutside');
  172. set(obj.hCbar, 'TickLength', [0 0], ...
  173. 'XTick', (0.5:numel(dose_levels)-0.5)/numel(dose_levels), ...
  174. 'XTickLabel', dose_labels);
  175. case 'colorwash'
  176. colormap(jet(256));
  177. set(obj.hAxis, 'CLim', [0 max(obj.hSVPS.optResults.dose{end}(:))]);
  178. obj.hCbar = colorbar('Peer', obj.hAxis, 'Location', 'SouthOutside');
  179. end
  180. end % function updateColorbar
  181. %-------------------------------------------------------------------------------
  182. function updateAxis(obj)
  183. % save axis scaling
  184. % xlim = get(obj.hAxis, 'XLim');
  185. % ylim = get(obj.hAxis, 'YLim');
  186. % % redraw more if slice changed
  187. % if obj.hSVPS.POI(obj.zOrien) ~= obj.hSVPS.prev_POI(obj.zOrien)
  188. % obj.redraw_image();
  189. % obj.redraw_contour();
  190. % end
  191. % obj.redraw_lines();
  192. % restore axis scaling
  193. % set(obj.hAxis, 'XLim', xlim);
  194. % set(obj.hAxis, 'YLim', ylim);
  195. end
  196. %-------------------------------------------------------------------------------
  197. function redraw_lines(obj)
  198. % P1 & P2 are the extent of the grid
  199. P1 = [min(obj.hSVPS.Geometry.y) min(obj.hSVPS.Geometry.x) min(obj.hSVPS.Geometry.z)];
  200. P2 = [max(obj.hSVPS.Geometry.y) max(obj.hSVPS.Geometry.x) max(obj.hSVPS.Geometry.z)];
  201. % F is the coordinate of the intersection of TCS planes
  202. F = [obj.hSVPS.Geometry.y(obj.hSVPS.POI(1)) ...
  203. obj.hSVPS.Geometry.x(obj.hSVPS.POI(2)) ...
  204. obj.hSVPS.Geometry.z(obj.hSVPS.POI(3))];
  205. switch obj.zOrien
  206. case 1
  207. set(obj.hLineH, 'XData', [P1(3) P2(3)], 'YData', [F(2) F(2)]);
  208. set(obj.hLineV, 'XData', [F(3) F(3)], 'YData', [P1(2) P2(2)]);
  209. case 2
  210. set(obj.hLineH, 'XData', [P1(3) P2(3)], 'YData', [F(1) F(1)]);
  211. set(obj.hLineV, 'XData', [F(3) F(3)], 'YData', [P1(1) P2(1)]);
  212. case 3
  213. set(obj.hLineH, 'XData', [P1(2) P2(2)], 'YData', [F(1) F(1)]);
  214. set(obj.hLineV, 'XData', [F(2) F(2)], 'YData', [P1(1) P2(1)]);
  215. end
  216. % Set visibility
  217. if obj.hSVPS.TCSVisible == true
  218. set(obj.hLineH, 'Visible', 'on');
  219. set(obj.hLineV, 'Visible', 'on');
  220. else
  221. set(obj.hLineH, 'Visible', 'off');
  222. set(obj.hLineV, 'Visible', 'off');
  223. end
  224. end
  225. %-------------------------------------------------------------------------------
  226. function redraw_image(obj)
  227. slice = obj.hSVPS.POI(obj.zOrien);
  228. % get CT image
  229. switch obj.zOrien
  230. case 1
  231. ctImage = imagescX((obj.hSVPS.Geometry.data(slice,:,:)), gray(256), obj.hSVPS.ctClim);
  232. case 2
  233. ctImage = imagescX((obj.hSVPS.Geometry.data(:,slice,:)), gray(256), obj.hSVPS.ctClim);
  234. case 3
  235. ctImage = imagescX((obj.hSVPS.Geometry.data(:,:,slice)), gray(256), obj.hSVPS.ctClim);
  236. end
  237. % set image to CT. Will re-set if need to blend with dose later.
  238. set(obj.hImage, 'CData',ctImage);
  239. % redraw dose
  240. delete(obj.hDoseContours);
  241. obj.hDoseContours = [];
  242. if ~isempty(obj.hSVPS.optResults)
  243. switch obj.zOrien
  244. case 1
  245. doseSlice = squeeze(obj.hSVPS.optResults.dose{end}(slice,:,:));
  246. xaxis = obj.hSVPS.Geometry.z;
  247. yaxis = obj.hSVPS.Geometry.y;
  248. case 2
  249. doseSlice = squeeze(obj.hSVPS.optResults.dose{end}(:,slice,:));
  250. xaxis = obj.hSVPS.Geometry.z;
  251. yaxis = obj.hSVPS.Geometry.x;
  252. case 3
  253. doseSlice = squeeze(obj.hSVPS.optResults.dose{end}(:,:,slice));
  254. xaxis = obj.hSVPS.Geometry.x;
  255. yaxis = obj.hSVPS.Geometry.y;
  256. end
  257. switch lower(obj.hSVPS.dosedisp.mode)
  258. case {'isodoseline'}
  259. if ~isempty(nonzeros(obj.hSVPS.dosedisp.level))
  260. hold(obj.hAxis, 'on');
  261. % sort dose levels before contourf
  262. [tilde, sorted_indices]= sort(obj.hSVPS.dosedisp.level);
  263. for idx = sorted_indices(:)'
  264. % scalar of current dose level
  265. dose_level = obj.hSVPS.dosedisp.level(idx);
  266. if dose_level ~= 0
  267. % plot this contour level and append handles to list
  268. % [level level] is used to avoid single level ambiguity of contour
  269. [tilde, obj.hDoseContours(end+1)] = contour(obj.hAxis, ...
  270. xaxis, yaxis, ...
  271. doseSlice, [dose_level dose_level], ...
  272. '-', 'LineWidth', 2, ...
  273. 'LineColor', obj.hSVPS.dosedisp.color(idx, :), ...
  274. 'DisplayName', sprintf('%.1f', dose_level));
  275. end
  276. end
  277. hold(obj.hAxis, 'off');
  278. end
  279. case 'isodose'
  280. % Using transparency with contourf like this fails due to the overlay of patches:
  281. % hPatches = findobj(obj.hDoseContours(end), 'Type', 'patch', '-property', 'FaceAlpha');
  282. % set(hPatches, 'FaceAlpha', obj.hSVPS.dosedisp.alpha(idx));
  283. if ~isempty(nonzeros(obj.hSVPS.dosedisp.level))
  284. doseMap = ones(size(doseSlice));
  285. alphaMap = zeros(size(doseSlice));
  286. % sort dose levels
  287. [tilde, sorted_indices]= sort(obj.hSVPS.dosedisp.level);
  288. for idx = sorted_indices(:)'
  289. % scalar of current dose level
  290. dose_level = obj.hSVPS.dosedisp.level(idx);
  291. % pixels below cmin will have alpha = 0
  292. if dose_level ~= 0
  293. doseMap(doseSlice >= dose_level) = idx;
  294. alphaMap(doseSlice >= dose_level) = obj.hSVPS.dosedisp.alpha(idx);
  295. end
  296. end
  297. % get the color
  298. doseImage = zeros(size(doseSlice,1), size(doseSlice,2), 3);
  299. R = obj.hSVPS.dosedisp.color(:, 1);
  300. G = obj.hSVPS.dosedisp.color(:, 2);
  301. B = obj.hSVPS.dosedisp.color(:, 3);
  302. doseImage(:,:,1) = reshape(R(doseMap), size(doseSlice));
  303. doseImage(:,:,2) = reshape(G(doseMap), size(doseSlice));
  304. doseImage(:,:,3) = reshape(B(doseMap), size(doseSlice));
  305. %
  306. compImage = imblend(doseImage, alphaMap, ctImage, 1);
  307. set(obj.hImage, 'CData', compImage);
  308. end
  309. case 'colorwash'
  310. doseImage = imagescX(doseSlice, jet(256), [0 max(obj.hSVPS.optResults.dose{end}(:))]);
  311. alphaMap = interp1([0; 40], [0; 1], doseSlice, 'linear', 1);
  312. alphaMap = reshape(alphaMap, size(doseSlice));
  313. compImage = imblend(doseImage, alphaMap, ctImage, 1);
  314. set(obj.hImage, 'CData', compImage);
  315. end
  316. end
  317. end
  318. %-------------------------------------------------------------------------------
  319. function redraw_contour(obj)
  320. slice = obj.hSVPS.POI(obj.zOrien);
  321. delete(obj.hROIs);
  322. obj.hROIs = [];
  323. hold(obj.hAxis,'on');
  324. for i = 1:numel(obj.hSVPS.Geometry.ROIS)
  325. % skip if display == OFF
  326. if obj.hSVPS.Geometry.ROIS{i}.visible == false
  327. continue;
  328. end
  329. switch obj.zOrien
  330. case 1
  331. curve_indices = find(obj.hSVPS.Geometry.ROIS{i}.Xcurves_slices == slice);
  332. if ~isempty(curve_indices)
  333. for curve_idx = curve_indices
  334. obj.hROIs(end+1) = plot(obj.hAxis, ...
  335. obj.hSVPS.Geometry.ROIS{i}.Xcurves{curve_idx}(:,3), ...
  336. obj.hSVPS.Geometry.ROIS{i}.Xcurves{curve_idx}(:,2), ...
  337. '--', 'LineWidth', 2, 'Color', obj.hSVPS.Geometry.ROIS{i}.color);
  338. end
  339. end
  340. case 2
  341. curve_indices = find(obj.hSVPS.Geometry.ROIS{i}.Ycurves_slices == slice);
  342. if ~isempty(curve_indices)
  343. for curve_idx = curve_indices
  344. obj.hROIs(end+1) = plot(obj.hAxis, ...
  345. obj.hSVPS.Geometry.ROIS{i}.Ycurves{curve_idx}(:,3), ...
  346. obj.hSVPS.Geometry.ROIS{i}.Ycurves{curve_idx}(:,1), ...
  347. '--', 'LineWidth', 2, 'Color', obj.hSVPS.Geometry.ROIS{i}.color);
  348. end
  349. end
  350. case 3
  351. % curve_indices = find(abs(obj.hSVPS.Geometry.ROIS{i}.curvesZ - obj.hSVPS.Geometry.z(slice)) < 1E-2);
  352. curve_indices = find(obj.hSVPS.Geometry.ROIS{i}.Zcurves_slices == slice);
  353. % skip if no curve found on current slice
  354. if ~isempty(curve_indices)
  355. for curve_idx = curve_indices
  356. obj.hROIs(end+1) = plot(obj.hAxis, ...
  357. obj.hSVPS.Geometry.ROIS{i}.curves{curve_idx}(:,1), ...
  358. obj.hSVPS.Geometry.ROIS{i}.curves{curve_idx}(:,2), ...
  359. '--', 'LineWidth', 2, 'Color', obj.hSVPS.Geometry.ROIS{i}.color);
  360. end
  361. end
  362. end
  363. end
  364. hold(obj.hAxis,'off');
  365. end
  366. %-------------------------------------------------------------------------------
  367. end % methods
  368. %===============================================================================
  369. end