File: UnitTestMultiThreshold.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (540 lines) | stat: -rw-r--r-- 17,223 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    UnitTestMultiThreshold.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 "vtkSmartPointer.h"
#include "vtkMultiThreshold.h"

#include "vtkImageData.h"
#include "vtkStructuredGrid.h"
#include "vtkUnstructuredGrid.h"
#include "vtkImageDataToPointSet.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkDataObject.h"
#include "vtkIntArray.h"
#include "vtkFloatArray.h"
#include "vtkCellData.h"
#include "vtkPointData.h"
#include "vtkMath.h"
#include "vtkExecutive.h"
#include "vtkTestErrorObserver.h"

static void TestPrint();
static int TestErrorsAndWarnings();
static int TestFilter(int, int);
static void CreateStructuredGrid(
  vtkSmartPointer<vtkStructuredGrid>&, int, int);
static int GetBlockCellCount(vtkMultiBlockDataSet *mbds, int block);

int UnitTestMultiThreshold(int, char *[])
{
  int status = 0;

  TestPrint();
  status += TestFilter(50, 40);
  status += TestErrorsAndWarnings();

  return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

void TestPrint()
{
  vtkSmartPointer<vtkMultiThreshold> threshold =
    vtkSmartPointer<vtkMultiThreshold>::New();

  // Print right after constructed
  threshold->Print(std::cout);

  vtkSmartPointer<vtkStructuredGrid> sg =
    vtkSmartPointer<vtkStructuredGrid>::New();

  CreateStructuredGrid(sg, 3, 3);
  threshold->SetInputData(0, sg);
  threshold->Update();

  // Print after update
  threshold->Print(std::cout);
}

void CreateStructuredGrid(vtkSmartPointer<vtkStructuredGrid> &sg,
                 int numCols,
                 int numRows)
{
  vtkSmartPointer<vtkImageData> image =
    vtkSmartPointer<vtkImageData>::New();

  // Specify the size of the image data
  image->SetDimensions(numCols + 1, numRows + 1, 1);

  image->AllocateScalars(VTK_INT,1);

  // Populate the point data
  vtkSmartPointer<vtkFloatArray> vectors =
    vtkSmartPointer<vtkFloatArray>::New();
  vectors->SetName("PointVectors");
  vectors->SetNumberOfComponents(3);
  vectors->SetNumberOfTuples((numRows + 1) * (numCols + 1));
  int pointNo = 0;
  for (int j = 0; j < numRows + 1; ++j)
  {
    for (int i = 0; i < numCols + 1; ++i)
    {
      float vec[3];
      int* pixel = static_cast<int*>(image->GetScalarPointer(i, j, 0));
      vec[0] = 0.0;
      vec[1] = 0.0;
      vec[2] = vtkMath::Random(-10.0, 10.0);
      vectors->SetTuple(pointNo, vec);
      *pixel = pointNo++;
    }
  }
  image->GetPointData()->AddArray(vectors);

  // Populate the cell data
  vtkSmartPointer<vtkIntArray> columns =
    vtkSmartPointer<vtkIntArray>::New();
  columns->SetNumberOfTuples(numCols * numRows);
  columns->SetName("Columns");

  vtkSmartPointer<vtkIntArray> rows =
    vtkSmartPointer<vtkIntArray>::New();
  rows->SetNumberOfTuples(numCols * numRows);
  rows->SetName("Rows");

  vtkSmartPointer<vtkIntArray> cells =
    vtkSmartPointer<vtkIntArray>::New();
  cells->SetNumberOfTuples(numCols * numRows);
  cells->SetName("Cells");

  int cell = 0;
  for (int row = 0; row < numRows; ++row)
  {
    for (int col = 0; col < numCols; ++col)
    {
      columns->SetTuple1(cell, col);
      rows->SetTuple1(cell, row);
      cells->SetTuple1(cell, cell);
      ++cell;
    }
  }
  image->GetCellData()->AddArray(columns);
  image->GetCellData()->AddArray(rows);
  image->GetCellData()->AddArray(cells);

  // Convert the image data to a point set
  vtkSmartPointer<vtkImageDataToPointSet> imToPs =
    vtkSmartPointer<vtkImageDataToPointSet>::New();
  imToPs->SetInputData(image);
  imToPs->Update();
  sg = imToPs->GetOutput();
}

int TestFilter(int columns, int rows)
{
  int status = 0;
  int cells = rows * columns;
  int points = (rows + 1) * (columns + 1);

  vtkSmartPointer<vtkStructuredGrid> sg =
    vtkSmartPointer<vtkStructuredGrid>::New();

  CreateStructuredGrid(sg, columns, rows);

  vtkSmartPointer<vtkMultiThreshold> threshold =
    vtkSmartPointer<vtkMultiThreshold>::New();

  threshold->SetInputData(0, sg);
  std::vector<int> intervalSets;
  std::vector<int> expectedCellCounts;

  // 0: Row rows/2 expect columns cells
  intervalSets.push_back(threshold->AddIntervalSet(
    rows/2, rows/2,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_CELLS, "Rows", 0, 1 ));
  expectedCellCounts.push_back(columns);

  // 1: Column columns/2 expect rows cells
  intervalSets.push_back(threshold->AddIntervalSet(
    columns/2, columns/2,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_CELLS, "Columns", 0, 1 ));
  expectedCellCounts.push_back(rows);

  // 2: Cells expect cells / 2
  intervalSets.push_back(threshold->AddIntervalSet(
                           cells / 2, cells,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::OPEN,
    vtkDataObject::FIELD_ASSOCIATION_CELLS, "Cells", 0, 1 ));
  expectedCellCounts.push_back((rows * columns) / 2);

  // 3: Points (0, points/2)
  intervalSets.push_back(threshold->AddIntervalSet(
    0, points / 2,
    vtkMultiThreshold::OPEN, vtkMultiThreshold::OPEN,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, 0, 0, 1 ));
  expectedCellCounts.push_back(-1);

  // 4: Row x and Column y expect 1 cell
  int intersection[2];
  intersection[0] = intervalSets[0];
  intersection[1] = intervalSets[1];
  intervalSets.push_back(threshold->AddBooleanSet(vtkMultiThreshold::AND, 2, intersection));
  expectedCellCounts.push_back(1);

  // 5: Row x or Column y expect rows + columns - 1
  intervalSets.push_back(threshold->AddBooleanSet(vtkMultiThreshold::OR, 2, intersection));
  expectedCellCounts.push_back(rows + columns - 1);

  // 6: Row x or Column y but not both  expect rows + columns - 2
  intervalSets.push_back(threshold->AddBooleanSet(vtkMultiThreshold::XOR, 2, intersection));
  expectedCellCounts.push_back(rows + columns - 2);

  // 7: expect rows + columns - 2
  intervalSets.push_back(threshold->AddBooleanSet(vtkMultiThreshold::WOR, 2, intersection));
  expectedCellCounts.push_back(rows + columns - 2);

  // 8: Not Row 1 or Column 2 expect rows * columns - 1
  intervalSets.push_back(threshold->AddBooleanSet(vtkMultiThreshold::NAND, 2, intersection));
  expectedCellCounts.push_back(rows * columns - 1);

  // 9-12: Convenience members
 intervalSets.push_back(threshold->AddLowpassIntervalSet(
    1,
    vtkDataObject::FIELD_ASSOCIATION_CELLS, "Rows", 0, 1 ));
  expectedCellCounts.push_back(2 * columns);

  intervalSets.push_back(threshold->AddHighpassIntervalSet(
    rows - 1,
    vtkDataObject::FIELD_ASSOCIATION_CELLS, "Rows", 0, 1 ));
  expectedCellCounts.push_back(columns);

  intervalSets.push_back(threshold->AddBandpassIntervalSet(
    1, 2,
    vtkDataObject::FIELD_ASSOCIATION_CELLS, "Columns", 0, 1 ));
  expectedCellCounts.push_back(2 * rows);

  intervalSets.push_back(threshold->AddNotchIntervalSet(
    1, 1,
    vtkDataObject::FIELD_ASSOCIATION_CELLS, "Rows", 0, 1 ));
  expectedCellCounts.push_back((rows - 1) * columns);

  // 13-16: PointVectors
  intervalSets.push_back(threshold->AddIntervalSet(
    1, 10,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, "PointVectors", 2, 1 ));
  expectedCellCounts.push_back(-1);

  intervalSets.push_back(threshold->AddIntervalSet(
    1, 10,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, "PointVectors", -1, 1 ));
  expectedCellCounts.push_back(-1);

  intervalSets.push_back(threshold->AddIntervalSet(
    1, 10,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, "PointVectors", -2, 1 ));
  expectedCellCounts.push_back(-1);

  intervalSets.push_back(threshold->AddIntervalSet(
    1, 10,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, "PointVectors", -3, 0 ));
  expectedCellCounts.push_back(-1);

  for (size_t n = 0; n < intervalSets.size(); ++n)
  {
    std::cout << "OutputSet: "
              << threshold->OutputSet(intervalSets[n]) << std::endl;
  }

  // Add the first set again, should do nothing
  std::cout << "OutputSet: "
            << threshold->OutputSet(intervalSets[0]) << std::endl;
  threshold->Update();

  int blocksBefore = threshold->GetOutput()->GetNumberOfBlocks();
  for (int b = 0; b < blocksBefore; ++b)
  {
    std::cout << "Block " << b << " has "
              << GetBlockCellCount(threshold->GetOutput(), b) << " cells";
    if (expectedCellCounts[b] != -1 &&
        expectedCellCounts[b] != GetBlockCellCount(threshold->GetOutput(), b))
    {
      std::cout << " but expected " << expectedCellCounts[b];
      ++status;
    }
    std::cout << std::endl;
  }

  // Add the first set again, should do nothing
  std::cout << "OutputSet: "
            << threshold->OutputSet(intervalSets[0]) << std::endl;
  threshold->Update();
  int blocksAfter = threshold->GetOutput()->GetNumberOfBlocks();
  if (blocksBefore != blocksAfter)
  {
    std::cout
      << "ERROR: A duplicate OutputSet() should not produce extra output"
      << std::endl;
    ++status;
  }
  threshold->Print(std::cout);
  return status;
}

int TestErrorsAndWarnings()
{
  int status = 0;
  vtkSmartPointer<vtkTest::ErrorObserver>  filterObserver =
    vtkSmartPointer<vtkTest::ErrorObserver>::New();

  vtkSmartPointer<vtkStructuredGrid> sg =
    vtkSmartPointer<vtkStructuredGrid>::New();

  CreateStructuredGrid(sg, 4, 3);

  vtkSmartPointer<vtkMultiThreshold> threshold =
    vtkSmartPointer<vtkMultiThreshold>::New();
  threshold->SetInputData(sg);
  threshold->AddObserver(vtkCommand::ErrorEvent, filterObserver);
  threshold->AddObserver(vtkCommand::WarningEvent, filterObserver);

  std::vector<int> intervalSets;
  intervalSets.push_back(threshold->AddIntervalSet(
    1, 1,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_CELLS, "Rows", 0, 1 ));
  intervalSets.push_back(threshold->AddIntervalSet(
    1, 1,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_CELLS, "aColumns", 0, 1 ));
  intervalSets.push_back(threshold->AddIntervalSet(
    2, 3,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_CELLS, "Cells", 0, 1 ));

  // WARNING: You passed a null array name
  intervalSets.push_back(threshold->AddIntervalSet(
    0, 2,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, (char *) NULL, 0, 1 ));
  if (filterObserver->GetWarning())
  {
    std::cout << "Caught expected warning: "
              << filterObserver->GetWarningMessage();
  }
  else
  {
    std::cout << "Failed to catch expected 'You passed a null array name' warning" << std::endl;
    ++status;
  }
  filterObserver->Clear();


  // WARNING: You passed an invalid attribute type (100)
  intervalSets.push_back(threshold->AddIntervalSet(
    0, 2,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, 100, 0, 1 ));
  if (filterObserver->GetWarning())
  {
    std::cout << "Caught expected warning: "
              << filterObserver->GetWarningMessage();
  }
  else
  {
    std::cout << "Failed to catch expected 'You passed an invalid attribute type (100)' warning" << std::endl;
    ++status;
  }
  filterObserver->Clear();

  int intersection[2];
  intersection[0] = intervalSets[0];

  // ERROR: Operators require at least one operand. You passed 0.
  intervalSets.push_back(threshold->AddBooleanSet(vtkMultiThreshold::AND, 0, intersection));
  if (filterObserver->GetError())
  {
    std::cout << "Caught expected error: "
              << filterObserver->GetErrorMessage();
  }
  else
  {
    std::cout << "Failed to catch expected 'Operators require at least one operand. You passed 0.' error" << std::endl;
    ++status;
  }
  filterObserver->Clear();

  // ERROR: Invalid operation (10)
  intervalSets.push_back(threshold->AddBooleanSet(10, 1, intersection));
  if (filterObserver->GetError())
  {
    std::cout << "Caught expected error: "
              << filterObserver->GetErrorMessage();
  }
  else
  {
    std::cout << "Failed to catch expected 'Invalid operation (10)' error" << std::endl;
    ++status;
  }
  filterObserver->Clear();

  // ERROR: Input 1 is invalid(100)
  intersection[1] = 100;
  intervalSets.push_back(threshold->AddBooleanSet(vtkMultiThreshold::XOR, 2, intersection));
  if (filterObserver->GetError())
  {
    std::cout << "Caught expected error: "
              << filterObserver->GetErrorMessage();
  }
  else
  {
    std::cout << "Failed to catch expected 'Input 1 is invalid(100)' error" << std::endl;
    ++status;
  }
  filterObserver->Clear();

  intersection[1] = intervalSets[1];
  intervalSets.push_back(threshold->AddBooleanSet(vtkMultiThreshold::XOR, 2, intersection));

  // 9: PointVectors
  intervalSets.push_back(threshold->AddIntervalSet(
    1, 10,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, "PointXXXVectors", 0, 1 ));
  intervalSets.push_back(threshold->AddIntervalSet(
    1, 10,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, 0, -1, 1 ));

  // WARNING: You passed an invalid atribute type (100)
  intervalSets.push_back(threshold->AddIntervalSet(
    1, 10,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    100, "PointVectors", -2, 1 ));
  if (filterObserver->GetWarning())
  {
    std::cout << "Caught expected warning: "
              << filterObserver->GetWarningMessage();
  }
  else
  {
    std::cout << "Failed to catch expected 'You passed an invalid atribute type (100)' warning" << std::endl;
    ++status;
  }
  filterObserver->Clear();


  // WARNING: Intervals must be specified with ascending values (xmin
  // <= xmax)
  intervalSets.push_back(threshold->AddIntervalSet(
    11, 10,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, "PointVectors", -3, 1 ));
  if (filterObserver->GetWarning())
  {
    std::cout << "Caught expected warning: "
              << filterObserver->GetWarningMessage();
  }
  else
  {
    std::cout << "Failed to catch expected 'One of the interval endpoints is not a number.' warning" << std::endl;
    ++status;
  }
  filterObserver->Clear();

#ifndef _WIN32
  // WARNING: One of the interval endpoints is not a number.
  intervalSets.push_back(threshold->AddIntervalSet(
    vtkMath::Nan(), 10,
    vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, "PointVectors", -3, 1 ));
  if (filterObserver->GetWarning())
  {
    std::cout << "Caught expected warning: "
              << filterObserver->GetWarningMessage();
  }
  else
  {
    std::cout << "Failed to catch expected 'One of the interval endpoints is not a number.' warning" << std::endl;
    ++status;
  }
  filterObserver->Clear();
#endif

  // WARNING: An open interval with equal endpoints will always be
  // empty. I won't help you waste my time.
  intervalSets.push_back(threshold->AddIntervalSet(
    10, 10,
    vtkMultiThreshold::OPEN, vtkMultiThreshold::OPEN,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, "PointVectors", -3, 1 ));
  if (filterObserver->GetWarning())
  {
    std::cout << "Caught expected warning: "
              << filterObserver->GetWarningMessage();
  }
  else
  {
    std::cout << "Failed to catch expected 'An open interval with equal endpoints will always be...' warning" << std::endl;
    ++status;
  }
  filterObserver->Clear();


  // WARNING: Cannot output 1000 because there is not set with that
  // label
  threshold->OutputSet(1000);
  if (filterObserver->GetWarning())
  {
    std::cout << "Caught expected warning: "
              << filterObserver->GetWarningMessage();
  }
  else
  {
    std::cout << "Failed to catch expected 'Cannot output 1000 because there is not set with that label' warning" << std::endl;
    ++status;
  }
  filterObserver->Clear();

  vtkSmartPointer<vtkTest::ErrorObserver>  executiveObserver =
    vtkSmartPointer<vtkTest::ErrorObserver>::New();

  threshold->GetExecutive()->AddObserver(vtkCommand::ErrorEvent, executiveObserver);
  threshold->Update();

  if (executiveObserver->GetError())
  {
    std::cout << "Caught expected error: "
              << executiveObserver->GetErrorMessage();
  }
  else
  {
    std::cout << "Failed to catch expected pipeline error" << std::endl;
    ++status;
  }
  filterObserver->Clear();

  return status;
}

int GetBlockCellCount(vtkMultiBlockDataSet *mbds, int block)
{
  vtkMultiBlockDataSet *mds =
    vtkMultiBlockDataSet::SafeDownCast(mbds->GetBlock(block));
  vtkUnstructuredGrid *ug =
    vtkUnstructuredGrid::SafeDownCast(mds->GetBlock(0));
  return ug->GetNumberOfCells();
}