-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_surf.m
85 lines (73 loc) · 2.23 KB
/
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
function [vertex_coords, faces, magic] = read_surf(fname)
%uses fread3
% [vertex_coords, faces] = read_surf(fname)
% reads a the vertex coordinates and face lists from a surface file
% note that reading the faces from a quad file can take a very long
% time due to the goofy format that they are stored in. If the faces
% output variable is not specified, they will not be read so it
% should execute pretty quickly.
%
%
% read_surf.m
%
% Original Author: Bruce Fischl
% CVS Revision Info:
% $Author: fischl $
% $Date: 2014/04/30 12:59:03 $
% $Revision: 1.7 $
%
% Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH"
%
% Terms and conditions for use, reproduction, distribution and contribution
% are found in the 'FreeSurfer Software License Agreement' contained
% in the file 'LICENSE' found in the FreeSurfer distribution, and here:
%
% https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense
%
% Reporting: [email protected]
%
%fid = fopen(fname, 'r') ;
%nvertices = fscanf(fid, '%d', 1);
%all = fscanf(fid, '%d %f %f %f %f\n', [5, nvertices]) ;
%curv = all(5, :)' ;
% open it as a big-endian file
%QUAD_FILE_MAGIC_NUMBER = (-1 & 0x00ffffff) ;
%NEW_QUAD_FILE_MAGIC_NUMBER = (-3 & 0x00ffffff) ;
TRIANGLE_FILE_MAGIC_NUMBER = 16777214 ;
QUAD_FILE_MAGIC_NUMBER = 16777215 ;
NEW_QUAD_FILE_MAGIC_NUMBER = 16777213 ;
fid = fopen(fname, 'rb', 'b') ;
if (fid < 0)
str = sprintf('could not open surface file %s.', fname) ;
error(str) ;
end
magic = fread3(fid) ;
if((magic == QUAD_FILE_MAGIC_NUMBER) | (magic == NEW_QUAD_FILE_MAGIC_NUMBER))
vnum = fread3(fid) ;
fnum = fread3(fid) ;
vertex_coords = fread(fid, vnum*3, 'int16') ./ 100 ;
if (nargout > 1)
for i=1:fnum
for n=1:4
faces(i,n) = fread3(fid) ;
end
end
end
elseif (magic == TRIANGLE_FILE_MAGIC_NUMBER)
fgets(fid) ;
fgets(fid) ;
vnum = fread(fid, 1, 'int32') ;
fnum = fread(fid, 1, 'int32') ;
vertex_coords = fread(fid, vnum*3, 'float32') ;
if (nargout > 1)
faces = fread(fid, fnum*3, 'int32') ;
faces = reshape(faces, 3, fnum)' ;
end
else
fprintf('ERROR: magic number %d unknown\n',magic);
vertex_coords = [];
faces = [];
return;
end
vertex_coords = reshape(vertex_coords, 3, vnum)' ;
fclose(fid) ;