File: TestHyperTreeGrid3DCursorsMandel.py

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,984 kB
  • sloc: cpp: 2,336,570; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 181; javascript: 165; objc: 153; tcl: 59
file content (308 lines) | stat: -rwxr-xr-x 8,299 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python
from vtkmodules.vtkCommonCore import (
    vtkBitArray,
    vtkDoubleArray,
    vtkUnsignedCharArray,
)
from vtkmodules.vtkCommonDataModel import (
    vtkHyperTreeGrid,
    vtkHyperTreeGridNonOrientedCursor,
    vtkHyperTreeGridNonOrientedGeometryCursor,
    vtkHyperTreeGridNonOrientedMooreSuperCursor,
    vtkHyperTreeGridNonOrientedUnlimitedMooreSuperCursor,
    vtkHyperTreeGridNonOrientedVonNeumannSuperCursor,
)

ROOT_SPLIT = 10
TARGET_LEVEL = 8
TARGET_LEVEL = 6
CUT_OFF = TARGET_LEVEL

# -----------------------------------------------------------------------------
# Helpers
# -----------------------------------------------------------------------------


def mandelbrotTest(x, y, timeStep=0):
    count = 0
    cReal = float(x)
    cImag = float(y)
    zReal = 0.0
    zImag = float(timeStep) / 10.0

    zReal2 = zReal * zReal
    zImag2 = zImag * zImag
    v1 = zReal2 + zImag2
    while v1 < 4.0 and count < 100:
        zImag = 2.0 * zReal * zImag + cImag
        zReal = zReal2 - zImag2 + cReal
        zReal2 = zReal * zReal
        zImag2 = zImag * zImag
        count += 1
        v1 = zReal2 + zImag2

    return count == 100


def mandelbrotSide(bounds):
    count = 1
    if mandelbrotTest(bounds[0], bounds[2]):
        count += 1
    if mandelbrotTest(bounds[1], bounds[2]):
        count += 1
    if mandelbrotTest(bounds[0], bounds[3]):
        count += 1
    if mandelbrotTest(bounds[1], bounds[3]):
        count += 1
    return count


def shouldRefine(level, bounds):
    if level >= TARGET_LEVEL:
        return False
    origin = mandelbrotTest(bounds[0], bounds[2])
    originX = mandelbrotTest(bounds[1], bounds[2])
    originY = mandelbrotTest(bounds[0], bounds[3])
    originXY = mandelbrotTest(bounds[1], bounds[3])
    canRefine = bounds[4] < 0.01

    if canRefine:
        if origin and originX and originY and originXY:
            return False
        if not origin and not originX and not originY and not originXY:
            return False
        return True
    return False


def handleNode(cursor, sideArray, levelArray):
    cellBounds = [0, -1, 0, -1, 0, -1]
    cursor.GetBounds(cellBounds)

    # Add field
    idx = cursor.GetGlobalNodeIndex()
    side = mandelbrotSide(cellBounds)
    sideArray.InsertTuple1(idx, side)
    mask.InsertTuple1(idx, side < CUT_OFF)

    if cursor.IsLeaf():
        if shouldRefine(cursor.GetLevel(), cellBounds):
            cursor.SubdivideLeaf()
            handleNode(cursor, sideArray, mask)

    else:
        for childIdx in range(cursor.GetNumberOfChildren()):
            cursor.ToChild(childIdx)
            handleNode(cursor, sideArray, mask)
            cursor.ToParent()


# -----------------------------------------------------------------------------
# Create Simple HTG
# -----------------------------------------------------------------------------

htg = vtkHyperTreeGrid()
htg.Initialize()
htg.SetDimensions(
    [ROOT_SPLIT + 1, ROOT_SPLIT + 1, 2]
)  # nb cells, not nb points : GridCell [ROOT_SPLIT, ROOT_SPLIT, 1]
htg.SetBranchFactor(2)

sideArray = vtkUnsignedCharArray()
sideArray.SetName("sideArray")
sideArray.SetNumberOfValues(0)
sideArray.SetNumberOfComponents(1)
htg.GetCellData().AddArray(sideArray)

mask = vtkBitArray()
mask.SetName("mask")

# X[-1.75, 0.75]
xValues = vtkDoubleArray()
xValues.SetNumberOfValues(ROOT_SPLIT + 1)
for i in range(ROOT_SPLIT + 1):
    xValues.SetValue(i, -1.75 + float(i) * 0.25)
htg.SetXCoordinates(xValues)

# Y[-1.25, 1.25]
yValues = vtkDoubleArray()
yValues.SetNumberOfValues(ROOT_SPLIT + 1)
for i in range(ROOT_SPLIT + 1):
    yValues.SetValue(i, -1.25 + float(i) * 0.25)
htg.SetYCoordinates(yValues)

# Z[0, 0]
zValues = vtkDoubleArray()
zValues.SetNumberOfValues(2)
zValues.SetValue(0, 0)
zValues.SetValue(1, 0.25)
htg.SetZCoordinates(zValues)

geoCursor = vtkHyperTreeGridNonOrientedGeometryCursor()

offsetIndex = 0
for treeId in range(htg.GetMaxNumberOfTrees()):
    htg.InitializeNonOrientedGeometryCursor(geoCursor, treeId, True)
    geoCursor.SetGlobalIndexStart(offsetIndex)
    handleNode(geoCursor, sideArray, mask)
    offsetIndex += geoCursor.GetTree().GetNumberOfVertices()

