0001 function [pick12,pick22,samp] = ...
0002 trans_coord_sbi(meg_dir, sysid, pick1, pick2, samp)
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 global vbmeg_inst;
0015 define = vbmeg_inst.const;
0016
0017 head_dir = '../head/';
0018 pos_dir = '../pos/';
0019 mri_dir = '../mri/';
0020
0021
0022
0023
0024 fname = sprintf('%s%s%s%s',meg_dir,head_dir,sysid,define.HEADER_EXTENSION);
0025 fid = fopen(fname ,'r','l');
0026 if fid==-1, disp('XXX Error : Cannot open head file !!\n'); return; end
0027
0028
0029 H_file=meg_hread('file',fid);
0030
0031
0032
0033
0034
0035
0036 H_pos = meg_hread('pos',fid,H_file.i_pos_begin);
0037 npos = H_pos.i_ncoil;
0038
0039 H_pos_rept = meg_hread('pos_rept',fid,H_file.i_pos_rept_begin);
0040 pos_lcz_begin = H_pos_rept.i_data_begin;
0041
0042
0043 H_mri = meg_hread('mri',fid,H_file.i_mri_begin);
0044 mark = H_mri.st_axes(1:npos,:);
0045 mrisys = H_mri.c_sysid{1};
0046
0047
0048 H_lcz = meg_hread('lcz',fid,H_file.i_lcz_begin);
0049 sphr = H_lcz.f_model_r;
0050 sph0 = H_lcz.st_model;
0051
0052 fclose(fid);
0053
0054
0055
0056
0057
0058
0059 fname = sprintf('%s%s%s%s',meg_dir,pos_dir,sysid,define.POS_EXTENSION);
0060 fid = fopen(fname ,'r','l');
0061
0062 if fid==-1, disp('XXX Error : Cannot open pos file !!\n'); return; end
0063
0064 fseek(fid,pos_lcz_begin,'bof');
0065 pos=zeros(npos,3);
0066
0067 for i=1:npos
0068 a=fread(fid,8,'float');
0069 b=reshape(a,1,8);
0070 pos(i,:)=b(1:3);
0071 end
0072
0073 fclose(fid); clear a b;
0074
0075
0076
0077
0078
0079 fname1 = sprintf('%s%s%s/%s%s',...
0080 meg_dir, mri_dir, mrisys, mrisys, define.DICOM1_EXTENSION);
0081
0082 fid=fopen(fname1);
0083
0084 if fid==-1,
0085 disp('XXX Error : Cannot open mri-table file !!\n');
0086 disp(sprintf('mri-table file=%s\n', fname1));
0087 return;
0088 end
0089
0090 a=fscanf(fid,'%s');
0091 fclose(fid);
0092
0093 sx=str2num(a(findstr(a,'image_x_vector=')+15:findstr(a,'image_y_vector=')-1));
0094 sy=str2num(a(findstr(a,'image_y_vector=')+15:findstr(a,'image_z_vector=')-1));
0095 sz=str2num(a(findstr(a,'image_z_vector=')+15:findstr(a,'pixel_spacing=')-1));
0096 s0=str2num(a(findstr(a,'z0_slice_origin=')+16:findstr(a,'magnification=')-1));
0097 ss=str2num(a(findstr(a,'pixel_spacing=')+14:findstr(a,'z0_slice_origin=')-1));
0098
0099 sp1=findstr(a,'slice_pitch=')+12;
0100 sp2=findstr(a(sp1:size(a,2)),'te=')-1;
0101 sp=str2num(a(sp1:sp1+sp2-1));
0102 nsl=str2num(a(findstr(a,'number_of_slices=')+17:findstr(a,'number_of_echos=')-1));
0103
0104
0105 fid=fopen(fname1);
0106
0107 b=1;
0108 while b,
0109 if ~isempty(findstr(char(fgets(fid)),'slice_location_table')), b=0; end
0110 end
0111
0112 sloc = zeros(nsl,1);
0113
0114 for i=1:nsl,
0115 sloc(i)=str2num(char(fgets(fid)));
0116 end
0117 fclose(fid);
0118
0119 scorner = sz*min(sloc)+s0;
0120 dcms0 = 128*sx*ss+127*sy*ss+fix(nsl/2)*sz*sp+scorner;
0121 dcms0 = dcms0.*1e-3;
0122
0123
0124
0125
0126 mark2 = mark + repmat(dcms0,npos,1);
0127
0128 if sphr<0,
0129 sph02=sph0+dcms0';
0130 else,
0131 sph02=[0,0,0]; disp('XXX : No suport, because not using VIVID');
0132 return;
0133 end
0134
0135
0136 nch = size(pick1,1);
0137
0138 pick1(:,4)=mod(pick1(:,4),pi);
0139 pick1(:,5:6)=mod(pick1(:,5:6),2*pi);
0140 pick2(:,4)=mod(pick2(:,4),pi);
0141 pick2(:,5:6)=mod(pick2(:,5:6),2*pi);
0142
0143
0144 pick1(:,4:6)=[sin(pick1(:,4)).*cos(pick1(:,5)) ...
0145 sin(pick1(:,4)).*sin(pick1(:,5)) cos(pick1(:,4))];
0146
0147 pick2(:,4:6)=[sin(pick2(:,4)).*cos(pick2(:,5)) ...
0148 sin(pick2(:,4)).*sin(pick2(:,5)) cos(pick2(:,4))];
0149
0150
0151 for i=1:npos
0152 sfromc(i) = min(sqrt(sum( ...
0153 ( pick1(:,1:3) - repmat(pos(i,:),size(pick1,1),1) ).^2,2)));
0154 end
0155
0156
0157 sel=find(sfromc<max(sfromc));
0158
0159
0160 ex=pos(sel(3),:)-pos(sel(1),:);
0161 ex=ex/norm(ex);
0162 ez=cross(ex,pos(sel(2),:)-pos(sel(1),:));
0163 ez=ez/norm(ez);
0164 ey=cross(ez,ex);
0165 mc=[ex; ey; ez];
0166
0167
0168 oc=sum((pos(sel(2),:)-pos(sel(1),:)).*ex)*ex + pos(sel(1),:);
0169
0170
0171 ex=mark2(sel(3),:)-mark2(sel(1),:);
0172 ex=ex/norm(ex);
0173 ez=cross(ex,mark2(sel(2),:)-mark2(sel(1),:));
0174 ez=ez/norm(ez);
0175 ey=cross(ez,ex);
0176 mm=[ex; ey; ez];
0177
0178
0179 om=sum((mark2(sel(2),:)-mark2(sel(1),:)).*ex)*ex + mark2(sel(1),:);
0180
0181 trans = mm'*mc;
0182
0183
0184 pick12(:,1:3)=(trans*(pick1(:,1:3)-repmat(oc,nch,1))')'+repmat(om,nch,1);
0185
0186 pick22(:,1:3)=(trans*(pick2(:,1:3)-repmat(oc,nch,1))')'+repmat(om,nch,1);
0187
0188
0189 pick12(:,4:6)=(trans*pick1(:,4:6)')';
0190
0191 pick22(:,4:6)=(trans*pick2(:,4:6)')';
0192
0193 samp.sph0 = sph02;
0194 samp.sphr = sphr;
0195 samp.mrisys = mrisys;