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/worklet/ExtractGeometry.h>
12 
13 #include <vtkm/cont/testing/MakeTestDataSet.h>
14 #include <vtkm/cont/testing/Testing.h>
15 
16 using vtkm::cont::testing::MakeTestDataSet;
17 
18 class TestingExtractGeometry
19 {
20 public:
21   /////////////////////////////////////////////////////////////////////////////////////////////////
TestExplicitById() const22   void TestExplicitById() const
23   {
24     std::cout << "Testing extract cell explicit by id:" << std::endl;
25 
26     using CellSetType = vtkm::cont::CellSetExplicit<>;
27     using OutCellSetType = vtkm::cont::CellSetPermutation<CellSetType>;
28 
29     // Input data set created
30     vtkm::cont::DataSet dataset = MakeTestDataSet().Make3DExplicitDataSet5();
31     CellSetType cellSet;
32     dataset.GetCellSet().CopyTo(cellSet);
33 
34     // Cells to extract
35     vtkm::cont::ArrayHandle<vtkm::Id> cellIds = vtkm::cont::make_ArrayHandle<vtkm::Id>({ 1, 2 });
36     const vtkm::Id nCells = cellIds.GetNumberOfValues();
37 
38     // Output data set with cell set containing extracted cells and all points
39     vtkm::worklet::ExtractGeometry extractGeometry;
40     OutCellSetType outCellSet = extractGeometry.Run(cellSet, cellIds);
41 
42     auto cellvar =
43       dataset.GetField("cellvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>();
44     auto cellFieldArray = extractGeometry.ProcessCellField(cellvar);
45 
46     VTKM_TEST_ASSERT(test_equal(outCellSet.GetNumberOfCells(), nCells),
47                      "Wrong result for ExtractCells");
48     VTKM_TEST_ASSERT(cellFieldArray.GetNumberOfValues() == nCells &&
49                        cellFieldArray.ReadPortal().Get(0) == 110.f,
50                      "Wrong cell field data");
51   }
52 
53   /////////////////////////////////////////////////////////////////////////////////////////////////
TestExplicitByBox() const54   void TestExplicitByBox() const
55   {
56     std::cout << "Testing extract cells with implicit function (box) on explicit:" << std::endl;
57 
58     using CellSetType = vtkm::cont::CellSetExplicit<>;
59 
60     // Input data set created
61     vtkm::cont::DataSet dataset = MakeTestDataSet().Make3DExplicitDataSet5();
62     CellSetType cellSet;
63     dataset.GetCellSet().CopyTo(cellSet);
64 
65     // Implicit function
66     vtkm::Vec3f minPoint(0.5f, 0.0f, 0.0f);
67     vtkm::Vec3f maxPoint(2.0f, 2.0f, 2.0f);
68 
69     bool extractInside = true;
70     bool extractBoundaryCells = false;
71     bool extractOnlyBoundaryCells = false;
72 
73     // Output data set with cell set containing extracted cells and all points
74     vtkm::worklet::ExtractGeometry extractGeometry;
75     vtkm::cont::DynamicCellSet outCellSet =
76       extractGeometry.Run(cellSet,
77                           dataset.GetCoordinateSystem("coordinates"),
78                           vtkm::Box(minPoint, maxPoint),
79                           extractInside,
80                           extractBoundaryCells,
81                           extractOnlyBoundaryCells);
82 
83     auto cellvar =
84       dataset.GetField("cellvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>();
85     auto cellFieldArray = extractGeometry.ProcessCellField(cellvar);
86 
87 
88     VTKM_TEST_ASSERT(test_equal(outCellSet.GetNumberOfCells(), 2), "Wrong result for ExtractCells");
89     VTKM_TEST_ASSERT(cellFieldArray.GetNumberOfValues() == 2 &&
90                        cellFieldArray.ReadPortal().Get(1) == 120.2f,
91                      "Wrong cell field data");
92   }
93 
94   /////////////////////////////////////////////////////////////////////////////////////////////////
TestUniformById2D() const95   void TestUniformById2D() const
96   {
97     std::cout << "Testing extract cells structured by id:" << std::endl;
98 
99     using CellSetType = vtkm::cont::CellSetStructured<2>;
100     using OutCellSetType = vtkm::cont::CellSetPermutation<CellSetType>;
101 
102 
103     // Input data set created
104     vtkm::cont::DataSet dataset = MakeTestDataSet().Make2DUniformDataSet1();
105     CellSetType cellSet;
106     dataset.GetCellSet().CopyTo(cellSet);
107 
108     // Cells to extract
109     vtkm::cont::ArrayHandle<vtkm::Id> cellIds =
110       vtkm::cont::make_ArrayHandle<vtkm::Id>({ 0, 4, 5, 10, 15 });
111     const vtkm::Id nCells = cellIds.GetNumberOfValues();
112 
113     // Output data set permutation of with only extracted cells
114     vtkm::worklet::ExtractGeometry extractGeometry;
115     OutCellSetType outCellSet = extractGeometry.Run(cellSet, cellIds);
116 
117     auto cellvar =
118       dataset.GetField("cellvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>();
119     auto cellFieldArray = extractGeometry.ProcessCellField(cellvar);
120 
121     VTKM_TEST_ASSERT(test_equal(outCellSet.GetNumberOfCells(), nCells),
122                      "Wrong result for ExtractCells");
123     VTKM_TEST_ASSERT(cellFieldArray.GetNumberOfValues() == nCells &&
124                        cellFieldArray.ReadPortal().Get(1) == 4.f,
125                      "Wrong cell field data");
126   }
127 
128   /////////////////////////////////////////////////////////////////////////////////////////////////
TestUniformById3D() const129   void TestUniformById3D() const
130   {
131     std::cout << "Testing extract cells structured by id:" << std::endl;
132 
133     using CellSetType = vtkm::cont::CellSetStructured<3>;
134     using OutCellSetType = vtkm::cont::CellSetPermutation<CellSetType>;
135 
136     // Input data set created
137     vtkm::cont::DataSet dataset = MakeTestDataSet().Make3DUniformDataSet1();
138     CellSetType cellSet;
139     dataset.GetCellSet().CopyTo(cellSet);
140 
141     // Cells to extract
142     vtkm::cont::ArrayHandle<vtkm::Id> cellIds =
143       vtkm::cont::make_ArrayHandle<vtkm::Id>({ 0, 4, 5, 10, 15 });
144     const vtkm::Id nCells = cellIds.GetNumberOfValues();
145 
146     // Output data set with cell set containing extracted cells and all points
147     vtkm::worklet::ExtractGeometry extractGeometry;
148     OutCellSetType outCellSet = extractGeometry.Run(cellSet, cellIds);
149 
150     auto cellvar =
151       dataset.GetField("cellvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>();
152     auto cellFieldArray = extractGeometry.ProcessCellField(cellvar);
153 
154     VTKM_TEST_ASSERT(test_equal(outCellSet.GetNumberOfCells(), nCells),
155                      "Wrong result for ExtractCells");
156     VTKM_TEST_ASSERT(cellFieldArray.GetNumberOfValues() == nCells &&
157                        cellFieldArray.ReadPortal().Get(2) == 5.f,
158                      "Wrong cell field data");
159   }
160 
161   /////////////////////////////////////////////////////////////////////////////////////////////////
TestUniformByBox() const162   void TestUniformByBox() const
163   {
164     std::cout << "Testing extract cells with implicit function (box):" << std::endl;
165 
166     using CellSetType = vtkm::cont::CellSetStructured<3>;
167 
168     // Input data set created
169     vtkm::cont::DataSet dataset = MakeTestDataSet().Make3DUniformDataSet1();
170     CellSetType cellSet;
171     dataset.GetCellSet().CopyTo(cellSet);
172 
173     // Implicit function
174     vtkm::Vec3f minPoint(1.0f, 1.0f, 1.0f);
175     vtkm::Vec3f maxPoint(3.0f, 3.0f, 3.0f);
176 
177     bool extractInside = true;
178     bool extractBoundaryCells = false;
179     bool extractOnlyBoundaryCells = false;
180 
181     // Output data set with cell set containing extracted points
182     vtkm::worklet::ExtractGeometry extractGeometry;
183     vtkm::cont::DynamicCellSet outCellSet =
184       extractGeometry.Run(cellSet,
185                           dataset.GetCoordinateSystem("coords"),
186                           vtkm::Box(minPoint, maxPoint),
187                           extractInside,
188                           extractBoundaryCells,
189                           extractOnlyBoundaryCells);
190 
191     auto cellvar =
192       dataset.GetField("cellvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>();
193     auto cellFieldArray = extractGeometry.ProcessCellField(cellvar);
194 
195     VTKM_TEST_ASSERT(test_equal(outCellSet.GetNumberOfCells(), 8), "Wrong result for ExtractCells");
196     VTKM_TEST_ASSERT(cellFieldArray.GetNumberOfValues() == 8 &&
197                        cellFieldArray.ReadPortal().Get(0) == 21.f,
198                      "Wrong cell field data");
199   }
200 
201   /////////////////////////////////////////////////////////////////////////////////////////////////
TestUniformBySphere() const202   void TestUniformBySphere() const
203   {
204     std::cout << "Testing extract cells with implicit function (sphere):" << std::endl;
205 
206     using CellSetType = vtkm::cont::CellSetStructured<3>;
207 
208     // Input data set created
209     vtkm::cont::DataSet dataset = MakeTestDataSet().Make3DUniformDataSet1();
210     CellSetType cellSet;
211     dataset.GetCellSet().CopyTo(cellSet);
212 
213     // Implicit function
214     vtkm::Vec3f center(2.f, 2.f, 2.f);
215     vtkm::FloatDefault radius(1.8f);
216 
217     bool extractInside = true;
218     bool extractBoundaryCells = false;
219     bool extractOnlyBoundaryCells = false;
220 
221     // Output data set with cell set containing extracted cells
222     vtkm::worklet::ExtractGeometry extractGeometry;
223     vtkm::cont::DynamicCellSet outCellSet =
224       extractGeometry.Run(cellSet,
225                           dataset.GetCoordinateSystem("coords"),
226                           vtkm::Sphere(center, radius),
227                           extractInside,
228                           extractBoundaryCells,
229                           extractOnlyBoundaryCells);
230 
231     auto cellvar =
232       dataset.GetField("cellvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>();
233     auto cellFieldArray = extractGeometry.ProcessCellField(cellvar);
234 
235     VTKM_TEST_ASSERT(test_equal(outCellSet.GetNumberOfCells(), 8), "Wrong result for ExtractCells");
236     VTKM_TEST_ASSERT(cellFieldArray.GetNumberOfValues() == 8 &&
237                        cellFieldArray.ReadPortal().Get(1) == 22.f,
238                      "Wrong cell field data");
239   }
240 
operator ()() const241   void operator()() const
242   {
243     this->TestUniformById2D();
244     this->TestUniformById3D();
245     this->TestUniformBySphere();
246     this->TestUniformByBox();
247     this->TestExplicitById();
248     this->TestExplicitByBox();
249   }
250 };
251 
UnitTestExtractGeometry(int argc,char * argv[])252 int UnitTestExtractGeometry(int argc, char* argv[])
253 {
254   return vtkm::cont::testing::Testing::Run(TestingExtractGeometry(), argc, argv);
255 }
256