vtk-m/vtkm/VecFromPortal.h

249 lines
7.6 KiB
C
Raw Normal View History

2015-08-13 22:53:34 +00:00
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
2019-04-15 23:24:21 +00:00
//
2015-08-13 22:53:34 +00:00
// 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_VecFromPortal_h
#define vtk_m_VecFromPortal_h
2015-08-13 22:53:34 +00:00
#include <vtkm/Math.h>
#include <vtkm/TypeTraits.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/Types.h>
2015-08-13 22:53:34 +00:00
#include <vtkm/VecTraits.h>
#include <vtkm/internal/ArrayPortalValueReference.h>
2017-05-18 14:29:41 +00:00
namespace vtkm
{
2015-08-13 22:53:34 +00:00
/// \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.
///
2017-05-18 14:29:41 +00:00
template <typename PortalType>
2015-08-13 22:53:34 +00:00
class VecFromPortal
{
public:
2017-05-18 14:29:41 +00:00
using ComponentType = typename std::remove_const<typename PortalType::ValueType>::type;
2015-08-13 22:53:34 +00:00
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
2017-05-18 14:29:41 +00:00
VecFromPortal(const PortalType& portal, vtkm::IdComponent numComponents = 0, vtkm::Id offset = 0)
: Portal(portal)
, NumComponents(numComponents)
, Offset(offset)
{
2015-08-13 22:53:34 +00:00
}
VTKM_EXEC_CONT
2017-05-18 14:29:41 +00:00
vtkm::IdComponent GetNumberOfComponents() const { return this->NumComponents; }
template <typename T, vtkm::IdComponent DestSize>
VTKM_EXEC_CONT void CopyInto(vtkm::Vec<T, DestSize>& dest) const
2015-08-13 22:53:34 +00:00
{
vtkm::IdComponent numComponents = vtkm::Min(DestSize, this->NumComponents);
for (vtkm::IdComponent index = 0; index < numComponents; index++)
{
dest[index] = this->Portal.Get(index + this->Offset);
}
}
template <vtkm::IdComponent N>
VTKM_EXEC_CONT operator vtkm::Vec<ComponentType, N>() const
{
vtkm::Vec<ComponentType, N> result;
this->CopyInto(result);
for (vtkm::IdComponent index = this->NumComponents; index < N; ++index)
{
result[index] = vtkm::TypeTraits<ComponentType>::ZeroInitialization();
}
return result;
}
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
2017-05-18 14:29:41 +00:00
vtkm::internal::ArrayPortalValueReference<PortalType> operator[](vtkm::IdComponent index) const
2015-08-13 22:53:34 +00:00
{
2017-05-18 14:29:41 +00:00
return vtkm::internal::ArrayPortalValueReference<PortalType>(this->Portal,
index + this->Offset);
2015-08-13 22:53:34 +00:00
}
// Only works with Vec-like objects with operator[] and GetNumberofComponents
template <typename OtherVecType>
VTKM_EXEC_CONT VecFromPortal& operator=(const OtherVecType& src)
{
vtkm::IdComponent numComponents = vtkm::Min(src.GetNumberOfComponents(), this->NumComponents);
for (vtkm::IdComponent index = 0; index < numComponents; ++index)
{
this->Portal.Set(index + this->Offset, src[index]);
}
return *this;
}
// Only works with Vec-like objects with operator[] and GetNumberofComponents
template <typename OtherVecType>
VTKM_EXEC_CONT VecFromPortal& operator+=(const OtherVecType& other)
{
vtkm::IdComponent numComponents = vtkm::Min(other.GetNumberOfComponents(), this->NumComponents);
for (vtkm::IdComponent index = 0; index < numComponents; ++index)
{
(*this)[index] += other[index];
}
return *this;
}
// Only works with Vec-like objects with operator[] and GetNumberofComponents
template <typename OtherVecType>
VTKM_EXEC_CONT VecFromPortal& operator-=(const OtherVecType& other)
{
vtkm::IdComponent numComponents = vtkm::Min(other.GetNumberOfComponents(), this->NumComponents);
for (vtkm::IdComponent index = 0; index < numComponents; ++index)
{
(*this)[index] -= other[index];
}
return *this;
}
private:
template <typename OtherVecType>
VTKM_EXEC_CONT void Multiply(const OtherVecType& other, vtkm::TypeTraitsVectorTag)
{
vtkm::IdComponent numComponents = vtkm::Min(other.GetNumberOfComponents(), this->NumComponents);
for (vtkm::IdComponent index = 0; index < numComponents; ++index)
{
(*this)[index] *= other[index];
}
}
template <typename ScalarType>
VTKM_EXEC_CONT void Multiply(ScalarType other, vtkm::TypeTraitsScalarTag)
{
for (vtkm::IdComponent index = 0; index < this->NumComponents; ++index)
{
(*this)[index] *= other;
}
}
public:
// Only works with Vec-like objects with operator[] and GetNumberofComponents
template <typename OtherVecType>
VTKM_EXEC_CONT VecFromPortal& operator*=(const OtherVecType& other)
{
this->Multiply(other, typename vtkm::TypeTraits<OtherVecType>::DimensionalityTag{});
return *this;
}
// Only works with Vec-like objects with operator[] and GetNumberofComponents
template <typename OtherVecType>
VTKM_EXEC_CONT VecFromPortal& operator/=(const OtherVecType& other)
{
vtkm::IdComponent numComponents = vtkm::Min(other.GetNumberOfComponents(), this->NumComponents);
for (vtkm::IdComponent index = 0; index < numComponents; ++index)
{
(*this)[index] /= other[index];
}
return *this;
}
// Only works with Vec-like objects with operator[] and GetNumberofComponents
template <typename OtherVecType>
VTKM_EXEC_CONT bool operator==(const OtherVecType& other)
{
if (this->NumComponents != other.GetNumberOfComponents())
{
return false;
}
for (vtkm::IdComponent index = 0; index < this->NumComponents; ++index)
{
if (this->Portal.Get(index + this->Offset) != other[index])
{
return false;
}
}
return true;
}
// Only works with Vec-like objects with operator[] and GetNumberofComponents
template <typename OtherVecType>
VTKM_EXEC_CONT bool operator!=(const OtherVecType& other)
{
return !(*this == other);
}
VTKM_EXEC_CONT const PortalType& GetPortal() const { return this->Portal; }
VTKM_EXEC_CONT vtkm::Id GetOffset() const { return this->Offset; }
2015-08-13 22:53:34 +00:00
private:
PortalType Portal;
vtkm::IdComponent NumComponents;
vtkm::Id Offset;
};
2017-05-18 14:29:41 +00:00
template <typename PortalType>
struct TypeTraits<vtkm::VecFromPortal<PortalType>>
2015-08-13 22:53:34 +00:00
{
private:
2018-02-22 13:29:13 +00:00
using ComponentType = typename PortalType::ValueType;
2015-08-13 22:53:34 +00:00
public:
2018-02-22 13:29:13 +00:00
using NumericTag = typename vtkm::TypeTraits<ComponentType>::NumericTag;
using DimensionalityTag = TypeTraitsVectorTag;
2015-08-13 22:53:34 +00:00
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
static vtkm::VecFromPortal<PortalType> ZeroInitialization()
2015-08-13 22:53:34 +00:00
{
return vtkm::VecFromPortal<PortalType>();
2015-08-13 22:53:34 +00:00
}
};
2017-05-18 14:29:41 +00:00
template <typename PortalType>
struct VecTraits<vtkm::VecFromPortal<PortalType>>
2015-08-13 22:53:34 +00:00
{
2018-02-22 13:29:13 +00:00
using VecType = vtkm::VecFromPortal<PortalType>;
2015-08-13 22:53:34 +00:00
2018-02-22 13:29:13 +00:00
using ComponentType = typename VecType::ComponentType;
using BaseComponentType = typename vtkm::VecTraits<ComponentType>::BaseComponentType;
2018-02-22 13:29:13 +00:00
using HasMultipleComponents = vtkm::VecTraitsTagMultipleComponents;
using IsSizeStatic = vtkm::VecTraitsTagSizeVariable;
2015-08-13 22:53:34 +00:00
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
2017-05-18 14:29:41 +00:00
static vtkm::IdComponent GetNumberOfComponents(const VecType& vector)
{
2015-08-13 22:53:34 +00:00
return vector.GetNumberOfComponents();
}
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
2017-05-18 14:29:41 +00:00
static ComponentType GetComponent(const VecType& vector, vtkm::IdComponent componentIndex)
2015-08-13 22:53:34 +00:00
{
return vector[componentIndex];
}
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
static void SetComponent(const VecType& vector,
vtkm::IdComponent componentIndex,
const ComponentType& value)
{
vector[componentIndex] = value;
}
VTKM_SUPPRESS_EXEC_WARNINGS
2017-05-18 14:29:41 +00:00
template <vtkm::IdComponent destSize>
VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest)
2015-08-13 22:53:34 +00:00
{
src.CopyInto(dest);
}
};
} // namespace vtkm
#endif //vtk_m_VecFromPortal_h