-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateVTUFromDiag.py
executable file
·73 lines (63 loc) · 2.16 KB
/
generateVTUFromDiag.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
import vtk
# Cell: Birth, Persistence, PairType (0),
# Points: CriticalType
def generateVTUFromDiag(diag):
points = vtk.vtkPoints()
criticalTypeArray = vtk.vtkIntArray()
criticalTypeArray.SetName("CriticalType")
allPointsIDs = []
for i in range(len(diag)):
pair = diag[i]
birth = pair[0]
death = pair[1]
pers = death - birth
# Insert Point
pointID1 = points.InsertNextPoint(birth, birth, 0)
pointID2 = points.InsertNextPoint(birth, death, 0)
allPointsIDs.append([pointID1, pointID2])
# Critical Type Array
critType = 0 if i == 0 else 2
criticalTypeArray.InsertNextTuple1(critType)
criticalTypeArray.InsertNextTuple1(3)
grid = vtk.vtkUnstructuredGrid()
grid.SetPoints(points)
grid.GetPointData().AddArray(criticalTypeArray)
birthArray = vtk.vtkFloatArray()
birthArray.SetName("Birth")
persArray = vtk.vtkFloatArray()
persArray.SetName("Persistence")
pairTypeArray = vtk.vtkIntArray()
pairTypeArray.SetName("PairType")
for i in range(len(diag)):
pIDs = allPointsIDs[i]
# Insert cell
pointIds = vtk.vtkIdList()
pointIds.SetNumberOfIds(2)
pointIds.SetId(0, pIDs[0])
pointIds.SetId(1, pIDs[1])
grid.InsertNextCell(vtk.VTK_LINE, pointIds)
#
birthArray.InsertNextTuple1(diag[i][0])
persArray.InsertNextTuple1(diag[i][1] - diag[i][0])
pairTypeArray.InsertNextTuple1(0)
grid.GetCellData().AddArray(birthArray)
grid.GetCellData().AddArray(persArray)
grid.GetCellData().AddArray(pairTypeArray)
return grid
def generateAndSaveDiags(data, baseName="", name=""):
allPD = []
for i in range(len(data)):
pd = generateVTUFromDiag(data[i])
allPD.append(pd)
block = vtk.vtkMultiBlockDataSet()
block.SetNumberOfBlocks(len(allPD))
for i in range(len(allPD)):
block.SetBlock(i, allPD[i])
writer = vtk.vtkXMLMultiBlockDataWriter()
writer.SetInputData(block)
if name == "":
if baseName != "":
baseName += "_"
name = baseName + "pd.vtm"
writer.SetFileName(name)
writer.Update()