forked from OpenFAST/openfast_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtk_file.py
1349 lines (1172 loc) · 44.2 KB
/
vtk_file.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Read/Write VTK files
Part of weio library: https://github.com/ebranlard/weio
"""
import pandas as pd
import numpy as np
import numpy
import os
from functools import reduce
import collections
try:
from .file import File, EmptyFileError, WrongFormatError, BrokenFormatError
except ImportError:
EmptyFileError = type('EmptyFileError', (Exception,),{})
WrongFormatError = type('WrongFormatError', (Exception,),{})
BrokenFormatError = type('BrokenFormatError', (Exception,),{})
File=dict
class VTKFile(File):
"""
Read/write a VTK file (.vtk).
Main attributes for grids:
---------
- xp_grid, yp_grid, zp_grid: vectors of points locations
- point_data_grid: dictionary containing data at the grid points
Main attributes for mesh:
---------
- points
- point_data
- cells
- cell_data
Main methods
------------
- read, write
Examples
--------
vtk = VTKFile('DisXZ1.vtk')
x = vtk.x_grid
z = vtk.z_grid
Ux = vtk.point_data_grid['DisXZ'][:,0,:,0]
"""
@staticmethod
def defaultExtensions():
return ['.vtk','.vtp']
@staticmethod
def formatName():
return 'VTK file'
def __init__(self,filename=None,**kwargs):
self.filename = None
# For regular grid
self.xp_grid=None # location of points
self.yp_grid=None
self.zp_grid=None
self.point_data_grid = None
# Main Data
self.points = None
self.field_data = {}
self.point_data = {}
self.dataset = {}
# Data for reading only
self.cell_data_raw = {}
self.c = None # Cell
self.ct = None # CellTypes
self.active = None
self.is_ascii = False
self.split = []
self.num_items = 0
self.section = None
# Propagate read
if filename:
self.read(filename=filename,**kwargs)
def read(self, filename=None):
""" read a VTK file """
if filename:
self.filename = filename
if not self.filename:
raise Exception('No filename provided')
if not os.path.isfile(self.filename):
raise OSError(2,'File not found:',self.filename)
if os.stat(self.filename).st_size == 0:
raise EmptyFileError('File is empty:',self.filename)
with open(filename, "rb") as f:
# initialize output data
# skip header and title
f.readline()
f.readline()
data_type = f.readline().decode("utf-8").strip().upper()
if data_type not in ["ASCII", "BINARY"]:
raise ReadError('Unknown VTK data type ',data_type)
self.is_ascii = data_type == "ASCII"
while True:
line = f.readline().decode("utf-8")
if not line:
# EOF
break
line = line.strip()
if len(line) == 0:
continue
self.split = line.split()
self.section = self.split[0].upper()
if self.section in vtk_sections:
_read_section(f, self)
else:
_read_subsection(f, self)
# --- Postpro
_check_mesh(self) # generate points if needed
cells, cell_data = translate_cells(self.c, self.ct, self.cell_data_raw)
self.cells = cells
self.cell_data = cell_data
if self.dataset['type']=='STRUCTURED_POINTS':
self.point_data_grid = {}
# We provide point_data_grid, corresponds to point_data but reshaped
for k,PD in self.point_data.items():
# NOTE: tested foe len(y)=1, len(z)=1
self.point_data_grid[k]=PD.reshape(len(self.xp_grid), len(self.yp_grid), len(self.zp_grid),PD.shape[1], order='F')
def write(self, filename=None, binary=True):
"""
Write to unstructured grid
TODO structured grid
"""
if filename:
self.filename = filename
if not self.filename:
raise Exception('No filename provided')
def pad(array):
return np.pad(array, ((0, 0), (0, 1)), "constant")
if self.points.shape[1] == 2:
points = pad(self.points)
else:
points = self.points
if self.point_data:
for name, values in self.point_data.items():
if len(values.shape) == 2 and values.shape[1] == 2:
self.point_data[name] = pad(values)
for name, data in self.cell_data.items():
for k, values in enumerate(data):
if len(values.shape) == 2 and values.shape[1] == 2:
data[k] = pad(data[k])
with open(filename, "wb") as f:
f.write(b"# vtk DataFile Version 4.2\n")
f.write("written \n".encode("utf-8"))
f.write(("BINARY\n" if binary else "ASCII\n").encode("utf-8"))
f.write(b"DATASET UNSTRUCTURED_GRID\n")
# write points and cells
_write_points(f, points, binary)
_write_cells(f, self.cells, binary)
# write point data
if self.point_data:
num_points = self.points.shape[0]
f.write("POINT_DATA {}\n".format(num_points).encode("utf-8"))
_write_field_data(f, self.point_data, binary)
# write cell data
if self.cell_data:
total_num_cells = sum(len(c.data) for c in self.cells)
f.write("CELL_DATA {}\n".format(total_num_cells).encode("utf-8"))
_write_field_data(f, self.cell_data, binary)
def __repr__(self):
s='<{} object> with keys:\n'.format(type(self).__name__)
for k,v in self.items():
s+=' - {}: {}\n'.format(k,v)
return s
def __repr__(self):
""" print function """
def show_grid(v,s):
if v is None:
return
if len(v)==0:
return
if len(v)==1:
lines.append('- {}: [{}], n: {}'.format(s,v[0],len(v)))
else:
lines.append('- {}: [{} ... {}], dx: {}, n: {}'.format(s,v[0],v[-1],v[1]-v[0],len(v)))
lines = ['<{} object> with attributes:'.format(type(self).__name__)]
show_grid(self.xp_grid, 'xp_grid')
show_grid(self.yp_grid, 'yp_grid')
show_grid(self.zp_grid, 'zp_grid')
if self.point_data_grid:
lines.append('- point_data_grid:')
for k,v in self.point_data_grid.items():
lines.append(' "{}" : {}'.format(k,v.shape))
lines.append('- points {}'.format(len(self.points)))
if len(self.cells) > 0:
lines.append("- cells:")
for tpe, elems in self.cells:
lines.append(" {}: {}".format(tpe,len(elems)))
else:
lines.append(" No cells.")
if self.point_data:
lines.append('- point_data:')
for k,v in self.point_data.items():
lines.append(' "{}" : {}'.format(k,v.shape))
if self.cell_data:
names = ", ".join(self.cell_data.keys())
lines.append(" Cell data: {}".format(names))
return "\n".join(lines)
def toDataFrame(self):
return None
# Save FlowData Object to vtk
# """
# n_points = self.dimensions.x1 * self.dimensions.x2 * self.dimensions.x3
# vtk_file = Output(filename)
#self.file = open(self.filename, "w")
#self.ln = "\n"
# vtk_file.write_line('# vtk DataFile Version 3.0')
# vtk_file.write_line('array.mean0D')
# vtk_file.write_line('ASCII')
# vtk_file.write_line('DATASET STRUCTURED_POINTS')
# vtk_file.write_line('DIMENSIONS {}'.format(self.dimensions))
# vtk_file.write_line('ORIGIN {}'.format(self.origin))
# vtk_file.write_line('SPACING {}'.format(self.spacing))
# vtk_file.write_line('POINT_DATA {}'.format(n_points))
# vtk_file.write_line('FIELD attributes 1')
# vtk_file.write_line('UAvg 3 {} float'.format(n_points))
# for u, v, w in zip(self.u, self.v, self.w):
# vtk_file.write_line('{}'.format(Vec3(u, v, w)))
# --- Paraview
# except: from paraview.simple import *
# sliceFile = sliceDir + '/' + tStartStr + '/' + sliceName
# print ' Slice file 1a: ' + sliceFile
# slice_1a_vtk = LegacyVTKReader( FileNames=[sliceFile] )
# sliceFile = sliceDir + '/' + tEndStr + '/' + sliceName
# DataRepresentation3 = GetDisplayProperties(slice_1a_vtk)
# DataRepresentation3.Visibility = 0
# SetActiveSource(slice_1a_vtk)
# --- VTK
# import np
# from vtk import vtkStructuredPointsReader
# from vtk.util import np as VN
#
# reader = vtkStructuredPointsReader()
# reader.SetFileName(filename)
# reader.ReadAllVectorsOn()
# reader.ReadAllScalarsOn()
# reader.Update()
#
# data = reader.GetOutput()
#
# dim = data.GetDimensions()
# vec = list(dim)
# vec = [i-1 for i in dim]
# vec.append(3)
#
# u = VN.vtk_to_np(data.GetCellData().GetArray('velocity'))
# b = VN.vtk_to_numpy(data.GetCellData().GetArray('cell_centered_B'))
#
# u = u.reshape(vec,order='F')
# b = b.reshape(vec,order='F')
#
# x = zeros(data.GetNumberOfPoints())
# y = zeros(data.GetNumberOfPoints())
# z = zeros(data.GetNumberOfPoints())
#
# for i in range(data.GetNumberOfPoints()):
# x[i],y[i],z[i] = data.GetPoint(i)
#
# x = x.reshape(dim,order='F')
# y = y.reshape(dim,order='F')
# z = z.reshape(dim,order='F')
# --- vtk
# import vtk
# import numpy
# import vtk_demo.version as version
#
#
# def main():
# """
# :return: The render window interactor.
# """
#
# chessboard_resolution = 5
# n_lut_colors = 256
# data_max_value = 1
#
# # Provide some geometry
# chessboard = vtk.vtkPlaneSource()
# chessboard.SetXResolution(chessboard_resolution)
# chessboard.SetYResolution(chessboard_resolution)
# num_squares = chessboard_resolution * chessboard_resolution
# # Force an update so we can set cell data
# chessboard.Update()
#
# # Make some arbitrary data to show on the chessboard geometry
# data = vtk.vtkFloatArray()
# for i in range(num_squares):
# if i == 4:
# # This square should in principle light up with color given by SetNanColor below
# data.InsertNextTuple1(numpy.nan)
# else:
# thing = (i * data_max_value) / (num_squares - 1)
# data.InsertNextTuple1(thing)
#
# # Make a LookupTable
# lut = vtk.vtkLookupTable()
# lut.SetNumberOfColors(n_lut_colors)
# lut.Build()
# lut.SetTableRange(0, data_max_value)
# lut.SetNanColor(.1, .5, .99, 1.0) # <------ This color gets used
# for i in range(n_lut_colors):
# # Fill it with arbitrary colors, e.g. grayscale
# x = data_max_value*i/(n_lut_colors-1)
# lut.SetTableValue(i, x, x, x, 1.0)
# lut.SetNanColor(.99, .99, .1, 1.0) # <----- This color gets ignored! ...except by GetNanColor
#
# print(lut.GetNanColor()) # <-- Prints the color set by the last SetNanColor call above!
#
# chessboard.GetOutput().GetCellData().SetScalars(data)
#
# mapper = vtk.vtkPolyDataMapper()
# mapper.SetInputConnection(chessboard.GetOutputPort())
# mapper.SetScalarRange(0, data_max_value)
# mapper.SetLookupTable(lut)
# mapper.Update()
#
# actor = vtk.vtkActor()
# actor.SetMapper(mapper)
#
# renderer = vtk.vtkRenderer()
# ren_win = vtk.vtkRenderWindow()
# ren_win.AddRenderer(renderer)
# renderer.SetBackground(vtk.vtkNamedColors().GetColor3d('MidnightBlue'))
# renderer.AddActor(actor)
#
# iren = vtk.vtkRenderWindowInteractor()
# --- pyvtk
# """Read vtk-file stored previously with tovtk."""
# p = pyvtk.VtkData(filename)
# xn = array(p.structure.points)
# dims = p.structure.dimensions
# try:
# N = eval(p.header.split(" ")[-1])
# --- Extract
# # Convert the center of the turbine coordinate in m to High-Resolution domains left most corner in (i,j)
# xe_index = int(origin_at_precusr[0]/10)
# ye_index = int(origin_at_precusr[1]/10)
#
# # Read the full domain from VTK
# reader = vtk.vtkStructuredPointsReader()
# reader.SetFileName(in_vtk)
# reader.Update()
#
# # Extract the High Resolution domain at same spacial spacing by specifying the (i,i+14),(j,j+14),(k,k+20) tuples
# extract = vtk.vtkExtractVOI()
# extract.SetInputConnection(reader.GetOutputPort())
# extract.SetVOI(xe_index, xe_index+14, ye_index, ye_index+14, 0, 26)
# extract.SetSampleRate(1, 1, 1)
# extract.Update()
#
# # Write the extract as VTK
# points = extract.GetOutput()
# vec = points.GetPointData().GetVectors('Amb')
#
# with open(out_vtk, 'a') as the_file:
# the_file.write('# vtk DataFile Version 3.0\n')
# the_file.write('High\n')
# the_file.write('ASCII\n')
# the_file.write('DATASET STRUCTURED_POINTS\n')
# the_file.write('DIMENSIONS %d %d %d\n' % points.GetDimensions())
# the_file.write('ORIGIN %f %f %f\n' % origin_at_stitch)
# the_file.write('SPACING %f %f %f\n' % points.GetSpacing())
# the_file.write('POINT_DATA %d\n' % points.GetNumberOfPoints())
# the_file.write('VECTORS Amb float\n')
# for i in range(points.GetNumberOfPoints()):
# the_file.write('%f %f %f\n' % vec.GetTuple(i) )
# --- Stitch
# reader = vtk.vtkStructuredPointsReader()
# reader.SetFileName(in_vtk)
# reader.Update()
#
# hAppend = vtk.vtkImageAppend()
# hAppend.SetAppendAxis(0)
# for i in range(nx):
# hAppend.AddInputData(reader.GetOutput())
# hAppend.Update()
#
# vAppend = vtk.vtkImageAppend()
# vAppend.SetAppendAxis(1)
# for i in range(ny):
# vAppend.AddInputData(hAppend.GetOutput())
# vAppend.Update()
#
# points = vAppend.GetOutput()
# vec = points.GetPointData().GetVectors('Amb')
#
# with open(out_vtk, 'a') as the_file:
# the_file.write('# vtk DataFile Version 3.0\n')
# the_file.write('Low\n')
# the_file.write('ASCII\n')
# the_file.write('DATASET STRUCTURED_POINTS\n')
# the_file.write('DIMENSIONS %d %d %d\n' % points.GetDimensions())
# the_file.write('ORIGIN %f %f %f\n' % points.GetOrigin())
# the_file.write('SPACING %f %f %f\n' % points.GetSpacing())
# the_file.write('POINT_DATA %d\n' % points.GetNumberOfPoints())
# the_file.write('VECTORS Amb float\n')
# for i in range(points.GetNumberOfPoints()):
# the_file.write('%f %f %f\n' % vec.GetTuple(i) )
#
# --------------------------------------------------------------------------------}
# --- The code below is taken from meshio
# https://github.com/nschloe/meshio
# The MIT License (MIT)
# Copyright (c) 2015-2020 meshio developers
# --------------------------------------------------------------------------------{
ReadError = BrokenFormatError
WriteError = BrokenFormatError
def _vtk_to_meshio_order(vtk_type, numnodes, dtype=int):
# meshio uses the same node ordering as VTK for most cell types. However, for the
# linear wedge, the ordering of the gmsh Prism [1] is adopted since this is found in
# most codes (Abaqus, Ansys, Nastran,...). In the vtkWedge [2], the normal of the
# (0,1,2) triangle points outwards, while in gmsh this normal points inwards.
# [1] http://gmsh.info/doc/texinfo/gmsh.html#Node-ordering
# [2] https://vtk.org/doc/nightly/html/classvtkWedge.html
if vtk_type == 13:
return numpy.array([0, 2, 1, 3, 5, 4], dtype=dtype)
else:
return numpy.arange(0, numnodes, dtype=dtype)
def _meshio_to_vtk_order(meshio_type, numnodes, dtype=int):
if meshio_type == "wedge":
return numpy.array([0, 2, 1, 3, 5, 4], dtype=dtype)
else:
return numpy.arange(0, numnodes, dtype=dtype)
vtk_to_meshio_type = {
0: "empty",
1: "vertex",
# 2: 'poly_vertex',
3: "line",
# 4: 'poly_line',
5: "triangle",
# 6: 'triangle_strip',
7: "polygon",
8: "pixel",
9: "quad",
10: "tetra",
# 11: 'voxel',
12: "hexahedron",
13: "wedge",
14: "pyramid",
15: "penta_prism",
16: "hexa_prism",
21: "line3",
22: "triangle6",
23: "quad8",
24: "tetra10",
25: "hexahedron20",
26: "wedge15",
27: "pyramid13",
28: "quad9",
29: "hexahedron27",
30: "quad6",
31: "wedge12",
32: "wedge18",
33: "hexahedron24",
34: "triangle7",
35: "line4",
42: "polyhedron",
#
# 60: VTK_HIGHER_ORDER_EDGE,
# 61: VTK_HIGHER_ORDER_TRIANGLE,
# 62: VTK_HIGHER_ORDER_QUAD,
# 63: VTK_HIGHER_ORDER_POLYGON,
# 64: VTK_HIGHER_ORDER_TETRAHEDRON,
# 65: VTK_HIGHER_ORDER_WEDGE,
# 66: VTK_HIGHER_ORDER_PYRAMID,
# 67: VTK_HIGHER_ORDER_HEXAHEDRON,
# Arbitrary order Lagrange elements
68: "VTK_LAGRANGE_CURVE",
69: "VTK_LAGRANGE_TRIANGLE",
70: "VTK_LAGRANGE_QUADRILATERAL",
71: "VTK_LAGRANGE_TETRAHEDRON",
72: "VTK_LAGRANGE_HEXAHEDRON",
73: "VTK_LAGRANGE_WEDGE",
74: "VTK_LAGRANGE_PYRAMID",
# Arbitrary order Bezier elements
75: "VTK_BEZIER_CURVE",
76: "VTK_BEZIER_TRIANGLE",
77: "VTK_BEZIER_QUADRILATERAL",
78: "VTK_BEZIER_TETRAHEDRON",
79: "VTK_BEZIER_HEXAHEDRON",
80: "VTK_BEZIER_WEDGE",
81: "VTK_BEZIER_PYRAMID",
}
meshio_to_vtk_type = {v: k for k, v in vtk_to_meshio_type.items()}
# --------------------------------------------------------------------------------}
# --- Mesh
# --------------------------------------------------------------------------------{
class CellBlock(collections.namedtuple("CellBlock", ["type", "data"])):
def __repr__(self):
return "<meshio CellBlock, type: {}, num cells: {}>".format(self.type,len(self.data))
# --------------------------------------------------------------------------------}
# --- File _vtk.py from meshio
# --------------------------------------------------------------------------------{
vtk_type_to_numnodes = numpy.array(
[
0, # empty
1, # vertex
-1, # poly_vertex
2, # line
-1, # poly_line
3, # triangle
-1, # triangle_strip
-1, # polygon
-1, # pixel
4, # quad
4, # tetra
-1, # voxel
8, # hexahedron
6, # wedge
5, # pyramid
10, # penta_prism
12, # hexa_prism
-1,
-1,
-1,
-1,
3, # line3
6, # triangle6
8, # quad8
10, # tetra10
20, # hexahedron20
15, # wedge15
13, # pyramid13
9, # quad9
27, # hexahedron27
6, # quad6
12, # wedge12
18, # wedge18
24, # hexahedron24
7, # triangle7
4, # line4
]
)
# These are all VTK data types.
# One sometimes finds 'vtktypeint64', but this is ill-formed.
vtk_to_numpy_dtype_name = {
"bit": "bool",
"unsigned_char": "uint8",
"char": "int8",
"unsigned_short": "uint16",
"short": "int16",
"unsigned_int": "uint32",
"int": "int32",
"unsigned_long": "uint64",
"long": "int64",
"float": "float32",
"double": "float64",
"vtktypeint32": "int32", # vtk DataFile Version 5.1
"vtktypeint64": "int64", # vtk DataFile Version 5.1
"vtkidtype": "int32", # may be either 32-bit or 64-bit (VTK_USE_64BIT_IDS)
}
numpy_to_vtk_dtype = {
v: k for k, v in vtk_to_numpy_dtype_name.items() if "vtk" not in k
}
# supported vtk dataset types
vtk_dataset_types = [
"UNSTRUCTURED_GRID",
"STRUCTURED_POINTS",
"STRUCTURED_GRID",
"RECTILINEAR_GRID",
]
# additional infos per dataset type
vtk_dataset_infos = {
"UNSTRUCTURED_GRID": [],
"STRUCTURED_POINTS": [
"DIMENSIONS",
"ORIGIN",
"SPACING",
"ASPECT_RATIO", # alternative for SPACING in version 1.0 and 2.0
],
"STRUCTURED_GRID": ["DIMENSIONS"],
"RECTILINEAR_GRID": [
"DIMENSIONS",
"X_COORDINATES",
"Y_COORDINATES",
"Z_COORDINATES",
],
}
# all main sections in vtk
vtk_sections = [
"METADATA",
"DATASET",
"POINTS",
"CELLS",
"CELL_TYPES",
"POINT_DATA",
"CELL_DATA",
"LOOKUP_TABLE",
"COLOR_SCALARS",
]
def read(filename):
"""Reads a VTK vtk file."""
with open(filename, "rb") as f:
out = read_buffer(f)
return out
def read_buffer(f):
# initialize output data
info = VTKFile()
# skip header and title
f.readline()
f.readline()
data_type = f.readline().decode("utf-8").strip().upper()
if data_type not in ["ASCII", "BINARY"]:
raise WrongFormatError("Unknown VTK data type ",data_type)
info.is_ascii = data_type == "ASCII"
while True:
line = f.readline().decode("utf-8")
if not line:
# EOF
break
line = line.strip()
if len(line) == 0:
continue
info.split = line.split()
info.section = info.split[0].upper()
if info.section in vtk_sections:
_read_section(f, info)
else:
_read_subsection(f, info)
_check_mesh(info)
cells, cell_data = translate_cells(info.c, info.ct, info.cell_data_raw)
info.cells = cells
info.cell_data = cell_data
return info
def _read_section(f, info):
if info.section == "METADATA":
_skip_meta(f)
elif info.section == "DATASET":
info.active = "DATASET"
info.dataset["type"] = info.split[1].upper()
if info.dataset["type"] not in vtk_dataset_types:
raise BrokenFormatError(
"Only VTK '{}' supported (not {}).".format(
"', '".join(vtk_dataset_types), info.dataset["type"]
)
)
elif info.section == "POINTS":
info.active = "POINTS"
info.num_points = int(info.split[1])
data_type = info.split[2].lower()
info.points = _read_points(f, data_type, info.is_ascii, info.num_points)
elif info.section == "CELLS":
info.active = "CELLS"
last_pos = f.tell()
try:
line = f.readline().decode("utf-8")
except UnicodeDecodeError:
line = ""
if "OFFSETS" in line:
# vtk DataFile Version 5.1 - appearing in Paraview 5.8.1 outputs
# No specification found for this file format.
# See the question on ParaView Discourse Forum:
# <https://discourse.paraview.org/t/specification-of-vtk-datafile-version-5-1/5127>.
info.num_offsets = int(info.split[1])
info.num_items = int(info.split[2])
dtype = numpy.dtype(vtk_to_numpy_dtype_name[line.split()[1]])
offsets = _read_cells(f, info.is_ascii, info.num_offsets, dtype)
line = f.readline().decode("utf-8")
assert "CONNECTIVITY" in line
dtype = numpy.dtype(vtk_to_numpy_dtype_name[line.split()[1]])
connectivity = _read_cells(f, info.is_ascii, info.num_items, dtype)
info.c = (offsets, connectivity)
else:
f.seek(last_pos)
info.num_items = int(info.split[2])
info.c = _read_cells(f, info.is_ascii, info.num_items)
elif info.section == "CELL_TYPES":
info.active = "CELL_TYPES"
info.num_items = int(info.split[1])
info.ct = _read_cell_types(f, info.is_ascii, info.num_items)
elif info.section == "POINT_DATA":
info.active = "POINT_DATA"
info.num_items = int(info.split[1])
elif info.section == "CELL_DATA":
info.active = "CELL_DATA"
info.num_items = int(info.split[1])
elif info.section == "LOOKUP_TABLE":
info.num_items = int(info.split[2])
numpy.fromfile(f, count=info.num_items * 4, sep=" ", dtype=float)
# rgba = data.reshape((info.num_items, 4))
elif info.section == "COLOR_SCALARS":
nValues = int(info.split[2])
# re-use num_items from active POINT/CELL_DATA
num_items = info.num_items
dtype = numpy.ubyte
if info.is_ascii:
dtype = float
numpy.fromfile(f, count=num_items * nValues, dtype=dtype)
def _read_subsection(f, info):
if info.active == "POINT_DATA":
d = info.point_data
elif info.active == "CELL_DATA":
d = info.cell_data_raw
elif info.active == "DATASET":
d = info.dataset
else:
d = info.field_data
if info.section in vtk_dataset_infos[info.dataset["type"]]:
if info.section[1:] == "_COORDINATES":
info.num_points = int(info.split[1])
data_type = info.split[2].lower()
d[info.section] = _read_coords(f, data_type, info.is_ascii, info.num_points)
else:
if info.section == "DIMENSIONS":
d[info.section] = list(map(int, info.split[1:]))
else:
d[info.section] = list(map(float, info.split[1:]))
if len(d[info.section]) != 3:
raise BrokenFormatError(
"Wrong number of info in section '{}'. Need 3, got {}.".format(
info.section, len(d[info.section])
)
)
elif info.section == "SCALARS":
d.update(_read_scalar_field(f, info.num_items, info.split, info.is_ascii))
elif info.section == "VECTORS":
d.update(_read_field(f, info.num_items, info.split, [3], info.is_ascii))
elif info.section == "TENSORS":
d.update(_read_field(f, info.num_items, info.split, [3, 3], info.is_ascii))
elif info.section == "FIELD":
d.update(_read_fields(f, int(info.split[2]), info.is_ascii))
else:
raise WrongFormatError("Unknown section ",info.section)
def _check_mesh(info):
if info.dataset["type"] == "UNSTRUCTURED_GRID":
if info.c is None:
raise ReadError("Required section CELLS not found.")
if info.ct is None:
raise ReadError("Required section CELL_TYPES not found.")
elif info.dataset["type"] == "STRUCTURED_POINTS":
dim = info.dataset["DIMENSIONS"]
ori = info.dataset["ORIGIN"]
spa = (
info.dataset["SPACING"]
if "SPACING" in info.dataset
else info.dataset["ASPECT_RATIO"]
)
axis = [
numpy.linspace(ori[i], ori[i] + (dim[i] - 1.0) * spa[i], dim[i])
for i in range(3)
]
info.xp_grid=axis[0]
info.yp_grid=axis[1]
info.zp_grid=axis[2]
info.points = _generate_points(axis)
info.c, info.ct = _generate_cells(dim=info.dataset["DIMENSIONS"])
elif info.dataset["type"] == "RECTILINEAR_GRID":
axis = [
info.dataset["X_COORDINATES"],
info.dataset["Y_COORDINATES"],
info.dataset["Z_COORDINATES"],
]
info.xp_grid=axis[0]
info.yp_grid=axis[1]
info.zp_grid=axis[2]
info.points = _generate_points(axis)
info.c, info.ct = _generate_cells(dim=info.dataset["DIMENSIONS"])
elif info.dataset["type"] == "STRUCTURED_GRID":
info.c, info.ct = _generate_cells(dim=info.dataset["DIMENSIONS"])
# TODO x_grid, y_grid, z_grid points
def _generate_cells(dim):
ele_dim = [d - 1 for d in dim if d > 1]
ele_no = numpy.prod(ele_dim, dtype=int)
spatial_dim = len(ele_dim)
if spatial_dim == 1:
# cells are lines in 1D
cells = numpy.empty((ele_no, 3), dtype=int)
cells[:, 0] = 2
cells[:, 1] = numpy.arange(ele_no, dtype=int)
cells[:, 2] = cells[:, 1] + 1
cell_types = numpy.full(ele_no, 3, dtype=int)
elif spatial_dim == 2:
# cells are quad in 2D
cells = numpy.empty((ele_no, 5), dtype=int)
cells[:, 0] = 4
cells[:, 1] = numpy.arange(0, ele_no, dtype=int)
cells[:, 1] += numpy.arange(0, ele_no, dtype=int) // ele_dim[0]
cells[:, 2] = cells[:, 1] + 1
cells[:, 3] = cells[:, 1] + 2 + ele_dim[0]
cells[:, 4] = cells[:, 3] - 1
cell_types = numpy.full(ele_no, 9, dtype=int)
else:
# cells are hex in 3D
cells = numpy.empty((ele_no, 9), dtype=int)
cells[:, 0] = 8
cells[:, 1] = numpy.arange(ele_no)
cells[:, 1] += (ele_dim[0] + ele_dim[1] + 1) * (
numpy.arange(ele_no) // (ele_dim[0] * ele_dim[1])
)
cells[:, 1] += (numpy.arange(ele_no) % (ele_dim[0] * ele_dim[1])) // ele_dim[0]
cells[:, 2] = cells[:, 1] + 1
cells[:, 3] = cells[:, 1] + 2 + ele_dim[0]
cells[:, 4] = cells[:, 3] - 1
cells[:, 5] = cells[:, 1] + (1 + ele_dim[0]) * (1 + ele_dim[1])
cells[:, 6] = cells[:, 5] + 1
cells[:, 7] = cells[:, 5] + 2 + ele_dim[0]
cells[:, 8] = cells[:, 7] - 1
cell_types = numpy.full(ele_no, 12, dtype=int)
return cells.reshape(-1), cell_types
def _generate_points(axis):
x_dim = len(axis[0])
y_dim = len(axis[1])
z_dim = len(axis[2])
pnt_no = x_dim * y_dim * z_dim
x_id, y_id, z_id = numpy.mgrid[0:x_dim, 0:y_dim, 0:z_dim]
points = numpy.empty((pnt_no, 3), dtype=axis[0].dtype)
# VTK sorts points and cells in Fortran order
points[:, 0] = axis[0][x_id.reshape(-1, order="F")]
points[:, 1] = axis[1][y_id.reshape(-1, order="F")]
points[:, 2] = axis[2][z_id.reshape(-1, order="F")]
return points
def _read_coords(f, data_type, is_ascii, num_points):
dtype = numpy.dtype(vtk_to_numpy_dtype_name[data_type])
if is_ascii:
coords = numpy.fromfile(f, count=num_points, sep=" ", dtype=dtype)
else:
# Binary data is big endian, see
# <https://www.vtk.org/Wiki/VTK/Writing_VTK_files_using_python#.22legacy.22>.
dtype = dtype.newbyteorder(">")
coords = numpy.fromfile(f, count=num_points, dtype=dtype)
line = f.readline().decode("utf-8")
if line != "\n":
raise ReadError()
return coords
def _read_points(f, data_type, is_ascii, num_points):
dtype = numpy.dtype(vtk_to_numpy_dtype_name[data_type])
if is_ascii:
points = numpy.fromfile(f, count=num_points * 3, sep=" ", dtype=dtype)
else:
# Binary data is big endian, see
# <https://www.vtk.org/Wiki/VTK/Writing_VTK_files_using_python#.22legacy.22>.
dtype = dtype.newbyteorder(">")
points = numpy.fromfile(f, count=num_points * 3, dtype=dtype)
line = f.readline().decode("utf-8")
if line != "\n":
raise ReadError()
return points.reshape((num_points, 3))
def _read_cells(f, is_ascii, num_items, dtype=numpy.dtype("int32")):
if is_ascii:
c = numpy.fromfile(f, count=num_items, sep=" ", dtype=dtype)
else:
dtype = dtype.newbyteorder(">")
c = numpy.fromfile(f, count=num_items, dtype=dtype)
line = f.readline().decode("utf-8")
if line != "\n":
raise ReadError()
return c
def _read_cell_types(f, is_ascii, num_items):
if is_ascii:
ct = numpy.fromfile(f, count=int(num_items), sep=" ", dtype=int)
else:
# binary
ct = numpy.fromfile(f, count=int(num_items), dtype=">i4")
line = f.readline().decode("utf-8")
# Sometimes, there's no newline at the end
if line.strip() != "":
raise ReadError()
return ct
def _read_scalar_field(f, num_data, split, is_ascii):
data_name = split[1]
data_type = split[2].lower()
try:
num_comp = int(split[3])
except IndexError:
num_comp = 1
# The standard says:
# > The parameter numComp must range between (1,4) inclusive; [...]
if not (0 < num_comp < 5):
raise ReadError("The parameter numComp must range between (1,4) inclusive")
dtype = numpy.dtype(vtk_to_numpy_dtype_name[data_type])
lt, _ = f.readline().decode("utf-8").split()
if lt.upper() != "LOOKUP_TABLE":
raise ReadError()
if is_ascii:
data = numpy.fromfile(f, count=num_data * num_comp, sep=" ", dtype=dtype)