VTK-m  2.2
ArrayHandleExtractComponent.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_cont_ArrayHandleExtractComponent_h
11 #define vtk_m_cont_ArrayHandleExtractComponent_h
12 
13 #include <vtkm/StaticAssert.h>
14 #include <vtkm/VecTraits.h>
16 #include <vtkm/cont/ArrayHandle.h>
17 
18 namespace vtkm
19 {
20 namespace internal
21 {
22 
23 template <typename PortalType>
24 class VTKM_ALWAYS_EXPORT ArrayPortalExtractComponent
25 {
26  using Writable = vtkm::internal::PortalSupportsSets<PortalType>;
27 
28 public:
29  using VectorType = typename PortalType::ValueType;
30  using Traits = vtkm::VecTraits<VectorType>;
31  using ValueType = typename Traits::ComponentType;
32 
34  ArrayPortalExtractComponent()
35  : Portal()
36  , Component(0)
37  {
38  }
39 
41  ArrayPortalExtractComponent(const PortalType& portal, vtkm::IdComponent component)
42  : Portal(portal)
43  , Component(component)
44  {
45  }
46 
47  ArrayPortalExtractComponent(const ArrayPortalExtractComponent&) = default;
48  ArrayPortalExtractComponent(ArrayPortalExtractComponent&&) = default;
49  ArrayPortalExtractComponent& operator=(const ArrayPortalExtractComponent&) = default;
50  ArrayPortalExtractComponent& operator=(ArrayPortalExtractComponent&&) = default;
51 
53  vtkm::Id GetNumberOfValues() const { return this->Portal.GetNumberOfValues(); }
54 
56  ValueType Get(vtkm::Id index) const
57  {
58  return Traits::GetComponent(this->Portal.Get(index), this->Component);
59  }
60 
61  template <typename Writable_ = Writable,
62  typename = typename std::enable_if<Writable_::value>::type>
63  VTKM_EXEC_CONT void Set(vtkm::Id index, const ValueType& value) const
64  {
65  VectorType vec = this->Portal.Get(index);
66  Traits::SetComponent(vec, this->Component, value);
67  this->Portal.Set(index, vec);
68  }
69 
71  const PortalType& GetPortal() const { return this->Portal; }
72 
73 private:
74  PortalType Portal;
75  vtkm::IdComponent Component;
76 }; // class ArrayPortalExtractComponent
77 
78 } // namespace internal
79 
80 namespace cont
81 {
82 
83 template <typename ArrayHandleType>
85 {
86 };
87 
88 namespace internal
89 {
90 
91 template <typename ArrayHandleType>
92 class Storage<typename vtkm::VecTraits<typename ArrayHandleType::ValueType>::ComponentType,
93  StorageTagExtractComponent<ArrayHandleType>>
94 {
95  using SourceValueType = typename ArrayHandleType::ValueType;
96  using ValueType = typename vtkm::VecTraits<SourceValueType>::ComponentType;
97  using SourceStorage = typename ArrayHandleType::StorageType;
98 
99 public:
100  VTKM_CONT static vtkm::IdComponent ComponentIndex(
101  const std::vector<vtkm::cont::internal::Buffer>& buffers)
102  {
103  return buffers[0].GetMetaData<vtkm::IdComponent>();
104  }
105 
106  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> SourceBuffers(
107  const std::vector<vtkm::cont::internal::Buffer>& buffers)
108  {
109  return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 1, buffers.end());
110  }
111 
112  using ReadPortalType =
113  vtkm::internal::ArrayPortalExtractComponent<typename SourceStorage::ReadPortalType>;
114  using WritePortalType =
115  vtkm::internal::ArrayPortalExtractComponent<typename SourceStorage::WritePortalType>;
116 
117  VTKM_CONT static vtkm::IdComponent GetNumberOfComponentsFlat(
118  const std::vector<vtkm::cont::internal::Buffer>&)
119  {
121  }
122 
123  VTKM_CONT static vtkm::Id GetNumberOfValues(
124  const std::vector<vtkm::cont::internal::Buffer>& buffers)
125  {
126  return SourceStorage::GetNumberOfValues(SourceBuffers(buffers));
127  }
128 
129  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>&,
130  const ValueType&,
131  vtkm::Id,
132  vtkm::Id,
134  {
135  throw vtkm::cont::ErrorBadType("Fill not supported for ArrayHandleExtractComponent.");
136  }
137 
138  VTKM_CONT static void ResizeBuffers(vtkm::Id numValues,
139  const std::vector<vtkm::cont::internal::Buffer>& buffers,
140  vtkm::CopyFlag preserve,
141  vtkm::cont::Token& token)
142  {
143  SourceStorage::ResizeBuffers(numValues, SourceBuffers(buffers), preserve, token);
144  }
145 
146  VTKM_CONT static ReadPortalType CreateReadPortal(
147  const std::vector<vtkm::cont::internal::Buffer>& buffers,
149  vtkm::cont::Token& token)
150  {
151  return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
152  ComponentIndex(buffers));
153  }
154 
155  VTKM_CONT static WritePortalType CreateWritePortal(
156  const std::vector<vtkm::cont::internal::Buffer>& buffers,
158  vtkm::cont::Token& token)
159  {
160  return WritePortalType(SourceStorage::CreateWritePortal(SourceBuffers(buffers), device, token),
161  ComponentIndex(buffers));
162  }
163 
164  VTKM_CONT static auto CreateBuffers(vtkm::IdComponent componentIndex = 0,
165  const ArrayHandleType& array = ArrayHandleType{})
166  -> decltype(vtkm::cont::internal::CreateBuffers())
167  {
168  return vtkm::cont::internal::CreateBuffers(componentIndex, array);
169  }
170 }; // class Storage
171 
172 }
173 }
174 } // namespace vtkm::cont::internal
175 
176 namespace vtkm
177 {
178 namespace cont
179 {
180 
192 template <typename ArrayHandleType>
194  : public vtkm::cont::ArrayHandle<
195  typename vtkm::VecTraits<typename ArrayHandleType::ValueType>::ComponentType,
196  StorageTagExtractComponent<ArrayHandleType>>
197 {
198 public:
205 
207  VTKM_CONT
208  ArrayHandleExtractComponent(const ArrayHandleType& array, vtkm::IdComponent component)
209  : Superclass(StorageType::CreateBuffers(component, array))
210  {
211  }
212 
215  {
216  return StorageType::ComponentIndex(this->GetBuffers());
217  }
218 
220  VTKM_CONT ArrayHandleType GetArray() const
221  {
222  using BaseArray = vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
223  typename ArrayHandleType::StorageTag>;
224  return ArrayHandleType(BaseArray(StorageType::SourceBuffers(this->GetBuffers())));
225  }
226 };
227 
230 template <typename ArrayHandleType>
232  const ArrayHandleType& array,
233  vtkm::IdComponent component)
234 {
235  return ArrayHandleExtractComponent<ArrayHandleType>(array, component);
236 }
237 
238 namespace internal
239 {
240 
242 // Doxygen has trouble parsing this, and it is not important to document.
243 
244 template <typename ArrayHandleType>
245 struct ArrayExtractComponentImpl<vtkm::cont::StorageTagExtractComponent<ArrayHandleType>>
246 {
248  vtkm::IdComponent componentIndex,
249  vtkm::CopyFlag allowCopy) const
250  -> decltype(ArrayExtractComponentImpl<typename ArrayHandleType::StorageTag>{}(
251  std::declval<ArrayHandleType>(),
252  componentIndex,
253  allowCopy))
254  {
255  using ValueType = typename ArrayHandleType::ValueType;
256  using ComponentType = typename vtkm::VecTraits<ValueType>::ComponentType;
257  using FlatComponent = vtkm::VecFlat<ComponentType>;
258  constexpr vtkm::IdComponent FLAT_SUB_COMPONENTS = FlatComponent::NUM_COMPONENTS;
259  return ArrayExtractComponentImpl<typename ArrayHandleType::StorageTag>{}(
260  src.GetArray(), (src.GetComponent() * FLAT_SUB_COMPONENTS) + componentIndex, allowCopy);
261  }
262 };
263 
265 
266 } // namespace internal
267 
268 }
269 } // namespace vtkm::cont
270 
271 //=============================================================================
272 // Specializations of serialization related classes
274 namespace vtkm
275 {
276 namespace cont
277 {
278 
279 template <typename AH>
280 struct SerializableTypeString<vtkm::cont::ArrayHandleExtractComponent<AH>>
281 {
282  static VTKM_CONT const std::string& Get()
283  {
284  static std::string name = "AH_ExtractComponent<" + SerializableTypeString<AH>::Get() + ">";
285  return name;
286  }
287 };
288 
289 template <typename AH>
290 struct SerializableTypeString<
291  vtkm::cont::ArrayHandle<typename vtkm::VecTraits<typename AH::ValueType>::ComponentType,
292  vtkm::cont::StorageTagExtractComponent<AH>>>
293  : SerializableTypeString<vtkm::cont::ArrayHandleExtractComponent<AH>>
294 {
295 };
296 }
297 } // vtkm::cont
298 
299 namespace mangled_diy_namespace
300 {
301 
302 template <typename AH>
303 struct Serialization<vtkm::cont::ArrayHandleExtractComponent<AH>>
304 {
305 private:
308 
309 public:
310  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
311  {
312  vtkmdiy::save(bb, Type(obj).GetComponent());
313  vtkmdiy::save(bb, Type(obj).GetArray());
314  }
315 
316  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
317  {
318  vtkm::IdComponent component = 0;
319  AH array;
320  vtkmdiy::load(bb, component);
321  vtkmdiy::load(bb, array);
322 
323  obj = vtkm::cont::make_ArrayHandleExtractComponent(array, component);
324  }
325 };
326 
327 template <typename AH>
328 struct Serialization<
329  vtkm::cont::ArrayHandle<typename vtkm::VecTraits<typename AH::ValueType>::ComponentType,
330  vtkm::cont::StorageTagExtractComponent<AH>>>
331  : Serialization<vtkm::cont::ArrayHandleExtractComponent<AH>>
332 {
333 };
334 } // diy
336 
337 #endif // vtk_m_cont_ArrayHandleExtractComponent_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.h
ArrayExtractComponent.h
vtkm::cont::ArrayHandle< vtkm::VecTraits< ArrayHandleType::ValueType >::ComponentType, StorageTagExtractComponent< ArrayHandleType > >::GetBuffers
const std::vector< vtkm::cont::internal::Buffer > & GetBuffers() const
Returns the internal Buffer structures that hold the data.
Definition: ArrayHandle.h:721
vtkm::exec::arg::load
T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Get
auto Get(const vtkm::Tuple< Ts... > &tuple)
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:81
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:243
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::cont::ArrayHandleExtractComponent::StorageType
typename Superclass::StorageType StorageType
Definition: ArrayHandleExtractComponent.h:204
vtkm::VecTraits::ComponentType
T ComponentType
Type of the components in the vector.
Definition: VecTraits.h:71
vtkm::cont::ErrorBadType
This class is thrown when VTK-m encounters data of a type that is incompatible with the current opera...
Definition: ErrorBadType.h:25
mangled_diy_namespace
Definition: Particle.h:351
vtkm::cont::ArrayHandleExtractComponent
A fancy ArrayHandle that turns a vector array into a scalar array by slicing out a single component o...
Definition: ArrayHandleExtractComponent.h:193
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::cont::ArrayHandleExtractComponent::GetComponent
vtkm::IdComponent GetComponent() const
Get the component index being extracted from the source array.
Definition: ArrayHandleExtractComponent.h:214
vtkm::cont::StorageTagExtractComponent
Definition: ArrayHandleExtractComponent.h:84
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:224
vtkm::cont::ArrayHandleExtractComponent::ArrayHandleExtractComponent
ArrayHandleExtractComponent(const ArrayHandleType &array, vtkm::IdComponent component)
Construct an ArrayHandleExtractComponent with a given array and component.
Definition: ArrayHandleExtractComponent.h:208
vtkm::cont::ArrayHandleExtractComponent::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(vtkm::cont::ArrayHandle< typename vtkm::VecTraits< typename ArrayHandleType::ValueType >::ComponentType, StorageTagExtractComponent< ArrayHandleType > >) >::type Superclass
Definition: ArrayHandleExtractComponent.h:204
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::cont::ArrayHandleExtractComponent::GetArray
ArrayHandleType GetArray() const
Get the source array of Vecs to get a component out of.
Definition: ArrayHandleExtractComponent.h:220
StaticAssert.h
vtkm::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:17
vtkm::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:61
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
VecTraits.h
vtkm::cont::make_ArrayHandleExtractComponent
ArrayHandleExtractComponent< ArrayHandleType > make_ArrayHandleExtractComponent(const ArrayHandleType &array, vtkm::IdComponent component)
make_ArrayHandleExtractComponent is convenience function to generate an ArrayHandleExtractComponent.
Definition: ArrayHandleExtractComponent.h:231