0001 function vb_head_join_files(head_files, file_root, ...
0002 joined_file, sigma, layer_tag, calc_mode, verbose_swt, check_level)
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
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 try
0067
0068 if ~exist('head_files', 'var'), head_files = []; end;
0069 if ~exist('file_root', 'var'), file_root = []; end;
0070 if ~exist('joined_file', 'var'), joined_file = []; end;
0071 if ~exist('sigma', 'var'), sigma = []; end;
0072 if ~exist('layer_tag', 'var'), layer_tag = []; end;
0073 if ~exist('calc_mode', 'var'), calc_mode = []; end;
0074 if ~exist('verbose_swt', 'var'), verbose_swt = []; end;
0075 if ~exist('check_level', 'var'), check_level = []; end;
0076
0077 [head_files, file_root, joined_file, sigma, layer_tag, calc_mode, ...
0078 VERBOSE, check_level] = inner_check_arguments( ...
0079 head_files, file_root, joined_file, sigma, layer_tag, calc_mode, ...
0080 verbose_swt, check_level);
0081
0082
0083 if ~iscell(head_files)
0084
0085 warning('%s is not cell so it is not required to join', head_files);
0086 return;
0087 end
0088
0089
0090
0091 XXhead = [];
0092 Vhead = [];
0093 Fhead = [];
0094 Nvertex = [];
0095 Npatch = [];
0096 R = [];
0097
0098
0099 switch calc_mode
0100 case 1, Rmethod = 'MEAN';
0101 case 2, Rmethod = 'MIN';
0102 case 3, Rmethod = 'MAX';
0103 end
0104
0105
0106 RR = [];
0107 Nfile = size(head_files,2);
0108 for nf = 1:Nfile
0109
0110 f = fullfile(file_root, head_files{nf});
0111 Vhead = vb_head_load_vhead(f);
0112 RR = [RR vb_util_distance_of_points(Vhead, calc_mode)];
0113 end
0114 [R idx] = sort(RR);
0115 Sigma = sigma(idx);
0116 LayerTag = layer_tag(:,idx);
0117 HeadFiles = head_files(:,idx);
0118
0119
0120 Vhead = [];
0121
0122 begin_v = 1;
0123 begin_p = 1;
0124 Npoint = 0;
0125 for n = 1:Nfile
0126 data = load(fullfile(file_root, head_files{n}));
0127
0128
0129
0130 len_v = size(vb_headinfo_get_vhead(data),1);
0131 len_p = size(vb_headinfo_get_fhead(data),1);
0132
0133 end_v = begin_v + len_v -1;
0134 end_p = begin_p + len_p -1;
0135 Nvertex = [Nvertex; begin_v end_v];
0136 Npatch = [Npatch; begin_p end_p];
0137 begin_v = end_v + 1;
0138 begin_p = end_p + 1;
0139
0140
0141
0142
0143 Vhead = [ Vhead; vb_headinfo_get_vhead(data)];
0144 Fhead = [ Fhead; vb_headinfo_get_fhead(data) + Npoint];
0145
0146
0147 if isempty(data.XXhead)
0148 if ~isempty(data.Vhead) && ~isempty(data.Fhead)
0149 [new_f, new_v, data.XXhead] = vb_out_normal(data.Fhead, data.Vhead);
0150 else
0151 if isempty(data.Vhead), warning('Vhead is empty'); end
0152 if isempty(data.Fhead), warning('Fhead is empty'); end
0153 warning('XXhead is empty');
0154 end
0155 end
0156 XXhead = [XXhead; vb_headinfo_get_xxhead(data)];
0157
0158 Npoint = Npoint + len_v;
0159 end
0160
0161
0162 vb_save(joined_file, ...
0163 'XXhead', 'Vhead', 'Fhead', ...
0164 'Nvertex', 'Npatch', 'R', 'Rmethod', 'Sigma', 'LayerTag');
0165
0166 if VERBOSE
0167 fprintf('--- saved as current joined file : %s\n', joined_file);
0168 end
0169
0170 catch
0171 rethrow(lasterror);
0172 end
0173
0174
0175
0176
0177
0178 function [head_files, file_root, joined_file, sigma, layer_tag, calc_mode, ...
0179 VERBOSE, check_level] = inner_check_arguments( ...
0180 head_files, file_root, joined_file, sigma, layer_tag, calc_mode, ...
0181 verbose_swt, check_level)
0182
0183
0184 if isempty(head_files)
0185 error('head_files is a required parameter');
0186 end
0187
0188
0189 if isempty(file_root)
0190 file_root = '';
0191 end
0192
0193 if isempty(joined_file)
0194 joined_file = fullfile(file_root, 'joined_head.head.mat');
0195 end
0196
0197 if isempty(sigma)
0198 basis_parm = vb_set_basis_parm;
0199 sigma = basis_parm.sigma;
0200 end
0201
0202 if isempty(layer_tag)
0203
0204 layer_tag = {'CSF', 'Skull', 'Scalp'};
0205 end
0206
0207 if isempty(calc_mode)
0208 calc_mode = 1;
0209 elseif isempty(find(calc_mode == [1 2 3]))
0210 error('undefined calc_mode : %d', calc_mode);
0211 end
0212
0213 if isempty(verbose_swt)
0214 VERBOSE = OFF;
0215 end
0216
0217 if isempty(check_level)
0218 check_level = 0;
0219 end
0220
0221
0222 head_files = vb_util_arrange_list(head_files, 1);
0223 sigma = vb_util_arrange_list(sigma, 1);
0224 layer_tag = vb_util_arrange_list(layer_tag, 1);
0225
0226
0227 if check_level == 0
0228 inner_check_layer_number(head_files, sigma, layer_tag);
0229
0230
0231 inner_check_files(head_files, file_root);
0232
0233 elseif check_level == 1
0234
0235
0236 else
0237 warning('unexpected check_level : %d', check_level);
0238 end
0239
0240
0241
0242
0243
0244 function inner_check_layer_number(head_files, sigma, layer_tag)
0245
0246 Nfiles = size(head_files, 2);
0247 Nsigma = size(sigma, 2);
0248 Nlytag = size(layer_tag, 2);
0249
0250 if (Nfiles ~= Nsigma) || (Nsigma ~= Nlytag) || (Nlytag ~= Nfiles)
0251 error('not be consistent [Nfiles,Nsigma,Nlytag] = [%d,%d,%d]', ...
0252 Nfiles, Nsigma, Nlytag);
0253 end
0254 return;
0255
0256
0257
0258
0259
0260 function inner_check_files(files, root_dir)
0261
0262 Nfiles = size(files, 2);
0263 for fn = 1:Nfiles
0264 cur_file = fullfile(root_dir, files{fn});
0265 if exist(cur_file, 'file') ~= 2
0266 error('cannot read file : %s', cur_file);
0267 end
0268 end
0269 return;
0270
0271
0272
0273
0274