File: TestImplicitArrayInstanciation.py

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 205,916 kB
  • sloc: cpp: 2,336,565; 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: 178; javascript: 165; objc: 153; tcl: 59
file content (168 lines) | stat: -rw-r--r-- 6,149 bytes parent folder | download | duplicates (3)
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
# SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
# SPDX-License-Identifier: BSD-3-Clause
import vtkmodules.vtkCommonCore

import math

# 'Char' is tested independently,
# it is the only case that fallback to 'str' python type
# while other are numeric ones, allowing for code factorization.
NUMERIC_TYPES = [
        'Double',
        'Float',
        'Int',
        'IdType',
        'Long',
        'Short',
        'SignedChar',
        'UnsignedChar',
        'UnsignedInt',
        'UnsignedLong',
        'UnsignedShort']

# ------------------------------------------------------------------------------
# Get an instance of the corresponding class
def GetArray(backend, dataType):
    vtkClass = 'vtk' + backend + dataType + 'Array'
    return getattr(vtkmodules.vtkCommonCore, vtkClass)()

# ------------------------------------------------------------------------------
# Compare value with tolerance. Print error if different.
def CheckResult(val, expected):
    if type(val) == str or type(expected) == str:
        if val != expected:
            print("ERROR: has {} instead of {}".format(val, expected))
            return False
    elif not math.isclose(val, expected):
        print("ERROR: has {} instead of {}".format(val, expected))
        return False

    return True

# ------------------------------------------------------------------------------
def TestConstantArray(constArray, value):
    constArray.SetNumberOfTuples(42)
    constArray.ConstructBackend(value)
    if not CheckResult(constArray.GetValue(1), value):
        print("ERROR: {} failed.".format(type(constArray)))

# ------------------------------------------------------------------------------
def TestAffineArray(affineArray, slope, intercept):
    affineArray.SetNumberOfTuples(42)
    affineArray.ConstructBackend(slope, intercept)
    if not CheckResult(affineArray.GetValue(1), slope + intercept):
        print("ERROR: {} failed.".format(type(affineArray)))

# ------------------------------------------------------------------------------
# dedicated test needed to avoid issue with type conversion
def TestAffineCharArray(affineArray, slope, intercept):
    affineArray.SetNumberOfTuples(42)
    affineArray.ConstructBackend(slope, intercept)
    if not CheckResult(affineArray.GetValue(1), chr(ord(slope) + ord(intercept))):
        print("ERROR: {} failed.".format(type(affineArray)))

# ------------------------------------------------------------------------------
def TestIndexedArray(indexedArray, originalArray):
    indexedArray.SetNumberOfTuples(4)
    indexes = vtkmodules.vtkCommonCore.vtkIdTypeArray()
    size = originalArray.GetNumberOfTuples()
    indexes.InsertNextValue(0)
    indexes.InsertNextValue(1)
    indexes.InsertNextValue(size - 2)
    indexes.InsertNextValue(size - 1)

    indexedArray.ConstructBackend(indexes, originalArray)
    if not CheckResult(indexedArray.GetValue(2), originalArray.GetValue(size - 2)):
        print("ERROR: {} failed.".format(type(indexedArray)))

# ------------------------------------------------------------------------------
def TestCompositeArray(compositeArray, arrayList):
    arrays = vtkmodules.vtkCommonCore.vtkDataArrayCollection()

    firstsize = 13
    first = arrayList[0]
    for idx in range(firstsize):
        first.InsertNextValue(idx)
    arrays.AddItem(first)

    second = arrayList[1]
    secondsize = 14
    for idx in range(firstsize):
        first.InsertNextValue(firstsize + idx)
    arrays.AddItem(second)

    compositeArray.SetNumberOfTuples(firstsize + secondsize)
    compositeArray.ConstructBackend(arrays)

    if not CheckResult(compositeArray.GetValue(firstsize + 1), firstsize+1):
        print("ERROR: {} failed.".format(type(constArray)))

# ------------------------------------------------------------------------------
def TestCompositeCharArray(compositeArray, arrayList):
    arrays = vtkmodules.vtkCommonCore.vtkDataArrayCollection()

    firstsize = 13
    first = arrayList[0]
    for idx in range(firstsize):
        first.InsertNextValue(chr(idx))
    arrays.AddItem(first)

    second = arrayList[1]
    secondsize = 14
    for idx in range(firstsize):
        first.InsertNextValue(chr(firstsize + idx))
    arrays.AddItem(second)

    compositeArray.SetNumberOfTuples(firstsize + secondsize)
    compositeArray.ConstructBackend(arrays)

    if not CheckResult(compositeArray.GetValue(firstsize + 1), chr(firstsize+1)):
        print("ERROR: {} failed.".format(type(constArray)))



# ------------------------------------------------------------------------------
def InstantiateNumericTypes():
    for dataType in NUMERIC_TYPES:
        constantArray = GetArray("Constant", dataType)
        TestConstantArray(constantArray, 13)

        affineArray = GetArray("Affine", dataType)
        TestAffineArray(affineArray, 1, 2)

        indexedArray = GetArray("Indexed", dataType)
        originalData = GetArray("", dataType)
        for val in range(100):
            originalData.InsertNextValue(val)
        TestIndexedArray(indexedArray, originalData)

        compositeArray = GetArray("Composite", dataType)
        first = GetArray("", dataType)
        second = GetArray("", dataType)
        TestCompositeArray(compositeArray, [first, second])

# ------------------------------------------------------------------------------
def InstantiateChar():
    dataType = 'Char'
    constantArray = GetArray("Constant", dataType)
    TestConstantArray(constantArray, 'a')

    affineArray = GetArray("Affine", dataType)
    TestAffineCharArray(affineArray, chr(1), 'b')

    indexedArray = GetArray("Indexed", dataType)
    originalData = GetArray("", dataType)
    originalData.InsertNextValue('a')
    originalData.InsertNextValue('b')
    originalData.InsertNextValue('c')
    originalData.InsertNextValue('d')
    TestIndexedArray(indexedArray, originalData)

    compositeArray = GetArray("Composite", dataType)
    first = GetArray("", dataType)
    second = GetArray("", dataType)
    TestCompositeCharArray(compositeArray, [first, second])


InstantiateNumericTypes()
InstantiateChar()