File: TestSelectionExpression.cxx

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 (156 lines) | stat: -rw-r--r-- 4,772 bytes parent folder | download | duplicates (2)
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

#include "vtkNew.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSignedCharArray.h"

#include <algorithm>
#include <map>
#include <random>
#include <string>

vtkSmartPointer<vtkSignedCharArray> NewArray(vtkIdType numVals)
{
  std::random_device rd;  // Will be used to obtain a seed for the random number engine
  std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
  std::uniform_int_distribution<> dis(0, 1);

  auto array = vtkSmartPointer<vtkSignedCharArray>::New();
  array->SetNumberOfComponents(1);
  array->SetNumberOfTuples(numVals);
  std::generate_n(array->GetPointer(0), numVals, [&]() { return dis(gen); });
  return array;
}

static inline bool get(vtkSignedCharArray* array, vtkIdType cc)
{
  return array->GetTypedComponent(cc, 0) != 0;
}

template <typename Functor>
void ValidateIternal(vtkSignedCharArray* result, const Functor& f)
{
  for (vtkIdType cc = 0; cc < 1024; ++cc)
  {
    bool val = f(cc);
    bool rval = get(result, cc);
    if (val != rval)
    {
      cerr << "ERROR: failed at index '" << cc << "'." << endl;
      throw std::runtime_error("value mismatch");
    }
  }
}

template <typename Functor>
void Validate(vtkSelection* expr, const char* exprstr,
  const std::vector<vtkSignedCharArray*>& arrays, const Functor& f)
{
  expr->SetExpression(exprstr);
  auto result = expr->Evaluate(arrays.data(), static_cast<unsigned int>(arrays.size()));
  if (!result)
  {
    throw std::runtime_error("null result");
  }
  ValidateIternal(result, f);
}

template <typename Functor, typename MapType>
void Validate(vtkSelection* expr, const char* exprstr, const MapType& arrays, const Functor& f)
{
  expr->SetExpression(exprstr);
  auto result = expr->Evaluate(arrays);
  if (!result)
  {
    throw std::runtime_error("null result");
  }
  ValidateIternal(result, f);
}

int TestSelectionExpression(int, char*[])
{
  // Test null arrays
  vtkNew<vtkSelection> testEmptySelection;
  testEmptySelection->SetNode("A", vtkSmartPointer<vtkSelectionNode>::New());
  testEmptySelection->SetNode("B", vtkSmartPointer<vtkSelectionNode>::New());
  testEmptySelection->SetNode("C", vtkSmartPointer<vtkSelectionNode>::New());
  testEmptySelection->SetExpression("A|B|C");
  std::vector<vtkSignedCharArray*> emptyArrays(testEmptySelection->GetNumberOfNodes(), nullptr);
  testEmptySelection->Evaluate(emptyArrays.data(), static_cast<unsigned int>(emptyArrays.size()));

  vtkNew<vtkSelection> expr;
  vtkNew<vtkSelectionNode> aItem;
  vtkNew<vtkSelectionNode> bItem;
  vtkNew<vtkSelectionNode> cItem;
  vtkNew<vtkSelectionNode> dItem;
  vtkNew<vtkSelectionNode> eItem;
  vtkNew<vtkSelectionNode> fItem;
  vtkNew<vtkSelectionNode> gItem;
  expr->SetNode("A", aItem);
  expr->SetNode("B", bItem);
  expr->SetNode("C", cItem);
  expr->SetNode("D", dItem);
  expr->SetNode("D", dItem);
  expr->SetNode("E", eItem);
  expr->SetNode("F", fItem);
  expr->SetNode("G", gItem);

  std::map<std::string, vtkSmartPointer<vtkSignedCharArray>> arrays;
  std::vector<vtkSignedCharArray*> arrays_ptrs(expr->GetNumberOfNodes(), nullptr);
  for (int cc = 0, max = expr->GetNumberOfNodes(); cc < max; ++cc)
  {
    const std::string varname = expr->GetNodeNameAtIndex(cc);
    auto newarray = NewArray(1024);
    arrays[varname] = newarray;
    arrays_ptrs[cc] = newarray;
  }

  Validate(expr, "A & (B | (C & D))", arrays_ptrs,
    [&](vtkIdType cc)
    {
      auto a = get(arrays["A"], cc);
      auto b = get(arrays["B"], cc);
      auto c = get(arrays["C"], cc);
      auto d = get(arrays["D"], cc);
      return a && (b || (c && d));
    });

  Validate(expr, "A & B | (C & D)", arrays_ptrs,
    [&](vtkIdType cc)
    {
      auto a = get(arrays["A"], cc);
      auto b = get(arrays["B"], cc);
      auto c = get(arrays["C"], cc);
      auto d = get(arrays["D"], cc);
      return (a && b) || (c && d);
    });

  Validate(expr, "(A & B) | (C | (D & E) ) | (C & D)", arrays_ptrs,
    [&](vtkIdType cc)
    {
      auto a = get(arrays["A"], cc);
      auto b = get(arrays["B"], cc);
      auto c = get(arrays["C"], cc);
      auto d = get(arrays["D"], cc);
      auto e = get(arrays["E"], cc);
      return (a && b) || (c || (d && e)) || (c && d);
    });

  // empty expression is treated as "|"
  Validate(expr, "", arrays,
    [&](vtkIdType cc)
    {
      auto a = get(arrays["A"], cc);
      auto b = get(arrays["B"], cc);
      auto c = get(arrays["C"], cc);
      auto d = get(arrays["D"], cc);
      auto e = get(arrays["E"], cc);
      auto f = get(arrays["F"], cc);
      auto g = get(arrays["G"], cc);
      return (a || b || c || d || e || f || g);
    });

  return EXIT_SUCCESS;
}