TestHyperTreeGrid2DFull.py 9.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python
"""
Create a HTG
without mask
during HTG build we build scalar, used Global Index implicit with SetGlobalIndexStart
SetGlobalIndexStart, one call by HT
"""
import vtk

htg = vtk.vtkHyperTreeGrid()
htg.Initialize()

scalarArray = vtk.vtkDoubleArray()
scalarArray.SetName('scalar')
scalarArray.SetNumberOfValues(0)
Yohann Bearzi's avatar
Yohann Bearzi committed
16
17
htg.GetCellData().AddArray(scalarArray)
htg.GetCellData().SetActiveScalars('scalar')
18

19
20
21
22
vecArray = vtk.vtkDoubleArray()
vecArray.SetName('vector')
vecArray.SetNumberOfValues(0)
vecArray.SetNumberOfComponents(3)
Yohann Bearzi's avatar
Yohann Bearzi committed
23
24
htg.GetCellData().AddArray(vecArray)
htg.GetCellData().SetActiveScalars('vector')
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
htg.SetDimensions([4, 3, 1])
htg.SetBranchFactor(2)

# Rectilinear grid coordinates
xValues = vtk.vtkDoubleArray()
xValues.SetNumberOfValues(4)
xValues.SetValue(0, -1)
xValues.SetValue(1, 0)
xValues.SetValue(2, 1)
xValues.SetValue(3, 2)
htg.SetXCoordinates(xValues)

yValues = vtk.vtkDoubleArray()
yValues.SetNumberOfValues(3)
yValues.SetValue(0, -1)
yValues.SetValue(1, 0)
yValues.SetValue(2, 1)
htg.SetYCoordinates(yValues)

zValues = vtk.vtkDoubleArray()
zValues.SetNumberOfValues(1)
zValues.SetValue(0, 0)
htg.SetZCoordinates(zValues)

# Let's split the various trees
cursor = vtk.vtkHyperTreeGridNonOrientedCursor()
offsetIndex = 0

# ROOT CELL 0
htg.InitializeNonOrientedCursor(cursor, 0, True)
cursor.SetGlobalIndexStart(offsetIndex)

idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 1)
60
61
62
vecArray.InsertComponent(idx, 0, 1)
vecArray.InsertComponent(idx, 1, 1)
vecArray.InsertComponent(idx, 2, 1)
63
64
65
66
67
68
69

cursor.SubdivideLeaf()

# ROOT CELL 0/[0-3]
cursor.ToChild(0)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 7)
70
71
72
vecArray.InsertComponent(idx, 0, 7)
vecArray.InsertComponent(idx, 1, 7)
vecArray.InsertComponent(idx, 2, 7)
73
74
75
76
77
cursor.ToParent()

cursor.ToChild(1)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 8)
78
79
80
vecArray.InsertComponent(idx, 0, 8)
vecArray.InsertComponent(idx, 1, 8)
vecArray.InsertComponent(idx, 2, 8)
81
82
83
84
85
cursor.ToParent()

cursor.ToChild(2)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 9)
86
87
88
vecArray.InsertComponent(idx, 0, 9)
vecArray.InsertComponent(idx, 1, 9)
vecArray.InsertComponent(idx, 2, 9)
89
90
91
92
93
cursor.ToParent()

cursor.ToChild(3)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 10)
94
95
96
vecArray.InsertComponent(idx, 0, 10)
vecArray.InsertComponent(idx, 1, 10)
vecArray.InsertComponent(idx, 2, 10)
97
98
99
100
101
102
103
104
105
106
107
cursor.ToParent()

# ROOT CELL 0

offsetIndex += cursor.GetTree().GetNumberOfVertices()

# ROOT CELL 1
htg.InitializeNonOrientedCursor(cursor, 1, True)
cursor.SetGlobalIndexStart(offsetIndex)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 2)
108
109
110
vecArray.InsertComponent(idx, 0, 2)
vecArray.InsertComponent(idx, 1, 2)
vecArray.InsertComponent(idx, 2, 2)
111
112
113
114
115
116
117
118
119

offsetIndex += cursor.GetTree().GetNumberOfVertices()

# ROOT CELL 2
htg.InitializeNonOrientedCursor(cursor, 2, True)
cursor.SetGlobalIndexStart(offsetIndex)

idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 3)
120
121
122
vecArray.InsertComponent(idx, 0, 3)
vecArray.InsertComponent(idx, 1, 3)
vecArray.InsertComponent(idx, 2, 3)
123
124
125
126
127
128
129

cursor.SubdivideLeaf()

# ROOT CELL 2/[0-3]
cursor.ToChild(0)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 11)
130
131
132
vecArray.InsertComponent(idx, 0, 11)
vecArray.InsertComponent(idx, 1, 11)
vecArray.InsertComponent(idx, 2, 11)
133
134
135
136
137
cursor.ToParent()

cursor.ToChild(1)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 12)
138
139
140
vecArray.InsertComponent(idx, 0, 12)
vecArray.InsertComponent(idx, 1, 12)
vecArray.InsertComponent(idx, 2, 12)
141
142
143
144
145
cursor.ToParent()

cursor.ToChild(2)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 13)
146
147
148
vecArray.InsertComponent(idx, 0, 13)
vecArray.InsertComponent(idx, 1, 13)
vecArray.InsertComponent(idx, 2, 13)
149
150
151
152
153
cursor.ToParent()

cursor.ToChild(3)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 14)
154
155
156
vecArray.InsertComponent(idx, 0, 14)
vecArray.InsertComponent(idx, 1, 14)
vecArray.InsertComponent(idx, 2, 14)
157
158
159
160
161
162
163
164
165
166
167
168
cursor.ToParent()

# ROOT CELL 2

offsetIndex += cursor.GetTree().GetNumberOfVertices()

# ROOT CELL 3
htg.InitializeNonOrientedCursor(cursor, 3, True)
cursor.SetGlobalIndexStart(offsetIndex)

idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 4)
169
170
171
vecArray.InsertComponent(idx, 0, 4)
vecArray.InsertComponent(idx, 1, 4)
vecArray.InsertComponent(idx, 2, 4)
172
173
174
175
176
177
178
179
180

offsetIndex += cursor.GetTree().GetNumberOfVertices()

# ROOT CELL 4
htg.InitializeNonOrientedCursor(cursor, 4, True)
cursor.SetGlobalIndexStart(offsetIndex)

idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 5)
181
182
183
184
vecArray.InsertComponent(idx, 0, 5)
vecArray.InsertComponent(idx, 1, 5)
vecArray.InsertComponent(idx, 2, 5)

185
186
187
188
189
190
191

cursor.SubdivideLeaf()

# ROOT CELL 4/[0-3]
cursor.ToChild(0)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 15)
192
193
194
vecArray.InsertComponent(idx, 0, 15)
vecArray.InsertComponent(idx, 1, 15)
vecArray.InsertComponent(idx, 2, 15)
195
196
197
198
199
cursor.ToParent()

cursor.ToChild(1)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 16)
200
201
202
vecArray.InsertComponent(idx, 0, 16)
vecArray.InsertComponent(idx, 1, 16)
vecArray.InsertComponent(idx, 2, 16)
203
204
205
206
207
cursor.ToParent()

cursor.ToChild(2)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 17)
208
209
210
vecArray.InsertComponent(idx, 0, 17)
vecArray.InsertComponent(idx, 1, 17)
vecArray.InsertComponent(idx, 2, 17)
211
212
213
214
215
cursor.ToParent()

cursor.ToChild(3)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 18)
216
217
218
vecArray.InsertComponent(idx, 0, 18)
vecArray.InsertComponent(idx, 1, 18)
vecArray.InsertComponent(idx, 2, 18)
219
220
221
222
223
224
225

cursor.SubdivideLeaf()

# ROOT CELL 4/3/[0-3]
cursor.ToChild(0)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 19)
226
227
228
vecArray.InsertComponent(idx, 0, 19)
vecArray.InsertComponent(idx, 1, 19)
vecArray.InsertComponent(idx, 2, 19)
229
230
231
232
233
234
235

cursor.SubdivideLeaf()

# ROOT CELL 4/3/0/[0-3]
cursor.ToChild(0)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 23)
236
237
238
vecArray.InsertComponent(idx, 0, 23)
vecArray.InsertComponent(idx, 1, 23)
vecArray.InsertComponent(idx, 2, 23)
239
240
241
242
243
cursor.ToParent()

cursor.ToChild(1)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 24)
244
245
246
vecArray.InsertComponent(idx, 0, 24)
vecArray.InsertComponent(idx, 1, 24)
vecArray.InsertComponent(idx, 2, 24)
247
248
249
250
251
cursor.ToParent()

