TestFieldList.cxx 4.58 KB
Newer Older
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
#include "vtkDataSetAttributes.h"
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include "vtkNew.h"
#include "vtkSmartPointer.h"

#include <string>

namespace
{
template <typename T>
vtkSmartPointer<T> CreateArray(const char* aname, int num_comps, vtkIdType numTuples)
{
  auto array = vtkSmartPointer<T>::New();
  array->SetName(aname);
  array->SetNumberOfComponents(num_comps);
  array->SetNumberOfTuples(numTuples);
  array->FillValue(typename T::ValueType());
  return array;
}

#define EXPECT_THAT(v, m)                                                                          \
  if ((v) != (m))                                                                                  \
  {                                                                                                \
    cerr << "FAILED at line " << __LINE__ << ": \n     " << #v << " must match " << #m << endl;    \
    return EXIT_FAILURE;                                                                           \
  }
}

int TestFieldList(int, char* [])
{

  {
    // For arrays without names, ensure we are doing a order-dependent matching.
    // For attributes, the attribute flag is preserved if the same arrays is
    // consistently flagged as an attribute on all DSA instances.
    vtkNew<vtkDataSetAttributes> dsa0;
    dsa0->SetScalars(CreateArray<vtkDoubleArray>(nullptr, 1, 20));
    dsa0->AddArray(CreateArray<vtkDoubleArray>(nullptr, 2, 20));
    dsa0->SetVectors(CreateArray<vtkDoubleArray>(nullptr, 3, 20));
    EXPECT_THAT(dsa0->GetNumberOfArrays(), 3);

    vtkNew<vtkDataSetAttributes> dsa1;
    dsa1->SetScalars(CreateArray<vtkDoubleArray>(nullptr, 1, 20));
    dsa1->AddArray(CreateArray<vtkDoubleArray>(nullptr, 3, 20));
    dsa1->AddArray(CreateArray<vtkDoubleArray>(nullptr, 3, 20));
    EXPECT_THAT(dsa1->GetNumberOfArrays(), 3);

    vtkDataSetAttributes::FieldList fl;
    fl.InitializeFieldList(dsa0);
    fl.IntersectFieldList(dsa1);

    vtkNew<vtkDataSetAttributes> output;
    fl.CopyAllocate(output, vtkDataSetAttributes::COPYTUPLE, 0, 0);
    EXPECT_THAT(output->GetNumberOfArrays(), 2);
    EXPECT_THAT(output->GetArray(0)->GetNumberOfComponents(), 1);
    EXPECT_THAT(output->GetArray(1)->GetNumberOfComponents(), 3);
    EXPECT_THAT(output->GetVectors(), nullptr);
    EXPECT_THAT(output->GetScalars() != nullptr, true);

    fl.InitializeFieldList(dsa0);
    fl.UnionFieldList(dsa1);
    output->Initialize();
    fl.CopyAllocate(output, vtkDataSetAttributes::COPYTUPLE, 0, 0);
    EXPECT_THAT(output->GetNumberOfArrays(), 4);
    EXPECT_THAT(output->GetArray(0)->GetNumberOfComponents(), 1);
    EXPECT_THAT(output->GetArray(1)->GetNumberOfComponents(), 2);
    EXPECT_THAT(output->GetArray(2)->GetNumberOfComponents(), 3);
    EXPECT_THAT(output->GetArray(3)->GetNumberOfComponents(), 3);
    EXPECT_THAT(output->GetVectors(), nullptr);
    EXPECT_THAT(output->GetScalars() != nullptr, true);

    // just to increase coverage.
    fl.PrintSelf(cout, vtkIndent());
  }

  {
    // If inputs arrays with different names for attributes,
    // make sure output doesn't have either of the arrays flagged as attributes.
    vtkNew<vtkDataSetAttributes> dsa0;
    dsa0->SetScalars(CreateArray<vtkDoubleArray>("scalars", 1, 20));
    dsa0->AddArray(CreateArray<vtkDoubleArray>("vectors", 3, 20));
    dsa0->AddArray(CreateArray<vtkDoubleArray>("common", 1, 20));
    dsa0->AddArray(CreateArray<vtkDoubleArray>("uncommon0", 1, 20));

    vtkNew<vtkDataSetAttributes> dsa1;
    dsa1->AddArray(CreateArray<vtkDoubleArray>("scalars", 1, 20));
    dsa1->SetVectors(CreateArray<vtkDoubleArray>("vectors", 3, 20));
    dsa1->AddArray(CreateArray<vtkDoubleArray>("common", 1, 20));
    dsa0->AddArray(CreateArray<vtkDoubleArray>("uncommon1", 1, 20));

    vtkDataSetAttributes::FieldList fl;
    fl.InitializeFieldList(dsa0);
    fl.IntersectFieldList(dsa1);

    vtkNew<vtkDataSetAttributes> output;
    fl.CopyAllocate(output, vtkDataSetAttributes::COPYTUPLE, 0, 0);
    EXPECT_THAT(output->GetNumberOfArrays(), 3);
    EXPECT_THAT(output->GetArray("uncommon0"), nullptr);
    EXPECT_THAT(output->GetArray("uncommon1"), nullptr);
    EXPECT_THAT(output->GetScalars(), nullptr);
    EXPECT_THAT(output->GetVectors(), nullptr);
    EXPECT_THAT(output->GetArray("scalars") != nullptr, true);
    EXPECT_THAT(output->GetArray("vectors") != nullptr, true);

    fl.InitializeFieldList(dsa0);
    fl.UnionFieldList(dsa1);
    output->Initialize();
    fl.CopyAllocate(output, vtkDataSetAttributes::COPYTUPLE, 0, 0);
    EXPECT_THAT(output->GetNumberOfArrays(), 5);
    EXPECT_THAT(output->GetScalars(), nullptr);
    EXPECT_THAT(output->GetVectors(), nullptr);
  }

  return EXIT_SUCCESS;
}