VTK-m  2.0
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::Id GetNumberOfValues(
190  const std::vector<vtkm::cont::internal::Buffer>& buffers)
191  {
192  return GetNumberOfValuesPerPlane(buffers) * GetNumberOfPlanesOwned(buffers);
193  }
194 
195  VTKM_CONT static vtkm::Id GetNumberOfValuesPerPlane(
196  const std::vector<vtkm::cont::internal::Buffer>& buffers)
197  {
198  return SourceStorage::GetNumberOfValues(SourceBuffers(buffers)) / 2;
199  }
200 
201  VTKM_CONT static vtkm::Id GetNumberOfPlanes(
202  const std::vector<vtkm::cont::internal::Buffer>& buffers)
203  {
204  return GetMetaData(buffers).NumberOfPlanes;
205  }
206 
207  VTKM_CONT static vtkm::Id GetNumberOfPlanesOwned(
208  const std::vector<vtkm::cont::internal::Buffer>& buffers)
209  {
210  return GetMetaData(buffers).NumberOfPlanesOwned;
211  }
212 
213  VTKM_CONT static vtkm::Id GetPlaneStartId(
214  const std::vector<vtkm::cont::internal::Buffer>& buffers)
215  {
216  return GetMetaData(buffers).PlaneStartId;
217  }
218 
219  VTKM_CONT static bool GetUseCylindrical(const std::vector<vtkm::cont::internal::Buffer>& buffers)
220  {
221  return GetMetaData(buffers).UseCylindrical;
222  }
223 
224  VTKM_CONT static ReadPortalType CreateReadPortal(
225  const std::vector<vtkm::cont::internal::Buffer>& buffers,
227  vtkm::cont::Token& token)
228  {
229  return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
230  GetNumberOfPlanes(buffers),
231  GetNumberOfPlanesOwned(buffers),
232  GetPlaneStartId(buffers),
233  GetUseCylindrical(buffers));
234  }
235 
236  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
237  const vtkm::cont::ArrayHandle<T>& array,
238  vtkm::Id numberOfPlanes,
239  vtkm::Id numberOfPlanesOwned,
240  vtkm::Id planeStartId,
241  bool useCylindrical)
242  {
243  return vtkm::cont::internal::CreateBuffers(
244  MetaData(numberOfPlanes, numberOfPlanesOwned, planeStartId, useCylindrical), array);
245  }
246 
247  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
248  {
249  return CreateBuffers(vtkm::cont::ArrayHandle<T>{}, 0, 0, 0, false);
250  }
251 
252  VTKM_CONT static vtkm::cont::ArrayHandle<T> GetArrayHandle(
253  const std::vector<vtkm::cont::internal::Buffer>& buffers)
254  {
255  return vtkm::cont::ArrayHandle<T>(SourceBuffers(buffers));
256  }
257 };
258 
259 } // namespace detail
260 
261 template <>
263  : public detail::XGCCoordinatesStorageImpl<vtkm::Float32>
264 {
265 public:
268 };
269 
270 template <>
272  : public detail::XGCCoordinatesStorageImpl<vtkm::Float64>
273 {
274 public:
277 };
278 
279 } // namespace internal
280 
281 template <typename T>
283  : public vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, vtkm::cont::StorageTagXGCCoordinates>
284 {
287 
288 public:
293 
294 private:
295  using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
296 
297 public:
298  VTKM_CONT
300  vtkm::Id numberOfPlanes,
301  vtkm::Id numberOfPlanesOwned,
302  vtkm::Id planeStartId,
303  bool cylindrical)
304  : Superclass(StorageType::CreateBuffers(array,
305  numberOfPlanes,
306  numberOfPlanesOwned,
307  planeStartId,
308  cylindrical))
309  {
310  }
311 
313 
315  {
316  return StorageType::GetNumberOfPlanes(this->GetBuffers());
317  }
318 
320  {
321  return StorageType::GetNumberOfPlanesOwned(this->GetBuffers());
322  }
323 
325  {
326  return StorageType::GetPlaneStartId(this->GetBuffers());
327  }
328 
330  {
331  return StorageType::GetUseCylindrical(this->GetBuffers());
332  }
333 
335  {
336  return StorageType::GetNumberOfValuesPerPlane(this->GetBuffers());
337  }
338 
340  {
341  return StorageType::GetArrayHandle(this->GetBuffers());
342  }
343 };
344 
345 template <typename T>
347  const vtkm::cont::ArrayHandle<T>& arrHandle,
348  vtkm::Id numberOfPlanesOwned,
349  bool cylindrical,
350  vtkm::Id numberOfPlanes = -1,
351  vtkm::Id planeStartId = 0)
352 {
353  if (numberOfPlanes == -1)
354  {
355  numberOfPlanes = numberOfPlanesOwned;
356  }
358  arrHandle, numberOfPlanes, numberOfPlanesOwned, planeStartId, cylindrical);
359 }
360 
361 template <typename T>
363  const T* array,
364  vtkm::Id length,
365  vtkm::Id numberOfPlanesOwned,
366  bool cylindrical,
367  vtkm::Id numberOfPlanes = -1,
368  vtkm::Id planeStartId = 0,
370 {
371  if (numberOfPlanes == -1)
372  {
373  numberOfPlanes = numberOfPlanesOwned;
374  }
376  numberOfPlanes,
377  numberOfPlanesOwned,
378  planeStartId,
379  cylindrical);
380 }
381 
382 // if all planes belong to a single partition, then numberOfPlanes and planeStartId not needed
383 template <typename T>
385  const std::vector<T>& array,
386  vtkm::Id numberOfPlanesOwned,
387  bool cylindrical,
388  vtkm::Id numberOfPlanes = -1,
389  vtkm::Id planeStartId = 0,
391 {
392  if (!array.empty())
393  {
394  if (numberOfPlanes == -1)
395  {
396  numberOfPlanes = numberOfPlanesOwned;
397  }
398  return make_ArrayHandleXGCCoordinates<T>(&array.front(),
399  static_cast<vtkm::Id>(array.size()),
400  numberOfPlanesOwned,
401  cylindrical,
402  numberOfPlanes,
403  planeStartId,
404  copy);
405  }
406  else
407  {
408  // Vector empty. Just return an empty array handle.
410  }
411 }
412 
413 }
414 } // end namespace vtkm::cont
415 
416 //=============================================================================
417 // Specializations of serialization related classes
419 namespace vtkm
420 {
421 namespace cont
422 {
423 
424 template <typename T>
425 struct SerializableTypeString<vtkm::cont::ArrayHandleXGCCoordinates<T>>
426 {
427  static VTKM_CONT const std::string& Get()
428  {
429  static std::string name = "AH_XGCCoordinates<" + SerializableTypeString<T>::Get() + ">";
430  return name;
431  }
432 };
433 
434 template <typename T>
435 struct SerializableTypeString<
436  vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, vtkm::cont::StorageTagXGCCoordinates>>
437  : SerializableTypeString<vtkm::cont::ArrayHandleXGCCoordinates<T>>
438 {
439 };
440 }
441 } // vtkm::cont
442 
443 namespace mangled_diy_namespace
444 {
445 
446 template <typename T>
447 struct Serialization<vtkm::cont::ArrayHandleXGCCoordinates<T>>
448 {
449 private:
452 
453 public:
454  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
455  {
456  Type ah = obj;
457  vtkmdiy::save(bb, ah.GetNumberOfPlanes());
458  vtkmdiy::save(bb, ah.GetNumberOfPlanesOwned());
459  vtkmdiy::save(bb, ah.GetPlaneStartId());
460  vtkmdiy::save(bb, ah.GetUseCylindrical());
461  vtkmdiy::save(bb, ah.GetArray());
462  }
463 
464  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& ah)
465  {
466  vtkm::Id numberOfPlanes;
467  vtkm::Id numberOfPlanesOwned;
468  vtkm::Id planeStartId;
469  bool isCylindrical;
471 
472  vtkmdiy::load(bb, numberOfPlanes);
473  vtkmdiy::load(bb, numberOfPlanesOwned);
474  vtkmdiy::load(bb, planeStartId);
475  vtkmdiy::load(bb, isCylindrical);
476  vtkmdiy::load(bb, array);
477 
479  array, numberOfPlanes, numberOfPlanesOwned, planeStartId, isCylindrical);
480  }
481 };
482 
483 template <typename T>
484 struct Serialization<vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, vtkm::cont::StorageTagXGCCoordinates>>
485  : Serialization<vtkm::cont::ArrayHandleXGCCoordinates<T>>
486 {
487 };
488 } // diy
490 #endif
vtkm::cont::make_ArrayHandle
VTKM_CONT 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:217
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
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:346
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::ArrayHandleXGCCoordinates::GetArray
VTKM_CONT OriginalType GetArray() const
Definition: ArrayHandleXGCCoordinates.h:339
vtkm::Cos
VTKM_EXEC_CONT vtkm::Float32 Cos(vtkm::Float32 x)
Compute the cosine of x.
Definition: Math.h:269
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:226
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::cont::ArrayHandleXGCCoordinates::GetPlaneStartId
VTKM_CONT vtkm::Id GetPlaneStartId() const
Definition: ArrayHandleXGCCoordinates.h:324
vtkm::cont::ArrayHandleXGCCoordinates::GetNumberOfPlanes
VTKM_CONT vtkm::Id GetNumberOfPlanes() const
Definition: ArrayHandleXGCCoordinates.h:314
IndicesExtrude.h
vtkm::cont::ArrayHandleXGCCoordinates::GetNumberOfPointsPerPlane
VTKM_CONT vtkm::Id GetNumberOfPointsPerPlane() const
Definition: ArrayHandleXGCCoordinates.h:334
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:1020
vtkm::Sin
VTKM_EXEC_CONT vtkm::Float32 Sin(vtkm::Float32 x)
Compute the sine of x.
Definition: Math.h:210
VTKM_STORAGE_NO_RESIZE
#define VTKM_STORAGE_NO_RESIZE
Definition: Storage.h:185
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::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:1026
VTKM_STORAGE_NO_WRITE_PORTAL
#define VTKM_STORAGE_NO_WRITE_PORTAL
Definition: Storage.h:198
Math.h
vtkm::cont::ArrayHandleXGCCoordinates::GetUseCylindrical
VTKM_CONT bool GetUseCylindrical() const
Definition: ArrayHandleXGCCoordinates.h:329
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::StorageTagXGCCoordinates
Definition: ArrayHandleXGCCoordinates.h:136
vtkm::cont::ArrayHandleXGCCoordinates::StorageType
vtkm::cont::internal::Storage< ValueType, StorageTag > StorageType
Definition: ArrayHandleXGCCoordinates.h:295
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::Vec< T, 3 >
Definition: Types.h:975
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::CopyFlag::Off
@ Off
vtkm::cont::ArrayHandleXGCCoordinates
Definition: ArrayHandleXGCCoordinates.h:282
vtkm::CopyFlag
CopyFlag
Definition: Flags.h:16
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
ErrorBadType.h
vtkm::cont::ArrayHandleXGCCoordinates::GetNumberOfPlanesOwned
VTKM_CONT vtkm::Id GetNumberOfPlanesOwned() const
Definition: ArrayHandleXGCCoordinates.h:319
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53
vtkm::cont::ArrayHandleXGCCoordinates::~ArrayHandleXGCCoordinates
~ArrayHandleXGCCoordinates()
Definition: ArrayHandleXGCCoordinates.h:312
vtkm::cont::ArrayHandleXGCCoordinates::ArrayHandleXGCCoordinates
VTKM_CONT ArrayHandleXGCCoordinates(const OriginalType &array, vtkm::Id numberOfPlanes, vtkm::Id numberOfPlanesOwned, vtkm::Id planeStartId, bool cylindrical)
Definition: ArrayHandleXGCCoordinates.h:299