0001 function [imdl,distr] = GREIT3D_distribution(fmdl, vopt)
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
0028
0029
0030
0031
0032
0033
0034
0035
0036 if ischar(fmdl) && strcmp(fmdl,'UNIT_TEST'); do_unit_test; return; end
0037
0038 imdl = select_imdl(fmdl,{'Basic GN dif'});
0039 imdl = mk_voxel_volume(imdl,vopt);
0040 msm = imdl.rec_model.mdl_slice_mapper;
0041 imdl.rec_model.mdl_slice_mapper.y_pts = ...
0042 fliplr(imdl.rec_model.mdl_slice_mapper.y_pts);
0043 [x, y, z] = ndgrid(msm.x_pts, msm.y_pts, msm.z_pts);
0044
0045 IN = point_in_vox(imdl.rec_model,[x(:),y(:),z(:)]);
0046 INdesity = nnz(IN)/prod(size(IN));
0047 if INdesity==0
0048 error('GREIT3D_distribution: grid doesn''t match model');
0049 elseif INdesity<0.5
0050 eidors_msg('GREIT3D_distribution: grid only matches %3.1f%% of model', INdesity*100, 1);
0051 end
0052 distr = [x(IN),y(IN),z(IN)]';
0053
0054 function out = point_in_vox(fmdl,points, epsilon)
0055
0056 if nargin < 3
0057 epsilon = 0;
0058 end
0059
0060 levels = unique(fmdl.nodes(:,3));
0061 out = false(numel(points)/3,1);
0062
0063 opt.faces = true;
0064 fmdl = fix_model(fmdl, opt);
0065
0066 jnk.nodes = fmdl.nodes(:,1:2);
0067 jnk.type = 'fwd_model';
0068
0069 for i = 1:numel(levels)-1
0070 idx = (points(:,3) - levels(i)) > epsilon;
0071 idx = idx & ((levels(i+1) - points(:,3)) > epsilon);
0072
0073 nidx = fmdl.nodes(:,3) == levels(i);
0074 F = all(nidx(fmdl.faces),2);
0075 jnk.elems = fmdl.faces(F,:);
0076 bnd = find_boundary(jnk);
0077 bnd = order_loop(jnk.nodes(unique(bnd(:)),:));
0078 out(idx) = out(idx) | inpolygon(points(idx,1),points(idx,2),bnd(:,1),bnd(:,2));
0079 end
0080
0081 function do_small_test
0082 fmdl= ng_mk_cyl_models([4,1],[16,1.5,2.5],[0.10]);
0083 fmdl= mystim_square(fmdl);
0084 vopt.imgsz = [16 16];
0085 vopt.zvec = linspace( 0.5,3.5,4);
0086 [imdl,opt.distr] = GREIT3D_distribution(fmdl, vopt);
0087 unit_test_cmp('Coarse1 size:', size(opt.distr),[3,672]);
0088 unit_test_cmp('Corase1 vals:', opt.distr(:,1),[-0.4375;-0.9375;1],1e-7);
0089
0090 vopt.zvec = linspace(-1,1,3);
0091 [imdl,opt.distr] = GREIT3D_distribution(fmdl, vopt);
0092 unit_test_cmp('Coarse2 size:', size(opt.distr),[3,224]);
0093 unit_test_cmp('Corase2 vals:', opt.distr(:,1),[-0.4375;-0.9375;0.5],1e-7);
0094
0095 vopt.zvec = linspace(-2,0,3);
0096 try
0097 get_error = 0;
0098 [imdl,opt.distr] = GREIT3D_distribution(fmdl, vopt);
0099 catch
0100 get_error = 1;
0101 end
0102 unit_test_cmp('Coarse3: got error', get_error,1)
0103
0104 function do_unit_test
0105 do_small_test
0106 [vh,vi] = test_fwd_solutions;
0107
0108 fmdl= ng_mk_cyl_models([4,1,.5],[16,1.5,2.5],[0.05]);
0109 fmdl= mystim_square(fmdl);
0110
0111 vopt.imgsz = [32 32];
0112 vopt.zvec = linspace( 0.5,3.5,7);
0113 vopt.save_memory = 1;
0114
0115 [imdl,opt.distr] = GREIT3D_distribution(fmdl, vopt);
0116 n_weight = 18.1608;
0117 imdl = mk_GREIT_model(imdl, 0.2, n_weight, opt);
0118
0119 unit_test_cmp('RM size', size(imdl.solve_use_matrix.RM), [5136,928]);
0120 tst = imdl.solve_use_matrix.RM(1,1:2);
0121 tst_= [8.572908305490031 -18.600780994066596];
0122 unit_test_cmp('RM', tst, tst_, 1e-10);
0123
0124 img = inv_solve(imdl, vh, vi);
0125 unit_test_cmp('img size', size(img.elem_data), [5136,5]);
0126 [mm,ll] =max(img.elem_data(:,1));
0127 unit_test_cmp('img', [mm,ll], ...
0128 [0.582158646727019, 1308], 1e-10);
0129
0130 img.show_slices.img_cols = 1;
0131 subplot(131); show_fem(fmdl); title 'fmdl'
0132 subplot(132); show_slices(img,[inf,inf,2]); title 'centre';
0133 subplot(133); show_slices(img,[inf,inf,1.5]); title 'bottom';
0134
0135
0136 function fmdl = mystim_square(fmdl);
0137 [~,fmdl] = elec_rearrange([16,2],'square', fmdl);
0138 [fmdl.stimulation, fmdl.meas_select] = ...
0139 mk_stim_patterns(32,1,[0,5],[0,5],{},1);
0140
0141 function [vh,vi] = test_fwd_solutions;
0142 posns= linspace(1.0,3.0,5);
0143 str=''; for i=1:length(posns);
0144 extra{i} = sprintf('ball%03d',round(posns(i)*100));
0145 str = [str,sprintf('solid %s=sphere(0.5,0,%f;0.1); ', extra{i}, posns(i))];
0146 end
0147 extra{i+1} = str;
0148 fmdl= ng_mk_cyl_models([4,1,.2],[16,1.5,2.5],[0.05],extra);
0149 fmdl = mystim_square(fmdl);
0150
0151 img= mk_image(fmdl,1);
0152 vh = fwd_solve(img);
0153
0154 for i=1:length(posns);
0155 img= mk_image(fmdl,1);
0156 img.elem_data(fmdl.mat_idx{i+1}) = 2;
0157 vi{i} = fwd_solve(img);
0158 end;
0159 vi = [vi{:}];