-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmakevtk.m
222 lines (195 loc) · 6.48 KB
/
makevtk.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
function makevtk(name,mesh)
%MAKEVTK make a *.VTK file for JIGSAW.
%
% MAKEVTK(NAME,MESH);
%
% The following entities are optionally written to "NAME.VTK". Ent-
% ities are written if they are present in the sructure MESH:
%
% MESH.POINT.COORD - [NPxND] array of point coordinates, where ND
% is the number of spatial dimenions.
%
% MESH.EDGE2.INDEX - [N2x 3] array of indexing for edge-2 elements,
% where INDEX(K,1:2) is an array of "points" associated with
% the K-TH edge, and INDEX(K,3) is an ID tag for the K-TH edge.
%
% MESH.TRIA3.INDEX - [N3x 4] array of indexing for tria-3 elements,
% where INDEX(K,1:3) is an array of "points" associated with
% the K-TH tria, and INDEX(K,4) is an ID tag for the K-TH tria.
%
% MESH.QUAD4.INDEX - [N4x 5] array of indexing for quad-4 elements,
% where INDEX(K,1:4) is an array of "points" associated with
% the K-TH quad, and INDEX(K,5) is an ID tag for the K-TH quad.
%
% MESH.TRIA4.INDEX - [M4x 5] array of indexing for tria-4 elements,
% where INDEX(K,1:4) is an array of "points" associated with
% the K-TH tria, and INDEX(K,5) is an ID tag for the K-TH tria.
%
% MESH.HEXA8.INDEX - [M8x 9] array of indexing for hexa-8 elements,
% where INDEX(K,1:8) is an array of "points" associated with
% the K-TH hexa, and INDEX(K,9) is an ID tag for the K-TH hexa.
%
% MESH.WEDG6.INDEX - [M6x 7] array of indexing for wedg-6 elements,
% where INDEX(K,1:6) is an array of "points" associated with
% the K-TH wedg, and INDEX(K,7) is an ID tag for the K-TH wedg.
%
% MESH.PYRA5.INDEX - [M5x 6] array of indexing for pyra-5 elements,
% where INDEX(K,1:5) is an array of "points" associated with
% the K-TH pyra, and INDEX(K,6) is an ID tag for the K-TH pyra.
%
% See also READVTK, MAKEMSH, READMSH, MAKEMESH, READMESH, MAKEOFF,
% READOFF, MAKESTL, READSTL
%
%---------------------------------------------------------------------
% Darren Engwirda
% github.com/dengwirda/jigsaw-matlab
% 22-Mar-2016
% d_engwirda@outlook.com
%---------------------------------------------------------------------
%
if (~ischar (name))
error('NAME must be a valid file-name!') ;
end
if (~isstruct(mesh))
error('MESH must be a valid structure!') ;
end
[path,file,fext] = fileparts(name);
if(~strcmp(lower(fext),'.vtk'))
name = [name,'.vtk'];
end
try
%-- try to write data to file
ffid = fopen(name, 'w') ;
npoint = 0; nedge2 = 0; ntria3 = 0; ntria4 = 0;
nquad4 = 0; nhexa8 = 0; nwedg6 = 0; npyra5 = 0;
if (meshhas(mesh,'point'))
npoint = size(mesh.point.coord,1);
end
if (meshhas(mesh,'edge2'))
nedge2 = size(mesh.edge2.index,1);
end
if (meshhas(mesh,'tria3'))
ntria3 = size(mesh.tria3.index,1);
end
if (meshhas(mesh,'quad4'))
nquad4 = size(mesh.quad4.index,1);
end
if (meshhas(mesh,'tria4'))
ntria4 = size(mesh.tria4.index,1);
end
if (meshhas(mesh,'hexa8'))
nhexa8 = size(mesh.hexa8.index,1);
end
if (meshhas(mesh,'wedg6'))
nwedg6 = size(mesh.wedg6.index,1);
end
if (meshhas(mesh,'pyra5'))
npyra5 = size(mesh.pyra5.index,1);
end
fprintf(ffid,['# vtk DataFile Version 3.0','\n']);
fprintf(ffid,[file,'\n']);
fprintf(ffid,['ASCII','\n']);
fprintf(ffid,['DATASET UNSTRUCTURED_GRID ','\n']);
fprintf(ffid,['POINTS %u double','\n'],npoint);
if (meshhas(mesh,'point'))
%-- write "POINT" data
switch (size(mesh.point.coord,2))
case +3
fprintf(ffid,[repmat('%1.16g ',1,2),'\n'], ...
mesh.point.coord(:,1:2)');
case +4
fprintf(ffid,[repmat('%1.16g ',1,3),'\n'], ...
mesh.point.coord(:,1:3)');
otherwise
error('Unsupported dimensionality!') ;
end
end
nline = nedge2 * 1 + ntria3 * 1 ...
+ ntria4 * 1 + nquad4 * 1 ...
+ nhexa8 * 1 + nwedg6 * 1 ...
+ npyra5 * 1 ;
nints = nedge2 * 3 + ntria3 * 4 ...
+ ntria4 * 5 + nquad4 * 5 ...
+ nhexa8 * 9 + nwedg6 * 7 ...
+ npyra5 * 6 ;
fprintf(ffid,['CELLS %u %u','\n'],[nline,nints]);
if (meshhas(mesh,'edge2'))
%-- write "EDGE2" data
fprintf(ffid,['2 ',repmat('%u ',1,2),'\n'], ...
mesh.edge2.index(:,1:2)'-1);
end
if (meshhas(mesh,'tria3'))
%-- write "TRIA3" data
fprintf(ffid,['3 ',repmat('%u ',1,3),'\n'], ...
mesh.tria3.index(:,1:3)'-1);
end
if (meshhas(mesh,'quad4'))
%-- write "QUAD4" data
fprintf(ffid,['4 ',repmat('%u ',1,4),'\n'], ...
mesh.quad4.index(:,1:3)'-1);
end
if (meshhas(mesh,'tria4'))
%-- write "TRIA4" data
fprintf(ffid,['4 ',repmat('%u ',1,4),'\n'], ...
mesh.tria4.index(:,1:4)'-1);
end
if (meshhas(mesh,'hexa8'))
%-- write "HEXA8" data
fprintf(ffid,['8 ',repmat('%u ',1,8),'\n'], ...
mesh.hexa8.index(:,1:8)'-1);
end
if (meshhas(mesh,'wedg6'))
%-- write "WEDG6" data
fprintf(ffid,['6 ',repmat('%u ',1,6),'\n'], ...
mesh.wedg6.index(:,1:6)'-1);
end
if (meshhas(mesh,'pyra5'))
%-- write "PYRA5" data
fprintf(ffid,['5 ',repmat('%u ',1,5),'\n'], ...
mesh.pyra5.index(:,1:5)'-1);
end
fprintf(ffid,['CELL_TYPES %u','\n'],nline);
vtk_edge2 = + 3 ;
vtk_tria3 = + 5 ;
vtk_quad4 = + 9 ;
vtk_tria4 = +10 ;
vtk_hexa8 = +12 ;
vtk_wedg6 = +13 ;
vtk_pyra5 = +14 ;
if (meshhas(mesh,'edge2'))
%-- write "EDGE2" type
fprintf(ffid,['%u','\n'],repmat(vtk_edge2,1,nedge2));
end
if (meshhas(mesh,'tria3'))
%-- write "TRIA3" type
fprintf(ffid,['%u','\n'],repmat(vtk_tria3,1,ntria3));
end
if (meshhas(mesh,'quad4'))
%-- write "QUAD4" type
fprintf(ffid,['%u','\n'],repmat(vtk_quad4,1,nquad4));
end
if (meshhas(mesh,'tria4'))
%-- write "TRIA4" type
fprintf(ffid,['%u','\n'],repmat(vtk_tria4,1,ntria4));
end
if (meshhas(mesh,'hexa8'))
%-- write "HEXA8" type
fprintf(ffid,['%u','\n'],repmat(vtk_hexa8,1,nhexa8));
end
if (meshhas(mesh,'wedg6'))
%-- write "WEDG6" type
fprintf(ffid,['%u','\n'],repmat(vtk_wedg6,1,nwedg6));
end
if (meshhas(mesh,'pyra5'))
%-- write "PYRA5" type
fprintf(ffid,['%u','\n'],repmat(vtk_pyra5,1,npyra5));
end
fclose(ffid);
catch err
%-- ensure that we close the file regardless!
if (ffid>-1)
fclose(ffid) ;
end
rethrow(err) ;
end
end