Add UncertainCellSet

This is a replacement for DynamicCellSet that requires less templating.
This commit is contained in:
Kenneth Moreland 2021-12-13 14:04:43 -07:00
parent 557055b9cc
commit ac024587a6
8 changed files with 822 additions and 1 deletions

@ -0,0 +1,21 @@
# UnknownCellSet
The `DynamicCellSet` class has been replaced with `UnknownCellSet`.
Likewise, the `DynamicCellSetBase` class (a templated version of
`DynamicCellSet`) has been replaced with `UncertainCellSet`.
These changes principally follow the changes to the `UnknownArrayHandle`
management class. The `ArrayHandle` version of a polymorphic manager has
gone through several refinements from `DynamicArrayHandle` to
`VariantArrayHandle` to its current form as `UnknownArrayHandle`.
Throughout these improvements for `ArrayHandle`, the equivalent classes for
`CellSet` have lagged behind. The `CellSet` version is decidedly simpler
because `CellSet` itself is polymorphic, but there were definitely
improvements to be had.
The biggest improvement was to remove the templating from the basic unknown
cell set. The old `DynamicArrayHandle` was actually a type alias for
`DynamicArrayHandleBase<VTKM_DEFAULT_CELL_SET_LIST>`. As
`VTKM_DEFAULT_CELL_SET_LIST` tends to be pretty long, `DynamicArrayHandle`
was actually a really long type. In contrast, `UnknownArrayHandle` is its
own untemplated class and will show up in linker symbols as such.

@ -122,7 +122,9 @@ set(headers
TryExecute.h
SerializableTypeString.h
UncertainArrayHandle.h
UncertainCellSet.h
UnknownArrayHandle.h
UnknownCellSet.h
VariantArrayHandle.h
)
@ -157,6 +159,7 @@ set(sources
Storage.cxx
Token.cxx
TryExecute.cxx
UnknownCellSet.cxx
)
# This list of sources has code that uses devices and so might need to be

