VTK-m  2.1
VecFromPortal.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_VecFromPortal_h
11 #define vtk_m_VecFromPortal_h
12 
13 #include <vtkm/Math.h>
14 #include <vtkm/TypeTraits.h>
15 #include <vtkm/Types.h>
16 #include <vtkm/VecTraits.h>
17 
19 
20 namespace vtkm
21 {
22 
28 template <typename PortalType>
30 {
31 public:
32  using ComponentType = typename std::remove_const<typename PortalType::ValueType>::type;
33 
36  VecFromPortal(const PortalType& portal, vtkm::IdComponent numComponents = 0, vtkm::Id offset = 0)
37  : Portal(portal)
38  , NumComponents(numComponents)
39  , Offset(offset)
40  {
41  }
42 
45 
46  template <typename T, vtkm::IdComponent DestSize>
48  {
49  vtkm::IdComponent numComponents = vtkm::Min(DestSize, this->NumComponents);
50  for (vtkm::IdComponent index = 0; index < numComponents; index++)
51  {
52  dest[index] = this->Portal.Get(index + this->Offset);
53  }
54  }
55 
56  template <vtkm::IdComponent N>
58  {
60  this->CopyInto(result);
61  for (vtkm::IdComponent index = this->NumComponents; index < N; ++index)
62  {
64  }
65  return result;
66  }
67 
70  vtkm::internal::ArrayPortalValueReference<PortalType> operator[](vtkm::IdComponent index) const
71  {
72  return vtkm::internal::ArrayPortalValueReference<PortalType>(this->Portal,
73  index + this->Offset);
74  }
75 
76  // Only works with Vec-like objects with operator[] and GetNumberofComponents
77  template <typename OtherVecType>
78  VTKM_EXEC_CONT VecFromPortal& operator=(const OtherVecType& src)
79  {
80  vtkm::IdComponent numComponents = vtkm::Min(src.GetNumberOfComponents(), this->NumComponents);
81  for (vtkm::IdComponent index = 0; index < numComponents; ++index)
82  {
83  this->Portal.Set(index + this->Offset, src[index]);
84  }
85  return *this;
86  }
87 
88  // Only works with Vec-like objects with operator[] and GetNumberofComponents
89  template <typename OtherVecType>
90  VTKM_EXEC_CONT VecFromPortal& operator+=(const OtherVecType& other)
91  {
92  vtkm::IdComponent numComponents = vtkm::Min(other.GetNumberOfComponents(), this->NumComponents);
93  for (vtkm::IdComponent index = 0; index < numComponents; ++index)
94  {
95  (*this)[index] += other[index];
96  }
97  return *this;
98  }
99 
100  // Only works with Vec-like objects with operator[] and GetNumberofComponents
101  template <typename OtherVecType>
102  VTKM_EXEC_CONT VecFromPortal& operator-=(const OtherVecType& other)
103  {
104  vtkm::IdComponent numComponents = vtkm::Min(other.GetNumberOfComponents(), this->NumComponents);
105  for (vtkm::IdComponent index = 0; index < numComponents; ++index)
106  {
107  (*this)[index] -= other[index];
108  }
109  return *this;
110  }
111 
112 private:
113  template <typename OtherVecType>
114  VTKM_EXEC_CONT void Multiply(const OtherVecType& other, vtkm::TypeTraitsVectorTag)
115  {
116  vtkm::IdComponent numComponents = vtkm::Min(other.GetNumberOfComponents(), this->NumComponents);
117  for (vtkm::IdComponent index = 0; index < numComponents; ++index)
118  {
119  (*this)[index] *= other[index];
120  }
121  }
122 
123  template <typename ScalarType>
125  {
126  for (vtkm::IdComponent index = 0; index < this->NumComponents; ++index)
127  {
128  (*this)[index] *= other;
129  }
130  }
131 
132 public:
133  // Only works with Vec-like objects with operator[] and GetNumberofComponents
134  template <typename OtherVecType>
135  VTKM_EXEC_CONT VecFromPortal& operator*=(const OtherVecType& other)
136  {
138  return *this;
139  }
140 
141  // Only works with Vec-like objects with operator[] and GetNumberofComponents
142  template <typename OtherVecType>
143  VTKM_EXEC_CONT VecFromPortal& operator/=(const OtherVecType& other)
144  {
145  vtkm::IdComponent numComponents = vtkm::Min(other.GetNumberOfComponents(), this->NumComponents);
146  for (vtkm::IdComponent index = 0; index < numComponents; ++index)
147  {
148  (*this)[index] /= other[index];
149  }
150  return *this;
151  }
152 
153  // Only works with Vec-like objects with operator[] and GetNumberofComponents
154  template <typename OtherVecType>
155  VTKM_EXEC_CONT bool operator==(const OtherVecType& other)
156  {
157  if (this->NumComponents != other.GetNumberOfComponents())
158  {
159  return false;
160  }
161  for (vtkm::IdComponent index = 0; index < this->NumComponents; ++index)
162  {
163  if (this->Portal.Get(index + this->Offset) != other[index])
164  {
165  return false;
166  }
167  }
168  return true;
169  }
170 
171  // Only works with Vec-like objects with operator[] and GetNumberofComponents
172  template <typename OtherVecType>
173  VTKM_EXEC_CONT bool operator!=(const OtherVecType& other)
174  {
175  return !(*this == other);
176  }
177 
178  VTKM_EXEC_CONT const PortalType& GetPortal() const { return this->Portal; }
179  VTKM_EXEC_CONT vtkm::Id GetOffset() const { return this->Offset; }
180 
181 private:
182  PortalType Portal;
185 };
186 
187 template <typename PortalType>
188 struct TypeTraits<vtkm::VecFromPortal<PortalType>>
189 {
190 private:
191  using ComponentType = typename PortalType::ValueType;
192 
193 public:
196 
200  {
202  }
203 };
204 
205 template <typename PortalType>
206 struct VecTraits<vtkm::VecFromPortal<PortalType>>
207 {
209 
214 
218  {
219  return vector.GetNumberOfComponents();
220  }
221 
224  static ComponentType GetComponent(const VecType& vector, vtkm::IdComponent componentIndex)
225  {
226  return vector[componentIndex];
227  }
228 
231  static void SetComponent(const VecType& vector,
232  vtkm::IdComponent componentIndex,
233  const ComponentType& value)
234  {
235  vector[componentIndex] = value;
236  }
237 
239  template <vtkm::IdComponent destSize>
241  {
242  src.CopyInto(dest);
243  }
244 };
245 
246 } // namespace vtkm
247 
248 #endif //vtk_m_VecFromPortal_h
vtkm::VecFromPortal::VecFromPortal
VecFromPortal(const PortalType &portal, vtkm::IdComponent numComponents=0, vtkm::Id offset=0)
Definition: VecFromPortal.h:36
vtkm::VecTraitsTagMultipleComponents
A tag for vectors that are "true" vectors (i.e.
Definition: VecTraits.h:23
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::TypeTraits
The TypeTraits class provides helpful compile-time information about the basic types used in VTKm (an...
Definition: TypeTraits.h:61
vtkm::VecFromPortal::operator*=
VecFromPortal & operator*=(const OtherVecType &other)
Definition: VecFromPortal.h:135
Types.h
vtkm::VecFromPortal::operator==
bool operator==(const OtherVecType &other)
Definition: VecFromPortal.h:155
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::VecTraits< vtkm::VecFromPortal< PortalType > >::SetComponent
static void SetComponent(const VecType &vector, vtkm::IdComponent componentIndex, const ComponentType &value)
Definition: VecFromPortal.h:231
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::VecFromPortal::operator=
VecFromPortal & operator=(const OtherVecType &src)
Definition: VecFromPortal.h:78
vtkm::VecFromPortal::Offset
vtkm::Id Offset
Definition: VecFromPortal.h:184
vtkm::VecFromPortal::operator+=
VecFromPortal & operator+=(const OtherVecType &other)
Definition: VecFromPortal.h:90
vtkm::VecFromPortal::Portal
PortalType Portal
Definition: VecFromPortal.h:182
vtkm::VecFromPortal::operator/=
VecFromPortal & operator/=(const OtherVecType &other)
Definition: VecFromPortal.h:143
vtkm::VecFromPortal::Multiply
void Multiply(const OtherVecType &other, vtkm::TypeTraitsVectorTag)
Definition: VecFromPortal.h:114
vtkm::TypeTraits< vtkm::VecFromPortal< PortalType > >::ZeroInitialization
static vtkm::VecFromPortal< PortalType > ZeroInitialization()
Definition: VecFromPortal.h:199
vtkm::VecTraits< vtkm::VecFromPortal< PortalType > >::CopyInto
static void CopyInto(const VecType &src, vtkm::Vec< ComponentType, destSize > &dest)
Definition: VecFromPortal.h:240
vtkm::VecTraits::BaseComponentType
T BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:78
TypeTraits.h
vtkm::VecFromPortal::Multiply
void Multiply(ScalarType other, vtkm::TypeTraitsScalarTag)
Definition: VecFromPortal.h:124
vtkm::VecFromPortal::GetNumberOfComponents
vtkm::IdComponent GetNumberOfComponents() const
Definition: VecFromPortal.h:44
vtkm::VecTraitsTagSizeVariable
A tag for vectors where the number of components are not determined until run time.
Definition: VecTraits.h:43
vtkm::VecFromPortal::GetOffset
vtkm::Id GetOffset() const
Definition: VecFromPortal.h:179
Math.h
ArrayPortalValueReference.h
vtkm::VecFromPortal::CopyInto
void CopyInto(vtkm::Vec< T, DestSize > &dest) const
Definition: VecFromPortal.h:47
vtkm::TypeTraits< vtkm::VecFromPortal< PortalType > >::ComponentType
typename PortalType::ValueType ComponentType
Definition: VecFromPortal.h:191
vtkm::VecFromPortal::NumComponents
vtkm::IdComponent NumComponents
Definition: VecFromPortal.h:183
vtkm::VecTraits< vtkm::VecFromPortal< PortalType > >::GetNumberOfComponents
static vtkm::IdComponent GetNumberOfComponents(const VecType &vector)
Definition: VecFromPortal.h:217
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::VecFromPortal::operator[]
vtkm::internal::ArrayPortalValueReference< PortalType > operator[](vtkm::IdComponent index) const
Definition: VecFromPortal.h:70
vtkm::TypeTraitsUnknownTag
Tag used to identify types that aren't Real, Integer, Scalar or Vector.
Definition: TypeTraits.h:20
vtkm::VecFromPortal::operator!=
bool operator!=(const OtherVecType &other)
Definition: VecFromPortal.h:173
vtkm::TypeTraitsScalarTag
Tag used to identify 0 dimensional types (scalars).
Definition: TypeTraits.h:44
vtkm::TypeTraitsVectorTag
Tag used to identify 1 dimensional types (vectors).
Definition: TypeTraits.h:51
vtkm::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::VecFromPortal::ComponentType
typename std::remove_const< typename PortalType::ValueType >::type ComponentType
Definition: VecFromPortal.h:32
vtkm::VecTraits< vtkm::VecFromPortal< PortalType > >::BaseComponentType
typename vtkm::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Definition: VecFromPortal.h:211
vtkm::VecTraits< vtkm::VecFromPortal< PortalType > >::ComponentType
typename VecType::ComponentType ComponentType
Definition: VecFromPortal.h:210
vtkm::TypeTraits::ZeroInitialization
static T ZeroInitialization()
A static function that returns 0 (or the closest equivalent to it) for the given type.
Definition: TypeTraits.h:77
vtkm::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:61
vtkm::VecTraits< vtkm::VecFromPortal< PortalType > >::GetComponent
static ComponentType GetComponent(const VecType &vector, vtkm::IdComponent componentIndex)
Definition: VecFromPortal.h:224
vtkm::VecFromPortal
A short variable-length array from a window in an ArrayPortal.
Definition: VecFromPortal.h:29
vtkm::VecFromPortal::operator-=
VecFromPortal & operator-=(const OtherVecType &other)
Definition: VecFromPortal.h:102
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53
vtkm::VecFromPortal::GetPortal
const PortalType & GetPortal() const
Definition: VecFromPortal.h:178
VecTraits.h
vtkm::TypeTraits< vtkm::VecFromPortal< PortalType > >::NumericTag
typename vtkm::TypeTraits< ComponentType >::NumericTag NumericTag
Definition: VecFromPortal.h:194