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 5359 2017-03-14 12:57:13Z 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 end
0055 
0056    hh= colorbar;
0057    drawnow
0058 
0059    cbsm = [];
0060    try;  cbsm =  img.calc_colours.cb_shrink_move;
0061    end;
0062 
0063    if nargin >= 3; cbsm = cb_shrink_move;
0064    end
0065 
0066    axpos = get(gca,'Position');
0067    if ~isempty(cbsm)
0068       do_cb_shrink_move( hh, cbsm );
0069    end
0070 
0071    %FIXME = AA+CG 30/1/12
0072    if nargin <4; 
0073        greyscale=[]; 
0074    else
0075        warning(['eidors_colourbar: greyscale is an experimental feature'...
0076            'and will be re-implemented']);
0077    end
0078 
0079    % Stop scale from being too small
0080    if max_scale<abs(ref_lev)
0081       if max_scale < 1e-10; max_scale = 1e-10; end
0082    else
0083       if max_scale/abs(ref_lev) < 1e-4; max_scale = ref_lev*1e-4; end 
0084    end
0085 
0086    % Get colormap limits  and move bottom so we don't see the background colour
0087    ylim = get(hh,'Ylim');
0088    cmsz = size(colormap,1);
0089    cbsz = ylim(2) - ylim(1);
0090    unit = cbsz/cmsz;
0091    ylim(1)= ylim(1)+unit;
0092    %FIXME = AA+CG 30/1/12
0093    if ~isempty(greyscale); ylim(1) = ylim(1) + unit ; end
0094    set(hh,'Ylim',ylim);
0095 
0096    c_ctr = mean(ylim);
0097    c_max = ylim(2) - c_ctr;
0098 
0099 
0100    try
0101       tick_vals = img.eidors_colourbar.tick_vals;
0102    catch
0103       try
0104          tick_div = img.eidors_colourbar.tick_divisions;
0105       catch
0106          tick_div = [];
0107       end
0108       tick_vals= get_tick_vals(max_scale, ref_lev, greyscale, tick_div);
0109    end
0110 
0111    % ref_lev goes to c_ctr. max_scale goes to c_max
0112 %FIXME - need a switch to control use of max scale
0113    tick_locs = (tick_vals - ref_lev)/max_scale * c_max + c_ctr;
0114 if isempty(greyscale) 
0115     tick_locs = (tick_vals - ref_lev)/max_scale * c_max + c_ctr;
0116 else
0117     tick_locs = tick_vals*c_max*2/max_scale +2.5;
0118 end
0119    % fix extremes so numbers are displayed
0120    tick_locs(abs(tick_locs-ylim(1))<eps) = ylim(1);
0121    tick_locs(abs(tick_locs-ylim(2))<eps) = ylim(2);
0122 
0123    set(hh,'YTick', tick_locs');
0124    set(hh,'YTickLabel', tick_vals');
0125 % Code to right align YTickLabel
0126 if 0
0127    lab = get(hh,'YTickLabel');
0128    for i=1:size(lab,1);
0129       nsp = sum(lab(i,:)==' ');
0130       lab(i,:) = lab(i,[end-(nsp-1:-1:0),1:(end-nsp)]);
0131    end
0132    set(hh,'YTickLabel',lab);
0133 end
0134 
0135    if ~isempty(cbsm) % RESET OUR AXES
0136       if ~all(cbsm == [1,1,0]); 
0137          set(gca,'position',axpos);
0138       end
0139    end
0140 
0141    % Clear hh do it is not displayed, unless output requested
0142    if nargout ==0; clear hh; end
0143 
0144 end
0145 
0146 function do_cb_shrink_move( hh, cbsm )
0147    % make colourbar smaller and closer to axis
0148 
0149    axpos = get(gca,'Position');
0150    posn= get(hh,'Position');
0151    if ~all(cbsm == [1,1,0]); 
0152       posn = [posn(1) - cbsm(3), posn(2) + posn(4)*(1-cbsm(2))/2, ...
0153               posn(3) * cbsm(1), posn(4) * cbsm(2)];
0154       set(hh,'Position', posn );
0155       set(gca,'Position',axpos);
0156    end
0157 
0158 end
0159 
0160 % This function is copied from calc_colours. (AA: 23/3/2015)
0161 % The plan is to eventually separate eidors colourbar from
0162 %  calc_colours in a future release. This is a step on the
0163 %  way to separating the code
0164 function pp=get_colours( img );
0165    global eidors_colours;
0166    pp= eidors_colours;
0167 
0168    pp.component = 'real'; % Don't get from globals
0169 
0170 % override global if calc.colours specified
0171    try
0172 % DAMN Matlab should have syntax for this loop
0173       fds= fieldnames(img(1).calc_colours);%assume all are the same!
0174       for fdn= fds(:)';
0175          fdn= fdn{1};
0176          pp.( fdn ) = img(1).calc_colours.(fdn);
0177       end
0178    end
0179 end
0180 
0181 function tick_vals= get_tick_vals(max_scale, ref_lev, greyscale, tick_div_in) 
0182 % COMMENTS: AA - 4 apr 13
0183 % If we want to ahve less aggressive rounding, we can do this
0184    F= 2;
0185    OrdOfMag = 10^floor(log10(max_scale*F))/F;
0186 
0187    scale1  = floor( max_scale / OrdOfMag + 2*eps );
0188 % disp([scale1, OrdOfMag, max_scale]); => DEBUG
0189    if     (scale1/F >= 8);  fms = F*8;   tick_div=2; 
0190    elseif (scale1/F >= 6);  fms = F*6;   tick_div=2; 
0191    elseif (scale1/F >= 4);  fms = F*4;   tick_div=2;
0192    elseif (scale1/F >= 3);  fms = F*3;   tick_div=3;
0193    elseif (scale1/F >= 2);  fms = F*2;   tick_div=2;
0194    elseif (scale1/F >= 1.5);fms = F*1.5; tick_div=3;
0195    elseif (scale1/F >= 1);  fms = F*1;   tick_div=2;
0196    else   (scale1/F >= 0.5);fms = F*0.5; tick_div=2;
0197    end
0198 
0199    if ~isempty(tick_div_in); tick_div = tick_div_in; end
0200 
0201 %  ticks = (1:tick_div)/tick_div;
0202 %  ticks(end) = [];
0203 
0204    scale_r  = OrdOfMag * fms;
0205 
0206 %  in order to make the labels clean, we round to a near level
0207    OrdOfMag = 10^floor(log10(max_scale));
0208    ref_r = OrdOfMag * round( ref_lev / OrdOfMag );
0209    
0210    %FIXME = AA+CG 30/1/12
0211 if isempty(greyscale)
0212     %  Set to 2 x range, so out of scale ticks are thrown
0213     tick_vals = linspace(-2,2,tick_div*4+1)*scale_r + ref_r;
0214 else
0215 %     tick_vals = [0:0.2:1]*max_scale;
0216      tick_vals = [0:0.2:1]*scale_r;
0217 end
0218 end
0219 
0220 % DEBUG CODE ATTEMPTING TO FIX CB
0221 function debug_code
0222          a = get(hh);
0223          set(hh,'Position', posn );
0224          a = rmfield(a,'CurrentPoint');
0225          a = rmfield(a,'TightInset');
0226          a = rmfield(a,'BeingDeleted');
0227          a = rmfield(a,'Type');
0228          a.Position = posn;
0229          set(hh,a);
0230          op = get(hh,'OuterPosition') 
0231          set(hh,'Position', posn );
0232          op1= get(hh,'OuterPosition') 
0233          set(hh,'OuterPosition',op);
0234          op2= get(hh,'OuterPosition') 
0235          set(gca,'Position',axpos);
0236 end
0237 
0238 function do_unit_test
0239    imgno = 1;
0240    imdl = mk_common_model('n3r2',[16,2]);
0241    img = mk_image(imdl);
0242    img=rmfield(img,'elem_data');
0243    img.node_data(1:252)= (0:251)/100 - 1.05;
0244    if 0
0245    fprintf('CMAX = %4.2f CMIN = %4.2f\n', ...
0246        max(img.node_data), min(img.node_data) );
0247    end
0248 
0249 
0250    subplot(3,3,imgno); imgno=imgno+1;
0251    show_slices(img,2); eidors_colourbar(img);
0252 
0253    % ref_level is not displayed, but will have its effect
0254    img.calc_colours.ref_level = -0.0234;
0255    subplot(3,3,imgno); imgno=imgno+1;
0256    img2= img;
0257    img2.eidors_colourbar.tick_divisions = 4;
0258    show_slices(img2,2); eidors_colourbar(img2);
0259 
0260    img.calc_colours.cmax = 1;
0261    subplot(3,3,imgno); imgno=imgno+1;
0262    img2= img;
0263    img2.eidors_colourbar.tick_vals = [-10:10]/5;
0264    show_slices(img2,2); eidors_colourbar(img2);
0265 
0266    MV =-0.05;
0267    img.calc_colours.cb_shrink_move = [.5,.8,MV];
0268    subplot(3,3,imgno); imgno=imgno+1;
0269    show_slices(img,2);
0270    eidors_colourbar(img);
0271 
0272    subplot(3,3,imgno); imgno=imgno+1;
0273    show_fem(img,1);
0274 
0275    subplot(3,3,imgno); imgno=imgno+1;
0276    show_slices(img,2);
0277     p = get(gca,'position');
0278    eidors_colourbar(img);
0279     set(gca,'position',p);
0280 
0281    show_slices(img,2);
0282     eidors_colourbar(img);
0283 
0284    subplot(3,3,imgno); imgno=imgno+1;
0285    img.node_data = 26e1*abs(img.node_data);
0286    show_slices(img,2);
0287    eidors_colourbar(img);
0288 
0289    subplot(3,3,imgno); imgno=imgno+1;
0290    img.node_data(1:252)= abs( (0:251)/100 - 1.05 );
0291    show_slices(img,2);
0292    eidors_colourbar(img);
0293 
0294 
0295    subplot(3,3,imgno); imgno=imgno+1;
0296    img.calc_colours.ref_level = 0.5;
0297    img.calc_colours.clim      = 0.5;
0298    show_slices(img,2);
0299    eidors_colourbar(img);
0300 
0301 end

Generated on Wed 21-Jun-2017 09:29:07 by m2html © 2005