/
transferTonumpy.py
72 lines (56 loc) · 1.99 KB
/
transferTonumpy.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
import vtk
import os
import sys
import numpy as np
import vtk as vtk
from vtk.util import numpy_support
from vtkmodules.vtkCommonDataModel import vtkStructuredPoints
# this is 5.1 anyway
# we can use structured_dataset to write into specific version
def writeDS(fname, ds) :
writer = vtk.vtkDataSetWriter()
writer.SetFileTypeToBinary()
writer.SetFileName(fname)
writer.SetInputData(ds)
writer.Update()
writer.Write()
# we can set the version of the writter if we
# use the dedicated writer to write the data
def writeStructuredDs(fname, ds):
writer = vtk.vtkStructuredPointsWriter()
writer.SetFileName(fname)
writer.SetFileVersion(42)
writer.SetInputData(ds)
writer.Update()
writer.Write()
def readDS(fname) :
reader = vtk.vtkDataSetReader()
reader.SetFileName(fname)
reader.ReadAllVectorsOn()
reader.ReadAllScalarsOn()
reader.Update()
ds = reader.GetOutput()
return ds
if __name__ == "__main__":
#creating sample data set
xdim=10
ydim=20
zdim=30
structured_dataset = vtkStructuredPoints()
structured_dataset.SetDimensions(xdim, ydim, zdim)
structured_dataset.SetOrigin(0, 0, 0)
point_data_raw = np.arange(0,xdim*ydim*zdim,1,dtype=int)
print(point_data_raw.shape)
vtkarray = numpy_support.numpy_to_vtk(point_data_raw)
# the default array name is unknown
vtkarray.SetName("pointArray")
structured_dataset.GetPointData().AddArray(vtkarray)
structured_dataset.GetPointData().SetActiveScalars("pointArray")
# this output use the most general datset writer which is 5.1 version
writeDS("test_raw_10_20_30.vtk",structured_dataset)
# this output use the specific structured point data set writer
# which can be set as the 4.2 version
writeStructuredDs("test_42_raw_10_20_30.vtk",structured_dataset)
# get data from the data set
numpy_array = structured_dataset.GetPointData().GetArray("pointArray")
# we can then do some operation based on the numpy array