-
Notifications
You must be signed in to change notification settings - Fork 7
/
freesurfer_read_surf.m
156 lines (136 loc) · 5.63 KB
/
freesurfer_read_surf.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function [vertices, faces] = freesurfer_read_surf(fname)
% freesurfer_read_surf - FreeSurfer I/O function to read a surface file
%
% [vertices, faces] = freesurfer_read_surf(fname)
%
% Reads the vertex coordinates (mm) and face lists from a surface file.
%
% Surface files are stored as either triangulations or quadrangulations.
% That is, for a triangulation, each face is defined by 3 vertices. For a
% quadrangulation, each face is defined by 4 vertices. The rows of 'faces'
% contain indices into the rows of 'vertices', the latter holds the XYZ
% coordinates of each vertex.
%
% The freesurfer faces index the vertices in counter-clockwise order (when
% viewed from the outside of the surface). This is consistent with a
% right-hand rule. If we have vertices
%
% C B
%
%
% A
%
% Then we can calculate an edge vector from A to B (ie, AB = B - A) and
% another edge vector from A to C (ie, AC = C - A). If you form a "gun"
% with your thumb and forefinger of the right hand, then align your thumb
% with the AB vector and your forefinger with the AC vector, your palm is
% facing out of the screen and extending your middle finger in the
% orthogonal direction to the plane of the screen will give the outward
% surface normal of the triangle ABC. (If you lookup "triangle" on
% Wolfram's mathworld, you can see that AB is referred to as c and AC is
% referred to as b.)
%
% However, if this surface is read into matlab, it will give INWARD surface
% normals in the matlab patch command. For some reason, matlab is not
% following the right hand rule. To get OUTWARD normals with the matlab
% patch command, use faces(:,[1 3 2]) (see below).
%
% The vertex coordinates are in mm. The FreeSurfer coordinate
% system for surfaces is quite simple, but relating to their MRI
% cor-??? files is too confusing to explain here; see the FreeSurfer
% homepage or google the documentation by Graham Wideman. For the
% surfaces, at least, the origin is somewhere in the center of the
% head, and the vertex XYZ coordinates are oriented such that +X is
% right, +Y is anterior and +Z is superior (this is the
% FreeSurfer RAS coordinate system).
%
% Note that reading the faces of a quad file can take a long
% time due to their compact storage format. In this case, the return of
% vertices can be faster if the face output variable is not specified; in
% this case, the faces are not read.
%
% Try this to visualize the surface:
% Hp = patch('vertices',vertices,'faces',faces(:,[1 3 2]),...
% 'facecolor',[.5 .5 .5],'edgecolor','none')
% camlight('headlight','infinite')
% vertnormals = get(Hp,'vertexnormals');
%
% See also freesurfer_write_surf, freesurfer_read_curv,
% freesurfer_read_wfile
%
% $Revision: 1.1.2.1 $ $Date: 2007/12/09 22:50:25 $
% Copyright (C) 2000 Darren L. Weber
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
% USA.
% History: 08/2000, Darren.Weber_at_radiology.ucsf.edu
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ver = '$Revision: 1.1.2.1 $ $Date: 2007/12/09 22:50:25 $';
fprintf('FREESURFER_READ_SURF [v %s]\n',ver(11:15));
if(nargin < 1)
help freesurfer_read_surf;
return;
end
%QUAD_FILE_MAGIC_NUMBER = (-1 & 0x00ffffff) ;
%NEW_QUAD_FILE_MAGIC_NUMBER = (-3 & 0x00ffffff) ;
TRIANGLE_FILE_MAGIC_NUMBER = 16777214;
QUAD_FILE_MAGIC_NUMBER = 16777215;
% open it as a big-endian file
fid = fopen(fname, 'rb', 'b');
if (fid < 0),
str = sprintf('could not open surface file %s.', fname);
error(str);
end
fprintf('...reading surface file: %s\n', fname);
tic;
magic = freesurfer_fread3(fid);
if (magic == QUAD_FILE_MAGIC_NUMBER),
Nvertices = freesurfer_fread3(fid);
Nfaces = freesurfer_fread3(fid);
fprintf('...reading %d quad file vertices\n',Nvertices);
vertices = fread(fid, Nvertices*3, 'int16') ./ 100 ;
if (nargout > 1),
fprintf('...reading %d quad file faces (please wait)\n',Nfaces);
faces = zeros(Nfaces,4);
for iface = 1:Nfaces,
for n=1:4,
faces(iface,n) = freesurfer_fread3(fid) ;
end
if(~rem(iface, 10000)), fprintf(' %7.0f',iface); end
if(~rem(iface,100000)), fprintf('\n'); end
end
end
elseif (magic == TRIANGLE_FILE_MAGIC_NUMBER),
fprintf('...reading triangle file\n');
tline = fgets(fid); % read creation date text line
tline = fgets(fid); % read info text line
Nvertices = fread(fid, 1, 'int32'); % number of vertices
Nfaces = fread(fid, 1, 'int32'); % number of faces
% vertices are read in column format and reshaped below
vertices = fread(fid, Nvertices*3, 'float32');
% faces are read in column format and reshaped
faces = fread(fid, Nfaces*3, 'int32');
faces = reshape(faces, 3, Nfaces)';
else
str = sprintf('unknown magic number in surface file %s.', fname);
error(str);
end
vertices = reshape(vertices, 3, Nvertices)';
fclose(fid);
fprintf('...adding 1 to face indices for matlab compatibility.\n');
faces = faces + 1;
t=toc; fprintf('...done (%6.2f sec)\n\n',t);
return