mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-09-16 17:22:55 +00:00
Added VecFromPortal class.
This commit is contained in:
parent
da2e601b6a
commit
c9d95298b0
@ -113,12 +113,12 @@ struct VecTraits
|
||||
vtkm::IdComponent component,
|
||||
ComponentType value);
|
||||
|
||||
/// Copies the components in this vector into a given Vec object.
|
||||
/// Copies the components in the given vector into a given Vec object.
|
||||
///
|
||||
template<vktm::IdComponent destSize>
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
static void
|
||||
ToVec(const VecType &src, vtkm::Vec<ComponentType,destSize> &dest);
|
||||
CopyInto(const VecType &src, vtkm::Vec<ComponentType,destSize> &dest);
|
||||
};
|
||||
#else // VTKM_DOXYGEN_ONLY
|
||||
;
|
||||
|
@ -20,6 +20,7 @@
|
||||
|
||||
set(headers
|
||||
ErrorMessageBuffer.h
|
||||
VecFromPortal.h
|
||||
WorkletInvokeFunctor.h
|
||||
WorkletInvokeFunctorDetail.h
|
||||
)
|
||||
|
138
vtkm/exec/internal/VecFromPortal.h
Normal file
138
vtkm/exec/internal/VecFromPortal.h
Normal file
@ -0,0 +1,138 @@
|
||||
//============================================================================
|
||||
// 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_exec_internal_VecFromPortal_h
|
||||
#define vtk_m_exec_internal_VecFromPortal_h
|
||||
|
||||
#include <vtkm/Math.h>
|
||||
#include <vtkm/Types.h>
|
||||
#include <vtkm/TypeTraits.h>
|
||||
#include <vtkm/VecTraits.h>
|
||||
|
||||
namespace vtkm {
|
||||
namespace exec {
|
||||
namespace internal {
|
||||
|
||||
/// \brief A short variable-length array from a window in an ArrayPortal.
|
||||
///
|
||||
/// The \c VecFromPortal class is a Vec-like class that holds an array portal
|
||||
/// and exposes a small window of that portal as if it were a \c Vec.
|
||||
///
|
||||
template<typename PortalType>
|
||||
class VecFromPortal
|
||||
{
|
||||
public:
|
||||
typedef typename PortalType::ValueType ComponentType;
|
||||
|
||||
VTKM_EXEC_EXPORT
|
||||
VecFromPortal() : NumComponents(0), Offset(0) { }
|
||||
|
||||
VTKM_EXEC_EXPORT
|
||||
VecFromPortal(const PortalType &portal,
|
||||
vtkm::IdComponent numComponents = 0,
|
||||
vtkm::Id offset = 0)
|
||||
: Portal(portal), NumComponents(numComponents), Offset(offset) { }
|
||||
|
||||
VTKM_EXEC_EXPORT
|
||||
vtkm::IdComponent GetNumberOfComponents() const {
|
||||
return this->NumComponents;
|
||||
}
|
||||
|
||||
template<vtkm::IdComponent DestSize>
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
void CopyInto(vtkm::Vec<ComponentType,DestSize> &dest) const
|
||||
{
|
||||
vtkm::IdComponent numComponents = vtkm::Min(DestSize, this->NumComponents);
|
||||
for (vtkm::IdComponent index = 0; index < numComponents; index++)
|
||||
{
|
||||
dest[index] = this->Portal.Get(index + this->Offset);
|
||||
}
|
||||
}
|
||||
|
||||
VTKM_EXEC_EXPORT
|
||||
ComponentType operator[](vtkm::IdComponent index) const
|
||||
{
|
||||
return this->Portal.Get(index + this->Offset);
|
||||
}
|
||||
|
||||
private:
|
||||
PortalType Portal;
|
||||
vtkm::IdComponent NumComponents;
|
||||
vtkm::Id Offset;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
} // namespace vtkm::exec::internal
|
||||
|
||||
// Implementations of traits classes, which by definition are in the vtkm
|
||||
// namespace.
|
||||
|
||||
namespace vtkm {
|
||||
|
||||
template<typename PortalType>
|
||||
struct TypeTraits<vtkm::exec::internal::VecFromPortal<PortalType> >
|
||||
{
|
||||
private:
|
||||
typedef typename PortalType::ValueType ComponentType;
|
||||
|
||||
public:
|
||||
typedef typename vtkm::TypeTraits<ComponentType>::NumericTag NumericTag;
|
||||
typedef TypeTraitsVectorTag DimensionalityTag;
|
||||
|
||||
VTKM_EXEC_EXPORT
|
||||
static vtkm::exec::internal::VecFromPortal<PortalType> ZeroInitialization()
|
||||
{
|
||||
return vtkm::exec::internal::VecFromPortal<PortalType>();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename PortalType>
|
||||
struct VecTraits<vtkm::exec::internal::VecFromPortal<PortalType> >
|
||||
{
|
||||
typedef vtkm::exec::internal::VecFromPortal<PortalType> VecType;
|
||||
|
||||
typedef typename VecType::ComponentType ComponentType;
|
||||
typedef vtkm::VecTraitsTagMultipleComponents HasMultipleComponents;
|
||||
typedef vtkm::VecTraitsTagSizeVariable IsSizeStatic;
|
||||
|
||||
VTKM_EXEC_EXPORT
|
||||
static vtkm::IdComponent GetNumberOfComponents(const VecType &vector) {
|
||||
return vector.GetNumberOfComponents();
|
||||
}
|
||||
|
||||
VTKM_EXEC_EXPORT
|
||||
static ComponentType GetComponent(const VecType &vector,
|
||||
vtkm::IdComponent componentIndex)
|
||||
{
|
||||
return vector[componentIndex];
|
||||
}
|
||||
|
||||
template<vtkm::IdComponent destSize>
|
||||
VTKM_EXEC_EXPORT
|
||||
static void CopyInto(const VecType &src,
|
||||
vtkm::Vec<ComponentType,destSize> &dest)
|
||||
{
|
||||
src.CopyInto(dest);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace vtkm
|
||||
|
||||
#endif //vtk_m_exec_internal_VecFromPortal_h
|
@ -22,6 +22,7 @@
|
||||
|
||||
set(unit_tests
|
||||
UnitTestErrorMessageBuffer.cxx
|
||||
UnitTestVecFromPortal.cxx
|
||||
UnitTestWorkletInvokeFunctor.cxx
|
||||
)
|
||||
vtkm_unit_tests(SOURCES ${unit_tests})
|
||||
|
119
vtkm/exec/internal/testing/UnitTestVecFromPortal.cxx
Normal file
119
vtkm/exec/internal/testing/UnitTestVecFromPortal.cxx
Normal file
@ -0,0 +1,119 @@
|
||||
//============================================================================
|
||||
// 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.
|
||||
//============================================================================
|
||||
|
||||
#include <vtkm/exec/internal/VecFromPortal.h>
|
||||
|
||||
#include <vtkm/testing/Testing.h>
|
||||
|
||||
namespace {
|
||||
|
||||
static const vtkm::IdComponent ARRAY_SIZE = 10;
|
||||
|
||||
template<typename T>
|
||||
void CheckType(T, T)
|
||||
{
|
||||
// Check passes if this function is called correctly.
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
class TestPortal
|
||||
{
|
||||
public:
|
||||
typedef T ValueType;
|
||||
|
||||
VTKM_EXEC_EXPORT
|
||||
vtkm::Id GetNumberOfValues() const { return ARRAY_SIZE; }
|
||||
|
||||
VTKM_EXEC_EXPORT
|
||||
ValueType Get(vtkm::Id index) const { return TestValue(index, ValueType()); }
|
||||
};
|
||||
|
||||
struct VecFromPortalTestFunctor
|
||||
{
|
||||
template<typename T>
|
||||
void operator()(T) const
|
||||
{
|
||||
typedef TestPortal<T> PortalType;
|
||||
typedef vtkm::exec::internal::VecFromPortal<PortalType> VecType;
|
||||
typedef vtkm::TypeTraits<VecType> TTraits;
|
||||
typedef vtkm::VecTraits<VecType> VTraits;
|
||||
|
||||
std::cout << "Checking VecFromPortal traits" << std::endl;
|
||||
|
||||
// The statements will fail to compile if the traits is not working as
|
||||
// expected.
|
||||
CheckType(typename TTraits::DimensionalityTag(),
|
||||
vtkm::TypeTraitsVectorTag());
|
||||
CheckType(typename VTraits::ComponentType(), T());
|
||||
CheckType(typename VTraits::HasMultipleComponents(),
|
||||
vtkm::VecTraitsTagMultipleComponents());
|
||||
CheckType(typename VTraits::IsSizeStatic(),
|
||||
vtkm::VecTraitsTagSizeVariable());
|
||||
|
||||
std::cout << "Checking VecFromPortal contents" << std::endl;
|
||||
|
||||
PortalType portal;
|
||||
|
||||
for (vtkm::Id offset = 0; offset < ARRAY_SIZE; offset++)
|
||||
{
|
||||
for (vtkm::IdComponent length = 0; length < ARRAY_SIZE-offset; length++)
|
||||
{
|
||||
VecType vec(portal, length, offset);
|
||||
|
||||
VTKM_TEST_ASSERT(vec.GetNumberOfComponents() == length,
|
||||
"Wrong length.");
|
||||
VTKM_TEST_ASSERT(VTraits::GetNumberOfComponents(vec) == length,
|
||||
"Wrong length.");
|
||||
|
||||
vtkm::Vec<T,ARRAY_SIZE> copyDirect;
|
||||
vec.CopyInto(copyDirect);
|
||||
|
||||
vtkm::Vec<T,ARRAY_SIZE> copyTraits;
|
||||
VTraits::CopyInto(vec, copyTraits);
|
||||
|
||||
for (vtkm::IdComponent index = 0; index < length; index++)
|
||||
{
|
||||
T expected = TestValue(index+offset, T());
|
||||
VTKM_TEST_ASSERT(test_equal(vec[index], expected), "Wrong value.");
|
||||
VTKM_TEST_ASSERT(
|
||||
test_equal(VTraits::GetComponent(vec, index), expected),
|
||||
"Wrong value.");
|
||||
VTKM_TEST_ASSERT(test_equal(copyDirect[index], expected),
|
||||
"Wrong copied value.");
|
||||
VTKM_TEST_ASSERT(test_equal(copyTraits[index], expected),
|
||||
"Wrong copied value.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void VecFromPortalTest()
|
||||
{
|
||||
vtkm::testing::Testing::TryTypes(VecFromPortalTestFunctor(),
|
||||
vtkm::TypeListTagCommon());
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
int UnitTestVecFromPortal(int, char *[])
|
||||
{
|
||||
return vtkm::testing::Testing::Run(VecFromPortalTest);
|
||||
}
|
Loading…
Reference in New Issue
Block a user