forked from ImageGuidedTherapyLab/FileConversionScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadVTK.m
82 lines (72 loc) · 2.03 KB
/
readVTK.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
function V = readVTK(vtkfile)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Usage: V = readVTK(vtkfile)
%
% V: The matrix to be stored
% vtkfile: The filename
% notes: Only reads binary STRUCTURED_POINTS
%
% Erik Vidholm 2006
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
V = 0;
% open file (OBS! big endian format)
fid = fopen(vtkfile,'r','b');
if( fid == -1 )
return
end
fgetl(fid); % # vtk DataFile Version x.x
fgetl(fid); % comments
fgetl(fid); % BINARY
fgetl(fid); % DATASET STRUCTURED_POINTS
s = fgetl(fid); % DIMENSIONS NX NY NZ
sz = sscanf(s, '%*s%d%d%d').'
fgetl(fid); % ORIGIN OX OY OZ
fgetl(fid); % SPACING SX SY SZ
fgetl(fid); % POINT_DATA NXNYNZ
s = fgetl(fid); % SCALARS/VECTORS name data_type (ex: SCALARS imagedata unsigned_char)
svstr = sscanf(s, '%s', 1)
dtstr = sscanf(s, '%*s%*s%s')
if( strcmp(svstr,'SCALARS') > 0 )
fgetl(fid); % the lookup table
if( strcmp(dtstr,'unsigned_char') > 0 )
% read data
V = fread(fid,prod(sz),'*uint8');
V = reshape(V,sz);
elseif( strcmp(dtstr,'char') > 0 )
% read data
V = fread(fid,prod(sz),'*int8');
V = reshape(V,sz);
elseif( strcmp(dtstr,'unsigned_short') > 0 )
% read data
V = fread(fid,prod(sz),'*uint16');
V = reshape(V,sz);
elseif( strcmp(dtstr,'short') > 0 )
% read data
V = fread(fid,prod(sz),'*int16');
V = reshape(V,sz);
elseif( strcmp(dtstr,'unsigned_int') > 0 )
% read data
V = fread(fid,prod(sz),'*uint32');
V = reshape(V,sz);
elseif( strcmp(dtstr,'int') > 0 )
% read data
V = fread(fid,prod(sz),'*int32');
V = reshape(V,sz);
elseif( strcmp(dtstr,'float') > 0 )
% read data
V = fread(fid,prod(sz),'*single');
V = reshape(V,sz);
elseif( strcmp(dtstr,'double') > 0 )
% read data
V = fread(fid,prod(sz),'*double');
V = reshape(V,sz);
end
elseif( strcmp(svstr,'VECTORS') > 0 )
if( strcmp(dtstr,'float') > 0 )
% read data
V = fread(fid,3*prod(sz),'*single');
V = reshape(V,[3 sz]);
V = permute(V,[2 3 4 1]);
end
end
fclose(fid);