Skip to content

Commit 12802fd

Browse files
authoredJul 3, 2020
Merge pull request #7 from Jimbles/dev
Fixed writing vertices and improved comments and docs
2 parents 611de4d + 166382c commit 12802fd

18 files changed

+138
-204
lines changed
 

‎README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ This has only been tested on Matlab 2019b with Python 3.8.3
1919
```matlab
2020
P=meshio.read(fname); % .msh .vtk .vtu .stl .obj TetGen ANSYS ...
2121
22-
% P.vtx - verticies
23-
% P.Cells - structure array for each geometry saved in file
24-
% P.Cells.tri - Trigangulation connectivity list for this cell
25-
% P.Cells.type - 'Tetra','Triangle','Line','Vertex'
26-
27-
% all contents can be plotted with
22+
% P.vtx - verticies
23+
% P.Cells - structure array for each geometry saved in file
24+
% P.Cells.tri - Trigangulation connectivity list for this cell
25+
% P.Cells.type - 'Tetra','Triangle','Line','Vertex'
26+
% P.cell_data - Per element data in cell array
27+
% P.cell_data_name - Data names in cell array
28+
% P.point_data - Per point data in cell array
29+
% P.point_data_name - Data names in cell array
30+
31+
% all contents can be plotted using:
2832
meshio.plot(P);
2933
```
3034

