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 "vtkmProbe.h"
18 #include "vtkFloatArray.h"
19 #include "vtkImageData.h"
20 #include "vtkNew.h"
21 #include "vtkDataSet.h"
22 #include "vtkPointData.h"
23 #include "vtkCellData.h"
24 #include "vtkDataArray.h"
25 
26 namespace
27 {
28 static int inputDim = 9;
29 static int sourceDim = 4;
populatePointAndCellArray(vtkFloatArray * pointArray,vtkFloatArray * cellArray)30 void populatePointAndCellArray(vtkFloatArray* pointArray,
31                                vtkFloatArray* cellArray)
32 {
33   pointArray->SetNumberOfValues(sourceDim * sourceDim);
34   pointArray->SetName("pointdata");
35   for (vtkIdType i = 0; i < static_cast<vtkIdType>(sourceDim * sourceDim); i++)
36   {
37     pointArray->SetValue(i, 0.3f * i);
38   }
39   cellArray->SetName("celldata");
40   cellArray->SetNumberOfValues((sourceDim - 1) * (sourceDim - 1));
41   for (vtkIdType i = 0; i < static_cast<vtkIdType>((sourceDim-1) * (sourceDim-1)); i++)
42   {
43     cellArray->SetValue(i, 0.7f * i);
44   }
45 
46 }
47 
GetExpectedPointData()48 const std::vector<float>& GetExpectedPointData()
49 {
50   static std::vector<float> expected = {
51     1.05f,  1.155f, 1.26f,  1.365f, 1.47f,  1.575f, 1.68f,  0.0f,   0.0f,   1.47f,  1.575f, 1.68f,
52     1.785f, 1.89f,  1.995f, 2.1f,   0.0f,   0.0f,   1.89f,  1.995f, 2.1f,   2.205f, 2.31f,  2.415f,
53     2.52f,  0.0f,   0.0f,   2.31f,  2.415f, 2.52f,  2.625f, 2.73f,  2.835f, 2.94f,  0.0f,   0.0f,
54     2.73f,  2.835f, 2.94f,  3.045f, 3.15f,  3.255f, 3.36f,  0.0f,   0.0f,   3.15f,  3.255f, 3.36f,
55     3.465f, 3.57f,  3.675f, 3.78f,  0.0f,   0.0f,   3.57f,  3.675f, 3.78f,  3.885f, 3.99f,  4.095f,
56     4.2f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f,
57     0.0f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f,   0.0f
58   };
59   return expected;
60 }
61 
GetExpectedCellData()62 const std::vector<float>& GetExpectedCellData()
63 {
64   static std::vector<float> expected = {
65     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,
66     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,
67     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,
68     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,
69     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,
70     0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
71   };
72   return expected;
73 }
74 
GetExpectedHiddenPoints()75 const std::vector<size_t>& GetExpectedHiddenPoints()
76 {
77   static std::vector<size_t> expected = { 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2,
78                                           2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0,
79                                           2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0,
80                                           0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2,
81                                           2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
82   return expected;
83 }
84 
GetExpectedHiddenCells()85 const std::vector<size_t>& GetExpectedHiddenCells()
86 {
87   static std::vector<size_t>expected = { 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 2, 2,
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                                          2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
91   return expected;
92 }
93 
94 template <typename T>
TestResultArray(vtkDataArray * result,const std::vector<T> & expected)95 void TestResultArray(vtkDataArray* result, const std::vector<T>& expected)
96 {
97   if (result->GetNumberOfValues() != static_cast<vtkIdType>(expected.size()))
98   {
99     std::cout << "Array " << result->GetName() << " has wrong size" << std::endl;
100   }
101   assert(result->GetNumberOfValues() == static_cast<vtkIdType>(expected.size()));
102 
103   for (vtkIdType i = 0; i < result->GetNumberOfValues(); ++i)
104   {
105     if ((result->GetComponent(0, i) - expected[static_cast<size_t>(i)]) > 1e-5)
106     {
107       std::cout << "Array " << result->GetName() << " has wrong value" <<
108                 " at index " << i << ". result value="<<
109                 result->GetComponent(0, i) << " expected value=" <<
110                    expected[static_cast<size_t>(i)] << std::endl;
111     }
112     assert((result->GetComponent(0, i) - expected[static_cast<size_t>(i)]) < 1e-5);
113   }
114 }
115 
116 } //  Anonymous namespace
117 
TestVTKMProbe(int,char * [])118 int TestVTKMProbe(int, char*[])
119 {
120   vtkNew<vtkImageData> input;
121   input->SetOrigin(0.7, 0.7, 0.0);
122   input->SetSpacing(0.35, 0.35, 1.0);
123   input->SetExtent(0, inputDim -1, 0, inputDim -1, 0, 0);
124 
125   vtkNew<vtkImageData> source;
126   source->SetOrigin(0.0, 0.0, 0.0);
127   source->SetSpacing(1.0, 1.0, 1.0);
128   source->SetExtent(0, sourceDim -1, 0, sourceDim -1, 0, 0);
129 
130   vtkNew<vtkFloatArray> pointArray, cellArray;
131   populatePointAndCellArray(pointArray, cellArray);
132   source->GetPointData()->AddArray(pointArray);
133   source->GetCellData()->AddArray(cellArray);
134 
135   vtkNew<vtkmProbe> probe;
136   probe->SetValidPointMaskArrayName("validPoint");
137   probe->SetValidCellMaskArrayName("validCell");
138   probe->SetInputData(input);
139   probe->SetSourceData(source);
140   probe->Update();
141 
142   vtkDataSet* result = probe->GetOutput();
143   TestResultArray(result->GetPointData()->GetArray(pointArray->GetName()),
144                   GetExpectedPointData());
145   TestResultArray(result->GetCellData()->GetArray(cellArray->GetName()),
146                   GetExpectedCellData());
147   TestResultArray(result->GetPointData()->GetArray("validPoint"),
148                   GetExpectedHiddenPoints());
149   TestResultArray(result->GetCellData()->GetArray("validCell"),
150                   GetExpectedHiddenCells());
151   return 0;
152 }
153