elem_select

PURPOSE ^

ELEM_SELECT: select element fractions inside a function

SYNOPSIS ^

function memb_frac = elem_select( fmdl, select_fcn )

DESCRIPTION ^

 ELEM_SELECT: select element fractions inside a function
   memb_frac = elem_select( fmdl, select_fcn )

  memb_frac = fraction of each element within the fcn
  fmdl =      fwd_model structure
  select_fcn = function to describe membership, 
              can also be a cell array of functions, but all must accept
              parameters x, y, and z. ELEM_SELECT then finds elements that
              satisfy ALL the functions at once
              finally, select_fcn can be a string which will then
                be turned into a function via inline(str,'x','y','z')
 OR
 select_fcn = string accepting named variables x,y,z.

 parameters
   fwd_model.elem_select.interp_no  - interpolation density

 Example:
   img = mk_image(mk_common_model('b2d1c',8));
   select_fcn = @(x,y,z) (x-0.2).^2+(y-0.5).^2<0.2^2
   memb_frac = elem_select( img.fwd_model, select_fcn)
   img.elem_data = 1 + memb_frac*0.1;
   show_fem(img);

 See Also:
   mk_c2f_circ_mapping

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function memb_frac = elem_select( fmdl, select_fcn )
0002 % ELEM_SELECT: select element fractions inside a function
0003 %   memb_frac = elem_select( fmdl, select_fcn )
0004 %
0005 %  memb_frac = fraction of each element within the fcn
0006 %  fmdl =      fwd_model structure
0007 %  select_fcn = function to describe membership,
0008 %              can also be a cell array of functions, but all must accept
0009 %              parameters x, y, and z. ELEM_SELECT then finds elements that
0010 %              satisfy ALL the functions at once
0011 %              finally, select_fcn can be a string which will then
0012 %                be turned into a function via inline(str,'x','y','z')
0013 % OR
0014 % select_fcn = string accepting named variables x,y,z.
0015 %
0016 % parameters
0017 %   fwd_model.elem_select.interp_no  - interpolation density
0018 %
0019 % Example:
0020 %   img = mk_image(mk_common_model('b2d1c',8));
0021 %   select_fcn = @(x,y,z) (x-0.2).^2+(y-0.5).^2<0.2^2
0022 %   memb_frac = elem_select( img.fwd_model, select_fcn)
0023 %   img.elem_data = 1 + memb_frac*0.1;
0024 %   show_fem(img);
0025 %
0026 % See Also:
0027 %   mk_c2f_circ_mapping
0028 
0029 if ischar(fmdl) && strcmp(fmdl,'UNIT_TEST'); do_unit_test; return; end
0030 
0031 
0032 % 4 for 2D, 3 for 3D
0033 dims = size(fmdl.nodes,2);
0034 interp_no = 6 - dims;
0035 try 
0036    interp_no = fmdl.elem_select.interp_no;
0037 end
0038 
0039 pts = interp_mesh( fmdl, interp_no );
0040 x = squeeze(pts(:,1,:));
0041 y = squeeze(pts(:,2,:));
0042 if dims ==2;
0043   z = 0*x;
0044 else
0045   z = squeeze(pts(:,3,:));
0046 end
0047 if ischar(select_fcn)
0048 %  we have a string, create a function
0049     select_fcn = inline(select_fcn, 'x','y','z');
0050     memb_frac = mean( feval(select_fcn,x,y,z), 2);
0051 elseif ~iscell(select_fcn) 
0052     % the normal case
0053     memb_frac = mean( feval(select_fcn,x,y,z), 2);
0054 else
0055     % many functions case
0056     memb_val = ones(size(x));
0057     for i = 1:numel(select_fcn)
0058         memb_val = memb_val .* feval(select_fcn{i},x,y,z);
0059     end
0060     memb_frac = mean(memb_val,2);
0061 end
0062 
0063 
0064 function do_unit_test;
0065     imdl = mk_common_model('a2c2',8);
0066     select_fcn = '(x-0.2).^2+(y-0.5).^2<0.2^2';
0067     memb_frac = elem_select( imdl.fwd_model, select_fcn);
0068     unit_test_cmp('a2c2 (string)',find(memb_frac), [5, 10,18,26,27]');
0069 
0070     imdl = mk_common_model('a2c2',8);
0071     select_fcn = inline('(x-0.2).^2+(y-0.5).^2<0.2^2','x','y','z');
0072     memb_frac = elem_select( imdl.fwd_model, select_fcn);
0073     unit_test_cmp('a2c2',find(memb_frac), [5, 10,18,26,27]');
0074 
0075 
0076     select_fcn2= inline('y<0.4','x','y','z');
0077     memb_frac = elem_select( imdl.fwd_model, {select_fcn,select_fcn2});
0078     unit_test_cmp('a2c2 (2fcns)',find(memb_frac), [5, 10,18,27]');
0079 
0080     imdl = mk_common_model('n3r2',[16,2]);
0081     select_fcn = inline('(x-0.2).^2+(y-0.5).^2 + (z-1).^2<0.1^2','x','y','z');
0082     memb_frac = elem_select( imdl.fwd_model, select_fcn);
0083     unit_test_cmp('n3r2',find(memb_frac), [156 159 162 168 431 434 437 503]');

Generated on Fri 30-Dec-2022 19:44:54 by m2html © 2005