0001 function [src] = mne_read_source_spaces(source,add_geom,tree)
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
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 me='MNE:mne_read_source_spaces';
0067
0068 global FIFF;
0069 if isempty(FIFF)
0070 FIFF = fiff_define_constants();
0071 end
0072
0073 if nargin == 1
0074 add_geom = false;
0075 fid = -1;
0076 fname = source;
0077 elseif nargin == 2
0078 fid = -1;
0079 fname = source;
0080 elseif nargin == 3
0081 a = whos('source');
0082 if strcmp(a.class,'char')
0083 fname = source;
0084 fid = -1;
0085 else
0086 fid = source;
0087 end
0088 else
0089 error(me,'Incorrect number of arguments');
0090 end
0091
0092
0093
0094 if fid < 0
0095 [ fid, tree ] = fiff_open(fname);
0096 open_here = true;
0097 else
0098 open_here = false;
0099 end
0100
0101
0102
0103 spaces = fiff_dir_tree_find(tree,FIFF.FIFFB_MNE_SOURCE_SPACE);
0104 if isempty(spaces)
0105 close_file;
0106 error(me,'No source spaces found');
0107 end
0108
0109 for k = 1:length(spaces)
0110 fprintf(1,'\tReading a source space...');
0111 this = read_source_space(spaces(k));
0112 fprintf(1,'[done]\n');
0113 if add_geom
0114 complete_source_space_info;
0115 end
0116 src(k) = this;
0117 end
0118
0119 fprintf(1,'\t%d source spaces read\n',length(spaces));
0120
0121 close_file;
0122
0123 return;
0124
0125 function [res] = read_source_space(this)
0126
0127
0128
0129 FIFF_BEM_SURF_NTRI=3104;
0130 FIFF_BEM_SURF_TRIANGLES=3106;
0131
0132 tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_ID);
0133 if isempty(tag)
0134 res.id = int32(FIFF.FIFFV_MNE_SURF_UNKNOWN);
0135 else
0136 res.id = int32(tag.data);
0137 end
0138
0139 tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NPOINTS);
0140 if isempty(tag)
0141 close_file;
0142 error(me,'Number of vertices not found');
0143 end
0144 res.np = tag.data;
0145
0146 tag = find_tag(this,FIFF_BEM_SURF_NTRI);
0147 if isempty(tag)
0148 tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NTRI);
0149 if isempty(tag)
0150 res.ntri = 0;
0151 else
0152 res.ntri = tag.data;
0153 end
0154 else
0155 res.ntri = tag.data;
0156 end
0157
0158 tag = find_tag(this,FIFF.FIFF_MNE_COORD_FRAME);
0159 if isempty(tag)
0160 close_file;
0161 error(me,'Coordinate frame information not found');
0162 end
0163 res.coord_frame = tag.data;
0164
0165
0166
0167 tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_POINTS);
0168 if isempty(tag)
0169 close_file;
0170 error(me,'Vertex data not found');
0171 end
0172 res.rr = tag.data;
0173 if size(res.rr,1) ~= res.np
0174 close_file;
0175 error(me,'Vertex information is incorrect');
0176 end
0177
0178 tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NORMALS);
0179 if isempty(tag)
0180 close_file;
0181 error(me,'Vertex normals not found');
0182 end
0183 res.nn = tag.data;
0184 if size(res.nn,1) ~= res.np
0185 close_file;
0186 error(me,'Vertex normal information is incorrect');
0187 end
0188
0189 if res.ntri > 0
0190 tag = find_tag(this,FIFF_BEM_SURF_TRIANGLES);
0191 if isempty(tag)
0192 tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_TRIANGLES);
0193 if isempty(tag)
0194 close_file;
0195 error(me,'Triangulation not found');
0196 else
0197 res.tris = tag.data;
0198 end
0199 else
0200 res.tris = tag.data;
0201 end
0202 if size(res.tris,1) ~= res.ntri
0203 close_file;
0204 error(me,'Triangulation information is incorrect');
0205 end
0206 else
0207 res.tris = [];
0208 end
0209
0210
0211
0212 tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NUSE);
0213 if isempty(tag)
0214 res.nuse = 0;
0215 res.inuse = int32(zeros(1,res.nuse));
0216 res.vertno = [];
0217 else
0218 res.nuse = tag.data;
0219 tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_SELECTION);
0220 if isempty(tag)
0221 close_file;
0222 error(me,'Source selection information missing');
0223 end
0224 res.inuse = int32(tag.data');
0225 res.vertno = int32(zeros(1,res.nuse));
0226 if length(res.inuse) ~= res.np
0227 close_file;
0228 error(me,'Incorrect number of entries in source space selection');
0229 end
0230 pp = 0;
0231 for p = 1:res.np
0232 if res.inuse(p)
0233 pp = pp + 1;
0234 res.vertno(pp) = p;
0235 end
0236 end
0237 end
0238
0239
0240
0241 tag1 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NUSE_TRI);
0242 tag2 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_USE_TRIANGLES);
0243 if isempty(tag1) || isempty(tag2)
0244 res.nuse_tri = int32(0);
0245 res.use_tris = [];
0246 else
0247 res.nuse_tri = tag1.data;
0248 res.use_tris = tag2.data;
0249 end
0250
0251
0252
0253 tag1 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NEAREST);
0254 tag2 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NEAREST_DIST);
0255 if isempty(tag1) || isempty(tag2)
0256 res.nearest = [];
0257 res.nearest_dist = [];
0258 else
0259 res.nearest = tag1.data' + 1;
0260 res.nearest_dist = tag2.data';
0261 end
0262 res.pinfo = mne_patch_info(res.nearest);
0263 if ~isempty(res.pinfo)
0264 fprintf(1,'Patch information added...');
0265 end
0266
0267
0268
0269 tag1 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_DIST);
0270 tag2 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_DIST_LIMIT);
0271 if isempty(tag1) || isempty(tag2)
0272 res.dist = [];
0273 res.dist_limit = [];
0274 else
0275 res.dist = tag1.data;
0276 res.dist_limit = tag2.data;
0277
0278
0279
0280 res.dist = res.dist + res.dist';
0281 end
0282 return;
0283
0284 function [tag] = find_tag(node,findkind)
0285
0286 for p = 1:node.nent
0287 if node.dir(p).kind == findkind
0288 tag = fiff_read_tag(fid,node.dir(p).pos);
0289 return;
0290 end
0291 end
0292 tag = [];
0293 return;
0294 end
0295
0296 end
0297
0298 function complete_source_space_info
0299
0300
0301
0302 fprintf(1,'\tCompleting triangulation info...');
0303 this.tri_area = zeros(1,this.ntri);
0304 r1 = this.rr(this.tris(:,1),:);
0305 r2 = this.rr(this.tris(:,2),:);
0306 r3 = this.rr(this.tris(:,3),:);
0307 this.tri_cent = (r1+r2+r3)/3.0;
0308 this.tri_nn = cross((r2-r1),(r3-r1));
0309 for p = 1:this.ntri
0310 size = sqrt(this.tri_nn(p,:)*this.tri_nn(p,:)');
0311 this.tri_area(p) = size/2.0;
0312 this.tri_nn(p,:) = this.tri_nn(p,:)/size;
0313 end
0314 fprintf(1,'[done]\n');
0315
0316
0317
0318 fprintf(1,'\tCompleting selection triangulation info...');
0319 if this.nuse_tri > 0
0320 r1 = this.rr(this.use_tris(:,1),:);
0321 r2 = this.rr(this.use_tris(:,2),:);
0322 r3 = this.rr(this.use_tris(:,3),:);
0323 this.use_tri_cent = (r1+r2+r3)/3.0;
0324 this.use_tri_nn = cross((r2-r1),(r3-r1));
0325 for p = 1:this.nuse_tri
0326 this.use_tri_area(p) = sqrt(this.use_tri_nn(p,:)*this.use_tri_nn(p,:)')/2.0;
0327 end
0328 end
0329 fprintf(1,'[done]\n');
0330 end
0331
0332 function close_file
0333
0334 if open_here
0335 fclose(fid);
0336 end
0337
0338 end
0339
0340
0341 end