1 //============================================================================
2 //  Copyright (c) Kitware, Inc.
3 //  All rights reserved.
4 //  See LICENSE.txt for details.
5 //  This software is distributed WITHOUT ANY WARRANTY; without even
6 //  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
7 //  PURPOSE.  See the above copyright notice for more information.
8 //
9 //  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 //  Copyright 2014 UT-Battelle, LLC.
11 //  Copyright 2014 Los Alamos National Security.
12 //
13 //  Under the terms of Contract DE-NA0003525 with NTESS,
14 //  the U.S. Government retains certain rights in this software.
15 //
16 //  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
17 //  Laboratory (LANL), the U.S. Government retains certain rights in
18 //  this software.
19 //============================================================================
20 
21 #include <iostream>
22 
23 #include <vtkm/cont/DataSet.h>
24 #include <vtkm/cont/DeviceAdapter.h>
25 #include <vtkm/cont/testing/MakeTestDataSet.h>
26 #include <vtkm/cont/testing/Testing.h>
27 
28 #include <vtkm/worklet/DispatcherMapField.h>
29 #include <vtkm/worklet/VertexClustering.h>
30 
TestVertexClustering()31 void TestVertexClustering()
32 {
33   VTKM_DEFAULT_DEVICE_ADAPTER_TAG device;
34 
35   const vtkm::Id3 divisions(3, 3, 3);
36   vtkm::cont::testing::MakeTestDataSet maker;
37   vtkm::cont::DataSet dataSet = maker.Make3DExplicitDataSetCowNose();
38 
39   //compute the bounds before calling the algorithm
40   vtkm::Bounds bounds = dataSet.GetCoordinateSystem().GetBounds();
41 
42   // run
43   vtkm::worklet::VertexClustering clustering;
44   vtkm::cont::DataSet outDataSet =
45     clustering.Run(dataSet.GetCellSet(), dataSet.GetCoordinateSystem(), bounds, divisions, device);
46 
47   using FieldArrayType = vtkm::cont::ArrayHandle<vtkm::Float32>;
48   FieldArrayType pointvar = clustering.ProcessPointField(
49     dataSet.GetPointField("pointvar").GetData().Cast<FieldArrayType>(), device);
50   FieldArrayType cellvar = clustering.ProcessCellField(
51     dataSet.GetCellField("cellvar").GetData().Cast<FieldArrayType>(), device);
52 
53   // test
54   const vtkm::Id output_pointIds = 18;
55   vtkm::Id output_pointId[output_pointIds] = {
56     0, 1, 3, 1, 4, 3, 2, 5, 3, 0, 3, 5, 2, 3, 6, 3, 4, 6
57   };
58   const vtkm::Id output_points = 7;
59   vtkm::Float64 output_point[output_points][3] = {
60     { 0.0174716, 0.0501928, 0.0930275 }, { 0.0307091, 0.15214200, 0.0539249 },
61     { 0.0174172, 0.1371240, 0.1245530 }, { 0.0480879, 0.15187400, 0.1073340 },
62     { 0.0180085, 0.2043600, 0.1453160 }, { -.000129414, 0.00247137, 0.1765610 },
63     { 0.0108188, 0.1527740, 0.1679140 }
64   };
65 
66   vtkm::Float32 output_pointvar[output_points] = { 28.f, 19.f, 25.f, 15.f, 16.f, 21.f, 30.f };
67   vtkm::Float32 output_cellvar[output_pointIds / 3] = { 145.f, 134.f, 138.f, 140.f, 149.f, 144.f };
68 
69   {
70     using CellSetType = vtkm::cont::CellSetSingleType<>;
71     CellSetType cellSet;
72     outDataSet.GetCellSet(0).CopyTo(cellSet);
73     auto cellArray =
74       cellSet.GetConnectivityArray(vtkm::TopologyElementTagPoint(), vtkm::TopologyElementTagCell());
75     std::cerr << "output_pointIds = " << cellArray.GetNumberOfValues() << "\n";
76     std::cerr << "output_pointId[] = ";
77     vtkm::cont::printSummary_ArrayHandle(cellArray, std::cerr, true);
78   }
79 
80   {
81     auto pointArray = outDataSet.GetCoordinateSystem(0).GetData();
82     std::cerr << "output_points = " << pointArray.GetNumberOfValues() << "\n";
83     std::cerr << "output_point[] = ";
84     vtkm::cont::printSummary_ArrayHandle(pointArray, std::cerr, true);
85   }
86 
87   vtkm::cont::printSummary_ArrayHandle(pointvar, std::cerr, true);
88   vtkm::cont::printSummary_ArrayHandle(cellvar, std::cerr, true);
89 
90   VTKM_TEST_ASSERT(outDataSet.GetNumberOfCoordinateSystems() == 1,
91                    "Number of output coordinate systems mismatch");
92   using PointType = vtkm::Vec<vtkm::Float64, 3>;
93   auto pointArray = outDataSet.GetCoordinateSystem(0).GetData();
94   VTKM_TEST_ASSERT(pointArray.GetNumberOfValues() == output_points,
95                    "Number of output points mismatch");
96   for (vtkm::Id i = 0; i < pointArray.GetNumberOfValues(); ++i)
97   {
98     const PointType& p1 = pointArray.GetPortalConstControl().Get(i);
99     PointType p2 = vtkm::make_Vec(output_point[i][0], output_point[i][1], output_point[i][2]);
100     VTKM_TEST_ASSERT(test_equal(p1, p2), "Point Array mismatch");
101   }
102 
103   using CellSetType = vtkm::cont::CellSetSingleType<>;
104   VTKM_TEST_ASSERT(outDataSet.GetNumberOfCellSets() == 1, "Number of output cellsets mismatch");
105   CellSetType cellSet;
106   outDataSet.GetCellSet(0).CopyTo(cellSet);
107   VTKM_TEST_ASSERT(
108     cellSet.GetConnectivityArray(vtkm::TopologyElementTagPoint(), vtkm::TopologyElementTagCell())
109         .GetNumberOfValues() == output_pointIds,
110     "Number of connectivity array elements mismatch");
111   for (vtkm::Id i = 0; i <
112        cellSet.GetConnectivityArray(vtkm::TopologyElementTagPoint(), vtkm::TopologyElementTagCell())
113          .GetNumberOfValues();
114        i++)
115   {
116     vtkm::Id id1 =
117       cellSet.GetConnectivityArray(vtkm::TopologyElementTagPoint(), vtkm::TopologyElementTagCell())
118         .GetPortalConstControl()
119         .Get(i);
120     vtkm::Id id2 = output_pointId[i];
121     VTKM_TEST_ASSERT(id1 == id2, "Connectivity Array mismatch");
122   }
123 
124   {
125     auto portal = pointvar.GetPortalConstControl();
126     VTKM_TEST_ASSERT(portal.GetNumberOfValues() == output_points, "Point field size mismatch.");
127     for (vtkm::Id i = 0; i < portal.GetNumberOfValues(); ++i)
128     {
129       VTKM_TEST_ASSERT(test_equal(portal.Get(i), output_pointvar[i]), "Point field mismatch.");
130     }
131   }
132 
133   {
134     auto portal = cellvar.GetPortalConstControl();
135     VTKM_TEST_ASSERT(portal.GetNumberOfValues() == output_pointIds / 3,
136                      "Cell field size mismatch.");
137     for (vtkm::Id i = 0; i < portal.GetNumberOfValues(); ++i)
138     {
139       VTKM_TEST_ASSERT(test_equal(portal.Get(i), output_cellvar[i]), "Cell field mismatch.");
140     }
141   }
142 
143 } // TestVertexClustering
144 
UnitTestVertexClustering(int,char * [])145 int UnitTestVertexClustering(int, char* [])
146 {
147   return vtkm::cont::testing::Testing::Run(TestVertexClustering);
148 }
149