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 <iostream>
12 
13 #include <vtkm/cont/DataSet.h>
14 #include <vtkm/cont/DeviceAdapter.h>
15 #include <vtkm/cont/testing/MakeTestDataSet.h>
16 #include <vtkm/cont/testing/Testing.h>
17 
18 #include <vtkm/worklet/DispatcherMapField.h>
19 #include <vtkm/worklet/VertexClustering.h>
20 
TestVertexClustering()21 void TestVertexClustering()
22 {
23   const vtkm::Id3 divisions(3, 3, 3);
24   vtkm::cont::testing::MakeTestDataSet maker;
25   vtkm::cont::DataSet dataSet = maker.Make3DExplicitDataSetCowNose();
26 
27   //compute the bounds before calling the algorithm
28   vtkm::Bounds bounds = dataSet.GetCoordinateSystem().GetBounds();
29 
30   // run
31   vtkm::worklet::VertexClustering clustering;
32   vtkm::cont::DataSet outDataSet =
33     clustering.Run(dataSet.GetCellSet(), dataSet.GetCoordinateSystem(), bounds, divisions);
34 
35   using FieldArrayType = vtkm::cont::ArrayHandle<vtkm::Float32>;
36   FieldArrayType pointvar = clustering.ProcessPointField(
37     dataSet.GetPointField("pointvar").GetData().AsArrayHandle<FieldArrayType>());
38   FieldArrayType cellvar = clustering.ProcessCellField(
39     dataSet.GetCellField("cellvar").GetData().AsArrayHandle<FieldArrayType>());
40 
41   // test
42   const vtkm::Id output_pointIds = 18;
43   vtkm::Id output_pointId[output_pointIds] = {
44     0, 1, 3, 1, 4, 3, 2, 5, 3, 0, 3, 5, 2, 3, 6, 3, 4, 6
45   };
46   const vtkm::Id output_points = 7;
47   vtkm::Float64 output_point[output_points][3] = {
48     { 0.0174716, 0.0501928, 0.0930275 }, { 0.0307091, 0.15214200, 0.0539249 },
49     { 0.0174172, 0.1371240, 0.1245530 }, { 0.0480879, 0.15187400, 0.1073340 },
50     { 0.0180085, 0.2043600, 0.1453160 }, { -.000129414, 0.00247137, 0.1765610 },
51     { 0.0108188, 0.1527740, 0.1679140 }
52   };
53 
54   vtkm::Float32 output_pointvar[output_points] = { 28.f, 19.f, 25.f, 15.f, 16.f, 21.f, 30.f };
55   vtkm::Float32 output_cellvar[output_pointIds / 3] = { 145.f, 134.f, 138.f, 140.f, 149.f, 144.f };
56 
57   {
58     using CellSetType = vtkm::cont::CellSetSingleType<>;
59     CellSetType cellSet;
60     outDataSet.GetCellSet().CopyTo(cellSet);
61     auto cellArray =
62       cellSet.GetConnectivityArray(vtkm::TopologyElementTagCell(), vtkm::TopologyElementTagPoint());
63     std::cerr << "output_pointIds = " << cellArray.GetNumberOfValues() << "\n";
64     std::cerr << "output_pointId[] = ";
65     vtkm::cont::printSummary_ArrayHandle(cellArray, std::cerr, true);
66   }
67 
68   {
69     auto pointArray = outDataSet.GetCoordinateSystem(0).GetData();
70     std::cerr << "output_points = " << pointArray.GetNumberOfValues() << "\n";
71     std::cerr << "output_point[] = ";
72     pointArray.PrintSummary(std::cerr);
73   }
74 
75   vtkm::cont::printSummary_ArrayHandle(pointvar, std::cerr, true);
76   vtkm::cont::printSummary_ArrayHandle(cellvar, std::cerr, true);
77 
78   VTKM_TEST_ASSERT(outDataSet.GetNumberOfCoordinateSystems() == 1,
79                    "Number of output coordinate systems mismatch");
80   using PointType = vtkm::Vec3f_64;
81   auto pointArray = outDataSet.GetCoordinateSystem(0).GetDataAsMultiplexer();
82   VTKM_TEST_ASSERT(pointArray.GetNumberOfValues() == output_points,
83                    "Number of output points mismatch");
84   for (vtkm::Id i = 0; i < pointArray.GetNumberOfValues(); ++i)
85   {
86     const PointType& p1 = pointArray.ReadPortal().Get(i);
87     PointType p2 = vtkm::make_Vec(output_point[i][0], output_point[i][1], output_point[i][2]);
88     VTKM_TEST_ASSERT(test_equal(p1, p2), "Point Array mismatch");
89   }
90 
91   using CellSetType = vtkm::cont::CellSetSingleType<>;
92   CellSetType cellSet;
93   outDataSet.GetCellSet().CopyTo(cellSet);
94   VTKM_TEST_ASSERT(
95     cellSet.GetConnectivityArray(vtkm::TopologyElementTagCell(), vtkm::TopologyElementTagPoint())
96         .GetNumberOfValues() == output_pointIds,
97     "Number of connectivity array elements mismatch");
98   for (vtkm::Id i = 0; i <
99        cellSet.GetConnectivityArray(vtkm::TopologyElementTagCell(), vtkm::TopologyElementTagPoint())
100          .GetNumberOfValues();
101        i++)
102   {
103     vtkm::Id id1 =
104       cellSet.GetConnectivityArray(vtkm::TopologyElementTagCell(), vtkm::TopologyElementTagPoint())
105         .ReadPortal()
106         .Get(i);
107     vtkm::Id id2 = output_pointId[i];
108     VTKM_TEST_ASSERT(id1 == id2, "Connectivity Array mismatch");
109   }
110 
111   {
112     auto portal = pointvar.ReadPortal();
113     VTKM_TEST_ASSERT(portal.GetNumberOfValues() == output_points, "Point field size mismatch.");
114     for (vtkm::Id i = 0; i < portal.GetNumberOfValues(); ++i)
115     {
116       VTKM_TEST_ASSERT(test_equal(portal.Get(i), output_pointvar[i]), "Point field mismatch.");
117     }
118   }
119 
120   {
121     auto portal = cellvar.ReadPortal();
122     VTKM_TEST_ASSERT(portal.GetNumberOfValues() == output_pointIds / 3,
123                      "Cell field size mismatch.");
124     for (vtkm::Id i = 0; i < portal.GetNumberOfValues(); ++i)
125     {
126       VTKM_TEST_ASSERT(test_equal(portal.Get(i), output_cellvar[i]), "Cell field mismatch.");
127     }
128   }
129 
130 } // TestVertexClustering
131 
UnitTestVertexClustering(int argc,char * argv[])132 int UnitTestVertexClustering(int argc, char* argv[])
133 {
134   return vtkm::cont::testing::Testing::Run(TestVertexClustering, argc, argv);
135 }
136