@ -0,0 +1,206 @@
//============================================================================
// 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.
//============================================================================
#ifndef vtk_m_cont_UncertainCellSet_h
#define vtk_m_cont_UncertainCellSet_h
#include <vtkm/cont/UnknownCellSet.h>
namespace vtkm
{
namespace cont
{
/// \brief A `CellSet` of an uncertain type.
///
/// `UncertainCellSet` holds a `CellSet` object using runtime polymorphism to
/// manage different types. It behaves like its superclass, `UnknownCellSet`,
/// except that it also contains a template parameter that provides a
/// `vtkm::List` of potential cell set types.
///
/// These potental types come into play when the `CastAndCall` method is called
/// (or the `UncertainCellSet` is used in the `vtkm::cont::CastAndCall` function).
/// In this case, the `CastAndCall` will search for `CellSet`s of types that match
/// this list.
///
/// Both `UncertainCellSet` and `UnknownCellSet` have a method named
/// `ResetCellSetList` that redefines the list of potential cell sets by returning
/// a new `UncertainCellSet` containing the same `CellSet` but with the new cell
/// set type list.
///
template <typename CellSetList>
class VTKM_ALWAYS_EXPORT UncertainCellSet : public vtkm::cont::UnknownCellSet
{
VTKM_IS_LIST(CellSetList);
VTKM_STATIC_ASSERT_MSG((!std::is_same<CellSetList, vtkm::ListUniversal>::value),
"Cannot use vtkm::ListUniversal with UncertainCellSet.");
using Superclass = UnknownCellSet;
using Thisclass = UncertainCellSet<CellSetList>;
public:
VTKM_CONT UncertainCellSet() = default;
template <typename CellSetType>
explicit VTKM_CONT UncertainCellSet(const CellSetType& cellSet)
: Superclass(cellSet)
{
}
explicit VTKM_CONT UncertainCellSet(const vtkm::cont::UnknownCellSet& src)
: Superclass(src)
{
}
UncertainCellSet(const Thisclass&) = default;
template <typename OtherCellSetList>
explicit VTKM_CONT UncertainCellSet(const UncertainCellSet<OtherCellSetList>& src)
: Superclass(src)
{
}
/// \brief Create a new cell set of the same type as this.
///
/// This method creates a new array that is the same type as this one and
/// returns a new `UncertainCellSet` for it.
///
VTKM_CONT Thisclass NewInstance() const { return Thisclass(this->Superclass::NewInstance()); }
/// \brief Call a functor using the underlying array type.
///
/// `CastAndCall` attempts to cast the held array to a specific value type,
/// and then calls the given functor with the cast array.
///
template <typename Functor, typename... Args>
VTKM_CONT void CastAndCall(Functor&& functor, Args&&... args) const
{
this->CastAndCallForTypes<CellSetList>(std::forward<Functor>(functor),
std::forward<Args>(args)...);
}
};
// Defined here to avoid circular dependencies between UnknownArrayHandle and UncertainArrayHandle.
template <typename NewCellSetList>
VTKM_CONT vtkm::cont::UncertainCellSet<NewCellSetList> UnknownCellSet::ResetCellSetList(
NewCellSetList) const
{
return vtkm::cont::UncertainCellSet<NewCellSetList>(*this);
}
template <typename NewCellSetList>
VTKM_CONT vtkm::cont::UncertainCellSet<NewCellSetList> UnknownCellSet::ResetCellSetList() const
{
return vtkm::cont::UncertainCellSet<NewCellSetList>(*this);
}
namespace internal
{
template <typename CellSetList>
struct DynamicTransformTraits<vtkm::cont::UncertainCellSet<CellSetList>>
{
using DynamicTag = vtkm::cont::internal::DynamicTransformTagCastAndCall;
};
} // namespace internal
} // namespace vtkm::cont
} // namespace vtkm
//=============================================================================
// Specializations of serialization related classes
/// @cond SERIALIZATION
namespace vtkm
{
namespace cont
{
template <typename CellSetList>
struct SerializableTypeString<vtkm::cont::UncertainCellSet<CellSetList>>
{
static VTKM_CONT std::string Get()
{
return SerializableTypeString<vtkm::cont::UnknownCellSet>::Get();
}
};
}
} // namespace vtkm::cont
namespace mangled_diy_namespace
{
namespace internal
{
struct UncertainCellSetSerializeFunctor
{
template <typename CellSetType>
void operator()(const CellSetType& cs, BinaryBuffer& bb) const
{
vtkmdiy::save(bb, vtkm::cont::SerializableTypeString<CellSetType>::Get());
vtkmdiy::save(bb, cs);
}
};
struct UncertainCellSetDeserializeFunctor
{
template <typename CellSetType>
void operator()(CellSetType,
vtkm::cont::UnknownCellSet& unknownArray,
const std::string& typeString,
bool& success,
BinaryBuffer& bb) const
{
if (!success && (typeString == vtkm::cont::SerializableTypeString<CellSetType>::Get()))
{
CellSetType knownArray;
vtkmdiy::load(bb, knownArray);
unknownArray = knownArray;
success = true;
}
}
};
} // internal
template <typename CellSetList>
struct Serialization<vtkm::cont::UncertainCellSet<CellSetList>>
{
using Type = vtkm::cont::UncertainCellSet<CellSetList>;
public:
static VTKM_CONT void save(BinaryBuffer& bb, const Type& obj)
{
obj.CastAndCall(internal::UncertainCellSetSerializeFunctor{}, bb);
}
static VTKM_CONT void load(BinaryBuffer& bb, Type& obj)
{
std::string typeString;
vtkmdiy::load(bb, typeString);
bool success = false;
vtkm::ListForEach(
internal::UncertainCellSetDeserializeFunctor{}, CellSetList{}, obj, typeString, success, bb);
if (!success)
{
throw vtkm::cont::ErrorBadType(
"Error deserializing Unknown/UncertainCellSet. Message TypeString: " + typeString);
}
}
};
} // namespace mangled_diy_namespace
/// @endcond SERIALIZATION
#endif //vtk_m_cont_UncertainCellSet_h