@@ -39,12 +43,22 @@ dt = delaunayTriangulation(x,y,z);
3943
dataex=1:size(dt.ConnectivityList,1);
4044
4145
%write to gmsh file with cell data
42-
meshio.write('example.msh',dt.Points,dt.ConnectivityList,dataex);
46+
meshio.write('example.msh',dt.Points,dt.ConnectivityList,{dataex},{'Data'});
4347
```
4448

4549
This can then be loaded directly into [gmsh](https://gmsh.info/)
50+
4651
![gmsh](examples/figures/RandEx_gmsh.png)
4752

48-
or into paraview (either by writing to .vtu or using meshio paraview plugin)
53+
or into paraview (either by writing to .vtu or loading .msh using meshio paraview plugin)
4954

5055
![paraview](examples/figures/RandEx_paraview.png)
56+
57+
### Convert file
58+
59+
Conversion through `meshio-convert` binary is harder to fit into matlab workflow, so this library can be used too
60+
61+
```matlab
62+
P=meshio.read('example.msh');
63+
meshio.structwrite('example.vtu',P);
64+
```

‎examples/example1.msh

-3.23 KB
Binary file not shown.

‎examples/examplecell.vtu

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎examples/examplecell1.msh

-4.01 KB
Binary file not shown.

‎examples/examplecell2.msh

-5.57 KB
Binary file not shown.

‎examples/examplemulti.vtu

Lines changed: 0 additions & 24 deletions
This file was deleted.

‎examples/examplepoint.vtu

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎examples/examplepoint1.msh

-3.51 KB
Binary file not shown.

‎examples/examplepoint2.msh

-4.08 KB
Binary file not shown.

‎examples/examplepointcell.msh

-6.42 KB
Binary file not shown.

‎examples/examplepointcell.vtu

Lines changed: 0 additions & 46 deletions
This file was deleted.

‎examples/figures/RandEx_gmsh.png

38.7 KB
Loading

‎examples/figures/RandEx_paraview.png

-152 Bytes
Loading

‎examples/finger_3D.vtu

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

‎examples/pyexample.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@
3333
F=meshio.read(inpathmulti)
3434
meshio.write_points_cells(outpathmultivtu,F.points,F.cells)
3535

36-
fc=F.cells
37-
fnewcell=[]
38-
fnewcell.append((fc[1].type,fc[1].data))
39-
fnewcell.append((fc[2].type,fc[2].data))
40-
41-
meshio.write_points_cells(outpathmultivtu,F.points,fnewcell)
4236
PP=meshio.read(outpathmultivtu)
4337

4438

‎examples/run_examples.m

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,67 +18,80 @@
1818
pointdataname={'test','teest','tesest'};
1919

2020
%write with no data
21-
meshio.write('example1.msh',dt.Points,dt.ConnectivityList);
22-
A=meshio.read('example1.msh');
21+
meshio.write('testout/example1.msh',dt.Points,dt.ConnectivityList);
22+
A=meshio.read('testout/example1.msh');
2323

2424
assert(isequal(A.vtx,dt.Points));
2525

2626

2727
%write with cell data
28-
meshio.write('examplecell1.msh',dt.Points,dt.ConnectivityList,celldata(1),celldataname(1));
29-
B=meshio.read('examplecell1.msh');
28+
meshio.write('testout/examplecell1.msh',dt.Points,dt.ConnectivityList,celldata(1),celldataname(1));
29+
B=meshio.read('testout/examplecell1.msh');
3030

3131
assert(isequal(celldata(1),B.cell_data));
3232

33-
meshio.write('examplecell2.msh',dt.Points,dt.ConnectivityList,celldata,celldataname);
34-
C=meshio.read('examplecell2.msh');
33+
meshio.write('testout/examplecell2.msh',dt.Points,dt.ConnectivityList,celldata,celldataname);
34+
C=meshio.read('testout/examplecell2.msh');
3535

3636
assert(isequal(celldata,C.cell_data));
3737

3838

3939
% write with point data
40-
meshio.write('examplepoint1.msh',dt.Points,dt.ConnectivityList,[],[],pointdata(1),pointdataname(1));
41-
D=meshio.read('examplepoint1.msh');
40+
meshio.write('testout/examplepoint1.msh',dt.Points,dt.ConnectivityList,[],[],pointdata(1),pointdataname(1));
41+
D=meshio.read('testout/examplepoint1.msh');
4242

4343
assert(isequal(pointdata(1),D.point_data));
4444

45-
meshio.write('examplepoint2.msh',dt.Points,dt.ConnectivityList,[],[],pointdata,pointdataname);
46-
E=meshio.read('examplepoint2.msh');
45+
meshio.write('testout/examplepoint2.msh',dt.Points,dt.ConnectivityList,[],[],pointdata,pointdataname);
46+
E=meshio.read('testout/examplepoint2.msh');
4747

4848
assert(isequal(pointdata,E.point_data));
4949

5050
%write with both point and cell data
51-
meshio.write('examplepointcell.msh',dt.Points,dt.ConnectivityList,celldata,celldataname,pointdata,pointdataname);
52-
F=meshio.read('examplepointcell.msh');
51+
meshio.write('testout/examplepointcell.msh',dt.Points,dt.ConnectivityList,celldata,celldataname,pointdata,pointdataname);
52+
F=meshio.read('testout/examplepointcell.msh');
5353

5454
assert(isequal(pointdata,F.point_data) && isequal(celldata,F.cell_data));
5555

5656
%convert file using structwrite
57-
meshio.structwrite('examplecell.vtu',C);
58-
meshio.structwrite('examplepoint.vtu',E);
59-
meshio.structwrite('examplepointcell.vtu',F);
60-
57+
meshio.structwrite('testout/examplecell.vtu',C);
58+
meshio.structwrite('testout/examplepoint.vtu',E);
59+
meshio.structwrite('testout/examplepointcell.vtu',F);
6160

6261
%% read gmsh file
6362

6463
% example gmsh file is conversion from .stl file
6564
% this contains vertcies, lines, triangles and tetrahedra which are all read
6665
% order is always vert/line/tri/tetra
67-
fname=('finger_3D.msh');
66+
fname=('finger_3D');
6867

69-
P=meshio.read(fname);
68+
P=meshio.read([fname '.msh']);
7069
meshio.plot(P);
7170

72-
% extract just the lines/triangles/tetrahedra
71+
% extract just the tetrahedra
7372
Ptet=P;
74-
Ptet.cells=Ptet.cells(23:end);
73+
Ptet.cells=Ptet.cells(end);
7574
meshio.plot(Ptet);
7675

77-
meshio.structwrite('examplemulti.vtu',Ptet);
76+
% write file back to vtu N.B. meshio merges cells of the same type
77+
meshio.structwrite([fname '.vtu'],P);
78+
Pvtu=meshio.read([fname '.vtu']);
79+
%Ptvu only has 4 cells, each for all vertex,line,tri,tetra
80+
81+
% meshio cannot write .msh files with different cell types in at the moment
82+
% (meshio v 4.0.16). So write each type to a separate file and merge them
83+
% in gmsh
84+
85+
try
86+
meshio.structwrite('testout/error.msh',P);
87+
88+
catch
89+
fprintf(2,'\n -----------\n meshio can only write 1 cell type (e.g. tetra) to msh file \n -----------\n');
90+
end
7891

7992

8093
%% read vtu and write new data
81-
fname=('NNexample.vtu');
94+
fname='NNexample.vtu';
8295
fnameoutcell='NNexampleNewDataC.vtu';
8396
fnameoutpoint='NNexampleNewDataP.vtu';
8497

‎examples/testout/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

‎src/meshio.m

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
% more formats at:
77
% https://github.com/nschloe/meshio
88
%
9-
% meshio.read - read mesh file
10-
% meshio.write - write matlab mesh to file
11-
% meshio.plot - plot all contents of a meshfile
12-
%
9+
% meshio.read - read mesh file to struct
10+
% meshio.write - write matlab mesh to file
11+
% meshio.plot - plot all contents of a meshfile
12+
% meshio.structwrite - write meshio.read output struct to file
1313
%
1414
% utilities
1515
% meshio.np2mat - np array to matlab array
@@ -64,7 +64,11 @@
6464
%meshio.read read mesh file using python meshio
6565
% Calls python meshio library and processes output to a
6666
% matlab struct
67-
%
67+
%
68+
% Inputs:
69+
% filename - needs extension .msh .vtu .vtk etc.
70+
%
71+
% Output:
6872
% objout.vtx - verticies
6973
% objout.Cells - structure array for each geometry saved in file
7074
% .Cells.tri - Trigangulation connectivity list for this cell
@@ -73,6 +77,12 @@
7377
% objout.cell_data_name - {cell array}
7478
% objout.point_data - data for each vertex {cell array}
7579
% objout.point_data_name - {cell array}
80+
%
81+
% Usage:
82+
% %read data
83+
% M=meshio.read('example.msh');
84+
% %plot contents
85+
% meshio.plot(M);
7686

7787

7888
% ------ load meshes ------
@@ -190,13 +200,33 @@
190200
function fileout = write(filename,points,cells,cell_data,cell_data_name,point_data,point_data_name)
191201
%meshio.write write mesh to file using meshio library
192202
%
193-
% Inputs
194-
% filename - needs extension
195-
% points - vtx
196-
% cells - connectivity array
197-
% [data] - optional must match size of points or nodes
198-
% [dataname] - optional string
199-
203+
% Inputs:
204+
% filename - needs extension .msh .vtu .vtk etc.
205+
% points - vtx coordinates
206+
% cells - connectivity array OR struct of cells.tri cells.type
207+
% as obtained from meshio.read
208+
% [cell_data] - element data in {cell array}
209+
% [cell_data_name] - strings in {cell array}
210+
% [point_data] - optional must match size of points or nodes
211+
% [point_data_name] - optional string
212+
%
213+
%
214+
% Usage - simple example
215+
% x = rand(20,1);
216+
% y = rand(20,1);
217+
% z= rand(20,1);
218+
% dt = delaunayTriangulation(x,y,z);
219+
% meshio.write('test.msh',dt.Points,dt.ConnectivityList);
220+
%
221+
% With cell and point data
222+
% celldata={1:size(dt.ConnectivityList,1)};
223+
% celldataname={'celldata'};
224+
% meshio.write('test.msh',dt.Points,dt.ConnectivityList,celldata,celldataname);
225+
%
226+
% pointdata={1:size(dt.Points,1)};
227+
% pointdataname={'pointdata'};
228+
% meshio.write('test.msh',dt.Points,dt.ConnectivityList,[],[],pointdata,pointdataname);
229+
200230

201231
fprintf('Meshio writing to meshfile : %s\n',filename);
202232

@@ -228,33 +258,37 @@
228258
pycellslist=py.list;
229259

230260
nCells=size(cells,2);
231-
vertexwarn=0;
261+
232262

233263
for iC=1:nCells
234264

235265
% convert cell array correct for python 0 indexing
236266
% meshio needs nodes as int32
237267
pycells = meshio.mat2nparray(int32(cells(iC).tri -1));
238268

239-
%correct for wrong shape if only 1 vertex - Not working as
240-
%getting Python Error: IndexError: tuple index out of range
269+
%correct for wrong shape if only 1 vertex
241270
if size(cells(iC).tri) ==1
242-
pycells=py.numpy.reshape(pycells,int32(1));
243-
if vertexwarn==0
244-
fprintf(2,'Writing verticies not currently working, so ignoring all\n');
245-
vertexwarn=1;
246-
end
247-
continue
271+
pycells.shape=py.tuple({int32(1),int32(1)});
248272
end
249-
273+
250274
% put nodes into a list of CellBlock type
251-
%pycellblock = py.meshio.CellBlock(cells(iC).type,pycells);
252275
pycellblock=py.tuple({cells(iC).type,pycells});
253276

254277
pycellslist.append(pycellblock);
278+
279+
celltypes{iC}=cells(iC).type;
280+
281+
end
282+
283+
% multiple cells of the same type will be merged into single
284+
% cell automatically by meshio, so warn user this is happening
285+
if (size(celltypes,2) > size(unique(celltypes),2))
286+
fprintf(2,'Caution repeated cell types will be merged by meshio \n');
255287
end
256288

257289

290+
291+
258292
% ------ convert cell and point data if needed ------
259293

260294
% check if cell data exists
@@ -383,23 +417,36 @@
383417

384418
function fileout = structwrite(filename,objin)
385419
%meshio.structwrite write file using struct output from meshio.read
386-
% this function is a wrapper for meshio.write
420+
% this function is a wrapper for meshio.write to make it more
421+
% convenient to convert files
422+
%
423+
% Inputs:
424+
% filename - needs extension .msh .vtu .vtk etc.
425+
% objin - struct same as meshio.read output
426+
%
427+
% Usage:
428+
% M=meshio.read('example.msh');
429+
% meshio.structwrite('example.vtu',M);
387430

388431
Docelldata=0;
389432
Dopointdata=0;
390433

434+
% check if cell data is present
391435
if all(isfield(objin,{'cell_data','cell_data_name'}))
392436
if ~isempty(objin.cell_data{1})
393437
Docelldata=1;
394438
end
395439
end
396440

441+
% check if point data is present
397442
if all(isfield(objin,{'point_data','point_data_name'}))
398443
if ~isempty(objin.point_data{1})
399444
Dopointdata=1;
400445
end
401446
end
402447

448+
% call meshio.write with appropriate inputs. this is a kludge
449+
% im sorry!
403450
if (Docelldata && Dopointdata)
404451

405452
meshio.write(filename,objin.vtx,objin.cells,objin.cell_data,objin.cell_data_name,objin.point_data,objin.point_data_name)
@@ -421,7 +468,12 @@
421468
%plot plot all elements within a chosen file
422469
% plots verticies, lines and triangles. plots surface mesh of
423470
% tetra meshes
471+
%
424472
% Use paraview for viewing data as its much better!
473+
%
474+
% Inputs:
475+
% objin - struct from meshio.write
476+
425477
numCells = size(objin.cells,2);
426478
figure
427479
hold on

0 commit comments

Comments
 (0)
Please sign in to comment.