-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_vtk_TriQuad.m
executable file
·75 lines (65 loc) · 1.67 KB
/
read_vtk_TriQuad.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
function [vertex, tri, quadri] = read_vtk_TriQuad(filename)
% read_vtk - read data from VTK file.
%
% [vertex,face] = read_vtk(filename, verbose);
%
% 'vertex' is a 'nb.vert x 3' array specifying the position of the vertices.
% 'face' is a 'nb.face x 3' array specifying the connectivity of the mesh.
%
% Copyright (c) Mario Richtsfeld
%
% Modifications by Costanza Simoncini:
% detection of triangles and quadrilaterals
fid = fopen(filename,'r');
if( fid==-1 )
error('Can''t open the file.');
return;
end
str = fgets(fid); % -1 if eof
if ~strcmp(str(3:5), 'vtk')
error('The file is not a valid VTK one.');
end
%%% read header %%%
str = fgets(fid);
str = fgets(fid);
str = fgets(fid);
str = fgets(fid);
nvert = sscanf(str,'%*s %d %*s', 1);
% read vertices
[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 polygons
str = fgets(fid);
str = fgets(fid);
info = sscanf(str,'%c %*s %*s', 1);
if info ~= 'C'
str = fgets(fid);
info = sscanf(str,'%c %*s %*s', 1);
end
if(info == 'C')
str = fgets(fid);
type = sscanf(str,'%d', 1);
tri=[];
quadri=[];
while type==4
idxFace = sscanf(str,'%*d %d %d %d %d\n', 4);
quadri=[quadri,idxFace];
str = fgets(fid);
type = sscanf(str,'%d', 1);
end
while type==3
idxFace = sscanf(str,'%*d %d %d %d\n', 3);
tri=[tri,idxFace];
str = fgets(fid);
type = sscanf(str,'%d', 1);
end
end
fclose(fid);
vertex=vertex';
tri=(tri+1)';
quadri=(quadri+1)';
return