Commit 9b62c9ee authored by Kenneth Moreland's avatar Kenneth Moreland

Enable storing variable-sized Vecs in UnknownArrayHandle

One of the features of `UnknownArrayHandle` is that it allows you to
query how many `Vec` components each value has without resolve the type
of the array. The functionality to implement this failed if you tried to
store an `ArrayHandle` that stored `Vec`-like objects with `Vec` sizes
that varied from value to value (i.e. an `ArrayHandleGroupVecVariable`).

Storing such an array in `UnknownArrayHandle` might not be the best
idea, but it should probably work. This change allows you to store such
an array. If you try to query the number of components, you will get 0.
parent fbb69b76
......@@ -51,10 +51,23 @@ static vtkm::Id UnknownAHNumberOfValues(void* mem)
return arrayHandle->GetNumberOfValues();
template <typename T, typename StaticSize = typename vtkm::VecTraits<T>::IsSizeStatic>
struct UnknownAHNumberOfComponentsImpl;
template <typename T>
struct UnknownAHNumberOfComponentsImpl<T, vtkm::VecTraitsTagSizeStatic>
static constexpr vtkm::IdComponent Value = vtkm::VecTraits<T>::NUM_COMPONENTS;
template <typename T>
struct UnknownAHNumberOfComponentsImpl<T, vtkm::VecTraitsTagSizeVariable>
static constexpr vtkm::IdComponent Value = 0;
template <typename T>
static vtkm::IdComponent UnknownAHNumberOfComponents()
return vtkm::VecTraits<T>::NUM_COMPONENTS;
return UnknownAHNumberOfComponentsImpl<T>::Value;
template <typename T, typename S>
......@@ -303,6 +316,8 @@ public:
/// If the array holds `vtkm::Vec` objects, this will return the number of components
/// in each value. If the array holds a basic C type (such as `float`), this will return 1.
/// If the array holds `Vec`-like objects that have the number of components that can vary
/// at runtime, this method will return 0 (because there is no consistent answer).
VTKM_CONT vtkm::IdComponent GetNumberOfComponents() const
......@@ -11,6 +11,12 @@
#include <vtkm/cont/UncertainArrayHandle.h>
#include <vtkm/cont/UnknownArrayHandle.h>
#include <vtkm/cont/ArrayHandleCast.h>
#include <vtkm/cont/ArrayHandleConstant.h>
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/ArrayHandleGroupVecVariable.h>
#include <vtkm/cont/ArrayHandleMultiplexer.h>
#include <vtkm/TypeTraits.h>
#include <vtkm/cont/testing/Testing.h>
......@@ -237,6 +243,25 @@ void CheckAsArrayHandle(const ArrayHandleType& array)
ArrayHandleType retrievedArray = arrayUnknown2.AsArrayHandle<ArrayHandleType>();
VTKM_TEST_ASSERT(array == retrievedArray);
std::cout << " Try adding arrays with variable amounts of components" << std::endl;
// There might be some limited functionality, but you should still be able
// to get arrays in and out.
// Note, this is a bad way to implement this array. You should something like
// ArrayHandleGroupVec instead.
using VariableVecArrayType =
VariableVecArrayType inArray = vtkm::cont::make_ArrayHandleGroupVecVariable(
array, vtkm::cont::make_ArrayHandleCounting<vtkm::Id>(0, 2, ARRAY_SIZE / 2 + 1));
VTKM_TEST_ASSERT(inArray.GetNumberOfValues() == ARRAY_SIZE / 2);
vtkm::cont::UnknownArrayHandle arrayUnknown2 = inArray;
VariableVecArrayType retrievedArray = arrayUnknown2.AsArrayHandle<VariableVecArrayType>();
VTKM_TEST_ASSERT(retrievedArray == inArray);
// A vtkm::Vec if NumComps > 1, otherwise a scalar
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