Replace DynamicCellSet implementation with UnknownCellSet

Soon, the `DynamicCellSet` interface will be deprecated so that code can
move to `UnknownCellSet`.
This commit is contained in:
Kenneth Moreland 2021-12-14 10:40:58 -07:00
parent ac024587a6
commit 6da5aa4564
5 changed files with 103 additions and 244 deletions

@ -16,6 +16,7 @@
#include <vtkm/cont/DefaultTypes.h>
#include <vtkm/cont/ErrorBadValue.h>
#include <vtkm/cont/Logging.h>
#include <vtkm/cont/UncertainCellSet.h>
namespace vtkm
{
@ -52,248 +53,30 @@ namespace cont
/// with the default cell set list.
///
template <typename CellSetList>
class VTKM_ALWAYS_EXPORT DynamicCellSetBase
class VTKM_ALWAYS_EXPORT DynamicCellSetBase : public vtkm::cont::UncertainCellSet<CellSetList>
{
VTKM_IS_LIST(CellSetList);
using Superclass = vtkm::cont::UncertainCellSet<CellSetList>;
public:
VTKM_CONT
DynamicCellSetBase() = default;
using Superclass::Superclass;
template <typename CellSetType>
VTKM_CONT DynamicCellSetBase(const CellSetType& cellSet)
: CellSet(std::make_shared<CellSetType>(cellSet))
VTKM_CONT DynamicCellSetBase<CellSetList> NewInstance() const
{
VTKM_IS_CELL_SET(CellSetType);
return DynamicCellSetBase<CellSetList>(this->Superclass::NewInstance());
}
template <typename OtherCellSetList>
VTKM_CONT explicit DynamicCellSetBase(const DynamicCellSetBase<OtherCellSetList>& src)
: CellSet(src.CellSet)
{
}
VTKM_CONT explicit DynamicCellSetBase(const std::shared_ptr<vtkm::cont::CellSet>& cs)
: CellSet(cs)
{
}
/// Returns true if this cell set is of the provided type.
///
template <typename CellSetType>
VTKM_CONT bool IsType() const
{
return (dynamic_cast<CellSetType*>(this->CellSet.get()) != nullptr);
}
/// Returns true if this cell set is the same (or equivalent) type as the
/// object provided.
///
template <typename CellSetType>
VTKM_CONT bool IsSameType(const CellSetType&) const
{
return this->IsType<CellSetType>();
}
/// Returns this cell set cast to the given \c CellSet type. Throws \c
/// ErrorBadType if the cast does not work. Use \c IsType to check if
/// the cast can happen.
///
template <typename CellSetType>
VTKM_CONT CellSetType& Cast() const
{
auto cellSetPointer = dynamic_cast<CellSetType*>(this->CellSet.get());
if (cellSetPointer == nullptr)
{
VTKM_LOG_CAST_FAIL(*this, CellSetType);
throw vtkm::cont::ErrorBadType("Bad cast of dynamic cell set.");
}
VTKM_LOG_CAST_SUCC(*this, *cellSetPointer);
return *cellSetPointer;
}
/// Given a reference to a concrete \c CellSet object, attempt to downcast
/// the contain cell set to the provided type and copy into the given \c
/// CellSet object. Throws \c ErrorBadType if the cast does not work.
/// Use \c IsType to check if the cast can happen.
///
/// Note that this is a shallow copy. Any data in associated arrays are not
/// copied.
///
template <typename CellSetType>
VTKM_CONT void CopyTo(CellSetType& cellSet) const
{
cellSet = this->Cast<CellSetType>();
}
/// Changes the cell set types to try casting to when resolving this dynamic
/// cell set, which is specified with a list tag like those in
/// CellSetList.h. Since C++ does not allow you to actually change the
/// template arguments, this method returns a new dynamic cell setobject.
/// This method is particularly useful to narrow down (or expand) the types
/// when using a cell set of particular constraints.
///
template <typename NewCellSetList>
VTKM_CONT DynamicCellSetBase<NewCellSetList> ResetCellSetList(
NewCellSetList = NewCellSetList()) const
VTKM_CONT vtkm::cont::DynamicCellSetBase<NewCellSetList> ResetCellSetList(NewCellSetList) const
{
VTKM_IS_LIST(NewCellSetList);
return DynamicCellSetBase<NewCellSetList>(*this);
return vtkm::cont::DynamicCellSetBase<NewCellSetList>(*this);
}
/// Attempts to cast the held cell set to a specific concrete type, then call
/// the given functor with the cast cell set. The cell sets tried in the cast
/// are those in the \c CellSetList template argument of the \c
/// DynamicCellSetBase class (or \c VTKM_DEFAULT_CELL_SET_LIST for \c
/// DynamicCellSet). You can use \c ResetCellSetList to get different
/// behavior from \c CastAndCall.
///
template <typename Functor, typename... Args>
VTKM_CONT void CastAndCall(Functor&& f, Args&&...) const;
/// \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 dynamic cell set for it. This method is convenient when
/// creating output data sets that should be the same type as some input cell
/// set.
///
VTKM_CONT
DynamicCellSetBase<CellSetList> NewInstance() const
template <typename NewCellSetList>
VTKM_CONT vtkm::cont::DynamicCellSetBase<NewCellSetList> ResetCellSetList() const
{
DynamicCellSetBase<CellSetList> newCellSet;
if (this->CellSet)
{
newCellSet.CellSet = this->CellSet->NewInstance();
}
return newCellSet;
return vtkm::cont::DynamicCellSetBase<NewCellSetList>(*this);
}
VTKM_CONT
vtkm::cont::CellSet* GetCellSetBase() { return this->CellSet.get(); }
VTKM_CONT
const vtkm::cont::CellSet* GetCellSetBase() const { return this->CellSet.get(); }
VTKM_CONT
vtkm::Id GetNumberOfCells() const
{
return this->CellSet ? this->CellSet->GetNumberOfCells() : 0;
}
VTKM_CONT
vtkm::Id GetNumberOfFaces() const
{
return this->CellSet ? this->CellSet->GetNumberOfFaces() : 0;
}
VTKM_CONT
vtkm::Id GetNumberOfEdges() const
{
return this->CellSet ? this->CellSet->GetNumberOfEdges() : 0;
}
VTKM_CONT
vtkm::Id GetNumberOfPoints() const
{
return this->CellSet ? this->CellSet->GetNumberOfPoints() : 0;
}
VTKM_CONT
void ReleaseResourcesExecution()
{
if (this->CellSet)
{
this->CellSet->ReleaseResourcesExecution();
}
}
VTKM_CONT
void PrintSummary(std::ostream& stream) const
{
if (this->CellSet)
{
this->CellSet->PrintSummary(stream);
}
else
{
stream << " DynamicCellSet = nullptr" << std::endl;
}
}
private:
std::shared_ptr<vtkm::cont::CellSet> CellSet;
template <typename>
friend class DynamicCellSetBase;
};
//=============================================================================
// Free function casting helpers
/// Returns true if \c dynamicCellSet matches the type of CellSetType.
///
template <typename CellSetType, typename Ts>
VTKM_CONT inline bool IsType(const vtkm::cont::DynamicCellSetBase<Ts>& dynamicCellSet)
{
return dynamicCellSet.template IsType<CellSetType>();
}
/// Returns \c dynamicCellSet cast to the given \c CellSet type. Throws \c
/// ErrorBadType if the cast does not work. Use \c IsType
/// to check if the cast can happen.
///
template <typename CellSetType, typename Ts>
VTKM_CONT inline CellSetType Cast(const vtkm::cont::DynamicCellSetBase<Ts>& dynamicCellSet)
{
return dynamicCellSet.template Cast<CellSetType>();
}
namespace detail
{
struct DynamicCellSetTry
{
DynamicCellSetTry(const vtkm::cont::CellSet* const cellSetBase)
: CellSetBase(cellSetBase)
{
}
template <typename CellSetType, typename Functor, typename... Args>
void operator()(CellSetType, Functor&& f, bool& called, Args&&... args) const
{
if (!called)
{
auto* cellset = dynamic_cast<const CellSetType*>(this->CellSetBase);
if (cellset)
{
VTKM_LOG_CAST_SUCC(*this->CellSetBase, *cellset);
f(*cellset, std::forward<Args>(args)...);
called = true;
}
}
}
const vtkm::cont::CellSet* const CellSetBase;
};
} // namespace detail
template <typename CellSetList>
template <typename Functor, typename... Args>
VTKM_CONT void DynamicCellSetBase<CellSetList>::CastAndCall(Functor&& f, Args&&... args) const
{
bool called = false;
detail::DynamicCellSetTry tryCellSet(this->CellSet.get());
vtkm::ListForEach(
tryCellSet, CellSetList{}, std::forward<Functor>(f), called, std::forward<Args>(args)...);
if (!called)
{
VTKM_LOG_CAST_FAIL(*this, CellSetList);
throw vtkm::cont::ErrorBadValue("Could not find appropriate cast for cell set.");
}
}
using DynamicCellSet = DynamicCellSetBase<VTKM_DEFAULT_CELL_SET_LIST>;
namespace internal
@ -317,13 +100,7 @@ namespace internal
template <typename T>
struct DynamicCellSetCheck
{
using type = std::false_type;
};
template <typename CellSetList>
struct DynamicCellSetCheck<vtkm::cont::DynamicCellSetBase<CellSetList>>
{
using type = std::true_type;
using type = vtkm::cont::internal::UnknownCellSetCheck<T>;
};
#define VTKM_IS_DYNAMIC_CELL_SET(T) \

@ -49,7 +49,7 @@ public:
VTKM_CONT UncertainCellSet() = default;
template <typename CellSetType>
explicit VTKM_CONT UncertainCellSet(const CellSetType& cellSet)
VTKM_CONT UncertainCellSet(const CellSetType& cellSet)
: Superclass(cellSet)
{
}

@ -49,6 +49,20 @@ class VTKM_CONT_EXPORT UnknownCellSet
{
std::shared_ptr<vtkm::cont::CellSet> Container;
void InitializeKnownOrUnknownCellSet(const UnknownCellSet& cellSet,
std::true_type vtkmNotUsed(isUnknownCellSet))
{
*this = cellSet;
}
template <typename CellSetType>
void InitializeKnownOrUnknownCellSet(const CellSetType& cellSet,
std::false_type vtkmNotUsed(isUnknownCellSet))
{
VTKM_IS_CELL_SET(CellSetType);
this->Container = std::shared_ptr<vtkm::cont::CellSet>(new CellSetType(cellSet));
}
public:
VTKM_CONT UnknownCellSet() = default;
UnknownCellSet(const UnknownCellSet&) = default;
@ -56,8 +70,8 @@ public:
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));
this->InitializeKnownOrUnknownCellSet(
cellSet, typename std::is_base_of<UnknownCellSet, CellSetType>::type{});
}
/// \brief Returns whether a cell set is stored in this `UnknownCellSet`.
@ -165,13 +179,14 @@ public:
VTKM_CONT void AsCellSet(CellSetType& cellSet) const
{
VTKM_IS_CELL_SET(CellSetType);
if (!this->IsType<CellSetType>())
CellSetType* cellSetPointer = dynamic_cast<CellSetType*>(this->Container.get());
if (cellSetPointer == nullptr)
{
VTKM_LOG_CAST_FAIL(*this, decltype(cellSet));
VTKM_LOG_CAST_FAIL(*this, CellSetType);
throwFailedDynamicCast(this->GetCellSetName(), vtkm::cont::TypeToString(cellSet));
}
cellSet = *reinterpret_cast<CellSetType*>(this->Container.get());
VTKM_LOG_CAST_SUCC(*this, *cellSetPointer);
cellSet = *cellSetPointer;
}
template <typename CellSetType>
@ -207,8 +222,58 @@ public:
///
template <typename CellSetList, typename Functor, typename... Args>
VTKM_CONT void CastAndCallForTypes(Functor&& functor, Args&&... args) const;
// Support for (soon to be) deprecated DynamicCellSet
// TODO: Deprecate these methods
template <typename CellSetType>
VTKM_CONT bool IsSameType(const CellSetType&) const
{
return this->IsType<CellSetType>();
}
template <typename CellSetType>
VTKM_CONT CellSetType Cast() const
{
return this->AsCellSet<CellSetType>();
}
template <typename CellSetType>
VTKM_CONT void CopyTo(CellSetType& cellSet) const
{
return this->AsCellSet(cellSet);
}
// template <typename Functor, typename... Args>
// VTKM_CONT void CastAndCall(Functor&& f, Args&&... args) const
// {
// this->CastAndCallForTypes<VTKM_DEFAULT_CELL_SET_LIST>(
// std::forward<Functor>(f), std::forward<Args>(args)...);
// }
};
//=============================================================================
// Free function casting helpers
// (Not sure if these should be deprecated.)
/// Returns true if `unknownCellSet` matches the type of `CellSetType`.
///
template <typename CellSetType>
VTKM_CONT inline bool IsType(const vtkm::cont::UnknownCellSet& unknownCellSet)
{
return unknownCellSet.IsType<CellSetType>();
}
/// Returns `unknownCellSet` cast to the given `CellSet` type. Throws
/// `ErrorBadType` if the cast does not work. Use `IsType`
/// to check if the cast can happen.
///
template <typename CellSetType>
VTKM_CONT inline CellSetType Cast(const vtkm::cont::UnknownCellSet& unknownCellSet)
{
return unknownCellSet.Cast<CellSetType>();
}
namespace internal
{
@ -262,6 +327,23 @@ void CastAndCall(const vtkm::cont::UnknownCellSet& cellSet, Functor&& f, Args&&.
std::forward<Args>(args)...);
}
namespace internal
{
/// Checks to see if the given object is an unknown (or uncertain) cell set. It
/// resolves to either `std::true_type` or `std::false_type`.
///
template <typename T>
using UnknownCellSetCheck = typename std::is_base_of<vtkm::cont::UnknownCellSet, T>::type;
#define VTKM_IS_UNKNOWN_CELL_SET(T) \
VTKM_STATIC_ASSERT(::vtkm::cont::internal::UnknownCellSetCheck<T>::value)
#define VTKM_IS_KNOWN_OR_UNKNOWN_CELL_SET(T) \
VTKM_STATIC_ASSERT(::vtkm::cont::internal::CellSetCheck<T>::type::value || \
::vtkm::cont::internal::UnknownCellSetCheck<T>::value)
} // namespace internal
} // namespace vtkm::cont
} // namespace vtkm

