function ihb_RedrawSlice(hFigMain, hAxis)
%==================================================================================
%   25.04.01    Sergey Pakhomov
%   Last modified 22.05.01
%==================================================================================
%----------------------------------------------------------------------------------
% Get application data for hAxis
%----------------------------------------------------------------------------------
sliceInfo = getappdata(hAxis, 'sliceInfo');
rngArray = getappdata(hAxis, 'rngArray');
sliceIndex = getappdata(hAxis, 'sliceIndex');
axType = getappdata(hAxis, 'axType');
xLim = getappdata(hAxis, 'xLim');
yLim = getappdata(hAxis, 'yLim');
%----------------------------------------------------------------------------------
set(hFigMain, 'Pointer',  'watch');
%----------------------------------------------------------------------------------
%  Read and draw image
%----------------------------------------------------------------------------------
set(0,'CurrentFigure', hFigMain);
set(hFigMain, 'CurrentAxes', hAxis);
imagePath = ihb_FileFolderName('ima');
name = fullfile(imagePath, sliceInfo(rngArray(sliceIndex)).fileName);
slice = imread(name, 'jpg');
% image call delete previous image and lines objects for this axis
hImage = image(slice, 'XData', xLim, 'YData', yLim);
set(hAxis, 'YDir', 'normal');
set(hAxis, 'XTick', [], 'YTick', []);
%----------------------------------------------------------------------------------
%  Set popup menu
%----------------------------------------------------------------------------------
hMenu = findobj('Type', 'uicontextmenu', 'Tag', axType);
if ~isempty(hMenu)
    delete(hMenu);
end
hMenu = uicontextmenu('Tag', axType);
for k=1:size(rngArray, 2)
    cb = [strcat('ihb_LoadSliceCB(',int2str(k),')')];
    if k == sliceIndex
        hItem = uimenu(hMenu, 'Label', sliceInfo(rngArray(k)).name, 'Checked', 'on');
    else
        hItem = uimenu(hMenu, 'Label', sliceInfo(rngArray(k)).name, 'Callback', cb);
    end