@ -600,7 +600,7 @@ public:
///@{
/// Returns this array cast appropriately and stored in the given `ArrayHandle` type.
/// Throws an `ErrorBadType` if the stored array cannot be stored in the given array type.
/// Use the `IsType` method to determine if the array can be returned with the given type.
/// Use the `CanConvert` method to determine if the array can be returned with the given type.
///
template <typename T, typename S>
VTKM_CONT void AsArrayHandle(vtkm::cont::ArrayHandle<T, S>& array) const

@ -0,0 +1,114 @@
//============================================================================
// 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/UnknownCellSet.h>
#include <vtkm/cont/UncertainCellSet.h>
#include <sstream>
namespace
{
// Could potentially precompile more cell sets to serialze if that is useful.
using UnknownSerializationCellSets = VTKM_DEFAULT_CELL_SET_LIST;
}
namespace vtkm
{
namespace cont
{
vtkm::cont::UnknownCellSet UnknownCellSet::NewInstance() const
{
UnknownCellSet newCellSet;
if (this->Container)
{
newCellSet.Container = this->Container->NewInstance();
}
return newCellSet;
}
std::string UnknownCellSet::GetCellSetName() const
{
if (this->Container)
{
return vtkm::cont::TypeToString(typeid(this->Container.get()));
}
else
{
return "";
}
}
void UnknownCellSet::PrintSummary(std::ostream& os) const
{
if (this->Container)
{
this->Container->PrintSummary(os);
}
else
{
os << " UnknownCellSet = nullptr\n";
}
}
namespace internal
{
void ThrowCastAndCallException(const vtkm::cont::UnknownCellSet& ref, const std::type_info& type)
{
std::ostringstream out;
out << "Could not find appropriate cast for array in CastAndCall.\n"
"CellSet: ";
ref.PrintSummary(out);
out << "TypeList: " << vtkm::cont::TypeToString(type) << "\n";
throw vtkm::cont::ErrorBadType(out.str());
}
} // namespace internal
} // namespace vtkm::cont
} // namespace vtkm
//=============================================================================
// Specializations of serialization related classes
namespace vtkm
{
namespace cont
{
std::string SerializableTypeString<vtkm::cont::UnknownCellSet>::Get()
{
return "UnknownCS";
}
}
} // namespace vtkm::cont
namespace mangled_diy_namespace
{
void Serialization<vtkm::cont::UnknownCellSet>::save(BinaryBuffer& bb,
const vtkm::cont::UnknownCellSet& obj)
{
vtkmdiy::save(bb, obj.ResetCellSetList<UnknownSerializationCellSets>());
}
void Serialization<vtkm::cont::UnknownCellSet>::load(BinaryBuffer& bb,
vtkm::cont::UnknownCellSet& obj)
{
vtkm::cont::UncertainCellSet<UnknownSerializationCellSets> uncertainCellSet;
vtkmdiy::load(bb, uncertainCellSet);
obj = uncertainCellSet;
}
} // namespace mangled_diy_namespace

301
vtkm/cont/UnknownCellSet.h Normal file

