1 //=============================================================================
2 //
3 //  Copyright (c) Kitware, Inc.
4 //  All rights reserved.
5 //  See LICENSE.txt for details.
6 //
7 //  This software is distributed WITHOUT ANY WARRANTY; without even
8 //  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9 //  PURPOSE.  See the above copyright notice for more information.
10 //
11 //  Copyright 2012 Sandia Corporation.
12 //  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
13 //  the U.S. Government retains certain rights in this software.
14 //
15 //=============================================================================
16 
17 #include "vtkCellData.h"
18 #include "vtkDataArray.h"
19 #include "vtkDataSet.h"
20 #include "vtkFloatArray.h"
21 #include "vtkImageData.h"
22 #include "vtkNew.h"
23 #include "vtkPointData.h"
24 #include "vtkmProbe.h"
25 
26 namespace
27 {
28 int inputDim = 9;
29 int sourceDim = 4;
populatePointAndCellArray(vtkFloatArray * pointArray,vtkFloatArray * cellArray)30 void populatePointAndCellArray(vtkFloatArray* pointArray, vtkFloatArray* cellArray)
31 {
32   pointArray->SetNumberOfValues(sourceDim * sourceDim);
33   pointArray->SetName("pointdata");
34   for (vtkIdType i = 0; i < static_cast<vtkIdType>(sourceDim * sourceDim); i++)
35   {
36     pointArray->SetValue(i, 0.3f * i);
37   }
38   cellArray->SetName("celldata");
39   cellArray->SetNumberOfValues((sourceDim - 1) * (sourceDim - 1));
40   for (vtkIdType i = 0; i < static_cast<vtkIdType>((sourceDim - 1) * (sourceDim - 1)); i++)
41   {
42     cellArray->SetValue(i, 0.7f * i);
43   }
44 }
45 
GetExpectedPointData()46 const std::vector<float>& GetExpectedPointData()
47 {
48   static std::vector<float> expected = {
49     1.05f, 1.155f, 1.26f, 1.365f, 1.47f, 1.575f, 1.68f, 0.0f, 0.0f, 1.47f, 1.575f, 1.68f,  //
50     1.785f, 1.89f, 1.995f, 2.1f, 0.0f, 0.0f, 1.89f, 1.995f, 2.1f, 2.205f, 2.31f, 2.415f,   //
51     2.52f, 0.0f, 0.0f, 2.31f, 2.415f, 2.52f, 2.625f, 2.73f, 2.835f, 2.94f, 0.0f, 0.0f,     //
52     2.73f, 2.835f, 2.94f, 3.045f, 3.15f, 3.255f, 3.36f, 0.0f, 0.0f, 3.15f, 3.255f, 3.36f,  //
53     3.465f, 3.57f, 3.675f, 3.78f, 0.0f, 0.0f, 3.57f, 3.675f, 3.78f, 3.885f, 3.99f, 4.095f, //
54     4.2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,                //
55     0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f                                   //
56   };
57   return expected;
58 }
59 
GetExpectedCellData()60 const std::vector<float>& GetExpectedCellData()
61 {
62   static std::vector<float> expected = {
63     0.0f, 0.7f, 0.7f, 0.7f, 1.4f, 1.4f, 1.4f, 0.0f, 0.0f, 2.1f, 2.8f, 2.8f, 2.8f, 3.5f, //
64     3.5f, 3.5f, 0.0f, 0.0f, 2.1f, 2.8f, 2.8f, 2.8f, 3.5f, 3.5f, 3.5f, 0.0f, 0.0f, 2.1f, //
65     2.8f, 2.8f, 2.8f, 3.5f, 3.5f, 3.5f, 0.0f, 0.0f, 4.2f, 4.9f, 4.9f, 4.9f, 5.6f, 5.6f, //
66     5.6f, 0.0f, 0.0f, 4.2f, 4.9f, 4.9f, 4.9f, 5.6f, 5.6f, 5.6f, 0.0f, 0.0f, 4.2f, 4.9f, //
67     4.9f, 4.9f, 5.6f, 5.6f, 5.6f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, //
68     0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f                    //
69   };
70   return expected;
71 }
72 
GetExpectedHiddenPoints()73 const std::vector<size_t>& GetExpectedHiddenPoints()
74 {
75   static std::vector<size_t> expected = {
76     0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, //
77     2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, //
78     2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, //
79     0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, //
80     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2              //
81   };
82   return expected;
83 }
84 
GetExpectedHiddenCells()85 const std::vector<size_t>& GetExpectedHiddenCells()
86 {
87   static std::vector<size_t> expected = {
88     0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 2, 2, //
89     0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 2, 2, //
90     0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 2, 2, //
91     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2  //
92   };
93   return expected;
94 }
95 
96 template <typename T>
TestResultArray(vtkDataArray * result,const std::vector<T> & expected)97 void TestResultArray(vtkDataArray* result, const std::vector<T>& expected)
98 {
99   if (result->GetNumberOfValues() != static_cast<vtkIdType>(expected.size()))
100   {
101     std::cout << "Array " << result->GetName() << " has wrong size" << std::endl;
102     exit(EXIT_FAILURE);
103   }
104 
105   for (vtkIdType i = 0; i < result->GetNumberOfValues(); ++i)
106   {
107     double diff = result->GetComponent(i, 0) - expected[static_cast<size_t>(i)];
108     diff = std::max(diff, -diff);
109     // The weird use of the not (!) operator here is to catch when diff is a NaN value,
110     // which will always return false for a comparison.
111     if (!(diff < 1e-5))
112     {
113       std::cout << "Array " << result->GetName() << " has wrong value"
114                 << " at index " << i << ". result value=" << result->GetComponent(i, 0)
115                 << " expected value=" << expected[static_cast<size_t>(i)] << std::endl;
116       exit(EXIT_FAILURE);
117     }
118   }
119 }
120 
121 } //  Anonymous namespace
122 
TestVTKMProbe(int,char * [])123 int TestVTKMProbe(int, char*[])
124 {
125   vtkNew<vtkImageData> input;
126   input->SetOrigin(0.7, 0.7, 0.0);
127   input->SetSpacing(0.35, 0.35, 1.0);
128   input->SetExtent(0, inputDim - 1, 0, inputDim - 1, 0, 0);
129 
130   vtkNew<vtkImageData> source;
131   source->SetOrigin(0.0, 0.0, 0.0);
132   source->SetSpacing(1.0, 1.0, 1.0);
133   source->SetExtent(0, sourceDim - 1, 0, sourceDim - 1, 0, 0);
134 
135   vtkNew<vtkFloatArray> pointArray, cellArray;
136   populatePointAndCellArray(pointArray, cellArray);
137   source->GetPointData()->AddArray(pointArray);
138   source->GetCellData()->AddArray(cellArray);
139 
140   vtkNew<vtkmProbe> probe;
141   probe->SetValidPointMaskArrayName("validPoint");
142   probe->SetValidCellMaskArrayName("validCell");
143   probe->SetInputData(input);
144   probe->SetSourceData(source);
145   probe->Update();
146 
147   vtkDataSet* result = probe->GetOutput();
148   TestResultArray(result->GetPointData()->GetArray(pointArray->GetName()), GetExpectedPointData());
149   TestResultArray(result->GetCellData()->GetArray(cellArray->GetName()), GetExpectedCellData());
150   TestResultArray(result->GetPointData()->GetArray("validPoint"), GetExpectedHiddenPoints());
151   TestResultArray(result->GetCellData()->GetArray("validCell"), GetExpectedHiddenCells());
152   return 0;
153 }
154