0001 function [fmdl,c2f_idx]= crop_model( axis_handle, fcn_handle );
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 if ischar(axis_handle) && strcmp(axis_handle,'UNIT_TEST'); do_unit_test; return; end
0028
0029
0030
0031
0032 usage_graphics= 1;
0033 try if axis_handle.type == 'fwd_model'
0034 usage_graphics= 0;
0035 end; end
0036
0037 if usage_graphics
0038 if isempty(axis_handle)
0039 axis_handle= gca;
0040 end
0041 crop_graphics_model(axis_handle, fcn_handle);
0042 else
0043 [fmdl,c2f_idx]= crop_fwd_model(axis_handle, fcn_handle);
0044 end
0045
0046
0047 function crop_graphics_model(axis_handle, fcn_handle);
0048 kk= get(axis_handle,'Children');
0049
0050
0051
0052 for k= kk(:)'
0053 try
0054 x= get(k,'XData');
0055 y= get(k,'YData');
0056 z= get(k,'ZData');
0057 c= get(k,'CData');
0058 idx= ~all( feval(fcn_handle,x,y,z) );
0059 if any(size(c)>[1,1])
0060 set(k,'Xdata', x(:,idx), ...
0061 'Ydata', y(:,idx), ...
0062 'Zdata', z(:,idx), ...
0063 'Cdata', c(:,idx));
0064 else
0065 set(k,'Xdata', x(:,idx), ...
0066 'Ydata', y(:,idx), ...
0067 'Zdata', z(:,idx));
0068 end
0069 end
0070 end
0071
0072
0073 function [fmdl1,c2f_idx]= crop_fwd_model(fmdl0, fcn_handle);
0074 fmdl1= fmdl0;
0075
0076
0077 nodes= fmdl0.nodes;
0078 [n,d]= size(nodes);
0079 n2xyz= eye(d,3);
0080 xyz= nodes*n2xyz;
0081
0082 idx0= all( feval(fcn_handle,xyz(:,1), ...
0083 xyz(:,2), ...
0084 xyz(:,3)),2);
0085
0086 fmdl1.nodes(idx0,:) = [];
0087
0088
0089 idx1= zeros(n,1);
0090 idx1(~idx0)= 1:sum(~idx0);
0091
0092 fmdl1.elems(:) = idx1(fmdl0.elems);
0093 remove= any( fmdl1.elems == 0, 2);
0094 fmdl1.elems(remove,:)= [];
0095
0096 c2f_idx= find(~remove);
0097
0098 fmdl1.boundary(:) = idx1(fmdl0.boundary);
0099 remove= any( fmdl1.boundary == 0, 2);
0100 fmdl1.boundary(remove,:)= [];
0101
0102
0103 if isfield(fmdl1,'electrode');
0104 n_elecs = length(fmdl1.electrode);
0105 rm_elec_list = zeros(n_elecs,1);
0106 for i=1:n_elecs;
0107 el_nodes= fmdl0.electrode(i).nodes;
0108 el_nodes(:)= idx1(el_nodes);
0109 if any(el_nodes==0);
0110 rm_elec_list(i) = 1;
0111 end
0112 fmdl1.electrode(i).nodes= el_nodes;
0113 end
0114 if any(rm_elec_list)
0115 str = sprintf('%d,', find(rm_elec_list));
0116 eidors_msg('crop_model: removing electrodes (%s)',str(1:end-1),1);
0117 fmdl1.electrode = fmdl1.electrode( find(~rm_elec_list));
0118 end
0119 end
0120
0121
0122 function do_unit_test
0123 subplot(331)
0124 imdl = mk_common_model('n3r2',[16,2]); fmdl= imdl.fwd_model;
0125 show_fem(fmdl);
0126 subplot(332)
0127 show_fem(fmdl); hh= gca;
0128 subplot(333)
0129 show_fem(fmdl);
0130 crop_model([],inline('z<2','x','y','z'));
0131 crop_model(hh,inline('x<0','x','y','z'));
0132
0133 subplot(334)
0134 fmdlc = fmdl;
0135 fmdlc = crop_model(fmdlc,inline('x<0','x','y','z'));
0136 show_fem(fmdlc);
0137
0138 subplot(337)
0139 imdl = mk_common_model('d2c2'); fmdl= imdl.fwd_model;
0140 show_fem(fmdl);
0141 subplot(338)
0142 show_fem(fmdl); hh= gca;
0143 title('expected fail');
0144 subplot(339)
0145 show_fem(fmdl);
0146 crop_model([],inline('y<0','x','y','z'));
0147 crop_model(hh,inline('x<0','x','y','z'));
0148 title('expected fail');
0149
0150