1#!/usr/bin/env python
2import vtk
3
4htg = vtk.vtkHyperTreeGrid()
5htg.Initialize()
6
7scalarArray = vtk.vtkDoubleArray()
8scalarArray.SetName('scalar')
9scalarArray.SetNumberOfValues(0)
10htg.GetCellData().AddArray(scalarArray)
11htg.GetCellData().SetActiveScalars('scalar')
12
13htg.SetDimensions([4, 3, 3])
14htg.SetBranchFactor(2)
15
16# Rectilinear grid coordinates
17xValues = vtk.vtkDoubleArray()
18xValues.SetNumberOfValues(4)
19xValues.SetValue(0, -1)
20xValues.SetValue(1, 0)
21xValues.SetValue(2, 1)
22xValues.SetValue(3, 2)
23htg.SetXCoordinates(xValues);
24
25yValues = vtk.vtkDoubleArray()
26yValues.SetNumberOfValues(3)
27yValues.SetValue(0, -1)
28yValues.SetValue(1, 0)
29yValues.SetValue(2, 1)
30htg.SetYCoordinates(yValues);
31
32zValues = vtk.vtkDoubleArray()
33zValues.SetNumberOfValues(4)
34zValues.SetValue(0, -1)
35zValues.SetValue(1, 0)
36zValues.SetValue(2, 1)
37zValues.SetValue(3, 2)
38htg.SetZCoordinates(zValues);
39
40# Let's split the various trees
41cursor = vtk.vtkHyperTreeGridNonOrientedCursor()
42offsetIndex = 0
43
44# ROOT CELL 0-5
45for iHT in range(6):
46  htg.InitializeNonOrientedCursor(cursor, iHT, True)
47  cursor.SetGlobalIndexStart(offsetIndex)
48  idx = cursor.GetGlobalNodeIndex()
49  scalarArray.InsertTuple1(idx, iHT+1)
50  offsetIndex += cursor.GetTree().GetNumberOfVertices()
51
52# ROOT CELL 6
53htg.InitializeNonOrientedCursor(cursor, 6, True)
54cursor.SetGlobalIndexStart(offsetIndex)
55idx = cursor.GetGlobalNodeIndex()
56scalarArray.InsertTuple1(idx, 7)
57cursor.SubdivideLeaf()
58
59# ROOT CELL 6/[0-7]
60for ichild in range(8):
61  cursor.ToChild(ichild)
62  idx = cursor.GetGlobalNodeIndex()
63  scalarArray.InsertTuple1(idx, 13+ichild)
64  cursor.ToParent()
65
66offsetIndex += cursor.GetTree().GetNumberOfVertices()
67
68# ROOT CELL 7
69htg.InitializeNonOrientedCursor(cursor, 7, True)
70cursor.SetGlobalIndexStart(offsetIndex)
71idx = cursor.GetGlobalNodeIndex()
72scalarArray.InsertTuple1(idx, 8)
73
74offsetIndex += cursor.GetTree().GetNumberOfVertices()
75
76# ROOT CELL 8
77htg.InitializeNonOrientedCursor(cursor, 8, True)
78cursor.SetGlobalIndexStart(offsetIndex)
79idx = cursor.GetGlobalNodeIndex()
80scalarArray.InsertTuple1(idx, 9)
81
82cursor.SubdivideLeaf()
83
84# ROOT CELL 8/[0-7]
85for ichild in range(8):
86  cursor.ToChild(ichild)
87  idx = cursor.GetGlobalNodeIndex()
88  scalarArray.InsertTuple1(idx, 21+ichild)
89  cursor.ToParent()
90
91offsetIndex += cursor.GetTree().GetNumberOfVertices()
92
93# ROOT CELL 9
94htg.InitializeNonOrientedCursor(cursor, 9, True)
95cursor.SetGlobalIndexStart(offsetIndex)
96idx = cursor.GetGlobalNodeIndex()
97scalarArray.InsertTuple1(idx, 10)
98
99offsetIndex += cursor.GetTree().GetNumberOfVertices()
100
101# ROOT CELL 10
102htg.InitializeNonOrientedCursor(cursor, 10, True)
103cursor.SetGlobalIndexStart(offsetIndex)
104idx = cursor.GetGlobalNodeIndex()
105scalarArray.InsertTuple1(idx, 11)
106
107cursor.SubdivideLeaf()
108
109# ROOT CELL 10/[0-7]
110for ichild in range(8):
111  cursor.ToChild(ichild)
112  idx = cursor.GetGlobalNodeIndex()
113  scalarArray.InsertTuple1(idx, 29+ichild)
114  cursor.ToParent()
115
116cursor.ToChild(7)
117cursor.SubdivideLeaf()
118# ROOT CELL 4/3/[0-3]
119for ichild in range(8):
120  cursor.ToChild(ichild)
121  idx = cursor.GetGlobalNodeIndex()
122  scalarArray.InsertTuple1(idx, 37+ichild)
123  cursor.ToParent()
124
125cursor.ToChild(4)
126cursor.SubdivideLeaf()
127
128# ROOT CELL 4/3/0/[0-3]
129for ichild in range(8):
130  cursor.ToChild(ichild)
131  idx = cursor.GetGlobalNodeIndex()
132  scalarArray.InsertTuple1(idx, 46+ichild)
133  cursor.ToParent()
134
135offsetIndex += cursor.GetTree().GetNumberOfVertices()
136
137# ROOT CELL 11
138htg.InitializeNonOrientedCursor(cursor, 11, True)
139cursor.SetGlobalIndexStart(offsetIndex)
140idx = cursor.GetGlobalNodeIndex()
141scalarArray.InsertTuple1(idx, 12)
142
143print('#',scalarArray.GetNumberOfTuples())
144print('DataRange: ',scalarArray.GetRange())
145
146isFilter = False
147
148# Axis reflection
149reflection = None
150if True:
151  print('With AxisReflection Filter (HTG)')
152  reflection = vtk.vtkHyperTreeGridAxisReflection()
153  if isFilter:
154    reflection.SetInputConnection(htg.GetOutputPort())
155  else:
156    reflection.SetInputData(htg)
157  reflection.SetPlaneToZ()
158  reflection.SetCenter(0)
159  isFilter = True
160else:
161  print('No AxisReflection Filter (HTG)')
162  reflection = htg
163
164# Geometries
165geometry = vtk.vtkHyperTreeGridGeometry()
166if isFilter:
167  geometry.SetInputConnection(reflection.GetOutputPort())
168else:
169  geometry.SetInputData(reflection)
170print('With Geometry Filter (HTG to NS)')
171
172# Shrink Filter
173if True:
174  print('With Shrink Filter (NS)')
175  # En 3D, le shrink ne doit pas se faire sur la geometrie car elle ne represente que la peau
176  shrink = vtk.vtkShrinkFilter()
177  shrink.SetInputConnection(geometry.GetOutputPort())
178  shrink.SetShrinkFactor(.8)
179else:
180  print('No Shrink Filter (NS)')
181  shrink = geometry
182
183# LookupTable
184lut = vtk.vtkLookupTable()
185lut.SetHueRange(0.66, 0)
186lut.UsingLogScale()
187lut.Build()
188
189# Mappers
190mapper = vtk.vtkDataSetMapper()
191mapper.SetInputConnection(geometry.GetOutputPort())
192
193mapper.SetLookupTable(lut)
194mapper.SetColorModeToMapScalars()
195mapper.SetScalarModeToUseCellFieldData()
196mapper.SelectColorArray('scalar')
197dataRange = [1,53] # Forced for compare with 3DMask
198mapper.SetScalarRange(dataRange[0], dataRange[1])
199
200# Actors
201actor1 = vtk.vtkActor()
202actor1.SetMapper(mapper)
203
204actor2 = vtk.vtkActor()
205actor2.SetMapper(mapper)
206actor2.GetProperty().SetColor(0, 0, 0)
207actor2.GetProperty().SetRepresentationToWireframe()
208
209# Camera
210shrink.Update()
211bd = shrink.GetOutput().GetBounds()
212camera = vtk.vtkCamera()
213camera.SetClippingRange(1., 100.)
214focal = []
215for i in range(3):
216  focal.append(bd[ 2 * i ] + (bd[ 2 * i + 1 ] - bd[ 2 * i]) / 2.)
217camera.SetFocalPoint(focal)
218camera.SetPosition(focal[0]+4, focal[1]+3, focal[2] + 6.)
219
220# Renderer
221renderer = vtk.vtkRenderer()
222renderer.SetActiveCamera(camera)
223renderer.AddActor(actor1)
224renderer.AddActor(actor2)
225
226# Render window
227renWin = vtk.vtkRenderWindow()
228renWin.AddRenderer(renderer)
229renWin.SetSize(600, 400)
230
231# Render window interactor
232iren = vtk.vtkRenderWindowInteractor()
233iren.SetRenderWindow(renWin)
234
235# render the image
236renWin.Render()
237# iren.Start()
238
239# prevent the tk window from showing up then start the event loop
240# --- end of script --
241