show_fem

PURPOSE ^

SHOW_FEM: show the EIDORS3D finite element model

SYNOPSIS ^

function hh=show_fem( mdl, options)

DESCRIPTION ^

 SHOW_FEM: show the EIDORS3D finite element model
 hh=show_fem( mdl, options )
 mdl is a EIDORS3D 'model' or 'image' structure
 hh= handle to the plotted model

 options may be specified by a list

 options specifies a set of options
   options(1) => show colourbar
   options(2) => show numbering on electrodes
   options(3) => number elements (==1) or nodes (==2);

 for detailed control of colours, use
    img.calc_colours."parameter" = value
 see help for calc_colours.

 for control of colourbar, use img.calc_colours.cb_shrink_move

 parameters
     fwd_model.show_fem.linecolour

 to change line properties:
      hh=show_fem(...); set(hh,'EdgeColor',[0,0,1];

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function hh=show_fem( mdl, options)
0002 % SHOW_FEM: show the EIDORS3D finite element model
0003 % hh=show_fem( mdl, options )
0004 % mdl is a EIDORS3D 'model' or 'image' structure
0005 % hh= handle to the plotted model
0006 %
0007 % options may be specified by a list
0008 %
0009 % options specifies a set of options
0010 %   options(1) => show colourbar
0011 %   options(2) => show numbering on electrodes
0012 %   options(3) => number elements (==1) or nodes (==2);
0013 %
0014 % for detailed control of colours, use
0015 %    img.calc_colours."parameter" = value
0016 % see help for calc_colours.
0017 %
0018 % for control of colourbar, use img.calc_colours.cb_shrink_move
0019 %
0020 % parameters
0021 %     fwd_model.show_fem.linecolour
0022 %
0023 % to change line properties:
0024 %      hh=show_fem(...); set(hh,'EdgeColor',[0,0,1];
0025 
0026 % (C) 2005-2011 Andy Adler. License: GPL version 2 or version 3
0027 % $Id: show_fem.m 5417 2017-04-25 12:51:16Z aadler $
0028 
0029 % TESTS
0030 switch nargin
0031    case 0;
0032      error('Insufficient parameters for show_fem');
0033    case 1;
0034      if ischar(mdl) && strcmp(mdl,'UNIT_TEST'); do_unit_test; return; end
0035      options = [];
0036 end
0037 [img,mdl,opts] = proc_params( mdl, options );
0038 
0039 if ~ishold
0040     cla;
0041 end
0042 
0043 switch opts.dims
0044    case 2;    hh= show_2d(img,mdl,opts); % 2D nodes
0045    case 3;    hh= show_3d(img,mdl,opts); % 3D nodes
0046    otherwise; error('model is not 2D or 3D');
0047 end
0048 
0049 if opts.show_elem_numbering
0050    xyzc= interp_mesh(mdl);
0051    placenumbers(xyzc, 7, [0,0,0],'none');
0052 end
0053 if opts.show_node_numbering
0054    xyzc= mdl.nodes;
0055    placenumbers(xyzc, 7, [0.0,0,0.5],[1,1,1]);
0056 end
0057 
0058 if nargout == 0; clear hh; end
0059 
0060 % this is moved to show_2d and show_3d
0061 % if ~ishold
0062 %    fix_axis
0063 % end
0064 
0065 function fix_axis
0066    ax = gca;
0067    axis tight
0068    set(ax,'DataAspectRatio',[1 1 1]);
0069    % expand the axis limits slightly
0070    [az el] = view;
0071    if el==90
0072       % for 2d plots we need x and y
0073       xl=get(ax,'XLim'); yl=get(ax,'Ylim');
0074       dx = diff(xl); dy = diff(yl);
0075       % make sure to expand away from the origin
0076       xb = strcmp(get(ax,'XAxisLocation'),'bottom');
0077       yn = strcmp(get(ax,'YDir'),'normal');
0078       if xb == yn
0079          set(ax,'YLim',yl + [0 1]*1e-3*dy);
0080       else
0081          set(ax,'YLim',yl + [-1 0]*1e-3*dy);
0082       end
0083       yl = strcmp(get(ax,'XAxisLocation'),'bottom');
0084       xn = strcmp(get(ax,'XDir'),'normal');
0085       if yl == xn
0086          set(ax,'XLim',xl + [0 1]*1e-3*dx);
0087       else
0088          set(ax,'XLim',xl + [-1 0]*1e-3*dx);
0089       end
0090    else
0091       % for 3d plots it's enough to adjust z
0092       zl = get(ax,'Zlim'); dz = diff(zl);
0093       if strcmp(get(ax,'Zdir'),'normal')
0094          set(ax,'ZLim',zl + [0 1]*1e-3*dz);
0095       else
0096          set(ax,'ZLim',zl + [-1 0]*1e-3*dz);
0097       end
0098    end
0099    
0100 
0101 function placenumbers(xyzc, fontsize, colour, bgcolour)
0102    xyzc= xyzc * eye(size(xyzc,2),3); %convert to 3D
0103    for i= 1:size(xyzc,1)
0104       text(xyzc(i,1),xyzc(i,2), xyzc(i,3), num2str(i), ...
0105             'HorizontalAlignment','center', ...
0106             'FontSize',fontsize,'Color',colour,'BackgroundColor',bgcolour);
0107    end
0108 
0109 function [img,mdl,opts] = proc_params( mdl, options );
0110 
0111    opts.do_colourbar=0;
0112    opts.number_electrodes=0;
0113    opts.show_numbering  =0;
0114    opts.show_elem_numbering = 0;
0115    opts.show_node_numbering = 0;
0116    if nargin >=2
0117        % fill in default options
0118        optionstr= zeros(1,100);
0119        optionstr(1:length(options)) = options;
0120 
0121        opts.do_colourbar=      optionstr(1);
0122        opts.number_electrodes= optionstr(2);
0123        switch optionstr(3)
0124           case 1; opts.show_elem_numbering = 1;
0125           case 2; opts.show_node_numbering = 1;
0126           case 3; opts.show_elem_numbering = 1;
0127                   opts.show_node_numbering = 1;
0128        end
0129    end
0130 
0131    
0132    % if we have an only img input, then define mdl
0133    if strcmp( mdl(1).type , 'image' )
0134       img= mdl;
0135       mdl= img(1).fwd_model;
0136    else 
0137       img = [];
0138    end
0139    
0140    opts.transparency_thresh = calc_colours('transparency_thresh');
0141    try
0142        opts.transparency_thresh = img.calc_colours.transparency_thresh;
0143    end
0144    
0145    opts.dims = size(mdl.nodes,2);
0146 
0147 % 2D Case
0148 function hh= show_2d(img,mdl,opts)
0149    hax= gca;
0150    pax= get(hax,'position');
0151    if ~isempty(img)
0152       colours= calc_colours(img, [] );
0153    else
0154       colours= [1,1,1]; % white elements if no image
0155    end
0156    hh= show_2d_fem( mdl, colours );
0157    show_electrodes_2d(mdl, opts.number_electrodes);
0158 
0159    set(hax,'position', pax);
0160    view(0, 90); axis('xy'); grid('off');
0161 
0162 % IN MATLAB 7 WE NEED TO RERUN THIS BECAUSE STUPID STUPID
0163 % MATLAB WILL RESET THE COLOURBAR EVERY TIME WE RUN PATCH!!!
0164    if exist('img','var') && opts.do_colourbar;
0165       colours= calc_colours(img, [], opts.do_colourbar);
0166       % Matlab is so weird. It puts the first colorbar in the wrong place
0167       %   sometimes ...  (tested with 6.5 and with 7.8)
0168       %   The trick is to never try to move it on the first go
0169       %   OR we reset it and then replace it. STUPID STUPID
0170 
0171      % Here's the magic trick I found. Force a drawnow,
0172      %     then delete and recreate
0173 
0174      % Because of a change in matlab colorbar somewhere around
0175      % 2012, none of this compensation code works any more. We
0176      % don't really understand what to do anymore, but this
0177      % seems best, for now ...
0178    end
0179    if ~ishold
0180       fix_axis
0181    end
0182 
0183    
0184 
0185 % 3D Case
0186 function hh= show_3d(img,mdl,opts)
0187    hh= show_3d_fem( mdl );
0188 
0189    if ~isempty(img)
0190        elem_data = get_img_data(img);
0191        show_inhomogeneities( elem_data , mdl, img, opts);
0192        if opts.do_colourbar
0193            calc_colours(img, [], opts.do_colourbar);
0194        end
0195    end
0196    % need to adjust the axis limits before we plot electrodes
0197    % who have thickness in all dimensions and thus cause 'axis tight' to
0198    % mess up the z limits
0199    if ~ishold
0200       fix_axis
0201    end
0202    if size(mdl.elems,2) == 3
0203       show_electrodes_surf(mdl, opts.number_electrodes);
0204    else
0205       show_electrodes_3d(mdl, opts.number_electrodes);
0206    end
0207 
0208 
0209 function show_electrodes_2d(mdl, number_electrodes)
0210     if ~isfield(mdl,'electrode'); return; end
0211 
0212     ee= get_boundary( mdl );
0213     ctr_x= mean(mdl.nodes(:,1));
0214     ctr_y= mean(mdl.nodes(:,2));
0215 
0216 % scale away from model
0217 
0218 for e=1:length(mdl.electrode)
0219     if isfield(mdl.electrode(e),'pos') && ~isfield(mdl.electrode(e),'nodes')
0220         vx = mdl.electrode(e).pos(:,1) - ctr_x;
0221         vy = mdl.electrode(e).pos(:,2) - ctr_y;
0222         idx = 1:length(vx);
0223     else
0224         elec_nodes= mdl.electrode(e).nodes;
0225         
0226         S= 1.00;
0227         vx= (mdl.nodes(elec_nodes,1) - ctr_x)*S;
0228         vy= (mdl.nodes(elec_nodes,2) - ctr_y)*S;
0229         % sort nodes around the model (to avoid crossed lines)
0230         [jnk,idx] = order_loop( [vx,vy] );
0231     end
0232         
0233     ecolour = electr_colour( e );
0234     if numel(vx) == 1
0235        % Point Electrode Models: put a circle around the node
0236        line(vx(idx)+ctr_x,vy(idx)+ctr_y,  ...
0237             'LineWidth', 2, 'LineStyle','-','Color', ecolour, ...
0238             'Marker','o','MarkerSize', 6,'MarkerEdgeColor',ecolour);
0239     else
0240        % Complete/Shunt Electrode Models (multiple nodes per electrode)
0241        %  put a line along the edges that form the electrode
0242        line(vx(idx)+ctr_x,vy(idx)+ctr_y,  ...
0243             'LineWidth', 3, 'LineStyle','-','Color', ecolour, ...
0244             'Marker','none','MarkerSize', 6,'MarkerEdgeColor',ecolour);
0245     end
0246     if number_electrodes
0247        S= 1.05;
0248        vx= vx*S;
0249        vy= vy*S;
0250        switch number_electrodes
0251           case {1 true}
0252              txt = num2str(e);
0253           case 2
0254              try, txt = mdl.electrode(e).label; end
0255        end
0256        hh= text(mean(vx)+ctr_x, mean(vy)+ctr_y, txt);
0257        set(hh, 'HorizontalAlignment','center', 'FontWeight','bold');
0258     end
0259 end
0260 
0261 function show_electrodes_surf(mdl, number_electrodes)
0262     if ~isfield(mdl,'electrode'); return; end
0263 
0264     ee= get_boundary( mdl );
0265     ctr_x= mean(mdl.nodes(:,1));
0266     ctr_y= mean(mdl.nodes(:,2));
0267     ctr_z= mean(mdl.nodes(:,3));
0268 % scale away from model
0269 
0270 for e=1:length(mdl.electrode)
0271     if isfield(mdl.electrode(e),'pos') && ~isfield(mdl.electrode(e),'nodes')
0272         vx = mdl.electrode(e).pos(:,1) - ctr_x;
0273         vy = mdl.electrode(e).pos(:,2) - ctr_y;
0274         vz = mdl.electrode(e).pos(:,3) - ctr_z;
0275         idx = 1:length(vx);
0276         S = 1.00;
0277     else
0278         elec_nodes= mdl.electrode(e).nodes;
0279         
0280         S= 1.00;
0281         vx= (mdl.nodes(elec_nodes,1) - ctr_x);
0282         vy= (mdl.nodes(elec_nodes,2) - ctr_y);
0283         vz= (mdl.nodes(elec_nodes,3) - ctr_z);
0284     end
0285     
0286     % sort nodes around the model (to avoid crossed lines)
0287     % TODO: figure out what to do in different directions
0288     [jnk,idx] = sort(unwrap(atan2( vy, vx )));
0289     
0290     ecolour = electr_colour( e );
0291     if numel(vx) == 1
0292        % Point Electrode Models: put a circle around the node
0293        line(S*vx(idx)+ctr_x,S*vy(idx)+ctr_y, S*vz(idx)+ctr_z,  ...
0294             'LineWidth', 2, 'LineStyle','-','Color', ecolour, ...
0295             'Marker','o','MarkerSize', 6,'MarkerEdgeColor',ecolour);
0296     else
0297        % Complete/Shunt Electrode Models (multiple nodes per electrode)
0298        %  put a line along the edges that form the electrode
0299        line(vx(idx)+ctr_x,vy(idx)+ctr_y, vz(idx)+ctr_z, ...
0300             'LineWidth', 3, 'LineStyle','-','Color', ecolour, ...
0301             'Marker','none','MarkerSize', 6,'MarkerEdgeColor',ecolour);
0302     end
0303     if number_electrodes
0304        S= 1.05;
0305 %        vx= (mdl.nodes(elec_nodes,1) - ctr_x)*S;
0306 %        vy= (mdl.nodes(elec_nodes,2) - ctr_y)*S;
0307 %        vz= (mdl.nodes(elec_nodes,3) - ctr_z)*S;
0308        switch number_electrodes
0309           case {1 true}
0310             txt = num2str(e);
0311           case 2
0312              try, txt = mdl.electrode(e).label; end
0313        end
0314        hh= text(S*mean(vx)+ctr_x, S*mean(vy)+ctr_y,S*mean(vz)+ctr_z,txt);
0315        set(hh, 'HorizontalAlignment','center', 'FontWeight','bold');
0316     end
0317 end
0318 
0319 function show_electrodes_3d(mdl, number_electrodes)
0320 % show electrode positions on model
0321 if ~isfield(mdl,'electrode'); return; end
0322 
0323 ee= get_boundary( mdl );
0324 for e=1:length(mdl.electrode)
0325     colour= electr_colour( e);
0326     
0327     if isfield(mdl.electrode(e),'pos') && ~isfield(mdl.electrode(e),'nodes')
0328         show_electrodes_surf(mdl, number_electrodes);
0329         return
0330     end
0331     elec_nodes= mdl.electrode(e).nodes;
0332     
0333     
0334     if length(elec_nodes) == 1  % point electrode model
0335         vtx= mdl.nodes(elec_nodes,:);
0336         line(vtx(1),vtx(2),vtx(3), ...
0337             'Marker','o','MarkerSize',12, ...
0338             'MarkerFaceColor',colour, 'MarkerEdgeColor', colour);
0339         if number_electrodes
0340             hh= text(vtx(1),vtx(2),vtx(3), num2str(e));
0341             set(hh, 'HorizontalAlignment','center', 'FontWeight','bold');
0342         end
0343     else
0344         % find elems on boundary attached to this electrode
0345         map = zeros(length(mdl.nodes),1);
0346         map(mdl.electrode(e).nodes) = 1;
0347         ec = map(ee);
0348         sels= find(all(ec'));
0349         
0350         ee= get_boundary( mdl );
0351         paint_electrodes(sels,ee,mdl.nodes,colour,number_electrodes);
0352         
0353         if number_electrodes
0354             el_nodes= mdl.nodes(unique(mdl.boundary(sels,:)),:);
0355             switch number_electrodes
0356                case {1 true}
0357                   txt = num2str(e);
0358                case 2
0359                   try, txt = mdl.electrode(e).label; end
0360             end
0361             hh=text( mean(el_nodes(:,1)), ...
0362                      mean(el_nodes(:,2)), ...
0363                      mean(el_nodes(:,3)), txt );
0364             set(hh,'FontWeight','bold','Color',[.8,.2,0],'FontSize',8);
0365         end
0366     end
0367 end
0368 
0369 function show_inhomogeneities( elem_data, mdl, img, opt)
0370 % show
0371 if size(elem_data,2)>1
0372    q = warning('query','backtrace');
0373    warning('backtrace','off');
0374    warning('EIDORS:FirstImageOnly','show_fem only shows first image');
0375    warning('backtrace',q.state);
0376 %    eidors_msg('warning: show_fem only shows first image',1);
0377 end
0378 repaint_inho(elem_data(:,1), 'use_global' , mdl.nodes, mdl.elems, ...
0379     opt.transparency_thresh, img); 
0380 if ~exist('OCTAVE_VERSION');
0381 camlight('left');
0382 lighting('none'); % lighting doesn't help much
0383 end
0384 
0385 function paint_electrodes_old(sel,srf,vtx, colour, show_num);
0386 %function paint_electrodes(sel,srf,vtx);
0387 %
0388 % plots the electrodes red at the boundaries.
0389 %
0390 % sel = The index of the electrode faces in the srf matrix
0391 %       sel can be created by set_electrodes.m
0392 % srf = the boundary faces (triangles)
0393 % vtx = The vertices matrix.
0394 
0395 l = srf(sel,1); m = srf(sel,2); n = srf(sel,3);
0396 
0397 Xs = [vtx(l,1);vtx(m,1);vtx(n,1)];
0398 Ys = [vtx(l,2);vtx(m,2);vtx(n,2)];
0399 Zs = [vtx(l,3);vtx(m,3);vtx(n,3)];
0400 
0401 h=patch(Xs,Ys,Zs, colour);
0402 % need 'direct' otherwise colourmap is screwed up
0403 set(h, 'FaceLighting','none', 'CDataMapping', 'direct' );
0404 
0405 function paint_electrodes(sel,srf,vtx, colour, show_num);
0406    if isempty(sel); return; end  % Not required after matlab 2014
0407 
0408    [u n m] = unique(srf(sel,:));
0409    fv.vertices = vtx(u,:);
0410    fv.faces = reshape(m,[],3);
0411    h = patch(fv,'FaceColor',colour);
0412    % need 'direct' otherwise colourmap is screwed up
0413    set(h, 'FaceLighting','none', 'CDataMapping', 'direct' );
0414 
0415 function hh= show_3d_fem( mdl, options )
0416    if mdl_dim(mdl) == 3  % volume simplices
0417       ee= get_boundary( mdl );
0418    elseif mdl_dim(mdl) == 2  % volume simplices
0419       ee= mdl.elems;
0420    elseif mdl_dim(mdl) == 1  % just lines between elements. Resistors?
0421       ee= mdl.elems;
0422    else
0423       error('Simplices are not 2D or 3D in mdl. Unsure how to display');
0424    end
0425    hh= trimesh(ee, mdl.nodes(:,1), ...
0426                    mdl.nodes(:,2), ...
0427                    mdl.nodes(:,3));
0428    set(hh, 'EdgeColor', [0,0,0]);
0429    axis('image');
0430    hidden('off');
0431 
0432 function hh=show_2d_fem( mdl, colours, imgno )
0433   szcolr = size(colours);
0434   if nargin<3;
0435       imgno = 1;
0436       if szcolr(1:2)>1; 
0437           eidors_msg('warning: show_fem only shows first image',1);
0438       end
0439   end
0440 
0441   if szcolr(1:2) == [1,3]  % no reconstruction  - just use white
0442      hh= patch('Faces',mdl.elems,'Vertices',mdl.nodes, 'facecolor',colours);
0443   elseif size(colours,1) == num_elems(mdl);
0444      colours = squeeze(colours(:,imgno,:));
0445 
0446      hh= patch('Faces',mdl.elems,'Vertices',mdl.nodes, 'facecolor','flat', ...
0447                'facevertexcdata',colours,'CDataMapping','direct'); 
0448   elseif size(colours,1) == num_nodes(mdl);
0449      colours = squeeze(colours(:,imgno,:));
0450      % 'interp' not supported in octave
0451      hh= patch('Faces',mdl.elems,'Vertices',mdl.nodes, 'facecolor','interp', ...
0452                'facevertexcdata',colours,'CDataMapping','direct'); 
0453   else
0454     eidors_msg('warning: image elements and mesh do not match. Showing grey',1);
0455     colours= [1,1,1]/2; % Grey to show we're not sure
0456     hh= patch('Faces',mdl.elems,'Vertices',mdl.nodes, 'facecolor',colours);
0457   end
0458 
0459   set(hh, 'FaceLighting','none', 'CDataMapping', 'direct' );
0460 
0461 function hh=show_2d_fem_oldmatlab( mdl, colours, imgno )
0462   szcolr = size(colours);
0463   if nargin<3;
0464       imgno = 1;
0465       if szcolr(1:2)>1;  eidors_msg('warning: show_fem only shows first image',1); end
0466   end
0467   
0468 % el_pos= avg_electrode_posn( mdl );
0469 % plot_2d_mesh(mdl.nodes', mdl.elems', el_pos', .95, [0,0,0]);
0470 
0471   S= 1; %.95; % shrink factor
0472   elem= mdl.elems'; e= size(elem,2);
0473   node= mdl.nodes'; n= size(node,2);
0474   Xs=zeros(3,e);
0475   Xs(:)=mdl.nodes(elem(:),1);
0476   Xs= S*Xs+ (1-S)*ones(3,1)*mean(Xs);
0477   Ys=zeros(3,e); Ys(:)=mdl.nodes(elem(:),2);
0478   Ys= S*Ys+ (1-S)*ones(3,1)*mean(Ys);
0479   Zs = zeros(size(Xs));
0480   if szcolr(1:2) == [1,3]  % no reconstruction  - just use white
0481      hh= patch(Xs,Ys,zeros(3,e),colours);
0482   elseif size(colours,1) == e % defined on elems
0483 % THE STUPID MATLAB 7 WILL RESET THE COLOURBAR WHENEVER YOU DO A PATCH. DAMN.
0484      colours = permute(colours(:,imgno,:),[2,1,3]);
0485   elseif size(colours,1) == n  % multiple images
0486      colours = colours(elem(:), imgno, :);
0487      colours = reshape( colours, size(elem,1), size(elem,2), []);
0488   else
0489     eidors_msg('warning: image elements and mesh do not match. Showing grey',1);
0490     colours= [1,1,1]/2; % Grey to show we're not sure
0491   end
0492 
0493   hh= patch(Xs,Ys,Zs,colours);
0494   set(hh, 'FaceLighting','none', 'CDataMapping', 'direct' );
0495   % FOR NODE RGB MATLAB SCREWS UP THE COLOURS FOR US (ONCE AGAIN). THERE MUST
0496   % BE SOME KIND OF SECRET FLAG@@@
0497 
0498   max_x= max(mdl.nodes(:,1)); min_x= min(mdl.nodes(:,1));
0499   max_y= max(mdl.nodes(:,2)); min_y= min(mdl.nodes(:,2));
0500   axis([ mean([max_x,min_x]) + 0.55*[-1,1]*(max_x-min_x), ...
0501          mean([max_y,min_y]) + 0.55*[-1,1]*(max_y-min_y) ]);
0502 
0503 
0504 
0505 function old_code_3d_plot
0506   domesnum= 0;
0507   donodenum= 0;
0508   dotext=0;
0509 
0510   xxx=zeros(4,e); xxx(:)=NODE(1,ELEM(:));
0511   xxx= S*xxx+ (1-S)*ones(4,1)*mean(xxx);
0512   yyy=zeros(4,e); yyy(:)=NODE(2,ELEM(:));
0513   yyy= S*yyy+ (1-S)*ones(4,1)*mean(yyy);
0514   zzz=zeros(4,e); zzz(:)=NODE(3,ELEM(:));
0515   zzz= S*zzz+ (1-S)*ones(4,1)*mean(zzz);
0516   plot3( xxx([1 2 3 1 4 2 3 4],:), ...
0517          yyy([1 2 3 1 4 2 3 4],:), ...
0518          zzz([1 2 3 1 4 2 3 4],:),'k' );
0519   hold on;
0520   xy=NODE(1:2,MES(1,:));
0521   plot3(arrow*xy,arrow*[0 1;-1 0]*xy,ones(8,1)*NODE(3,MES(1,:)),'b') 
0522   hold off;
0523   axis([ [-1.1 1.1]*max(NODE(1,:)) [-1.1 1.1]*max(NODE(2,:)) ...
0524          [-1.1 1.1]*max(NODE(3,:))  ])
0525 
0526 
0527 function plot_2d_mesh(NODE,ELEM,el_pos, S, options)
0528 %  if options(1) -> do mesh num
0529 %  if options(2) -> do node num
0530 %  if options(3) -> do text
0531 %  S=.95; % shrink simplices by this factor
0532 
0533   e=size(ELEM,2);
0534   d=size(ELEM,1);
0535   R= zeros(1,e);
0536 
0537 
0538   arrow= [1.02 0;1.06 .05;1.06 .02;1.1 .02; ...
0539           1.1 -.02; 1.06 -.02;1.06 -.05;1.02 0];
0540   % arrow= [1.06 0;1.18 .15;1.18 .06;1.3 .06; ...
0541   %          1.3 -.06; 1.18 -.06;1.18 -.15;1.06 0];
0542   xxx=zeros(3,e); xxx(:)=NODE(1,ELEM(:));
0543   xxx= S*xxx+ (1-S)*ones(3,1)*mean(xxx);
0544   xxx= [xxx;xxx(1,:)];
0545 
0546   yyy=zeros(3,e); yyy(:)=NODE(2,ELEM(:));
0547   yyy= S*yyy+ (1-S)*ones(3,1)*mean(yyy);
0548   yyy= [yyy;yyy(1,:)];
0549 
0550   if isempty(el_pos)
0551       plot(xxx,yyy,'b');
0552   else
0553       xy= el_pos;
0554       hh=plot([xxx;xxx(1,:)],[yyy;yyy(1,:)],'b', ...
0555               arrow*xy,arrow*[0 1;-1 0]*xy,'r');
0556       set(hh(find(R>0)),'Color',[1 0 0],'LineWidth',2);
0557       set(hh(find(R<0)),'Color',[0 0 1],'LineWidth',2);
0558   end
0559 
0560   if options(1) %domesnum
0561     mesnum= reshape(sprintf(' %-2d',[0:15]),3,16)';
0562     text(NODE(1,MES(1,:))*1.08,NODE(2,MES(1,:))*1.08,mesnum, ...
0563          'Color','green','HorizontalAlignment','center');
0564   end %if domesnum
0565 
0566   if options(2) %donodenum
0567     nodenum= reshape(sprintf('%3d',1:length(NODE)),3,length(NODE))';
0568     text(NODE(1,:),NODE(2,:),nodenum, ...
0569          'Color','yellow','HorizontalAlignment','center','FontSize',14);
0570   end %if domesnum
0571 
0572   if options(3) %dotext
0573     numeros= reshape(sprintf('%4d',[1:e]),4,e)';
0574 %   decal= ( 2-0.2*floor(log10([1:e]')))*sqrt(axis*axis'/4)/100;
0575     decal= .02;
0576     xcoor=mean(NODE(2*ELEM-1))';
0577     ycoor=mean(NODE(2*ELEM))';
0578     text(xcoor-decal,ycoor,numeros,'FontSize',8, ...
0579          'HorizontalAlignment','center');
0580 
0581   end  % if nargin~=0
0582   axis([ [-1.1 1.1]*max(NODE(1,:)) [-1.1 1.1]*max(NODE(2,:)) ])
0583 
0584 function  mes= avg_electrode_posn( mdl )
0585    if ~isfield(mdl,'electrode'); mes=[]; return; end
0586    n_elec= length( mdl.electrode );
0587    nodes = mdl.nodes;
0588    mes= zeros( n_elec, mdl_dim(mdl) )
0589    for i= 1:n_elec
0590       e_nodes=  mdl.electrode(i).nodes;
0591       mes(i,:)= mean( nodes(e_nodes,:) , 1 );
0592    end
0593 
0594 function colour= electr_colour( e);
0595     if e==1;
0596        colour = [0,.7,0]; % light green electrode #1
0597     elseif e==2
0598        colour = [0,.5,0]; % mid-green electrode #2
0599     else
0600        colour = [0,.3,0]; % dark green
0601     end
0602 
0603 function ee= get_boundary( mdl )
0604    if isfield(mdl,'boundary')
0605        ee= mdl.boundary;
0606    else
0607        % calc and cache boundary
0608        ee = find_boundary( mdl.elems );
0609    end
0610 
0611 
0612 % TESTS:
0613 function do_unit_test
0614      ver= eidors_obj('interpreter_version');
0615    clf
0616 
0617    img=calc_jacobian_bkgnd(mk_common_model('a2c0',8)); 
0618    img.elem_data=rand(size(img.fwd_model.elems,1),1);
0619    subplot(3,4,1); show_fem(img.fwd_model,[0,0,1]) 
0620    title('regular mesh numbered');
0621 
0622 if ~ver.isoctave 
0623    imgn = rmfield(img,'elem_data');
0624    imgn.node_data=rand(size(img.fwd_model.nodes,1),1);
0625    subplot(3,4,9); show_fem(imgn) 
0626    title('interpolated node colours');
0627 end
0628 
0629    img2(1) = img; img2(2) = img;
0630    subplot(3,4,2); show_fem(img,[1]);
0631    title('colours with legend');
0632    subplot(3,4,3); show_fem(img2,[0,1]);
0633    title('colours with legend');
0634    img.calc_colours.mapped_colour = 0; % USE RGB colours
0635    subplot(3,4,4); show_fem(img,[0,1,1]);
0636    title('RGB colours');
0637    subplot(3,4,4); show_fem(img);
0638    title('RGB colours');
0639 
0640    img.elem_data = [1:10];
0641    subplot(3,4,12);show_fem(img); %Should show grey
0642    title('error -> show grey');
0643 
0644 if ~ver.isoctave
0645    imgn.calc_colours.mapped_colour = 0; % USE RGB colours
0646    subplot(3,4,10);show_fem(imgn,[0,1]) 
0647    title('interpolated node colours');
0648 
0649 
0650    subplot(3,4,11);hh=show_fem(imgn); set(hh,'EdgeColor',[0,0,1]);
0651    title('with edge colours');
0652 end
0653 
0654    img3=calc_jacobian_bkgnd(mk_common_model('n3r2',[16,2]));
0655    img3.elem_data= randn(828,1);                       
0656    subplot(3,4,5); show_fem(img3.fwd_model) 
0657    subplot(3,4,6); show_fem(img3,[1])
0658    subplot(3,4,7); show_fem(img3,[1,1])
0659    subplot(3,4,8); show_fem(img3,[1,1,1])
0660

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