VecFromPortal.h 4.03 KB
Newer Older
1 2 3 4 5 6 7 8
//============================================================================
//  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.
//
9
//  Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 11 12
//  Copyright 2015 UT-Battelle, LLC.
//  Copyright 2015 Los Alamos National Security.
//
13
//  Under the terms of Contract DE-NA0003525 with NTESS,
14 15 16 17 18 19
//  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.
//============================================================================
20 21
#ifndef vtk_m_VecFromPortal_h
#define vtk_m_VecFromPortal_h
22 23 24

#include <vtkm/Math.h>
#include <vtkm/TypeTraits.h>
25
#include <vtkm/Types.h>
26 27
#include <vtkm/VecTraits.h>

28 29
#include <vtkm/internal/ArrayPortalValueReference.h>

30 31
namespace vtkm
{
32 33 34 35 36 37

/// \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.
///
38
template <typename PortalType>
39 40 41
class VecFromPortal
{
public:
42
  using ComponentType = typename std::remove_const<typename PortalType::ValueType>::type;
43

44
  VTKM_SUPPRESS_EXEC_WARNINGS
45
  VTKM_EXEC_CONT
46 47 48 49 50
  VecFromPortal()
    : NumComponents(0)
    , Offset(0)
  {
  }
51

52
  VTKM_SUPPRESS_EXEC_WARNINGS
53
  VTKM_EXEC_CONT
54 55 56 57 58
  VecFromPortal(const PortalType& portal, vtkm::IdComponent numComponents = 0, vtkm::Id offset = 0)
    : Portal(portal)
    , NumComponents(numComponents)
    , Offset(offset)
  {
59 60
  }

61
  VTKM_EXEC_CONT
62 63 64 65
  vtkm::IdComponent GetNumberOfComponents() const { return this->NumComponents; }

  template <typename T, vtkm::IdComponent DestSize>
  VTKM_EXEC_CONT void CopyInto(vtkm::Vec<T, DestSize>& dest) const
66 67 68 69 70 71 72 73
  {
    vtkm::IdComponent numComponents = vtkm::Min(DestSize, this->NumComponents);
    for (vtkm::IdComponent index = 0; index < numComponents; index++)
    {
      dest[index] = this->Portal.Get(index + this->Offset);
    }
  }

74
  VTKM_SUPPRESS_EXEC_WARNINGS
75
  VTKM_EXEC_CONT
76
  vtkm::internal::ArrayPortalValueReference<PortalType> operator[](vtkm::IdComponent index) const
77
  {
78 79
    return vtkm::internal::ArrayPortalValueReference<PortalType>(this->Portal,
                                                                 index + this->Offset);
80 81 82 83 84 85 86 87
  }

private:
  PortalType Portal;
  vtkm::IdComponent NumComponents;
  vtkm::Id Offset;
};

88 89
template <typename PortalType>
struct TypeTraits<vtkm::VecFromPortal<PortalType>>
90 91
{
private:
92
  using ComponentType = typename PortalType::ValueType;
93 94

public:
95 96
  using NumericTag = typename vtkm::TypeTraits<ComponentType>::NumericTag;
  using DimensionalityTag = TypeTraitsVectorTag;
97

98 99 100
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
  static vtkm::VecFromPortal<PortalType> ZeroInitialization()
101
  {
102
    return vtkm::VecFromPortal<PortalType>();
103 104 105
  }
};

106 107
template <typename PortalType>
struct VecTraits<vtkm::VecFromPortal<PortalType>>
108
{
109
  using VecType = vtkm::VecFromPortal<PortalType>;
110

111 112 113
  using ComponentType = typename VecType::ComponentType;
  using HasMultipleComponents = vtkm::VecTraitsTagMultipleComponents;
  using IsSizeStatic = vtkm::VecTraitsTagSizeVariable;
114

115 116
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
117 118
  static vtkm::IdComponent GetNumberOfComponents(const VecType& vector)
  {
119 120 121
    return vector.GetNumberOfComponents();
  }

122 123
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
124
  static ComponentType GetComponent(const VecType& vector, vtkm::IdComponent componentIndex)
125 126 127 128
  {
    return vector[componentIndex];
  }

129
  VTKM_SUPPRESS_EXEC_WARNINGS
130 131
  template <vtkm::IdComponent destSize>
  VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest)
132 133 134 135 136 137 138
  {
    src.CopyInto(dest);
  }
};

} // namespace vtkm

139
#endif //vtk_m_VecFromPortal_h