File: TestHyperTreeGridBitmask.cxx

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (253 lines) | stat: -rw-r--r-- 8,628 bytes parent folder | download
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestHyperTreeGridBitmask.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkBitArray.h"
#include "vtkHyperTree.h"
#include "vtkHyperTreeGrid.h"
#include "vtkHyperTreeGridNonOrientedCursor.h"
#include "vtkUniformHyperTreeGrid.h"

#include <iostream>
#include <string>
#include <vector>
// exist in ElderChildIndex test
void SubdivideAndInitMaskChildrenTrue(vtkHyperTreeGridNonOrientedCursor* cursor);
void CheckTestStatusHTG(int rc, const std::string& TestName);
//------------------------------------------------------------------------------
bool compareMask(vtkBitArray* bm, const std::vector<bool>& ref_bm)
{
  const size_t nbElementsBm = bm->GetNumberOfValues();
  const size_t nbElementsRef = ref_bm.size();
  if (nbElementsRef != nbElementsBm)
  {
    std::cout << "Not same amount of bits in expected and actual mask: [REF]" << nbElementsRef
              << " vs [Actual]" << nbElementsBm << std::endl;
    return false;
  }
  for (size_t i = 0; i < nbElementsRef; ++i)
  {
    const bool ref_val = ref_bm.at(i);
    const vtkIdType vtkIdx = static_cast<vtkIdType>(i);
    const bool act_val = bm->GetValue(vtkIdx) != 0;
    if (ref_val != act_val)
    {
      std::cout << "Mask value different for idx " << i << ": " << ref_val << "[REF] vs " << act_val
                << "[Actual]" << std::endl;
      return false;
    }
  }
  return true;
}
//------------------------------------------------------------------------------
void displayMask(const std::string& msg, vtkHyperTreeGrid* htg)
{
  if (!msg.empty())
  {
    std::cout << msg << " ";
    std::cout.flush();
  }
  auto* bm = htg->GetMask();
  if (!bm)
  {
    std::cout << "No Mask" << std::endl;
    std::cout.flush();
    return;
  }
  std::cout << "Mask: ";
  std::cout.flush();
  const vtkIdType nbElements = bm->GetNumberOfValues();
  for (vtkIdType i = 0; i < nbElements; ++i)
  {
    std::cout << bm->GetValue(i);
  }
  std::cout << std::endl;
  std::cout.flush();
}
//------------------------------------------------------------------------------
void initUniformHyperTreeOneRootCell(vtkUniformHyperTreeGrid* uhtg)
{
  std::cout << "Initializing Uniform Grid\n";
  std::cout.flush();
  uhtg->SetBranchFactor(2);
  uhtg->SetGridScale(1.1);
  uhtg->SetOrigin(0., 0., 0.);
  uhtg->SetDimensions(3, 3, 2);
  vtkNew<vtkBitArray> mask;
  uhtg->SetMask(mask);

  vtkNew<vtkHyperTreeGridNonOrientedCursor> cursor;
  const unsigned int treeId = 1;
  const vtkIdType nbElementsInHTG = uhtg->GetNumberOfCells();
  uhtg->InitializeNonOrientedCursor(cursor, treeId, true);
  cursor->SetGlobalIndexStart(nbElementsInHTG);
  cursor->SetMask(false);
}
//------------------------------------------------------------------------------
int TestUniformHyperTreeOneRootCell()
{
  vtkNew<vtkUniformHyperTreeGrid> uhtg;
  initUniformHyperTreeOneRootCell(uhtg);
  auto* bm = uhtg->GetMask();
  //                       0
  return !compareMask(bm, { false });
}
void subdivide(vtkHyperTreeGridNonOrientedCursor* cursor, const std::vector<int>& sub)
{
  for (size_t i = 0; i < sub.size(); ++i)
  {
    const int subIdx = sub[i];
    if (subIdx == -1)
    {
      cursor->ToParent();
      continue;
    }
    if (cursor->IsLeaf())
    {
      SubdivideAndInitMaskChildrenTrue(cursor);
    }
    cursor->ToChild(subIdx);
    cursor->SetMask(false);
  }
}
//------------------------------------------------------------------------------
int TestUniformHyperTreeOneRootCellSubdivided()
{
  vtkNew<vtkUniformHyperTreeGrid> uhtg;
  initUniformHyperTreeOneRootCell(uhtg);
  vtkHyperTreeGrid::vtkHyperTreeGridIterator it;
  uhtg->InitializeTreeIterator(it);
  vtkIdType treeId = -1;
  it.GetNextTree(treeId);
  if (treeId == -1)
  {
    std::cout << "Impossible to retrieve either of the trees" << std::endl;
    std::cout.flush();
    return 1;
  }
  vtkNew<vtkHyperTreeGridNonOrientedCursor> cursor;
  uhtg->InitializeNonOrientedCursor(cursor, treeId);
  cursor->SetMask(false);
  subdivide(cursor, { 7, 2, -1, -1, 2, 7 });
  // 0 11011110 11011111 11111110
  auto* bm = uhtg->GetMask();
  displayMask("OneRootCellSubdivided", uhtg);
  // 0 11011110 11011111 11111110
  std::vector<bool> exp_bm(25, true);
  for (unsigned int i : { 0, 3, 8, 11, 24 })
  {
    exp_bm[i] = false;
  }
  return !compareMask(bm, exp_bm);
}
//------------------------------------------------------------------------------
void initUniformHyperTreeSeveralRootCells(
  vtkUniformHyperTreeGrid* uhtg, const std::vector<std::vector<int>>& sub = {})
{
  std::cout << "Init Uniform Grid several root cells\n";
  std::cout.flush();
  uhtg->SetBranchFactor(2);
  uhtg->SetGridScale(1.1);
  uhtg->SetOrigin(0., 0., 0.);
  uhtg->SetDimensions(3, 4, 2);
  vtkNew<vtkBitArray> mask;
  uhtg->SetMask(mask);
  vtkNew<vtkHyperTreeGridNonOrientedCursor> cursor;
  const std::vector<unsigned int> treeIds = { 3, 1, 5, 4 };
  unsigned int treeIndex = 0;
  for (unsigned int treeId : treeIds)
  {
    const vtkIdType nbElementsInHTG = uhtg->GetNumberOfCells();
    uhtg->InitializeNonOrientedCursor(cursor, treeId, true);
    cursor->SetGlobalIndexStart(nbElementsInHTG);
    // std::cout << "GlobalIndexStart of HT " << treeId << " : " << nbElementsInHTG << std::endl;
    cursor->SetMask(false);
    if (!sub.empty())
    {
      subdivide(cursor, sub.at(treeIndex));
    }
    // displayMask("Inserted 1 root cell (tree)", uhtg);
    ++treeIndex;
  }
  displayMask("Inserted 4 root cells (trees)", uhtg);
}
//------------------------------------------------------------------------------
int TestUniformHyperTreeSeveralRootCells()
{
  std::cout << "Test Uniform Grid several root cells\n";
  std::cout.flush();
  vtkNew<vtkUniformHyperTreeGrid> uhtg;
  initUniformHyperTreeSeveralRootCells(uhtg);
  auto* bm = uhtg->GetMask();
  //                        0      0      0      0
  const std::vector<bool> exp_bm(4, false);
  return !compareMask(bm, exp_bm);
}
//------------------------------------------------------------------------------
int TestUniformHyperTreeSeveralRootCellsSubdivided()
{
  std::cout << "Test Uniform Grid several root cells subdivided\n";
  std::cout.flush();
  vtkNew<vtkUniformHyperTreeGrid> uhtg;
  initUniformHyperTreeSeveralRootCells(uhtg, { { 5, 0, -1, 2 }, { 7, 7, 7 }, { 4, -1, 6 }, { 4 } });
  auto* bm = uhtg->GetMask();
  //(HTa - 3) 0 11111011 01011111
  //(HTb - 1) 0 11111110 11111110 11111110
  //(HTc - 5) 0 11110101
  //(HTd - 4) 0 11101111
  // 0      6   9 10     17       25       33       41 42   4749 51    56
  // 0 11111011 01011111 0 11111110 11111110 11111110 0 11110101 0 11110111
  std::vector<bool> exp_bm(60, true);
  for (unsigned int i : { 0, 6, 9, 11, 17, 25, 33, 41, 42, 47, 49, 51, 56 })
  {
    exp_bm[i] = false;
  }
  displayMask("TestUniformHyperTreeSeveralRootCellsSubdivided", uhtg);
  return !compareMask(bm, exp_bm);
}
//------------------------------------------------------------------------------
int TestUniformHyperTreeEmpty()
{
  std::cout << "Initializing Empty Uniform Grid\n";
  std::cout.flush();
  vtkNew<vtkUniformHyperTreeGrid> uhtg;
  uhtg->SetBranchFactor(2);
  uhtg->SetGridScale(1.1);
  uhtg->SetOrigin(0., 0., 0.);
  uhtg->SetDimensions(3, 3, 2);
  vtkNew<vtkBitArray> mask;
  uhtg->SetMask(mask);
  displayMask("Empty HTG", uhtg);
  auto* bm = uhtg->GetMask();
  return !(bm->GetNumberOfValues() == 0);
}

//------------------------------------------------------------------------------
int TestHyperTreeGridBitmask(int, char*[])
{
  std::cout << "Starting tests" << std::endl;
  std::cout.flush();
  int rc = 0;
  rc += TestUniformHyperTreeEmpty();
  CheckTestStatusHTG(rc, "TestUniformHyperTreeEmpty");
  rc += TestUniformHyperTreeOneRootCell();
  CheckTestStatusHTG(rc, "TestUniformHyperTreeOneRootCell");
  rc += TestUniformHyperTreeOneRootCellSubdivided();
  CheckTestStatusHTG(rc, "TestUniformHyperTreeOneRootCellSubdivided");
  rc += TestUniformHyperTreeSeveralRootCells();
  CheckTestStatusHTG(rc, "TestUniformHyperTreeSeveralRootCells");
  rc += TestUniformHyperTreeSeveralRootCellsSubdivided();
  CheckTestStatusHTG(rc, "TestUniformHyperTreeSeveralRootCellsSubdivided");

  return (rc);
}