@ -0,0 +1,301 @@
//============================================================================
// 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.
//============================================================================
#ifndef vtk_m_cont_UnknownCellSet_h
#define vtk_m_cont_UnknownCellSet_h
#include <vtkm/cont/CastAndCall.h>
#include <vtkm/cont/CellSet.h>
#include <vtkm/cont/DefaultTypes.h>
#include <vtkm/cont/vtkm_cont_export.h>
#include <memory>
namespace vtkm
{
namespace cont
{
// Forward declaration. Include UncertainCellSet.h if using this.
template <typename CellSetList>
class UncertainCellSet;
/// \brief A CellSet of an unknown type.
///
/// `UnknownCellSet` holds a `CellSet` object using runtime polymorphism to manage
/// the dynamic type rather than compile-time templates. This adds a programming
/// convenience that helps avoid a proliferation of templates.
///
/// To interface between the runtime polymorphism and the templated algorithms
/// in VTK-m, `UnknownCellSet` contains a method named `CastAndCallForTypes` that
/// determines the correct type from some known list of types. This mechanism is
/// used internally by VTK-m's worklet invocation mechanism to determine the type
/// when running algorithms.
///
/// If the `UnknownCellSet` is used in a context where the possible cell set types
/// can be whittled down to a finite list, you can specify lists of cell set types
/// using the `ResetCellSetList` method. This will convert this object to an
/// `UncertainCellSet` of the given types. In cases where a finite set of types
/// are needed but there is no subset, `VTKM_DEFAULT_CELL_SET_LIST`
///
class VTKM_CONT_EXPORT UnknownCellSet
{
std::shared_ptr<vtkm::cont::CellSet> Container;
public:
VTKM_CONT UnknownCellSet() = default;
UnknownCellSet(const UnknownCellSet&) = default;
template <typename CellSetType>
VTKM_CONT UnknownCellSet(const CellSetType& cellSet)
{
VTKM_IS_CELL_SET(CellSetType);
this->Container = std::shared_ptr<vtkm::cont::CellSet>(new CellSetType(cellSet));
}
/// \brief Returns whether a cell set is stored in this `UnknownCellSet`.
///
/// If the `UnknownCellSet` is constructed without a `CellSet`, it will not
/// have an underlying type, and therefore the operations will be invalid.
///
VTKM_CONT bool IsValid() const { return static_cast<bool>(this->Container); }
/// \brief Returns a pointer to the `CellSet` base class.
///
VTKM_CONT vtkm::cont::CellSet* GetCellSetBase() { return this->Container.get(); }
VTKM_CONT const vtkm::cont::CellSet* GetCellSetBase() const { return this->Container.get(); }
/// \brief Create a new cell set of the same type as this cell set.
///
/// This method creates a new cell set that is the same type as this one
/// and returns a new `UnknownCellSet` for it. This method is convenient
/// when creating output cell sets that should be the same type as the
/// input cell set.
///
VTKM_CONT UnknownCellSet NewInstance() const;
/// \brief Returns the name of the cell set type stored in the array.
///
/// Returns an empty string if no cell set is stored.
///
VTKM_CONT std::string GetCellSetName() const;
/// \brief Returns true if this cell set matches the `CellSetType` template argument.
///
template <typename CellSetType>
VTKM_CONT bool IsType() const
{
return (dynamic_cast<const CellSetType*>(this->Container.get()) != nullptr);
}
VTKM_CONT vtkm::Id GetNumberOfCells() const
{
return this->Container ? this->Container->GetNumberOfCells() : 0;
}
VTKM_CONT vtkm::Id GetNumberOfFaces() const
{
return this->Container ? this->Container->GetNumberOfFaces() : 0;
}
VTKM_CONT vtkm::Id GetNumberOfEdges() const
{
return this->Container ? this->Container->GetNumberOfEdges() : 0;
}
VTKM_CONT vtkm::Id GetNumberOfPoints() const
{
return this->Container ? this->Container->GetNumberOfPoints() : 0;
}
VTKM_CONT vtkm::UInt8 GetCellShape(vtkm::Id id) const
{
return this->GetCellSetBase()->GetCellShape(id);
}
VTKM_CONT vtkm::IdComponent GetNumberOfPointsInCell(vtkm::Id id) const
{
return this->GetCellSetBase()->GetNumberOfPointsInCell(id);
}
VTKM_CONT void GetCellPointIds(vtkm::Id id, vtkm::Id* ptids) const
{
return this->GetCellSetBase()->GetCellPointIds(id, ptids);
}
VTKM_CONT void DeepCopyFrom(const CellSet* src) { this->GetCellSetBase()->DeepCopy(src); }
VTKM_CONT void PrintSummary(std::ostream& os) const;
VTKM_CONT void ReleaseResourcesExecution()
{
if (this->Container)
{
this->Container->ReleaseResourcesExecution();
}
}
/// \brief Returns true if this cell set can be retrieved as the given type.
///
/// This method will return true if calling `AsCellSet` of the given type will
/// succeed. This result is similar to `IsType`, and if `IsType` returns true,
/// then this will return true. However, this method will also return true for
/// other types where automatic conversions are made.
///
template <typename CellSetType>
VTKM_CONT bool CanConvert() const
{
// TODO: Currently, these are the same. But in the future we expect to support
// special CellSet types that can convert back and forth such as multiplexed
// cell sets or a cell set that can hold structured grids of any dimension.
return this->IsType<CellSetType>();
}
///@{
/// \brief Get the cell set as a known type.
///
/// Returns this cell set cast appropriately and stored in the given `CellSet`
/// type. Throws an `ErrorBadType` if the stored cell set cannot be stored in
/// the given cell set type. Use the `CanConvert` method to determine if the
/// cell set can be returned with the given type.
///
template <typename CellSetType>
VTKM_CONT void AsCellSet(CellSetType& cellSet) const
{
VTKM_IS_CELL_SET(CellSetType);
if (!this->IsType<CellSetType>())
{
VTKM_LOG_CAST_FAIL(*this, decltype(cellSet));
throwFailedDynamicCast(this->GetCellSetName(), vtkm::cont::TypeToString(cellSet));
}
cellSet = *reinterpret_cast<CellSetType*>(this->Container.get());
}
template <typename CellSetType>
VTKM_CONT CellSetType AsCellSet() const
{
CellSetType cellSet;
this->AsCellSet(cellSet);
return cellSet;
}
///@}
/// \brief Assigns potential cell set types.
///
/// Calling this method will return an `UncertainCellSet` with the provided
/// cell set list. The returned object will hold the same `CellSet`, but
/// `CastAndCall`'s on the returned object will be constrained to the given
/// types.
///
// Defined in UncertainCellSet.h
template <typename CellSetList>
VTKM_CONT vtkm::cont::UncertainCellSet<CellSetList> ResetCellSetList(CellSetList) const;
template <typename CellSetList>
VTKM_CONT vtkm::cont::UncertainCellSet<CellSetList> ResetCellSetList() const;
/// \brief Call a functor using the underlying cell set type.
///
/// `CastAndCallForTypes` attemts to cast the held cell set to a specific type
/// and then calls the given functor with the cast cell set. You must specify
/// the `CellSetList` (in a `vtkm::List`) as a template argument.
///
/// After the functor argument, you may add any number of arguments that will
/// be passed to the functor after the converted cell set.
///
template <typename CellSetList, typename Functor, typename... Args>
VTKM_CONT void CastAndCallForTypes(Functor&& functor, Args&&... args) const;
};
namespace internal
{
VTKM_CONT_EXPORT void ThrowCastAndCallException(const vtkm::cont::UnknownCellSet&,
const std::type_info&);
template <>
struct DynamicTransformTraits<vtkm::cont::UnknownCellSet>
{
using DynamicTag = vtkm::cont::internal::DynamicTransformTagCastAndCall;
};
} // namespace internal
template <typename CellSetList, typename Functor, typename... Args>
VTKM_CONT void UnknownCellSet::CastAndCallForTypes(Functor&& functor, Args&&... args) const
{
VTKM_IS_LIST(CellSetList);
bool called = false;
vtkm::ListForEach(
[&](auto cellSet) {
if (!called && this->CanConvert<decltype(cellSet)>())
{
called = true;
this->AsCellSet(cellSet);
VTKM_LOG_CAST_SUCC(*this, cellSet);
// If you get a compile error here, it means that you have called CastAndCall for a
// vtkm::cont::UnknownCellSet and the arguments of the functor do not match those
// being passed. This is often because it is calling the functor with a CellSet
// type that was not expected. Either add overloads to the functor to accept all
// possible array types or constrain the types tried for the CastAndCall.
functor(cellSet, args...);
}
},
CellSetList{});
if (!called)
{
VTKM_LOG_CAST_FAIL(*this, CellSetList);
internal::ThrowCastAndCallException(*this, typeid(CellSetList));
}
}
/// A specialization of `CastAndCall` for `UnknownCellSet`.
/// Since we have no hints on the types, use `VTKM_DEFAULT_CELL_SET_LIST`.
template <typename Functor, typename... Args>
void CastAndCall(const vtkm::cont::UnknownCellSet& cellSet, Functor&& f, Args&&... args)
{
cellSet.CastAndCallForTypes<VTKM_DEFAULT_CELL_SET_LIST>(std::forward<Functor>(f),
std::forward<Args>(args)...);
}
} // namespace vtkm::cont
} // namespace vtkm
//=============================================================================
// Specializations of serialization related classes
/// @cond SERIALIZATION
namespace vtkm
{
namespace cont
{
template <>
struct VTKM_CONT_EXPORT SerializableTypeString<vtkm::cont::UnknownCellSet>
{
static VTKM_CONT std::string Get();
};
}
} // namespace vtkm::cont
namespace mangled_diy_namespace
{
template <>
struct VTKM_CONT_EXPORT Serialization<vtkm::cont::UnknownCellSet>
{
public:
static VTKM_CONT void save(BinaryBuffer& bb, const vtkm::cont::UnknownCellSet& obj);
static VTKM_CONT void load(BinaryBuffer& bb, vtkm::cont::UnknownCellSet& obj);
};
} // namespace mangled_diy_namespace
/// @endcond SERIALIZATION
#endif //vtk_m_cont_UnknownCellSet_h

