-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtk.py
59 lines (48 loc) · 1.43 KB
/
vtk.py
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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 13 11:17:37 2012
@author: pietro
"""
from __future__ import print_function
VTKTXT = """# vtk DataFile Version 3.1
{desc}
ASCII
DATASET UNSTRUCTURED_GRID
POINTS {lenpnts} FLOAT
{pnts}
CELLS {lentri} {celldim}
3 {tri}
CELL_TYPES {lentri}
{celltyp}
POINT_DATA {lenpnts}
{scalar}
"""
SCALARDATA = """SCALARS {label} FLOAT
LOOKUP_TABLE default
{values}
"""
def exportVTK(fname, points, triangles, pointsdata, description = ''):
pnts = '\n'.join(['{0} {1} {2}'.format(x,y,z) for x, y, z in points])
tri = '\n3 '.join(['{0} {1} {2}'.format(x,y,z) for x, y, z in triangles])
celltypes = [5,] * len(triangles)
celltyp = ' '.join([str(i) for i in celltypes])
data = []
for key, vals in pointsdata.items():
values = '\n'.join(['{0}'.format(v) for v in vals])
data.append(SCALARDATA.format(label = key, values = values ))
scalar = '\n'.join(data)
lentri = len(triangles)
lenpnts = len(points)
celldim = 4*len(triangles)
filename = file(fname,'w+')
print(VTKTXT.format(desc=description, lenpnts=lenpnts, pnts=pnts,
lentri=lentri, tri=tri,
celldim=celldim, celltyp=celltyp, scalar=scalar),
file = filename)
p = [[0,0,0],[1,1,1],[1,0,1],[0,1,1]]
tri = [[0,1,2],[0,1,3]]
pdata = {'temperature' : [0,]*len(p),
'pressure' : [1,]*len(p)}
print('start')
exportVTK('prova.vtk', p, tri, pdata)
print('done!')