0001 function [BEM, Vhead, Fhead, Omega, Sout, SPHinfo] = vb_prepare_bem(basis_parm)
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
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 bem_mode = basis_parm.bem_mode;
0050
0051 flg_joined_head = false;
0052 if isfield(basis_parm, 'head_file') && ~iscell(basis_parm.head_file)
0053 headfile = [basis_parm.proj_root filesep basis_parm.head_file];
0054 else
0055 headfile = [];
0056 flg_joined_head = true;
0057 end
0058
0059 global vbmeg_inst;
0060 const = vbmeg_inst.const;
0061
0062
0063
0064
0065
0066
0067 switch bem_mode
0068 case const.BASIS_MEG_SPHERE,
0069
0070 BEM = [];
0071 Vhead = [];
0072 Fhead = [];
0073 Omega = [];
0074 Sout = [];
0075
0076 SPHinfo = [];
0077
0078 case const.BASIS_MEG_BEM,
0079
0080
0081
0082 load([headfile]);
0083 fprintf('# of vertices = %d\n',size(Vhead,1));
0084
0085 if ~exist('Omega','var') | basis_parm.Recalc == ON,
0086 tic;
0087 fprintf('--- Solid angle calculation ');
0088
0089
0090
0091 [Omega, Sout]= vb_solid_angle_grk(Vhead,Fhead,XXhead);
0092
0093 fprintf('%f[sec]\n',toc);
0094 vb_save([headfile],'Omega','Sout');
0095 end
0096
0097 BEM = [];
0098 SPHinfo = [];
0099
0100 case const.BASIS_MEG_HARMONICS,
0101
0102
0103
0104 load([headfile], 'Vhead', 'Fhead', 'XXhead');
0105
0106 fprintf('# of vertices = %d\n',size(Vhead,1));
0107
0108
0109 meg_file = [basis_parm.proj_root filesep basis_parm.meg_file];
0110 [pick, Qpick, CoilWeight, Vcenter] = vb_load_sensor(meg_file);
0111 MEGinfo = vb_load_meg_info(meg_file);
0112
0113 if ~isfield(MEGinfo,'SPHinfo') | basis_parm.Recalc == ON,
0114
0115
0116 if basis_parm.DEBUG == ON,
0117
0118 Hcenter = [0 0 0];
0119 else
0120 Hcenter = mean(Vhead);
0121 end
0122
0123
0124
0125
0126
0127 Vhead = [Vhead(:,1)-Hcenter(1), ...
0128 Vhead(:,2)-Hcenter(2), Vhead(:,3)-Hcenter(3)];
0129 pick = [ pick(:,1)-Hcenter(1), ...
0130 pick(:,2)-Hcenter(2), pick(:,3)-Hcenter(3)];
0131
0132
0133 Rmax = vb_average_radius(Hcenter, Vhead);
0134
0135
0136 func_order = basis_parm.func_order;
0137
0138
0139 SPHinfo.harmo_coef = ...
0140 vb_spherical_harmo_coef(Vhead, XXhead, func_order, Rmax, pick, Qpick);
0141
0142
0143 SPHinfo.func_order = func_order;
0144 SPHinfo.radius = Rmax;
0145 MEGinfo.SPHinfo = SPHinfo;
0146 vb_save(meg_file, 'MEGinfo');
0147 else
0148
0149 SPHinfo = MEGinfo.SPHinfo;
0150 end
0151
0152 BEM = [];
0153 Omega = [];
0154 Sout = [];
0155
0156 case const.BASIS_EEG_SPHERE,
0157
0158
0159
0160
0161
0162 BEM.R = basis_parm.radius;
0163 BEM.sigma = basis_parm.sigma;
0164
0165 Vhead = [];
0166 Fhead = [];
0167 Omega = [];
0168 Sout = [];
0169 SPHinfo = [];
0170
0171 case const.BASIS_EEG_BEM,
0172
0173
0174
0175 required_fields = {'head_file', 'sigma'};
0176 optional_fields = {'LayerTag'};
0177
0178 for i = 1:length(required_fields)
0179 if ~isfield(basis_parm, required_fields{i})
0180 error('incomplete basis_parm : does not exist : %s', required_fields{i});
0181 end
0182 end
0183
0184 for i = 1:length(optional_fields)
0185 if ~isfield(basis_parm, optional_fields{i})
0186 warning('incomplete basis_parm : does not exist : %s', optional_fields{i});
0187 basis_parm.LayerTag = char('CSF', 'Skull', 'Scalp');
0188 end
0189 end
0190
0191
0192 new_headfile = vb_util_make_joined_head_file( ...
0193 basis_parm.proj_root, basis_parm.head_file, basis_parm.sigma, basis_parm.LayerTag);
0194
0195 load([new_headfile]);
0196 fprintf('# of vertices = %d\n',size(Vhead,1));
0197
0198 if ~exist('BEM','var'),
0199
0200 necessary_info = {'R', 'Sigma', 'Nvertex', 'Npatch'};
0201 if exist('R', 'var') && exist('Sigma', 'var') && exist('Nvertex', 'var') && exist('Npatch', 'var')
0202 BEM.R = R;
0203 BEM.sigma = [Sigma 0];
0204 BEM.Nvertex = Nvertex;
0205 BEM.Npatch = Npatch;
0206 elseif ~flg_joined_head
0207 if ~exist('Vhead', 'var') || ~exist('Fhead', 'var')
0208 error('Head file must have Vhead and Fhead field.(%s)\n', new_headfile);
0209 end
0210
0211
0212 if ~exist('R', 'var') BEM.R = calc_average_radius(Vhead); end;
0213 if ~exist('Sigma', 'var') BEM.sigma = [1.0 0]; end;
0214 if ~exist('Nvertex', 'var') BEM.Nvertex = [1 length(Vhead)]; end;
0215 if ~exist('Npatch', 'var') BEM.Npatch = [1 length(Fhead)]; end;
0216 else
0217
0218 for i = 1:length(necessary_info)
0219 if ~exist(necessary_info{i}, 'var')
0220 warning('The headfile must have <%s>.\n', necessary_info{i});
0221 end
0222 end
0223 error('Making BEM is incomplete. Check --> %s', headfile);
0224 end
0225 end
0226
0227 if ~exist('Omega','var') | basis_parm.Recalc == ON,
0228 tic;
0229 fprintf('--- Solid angle calculation ');
0230
0231
0232
0233 [Omega, Sout]= vb_solid_angle_grk(Vhead,Fhead,XXhead);
0234
0235 fprintf('%f[sec]\n',toc);
0236
0237 omega_file = vb_util_make_omega_file(new_headfile, Omega, Sout);
0238 end
0239 SPHinfo = [];
0240 end
0241
0242