1 | function data=read2DScalarVTK(filename)
|
---|
2 | fid=fopen(filename);
|
---|
3 | % search for POINTS Section
|
---|
4 | line=fsearch(fid,'POINTS');
|
---|
5 | npoints=sscanf(line,'POINTS %d');
|
---|
6 |
|
---|
7 | % Read in points
|
---|
8 | data.Vertices=zeros(3,npoints);
|
---|
9 | data.Vertices(:)=fscanf(fid,'%f',npoints*3);
|
---|
10 |
|
---|
11 | % Search for POLYGONS section
|
---|
12 | line=fsearch(fid,'POLYGONS');
|
---|
13 | npolygons=sscanf(line,'POLYGONS %d');
|
---|
14 |
|
---|
15 | % read in POLYGON data
|
---|
16 | data.Faces=zeros(4,npolygons,'int64');
|
---|
17 | data.Faces(:)=fscanf(fid,'%d',npolygons*4);
|
---|
18 | data.Faces=data.Faces(2:4,:); %remove the leading number 3 - assuming mesh is trianglular
|
---|
19 | data.Faces=data.Faces+1; %matlab complains otherwise
|
---|
20 |
|
---|
21 | % Search for Scalar data
|
---|
22 | line=fsearch(fid,'CELL_DATA');
|
---|
23 | ncells=sscanf(line,'CELL_DATA %d');
|
---|
24 | class=fscanf(fid,'%s',1);
|
---|
25 | var=fscanf(fid,'%s',1);
|
---|
26 | type=fscanf(fid,'%s',1);
|
---|
27 | line=fsearch(fid,'LOOKUP_TABLE');
|
---|
28 |
|
---|
29 | % Read in Scalar date
|
---|
30 | data.FaceVertexCData=zeros(1,ncells);
|
---|
31 | data.FaceVertexCData(:)=fscanf(fid,'%f',ncells);
|
---|
32 |
|
---|
33 | % Transpose things to order expected by patch command
|
---|
34 | data.Faces=data.Faces';
|
---|
35 | data.Vertices=data.Vertices';
|
---|
36 | data.FaceVertexCData=data.FaceVertexCData';
|
---|
37 | end
|
---|
38 |
|
---|
39 | function line=fsearch(fid,str)
|
---|
40 | while true
|
---|
41 | line=fgetl(fid);
|
---|
42 | if numel(line) >= numel(str)
|
---|
43 | if (strcmp(line(1:numel(str)),str))
|
---|
44 | break
|
---|
45 | end
|
---|
46 | end
|
---|
47 | end
|
---|
48 | end |
---|