0001 function [CoilDef,Header] = mne_load_coil_def(fname);
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 me='MNE:mne_load_coil_def';
0048
0049
0050
0051 if nargin == 0
0052 fname = mne_file_name('setup/mne/coil_def.dat');
0053 if ~exist(fname,'file')
0054 fname = mne_file_name('share/mne/coil_def.dat');
0055 if ~exist(fname,'file')
0056 error(me,'The standard coil definition file was not found');
0057 end
0058 end
0059 end
0060
0061
0062
0063
0064
0065 fid = fopen(fname,'rt');
0066
0067 if fid < 0
0068 error(me,'Could not open coil definition file %s',fname);
0069 end
0070
0071 istr = 1;
0072 str_array = cell(1000,1);
0073 str_array{istr} = fgetl(fid);
0074
0075 while ischar(str_array{istr}),
0076 istr = istr + 1;
0077 str_array{istr} = fgetl(fid);
0078 end
0079 fclose(fid);
0080
0081 str_array = str_array(1:(istr-1));
0082
0083
0084
0085
0086 HeaderLines = strmatch('#',str_array);
0087
0088
0089 StructureLines = strmatch('# struct',str_array);
0090
0091 if length(StructureLines) ~= 2,
0092 error(me,'%s anticipates two lines of structures',mfilename)
0093 end
0094
0095
0096 FormatLines = strmatch('# format',str_array);
0097
0098
0099 FieldNames = cell(1,2);
0100 Format = cell(1,2);
0101
0102
0103 for i = 1:2,
0104 FieldNames{i} = strread(str_array{StructureLines(i)},'%s');
0105 FieldNames{i}(1:2) = [];
0106
0107 [ignore,Format{i}] = strtok(str_array{FormatLines(i)},'''');
0108 Format{i} = strrep(Format{i},'''','');
0109 end
0110
0111
0112
0113
0114 struct_arg = [FieldNames{1} cell(length(FieldNames{1}),1)]';
0115
0116
0117
0118 [CoilDef(1:100)] = deal(struct(struct_arg{:}));
0119
0120
0121
0122 iCoil = 0;
0123 iLine = HeaderLines(end);
0124
0125 while iLine < length(str_array),
0126 iCoil = iCoil + 1;
0127 iLine = iLine + 1;
0128
0129
0130
0131 [numeric_items, description] = strtok(str_array{iLine},'"');
0132 temp = sscanf(numeric_items,Format{1});
0133
0134 for i = 1:(length(FieldNames{1})-1),
0135 CoilDef(iCoil).(FieldNames{1}{i}) = temp(i);
0136 end
0137
0138
0139 description = strrep(description,'"','');
0140 CoilDef(iCoil).(FieldNames{1}{end}) = description;
0141
0142
0143 CoilDef(iCoil).coildefs = zeros(CoilDef(iCoil).num_points,7);
0144 for i = 1:CoilDef(iCoil).num_points,
0145 iLine = iLine + 1;
0146 CoilDef(iCoil).coildefs(i,:) = sscanf(str_array{iLine},...
0147 Format{2})';
0148 end
0149
0150
0151
0152 CoilDef(iCoil).FV = draw_sensor(CoilDef(iCoil));
0153
0154 end
0155
0156 CoilDef = CoilDef(1:iCoil);
0157
0158 Header = str_array(HeaderLines);
0159
0160 function FV = draw_sensor(CoilDef);
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 FV = struct('faces',[],'vertices',[]);
0179
0180
0181
0182 switch CoilDef.id
0183 case {2,3012,3013,3011}
0184
0185
0186 LongSide = CoilDef.size*1000;
0187 Offset = 2.5;
0188 FV.vertices = [0 0 0; Offset 0 0; ...
0189 Offset -LongSide/2 0; LongSide/2 -LongSide/2 0; ...
0190 LongSide/2 LongSide/2 0; ...
0191 Offset LongSide/2 0; Offset 0 0; ...
0192 0 0 0; -Offset 0 0; -Offset -LongSide/2 0; ...
0193 -LongSide/2 -LongSide/2 0; ...
0194 -LongSide/2 LongSide/2 0; ...
0195 -Offset LongSide/2 0; -Offset 0 0]/1000;
0196 FV.faces = [1:length(FV.vertices)];
0197 case 2000
0198
0199 LongSide = 2;
0200 FV.vertices = [-1 1 0;1 1 0;1 -1 0; -1 -1 0]*LongSide/1000/2;
0201 FV.faces = [1:length(FV.vertices)];
0202 case {3022, 3023, 3024}
0203
0204 LongSide = CoilDef.size*1000;
0205 FV.vertices = [-1 1 0;1 1 0;1 -1 0; -1 -1 0]*LongSide/1000/2;
0206 FV.faces = [1:length(FV.vertices)];
0207 case {4001,4003,5002}
0208
0209 Radius = CoilDef.size*1000/2;
0210 Len_cir = 15;
0211 circle = cos(2*pi*[0:(Len_cir-1)]/Len_cir) + ...
0212 sqrt(-1)*sin(2*pi*[0:(Len_cir-1)]/Len_cir);
0213 FV.vertices = ...
0214 [real(circle)' imag(circle)' zeros(Len_cir,1)]*Radius/1000;
0215 FV.faces = [1:length(FV.vertices)];
0216 case {4002, 5001, 5003, 4004, 6001, 7001}
0217
0218 Radius = CoilDef.size*1000/2;
0219 Baseline = CoilDef.baseline*1000;
0220 Len_cir = 15;
0221
0222 circle = cos(2*pi*[0:Len_cir]/Len_cir) + ...
0223 sqrt(-1)*sin(2*pi*[0:Len_cir]/Len_cir);
0224 circle = circle*Radius;
0225 FV.vertices = ...
0226 [[real(circle)' imag(circle)' zeros(Len_cir+1,1)];...
0227 [real(circle)' -imag(circle)' zeros(Len_cir+1,1)+Baseline]]/1000;
0228 FV.faces = [1:length(FV.vertices)];
0229 case {5004,4005}
0230
0231 Radius = CoilDef.size*1000/2;
0232 Baseline = CoilDef.baseline*1000;
0233 Len_cir = 15;
0234
0235 circle = cos(2*pi*[0:Len_cir]/Len_cir) + ...
0236 sqrt(-1)*sin(2*pi*[0:Len_cir]/Len_cir);
0237 circle = circle*Radius;
0238 FV.vertices = ...
0239 [[real(circle)'+Baseline/2.0 imag(circle)' zeros(Len_cir+1,1)];...
0240 [real(circle)'-Baseline/2.0 -imag(circle)' zeros(Len_cir+1,1)]]/1000;
0241 FV.faces = [1:length(FV.vertices)];
0242 otherwise
0243 FV = [];
0244 end
0245
0246