@ -94,6 +94,7 @@ set(unit_tests
UnitTestToken.cxx
UnitTestTryExecute.cxx
UnitTestUnknownArrayHandle.cxx
UnitTestUnknownCellSet.cxx
UnitTestVariantArrayHandle.cxx
)

@ -0,0 +1,175 @@
//============================================================================
// 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/UncertainCellSet.h>
#include <vtkm/cont/ArrayHandleConstant.h>
#include <vtkm/cont/testing/Testing.h>
namespace
{
using NonDefaultCellSetList =
vtkm::List<vtkm::cont::CellSetStructured<1>,
vtkm::cont::CellSetExplicit<vtkm::cont::ArrayHandleConstant<vtkm::UInt8>::StorageTag>>;
template <typename ExpectedCellType>
struct CheckFunctor
{
void operator()(const ExpectedCellType&, bool& called) const { called = true; }
template <typename UnexpectedType>
void operator()(const UnexpectedType&, bool& called) const
{
VTKM_TEST_FAIL("CastAndCall functor called with wrong type.");
called = false;
}
};
class DummyCellSet : public vtkm::cont::CellSet
{
};
void CheckEmptyUnknownCellSet()
{
vtkm::cont::UnknownCellSet empty;
VTKM_TEST_ASSERT(empty.GetNumberOfCells() == 0, "UnknownCellSet should have no cells");
VTKM_TEST_ASSERT(empty.GetNumberOfFaces() == 0, "UnknownCellSet should have no faces");
VTKM_TEST_ASSERT(empty.GetNumberOfEdges() == 0, "UnknownCellSet should have no edges");
VTKM_TEST_ASSERT(empty.GetNumberOfPoints() == 0, "UnknownCellSet should have no points");
empty.PrintSummary(std::cout);
using CellSet2D = vtkm::cont::CellSetStructured<2>;
using CellSet3D = vtkm::cont::CellSetStructured<3>;
VTKM_TEST_ASSERT(!empty.IsType<CellSet2D>(), "UnknownCellSet reports wrong type.");
VTKM_TEST_ASSERT(!empty.IsType<CellSet3D>(), "UnknownCellSet reports wrong type.");
VTKM_TEST_ASSERT(!empty.IsType<DummyCellSet>(), "UnknownCellSet reports wrong type.");
VTKM_TEST_ASSERT(!empty.CanConvert<CellSet2D>(), "UnknownCellSet reports wrong type.");
VTKM_TEST_ASSERT(!empty.CanConvert<CellSet3D>(), "UnknownCellSet reports wrong type.");
VTKM_TEST_ASSERT(!empty.CanConvert<DummyCellSet>(), "UnknownCellSet reports wrong type.");
bool gotException = false;
try
{
CellSet2D instance = empty.AsCellSet<CellSet2D>();
}
catch (vtkm::cont::ErrorBadType&)
{
gotException = true;
}
VTKM_TEST_ASSERT(gotException, "Empty UnknownCellSet should have thrown on casting");
auto empty2 = empty.NewInstance();
VTKM_TEST_ASSERT(empty.GetCellSetBase() == nullptr, "UnknownCellSet should contain a nullptr");
VTKM_TEST_ASSERT(empty2.GetCellSetBase() == nullptr, "UnknownCellSet should contain a nullptr");
}
template <typename CellSetType, typename CellSetList>
void CheckUnknownCellSet(vtkm::cont::UnknownCellSet unknownCellSet)
{
VTKM_TEST_ASSERT(unknownCellSet.CanConvert<CellSetType>());
VTKM_TEST_ASSERT(!unknownCellSet.CanConvert<DummyCellSet>());
unknownCellSet.AsCellSet<CellSetType>();
bool called = false;
unknownCellSet.CastAndCallForTypes<CellSetList>(CheckFunctor<CellSetType>(), called);
VTKM_TEST_ASSERT(
called, "The functor was never called (and apparently a bad value exception not thrown).");
if (vtkm::ListHas<CellSetList, VTKM_DEFAULT_CELL_SET_LIST>::value)
{
called = false;
CastAndCall(unknownCellSet, CheckFunctor<CellSetType>(), called);
VTKM_TEST_ASSERT(
called, "The functor was never called (and apparently a bad value exception not thrown).");
}
vtkm::cont::UncertainCellSet<CellSetList> uncertainCellSet(unknownCellSet);
called = false;
uncertainCellSet.CastAndCall(CheckFunctor<CellSetType>(), called);
VTKM_TEST_ASSERT(
called, "The functor was never called (and apparently a bad value exception not thrown).");
called = false;
CastAndCall(uncertainCellSet, CheckFunctor<CellSetType>(), called);
VTKM_TEST_ASSERT(
called, "The functor was never called (and apparently a bad value exception not thrown).");
}
template <typename CellSetType>
void TryNewInstance(vtkm::cont::UnknownCellSet& originalCellSet)
{
vtkm::cont::UnknownCellSet newCellSet = originalCellSet.NewInstance();
VTKM_TEST_ASSERT(newCellSet.IsType<CellSetType>(), "New cell set wrong type.");
VTKM_TEST_ASSERT(originalCellSet.GetCellSetBase() != newCellSet.GetCellSetBase(),
"NewInstance did not make a copy.");
}
template <typename CellSetType, typename CellSetList>
void TryCellSet(vtkm::cont::UnknownCellSet& unknownCellSet)
{
CheckUnknownCellSet<CellSetType, CellSetList>(unknownCellSet);
CheckUnknownCellSet<CellSetType, vtkm::List<CellSetType>>(unknownCellSet);
TryNewInstance<CellSetType>(unknownCellSet);
}
template <typename CellSetType>
void TryDefaultCellSet(CellSetType cellSet)
{
vtkm::cont::UnknownCellSet unknownCellSet(cellSet);
TryCellSet<CellSetType, VTKM_DEFAULT_CELL_SET_LIST>(unknownCellSet);
}
template <typename CellSetType>
void TryNonDefaultCellSet(CellSetType cellSet)
{
vtkm::cont::UnknownCellSet unknownCellSet(cellSet);
TryCellSet<CellSetType, NonDefaultCellSetList>(unknownCellSet);
}
void TestDynamicCellSet()
{
std::cout << "Try default types with default type lists." << std::endl;
std::cout << "*** 2D Structured Grid ******************" << std::endl;
TryDefaultCellSet(vtkm::cont::CellSetStructured<2>());
std::cout << "*** 3D Structured Grid ******************" << std::endl;
TryDefaultCellSet(vtkm::cont::CellSetStructured<3>());
std::cout << "*** Explicit Grid ***********************" << std::endl;
TryDefaultCellSet(vtkm::cont::CellSetExplicit<>());
std::cout << std::endl << "Try non-default types." << std::endl;
std::cout << "*** 1D Structured Grid ******************" << std::endl;
TryNonDefaultCellSet(vtkm::cont::CellSetStructured<1>());
std::cout << "*** Explicit Grid Constant Shape ********" << std::endl;
TryNonDefaultCellSet(
vtkm::cont::CellSetExplicit<vtkm::cont::ArrayHandleConstant<vtkm::UInt8>::StorageTag>());
std::cout << std::endl << "Try empty DynamicCellSet." << std::endl;
CheckEmptyUnknownCellSet();
}
} // anonymous namespace
int UnitTestUnknownCellSet(int argc, char* argv[])
{
return vtkm::cont::testing::Testing::Run(TestDynamicCellSet, argc, argv);
}