-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertVTKToOff.m
128 lines (100 loc) · 2.89 KB
/
convertVTKToOff.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
function [n_pts, n_polys] = convertVTKToOff(vtk_fname, off_fname)
%% convert a vtk ascii file to .off format
display( ['Running ', mfilename ]);
fid = fopen (vtk_fname);
% read in the file version id # vtk DataFile Version 3.0
C = textscan( fid, '%s', 5);
if strcmp(C{1}{1}, '#') & strcmp(C{1}{2}, 'vtk') & ...
strcmp(C{1}{3} ,'DataFile') &strcmp(C{1}{4}, 'Version')
display( ['vtk data file version ' C{1}{5}] );
end
%read the next line
C = textscan( fid, '%s', 2, 'delimiter', '\n');
str = '';
for k = 1:length(C)
str = C{1}{k};
disp(str);
end
%read the next 2 line ASCII && 'DATASET POLYDATA'
C = textscan( fid, '%s', 2, 'delimiter', '\n');
if strcmp (C{1}{1} , 'ASCII' ) & strcmp (C{1}{2} , 'DATASET POLYDATA' )
disp('ASCII polydata file')
else
return
end
%read the next line POINTS n_pts datatype
C = textscan( fid, '%s %d %s', 1);
if strcmp(C{1}{1} , 'POINTS')
n_pts = C{2};
disp(sprintf('number points %d of %s type',n_pts, C{3}{1}));
else
return
end
% read in the points
C = textscan(fid, '%f', n_pts*3);
pts = []; j = 1;
for k = 1:3:length(C{1})
pts(j,:) = [C{1}(k),C{1}(k+1),C{1}(k+2)];
j = j+1;
end
%disp(pts);
%read the next line POLYGONS ncells size
C = textscan( fid, '%s %d %d', 1);
if strcmp(C{1}{1} , 'POLYGONS')
n_polys = C{2}(1);
poly_list_size = C{3}(1);
%disp(sprintf('number polys %d, size %d',n_polys, poly_list_size));
else
return
end
% read in the polygons
C = textscan(fid, '%d', poly_list_size);
polgon_array = {}; j = 1; k = 1;
while k < length(C{1})
n_vtx = C{1}(k);
polygon_array{j}(1) = n_vtx;
for vtx = 1 : n_vtx
polygon_array{j}(1+vtx) = C{1}(k+vtx);
end
% disp(polygon_array{j});
j = j+1;
k = k+n_vtx+1;
end
%ignore everything else
% % %read in the next set of strings until i hit normal
% % while(1)
% % C = textscan( fid, '%s', 1);
% % if ( strcmp(C{1}{1}, 'NORMALS' ) )
% % %read till end of line
% % C = textscan( fid, '%s', 1, 'delimiter', '\n');
% % %disp(C{1});
% % % read in the normals (for each point)
% % C = textscan(fid, '%f', n_pts*3);
% % normals = []; j = 1;
% % for k = 1:3:length(C{1})
% % normals(j,:) = [C{1}(k),C{1}(k+1),C{1}(k+2)];
% % j = j+1;
% % end
% % % disp(normals);
% % break;
% % end
% % end
fclose(fid);
fid_off = fopen (off_fname, 'w');
%header
fprintf( fid_off, 'OFF %d %d 0\n', n_pts, n_polys);
%point data
for p = 1 : n_pts
fprintf( fid_off, '%f %f %f \n', pts(p,:) );
end
%polygon data
for p = 1 : n_polys
n_vtx = polygon_array{p}(1);
fprintf( fid_off, '%d ', n_vtx );
for vtx = 1 : n_vtx
fprintf( fid_off, '%d ', polygon_array{p}(1+vtx) );
end
fprintf( fid_off, '\n');
end
fclose(fid_off);
end