0001 function fiff_write_dig_file(filename,lpa,nas,rpa,hpi,eeg,eegref,extra)
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 me = 'MNE:mne_create_dig_file';
0033
0034 global FIFF;
0035 if isempty(FIFF)
0036 FIFF = fiff_define_constants();
0037 end
0038
0039
0040
0041 fid = fiff_start_file(filename);
0042
0043
0044
0045
0046 d1 = nas - lpa;
0047 d2 = rpa - lpa;
0048
0049 alpha = d1*d2'/(d2*d2');
0050 r0 = (1-alpha)*lpa + alpha*rpa;
0051 ex = d2/sqrt(d2*d2');
0052 ey = (nas-r0);
0053 ey = ey/sqrt(ey*ey');
0054 ez = cross(ex,ey);
0055
0056 T = inv([ ex 0 ; ey 0 ; ez 0 ; r0 1 ]);
0057
0058
0059 T = T(:,1:3);
0060
0061
0062
0063
0064 fiff_start_block(fid,FIFF.FIFFB_ISOTRAK);
0065
0066 fiff_write_int(fid,FIFF.FIFF_MNE_COORD_FRAME,FIFF.FIFFV_COORD_HEAD);
0067
0068 d.kind = FIFF.FIFFV_POINT_CARDINAL;
0069 d.ident = FIFF.FIFFV_POINT_LPA;
0070 d.r = [lpa 1]*T;
0071 fiff_write_dig_point(fid,d);
0072
0073 d.kind = FIFF.FIFFV_POINT_CARDINAL;
0074 d.ident = FIFF.FIFFV_POINT_NASION;
0075 d.r = [nas 1]*T;
0076 fiff_write_dig_point(fid,d);
0077
0078 d.kind = FIFF.FIFFV_POINT_CARDINAL;
0079 d.ident = FIFF.FIFFV_POINT_RPA;
0080 d.r = [rpa 1]*T;
0081 fiff_write_dig_point(fid,d);
0082
0083
0084 fprintf(1,'Wrote the fiducial locations\n');
0085
0086 if exist('hpi','var')
0087 for k = 1:size(hpi,1)
0088 d.kind = FIFF.FIFFV_POINT_HPI;
0089 d.ident = k;
0090 d.r = [hpi(k,:) 1]*T;
0091 fiff_write_dig_point(fid,d);
0092 end
0093 if (size(hpi,1) > 0)
0094 fprintf(1,'Wrote %d HPI coil locations\n',size(hpi,1));
0095 end
0096 end
0097
0098 if exist('eeg','var')
0099 if exist('eegref','var') && ~isempty(eegref)
0100 d.kind = FIFF.FIFFV_POINT_EEG;
0101 d.ident = 0;
0102 d.r = [eegref 1]*T;
0103 fiff_write_dig_point(fid,d);
0104 fprintf(1,'Wrote EEG reference electrode location\n');
0105 end
0106 for k = 1:size(eeg,1)
0107 d.kind = FIFF.FIFFV_POINT_EEG;
0108 d.ident = k;
0109 d.r = [eeg(k,:) 1]*T;
0110 fiff_write_dig_point(fid,d);
0111 end
0112 if (size(eeg,1) > 0)
0113 fprintf(1,'Wrote %d EEG electrode locations\n',size(eeg,1));
0114 end
0115 end
0116
0117 if exist('extra','var')
0118 for k = 1:size(extra,1)
0119 d.kind = FIFF.FIFFV_POINT_EXTRA;
0120 d.ident = k;
0121 d.r = [extra(k,:) 1]*T;
0122 fiff_write_dig_point(fid,d);
0123 end
0124 if (size(extra,1) > 0)
0125 fprintf(1,'Wrote %d extra points\n',size(extra,1));
0126 end
0127 end
0128
0129 fiff_end_block(fid,FIFF.FIFFB_ISOTRAK);
0130
0131 fiff_end_file(fid);
0132