-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread_off.m
91 lines (74 loc) · 1.94 KB
/
read_off.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
function [vertex,face] = read_off(filename)
fid = fopen(filename,'r');
if( fid==-1 )
error('Can''t open the file.');
return;
end
str = fgets(fid); % -1 if eof
if ~strcmp(str(1:3), 'OFF')
error('The file is not a valid OFF one.');
end
str = fgets(fid);
[a,str] = strtok(str); nvert = str2num(a);
[a,str] = strtok(str); nface = str2num(a);
[A,cnt] = fscanf(fid,'%f %f %f', 3*nvert);
if cnt~=3*nvert
warning('Problem in reading vertices.');
end
A = reshape(A, 3, cnt/3);
vertex = A;
% read Face 1 1088 480 1022
[A,cnt] = fscanf(fid,'%d %d %d %d\n', 4*nface);
if cnt~=4*nface
warning('Problem in reading faces.');
end
A = reshape(A, 4, cnt/4);
face = A(2:4,:)+1;
fclose(fid);
vertex=vertex/mean(var(vertex'));
vertex=bsxfun(@minus,vertex',mean(vertex'))';
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% old code
str = ['Load mesh ', filename, ' : ', num2str(nvert), ' vertices, ', num2str(nface), ' faces.'];
str = strrep(str,'_','\_'); % problem when displaying '_' (LaTeX special char)
str = strrep(str,'\','/'); % problem when displaying '\' (LaTeX special char)
if verbose
h = waitbar(0,str);
end
vertex = zeros(nvert,3);
face = zeros(nface,3);
% loading vertex
i = 0;
while i<nvert
if verbose
waitbar(i/(nvert+nface));
end
str = fgets(fid);
if ~strcmp(strtrim(str), '')
i = i+1;
[a,str] = strtok(str); x = str2num(a);
[a,str] = strtok(str); y = str2num(a);
[a,str] = strtok(str); z = str2num(a);
vertex(i,:) = [x y z];
end
end
% loading face
for i=1:nface
if verbose
waitbar((i+nvert)/(nvert+nface));
end
str = fgets(fid);
[a,str] = strtok(str); nb = str2num(a);
if nb~=3
error('The file is not a valid OFF one.');
end
[a,str] = strtok(str); x = str2num(a);
[a,str] = strtok(str); y = str2num(a);
[a,str] = strtok(str); z = str2num(a);
face(i,:) = [x y z]+1;
end
if verbose
close(h);
end
fclose(fid);