-
Notifications
You must be signed in to change notification settings - Fork 11
/
ReadMeshFromVTKFile.m
152 lines (114 loc) · 3.53 KB
/
ReadMeshFromVTKFile.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
%===========================================================================================
%Copyright (c) 2018 by Georgia Tech Research Corporation.
%All rights reserved.
%
%The files contain code and data associated with the paper titled
%"A Deep Learning Approach to Estimate Stress Distribution: A Fast and
%Accurate Surrogate of Finite Element Analysis".
%
%The paper is authored by Liang Liang, Minliang Liu, Caitlin Martin,
%and Wei Sun, and published at Journal of The Royal Society Interface, 2018.
%
%The file list: ShapeData.mat, StressData.mat, DLStress.py, im2patch.m,
%UnsupervisedLearning.m, ReadMeshFromVTKFile.m, ReadPolygonMeshFromVTKFile.m,
%WritePolygonMeshAsVTKFile.m, Visualization.m, TemplateMesh3D.vtk, TemplateMesh2D.vtk.
%Note: *.m and *.py files were converted to pdf files for documentation purpose.
%
%THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
%INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
%FOR A PARTICULAR PURPOSE.
%===========================================================================================
%%
function Mesh = ReadMeshFromVTKFile(FilePathAndName)
%Mesh.Point
%Mesh.Element
Mesh.Point=[];
Mesh.Element={};
fid=fopen(FilePathAndName, 'r');
if fid == -1
error('can not open vtk file')
return
end
LineStr = fgets(fid);%# vtk DataFile Version 3.0
tempIndex = strfind(LineStr, '#');
if isempty(tempIndex)
disp('Can not find the key char: #')
fclose(fid);
end
LineStr = fgets(fid); %vtk output
LineStr = fgets(fid); %ASCII
tempIndex = strfind(LineStr, 'ASCII');
if isempty(tempIndex)
disp('Can not find the key word: ASCII')
fclose(fid);
end
LineStr = fgets(fid);
tempIndex = strfind(LineStr, 'DATASET');
if isempty(tempIndex)
disp('Can not find the key word: DATASET')
fclose(fid);
end
LineStr = fgets(fid);
tempIndex = strfind(LineStr, 'POINTS');
if isempty(tempIndex)
disp('Can not find the key word: POINTS')
fclose(fid);
end
PointCounter = 0;
Mesh.Point=zeros(3,1000);%pre-allocate memory
while 1
LineStr = fgets(fid);
if ~ischar(LineStr)
break;
end
temp = textscan(LineStr,'%f ');
temp=temp{1}';
if PointCounter+1 > length(Mesh.Point(1,:))
tempPoint=zeros(3, PointCounter+1000);
tempPoint(:,1:PointCounter)=Mesh.Point;
Mesh.Point=tempPoint;
end
if length(temp) == 9
Mesh.Point(:,PointCounter+1)=temp([1,2,3]);
Mesh.Point(:,PointCounter+2)=temp([4,5,6]);
Mesh.Point(:,PointCounter+3)=temp([7,8,9]);
PointCounter=PointCounter+3;
elseif length(temp) == 6
Mesh.Point(:,PointCounter+1)=temp([1,2,3]);
Mesh.Point(:,PointCounter+2)=temp([4,5,6]);
PointCounter=PointCounter+2;
elseif length(temp) == 3
Mesh.Point(:,PointCounter+1)=temp([1,2,3]);
PointCounter=PointCounter+1;
else
break;
end
end
while ischar(LineStr)
tempIndex = strfind(LineStr,'POLYGONS');
if ~isempty(tempIndex)
break;
end
tempIndex = strfind(LineStr,'CELLS');
if ~isempty(tempIndex)
break;
end
LineStr = fgets(fid);
end
ElementCounter = 0;
while 1
LineStr = fgets(fid);
if ~ischar(LineStr)
break;
end
temp = textscan(LineStr,'%f ');
temp=temp{1}';
if length(temp) <= 1
break;
end
ElementCounter=ElementCounter+1;
% temp(1) is PointCount in this Element
Mesh.Element{ElementCounter}=temp(2:end) + 1; % "+1" change 0-index to 1_index
end
fclose(fid);
Mesh.Point=Mesh.Point(:,1:PointCounter);