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 imdl = mk_common_model('a2c0',8); fmdl= imdl.fwd_model;
0124 fmdl = crop_model(fmdl,inline('x<0','x','y','z'));
0125 unit_test_cmp('crop_model-a2c0-01',length(fmdl.electrode),5);
0126 unit_test_cmp('crop_model-a2c0-02',size(fmdl.elems),[32,3]);
0127 unit_test_cmp('crop_model-a2c0-03',size(fmdl.nodes),[25,2]);
0128
0129 imdl = mk_common_model('n3r2',[16,2]); fmdl= imdl.fwd_model;
0130 fmdl = crop_model(fmdl,inline('x<0','x','y','z'));
0131 unit_test_cmp('crop_model-n3r2-01',length(fmdl.electrode),16);
0132 unit_test_cmp('crop_model-n3r2-02',size(fmdl.elems),[342,4]);
0133 unit_test_cmp('crop_model-n3r2-03',size(fmdl.nodes),[128,3]);
0134 fmdl = crop_model(fmdl,inline('z<2','x','y','z'));
0135 unit_test_cmp('crop_model-n3r2-04',length(fmdl.electrode),8);
0136 unit_test_cmp('crop_model-n3r2-05',size(fmdl.elems),[114,4]);
0137 unit_test_cmp('crop_model-n3r2-06',size(fmdl.nodes),[64,3]);
0138
0139
0140
0141
0142 subplot(331)
0143 imdl = mk_common_model('n3r2',[16,2]); fmdl= imdl.fwd_model;
0144 show_fem(fmdl);
0145 subplot(332)
0146 show_fem(fmdl); hh= gca;
0147 subplot(333)
0148 show_fem(fmdl);
0149 crop_model([],inline('z<2','x','y','z'));
0150 crop_model(hh,inline('x<0','x','y','z'));
0151
0152 subplot(334)
0153 fmdlc = fmdl;
0154 fmdlc = crop_model(fmdlc,inline('x<0','x','y','z'));
0155 show_fem(fmdlc);
0156
0157 subplot(337)
0158 imdl = mk_common_model('d2c2'); fmdl= imdl.fwd_model;
0159 show_fem(fmdl);
0160 subplot(338)
0161 show_fem(fmdl); hh= gca;
0162 title('expected fail');
0163 subplot(339)
0164 show_fem(fmdl);
0165 crop_model([],inline('y<0','x','y','z'));
0166 crop_model(hh,inline('x<0','x','y','z'));
0167 title('expected fail');
0168
0169