cursor.ToChild(2)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 25)
252
253
254
vecArray.InsertComponent(idx, 0, 25)
vecArray.InsertComponent(idx, 1, 25)
vecArray.InsertComponent(idx, 2, 25)
255
256
257
258
259
cursor.ToParent()

cursor.ToChild(3)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 26)
260
261
262
vecArray.InsertComponent(idx, 0, 26)
vecArray.InsertComponent(idx, 1, 26)
vecArray.InsertComponent(idx, 2, 26)
263
264
265
266
267
268
269
270
271
cursor.ToParent()

# ROOT CELL 4/3/0
cursor.ToParent()

# ROOT CELL 4/3/[1-3]
cursor.ToChild(1)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 20)
272
273
274
vecArray.InsertComponent(idx, 0, 20)
vecArray.InsertComponent(idx, 1, 20)
vecArray.InsertComponent(idx, 2, 20)
275
276
277
278
279
cursor.ToParent()

cursor.ToChild(2)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 21)
280
281
282
vecArray.InsertComponent(idx, 0, 21)
vecArray.InsertComponent(idx, 1, 21)
vecArray.InsertComponent(idx, 2, 21)
283
284
285
286
287
cursor.ToParent()

cursor.ToChild(3)
idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 22)
288
289
290
vecArray.InsertComponent(idx, 0, 22)
vecArray.InsertComponent(idx, 1, 22)
vecArray.InsertComponent(idx, 2, 22)
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
cursor.ToParent()

# ROOT CELL 4/3

cursor.ToParent()

# ROOT CELL 4

offsetIndex += cursor.GetTree().GetNumberOfVertices()

# ROOT CELL 5
htg.InitializeNonOrientedCursor(cursor, 5, True)
cursor.SetGlobalIndexStart(offsetIndex)

idx = cursor.GetGlobalNodeIndex()
scalarArray.InsertTuple1(idx, 6)
307
308
309
vecArray.InsertComponent(idx, 0, 6)
vecArray.InsertComponent(idx, 1, 6)
vecArray.InsertComponent(idx, 2, 6)
310
311
312
313

print('#',scalarArray.GetNumberOfTuples())
print('DataRange: ',scalarArray.GetRange())

314
315
316
317
print('#',vecArray.GetNumberOfTuples())
print('#',vecArray.GetNumberOfComponents())
print('DataRange: ',vecArray.GetRange())

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
# Geometries
geometry = vtk.vtkHyperTreeGridGeometry()
geometry.SetInputData(htg)
print('With Geometry Filter (HTG to NS)')

# Shrink Filter
if True:
  print('With Shrink Filter (NS)')
  # En 3D, le shrink ne doit pas se faire sur la geometrie car elle ne represente que la peau
  shrink = vtk.vtkShrinkFilter()
  shrink.SetInputConnection(geometry.GetOutputPort())
  shrink.SetShrinkFactor(.8)
else:
  print('No Shrink Filter (NS)')
  shrink = geometry

# LookupTable
lut = vtk.vtkLookupTable()
lut.SetHueRange(0.66, 0)
lut.UsingLogScale()
lut.Build()

# Mappers
mapper = vtk.vtkDataSetMapper()
mapper.SetInputConnection(shrink.GetOutputPort())

mapper.SetLookupTable(lut)
mapper.SetColorModeToMapScalars()
mapper.SetScalarModeToUseCellFieldData()
mapper.SelectColorArray('scalar')
dataRange = [1,26] # Forced for compare with 2DMask
mapper.SetScalarRange(dataRange[0], dataRange[1])

# Actors
actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Camera
bd = htg.GetBounds()
camera = vtk.vtkCamera()
camera.SetClippingRange(1., 100.)
focal = []
for i in range(3):
  focal.append(bd[ 2 * i ] + (bd[ 2 * i + 1 ] - bd[ 2 * i]) / 2.)
camera.SetFocalPoint(focal)
camera.SetPosition(focal[0], focal[1], focal[2] + 4.)

# Renderer
renderer = vtk.vtkRenderer()
renderer.SetActiveCamera(camera)
renderer.AddActor(actor)

# Render window
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)
renWin.SetSize(600, 400)

# Render window interactor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# render the image
renWin.Render()
# iren.Start()

# prevent the tk window from showing up then start the event loop
# --- end of script --