end
set(hImage, 'UIContextMenu', hMenu);
%------------------------------------------------------------------------------
% Get cluster's information
%------------------------------------------------------------------------------
clusters = getappdata(hFigMain, 'clusters');
indClusterToView = getappdata(hFigMain, 'indClusterToView');
cl = clusters(indClusterToView);
iv = cl.hThreshold;
drawSymLS = getappdata(hFigMain, 'drawSymLS');
drawSymRS = getappdata(hFigMain, 'drawSymRS');
%----------------------------------------------------------------------------------
% Get default values
%----------------------------------------------------------------------------------
dflFileName = ihb_FileFolderName('dfl');
load(dflFileName, 'ihbdfl_bApplySmoothing');
load(dflFileName, 'ihbdfl_smooth_filter', 'ihbdfl_smooth_size', 'ihbdfl_smooth_sd');
load(dflFileName, 'ihbdfl_color_clOrig', 'ihbdfl_color_clSymm');
load(dflFileName, 'ihbdfl_line_width');
%------------------------------------------------------------------------------
% Smooth volume data
%------------------------------------------------------------------------------
if ihbdfl_bApplySmoothing == 1
    switch ihbdfl_smooth_filter
    case 'box'
        V = smooth3(cl.vTal, 'box', ihbdfl_smooth_size);
    case 'gaussian'
        V = smooth3(cl.vTal, 'gaussian', ihbdfl_smooth_size, ihbdfl_smooth_sd');
    end
else
    V = cl.vTal;
end
%----------------------------------------------------------------------------------
% Draw cluster contours
%----------------------------------------------------------------------------------
switch axType
case 'ax'
    z = sliceInfo(rngArray(sliceIndex)).dist;
    hPatch = contourslice(cl.xTal, cl.yTal, cl.zTal, V, [], [], [z], [iv iv]);
    drawContour(hPatch, 2, 1, 2, ihbdfl_color_clOrig, ihbdfl_color_clSymm, ihbdfl_line_width, 0);
    ihb_TalSpaceView3D('newAxial');
case 'fr'
    y = sliceInfo(rngArray(sliceIndex)).dist;
    hPatch = contourslice(cl.xTal, cl.yTal, cl.zTal, V, [], [y], [], [iv iv]);
    drawContour(hPatch, 2, 1, 3, ihbdfl_color_clOrig, ihbdfl_color_clSymm, ihbdfl_line_width, 0);
    ihb_TalSpaceView3D('newFrontal');
case 'sl'
    x = sliceInfo(rngArray(sliceIndex)).dist;
    if drawSymLS == 0
        hPatch = contourslice(cl.xTal, cl.yTal, cl.zTal, V, [ x], [], [], [iv iv]);
    else
        hPatch = contourslice(cl.xTal, cl.yTal, cl.zTal, V, [-x], [], [], [iv iv]);
    end
    drawContour(hPatch, drawSymLS, 2, 3, ihbdfl_color_clOrig, ihbdfl_color_clSymm, ihbdfl_line_width, 1);
    ihb_TalSpaceView3D('newSaggital');
case 'sr'
    x = sliceInfo(rngArray(sliceIndex)).dist;
    if drawSymRS == 0
        hPatch = contourslice(cl.xTal, cl.yTal, cl.zTal, V, [ x], [], [], [iv iv]);
    else
        hPatch = contourslice(cl.xTal, cl.yTal, cl.zTal, V, [-x], [], [], [iv iv]);
    end
    drawContour(hPatch, drawSymRS, 2, 3, ihbdfl_color_clOrig, ihbdfl_color_clSymm, ihbdfl_line_width, 0);
    ihb_TalSpaceView3D('newSaggital');
end    
%----------------------------------------------------------------------------------
set(hFigMain, 'Pointer',  'arrow');
%==================================================================================
% Service function to draw contours
%==================================================================================
function drawContour(hCntPatch, drawType, indX, indY, clrOrig, clrSymm, lineWidth, bIsSL)
%----------------------------------------------------------------------------------
% hCntPatch     handle of the patch object
% drawType      0 draw only primary
%               1 draw only symm
%               2 draw both
% indX          index of coordinates (1 - X; 2 - Y; 3 - Z)
% indY          to use as X and Y
% clrOrig       color for original cluster
% clrSymm       color for symmetrical cluster
% lineWidth     line width
% bIsSL         1 if Saggital left
%----------------------------------------------------------------------------------
dflFileName = ihb_FileFolderName('dfl');
load(dflFileName, 'ihbdfl_yLimD');
vertices = get(hCntPatch, 'Vertices');
if isempty(hCntPatch)
    return
else
    delete(hCntPatch);
end

if ~iscell(vertices);
    vertices = {vertices};
end

nCnt = size(vertices, 1);
for iCnt = 1:nCnt
    cnt = vertices{iCnt};
    n = size(cnt, 1);
    if n == 0
        continue
    end
    cnt(n, :) = cnt(1, :);
    
    if bIsSL == 1
        cnt(:, 2) = ihbdfl_yLimD(2) - (cnt(:, 2) - ihbdfl_yLimD(1));
    end
    
    switch drawType
    case 0
		hLine = line(...
            'XData', cnt(:, indX), 'YData', cnt(:, indY),...
            'Tag', 'ihb_LineOriginal',...
            'Color', clrOrig, 'LineWidth', lineWidth);
    case 1
		hLine = line(...
            'XData', cnt(:, indX), 'YData', cnt(:, indY),...
            'Tag', 'ihb_LineSymmetrical',...
            'Color', clrSymm, 'LineWidth', lineWidth);
    case 2
		hLine = line(...
            'XData', cnt(:, indX), 'YData', cnt(:, indY),...
            'Tag', 'ihb_LineOriginal',...
            'Color', clrOrig, 'LineWidth', lineWidth);
		hLine = line(...
            'XData', -cnt(:, indX), 'YData', cnt(:, indY),...
            'Tag', 'ihb_LineSymmetrical',...
            'Color', clrSymm, 'LineWidth', lineWidth);
   end
end
