vtk-m/vtkm/worklet/testing/UnitTestCellSetDualGraph.cxx
Kenneth Moreland b1343474c1 Consolidate count-to-offset algorithms
For no particularly good reason, there were two functions that converted
and array of counts to an array of offsets: `ConvertNumComponentsToOffsets`
and `ConvertNumIndicesToOffsets`. These functions were identical, except
one was defined in `ArrayHandleGroupVecVariable.h` and the other was
defined in `CellSetExplicit.h`.

These two functions have been consolidated into one (which is now called
`ConvertNumComponentsToOffsets`). The consolidated function has also been
put in its own header file: `ConvertNumComponentsToOffsets.h`.

Normally, backward compatibility would be established using deprecated
features. However, one of the things being worked on is the removal of
device-specific code (e.g. `vtkm::cont::Algorithm`) from core classes like
`CellSetExplicit` so that less code needs to use the device compiler
(especially downstream code).

Part of this change removed unnecessary includes of `Algorithm.h` in
`ArrayHandleGroupVecVariable.h` and `CellSetExplicit.h`. This header had to
be added to some classes that were not including it themselves.
2021-09-16 14:24:41 -06:00

113 lines
4.2 KiB
C++

//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//============================================================================
#include <vtkm/cont/ArrayHandleGroupVecVariable.h>
#include <vtkm/cont/ConvertNumComponentsToOffsets.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/filter/Contour.h>
#include <vtkm/worklet/connectivities/CellSetDualGraph.h>
class TestCellSetDualGraph
{
private:
using GroupedConnectivityArrayType =
vtkm::cont::ArrayHandleGroupVecVariable<vtkm::cont::ArrayHandle<vtkm::Id>,
vtkm::cont::ArrayHandle<vtkm::Id>>;
static GroupedConnectivityArrayType MakeGroupedConnectivity(
vtkm::cont::ArrayHandle<vtkm::Id> connectivity,
vtkm::cont::ArrayHandle<vtkm::Id> counts)
{
return GroupedConnectivityArrayType(connectivity,
vtkm::cont::ConvertNumComponentsToOffsets(counts));
}
static bool TestConnectivity(GroupedConnectivityArrayType computedConnectivityArray,
GroupedConnectivityArrayType expectedConnectivityArray)
{
auto computedConnections = computedConnectivityArray.ReadPortal();
auto expectedConnections = expectedConnectivityArray.ReadPortal();
vtkm::Id numItems = computedConnections.GetNumberOfValues();
if (numItems != expectedConnections.GetNumberOfValues())
{
return false;
}
for (vtkm::Id itemIndex = 0; itemIndex < numItems; ++itemIndex)
{
auto computed = computedConnections.Get(itemIndex);
auto expected = expectedConnections.Get(itemIndex);
vtkm::IdComponent numConnections = computed.GetNumberOfComponents();
if (numConnections != expected.GetNumberOfComponents())
{
return false;
}
// computed and expected are Vec-like objects that should represent the same thing.
// However, although both should have the same indices, they may be in different
// orders.
std::set<vtkm::Id> computedSet;
std::set<vtkm::Id> expectedSet;
for (vtkm::IdComponent componentIndex = 0; componentIndex < numConnections; ++componentIndex)
{
computedSet.insert(computed[componentIndex]);
expectedSet.insert(expected[componentIndex]);
}
if (computedSet != expectedSet)
{
return false;
}
}
return true;
}
public:
void TestTriangleMesh() const
{
auto connectivity = vtkm::cont::make_ArrayHandle<vtkm::Id>(
{ 0, 2, 4, 1, 3, 5, 2, 6, 4, 5, 3, 7, 2, 9, 6, 4, 6, 8 });
vtkm::cont::CellSetSingleType<> cellSet;
cellSet.Fill(10, vtkm::CELL_SHAPE_TRIANGLE, 3, connectivity);
vtkm::cont::ArrayHandle<vtkm::Id> numIndicesArray;
vtkm::cont::ArrayHandle<vtkm::Id> indexOffsetArray;
vtkm::cont::ArrayHandle<vtkm::Id> connectivityArray;
vtkm::worklet::connectivity::CellSetDualGraph().Run(
cellSet, numIndicesArray, indexOffsetArray, connectivityArray);
vtkm::cont::ArrayHandle<vtkm::Id> expectedNumIndices =
vtkm::cont::make_ArrayHandle<vtkm::Id>({ 1, 1, 3, 1, 1, 1 });
VTKM_TEST_ASSERT(
test_equal_portals(numIndicesArray.ReadPortal(), expectedNumIndices.ReadPortal()));
vtkm::cont::ArrayHandle<vtkm::Id> expectedIndexOffset =
vtkm::cont::make_ArrayHandle<vtkm::Id>({ 0, 1, 2, 5, 6, 7 });
VTKM_TEST_ASSERT(
test_equal_portals(indexOffsetArray.ReadPortal(), expectedIndexOffset.ReadPortal()));
vtkm::cont::ArrayHandle<vtkm::Id> expectedConnectivity =
vtkm::cont::make_ArrayHandle<vtkm::Id>({ 2, 3, 0, 4, 5, 1, 2, 2 });
VTKM_TEST_ASSERT(
TestConnectivity(MakeGroupedConnectivity(connectivityArray, numIndicesArray),
MakeGroupedConnectivity(expectedConnectivity, numIndicesArray)));
}
void operator()() const { this->TestTriangleMesh(); }
};
int UnitTestCellSetDualGraph(int argc, char* argv[])
{
return vtkm::cont::testing::Testing::Run(TestCellSetDualGraph(), argc, argv);
}