VTK-m  2.0
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 SourceStorageTag = typename ArrayHandleType::StorageTag;
98  using SourceStorage = vtkm::cont::internal::Storage<SourceValueType, SourceStorageTag>;
99 
100 public:
101  VTKM_CONT static vtkm::IdComponent ComponentIndex(
102  const std::vector<vtkm::cont::internal::Buffer>& buffers)
103  {
104  return buffers[0].GetMetaData<vtkm::IdComponent>();
105  }
106 
107  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> SourceBuffers(
108  const std::vector<vtkm::cont::internal::Buffer>& buffers)
109  {
110  return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 1, buffers.end());
111  }
112 
113  using ReadPortalType =
114  vtkm::internal::ArrayPortalExtractComponent<typename SourceStorage::ReadPortalType>;
115  using WritePortalType =
116  vtkm::internal::ArrayPortalExtractComponent<typename SourceStorage::WritePortalType>;
117 
118  VTKM_CONT static vtkm::Id GetNumberOfValues(
119  const std::vector<vtkm::cont::internal::Buffer>& buffers)
120  {
121  return SourceStorage::GetNumberOfValues(SourceBuffers(buffers));
122  }
123 
124  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>&,
125  const ValueType&,
126  vtkm::Id,
127  vtkm::Id,
129  {
130  throw vtkm::cont::ErrorBadType("Fill not supported for ArrayHandleExtractComponent.");
131  }
132 
133  VTKM_CONT static void ResizeBuffers(vtkm::Id numValues,
134  const std::vector<vtkm::cont::internal::Buffer>& buffers,
135  vtkm::CopyFlag preserve,
136  vtkm::cont::Token& token)
137  {
138  SourceStorage::ResizeBuffers(numValues, SourceBuffers(buffers), preserve, token);
139  }
140 
141  VTKM_CONT static ReadPortalType CreateReadPortal(
142  const std::vector<vtkm::cont::internal::Buffer>& buffers,
144  vtkm::cont::Token& token)
145  {
146  return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
147  ComponentIndex(buffers));
148  }
149 
150  VTKM_CONT static WritePortalType CreateWritePortal(
151  const std::vector<vtkm::cont::internal::Buffer>& buffers,
153  vtkm::cont::Token& token)
154  {
155  return WritePortalType(SourceStorage::CreateWritePortal(SourceBuffers(buffers), device, token),
156  ComponentIndex(buffers));
157  }
158 
159  VTKM_CONT static auto CreateBuffers(vtkm::IdComponent componentIndex = 0,
160  const ArrayHandleType& array = ArrayHandleType{})
161  -> decltype(vtkm::cont::internal::CreateBuffers())
162  {
163  return vtkm::cont::internal::CreateBuffers(componentIndex, array);
164  }
165 }; // class Storage
166 
167 }
168 }
169 } // namespace vtkm::cont::internal
170 
171 namespace vtkm
172 {
173 namespace cont
174 {
175 
187 template <typename ArrayHandleType>
189  : public vtkm::cont::ArrayHandle<
190  typename vtkm::VecTraits<typename ArrayHandleType::ValueType>::ComponentType,
191  StorageTagExtractComponent<ArrayHandleType>>
192 {
193 public:
200 
201 protected:
202  using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
203 
204 public:
205  VTKM_CONT
206  ArrayHandleExtractComponent(const ArrayHandleType& array, vtkm::IdComponent component)
207  : Superclass(StorageType::CreateBuffers(component, array))
208  {
209  }
210 
212  {
213  return StorageType::ComponentIndex(this->GetBuffers());
214  }
215 
216  VTKM_CONT ArrayHandleType GetArray() const
217  {
218  using BaseArray = vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
219  typename ArrayHandleType::StorageTag>;
220  return ArrayHandleType(BaseArray(StorageType::SourceBuffers(this->GetBuffers())));
221  }
222 };
223 
226 template <typename ArrayHandleType>
228  const ArrayHandleType& array,
229  vtkm::IdComponent component)
230 {
231  return ArrayHandleExtractComponent<ArrayHandleType>(array, component);
232 }
233 
234 namespace internal
235 {
236 
237 template <typename ArrayHandleType>
238 struct ArrayExtractComponentImpl<vtkm::cont::StorageTagExtractComponent<ArrayHandleType>>
239 {
241  vtkm::IdComponent componentIndex,
242  vtkm::CopyFlag allowCopy) const
243  -> decltype(ArrayExtractComponentImpl<typename ArrayHandleType::StorageTag>{}(
244  std::declval<ArrayHandleType>(),
245  componentIndex,
246  allowCopy))
247  {
248  using ValueType = typename ArrayHandleType::ValueType;
249  using ComponentType = typename vtkm::VecTraits<ValueType>::ComponentType;
250  using FlatComponent = vtkm::VecFlat<ComponentType>;
251  constexpr vtkm::IdComponent FLAT_SUB_COMPONENTS = FlatComponent::NUM_COMPONENTS;
252  return ArrayExtractComponentImpl<typename ArrayHandleType::StorageTag>{}(
253  src.GetArray(), (src.GetComponent() * FLAT_SUB_COMPONENTS) + componentIndex, allowCopy);
254  }
255 };
256 
257 } // namespace internal
258 
259 }
260 } // namespace vtkm::cont
261 
262 //=============================================================================
263 // Specializations of serialization related classes
265 namespace vtkm
266 {
267 namespace cont
268 {
269 
270 template <typename AH>
271 struct SerializableTypeString<vtkm::cont::ArrayHandleExtractComponent<AH>>
272 {
273  static VTKM_CONT const std::string& Get()
274  {
275  static std::string name = "AH_ExtractComponent<" + SerializableTypeString<AH>::Get() + ">";
276  return name;
277  }
278 };
279 
280 template <typename AH>
281 struct SerializableTypeString<
282  vtkm::cont::ArrayHandle<typename vtkm::VecTraits<typename AH::ValueType>::ComponentType,
283  vtkm::cont::StorageTagExtractComponent<AH>>>
284  : SerializableTypeString<vtkm::cont::ArrayHandleExtractComponent<AH>>
285 {
286 };
287 }
288 } // vtkm::cont
289 
290 namespace mangled_diy_namespace
291 {
292 
293 template <typename AH>
294 struct Serialization<vtkm::cont::ArrayHandleExtractComponent<AH>>
295 {
296 private:
299 
300 public:
301  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
302  {
303  vtkmdiy::save(bb, Type(obj).GetComponent());
304  vtkmdiy::save(bb, Type(obj).GetArray());
305  }
306 
307  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
308  {
309  vtkm::IdComponent component = 0;
310  AH array;
311  vtkmdiy::load(bb, component);
312  vtkmdiy::load(bb, array);
313 
314  obj = vtkm::cont::make_ArrayHandleExtractComponent(array, component);
315  }
316 };
317 
318 template <typename AH>
319 struct Serialization<
320  vtkm::cont::ArrayHandle<typename vtkm::VecTraits<typename AH::ValueType>::ComponentType,
321  vtkm::cont::StorageTagExtractComponent<AH>>>
322  : Serialization<vtkm::cont::ArrayHandleExtractComponent<AH>>
323 {
324 };
325 } // diy
327 
328 #endif // vtk_m_cont_ArrayHandleExtractComponent_h
vtkm::cont::ArrayHandle< vtkm::VecTraits< ArrayHandleType::ValueType >::ComponentType, StorageTagExtractComponent< ArrayHandleType > >::GetBuffers
const VTKM_CONT std::vector< vtkm::cont::internal::Buffer > & GetBuffers() const
Returns the internal Buffer structures that hold the data.
Definition: ArrayHandle.h:696
vtkm::cont::make_ArrayHandleExtractComponent
VTKM_CONT ArrayHandleExtractComponent< ArrayHandleType > make_ArrayHandleExtractComponent(const ArrayHandleType &array, vtkm::IdComponent component)
make_ArrayHandleExtractComponent is convenience function to generate an ArrayHandleExtractComponent.
Definition: ArrayHandleExtractComponent.h:227
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
ArrayExtractComponent.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::ArrayHandleExtractComponent::GetComponent
VTKM_CONT vtkm::IdComponent GetComponent() const
Definition: ArrayHandleExtractComponent.h:211
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Get
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT auto Get(const vtkm::Tuple< Ts... > &tuple) -> decltype(tuple.template Get< Index >())
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:83
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::cont::ArrayHandleExtractComponent::ArrayHandleExtractComponent
VTKM_CONT ArrayHandleExtractComponent(const ArrayHandleType &array, vtkm::IdComponent component)
Definition: ArrayHandleExtractComponent.h:206
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:331
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::exec::arg::load
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
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:188
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::cont::StorageTagExtractComponent
Definition: ArrayHandleExtractComponent.h:84
vtkm::cont::ArrayHandleExtractComponent::GetArray
VTKM_CONT ArrayHandleType GetArray() const
Definition: ArrayHandleExtractComponent.h:216
vtkm::cont::ArrayHandleExtractComponent::StorageType
vtkm::cont::internal::Storage< ValueType, StorageTag > StorageType
Definition: ArrayHandleExtractComponent.h:202
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:224
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
StaticAssert.h
vtkm::VecTraits::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:73
vtkm::CopyFlag
CopyFlag
Definition: Flags.h:16
vtkm::VecTraits
The VecTraits class gives several static members that define how to use a given type as a vector.
Definition: VecTraits.h:66
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
vtkm::cont::ArrayHandleExtractComponent::VTKM_ARRAY_HANDLE_SUBCLASS
VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleExtractComponent,(ArrayHandleExtractComponent< ArrayHandleType >),(vtkm::cont::ArrayHandle< typename vtkm::VecTraits< typename ArrayHandleType::ValueType >::ComponentType, StorageTagExtractComponent< ArrayHandleType >>))
VecTraits.h