VTK-m  2.2
ArrayHandleXGCCoordinates.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_ArrayHandleXGCCoordinates_h
11 #define vtk_m_cont_ArrayHandleXGCCoordinates_h
12 
13 #include <vtkm/Math.h>
14 
15 #include <vtkm/cont/ArrayHandle.h>
16 #include <vtkm/cont/ErrorBadType.h>
17 
19 
20 
21 namespace vtkm
22 {
23 namespace internal
24 {
25 
26 template <typename PortalType>
27 struct VTKM_ALWAYS_EXPORT ArrayPortalXGCCoordinates
28 {
30 
33  ArrayPortalXGCCoordinates()
34  : Portal()
35  , NumberOfPointsPerPlane(0)
36  , NumberOfPlanes(0)
37  , NumberOfPlanesOwned(0)
38  , PlaneStartId(0)
39  , UseCylindrical(false){};
40 
43  ArrayPortalXGCCoordinates(const PortalType& p,
44  vtkm::Id numOfPlanes,
45  vtkm::Id numOfPlanesOwned,
46  vtkm::Id planeStartId,
47  bool cylindrical = false)
48  : Portal(p)
49  , NumberOfPlanes(numOfPlanes)
50  , NumberOfPlanesOwned(numOfPlanesOwned)
51  , PlaneStartId(planeStartId)
52  , UseCylindrical(cylindrical)
53  {
54  this->NumberOfPointsPerPlane = this->Portal.GetNumberOfValues() / 2;
55  }
56 
59  vtkm::Id GetNumberOfValues() const
60  {
61  return (this->NumberOfPointsPerPlane * static_cast<vtkm::Id>(NumberOfPlanesOwned));
62  }
63 
66  ValueType Get(vtkm::Id index) const
67  {
68  const vtkm::Id realIdx = ((index * 2) % this->Portal.GetNumberOfValues()) / 2;
69  const vtkm::Id whichPlane = (index * 2) / this->Portal.GetNumberOfValues() + this->PlaneStartId;
70  return this->Get(vtkm::Id2(realIdx, whichPlane));
71  }
72 
75  ValueType Get(vtkm::Id2 index) const
76  {
77  using CompType = typename ValueType::ComponentType;
78 
79  const vtkm::Id realIdx = (index[0] * 2);
80  const vtkm::Id whichPlane = index[1];
81  const auto phi = static_cast<CompType>(whichPlane * (vtkm::TwoPi() / this->NumberOfPlanes));
82 
83  auto r = this->Portal.Get(realIdx);
84  auto z = this->Portal.Get(realIdx + 1);
85  if (this->UseCylindrical)
86  {
87  return ValueType(r, phi, z);
88  }
89  else
90  {
91  return ValueType(r * vtkm::Cos(phi), r * vtkm::Sin(phi), z);
92  }
93  }
94 
97  vtkm::Vec<ValueType, 6> GetWedge(const exec::IndicesExtrude& index) const
98  {
99  using CompType = typename ValueType::ComponentType;
100 
102  for (int j = 0; j < 2; ++j)
103  {
104  const auto phi =
105  static_cast<CompType>(index.Planes[j] * (vtkm::TwoPi() / this->NumberOfPlanes));
106  for (int i = 0; i < 3; ++i)
107  {
108  const vtkm::Id realIdx = index.PointIds[j][i] * 2;
109  auto r = this->Portal.Get(realIdx);
110  auto z = this->Portal.Get(realIdx + 1);
111  result[3 * j + i] = this->UseCylindrical
112  ? ValueType(r, phi, z)
113  : ValueType(r * vtkm::Cos(phi), r * vtkm::Sin(phi), z);
114  }
115  }
116 
117  return result;
118  }
119 
120 private:
121  PortalType Portal;
122  vtkm::Id NumberOfPointsPerPlane;
123  vtkm::Id NumberOfPlanes;
124  vtkm::Id NumberOfPlanesOwned;
125  vtkm::Id PlaneStartId;
126  bool UseCylindrical;
127 };
128 
129 }
130 } // namespace vtkm::internal
131 
132 namespace vtkm
133 {
134 namespace cont
135 {
137 {
138 };
139 
140 namespace internal
141 {
142 
143 struct XGCCoordinatesMetaData
144 {
145  vtkm::Id NumberOfPlanes = 0;
146  vtkm::Id NumberOfPlanesOwned = 0;
147  vtkm::Id PlaneStartId = -1;
148  bool UseCylindrical = false;
149 
150  XGCCoordinatesMetaData() = default;
151 
152  XGCCoordinatesMetaData(vtkm::Id numberOfPlanes,
153  vtkm::Id numberOfPlanesOwned,
154  vtkm::Id planeStartId,
155  bool useCylindrical)
156  : NumberOfPlanes(numberOfPlanes)
157  , NumberOfPlanesOwned(numberOfPlanesOwned)
158  , PlaneStartId(planeStartId)
159  , UseCylindrical(useCylindrical)
160  {
161  }
162 };
163 
164 namespace detail
165 {
166 
167 template <typename T>
168 class XGCCoordinatesStorageImpl
169 {
170  using SourceStorage = Storage<T, StorageTagBasic>; // only allow input AH to use StorageTagBasic
171  using MetaData = XGCCoordinatesMetaData;
172 
173  static MetaData& GetMetaData(const std::vector<vtkm::cont::internal::Buffer>& buffers)
174  {
175  return buffers[0].GetMetaData<MetaData>();
176  }
177 
178  // Used to skip the metadata buffer and return only actual data buffers
179  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> SourceBuffers(
180  const std::vector<vtkm::cont::internal::Buffer>& buffers)
181  {
182  return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 1, buffers.end());
183  }
184 
185 public:
186  using ReadPortalType =
187  vtkm::internal::ArrayPortalXGCCoordinates<typename SourceStorage::ReadPortalType>;
188 
189  VTKM_CONT static vtkm::IdComponent GetNumberOfComponentsFlat(
190  const std::vector<vtkm::cont::internal::Buffer>&)
191  {
192  return 3;
193  }
194 
195  VTKM_CONT static vtkm::Id GetNumberOfValues(
196  const std::vector<vtkm::cont::internal::Buffer>& buffers)
197  {
198  return GetNumberOfValuesPerPlane(buffers) * GetNumberOfPlanesOwned(buffers);
199  }
200 
201  VTKM_CONT static vtkm::Id GetNumberOfValuesPerPlane(
202  const std::vector<vtkm::cont::internal::Buffer>& buffers)
203  {
204  return SourceStorage::GetNumberOfValues(SourceBuffers(buffers)) / 2;
205  }
206 
207  VTKM_CONT static vtkm::Id GetNumberOfPlanes(
208  const std::vector<vtkm::cont::internal::Buffer>& buffers)
209  {
210  return GetMetaData(buffers).NumberOfPlanes;
211  }
212 
213  VTKM_CONT static vtkm::Id GetNumberOfPlanesOwned(
214  const std::vector<vtkm::cont::internal::Buffer>& buffers)
215  {
216  return GetMetaData(buffers).NumberOfPlanesOwned;
217  }
218 
219  VTKM_CONT static vtkm::Id GetPlaneStartId(
220  const std::vector<vtkm::cont::internal::Buffer>& buffers)
221  {
222  return GetMetaData(buffers).PlaneStartId;
223  }
224 
225  VTKM_CONT static bool GetUseCylindrical(const std::vector<vtkm::cont::internal::Buffer>& buffers)
226  {
227  return GetMetaData(buffers).UseCylindrical;
228  }
229 
230  VTKM_CONT static ReadPortalType CreateReadPortal(
231  const std::vector<vtkm::cont::internal::Buffer>& buffers,
233  vtkm::cont::Token& token)
234  {
235  return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
236  GetNumberOfPlanes(buffers),
237  GetNumberOfPlanesOwned(buffers),
238  GetPlaneStartId(buffers),
239  GetUseCylindrical(buffers));
240  }
241 
242  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
243  const vtkm::cont::ArrayHandle<T>& array,
244  vtkm::Id numberOfPlanes,
245  vtkm::Id numberOfPlanesOwned,
246  vtkm::Id planeStartId,
247  bool useCylindrical)
248  {
249  return vtkm::cont::internal::CreateBuffers(
250  MetaData(numberOfPlanes, numberOfPlanesOwned, planeStartId, useCylindrical), array);
251  }
252 
253  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
254  {
255  return CreateBuffers(vtkm::cont::ArrayHandle<T>{}, 0, 0, 0, false);
256  }
257 
258  VTKM_CONT static vtkm::cont::ArrayHandle<T> GetArrayHandle(
259  const std::vector<vtkm::cont::internal::Buffer>& buffers)
260  {
261  return vtkm::cont::ArrayHandle<T>(SourceBuffers(buffers));
262  }
263 };
264 
265 } // namespace detail
266 
267 template <>
269  : public detail::XGCCoordinatesStorageImpl<vtkm::Float32>
270 {
271 public:
274 };
275 
276 template <>
278  : public detail::XGCCoordinatesStorageImpl<vtkm::Float64>
279 {
280 public:
283 };
284 
285 } // namespace internal
286 
287 template <typename T>
289  : public vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, vtkm::cont::StorageTagXGCCoordinates>
290 {
293 
294 public:
299 
300  VTKM_CONT
302  vtkm::Id numberOfPlanes,
303  vtkm::Id numberOfPlanesOwned,
304  vtkm::Id planeStartId,
305  bool cylindrical)
306  : Superclass(StorageType::CreateBuffers(array,
307  numberOfPlanes,
308  numberOfPlanesOwned,
309  planeStartId,
310  cylindrical))
311  {
312  }
313 
315 
317  {
318  return StorageType::GetNumberOfPlanes(this->GetBuffers());
319  }
320 
322  {
323  return StorageType::GetNumberOfPlanesOwned(this->GetBuffers());
324  }
325 
327  {
328  return StorageType::GetPlaneStartId(this->GetBuffers());
329  }
330 
332  {
333  return StorageType::GetUseCylindrical(this->GetBuffers());
334  }
335 
337  {
338  return StorageType::GetNumberOfValuesPerPlane(this->GetBuffers());
339  }
340 
342  {
343  return StorageType::GetArrayHandle(this->GetBuffers());
344  }
345 };
346 
347 template <typename T>
349  const vtkm::cont::ArrayHandle<T>& arrHandle,
350  vtkm::Id numberOfPlanesOwned,
351  bool cylindrical,
352  vtkm::Id numberOfPlanes = -1,
353  vtkm::Id planeStartId = 0)
354 {
355  if (numberOfPlanes == -1)
356  {
357  numberOfPlanes = numberOfPlanesOwned;
358  }
360  arrHandle, numberOfPlanes, numberOfPlanesOwned, planeStartId, cylindrical);
361 }
362 
363 template <typename T>
365  const T* array,
366  vtkm::Id length,
367  vtkm::Id numberOfPlanesOwned,
368  bool cylindrical,
369  vtkm::Id numberOfPlanes = -1,
370  vtkm::Id planeStartId = 0,
372 {
373  if (numberOfPlanes == -1)
374  {
375  numberOfPlanes = numberOfPlanesOwned;
376  }
378  numberOfPlanes,
379  numberOfPlanesOwned,
380  planeStartId,
381  cylindrical);
382 }
383 
384 // if all planes belong to a single partition, then numberOfPlanes and planeStartId not needed
385 template <typename T>
387  const std::vector<T>& array,
388  vtkm::Id numberOfPlanesOwned,
389  bool cylindrical,
390  vtkm::Id numberOfPlanes = -1,
391  vtkm::Id planeStartId = 0,
393 {
394  if (!array.empty())
395  {
396  if (numberOfPlanes == -1)
397  {
398  numberOfPlanes = numberOfPlanesOwned;
399  }
400  return make_ArrayHandleXGCCoordinates<T>(&array.front(),
401  static_cast<vtkm::Id>(array.size()),
402  numberOfPlanesOwned,
403  cylindrical,
404  numberOfPlanes,
405  planeStartId,
406  copy);
407  }
408  else
409  {
410  // Vector empty. Just return an empty array handle.
412  }
413 }
414 
415 }
416 } // end namespace vtkm::cont
417 
418 //=============================================================================
419 // Specializations of serialization related classes
421 namespace vtkm
422 {
423 namespace cont
424 {
425 
426 template <typename T>
427 struct SerializableTypeString<vtkm::cont::ArrayHandleXGCCoordinates<T>>
428 {
429  static VTKM_CONT const std::string& Get()
430  {
431  static std::string name = "AH_XGCCoordinates<" + SerializableTypeString<T>::Get() + ">";
432  return name;
433  }
434 };
435 
436 template <typename T>
437 struct SerializableTypeString<
438  vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, vtkm::cont::StorageTagXGCCoordinates>>
439  : SerializableTypeString<vtkm::cont::ArrayHandleXGCCoordinates<T>>
440 {
441 };
442 }
443 } // vtkm::cont
444 
445 namespace mangled_diy_namespace
446 {
447 
448 template <typename T>
449 struct Serialization<vtkm::cont::ArrayHandleXGCCoordinates<T>>
450 {
451 private:
454 
455 public:
456  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
457  {
458  Type ah = obj;
459  vtkmdiy::save(bb, ah.GetNumberOfPlanes());
460  vtkmdiy::save(bb, ah.GetNumberOfPlanesOwned());
461  vtkmdiy::save(bb, ah.GetPlaneStartId());
462  vtkmdiy::save(bb, ah.GetUseCylindrical());
463  vtkmdiy::save(bb, ah.GetArray());
464  }
465 
466  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& ah)
467  {
468  vtkm::Id numberOfPlanes;
469  vtkm::Id numberOfPlanesOwned;
470  vtkm::Id planeStartId;
471  bool isCylindrical;
473 
474  vtkmdiy::load(bb, numberOfPlanes);
475  vtkmdiy::load(bb, numberOfPlanesOwned);
476  vtkmdiy::load(bb, planeStartId);
477  vtkmdiy::load(bb, isCylindrical);
478  vtkmdiy::load(bb, array);
479 
481  array, numberOfPlanes, numberOfPlanesOwned, planeStartId, isCylindrical);
482  }
483 };
484 
485 template <typename T>
486 struct Serialization<vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, vtkm::cont::StorageTagXGCCoordinates>>
487  : Serialization<vtkm::cont::ArrayHandleXGCCoordinates<T>>
488 {
489 };
490 } // diy
492 #endif
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.h
vtkm::cont::make_ArrayHandleXGCCoordinates
vtkm::cont::ArrayHandleXGCCoordinates< T > make_ArrayHandleXGCCoordinates(const vtkm::cont::ArrayHandle< T > &arrHandle, vtkm::Id numberOfPlanesOwned, bool cylindrical, vtkm::Id numberOfPlanes=-1, vtkm::Id planeStartId=0)
Definition: ArrayHandleXGCCoordinates.h:348
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
IndicesExtrude.h
vtkm::cont::ArrayHandleXGCCoordinates::ArrayHandleXGCCoordinates
ArrayHandleXGCCoordinates(const OriginalType &array, vtkm::Id numberOfPlanes, vtkm::Id numberOfPlanesOwned, vtkm::Id planeStartId, bool cylindrical)
Definition: ArrayHandleXGCCoordinates.h:301
vtkm::Vec3f_32
vtkm::Vec< vtkm::Float32, 3 > Vec3f_32
Vec3f_32 corresponds to a 3-dimensional vector of 32-bit floating point values.
Definition: Types.h:1061
vtkm::cont::ArrayHandleXGCCoordinates::GetNumberOfPointsPerPlane
vtkm::Id GetNumberOfPointsPerPlane() const
Definition: ArrayHandleXGCCoordinates.h:336
VTKM_STORAGE_NO_RESIZE
#define VTKM_STORAGE_NO_RESIZE
Definition: Storage.h:185
vtkm::cont::ArrayHandleXGCCoordinates::GetArray
OriginalType GetArray() const
Definition: ArrayHandleXGCCoordinates.h:341
mangled_diy_namespace
Definition: Particle.h:351
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::Vec3f_64
vtkm::Vec< vtkm::Float64, 3 > Vec3f_64
Vec3f_64 corresponds to a 3-dimensional vector of 64-bit floating point values.
Definition: Types.h:1067
VTKM_STORAGE_NO_WRITE_PORTAL
#define VTKM_STORAGE_NO_WRITE_PORTAL
Definition: Storage.h:198
Math.h
vtkm::cont::ArrayHandleXGCCoordinates::GetNumberOfPlanes
vtkm::Id GetNumberOfPlanes() const
Definition: ArrayHandleXGCCoordinates.h:316
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::StorageTagXGCCoordinates
Definition: ArrayHandleXGCCoordinates.h:136
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::cont::ArrayHandleXGCCoordinates::GetNumberOfPlanesOwned
vtkm::Id GetNumberOfPlanesOwned() const
Definition: ArrayHandleXGCCoordinates.h:321
vtkm::cont::ArrayHandleXGCCoordinates::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(vtkm::cont::ArrayHandle< vtkm::Vec< T, 3 >, vtkm::cont::StorageTagXGCCoordinates >) >::type Superclass
Definition: ArrayHandleXGCCoordinates.h:298
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::Vec< T, 3 >
Definition: Types.h:1016
vtkm::cont::ArrayHandleXGCCoordinates::GetUseCylindrical
bool GetUseCylindrical() const
Definition: ArrayHandleXGCCoordinates.h:331
vtkm::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::cont::ArrayHandleXGCCoordinates::StorageType
typename Superclass::StorageType StorageType
Definition: ArrayHandleXGCCoordinates.h:298
vtkm::CopyFlag::Off
@ Off
vtkm::cont::ArrayHandleXGCCoordinates::GetPlaneStartId
vtkm::Id GetPlaneStartId() const
Definition: ArrayHandleXGCCoordinates.h:326
vtkm::cont::ArrayHandleXGCCoordinates
Definition: ArrayHandleXGCCoordinates.h:288
vtkm::Sin
vtkm::Float32 Sin(vtkm::Float32 x)
Definition: Math.h:166
vtkm::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:17
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
ErrorBadType.h
vtkm::cont::make_ArrayHandle
vtkm::cont::ArrayHandleBasic< T > make_ArrayHandle(const T *array, vtkm::Id numberOfValues, vtkm::CopyFlag copy)
A convenience function for creating an ArrayHandle from a standard C array.
Definition: ArrayHandleBasic.h:270
vtkm::Cos
vtkm::Float32 Cos(vtkm::Float32 x)
Definition: Math.h:227
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53
vtkm::cont::ArrayHandleXGCCoordinates::~ArrayHandleXGCCoordinates
~ArrayHandleXGCCoordinates()
Definition: ArrayHandleXGCCoordinates.h:314