Commit 9a8809f9 authored by Robert Maynard's avatar Robert Maynard

Add CellSetPermutation which allows custom iteration over a cell set.

When you create a CellSetPermutation you provide an array of the cell ids that
you want to iterate. This allows the user to do custom blanking of a data set,
or to do multi iteration over a set of cells.
parent 66f6db5a
......@@ -38,9 +38,10 @@ set(headers
Assert.h
CellSet.h
CellSetExplicit.h
CellSetSingleType.h
CellSetListTag.h
CellSetSingleType.h
CellSetStructured.h
CellSetPermutation.h
CoordinateSystem.h
DataSet.h
DeviceAdapter.h
......
......@@ -89,6 +89,15 @@ class CellSetExplicit : public CellSet
public:
typedef vtkm::Id SchedulingRangeType;
//point to cell is used when iterating cells and asking for point properties
typedef ConnectivityChooser< vtkm::TopologyElementTagPoint,
vtkm::TopologyElementTagCell > PointToCellConnectivityType;
typedef typename PointToCellConnectivityType::ShapeArrayType ShapeArrayType;
typedef typename PointToCellConnectivityType::NumIndicesArrayType NumIndicesArrayType;
typedef typename PointToCellConnectivityType::ConnectivityArrayType ConnectivityArrayType;
typedef typename PointToCellConnectivityType::IndexOffsetArrayType IndexOffsetArrayType;
VTKM_CONT_EXPORT
CellSetExplicit(vtkm::Id numpoints = 0,
const std::string &name = std::string(),
......
//============================================================================
// 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 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 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_cont_CellSetPermutation_h
#define vtk_m_cont_CellSetPermutation_h
#include <vtkm/CellShape.h>
#include <vtkm/CellTraits.h>
#include <vtkm/cont/ArrayHandlePermutation.h>
#include <vtkm/cont/CellSet.h>
#include <vtkm/cont/CellSetExplicit.h>
#include <vtkm/cont/CellSetStructured.h>
#include <vtkm/exec/ConnectivityStructuredPermuted.h>
namespace vtkm {
namespace cont {
namespace internal{
template<typename ValidCellArrayHandleType, typename OriginalCellSet>
struct CellSetPermutationTraits
{
typedef typename OriginalCellSet::ShapeArrayType ShapeArrayType;
typedef typename OriginalCellSet::NumIndicesArrayType NumIndicesArrayType;
typedef typename OriginalCellSet::ConnectivityArrayType ConnectivityArrayType;
typedef typename OriginalCellSet::IndexOffsetArrayType IndexOffsetArrayType;
typedef vtkm::cont::ArrayHandlePermutation<ValidCellArrayHandleType,
ShapeArrayType> PermShapeArrayType;
typedef vtkm::cont::ArrayHandlePermutation<ValidCellArrayHandleType,
NumIndicesArrayType> PermNumIndicesArrayType;
typedef vtkm::cont::ArrayHandlePermutation<ValidCellArrayHandleType,
IndexOffsetArrayType> PermIndexOffsetArrayType;
typedef vtkm::cont::CellSetExplicit<
typename PermShapeArrayType::StorageTag,
typename PermNumIndicesArrayType::StorageTag,
typename ConnectivityArrayType::StorageTag,
typename PermIndexOffsetArrayType::StorageTag> PermutedCellSetType;
};
template<typename ValidCellArrayHandleType, vtkm::IdComponent DIMENSION>
struct CellSetPermutationTraits<ValidCellArrayHandleType, CellSetStructured<DIMENSION> >
{
};
}
template< typename ValidCellArrayHandleType,
typename OriginalCellSet >
class CellSetPermutation : public CellSet
{
public:
typedef typename vtkm::cont::internal::CellSetPermutationTraits<
ValidCellArrayHandleType,OriginalCellSet>::PermutedCellSetType PermutedCellSetType;
typedef typename PermutedCellSetType::ShapeArrayType ShapeArrayType;
typedef typename PermutedCellSetType::NumIndicesArrayType NumIndicesArrayType;
typedef typename PermutedCellSetType::ConnectivityArrayType ConnectivityArrayType;
typedef typename PermutedCellSetType::IndexOffsetArrayType IndexOffsetArrayType;
VTKM_CONT_EXPORT
CellSetPermutation(const ValidCellArrayHandleType& validCellIds,
const OriginalCellSet& cellset,
const std::string &name = std::string(),
vtkm::IdComponent dimensionality = 3)
: CellSet(name,dimensionality),
PermutedCellSet(0, name, dimensionality)
{
this->Fill(validCellIds, cellset);
}
VTKM_CONT_EXPORT
CellSetPermutation(const std::string &name = std::string(),
vtkm::IdComponent dimensionality = 3)
: CellSet(name,dimensionality),
ValidCellIds(),
PermutedCellSet(0, name, dimensionality)
{
}
//This is the way you can fill the memory from another system without copying
VTKM_CONT_EXPORT
void Fill(const ValidCellArrayHandleType &validCellIds,
const OriginalCellSet& cellset)
{
typedef vtkm::cont::internal::CellSetPermutationTraits<
ValidCellArrayHandleType, OriginalCellSet> Traits;
typedef vtkm::TopologyElementTagPoint ElemPointTag;
typedef vtkm::TopologyElementTagCell ElemCellTag;
PermutedCellSetType permutedCellSet(0,this->PermutedCellSet.GetName(),
cellset.GetDimensionality());
typename Traits::PermShapeArrayType shapeArray(validCellIds,
cellset.GetShapesArray(ElemPointTag(),ElemCellTag()));
typename Traits::PermNumIndicesArrayType numArray(validCellIds,
cellset.GetNumIndicesArray(ElemPointTag(),ElemCellTag()));
typename Traits::PermIndexOffsetArrayType offsArray(validCellIds,
cellset.GetIndexOffsetArray(ElemPointTag(),ElemCellTag()));
permutedCellSet.Fill( shapeArray,
numArray,
cellset.GetConnectivityArray(ElemPointTag(),ElemCellTag()),
offsArray);
this->ValidCellIds = validCellIds;
this->PermutedCellSet = permutedCellSet;
}
VTKM_CONT_EXPORT
vtkm::Id GetNumberOfCells() const
{
return this->PermutedCellSet.GetNumberOfCells();
}
VTKM_CONT_EXPORT
vtkm::Id GetSchedulingRange(vtkm::TopologyElementTagCell) const
{
return this->PermutedCellSet.GetNumberOfCells();
}
VTKM_CONT_EXPORT
vtkm::Id GetSchedulingRange(vtkm::TopologyElementTagPoint) const
{
return this->PermutedCellSet.GetNumberOfPoints();
}
template <typename DeviceAdapter, typename FromTopology, typename ToTopology>
struct ExecutionTypes
{
VTKM_IS_DEVICE_ADAPTER_TAG(DeviceAdapter);
VTKM_IS_TOPOLOGY_ELEMENT_TAG(FromTopology);
VTKM_IS_TOPOLOGY_ELEMENT_TAG(ToTopology);
typedef typename PermutedCellSetType::template ExecutionTypes<
DeviceAdapter,
FromTopology,
ToTopology>::ExecObjectType ExecObjectType;
};
template<typename Device, typename FromTopology, typename ToTopology>
typename ExecutionTypes<Device,FromTopology,ToTopology>::ExecObjectType
PrepareForInput(Device d, FromTopology f, ToTopology t) const
{
return this->PermutedCellSet.PrepareForInput(d,f,t);
}
virtual void PrintSummary(std::ostream &out) const
{
out << " CellSetPermutation<ExplicitCellType>: " <<std::endl;
PermutedCellSet.PrintSummary(out);
}
private:
ValidCellArrayHandleType ValidCellIds;
PermutedCellSetType PermutedCellSet;
};
template< typename ValidCellArrayHandleType,vtkm::IdComponent Dimension>
class CellSetPermutation<ValidCellArrayHandleType, CellSetStructured<Dimension> > : public CellSet
{
typedef vtkm::internal::ConnectivityStructuredInternals<Dimension>
InternalsType;
public:
VTKM_CONT_EXPORT
CellSetPermutation(const ValidCellArrayHandleType& validCellIds,
const CellSetStructured<Dimension>& cellset,
const std::string &name = std::string(),
vtkm::IdComponent dimensionality = Dimension)
: CellSet(name,dimensionality),
ValidCellIds(),
FullCellSet()
{
this->Fill(validCellIds, cellset);
}
VTKM_CONT_EXPORT
CellSetPermutation(const std::string &name = std::string(),
vtkm::IdComponent dimensionality = Dimension)
: CellSet(name,dimensionality),
ValidCellIds(),
FullCellSet()
{
}
VTKM_CONT_EXPORT
vtkm::Id GetNumberOfCells() const
{
return this->ValidCellIds.GetNumberOfValues();
}
//This is the way you can fill the memory from another system without copying
VTKM_CONT_EXPORT
void Fill(const ValidCellArrayHandleType &validCellIds,
const CellSetStructured<Dimension>& cellset)
{
ValidCellIds = validCellIds;
FullCellSet = cellset;
}
template<typename TopologyElement>
VTKM_CONT_EXPORT
vtkm::Id GetSchedulingRange(TopologyElement) const {
VTKM_IS_TOPOLOGY_ELEMENT_TAG(TopologyElement);
return this->ValidCellIds.GetNumberOfValues();
}
template<typename Device, typename FromTopology, typename ToTopology>
struct ExecutionTypes {
VTKM_IS_DEVICE_ADAPTER_TAG(Device);
VTKM_IS_TOPOLOGY_ELEMENT_TAG(FromTopology);
VTKM_IS_TOPOLOGY_ELEMENT_TAG(ToTopology);
typedef typename ValidCellArrayHandleType::template ExecutionTypes<Device>::PortalConst ExecPortalType;
typedef vtkm::exec::ConnectivityStructuredPermuted< ExecPortalType,
FromTopology,ToTopology,Dimension> ExecObjectType;
};
template<typename Device, typename FromTopology, typename ToTopology>
typename ExecutionTypes<Device,FromTopology,ToTopology>::ExecObjectType
PrepareForInput(Device d, FromTopology f, ToTopology t) const
{
typedef typename
ExecutionTypes<Device,FromTopology,ToTopology>::ExecObjectType
ConnectivityType;
return ConnectivityType(this->ValidCellIds.PrepareForInput(d),
this->FullCellSet.PrepareForInput(d,f,t) );
}
virtual void PrintSummary(std::ostream &out) const
{
out << " CellSetPermutation<StructuredCellType<"<<Dimension<<"> >: " <<std::endl;
}
private:
std::string Name;
vtkm::Id Dimensionality;
ValidCellArrayHandleType ValidCellIds;
CellSetStructured<Dimension> FullCellSet;
};
}
} // namespace vtkm::cont
#endif //vtk_m_cont_CellSetPermutation_h
......@@ -45,6 +45,7 @@ set(unit_tests
UnitTestDataSetRegular.cxx
UnitTestDataSetExplicit.cxx
UnitTestDataSetSingleType.cxx
UnitTestDataSetPermutation.cxx
UnitTestDeviceAdapterAlgorithmDependency.cxx
UnitTestDeviceAdapterAlgorithmGeneral.cxx
UnitTestDeviceAdapterSerial.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 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 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/cont/DataSet.h>
#include <vtkm/cont/CellSetSingleType.h>
#include <vtkm/cont/CellSetPermutation.h>
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/cont/testing/MakeTestDataSet.h>
#include <vtkm/worklet/CellAverage.h>
#include <vtkm/worklet/DispatcherMapTopology.h>
namespace {
template<typename T, typename Storage>
bool TestArrayHandle(const vtkm::cont::ArrayHandle<T, Storage> &ah, const T *expected,
vtkm::Id size)
{
if (size != ah.GetNumberOfValues())
{
return false;
}
for (vtkm::Id i = 0; i < size; ++i)
{
if (ah.GetPortalConstControl().Get(i) != expected[i])
{
return false;
}
}
return true;
}
inline vtkm::cont::DataSet make_SingleTypeDataSet()
{
using vtkm::cont::Field;
vtkm::cont::DataSet dataSet;
const int nVerts = 5;
typedef vtkm::Vec<vtkm::Float32,3> CoordType;
CoordType coordinates[nVerts] = {
CoordType(0, 0, 0),
CoordType(1, 0, 0),
CoordType(1, 1, 0),
CoordType(2, 1, 0),
CoordType(2, 2, 0)
};
//Set coordinate system
dataSet.AddCoordinateSystem(
vtkm::cont::CoordinateSystem("coordinates", 1, coordinates, nVerts));
//Set point scalar
vtkm::Float32 vars[nVerts] = {10.1f, 20.1f, 30.2f, 40.2f, 50.3f};
dataSet.AddField(Field("pointvar", 1, vtkm::cont::Field::ASSOC_POINTS, vars, nVerts));
std::vector<vtkm::Id> conn;
// First Cell
conn.push_back(0);
conn.push_back(1);
conn.push_back(2);
// Second Cell
conn.push_back(1);
conn.push_back(2);
conn.push_back(3);
// Third Cell
conn.push_back(2);
conn.push_back(3);
conn.push_back(4);
vtkm::cont::CellSetSingleType<> cellSet(vtkm::CellShapeTagTriangle(),
"cells");
cellSet.FillViaCopy(conn);
dataSet.AddCellSet(cellSet);
return dataSet;
}
void TestDataSet_Explicit()
{
vtkm::cont::DataSet dataSet = make_SingleTypeDataSet();
std::vector<vtkm::Id> validIds;
validIds.push_back(1); //iterate the 2nd cell 4 times
validIds.push_back(1);
validIds.push_back(1);
validIds.push_back(1);
vtkm::cont::ArrayHandle<vtkm::Id> validCellIds =
vtkm::cont::make_ArrayHandle(validIds);
//get the cellset single type from the dataset
typedef vtkm::cont::CellSetSingleType<> CellSetType;
CellSetType cellSet = dataSet.GetCellSet(0).CastTo<CellSetType>();
//verify that we can create a subset of a singlset
typedef vtkm::cont::CellSetPermutation<vtkm::cont::ArrayHandle<vtkm::Id>,
vtkm::cont::CellSetSingleType<> > SubsetType;
SubsetType subset;
subset.Fill(validCellIds,cellSet);
subset.PrintSummary(std::cout);
typedef SubsetType::ExecutionTypes<vtkm::cont::DeviceAdapterTagSerial,
vtkm::TopologyElementTagPoint,
vtkm::TopologyElementTagCell>::ExecObjectType ExecObjectType;
ExecObjectType execConnectivity;
execConnectivity = subset.PrepareForInput(vtkm::cont::DeviceAdapterTagSerial(),
vtkm::TopologyElementTagPoint(),
vtkm::TopologyElementTagCell());
//run a basic for-each topology algorithm on this
vtkm::cont::ArrayHandle<vtkm::Float32> result;
vtkm::worklet::DispatcherMapTopology<vtkm::worklet::CellAverage> dispatcher;
dispatcher.Invoke(dataSet.GetField("pointvar").GetData(),
subset,
result);
//iterate same cell 4 times
vtkm::Float32 expected[4] = { 30.1667f, 30.1667f, 30.1667f, 30.1667f };
for (int i = 0; i < 4; ++i)
{
VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(i),
expected[i]), "Wrong result for CellAverage worklet on explicit subset data");
}
}
void TestDataSet_Structured2D()
{
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet = testDataSet.Make2DRegularDataSet0();
std::vector<vtkm::Id> validIds;
validIds.push_back(1); //iterate the 2nd cell 4 times
validIds.push_back(1);
validIds.push_back(1);
validIds.push_back(1);
vtkm::cont::ArrayHandle<vtkm::Id> validCellIds =
vtkm::cont::make_ArrayHandle(validIds);
typedef vtkm::cont::CellSetStructured<2> CellSetType;
CellSetType cellSet = dataSet.GetCellSet(0).CastTo<CellSetType>();
//verify that we can create a subset of a 2d RegularDataSet
vtkm::cont::CellSetPermutation<vtkm::cont::ArrayHandle<vtkm::Id>,
vtkm::cont::CellSetStructured<2> > subset;
subset.Fill(validCellIds,cellSet);
subset.PrintSummary(std::cout);
//verify that we can call PrepareForInput on CellSetSingleType
typedef vtkm::cont::DeviceAdapterTagSerial DeviceAdapterTag;
//verify that PrepareForInput exists
subset.PrepareForInput(DeviceAdapterTag(),
vtkm::TopologyElementTagPoint(),
vtkm::TopologyElementTagCell());
//run a basic for-each topology algorithm on this
vtkm::cont::ArrayHandle<vtkm::Float32> result;
vtkm::worklet::DispatcherMapTopology<vtkm::worklet::CellAverage> dispatcher;
dispatcher.Invoke(dataSet.GetField("pointvar").GetData(),
subset,
result);
vtkm::Float32 expected[4] = { 40.1f, 40.1f, 40.1f, 40.1f };
for (int i = 0; i < 4; ++i)
{
VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(i),
expected[i]), "Wrong result for CellAverage worklet on 2d structured subset data");
}
}
void TestDataSet_Structured3D()
{
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet = testDataSet.Make3DRegularDataSet0();
std::vector<vtkm::Id> validIds;
validIds.push_back(1); //iterate the 2nd cell 4 times
validIds.push_back(1);
validIds.push_back(1);
validIds.push_back(1);
vtkm::cont::ArrayHandle<vtkm::Id> validCellIds =
vtkm::cont::make_ArrayHandle(validIds);
typedef vtkm::cont::CellSetStructured<3> CellSetType;
CellSetType cellSet = dataSet.GetCellSet(0).CastTo<CellSetType>();
//verify that we can create a subset of a 2d RegularDataSet
vtkm::cont::CellSetPermutation<vtkm::cont::ArrayHandle<vtkm::Id>,
vtkm::cont::CellSetStructured<3> > subset;
subset.Fill(validCellIds,cellSet);
subset.PrintSummary(std::cout);
//verify that PrepareForInput exists
subset.PrepareForInput(
vtkm::cont::DeviceAdapterTagSerial(),
vtkm::TopologyElementTagPoint(),
vtkm::TopologyElementTagCell());
//run a basic for-each topology algorithm on this
vtkm::cont::ArrayHandle<vtkm::Float32> result;
vtkm::worklet::DispatcherMapTopology<vtkm::worklet::CellAverage> dispatcher;
dispatcher.Invoke(dataSet.GetField("pointvar").GetData(),
subset,
result);
vtkm::Float32 expected[4] = { 70.2125f, 70.2125f, 70.2125f, 70.2125f };
for (int i = 0; i < 4; ++i)
{
VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(i),
expected[i]), "Wrong result for CellAverage worklet on 2d structured subset data");
}
}
void TestDataSet_Permutation()
{
std::cout << std::endl;
std::cout << "--TestDataSet_Permutation--" << std::endl << std::endl;
TestDataSet_Explicit();
TestDataSet_Structured2D();
TestDataSet_Structured3D();
}
}
int UnitTestDataSetPermutation(int, char *[])
{
return vtkm::cont::testing::Testing::Run(TestDataSet_Permutation);
}
......@@ -24,11 +24,13 @@ set(headers
CellInterpolate.h
ConnectivityExplicit.h
ConnectivityStructured.h
ConnectivityStructuredPermuted.h
ExecutionObjectBase.h
ExecutionWholeArray.h
FunctorBase.h
NewtonsMethod.h
ParametricCoordinates.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 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 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_exec_ConnectivityStructuredPermuted_h
#define vtk_m_exec_ConnectivityStructuredPermuted_h
#include <vtkm/TopologyElementTag.h>
#include <vtkm/Types.h>
#include <vtkm/exec/ConnectivityStructured.h>
namespace vtkm {
namespace exec {
template<typename PermutationPortal,
typename FromTopology,
typename ToTopology,
vtkm::IdComponent Dimension>
class ConnectivityStructuredPermuted
{
VTKM_IS_TOPOLOGY_ELEMENT_TAG(FromTopology);
VTKM_IS_TOPOLOGY_ELEMENT_TAG(ToTopology);
typedef vtkm::exec::ConnectivityStructured<FromTopology,
ToTopology,
Dimension> StructuredType;
public:
typedef vtkm::Id SchedulingRangeType;
VTKM_EXEC_CONT_EXPORT
ConnectivityStructuredPermuted():
Portal(),
FullStructuredGrid()
{
}
VTKM_EXEC_CONT_EXPORT
ConnectivityStructuredPermuted(const PermutationPortal& portal,
const StructuredType &src):
Portal(portal),
FullStructuredGrid(src)
{
}
VTKM_EXEC_CONT_EXPORT
ConnectivityStructuredPermuted(const ConnectivityStructuredPermuted &src):
Portal(src.Portal),
FullStructuredGrid(src.FullStructuredGrid)
{
}
VTKM_EXEC_EXPORT
vtkm::IdComponent GetNumberOfIndices(vtkm::Id index) const {
return this->FullStructuredGrid.GetNumberOfIndices( this->Portal.Get(index) );
}
// This needs some thought. What does cell shape mean when the to topology
// is not a cell?
typedef typename StructuredType::CellShapeTag CellShapeTag;
VTKM_EXEC_EXPORT
CellShapeTag GetCellShape(vtkm::Id=0) const {
return CellShapeTag();
}
typedef typename StructuredType::IndicesType IndicesType;
VTKM_EXEC_EXPORT
IndicesType GetIndices(vtkm::Id index) const
{
return this->FullStructuredGrid.GetIndices( this->Portal.Get(index) );
}
private:
PermutationPortal Portal;
StructuredType FullStructuredGrid;
};
}
} // namespace vtkm::exec
#endif //vtk_m_exec_ConnectivityStructuredPermuted_h
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