print("offsetIndex: ", offsetIndex)

# Squeeze
htg.Squeeze()

# Select an active scalar field
htg.GetCellData().SetActiveScalars("sideArray")

# DataRange sideArray on PointData HTG
dataRange = htg.GetCellData().GetArray("sideArray").GetRange()
print("sideArray on PointData HTG:", dataRange)
print("HTG:", htg.GetNumberOfCells())


# Tests cursors... in Python
# maxDepth allows for testing even unlimited cursors
def recursive(cursor, maxDepth=-1):
    if cursor.IsLeaf() or maxDepth == 0:
        return 1
    nb = 0
    for ichild in range(cursor.GetNumberOfChildren()):
        cursor.ToChild(ichild)
        nb += recursive(cursor, maxDepth - 1)
        cursor.ToParent()
    return nb


def nonOrientedCursor(htg, expected):
    nb = 0
    cursor = vtkHyperTreeGridNonOrientedCursor()
    for treeId in range(htg.GetMaxNumberOfTrees()):
        htg.InitializeNonOrientedCursor(cursor, treeId)
        if not cursor.IsMasked():
            nb += recursive(cursor)
    print("nb: ", nb)
    if expected and nb != expected:
        print("ERROR Not corresponding expected value")
    return nb


expected = nonOrientedCursor(htg, None)


def nonOrientedGeometryCursor(htg, expected):
    nb = 0
    cursor = vtkHyperTreeGridNonOrientedGeometryCursor()
    for treeId in range(htg.GetMaxNumberOfTrees()):
        htg.InitializeNonOrientedGeometryCursor(cursor, treeId)
        if not cursor.IsMasked():
            nb += recursive(cursor)
    print("nb: ", nb)
    if expected and nb != expected:
        print("ERROR Not corresponding expected value")
    return nb


nonOrientedGeometryCursor(htg, expected)


def nonOrientedVonNeumannSuperCursor(htg, expected):
    nb = 0
    cursor = vtkHyperTreeGridNonOrientedVonNeumannSuperCursor()
    for treeId in range(htg.GetMaxNumberOfTrees()):
        htg.InitializeNonOrientedVonNeumannSuperCursor(cursor, treeId)
        if not cursor.IsMasked():
            nb += recursive(cursor)
    print("nb: ", nb)
    if expected and nb != expected:
        print("ERROR Not corresponding expected value")
    return nb


nonOrientedVonNeumannSuperCursor(htg, expected)


def nonOrientedMooreSuperCursor(htg, expected):
    nb = 0
    cursor = vtkHyperTreeGridNonOrientedMooreSuperCursor()
    for treeId in range(htg.GetMaxNumberOfTrees()):
        htg.InitializeNonOrientedMooreSuperCursor(cursor, treeId)
        if not cursor.IsMasked():
            nb += recursive(cursor)
    print("nb: ", nb)
    if expected and nb != expected:
        print("ERROR Not corresponding expected value")
    return nb


nonOrientedMooreSuperCursor(htg, expected)


def nonOrientedUnlimitedMooreSuperCursor(htg):
    nb = 0
    cursor = vtkHyperTreeGridNonOrientedUnlimitedMooreSuperCursor()
    for treeId in range(3):
        htg.InitializeNonOrientedUnlimitedMooreSuperCursor(cursor, treeId)
        nb += recursive(cursor, 3)
    if nb != (3 * 512):
        print("ERROR Unexpected value for unlimited cursor")
    return nb


nonOrientedUnlimitedMooreSuperCursor(htg)


# Test Find
findx = [
    -1.75 + float(ROOT_SPLIT) * 0.5 * 0.25,
    -1.75 + float(ROOT_SPLIT) * 0.5 * 0.25,
    0.11,
]
print("--------- FindNonOrientedGeometryCursor ---------")
cursor = htg.FindNonOrientedGeometryCursor(findx)
cursor.UnRegister(htg)

bbox = [
    -1.0,
    -1.0,
    -1.0,
    -1.0,
    -1.0,
    -1.0,
]
cursor.GetBounds(bbox)
print("bbox: ", bbox)

point = [
    -1.0,
    -1.0,
    -1.0,
]
cursor.GetPoint(point)
print("point: ", point[0], point[1], point[2])

print("IsLeaf: ", cursor.IsLeaf())
assert cursor.IsLeaf()

print(bbox[0], " <= ", findx[0], " and ", findx[0], " <= ", bbox[1])

if bbox[0] <= findx[0] and findx[0] <= bbox[1]:
    pass
else:
    assert bbox[0] <= findx[0] and findx[0] <= bbox[1]

print(bbox[2], " <= ", findx[1], " and ", findx[1], " <= ", bbox[3])

if bbox[2] <= findx[1] and findx[1] <= bbox[3]:
    pass
else:
    assert bbox[2] <= findx[1] and findx[1] <= bbox[3]

print(bbox[4], " <= ", findx[2], " and ", findx[2], " <= ", bbox[5])

if bbox[4] <= findx[2] and findx[2] <= bbox[5]:
    pass
else:
    assert bbox[4] <= findx[2] and findx[2] <= bbox[5]

# --- end of script --