1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10
11 #include <vtkm/cont/testing/MakeTestDataSet.h>
12 #include <vtkm/cont/testing/Testing.h>
13
14 #include <vtkm/filter/CleanGrid.h>
15 #include <vtkm/filter/Threshold.h>
16
17 using vtkm::cont::testing::MakeTestDataSet;
18
19 namespace
20 {
21
22 class TestingThreshold
23 {
24 public:
TestRegular2D() const25 void TestRegular2D() const
26 {
27 std::cout << "Testing threshold on 2D regular dataset" << std::endl;
28
29 vtkm::cont::DataSet dataset = MakeTestDataSet().Make2DUniformDataSet0();
30
31 vtkm::filter::Threshold threshold;
32 threshold.SetLowerThreshold(60.1);
33 threshold.SetUpperThreshold(60.1);
34 threshold.SetActiveField("pointvar");
35 threshold.SetFieldsToPass("cellvar");
36 auto output = threshold.Execute(dataset);
37
38 VTKM_TEST_ASSERT(output.GetNumberOfFields() == 1,
39 "Wrong number of fields in the output dataset");
40
41 vtkm::cont::ArrayHandle<vtkm::Float32> cellFieldArray;
42 output.GetField("cellvar").GetData().AsArrayHandle(cellFieldArray);
43
44 VTKM_TEST_ASSERT(cellFieldArray.GetNumberOfValues() == 1 &&
45 cellFieldArray.ReadPortal().Get(0) == 200.1f,
46 "Wrong cell field data");
47
48 // Make sure that the resulting data set can be successfully passed to another
49 // simple filter using the cell set.
50 vtkm::filter::CleanGrid clean;
51 clean.Execute(output);
52 }
53
TestRegular3D() const54 void TestRegular3D() const
55 {
56 std::cout << "Testing threshold on 3D regular dataset" << std::endl;
57 vtkm::cont::DataSet dataset = MakeTestDataSet().Make3DUniformDataSet0();
58
59 vtkm::filter::Threshold threshold;
60
61 threshold.SetLowerThreshold(20.1);
62 threshold.SetUpperThreshold(20.1);
63 threshold.SetActiveField("pointvar");
64 threshold.SetFieldsToPass("cellvar");
65 auto output = threshold.Execute(dataset);
66
67 VTKM_TEST_ASSERT(output.GetNumberOfFields() == 1,
68 "Wrong number of fields in the output dataset");
69
70 vtkm::cont::ArrayHandle<vtkm::Float32> cellFieldArray;
71 output.GetField("cellvar").GetData().AsArrayHandle(cellFieldArray);
72
73 VTKM_TEST_ASSERT(cellFieldArray.GetNumberOfValues() == 2 &&
74 cellFieldArray.ReadPortal().Get(0) == 100.1f &&
75 cellFieldArray.ReadPortal().Get(1) == 100.2f,
76 "Wrong cell field data");
77
78 // Make sure that the resulting data set can be successfully passed to another
79 // simple filter using the cell set.
80 vtkm::filter::CleanGrid clean;
81 clean.Execute(output);
82 }
83
TestExplicit3D() const84 void TestExplicit3D() const
85 {
86 std::cout << "Testing threshold on 3D explicit dataset" << std::endl;
87 vtkm::cont::DataSet dataset = MakeTestDataSet().Make3DExplicitDataSet1();
88
89 vtkm::filter::Threshold threshold;
90
91 threshold.SetLowerThreshold(20.1);
92 threshold.SetUpperThreshold(20.1);
93 threshold.SetActiveField("pointvar");
94 threshold.SetFieldsToPass("cellvar");
95 auto output = threshold.Execute(dataset);
96
97 VTKM_TEST_ASSERT(output.GetNumberOfFields() == 1,
98 "Wrong number of fields in the output dataset");
99
100 vtkm::cont::ArrayHandle<vtkm::Float32> cellFieldArray;
101 output.GetField("cellvar").GetData().AsArrayHandle(cellFieldArray);
102
103 VTKM_TEST_ASSERT(cellFieldArray.GetNumberOfValues() == 2 &&
104 cellFieldArray.ReadPortal().Get(0) == 100.1f &&
105 cellFieldArray.ReadPortal().Get(1) == 100.2f,
106 "Wrong cell field data");
107
108 // Make sure that the resulting data set can be successfully passed to another
109 // simple filter using the cell set.
110 vtkm::filter::CleanGrid clean;
111 clean.Execute(output);
112 }
113
TestExplicit3DZeroResults() const114 void TestExplicit3DZeroResults() const
115 {
116 std::cout << "Testing threshold on 3D explicit dataset with empty results" << std::endl;
117 vtkm::cont::DataSet dataset = MakeTestDataSet().Make3DExplicitDataSet1();
118
119 vtkm::filter::Threshold threshold;
120
121 threshold.SetLowerThreshold(500.1);
122 threshold.SetUpperThreshold(500.1);
123 threshold.SetActiveField("pointvar");
124 threshold.SetFieldsToPass("cellvar");
125 auto output = threshold.Execute(dataset);
126
127 VTKM_TEST_ASSERT(output.GetNumberOfFields() == 1,
128 "Wrong number of fields in the output dataset");
129
130 vtkm::cont::ArrayHandle<vtkm::Float32> cellFieldArray;
131 output.GetField("cellvar").GetData().AsArrayHandle(cellFieldArray);
132
133 VTKM_TEST_ASSERT(cellFieldArray.GetNumberOfValues() == 0, "field should be empty");
134
135 // Make sure that the resulting data set can be successfully passed to another
136 // simple filter using the cell set.
137 vtkm::filter::CleanGrid clean;
138 clean.Execute(output);
139 }
140
operator ()() const141 void operator()() const
142 {
143 this->TestRegular2D();
144 this->TestRegular3D();
145 this->TestExplicit3D();
146 this->TestExplicit3DZeroResults();
147 }
148 };
149 }
150
UnitTestThresholdFilter(int argc,char * argv[])151 int UnitTestThresholdFilter(int argc, char* argv[])
152 {
153 return vtkm::cont::testing::Testing::Run(TestingThreshold(), argc, argv);
154 }
155