-
Notifications
You must be signed in to change notification settings - Fork 11
/
WritePolygonMeshAsVTKFile.m
132 lines (108 loc) · 3.84 KB
/
WritePolygonMeshAsVTKFile.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
%===========================================================================================
%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 WritePolygonMeshAsVTKFile(PolyMesh, FilePathAndName)
% save PolyMesh as vtkPolyData in *.vtk
% PolyMesh.Point: 3xN matrix
% PolyMesh.Face: 1xN cell array
% PolyMesh.PointData().Name
% PolyMesh.PointData().Data
% PolyMesh.FaceData().Name
% PolyMesh.FaceData().Data
if isempty(PolyMesh)
error('PolyMesh is empty')
return
end
fid = fopen(FilePathAndName, 'W');
if fid == -1
error('can not open vtk file')
return
end
LineStr='# vtk DataFile Version 3.0';
fprintf(fid, [LineStr '\n']);
LineStr='PolygonMesh@Matlab';
fprintf(fid, [LineStr '\n']);
LineStr='ASCII';
fprintf(fid, [LineStr '\n']);
LineStr='DATASET POLYDATA';
fprintf(fid, [LineStr '\n']);
[~, PointCount]=size(PolyMesh.Point);
LineStr=['POINTS ' num2str(PointCount) ' ' class(PolyMesh.Point)];
fprintf(fid, [LineStr '\n']);
precision = 10;
for k=1:PointCount
temp=PolyMesh.Point(:,k);
LineStr=[num2str(temp(1), precision) ' ' num2str(temp(2), precision) ' ' num2str(temp(3), precision)];
fprintf(fid, [LineStr '\n']);
end
FaceCount=length(PolyMesh.Face);
TotalNumber=0;
for k=1:FaceCount
temp=PolyMesh.Face{k};
TotalNumber=TotalNumber + 1 +length(temp);
end
LineStr=['POLYGONS ' num2str(FaceCount) ' ' num2str(TotalNumber)];
fprintf(fid, [LineStr '\n']);
for k=1:FaceCount
temp=PolyMesh.Face{k};
temp=temp-1; % change matlab index (1-start) to c++ index (0-start)
PointCount_k=length(temp);
LineStr=num2str(PointCount_k);
for n=1:PointCount_k
LineStr=[LineStr ' ' num2str(temp(n), precision)];
end
fprintf(fid, [LineStr '\n']);
end
%PointData(n).Name
%PointData(n).Data
if isfield(PolyMesh, 'PointData')
LineStr=['POINT_DATA ' num2str(PointCount)];
fprintf(fid, [LineStr '\n']);
LineStr=['FIELD FieldData ' num2str(length(PolyMesh.PointData))];
fprintf(fid, [LineStr '\n']);
for n=1:length(PolyMesh.PointData)
LineStr=[PolyMesh.PointData(n).Name ' 1 ' num2str(PointCount) ' double'];
fprintf(fid, [LineStr '\n']);
for k=1:PointCount
temp=PolyMesh.PointData(n).Data(k);
LineStr=num2str(temp, precision);
fprintf(fid, [LineStr '\n']);
end
end
end
%FaceData(n).Name
%FaceData(n).Data
if isfield(PolyMesh, 'CellData')
LineStr=['Cell_DATA ' num2str(FaceCount)];
fprintf(fid, [LineStr '\n']);
LineStr=['FIELD FieldData ' num2str(length(PolyMesh.ElementData))];
fprintf(fid, [LineStr '\n']);
for n=1:length(PolyMesh.FaceData)
LineStr=[PolyMesh.ElementData(n).Name ' 1' num2str(FaceCount) ' double'];
fprintf(fid, [LineStr '\n']);
for k=1:FaceCount
temp=PolyMesh.FaceData(n).Data(k);
LineStr=num2str(temp, precision);
fprintf(fid, [LineStr '\n']);
end
end
end
fclose(fid);