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