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 
    handle can be modified: set(hh,'ylim', ...)

 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;

 usage: eidors_colourbar( img ); % Set own tick positions
   img.eidors_colourbar.tick_divisions = 5; % Extra tick divisions

 Optional parameter:
    cb_shrink_move(1) = horizontal shrink (relative)
    cb_shrink_move(2) = vertial shrink (relative)
    cb_shrink_move(3) = horizontal move (absolute screen units)
  img.calc_colours.cb_shrink_move = [1,2,0];

 Make sure you use 'axis tight' in order for cb_shrink_move to
    give a correct looking image

 KNOWN ISSUE: if you use cb_shrink_move, then matlab will
   forget the link between the figure and its colorbar. Future
   plots in the same axis will continue to shrink. In general, the
   axis will need to be cleared or reinitialized.
 EXAMPLE:
   show_slices(img,2);
    p = get(gca,'position') 
   eidors_colourbar(img);
    set(gca,'position',p); %%% Reset axes after colourbar and move

 The colorbars are removed with colorbar('delete')

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

Generated on Sun 29-Dec-2024 11:41:59 by m2html © 2005