Commit ce338d14 authored by Kenneth Moreland's avatar Kenneth Moreland

Add a worklet for copying cell sets

parent 3f9a1edb
......@@ -443,11 +443,12 @@ make_ArrayHandleGroupVecVariable(const SourceArrayHandleType &sourceArray,
namespace detail {
template<typename NumComponentsArrayType>
template<typename NumComponentsArrayType,
typename OffsetsArrayType>
struct ConvertNumComponentsToOffsetsFunctor
{
const NumComponentsArrayType NumComponentsArray;
vtkm::cont::ArrayHandle<vtkm::Id> OffsetsArray;
OffsetsArrayType OffsetsArray;
vtkm::Id SourceArraySize;
VTKM_CONT
......@@ -468,16 +469,19 @@ struct ConvertNumComponentsToOffsetsFunctor
}
};
template<typename NumComponentsArrayType>
template<typename NumComponentsArrayType,
typename OffsetsArrayType>
VTKM_CONT
vtkm::cont::ArrayHandle<vtkm::Id>
DoConvertNumComponentsToOffsets(const NumComponentsArrayType &numComponentsArray,
vtkm::Id &sourceArraySize)
void DoConvertNumComponentsToOffsets(
const NumComponentsArrayType &numComponentsArray,
OffsetsArrayType &offsetsArray,
vtkm::Id &sourceArraySize)
{
VTKM_IS_ARRAY_HANDLE(NumComponentsArrayType);
VTKM_IS_ARRAY_HANDLE(OffsetsArrayType);
detail::ConvertNumComponentsToOffsetsFunctor<NumComponentsArrayType>
functor(numComponentsArray);
detail::ConvertNumComponentsToOffsetsFunctor<
NumComponentsArrayType,OffsetsArrayType> functor(numComponentsArray);
bool success = vtkm::cont::TryExecute(functor);
if (!success)
......@@ -488,7 +492,7 @@ DoConvertNumComponentsToOffsets(const NumComponentsArrayType &numComponentsArray
}
sourceArraySize = functor.SourceArraySize;
return functor.OffsetsArray;
offsetsArray = functor.OffsetsArray;
}
} // namespace detail
......@@ -501,27 +505,44 @@ DoConvertNumComponentsToOffsets(const NumComponentsArrayType &numComponentsArray
/// If an optional second parameter is given, the expected size of the source
/// values array is returned in it.
///
template<typename NumComponentsArrayType>
template<typename NumComponentsArrayType,
typename OffsetsStorage>
VTKM_CONT
vtkm::cont::ArrayHandle<vtkm::Id>
ConvertNumComponentsToOffsets(const NumComponentsArrayType &numComponentsArray,
vtkm::Id &sourceArraySize)
void ConvertNumComponentsToOffsets(
const NumComponentsArrayType &numComponentsArray,
vtkm::cont::ArrayHandle<vtkm::Id,OffsetsStorage> &offsetsArray,
vtkm::Id &sourceArraySize)
{
VTKM_IS_ARRAY_HANDLE(NumComponentsArrayType);
return detail::DoConvertNumComponentsToOffsets(
detail::DoConvertNumComponentsToOffsets(
vtkm::cont::make_ArrayHandleCast<vtkm::Id>(numComponentsArray),
offsetsArray,
sourceArraySize);
}
template<typename StorageTag>
template<typename NumComponentsArrayType,
typename OffsetsStorage>
VTKM_CONT
void ConvertNumComponentsToOffsets(
const NumComponentsArrayType &numComponentsArray,
vtkm::cont::ArrayHandle<vtkm::Id,OffsetsStorage> &offsetsArray)
{
vtkm::Id dummy;
vtkm::cont::ConvertNumComponentsToOffsets(
numComponentsArray, offsetsArray, dummy);
}
template<typename NumComponentsArrayType>
VTKM_CONT
vtkm::cont::ArrayHandle<vtkm::Id>
ConvertNumComponentsToOffsets(
const vtkm::cont::ArrayHandle<vtkm::Id,StorageTag> &numComponentsArray,
vtkm::Id &sourceArraySize)
ConvertNumComponentsToOffsets(const NumComponentsArrayType &numComponentsArray,
vtkm::Id &sourceArraySize)
{
return detail::DoConvertNumComponentsToOffsets(numComponentsArray,
sourceArraySize);
VTKM_IS_ARRAY_HANDLE(NumComponentsArrayType);
vtkm::cont::ArrayHandle<vtkm::Id> offsetsArray;
vtkm::cont::ConvertNumComponentsToOffsets(
numComponentsArray, offsetsArray, sourceArraySize);
return offsetsArray;
}
template<typename NumComponentsArrayType>
VTKM_CONT
......@@ -529,7 +550,7 @@ vtkm::cont::ArrayHandle<vtkm::Id>
ConvertNumComponentsToOffsets(const NumComponentsArrayType &numComponentsArray)
{
vtkm::Id dummy;
return ConvertNumComponentsToOffsets(numComponentsArray, dummy);
return vtkm::cont::ConvertNumComponentsToOffsets(numComponentsArray, dummy);
}
}
......
......@@ -132,6 +132,9 @@ template< typename OriginalCellSet,
typename PermutationArrayHandleType = vtkm::cont::ArrayHandle< vtkm::Id, VTKM_DEFAULT_CELLSET_PERMUTATION_STORAGE_TAG > >
class CellSetPermutation : public vtkm::cont::internal::CellSetGeneralPermutation<OriginalCellSet, PermutationArrayHandleType>
{
VTKM_IS_CELL_SET(OriginalCellSet);
VTKM_IS_ARRAY_HANDLE(PermutationArrayHandleType);
typedef typename vtkm::cont::internal::CellSetGeneralPermutation<
OriginalCellSet, PermutationArrayHandleType> ParentType;
public:
......@@ -151,6 +154,32 @@ public:
};
template<typename OriginalCellSet, typename PermutationArrayHandleType>
vtkm::cont::CellSetPermutation<OriginalCellSet,PermutationArrayHandleType>
make_CellSetPermutation(const PermutationArrayHandleType &cellIndexMap,
const OriginalCellSet &cellSet,
const std::string &name)
{
VTKM_IS_CELL_SET(OriginalCellSet);
VTKM_IS_ARRAY_HANDLE(PermutationArrayHandleType);
return vtkm::cont::CellSetPermutation<
OriginalCellSet,PermutationArrayHandleType>(
cellIndexMap, cellSet, name);
}
template<typename OriginalCellSet, typename PermutationArrayHandleType>
vtkm::cont::CellSetPermutation<OriginalCellSet,PermutationArrayHandleType>
make_CellSetPermutation(const PermutationArrayHandleType &cellIndexMap,
const OriginalCellSet &cellSet)
{
VTKM_IS_CELL_SET(OriginalCellSet);
VTKM_IS_ARRAY_HANDLE(PermutationArrayHandleType);
return vtkm::cont::make_CellSetPermutation(
cellIndexMap, cellSet, cellSet.GetName());
}
}
} // namespace vtkm::cont
......
......@@ -26,6 +26,7 @@ set(headers
WorkletMapTopology.h
AverageByKey.h
CellAverage.h
CellDeepCopy.h
Clip.h
ExternalFaces.h
FieldHistogram.h
......
//============================================================================
// 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.
//
// Copyright 2016 Sandia Corporation.
// Copyright 2016 UT-Battelle, LLC.
// Copyright 2016 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_worklet_CellDeepCopy_h
#define vtk_m_worklet_CellDeepCopy_h
#include <vtkm/cont/ArrayHandleConstant.h>
#include <vtkm/cont/ArrayHandleGroupVecVariable.h>
#include <vtkm/cont/CellSetExplicit.h>
#include <vtkm/worklet/DispatcherMapTopology.h>
#include <vtkm/worklet/WorkletMapTopology.h>
namespace vtkm {
namespace worklet {
/// Container for worklets and helper methods to copy a cell set to a new
/// \c CellSetExplicit structure
///
struct CellDeepCopy
{
struct CountCellPoints : vtkm::worklet::WorkletMapPointToCell
{
typedef void ControlSignature(CellSetIn inputTopology,
FieldOut<> numPointsInCell);
typedef _2 ExecutionSignature(PointCount);
VTKM_EXEC
vtkm::IdComponent operator()(vtkm::IdComponent numPoints) const
{
return numPoints;
}
};
struct PassCellStructure : vtkm::worklet::WorkletMapPointToCell
{
typedef void ControlSignature(CellSetIn inputTopology,
FieldOut<> shapes,
FieldOut<> pointIndices);
typedef void ExecutionSignature(CellShape, PointIndices, _2, _3);
template<typename CellShape,
typename InPointIndexType,
typename OutPointIndexType>
VTKM_EXEC
void operator()(const CellShape &inShape,
const InPointIndexType &inPoints,
vtkm::UInt8 &outShape,
OutPointIndexType &outPoints) const
{
outShape = inShape.Id;
vtkm::IdComponent numPoints = inPoints.GetNumberOfComponents();
VTKM_ASSERT(numPoints == outPoints.GetNumberOfComponents());
for (vtkm::IdComponent pointIndex = 0; pointIndex<numPoints; pointIndex++)
{
outPoints[pointIndex] = inPoints[pointIndex];
}
}
};
template<typename InCellSetType,
typename ShapeStorage,
typename NumIndicesStorage,
typename ConnectivityStorage,
typename OffsetsStorage,
typename Device>
VTKM_CONT
static void
Run(const InCellSetType &inCellSet,
vtkm::cont::CellSetExplicit<ShapeStorage,NumIndicesStorage,ConnectivityStorage,OffsetsStorage> &outCellSet,
Device)
{
VTKM_IS_CELL_SET(InCellSetType);
vtkm::cont::ArrayHandle<vtkm::IdComponent,NumIndicesStorage> numIndices;
vtkm::worklet::DispatcherMapTopology<CountCellPoints,Device> countDispatcher;
countDispatcher.Invoke(inCellSet, numIndices);
vtkm::cont::ArrayHandle<vtkm::UInt8,ShapeStorage> shapes;
vtkm::cont::ArrayHandle<vtkm::Id,ConnectivityStorage> connectivity;
vtkm::cont::ArrayHandle<vtkm::Id,OffsetsStorage> offsets;
vtkm::Id connectivitySize;
vtkm::cont::ConvertNumComponentsToOffsets(
numIndices, offsets, connectivitySize);
connectivity.Allocate(connectivitySize);
vtkm::worklet::DispatcherMapTopology<PassCellStructure,Device> passDispatcher;
passDispatcher.Invoke(
inCellSet,
shapes,
vtkm::cont::make_ArrayHandleGroupVecVariable(connectivity, offsets));
vtkm::cont::CellSetExplicit<ShapeStorage,NumIndicesStorage,ConnectivityStorage,OffsetsStorage>
newCellSet(inCellSet.GetNumberOfPoints(), inCellSet.GetName());
newCellSet.Fill(shapes, numIndices, connectivity, offsets);
outCellSet = newCellSet;
}
template<typename InCellSetType, typename Device>
VTKM_CONT
static vtkm::cont::CellSetExplicit<>
Run(const InCellSetType &inCellSet, Device)
{
VTKM_IS_CELL_SET(InCellSetType);
vtkm::cont::CellSetExplicit<> outCellSet;
Run(inCellSet, outCellSet, Device());
return outCellSet;
}
};
}
} // namespace vtkm::worklet
#endif //vtk_m_worklet_CellDeepCopy_h
......@@ -49,7 +49,8 @@ public:
///
struct GeneratePointMask : public vtkm::worklet::WorkletMapField
{
typedef void ControlSignature(FieldIn<>, WholeArrayInOut<>);
typedef void ControlSignature(FieldIn<> pointIndices,
WholeArrayInOut<> pointMask);
typedef void ExecutionSignature(_1, _2);
template<typename PointMaskPortalType>
......@@ -68,9 +69,9 @@ public:
///
struct TransformPointIndices : public vtkm::worklet::WorkletMapField
{
typedef void ControlSignature(FieldIn<IdType>,
WholeArrayIn<IdType>,
FieldOut<IdType>);
typedef void ControlSignature(FieldIn<IdType> pointIndex,
WholeArrayIn<IdType> indexMap,
FieldOut<IdType> mappedPoints);
typedef _3 ExecutionSignature(_1, _2);
template<typename IndexMapPortalType>
......
......@@ -20,6 +20,7 @@
set(unit_tests
UnitTestCellAverage.cxx
UnitTestCellDeepCopy.cxx
UnitTestCellGradient.cxx
UnitTestClipping.cxx
UnitTestExternalFaces.cxx
......
//============================================================================
// 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.
//
// Copyright 2016 Sandia Corporation.
// Copyright 2016 UT-Battelle, LLC.
// Copyright 2016 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/worklet/CellDeepCopy.h>
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/CellSetExplicit.h>
#include <vtkm/cont/CellSetPermutation.h>
#include <vtkm/cont/testing/MakeTestDataSet.h>
#include <vtkm/cont/testing/Testing.h>
namespace {
vtkm::cont::CellSetExplicit<>
CreateCellSet()
{
vtkm::cont::testing::MakeTestDataSet makeData;
vtkm::cont::DataSet data = makeData.Make3DExplicitDataSet0();
vtkm::cont::CellSetExplicit<> cellSet;
data.GetCellSet().CopyTo(cellSet);
return cellSet;
}
vtkm::cont::CellSetPermutation<
vtkm::cont::CellSetExplicit<>,
vtkm::cont::ArrayHandleCounting<vtkm::Id> >
CreatePermutedCellSet()
{
std::cout << "Creating input cell set" << std::endl;
vtkm::cont::CellSetExplicit<> cellSet = CreateCellSet();
return vtkm::cont::make_CellSetPermutation(
vtkm::cont::ArrayHandleCounting<vtkm::Id>(
cellSet.GetNumberOfCells()-1, -1, cellSet.GetNumberOfCells()),
cellSet);
}
template<typename CellSetType>
vtkm::cont::CellSetExplicit<>
DoCellDeepCopy(const CellSetType &inCells)
{
std::cout << "Doing cell copy" << std::endl;
return vtkm::worklet::CellDeepCopy::Run(inCells,
VTKM_DEFAULT_DEVICE_ADAPTER_TAG());
}
void CheckOutput(const vtkm::cont::CellSetExplicit<> &copiedCells)
{
std::cout << "Checking copied cells" << std::endl;
vtkm::cont::CellSetExplicit<> originalCells = CreateCellSet();
vtkm::Id numberOfCells = copiedCells.GetNumberOfCells();
VTKM_TEST_ASSERT(numberOfCells == originalCells.GetNumberOfCells(),
"Result has wrong number of cells");
// Cells should be copied backward. Check that.
for (vtkm::Id cellIndex = 0; cellIndex < numberOfCells; cellIndex++)
{
vtkm::Id oCellIndex = numberOfCells - cellIndex - 1;
VTKM_TEST_ASSERT(copiedCells.GetCellShape(cellIndex) ==
originalCells.GetCellShape(oCellIndex),
"Bad cell shape");
vtkm::IdComponent numPoints =
copiedCells.GetNumberOfPointsInCell(cellIndex);
VTKM_TEST_ASSERT(
numPoints == originalCells.GetNumberOfPointsInCell(oCellIndex),
"Bad number of points in cell");
// Only checking 3 points. All cells should have at least 3
vtkm::Vec<vtkm::Id,3> cellPoints;
copiedCells.GetIndices(cellIndex, cellPoints);
vtkm::Vec<vtkm::Id,3> oCellPoints;
originalCells.GetIndices(oCellIndex, oCellPoints);
VTKM_TEST_ASSERT(cellPoints == oCellPoints,
"Point indices not copied correctly");
}
}
void RunTest()
{
vtkm::cont::CellSetExplicit<> cellSet =
DoCellDeepCopy(CreatePermutedCellSet());
CheckOutput(cellSet);
}
} // anonymous namespace
int UnitTestCellDeepCopy(int, char *[])
{
return vtkm::cont::testing::Testing::Run(RunTest);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment