eidors_colourbar

PURPOSE ^

EIDORS_COLOURBAR - create an eidors colourbar with scaling to image

SYNOPSIS ^

function hh= eidors_colourbar(max_scale,ref_lev, cb_shrink_move, greyscale)

DESCRIPTION ^

 EIDORS_COLOURBAR - create an eidors colourbar with scaling to image
 usage: eidors_colourbar( img )
    show a colourbar on the current axis representing img

 usage: hh= eidors_colourbar( img )
    return a handle to colourbar created 

 usage: eidors_colourbar( img );
   img.calc_colours.ref_level =  %  centre of the colour scale
   img.calc_colours.clim      =  %  max diff from ref_level

 usage: eidors_colourbar( img ); % Set own tick positions
   img.eidors_colourbar.tick_vals = [-20:20]/10;

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function hh= eidors_colourbar(max_scale,ref_lev, cb_shrink_move, greyscale)
0002 % EIDORS_COLOURBAR - create an eidors colourbar with scaling to image
0003 % usage: eidors_colourbar( img )
0004 %    show a colourbar on the current axis representing img
0005 %
0006 % usage: hh= eidors_colourbar( img )
0007 %    return a handle to colourbar created
0008 %
0009 % usage: eidors_colourbar( img );
0010 %   img.calc_colours.ref_level =  %  centre of the colour scale
0011 %   img.calc_colours.clim      =  %  max diff from ref_level
0012 %
0013 % usage: eidors_colourbar( img ); % Set own tick positions
0014 %   img.eidors_colourbar.tick_vals = [-20:20]/10;
0015 
0016 % usage: eidors_colourbar( img ); % Set own tick positions
0017 %   img.eidors_colourbar.tick_divisions = 5; % Extra tick divisions
0018 %
0019 % Optional parameter:
0020 %    cb_shrink_move(1) = horizontal shrink (relative)
0021 %    cb_shrink_move(2) = vertial shrink (relative)
0022 %    cb_shrink_move(3) = horizontal move (absolute screen units)
0023 %
0024 % Make sure you use 'axis tight' in order for cb_shrink_move to
0025 %    give a correct looking image
0026 %
0027 % KNOWN ISSUE: if you use cb_shrink_move, then matlab will
0028 %   forget the link between the figure and its colorbar. Future
0029 %   plots in the same axis will continue to shrink. In general, the
0030 %   axis will need to be cleared or reinitialized.
0031 % EXAMPLE:
0032 %   show_slices(img,2);
0033 %    p = get(gca,'position')
0034 %   eidors_colourbar(img);
0035 %    set(gca,'position',p); %%% Reset axes after colourbar and move
0036 %
0037 % The colorbars are removed with colorbar('delete')
0038 
0039 % (C) 2005-2010 Andy Adler. License: GPL version 2 or version 3
0040 % $Id: eidors_colourbar.m 5994 2019-06-27 16:42:10Z aadler $
0041 
0042 
0043 if ischar(max_scale) && strcmp(max_scale,'UNIT_TEST'); do_unit_test; return; end
0044 
0045 % if called as a simple eidors colourbar function
0046 if isstruct(max_scale) && strcmp(max_scale.type,'image')
0047 % OLD WAY WAS TO CALL calc_colours first
0048 %   calc_colours(max_scale,[],1);
0049 %   return
0050    img = max_scale;
0051    pp=get_colours( img );
0052    img_data= get_img_data( img );
0053    [scl_data, ref_lev, max_scale] = scale_for_display( img_data(:), pp);
0054    try if strcmp(pp.component,'imag');
0055        ref_lev = imag(ref_lev);
0056    end; end
0057    ref_lev = real(ref_lev);
0058 end
0059 
0060    hh= colorbar;
0061    drawnow
0062 
0063    cbsm = [];
0064    try;  cbsm =  img.calc_colours.cb_shrink_move;
0065    end;
0066 
0067    if nargin >= 3; cbsm = cb_shrink_move;
0068    end
0069 
0070    axpos = get(gca,'Position');
0071    if ~isempty(cbsm)
0072       do_cb_shrink_move( hh, cbsm );
0073    end
0074 
0075    %FIXME = AA+CG 30/1/12
0076    if nargin <4; 
0077        greyscale=[]; 
0078    else
0079        warning(['eidors_colourbar: greyscale is an experimental feature'...
0080            'and will be re-implemented']);
0081    end
0082 
0083    % Stop scale from being too small
0084    if max_scale<abs(ref_lev)
0085       if max_scale < 1e-10; max_scale = 1e-10; end
0086    else
0087       if max_scale/abs(ref_lev) < 1e-4; max_scale = ref_lev*1e-4; end 
0088    end
0089 
0090    % Get colormap limits  and move bottom so we don't see the background colour
0091    ylim = get(hh,'Ylim');
0092    cmsz = size(colormap,1);
0093    cbsz = ylim(2) - ylim(1);
0094    unit = cbsz/cmsz;
0095    ylim(1)= ylim(1)+unit;
0096    %FIXME = AA+CG 30/1/12
0097    if ~isempty(greyscale); ylim(1) = ylim(1) + unit ; end
0098    set(hh,'Ylim',ylim);
0099 
0100    c_ctr = mean(ylim);
0101    c_max = ylim(2) - c_ctr;
0102 
0103 
0104    try
0105       tick_vals = img.eidors_colourbar.tick_vals;
0106    catch
0107       try
0108          tick_div = img.eidors_colourbar.tick_divisions;
0109       catch
0110          tick_div = [];
0111       end
0112       tick_vals= get_tick_vals(max_scale, ref_lev, greyscale, tick_div);
0113    end
0114 
0115    % ref_lev goes to c_ctr. max_scale goes to c_max
0116 %FIXME - need a switch to control use of max scale
0117    tick_locs = (tick_vals - ref_lev)/max_scale * c_max + c_ctr;
0118 if isempty(greyscale) 
0119     tick_locs = (tick_vals - ref_lev)/max_scale * c_max + c_ctr;
0120 else
0121     tick_locs = tick_vals*c_max*2/max_scale +2.5;
0122 end
0123    % fix extremes so numbers are displayed
0124    tick_locs(abs(tick_locs-ylim(1))<eps) = ylim(1);
0125    tick_locs(abs(tick_locs-ylim(2))<eps) = ylim(2);
0126 
0127    set(hh,'YTick', tick_locs');
0128    set(hh,'YTickLabel', tick_vals');
0129 % Code to right align YTickLabel
0130 if 0
0131    lab = get(hh,'YTickLabel');
0132    for i=1:size(lab,1);
0133       nsp = sum(lab(i,:)==' ');
0134       lab(i,:) = lab(i,[end-(nsp-1:-1:0),1:(end-nsp)]);
0135    end
0136    set(hh,'YTickLabel',lab);
0137 end
0138 
0139    if ~isempty(cbsm) % RESET OUR AXES
0140       if ~all(cbsm == [1,1,0]); 
0141          set(gca,'position',axpos);
0142       end
0143    end
0144 
0145    % Clear hh do it is not displayed, unless output requested
0146    if nargout ==0; clear hh; end
0147 
0148 end
0149 
0150 function do_cb_shrink_move( hh, cbsm )
0151    % make colourbar smaller and closer to axis
0152 
0153    axpos = get(gca,'Position');
0154    posn= get(hh,'Position');
0155    if ~all(cbsm == [1,1,0]); 
0156       posn = [posn(1) - cbsm(3), posn(2) + posn(4)*(1-cbsm(2))/2, ...
0157               posn(3) * cbsm(1), posn(4) * cbsm(2)];
0158       set(hh,'Position', posn );
0159       set(gca,'Position',axpos);
0160    end
0161 
0162 end
0163 
0164 % This function is copied from calc_colours. (AA: 23/3/2015)
0165 % The plan is to eventually separate eidors colourbar from
0166 %  calc_colours in a future release. This is a step on the
0167 %  way to separating the code
0168 function pp=get_colours( img );
0169    global eidors_colours;
0170    pp= eidors_colours;
0171 
0172    pp.component = 'real'; % will be overriden if in image
0173 
0174 % override global if calc.colours specified
0175    try
0176 % DAMN Matlab should have syntax for this loop
0177       fds= fieldnames(img(1).calc_colours);%assume all are the same!
0178       for fdn= fds(:)';
0179          fdn= fdn{1};
0180          pp.( fdn ) = img(1).calc_colours.(fdn);
0181       end
0182    end
0183 end
0184 
0185 function tick_vals= get_tick_vals(max_scale, ref_lev, greyscale, tick_div_in) 
0186 % COMMENTS: AA - 4 apr 13
0187 % If we want to ahve less aggressive rounding, we can do this
0188    F= 2;
0189    OrdOfMag = 10^floor(log10(max_scale*F))/F;
0190 
0191    scale1  = floor( max_scale / OrdOfMag + 2*eps );
0192 % disp([scale1, OrdOfMag, max_scale]); => DEBUG
0193    if     (scale1/F >= 8);  fms = F*8;   tick_div=2; 
0194    elseif (scale1/F >= 6);  fms = F*6;   tick_div=2; 
0195    elseif (scale1/F >= 4);  fms = F*4;   tick_div=2;
0196    elseif (scale1/F >= 3);  fms = F*3;   tick_div=3;
0197    elseif (scale1/F >= 2);  fms = F*2;   tick_div=2;
0198    elseif (scale1/F >= 1.5);fms = F*1.5; tick_div=3;
0199    elseif (scale1/F >= 1);  fms = F*1;   tick_div=2;
0200    else   (scale1/F >= 0.5);fms = F*0.5; tick_div=2;
0201    end
0202 
0203    if ~isempty(tick_div_in); tick_div = tick_div_in; end
0204 
0205 %  ticks = (1:tick_div)/tick_div;
0206 %  ticks(end) = [];
0207 
0208    scale_r  = OrdOfMag * fms;
0209 
0210 %  in order to make the labels clean, we round to a near level
0211    OrdOfMag = 10^floor(log10(max_scale));
0212    ref_r = OrdOfMag * round( ref_lev / OrdOfMag );
0213    
0214    %FIXME = AA+CG 30/1/12
0215 if isempty(greyscale)
0216     %  Set to 2 x range, so out of scale ticks are thrown
0217     tick_vals = linspace(-2,2,tick_div*4+1)*scale_r + ref_r;
0218 else
0219 %     tick_vals = [0:0.2:1]*max_scale;
0220      tick_vals = [0:0.2:1]*scale_r;
0221 end
0222 end
0223 
0224 % DEBUG CODE ATTEMPTING TO FIX CB
0225 function debug_code
0226          a = get(hh);
0227          set(hh,'Position', posn );
0228          a = rmfield(a,'CurrentPoint');
0229          a = rmfield(a,'TightInset');
0230          a = rmfield(a,'BeingDeleted');
0231          a = rmfield(a,'Type');
0232          a.Position = posn;
0233          set(hh,a);
0234          op = get(hh,'OuterPosition') 
0235          set(hh,'Position', posn );
0236          op1= get(hh,'OuterPosition') 
0237          set(hh,'OuterPosition',op);
0238          op2= get(hh,'OuterPosition') 
0239          set(gca,'Position',axpos);
0240 end
0241 
0242 function do_unit_test
0243    imgno = 1;
0244    imdl = mk_common_model('n3r2',[16,2]);
0245    img = mk_image(imdl);
0246    img=rmfield(img,'elem_data');
0247    img.node_data(1:252)= (0:251)/100 - 1.05;
0248    if 0
0249    fprintf('CMAX = %4.2f CMIN = %4.2f\n', ...
0250        max(img.node_data), min(img.node_data) );
0251    end
0252 
0253 
0254    subplot(3,3,imgno); imgno=imgno+1;
0255    show_slices(img,2); eidors_colourbar(img);
0256 
0257    % ref_level is not displayed, but will have its effect
0258    img.calc_colours.ref_level = -0.0234;
0259    subplot(3,3,imgno); imgno=imgno+1;
0260    img2= img;
0261    img2.eidors_colourbar.tick_divisions = 4;
0262    show_slices(img2,2); eidors_colourbar(img2);
0263 
0264    img.calc_colours.cmax = 1;
0265    subplot(3,3,imgno); imgno=imgno+1;
0266    img2= img;
0267    img2.eidors_colourbar.tick_vals = [-10:10]/5;
0268    show_slices(img2,2); eidors_colourbar(img2);
0269 
0270    MV =-0.05;
0271    img.calc_colours.cb_shrink_move = [.5,.8,MV];
0272    subplot(3,3,imgno); imgno=imgno+1;
0273    show_slices(img,2);
0274    eidors_colourbar(img);
0275 
0276    subplot(3,3,imgno); imgno=imgno+1;
0277    show_fem(img,1);
0278 
0279    subplot(3,3,imgno); imgno=imgno+1;
0280    show_slices(img,2);
0281     p = get(gca,'position');
0282    eidors_colourbar(img);
0283     set(gca,'position',p);
0284 
0285    show_slices(img,2);
0286     eidors_colourbar(img);
0287 
0288    subplot(3,3,imgno); imgno=imgno+1;
0289    img.node_data = 26e1*abs(img.node_data);
0290    show_slices(img,2);
0291    eidors_colourbar(img);
0292 
0293    subplot(3,3,imgno); imgno=imgno+1;
0294    img.node_data(1:252)= abs( (0:251)/100 - 1.05 );
0295    show_slices(img,2);
0296    eidors_colourbar(img);
0297 
0298 
0299    subplot(3,3,imgno); imgno=imgno+1;
0300    img.calc_colours.ref_level = 0.5;
0301    img.calc_colours.clim      = 0.5;
0302    show_slices(img,2);
0303    eidors_colourbar(img);
0304 
0305 end

Generated on Tue 31-Dec-2019 17:03:26 by m2html © 2005