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