mesh_write_emse - Save mesh to EMSE (.wfr) file USEAGE: mesh_write_emse(p) Write a .wfr file, in minor revision 3 format (ascii), for each mesh in p.mesh.data. If any cells of p.mesh.data.meshtype are empty, these cells will be skipped. See the EMSE website at http://www.sourcesignal.com for more information on file formats.
0001 function mesh_write_emse(p) 0002 0003 % mesh_write_emse - Save mesh to EMSE (.wfr) file 0004 % 0005 % USEAGE: mesh_write_emse(p) 0006 % 0007 % Write a .wfr file, in minor revision 3 format (ascii), 0008 % for each mesh in p.mesh.data. If any cells of 0009 % p.mesh.data.meshtype are empty, these cells will 0010 % be skipped. 0011 % 0012 % See the EMSE website at http://www.sourcesignal.com 0013 % for more information on file formats. 0014 % 0015 0016 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $ 0017 0018 % Licence: GNU GPL, no implied or express warranties 0019 % History: 06/2002 Darren.Weber_at_radiology.ucsf.edu 0020 % - created function from mesh_emse2matlab 0021 % 0022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0023 0024 fprintf('\nMESH_WRITE_EMSE...\n'); 0025 0026 if ~exist('p','var'), 0027 error('...no input p struct.\n'); 0028 elseif isempty(p), 0029 error('...input p struct is empty.\n'); 0030 elseif isempty(p.mesh.data), 0031 error('...input p struct has no mesh data.\n'); 0032 end 0033 0034 [path,name,ext] = fileparts(strcat(p.mesh.path,filesep,p.mesh.file)); 0035 file = fullfile(path,[name ext]); 0036 0037 fprintf('...writing EMSE meshes to:\n\t%s\n',fullfile(path,name)); 0038 0039 tic; 0040 0041 Meshes = p.mesh.data.meshtype; 0042 0043 for i=1:size(Meshes,2), 0044 0045 if Meshes{i}, 0046 write_emse(file,p.mesh.data.vertices{i},p.mesh.data.faces{i},Meshes{i}); 0047 end 0048 0049 end 0050 0051 t=toc; fprintf('...done (%5.2f sec).\n\n',t); 0052 0053 return 0054 0055 0056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0058 function write_emse(file,vertex,face,meshtype) 0059 0060 [path,name,ext] = fileparts(file); 0061 ext = '.wfr'; 0062 0063 name = strcat(name,'_',meshtype); 0064 0065 file = fullfile(path,[name ext]); 0066 0067 0068 fid = fopen(file,'w','ieee-le'); 0069 0070 if(fid == -1), 0071 fprintf('...could not open file: %s',file); 0072 return; 0073 else 0074 0075 fprintf('...writing tesselation: %s\n',[name ext]); 0076 0077 % Write prolog 0078 fprintf(fid,'3\t4000\n'); 0079 fprintf(fid,'3\n'); 0080 0081 % Write mesh type 0082 type = lower(meshtype); 0083 switch type, 0084 case 'unknown', fprintf(fid, '0\n'); 0085 case 'scalp', fprintf(fid, '40\n'); 0086 case 'outer skull', fprintf(fid, '80\n'); 0087 case 'outer_skull', fprintf(fid, '80\n'); 0088 case 'inner skull', fprintf(fid,'100\n'); 0089 case 'inner_skull', fprintf(fid,'100\n'); 0090 case 'cortex', fprintf(fid,'200\n'); 0091 case 'pial', fprintf(fid,'200\n'); % cortex variant 0092 case 'white', fprintf(fid,'200\n'); % cortex variant 0093 case 'smoothwm', fprintf(fid,'200\n'); % cortex variant 0094 otherwise, fprintf(fid, '0\n'); 0095 fprintf('\n...WARNING, unknown meshtype!\n\n'); 0096 end 0097 0098 0099 % EMSE Voxel Coordinates 0100 % Voxel coordinates measure location in terms of the voxels inherent in 0101 % the given volumetric set. The origin is the bottom (inferior) axial 0102 % slice, the posterior row and in the rightmost column. This coordinate 0103 % system is right-handed (although, internally, the origin is in the 0104 % anterior row, and thus is left-handed; this representation is not 0105 % available to the user). The order of the displayed coordinates is 0106 % (slice#, row#, column#). 0107 % 0108 % EMSE MRI Coordinates 0109 % MRI coordinates share the same origin as internal voxel coordinates, 0110 % but differ from the latter in two ways: first, the coordinates 0111 % are measured in millimeters, not voxels. Secondly, the origin is that 0112 % of the internal representation; that is, the inferior slice, anterior 0113 % row and rightmost column. As mentioned above, this internal representation 0114 % is left-handed. To correct for this, the row axis is numbered in the 0115 % opposite direction, making the displayed coordinate system right-handed. 0116 % The order of the displayed coordinates is (x, y, z). 0117 0118 % Given a point P(x,y,z) in head frame (the activation point on the 0119 % cortical mesh) and you want to find the corresponding voxel in the 0120 % vmi file. Symbolically you have P(head) and you want to find P(voxel). 0121 % 0122 % 1. The registration file contains the matrix HeadToImage, 0123 % so P(MRI-mm) = HeadToImage*P(head), where P(MRI-mm) is the 0124 % point in MRI coordinates. 0125 % 2. From the voxel size, you can find P(MRI-voxel), which 0126 % is the MRI coordinates expressed in voxels 0127 % 3. Use the offset between the MRI coordinate frame and 0128 % the Image coordinate frame to find P(voxel). 0129 % 0130 %Demetrios Voreades, Ph.D. 0131 %Applications Engineer, Source Signal Imaging 0132 % 0133 0134 0135 % Rotate -90 degrees around Z, given that emse coordinates 0136 % have +X through Nasion and +Y through left ear. 0137 fprintf('...rotating coordinate axes so +X anterior, +Y left, +Z superior\n'); 0138 vertex = rz(vertex,-90,'degrees'); 0139 0140 % Write vertex data 0141 for v = 1:size(vertex,1), 0142 fprintf(fid,'v\t%12.8f\t%12.8f\t%12.8f\n',vertex(v,1),vertex(v,2),vertex(v,3)); 0143 end 0144 0145 % matlab vertex indices start at one, 0146 % not zero, so we subtract one from matlab values 0147 face = face - 1; 0148 for t = 1:size(face,1), 0149 fprintf(fid,'t\t%d\t%d\t%d\t\n',face(t,1),face(t,2),face(t,3)); 0150 end 0151 0152 0153 fclose(fid); 0154 0155 end 0156 0157 return