@ -63,7 +63,7 @@ void TestExternalFacesExplicitGrid(const vtkm::cont::DataSet& ds,
vtkm::cont::DataSet resultds = externalFaces.Execute(ds);
// verify cellset
vtkm::cont::CellSetExplicit<>& new_cellSet =
vtkm::cont::CellSetExplicit<> new_cellSet =
resultds.GetCellSet().Cast<vtkm::cont::CellSetExplicit<>>();
const vtkm::Id numOutputExtFaces = new_cellSet.GetNumberOfCells();
VTKM_TEST_ASSERT(numOutputExtFaces == numExpectedExtFaces, "Number of External Faces mismatch");

@ -165,7 +165,7 @@ void TestSplitSharpEdgesFilterNoSplit(vtkm::cont::DataSet& simpleCubeWithSN,
vtkm::cont::DataSet result = splitSharpEdgesFilter.Execute(simpleCubeWithSN);
auto newCoords = result.GetCoordinateSystem().GetDataAsMultiplexer();
vtkm::cont::CellSetExplicit<>& newCellset =
vtkm::cont::CellSetExplicit<> newCellset =
result.GetCellSet().Cast<vtkm::cont::CellSetExplicit<>>();
auto newCoordsP = newCoords.ReadPortal();
vtkm::cont::ArrayHandle<vtkm::FloatDefault> newPointvarField;