VTK-m  2.1
Types.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_Types_h
11 #define vtk_m_Types_h
12 
15 
16 #include <vtkm/Assert.h>
17 #include <vtkm/StaticAssert.h>
18 
19 #include <cstdint>
20 #include <iostream>
21 #include <type_traits>
22 
149 namespace vtkm
150 {
151 //*****************************************************************************
152 // Typedefs for basic types.
153 //*****************************************************************************
154 
157 using Float32 = float;
158 
161 using Float64 = double;
162 
165 using Int8 = int8_t;
166 
169 using UInt8 = uint8_t;
170 
173 using Int16 = int16_t;
174 
177 using UInt16 = uint16_t;
178 
181 using Int32 = int32_t;
182 
185 using UInt32 = uint32_t;
186 
195 
199 
200 //In this order so that we exactly match the logic that exists in VTK
201 #if (VTKM_SIZE_LONG_LONG == 8) || defined(VTKM_DOXYGEN_ONLY)
202 using Int64 = signed long long;
207 using UInt64 = unsigned long long;
208 #define VTKM_UNUSED_INT_TYPE long
209 #elif VTKM_SIZE_LONG == 8
210 using Int64 = signed long;
215 using UInt64 = unsigned long;
216 #define VTKM_UNUSED_INT_TYPE long long
217 #else
218 #error Could not find a 64-bit integer.
219 #endif
220 
226 #ifdef VTKM_USE_64BIT_IDS
227 using Id = vtkm::Int64;
228 #else
229 using Id = vtkm::Int32;
230 #endif
231 
233 #ifdef VTKM_USE_DOUBLE_PRECISION
235 #else
237 #endif
238 
239 
240 namespace internal
241 {
242 
244 struct NullType
245 {
246 };
247 
248 } // namespace internal
249 
250 // Disable conversion warnings for Add, Subtract, Multiply, Divide on GCC only.
251 // GCC creates false positive warnings for signed/unsigned char* operations.
252 // This occurs because the values are implicitly casted up to int's for the
253 // operation, and than casted back down to char's when return.
254 // This causes a false positive warning, even when the values is within
255 // the value types range
256 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
257 #pragma GCC diagnostic push
258 #pragma GCC diagnostic ignored "-Wconversion"
259 #endif // gcc || clang
260 struct Add
261 {
262  template <typename T, typename U>
263  inline VTKM_EXEC_CONT auto operator()(const T& a, const U& b) const -> decltype(a + b)
264  {
265  return a + b;
266  }
267 
268  // If both arguments are short integers, explicitly cast the result back to the
269  // type to avoid narrowing conversion warnings from operations that promote to
270  // integers.
271  template <typename T>
272  inline VTKM_EXEC_CONT
273  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
274  operator()(T a, T b) const
275  {
276  return static_cast<T>(a + b);
277  }
278 };
279 
280 struct Subtract
281 {
282  template <typename T, typename U>
283  inline VTKM_EXEC_CONT auto operator()(const T& a, const U& b) const -> decltype(a - b)
284  {
285  return a - b;
286  }
287 
288  // If both arguments are short integers, explicitly cast the result back to the
289  // type to avoid narrowing conversion warnings from operations that promote to
290  // integers.
291  template <typename T>
292  inline VTKM_EXEC_CONT
293  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
294  operator()(T a, T b) const
295  {
296  return static_cast<T>(a - b);
297  }
298 };
299 
300 struct Multiply
301 {
302  template <typename T, typename U>
303  inline VTKM_EXEC_CONT auto operator()(const T& a, const U& b) const -> decltype(a * b)
304  {
305  return a * b;
306  }
307 
308  // If both arguments are short integers, explicitly cast the result back to the
309  // type to avoid narrowing conversion warnings from operations that promote to
310  // integers.
311  template <typename T>
312  inline VTKM_EXEC_CONT
313  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
314  operator()(T a, T b) const
315  {
316  return static_cast<T>(a * b);
317  }
318 };
319 
320 struct Divide
321 {
322  template <typename T, typename U>
323  inline VTKM_EXEC_CONT auto operator()(const T& a, const U& b) const -> decltype(a / b)
324  {
325  return a / b;
326  }
327 
328  // If both arguments are short integers, explicitly cast the result back to the
329  // type to avoid narrowing conversion warnings from operations that promote to
330  // integers.
331  template <typename T>
332  inline VTKM_EXEC_CONT
333  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
334  operator()(T a, T b) const
335  {
336  return static_cast<T>(a / b);
337  }
338 };
339 
340 struct Negate
341 {
342  template <typename T>
343  inline VTKM_EXEC_CONT T operator()(const T& x) const
344  {
345  return T(-x);
346  }
347 };
348 
349 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
350 #pragma GCC diagnostic pop
351 #endif // gcc || clang
352 
353 //-----------------------------------------------------------------------------
354 
355 // Pre declaration
356 template <typename T, vtkm::IdComponent Size>
358 
359 template <typename T>
361 
362 template <typename T>
364 
365 namespace detail
366 {
367 
370 // Disable conversion warnings for Add, Subtract, Multiply, Divide on GCC only.
371 // GCC creates false positive warnings for signed/unsigned char* operations.
372 // This occurs because the values are implicitly casted up to int's for the
373 // operation, and than casted back down to char's when return.
374 // This causes a false positive warning, even when the values is within
375 // the value types range
376 //
377 // NVCC 7.5 and below does not recognize this pragma inside of class bodies,
378 // so put them before entering the class.
379 //
380 #if (defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8))
381 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
382 #pragma GCC diagnostic push
383 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
384 #pragma GCC diagnostic ignored "-Wpragmas"
385 #pragma GCC diagnostic ignored "-Wconversion"
386 #pragma GCC diagnostic ignored "-Wfloat-conversion"
387 #endif // gcc || clang
388 #endif // use cuda < 8
389 template <typename T, typename DerivedClass>
390 class VTKM_ALWAYS_EXPORT VecBaseCommon
391 {
392 public:
393  using ComponentType = T;
394 
395 protected:
396  VecBaseCommon() = default;
397 
399  const DerivedClass& Derived() const { return *static_cast<const DerivedClass*>(this); }
400 
402  DerivedClass& Derived() { return *static_cast<DerivedClass*>(this); }
403 
404 private:
405  // Only for internal use
407  inline vtkm::IdComponent NumComponents() const { return this->Derived().GetNumberOfComponents(); }
408 
409  // Only for internal use
411  inline const T& Component(vtkm::IdComponent index) const { return this->Derived()[index]; }
412 
413  // Only for internal use
415  inline T& Component(vtkm::IdComponent index) { return this->Derived()[index]; }
416 
417 public:
418  template <vtkm::IdComponent OtherSize>
419  VTKM_EXEC_CONT void CopyInto(vtkm::Vec<ComponentType, OtherSize>& dest) const
420  {
421  for (vtkm::IdComponent index = 0; (index < this->NumComponents()) && (index < OtherSize);
422  index++)
423  {
424  dest[index] = this->Component(index);
425  }
426  }
427 
428  // Only works with Vec-like objects with operator[] and GetNumberOfComponents().
429  template <typename OtherVecType>
430  VTKM_EXEC_CONT DerivedClass& operator=(const OtherVecType& src)
431  {
432  VTKM_ASSERT(this->NumComponents() == src.GetNumberOfComponents());
433  for (vtkm::IdComponent i = 0; i < this->NumComponents(); ++i)
434  {
435  this->Component(i) = src[i];
436  }
437  return this->Derived();
438  }
439 
441  bool operator==(const DerivedClass& other) const
442  {
443  bool equal = true;
444  for (vtkm::IdComponent i = 0; i < this->NumComponents() && equal; ++i)
445  {
446  equal = (this->Component(i) == other[i]);
447  }
448  return equal;
449  }
450 
452  bool operator<(const DerivedClass& other) const
453  {
454  for (vtkm::IdComponent i = 0; i < this->NumComponents(); ++i)
455  {
456  // ignore equals as that represents check next value
457  if (this->Component(i) < other[i])
458  {
459  return true;
460  }
461  else if (other[i] < this->Component(i))
462  {
463  return false;
464  }
465  } // if all same we are not less
466 
467  return false;
468  }
469 
471  bool operator!=(const DerivedClass& other) const { return !(this->operator==(other)); }
472 
473 #if (!(defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8)))
474 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
475 #pragma GCC diagnostic push
476 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
477 #pragma GCC diagnostic ignored "-Wpragmas"
478 #pragma GCC diagnostic ignored "-Wconversion"
479 #pragma GCC diagnostic ignored "-Wfloat-conversion"
480 #endif // gcc || clang
481 #endif // not using cuda < 8
482 
483  template <vtkm::IdComponent Size>
485  const vtkm::Vec<ComponentType, Size>& other) const
486  {
487  VTKM_ASSERT(Size == this->NumComponents());
489  for (vtkm::IdComponent i = 0; i < Size; ++i)
490  {
491  result[i] = this->Component(i) + other[i];
492  }
493  return result;
494  }
495 
496  template <typename OtherClass>
497  inline VTKM_EXEC_CONT DerivedClass& operator+=(const OtherClass& other)
498  {
499  VTKM_ASSERT(this->NumComponents() == other.GetNumberOfComponents());
500  for (vtkm::IdComponent i = 0; i < this->NumComponents(); ++i)
501  {
502  this->Component(i) += other[i];
503  }
504  return this->Derived();
505  }
506 
507  template <vtkm::IdComponent Size>
509  const vtkm::Vec<ComponentType, Size>& other) const
510  {
511  VTKM_ASSERT(Size == this->NumComponents());
513  for (vtkm::IdComponent i = 0; i < Size; ++i)
514  {
515  result[i] = this->Component(i) - other[i];
516  }
517  return result;
518  }
519 
520  template <typename OtherClass>
521  inline VTKM_EXEC_CONT DerivedClass& operator-=(const OtherClass& other)
522  {
523  VTKM_ASSERT(this->NumComponents() == other.GetNumberOfComponents());
524  for (vtkm::IdComponent i = 0; i < this->NumComponents(); ++i)
525  {
526  this->Component(i) -= other[i];
527  }
528  return this->Derived();
529  }
530 
531  template <vtkm::IdComponent Size>
533  const vtkm::Vec<ComponentType, Size>& other) const
534  {
536  for (vtkm::IdComponent i = 0; i < Size; ++i)
537  {
538  result[i] = this->Component(i) * other[i];
539  }
540  return result;
541  }
542 
543  template <typename OtherClass>
544  inline VTKM_EXEC_CONT DerivedClass& operator*=(const OtherClass& other)
545  {
546  VTKM_ASSERT(this->NumComponents() == other.GetNumberOfComponents());
547  for (vtkm::IdComponent i = 0; i < this->NumComponents(); ++i)
548  {
549  this->Component(i) *= other[i];
550  }
551  return this->Derived();
552  }
553 
554  template <vtkm::IdComponent Size>
556  const vtkm::Vec<ComponentType, Size>& other) const
557  {
559  for (vtkm::IdComponent i = 0; i < Size; ++i)
560  {
561  result[i] = this->Component(i) / other[i];
562  }
563  return result;
564  }
565 
566  template <typename OtherClass>
567  VTKM_EXEC_CONT DerivedClass& operator/=(const OtherClass& other)
568  {
569  VTKM_ASSERT(this->NumComponents() == other.GetNumberOfComponents());
570  for (vtkm::IdComponent i = 0; i < this->NumComponents(); ++i)
571  {
572  this->Component(i) /= other[i];
573  }
574  return this->Derived();
575  }
576 
577 #if (!(defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8)))
578 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
579 #pragma GCC diagnostic pop
580 #endif // gcc || clang
581 #endif // not using cuda < 8
582 
584  ComponentType* GetPointer() { return &this->Component(0); }
585 
587  const ComponentType* GetPointer() const { return &this->Component(0); }
588 };
589 
590 
593 template <typename T, vtkm::IdComponent Size, typename DerivedClass>
594 class VTKM_ALWAYS_EXPORT VecBase : public vtkm::detail::VecBaseCommon<T, DerivedClass>
595 {
596 public:
597  using ComponentType = T;
598  static constexpr vtkm::IdComponent NUM_COMPONENTS = Size;
599 
600  VecBase() = default;
601 
602  // The enable_if predicate will disable this constructor for Size=1 so that
603  // the variadic constructor constexpr VecBase(T, Ts&&...) is called instead.
605  template <vtkm::IdComponent Size2 = Size, typename std::enable_if<Size2 != 1, int>::type = 0>
606  VTKM_EXEC_CONT explicit VecBase(const ComponentType& value)
607  {
608  for (vtkm::IdComponent i = 0; i < Size; ++i)
609  {
610  this->Components[i] = value;
611  }
612  }
613 
615  template <typename... Ts>
616  VTKM_EXEC_CONT constexpr VecBase(ComponentType value0, Ts&&... values)
617  : Components{ value0, values... }
618  {
619  VTKM_STATIC_ASSERT(sizeof...(Ts) + 1 == Size);
620  }
621 
624  VecBase(std::initializer_list<ComponentType> values)
625  {
626  ComponentType* dest = this->Components;
627  auto src = values.begin();
628  if (values.size() == 1)
629  {
630  for (vtkm::IdComponent i = 0; i < Size; ++i)
631  {
632  this->Components[i] = *src;
633  ++dest;
634  }
635  }
636  else
637  {
638  VTKM_ASSERT((values.size() == NUM_COMPONENTS) &&
639  "Vec object initialized wrong number of components.");
640  for (; src != values.end(); ++src)
641  {
642  *dest = *src;
643  ++dest;
644  }
645  }
646  }
647 
648 #if (!(defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8)))
649 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
650 #pragma GCC diagnostic push
651 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
652 #pragma GCC diagnostic ignored "-Wpragmas"
653 #pragma GCC diagnostic ignored "-Wconversion"
654 #pragma GCC diagnostic ignored "-Wfloat-conversion"
655 #endif // gcc || clang
656 #endif //not using cuda < 8
657 #if defined(VTKM_MSVC)
658 #pragma warning(push)
659 #pragma warning(disable : 4244)
660 #endif
661 
663  template <typename OtherValueType, typename OtherDerivedType>
664  VTKM_EXEC_CONT explicit VecBase(const VecBase<OtherValueType, Size, OtherDerivedType>& src)
665  {
666  //DO NOT CHANGE THIS AND THE ABOVE PRAGMA'S UNLESS YOU FULLY UNDERSTAND THE
667  //ISSUE https://gitlab.kitware.com/vtk/vtk-m/-/issues/221
668  for (vtkm::IdComponent i = 0; i < Size; ++i)
669  {
670  this->Components[i] = src[i];
671  }
672  }
673 
674 public:
675  inline VTKM_EXEC_CONT constexpr vtkm::IdComponent GetNumberOfComponents() const
676  {
677  return NUM_COMPONENTS;
678  }
679 
680  inline VTKM_EXEC_CONT constexpr const ComponentType& operator[](vtkm::IdComponent idx) const
681  {
682  return this->Components[idx];
683  }
684 
685  inline VTKM_EXEC_CONT ComponentType& operator[](vtkm::IdComponent idx)
686  {
687  VTKM_ASSERT(idx >= 0);
688  VTKM_ASSERT(idx < NUM_COMPONENTS);
689  return this->Components[idx];
690  }
691 
693  template <typename OtherComponentType, typename OtherClass>
694  inline VTKM_EXEC_CONT DerivedClass
695  operator+(const VecBaseCommon<OtherComponentType, OtherClass>& other) const
696  {
697  const OtherClass& other_derived = static_cast<const OtherClass&>(other);
698  VTKM_ASSERT(NUM_COMPONENTS == other_derived.GetNumberOfComponents());
699 
700  DerivedClass result;
701  for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; ++i)
702  {
703  result[i] = this->Components[i] + static_cast<ComponentType>(other_derived[i]);
704  }
705  return result;
706  }
707 
709  template <typename OtherComponentType, typename OtherClass>
710  inline VTKM_EXEC_CONT DerivedClass
711  operator-(const VecBaseCommon<OtherComponentType, OtherClass>& other) const
712  {
713  const OtherClass& other_derived = static_cast<const OtherClass&>(other);
714  VTKM_ASSERT(NUM_COMPONENTS == other_derived.GetNumberOfComponents());
715 
716  DerivedClass result;
717  for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; ++i)
718  {
719  result[i] = this->Components[i] - static_cast<ComponentType>(other_derived[i]);
720  }
721  return result;
722  }
723 
725  template <typename OtherComponentType, typename OtherClass>
726  inline VTKM_EXEC_CONT DerivedClass
727  operator*(const VecBaseCommon<OtherComponentType, OtherClass>& other) const
728  {
729  const OtherClass& other_derived = static_cast<const OtherClass&>(other);
730  VTKM_ASSERT(NUM_COMPONENTS == other_derived.GetNumberOfComponents());
731 
732  DerivedClass result;
733  for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; ++i)
734  {
735  result[i] = this->Components[i] * static_cast<ComponentType>(other_derived[i]);
736  }
737  return result;
738  }
739 
741  template <typename OtherComponentType, typename OtherClass>
742  inline VTKM_EXEC_CONT DerivedClass
743  operator/(const VecBaseCommon<OtherComponentType, OtherClass>& other) const
744  {
745  const OtherClass& other_derived = static_cast<const OtherClass&>(other);
746  VTKM_ASSERT(NUM_COMPONENTS == other_derived.GetNumberOfComponents());
747 
748  DerivedClass result;
749  for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; ++i)
750  {
751  result[i] = this->Components[i] / static_cast<ComponentType>(other_derived[i]);
752  }
753  return result;
754  }
755 
756 #if (!(defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8)))
757 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
758 #pragma GCC diagnostic pop
759 #endif // gcc || clang
760 #endif // not using cuda < 8
761 #if defined(VTKM_MSVC)
762 #pragma warning(pop)
763 #endif
764 
765 protected:
766  ComponentType Components[NUM_COMPONENTS];
767 };
768 
769 #if (defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8))
770 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
771 #pragma GCC diagnostic pop
772 #endif // gcc || clang
773 #endif // use cuda < 8
774 
777 template <typename T, typename DerivedClass>
778 class VTKM_ALWAYS_EXPORT VecCBase : public vtkm::detail::VecBaseCommon<T, DerivedClass>
779 {
780 protected:
782  VecCBase() {}
783 };
784 
785 } // namespace detail
786 
787 //-----------------------------------------------------------------------------
788 
804 template <typename T, vtkm::IdComponent Size>
805 class VTKM_ALWAYS_EXPORT Vec : public detail::VecBase<T, Size, Vec<T, Size>>
806 {
807  using Superclass = detail::VecBase<T, Size, Vec<T, Size>>;
808 
809 public:
810 #ifdef VTKM_DOXYGEN_ONLY
811  using ComponentType = T;
812  static constexpr vtkm::IdComponent NUM_COMPONENTS = Size;
813 #endif
814 
815  using Superclass::Superclass;
816  Vec() = default;
817 #if defined(_MSC_VER) && _MSC_VER < 1910
818  template <typename... Ts>
819  constexpr Vec(T value, Ts&&... values)
820  : Superclass(value, std::forward<Ts>(values)...)
821  {
822  }
823 #endif
824 
825  inline VTKM_EXEC_CONT void CopyInto(Vec<T, Size>& dest) const { dest = *this; }
826 };
827 
828 //-----------------------------------------------------------------------------
829 // Specializations for common small tuples. We implement them a bit specially.
830 
831 // A vector of size 0 cannot use VecBase because it will try to create a
832 // zero length array which troubles compilers. Vecs of size 0 are a bit
833 // pointless but might occur in some generic functions or classes.
834 template <typename T>
836 {
837 public:
838  using ComponentType = T;
839  static constexpr vtkm::IdComponent NUM_COMPONENTS = 0;
840 
841  Vec() = default;
842  VTKM_EXEC_CONT explicit Vec(const ComponentType&) {}
843 
844  template <typename OtherType>
846  {
847  }
848 
851  {
852  return *this;
853  }
854 
856  {
857  return NUM_COMPONENTS;
858  }
859 
862  {
863  return ComponentType();
864  }
865 
867  bool operator==(const Vec<T, NUM_COMPONENTS>& vtkmNotUsed(other)) const { return true; }
869  bool operator!=(const Vec<T, NUM_COMPONENTS>& vtkmNotUsed(other)) const { return false; }
870 };
871 
872 // Vectors of size 1 should implicitly convert between the scalar and the
873 // vector. Otherwise, it should behave the same.
874 template <typename T>
875 class VTKM_ALWAYS_EXPORT Vec<T, 1> : public detail::VecBase<T, 1, Vec<T, 1>>
876 {
877  using Superclass = detail::VecBase<T, 1, Vec<T, 1>>;
878 
879 public:
880  Vec() = default;
881  VTKM_EXEC_CONT constexpr Vec(const T& value)
882  : Superclass(value)
883  {
884  }
885 
886  template <typename OtherType>
888  : Superclass(src)
889  {
890  }
891 };
892 
893 //-----------------------------------------------------------------------------
894 // Specializations for common tuple sizes (with special names).
895 
896 template <typename T>
897 class VTKM_ALWAYS_EXPORT Vec<T, 2> : public detail::VecBase<T, 2, Vec<T, 2>>
898 {
899  using Superclass = detail::VecBase<T, 2, Vec<T, 2>>;
900 
901 public:
902  Vec() = default;
903  VTKM_EXEC_CONT Vec(const T& value)
904  : Superclass(value)
905  {
906  }
907 
908  template <typename OtherType>
910  : Superclass(src)
911  {
912  }
913 
915  constexpr Vec(const T& x, const T& y)
916  : Superclass(x, y)
917  {
918  }
919 };
920 
924 
928 
935 
941 
947 
953 
959 
965 
971 
977 
982 #ifdef VTKM_USE_64BIT_IDS
984 #else
986 #endif
987 
993 
999 
1005 
1011 
1012 template <typename T>
1013 class VTKM_ALWAYS_EXPORT Vec<T, 3> : public detail::VecBase<T, 3, Vec<T, 3>>
1014 {
1015  using Superclass = detail::VecBase<T, 3, Vec<T, 3>>;
1016 
1017 public:
1018  Vec() = default;
1019  VTKM_EXEC_CONT Vec(const T& value)
1020  : Superclass(value)
1021  {
1022  }
1023 
1024  template <typename OtherType>
1026  : Superclass(src)
1027  {
1028  }
1029 
1031  constexpr Vec(const T& x, const T& y, const T& z)
1032  : Superclass(x, y, z)
1033  {
1034  }
1035 };
1036 
1042 
1046 
1053 
1059 
1065 
1071 
1077 
1083 
1089 
1095 
1100 #ifdef VTKM_USE_64BIT_IDS
1102 #else
1104 #endif
1105 
1111 
1117 
1123 
1129 
1130 template <typename T>
1131 class VTKM_ALWAYS_EXPORT Vec<T, 4> : public detail::VecBase<T, 4, Vec<T, 4>>
1132 {
1133  using Superclass = detail::VecBase<T, 4, Vec<T, 4>>;
1134 
1135 public:
1136  Vec() = default;
1137  VTKM_EXEC_CONT Vec(const T& value)
1138  : Superclass(value)
1139  {
1140  }
1141 
1142  template <typename OtherType>
1144  : Superclass(src)
1145  {
1146  }
1147 
1149  constexpr Vec(const T& x, const T& y, const T& z, const T& w)
1150  : Superclass(x, y, z, w)
1151  {
1152  }
1153 };
1154 
1158 
1162 
1169 
1175 
1181 
1187 
1193 
1199 
1205 
1211 
1216 #ifdef VTKM_USE_64BIT_IDS
1218 #else
1220 #endif
1221 
1227 
1233 
1239 
1245 
1249 template <typename T, typename... Ts>
1250 VTKM_EXEC_CONT constexpr vtkm::Vec<T, vtkm::IdComponent(sizeof...(Ts) + 1)> make_Vec(T value0,
1251  Ts&&... args)
1252 {
1253  return vtkm::Vec<T, vtkm::IdComponent(sizeof...(Ts) + 1)>(value0, T(args)...);
1254 }
1255 
1274 template <typename T>
1275 class VTKM_ALWAYS_EXPORT VecC : public detail::VecCBase<T, VecC<T>>
1276 {
1277  using Superclass = detail::VecCBase<T, VecC<T>>;
1278 
1279  VTKM_STATIC_ASSERT_MSG(std::is_const<T>::value == false,
1280  "You cannot use VecC with a const type as its template argument. "
1281  "Use either const VecC or VecCConst.");
1282 
1283 public:
1284 #ifdef VTKM_DOXYGEN_ONLY
1285  using ComponentType = T;
1286 #endif
1287 
1290  : Components(nullptr)
1291  , NumberOfComponents(0)
1292  {
1293  }
1294 
1296  VecC(T* array, vtkm::IdComponent size)
1297  : Components(array)
1298  , NumberOfComponents(size)
1299  {
1300  }
1301 
1302  template <vtkm::IdComponent Size>
1304  : Components(src.GetPointer())
1305  , NumberOfComponents(Size)
1306  {
1307  }
1308 
1310  explicit VecC(T& src)
1311  : Components(&src)
1312  , NumberOfComponents(1)
1313  {
1314  }
1315 
1317  VecC(const VecC<T>& src)
1318  : Components(src.Components)
1319  , NumberOfComponents(src.NumberOfComponents)
1320  {
1321  }
1322 
1323  inline VTKM_EXEC_CONT const T& operator[](vtkm::IdComponent index) const
1324  {
1325  VTKM_ASSERT(index >= 0);
1326  VTKM_ASSERT(index < this->NumberOfComponents);
1327  return this->Components[index];
1328  }
1329 
1331  {
1332  VTKM_ASSERT(index >= 0);
1333  VTKM_ASSERT(index < this->NumberOfComponents);
1334  return this->Components[index];
1335  }
1336 
1338  {
1339  return this->NumberOfComponents;
1340  }
1341 
1344  {
1345  VTKM_ASSERT(this->NumberOfComponents == src.GetNumberOfComponents());
1346  for (vtkm::IdComponent index = 0; index < this->NumberOfComponents; index++)
1347  {
1348  (*this)[index] = src[index];
1349  }
1350 
1351  return *this;
1352  }
1353 
1354 private:
1355  T* const Components;
1357 };
1358 
1368 template <typename T>
1369 class VTKM_ALWAYS_EXPORT VecCConst : public detail::VecCBase<T, VecCConst<T>>
1370 {
1371  using Superclass = detail::VecCBase<T, VecCConst<T>>;
1372 
1373  VTKM_STATIC_ASSERT_MSG(std::is_const<T>::value == false,
1374  "You cannot use VecCConst with a const type as its template argument. "
1375  "Remove the const from the type.");
1376 
1377 public:
1378 #ifdef VTKM_DOXYGEN_ONLY
1379  using ComponentType = T;
1380 #endif
1381 
1384  : Components(nullptr)
1385  , NumberOfComponents(0)
1386  {
1387  }
1388 
1390  VecCConst(const T* array, vtkm::IdComponent size)
1391  : Components(array)
1392  , NumberOfComponents(size)
1393  {
1394  }
1395 
1396  template <vtkm::IdComponent Size>
1398  : Components(src.GetPointer())
1399  , NumberOfComponents(Size)
1400  {
1401  }
1402 
1404  explicit VecCConst(const T& src)
1405  : Components(&src)
1406  , NumberOfComponents(1)
1407  {
1408  }
1409 
1412  : Components(src.Components)
1413  , NumberOfComponents(src.NumberOfComponents)
1414  {
1415  }
1416 
1418  VecCConst(const VecC<T>& src)
1419  : Components(src.Components)
1420  , NumberOfComponents(src.NumberOfComponents)
1421  {
1422  }
1423 
1424  inline VTKM_EXEC_CONT const T& operator[](vtkm::IdComponent index) const
1425  {
1426  VTKM_ASSERT(index >= 0);
1427  VTKM_ASSERT(index < this->NumberOfComponents);
1428  return this->Components[index];
1429  }
1430 
1432  {
1433  return this->NumberOfComponents;
1434  }
1435 
1436 private:
1437  const T* const Components;
1439 
1440  // You are not allowed to assign to a VecCConst, so these operators are not
1441  // implemented and are disallowed.
1442  void operator=(const VecCConst<T>&) = delete;
1443  void operator+=(const VecCConst<T>&) = delete;
1444  void operator-=(const VecCConst<T>&) = delete;
1445  void operator*=(const VecCConst<T>&) = delete;
1446  void operator/=(const VecCConst<T>&) = delete;
1447 };
1448 
1451 template <typename T>
1452 static inline VTKM_EXEC_CONT vtkm::VecC<T> make_VecC(T* array, vtkm::IdComponent size)
1453 {
1454  return vtkm::VecC<T>(array, size);
1455 }
1456 
1459 template <typename T>
1460 static inline VTKM_EXEC_CONT vtkm::VecCConst<T> make_VecC(const T* array, vtkm::IdComponent size)
1461 {
1462  return vtkm::VecCConst<T>(array, size);
1463 }
1464 
1465 namespace detail
1466 {
1467 
1468 template <typename T>
1469 static inline VTKM_EXEC_CONT auto vec_dot(const T& a, const T& b)
1470 {
1471  auto result = a[0] * b[0];
1472  for (vtkm::IdComponent i = 1; i < a.GetNumberOfComponents(); ++i)
1473  {
1474  result = result + a[i] * b[i];
1475  }
1476  return result;
1477 }
1478 template <typename T, vtkm::IdComponent Size>
1479 static inline VTKM_EXEC_CONT auto vec_dot(const vtkm::Vec<T, Size>& a, const vtkm::Vec<T, Size>& b)
1480 {
1481  auto result = a[0] * b[0];
1482  for (vtkm::IdComponent i = 1; i < Size; ++i)
1483  {
1484  result = result + a[i] * b[i];
1485  }
1486  return result;
1487 }
1488 
1489 } // namespace detail
1490 
1491 template <typename T>
1492 static inline VTKM_EXEC_CONT auto Dot(const T& a, const T& b)
1493 {
1494  return detail::vec_dot(a, b);
1495 }
1496 
1497 template <typename T>
1498 static inline VTKM_EXEC_CONT auto Dot(const vtkm::Vec<T, 2>& a, const vtkm::Vec<T, 2>& b)
1499 {
1500  return (a[0] * b[0]) + (a[1] * b[1]);
1501 }
1502 template <typename T>
1503 static inline VTKM_EXEC_CONT auto Dot(const vtkm::Vec<T, 3>& a, const vtkm::Vec<T, 3>& b)
1504 {
1505  return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);
1506 }
1507 template <typename T>
1508 static inline VTKM_EXEC_CONT auto Dot(const vtkm::Vec<T, 4>& a, const vtkm::Vec<T, 4>& b)
1509 {
1510  return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]) + (a[3] * b[3]);
1511 }
1512 // Integer types of a width less than an integer get implicitly casted to
1513 // an integer when doing a multiplication.
1514 #define VTK_M_SCALAR_DOT(stype) \
1515  static inline VTKM_EXEC_CONT auto dot(stype a, stype b) { return a * b; } /* LEGACY */ \
1516  static inline VTKM_EXEC_CONT auto Dot(stype a, stype b) { return a * b; }
1527 
1528 // v============ LEGACY =============v
1529 template <typename T>
1530 static inline VTKM_EXEC_CONT auto dot(const T& a, const T& b) -> decltype(detail::vec_dot(a, b))
1531 {
1532  return vtkm::Dot(a, b);
1533 }
1534 template <typename T>
1535 static inline VTKM_EXEC_CONT auto dot(const vtkm::Vec<T, 2>& a, const vtkm::Vec<T, 2>& b)
1536 {
1537  return vtkm::Dot(a, b);
1538 }
1539 template <typename T>
1540 static inline VTKM_EXEC_CONT auto dot(const vtkm::Vec<T, 3>& a, const vtkm::Vec<T, 3>& b)
1541 {
1542  return vtkm::Dot(a, b);
1543 }
1544 template <typename T>
1545 static inline VTKM_EXEC_CONT auto dot(const vtkm::Vec<T, 4>& a, const vtkm::Vec<T, 4>& b)
1546 {
1547  return vtkm::Dot(a, b);
1548 }
1549 // ^============ LEGACY =============^
1550 
1551 template <typename T, vtkm::IdComponent Size>
1553 {
1554  T result = a[0];
1555  for (vtkm::IdComponent i = 1; i < Size; ++i)
1556  {
1557  result += a[i];
1558  }
1559  return result;
1560 }
1561 
1562 template <typename T>
1564 {
1565  return a[0] + a[1];
1566 }
1567 
1568 template <typename T>
1570 {
1571  return a[0] + a[1] + a[2];
1572 }
1573 
1574 template <typename T>
1576 {
1577  return a[0] + a[1] + a[2] + a[3];
1578 }
1579 
1580 template <typename T, vtkm::IdComponent Size>
1582 {
1583  T result = a[0];
1584  for (vtkm::IdComponent i = 1; i < Size; ++i)
1585  {
1586  result *= a[i];
1587  }
1588  return result;
1589 }
1590 
1591 template <typename T>
1593 {
1594  return a[0] * a[1];
1595 }
1596 
1597 template <typename T>
1599 {
1600  return a[0] * a[1] * a[2];
1601 }
1602 
1603 template <typename T>
1605 {
1606  return a[0] * a[1] * a[2] * a[3];
1607 }
1608 
1609 // A pre-declaration of vtkm::Pair so that classes templated on them can refer
1610 // to it. The actual implementation is in vtkm/Pair.h.
1611 template <typename U, typename V>
1612 struct Pair;
1613 
1616 template <typename T, vtkm::IdComponent Size>
1617 inline VTKM_CONT std::ostream& operator<<(std::ostream& stream, const vtkm::Vec<T, Size>& vec)
1618 {
1619  stream << "[";
1620  for (vtkm::IdComponent component = 0; component < Size - 1; component++)
1621  {
1622  stream << vec[component] << ",";
1623  }
1624  return stream << vec[Size - 1] << "]";
1625 }
1626 
1629 template <typename T, typename U>
1630 inline VTKM_EXEC_CONT std::ostream& operator<<(std::ostream& stream, const vtkm::Pair<T, U>& vec)
1631 {
1632  return stream << "[" << vec.first << "," << vec.second << "]";
1633 }
1634 
1635 } // End of namespace vtkm
1636 
1638 // Declared inside of vtkm namespace so that the operator work with ADL lookup
1639 #endif //vtk_m_Types_h
vtkm::operator!=
bool operator!=(const vtkm::Matrix< T, NumRow, NumCol > &a, const vtkm::Matrix< T, NumRow, NumCol > &b)
Definition: Matrix.h:631
vtkm::Vec< T, 1 >::Superclass
detail::VecBase< T, 1, Vec< T, 1 > > Superclass
Definition: Types.h:877
vtkm::VecCConst::VecCConst
VecCConst(const VecC< T > &src)
Definition: Types.h:1418
vtkm::Divide
Definition: Types.h:320
vtkm::VecCConst::VecCConst
VecCConst()
Definition: Types.h:1383
vtkm::Vec< T, 2 >::Vec
Vec(const Vec< OtherType, 2 > &src)
Definition: Types.h:909
vtkm::VecC::operator[]
const T & operator[](vtkm::IdComponent index) const
Definition: Types.h:1323
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Subtract
Definition: Types.h:280
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::make_Vec
constexpr vtkm::Vec< T, vtkm::IdComponent(sizeof...(Ts)+1)> make_Vec(T value0, Ts &&... args)
Initializes and returns a Vec containing all the arguments.
Definition: Types.h:1250
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::operator/
VTKM_EXEC_CONT vtkm::Vec< T, Size > operator/(vtkm::Vec< T, Size > a, const vtkm::Vec< T, Size > &b)
Definition: VecOperators.h:371
vtkm::Add::operator()
auto operator()(const T &a, const U &b) const -> decltype(a+b)
Definition: Types.h:263
vtkm::ReduceProduct
T ReduceProduct(const vtkm::Vec< T, Size > &a)
Definition: Types.h:1581
vtkm::VecCConst::operator[]
const T & operator[](vtkm::IdComponent index) const
Definition: Types.h:1424
vtkm::VecC::GetNumberOfComponents
vtkm::IdComponent GetNumberOfComponents() const
Definition: Types.h:1337
vtkm::Vec< T, 3 >::Superclass
detail::VecBase< T, 3, Vec< T, 3 > > Superclass
Definition: Types.h:1015
vtkm::operator<<
std::ostream & operator<<(std::ostream &stream, const vtkm::Bounds &bounds)
Helper function for printing bounds during testing.
Definition: Bounds.h:248
vtkm::WordTypeDefault
vtkm::UInt32 WordTypeDefault
The default word size used for atomic bitwise operations.
Definition: Types.h:198
vtkm::ReduceSum
T ReduceSum(const vtkm::Vec< T, Size > &a)
Definition: Types.h:1552
vtkm::Vec< vtkm::Float32, 2 >::ComponentType
vtkm::Float32 ComponentType
Definition: Types.h:811
vtkm::Int16
int16_t Int16
Base type to use for 16-bit signed integer numbers.
Definition: Types.h:173
vtkm::operator*
VTKM_EXEC_CONT vtkm::Vec< T, Size > operator*(vtkm::Vec< T, Size > a, const vtkm::Vec< T, Size > &b)
Definition: VecOperators.h:185
vtkm::Vec< T, 0 >::GetNumberOfComponents
constexpr vtkm::IdComponent GetNumberOfComponents() const
Definition: Types.h:855
vtkm::Vec< T, 3 >::Vec
Vec(const T &value)
Definition: Types.h:1019
Assert.h
vtkm::VecC::operator[]
T & operator[](vtkm::IdComponent index)
Definition: Types.h:1330
vtkm::VecC::ComponentType
T ComponentType
Definition: Types.h:1285
vtkm::Multiply::operator()
auto operator()(const T &a, const U &b) const -> decltype(a *b)
Definition: Types.h:303
vtkm::Vec< T, 3 >::Vec
constexpr Vec(const T &x, const T &y, const T &z)
Definition: Types.h:1031
vtkm::Vec< T, 1 >::Vec
constexpr Vec(const T &value)
Definition: Types.h:881
vtkm::Vec< vtkm::Float32, 2 >::Superclass
detail::VecBase< vtkm::Float32, Size, Vec< vtkm::Float32, Size > > Superclass
Definition: Types.h:807
VecOperators.h
VTK_M_SCALAR_DOT
#define VTK_M_SCALAR_DOT(stype)
Definition: Types.h:1514
vtkm::operator==
bool operator==(const vtkm::Matrix< T, NumRow, NumCol > &a, const vtkm::Matrix< T, NumRow, NumCol > &b)
Definition: Matrix.h:617
vtkm::Add
Definition: Types.h:260
ExportMacros.h
vtkm::operator+
VTKM_EXEC_CONT vtkm::Vec< T, Size > operator+(vtkm::Vec< T, Size > a, const vtkm::Vec< T, Size > &b)
Definition: VecOperators.h:92
vtkm::Negate::operator()
T operator()(const T &x) const
Definition: Types.h:343
vtkm::Vec< T, 2 >::Vec
constexpr Vec(const T &x, const T &y)
Definition: Types.h:915
vtkm::Vec< T, 4 >
Definition: Types.h:1131
vtkm::operator-
VTKM_EXEC_CONT std::enable_if<(std::is_floating_point< T >::value||std::is_signed< T >::value), vtkm::Vec< T, Size > >::type operator-(vtkm::Vec< T, Size > x)
Definition: VecOperators.h:41
vtkm::Vec< T, 1 >::Vec
Vec(const Vec< OtherType, 1 > &src)
Definition: Types.h:887
vtkm::Vec< T, 4 >::Superclass
detail::VecBase< T, 4, Vec< T, 4 > > Superclass
Definition: Types.h:1133
vtkm::VecC::VecC
VecC(T *array, vtkm::IdComponent size)
Definition: Types.h:1296
VTKM_STATIC_ASSERT
#define VTKM_STATIC_ASSERT(condition)
Definition: StaticAssert.h:16
vtkm::VecC::operator=
VecC< T > & operator=(const VecC< T > &src)
Definition: Types.h:1343
vtkm::Vec< T, 4 >::Vec
Vec(const T &value)
Definition: Types.h:1137
vtkm::VecC::Components
T *const Components
Definition: Types.h:1355
vtkm::Multiply
Definition: Types.h:300
vtkm::Int8
int8_t Int8
Base type to use for 8-bit signed integer numbers.
Definition: Types.h:165
vtkm::VecCConst
A const version of VecC.
Definition: Types.h:363
vtkm::VecCConst::VecCConst
VecCConst(const VecCConst< T > &src)
Definition: Types.h:1411
vtkm::Pair::first
FirstType first
The pair's first object.
Definition: Pair.h:50
VTKM_STATIC_ASSERT_MSG
#define VTKM_STATIC_ASSERT_MSG(condition, message)
Definition: StaticAssert.h:18
vtkm::Vec< T, 2 >
Definition: Types.h:897
vtkm::Vec< T, 4 >::Vec
constexpr Vec(const T &x, const T &y, const T &z, const T &w)
Definition: Types.h:1149
Configure.h
vtkm::Vec< T, 0 >::operator[]
constexpr ComponentType operator[](vtkm::IdComponent) const
Definition: Types.h:861
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::Int64
signed long long Int64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:204
vtkm::VecCConst::Superclass
detail::VecCBase< T, VecCConst< T > > Superclass
Definition: Types.h:1371
vtkm::Divide::operator()
auto operator()(const T &a, const U &b) const -> decltype(a/b)
Definition: Types.h:323
vtkm::Vec< T, 0 >::Vec
Vec(const ComponentType &)
Definition: Types.h:842
vtkm::UInt8
uint8_t UInt8
Base type to use for 8-bit unsigned integer numbers.
Definition: Types.h:169
vtkm::Vec< T, 3 >::Vec
Vec(const Vec< OtherType, 3 > &src)
Definition: Types.h:1025
vtkm::Subtract::operator()
auto operator()(const T &a, const U &b) const -> decltype(a - b)
Definition: Types.h:283
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::VecCConst::ComponentType
T ComponentType
Definition: Types.h:1379
vtkm::Vec< T, 3 >
Definition: Types.h:1013
vtkm::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::FloatDefault
vtkm::Float32 FloatDefault
The floating point type to use when no other precision is specified.
Definition: Types.h:236
vtkm::UInt32
uint32_t UInt32
Base type to use for 32-bit unsigned integer numbers.
Definition: Types.h:185
vtkm::Vec< T, 2 >::Superclass
detail::VecBase< T, 2, Vec< T, 2 > > Superclass
Definition: Types.h:899
vtkm::VecCConst::VecCConst
VecCConst(const T &src)
Definition: Types.h:1404
StaticAssert.h
vtkm::Float32
float Float32
Base type to use for 32-bit floating-point numbers.
Definition: Types.h:157
vtkm::Vec::CopyInto
void CopyInto(Vec< T, Size > &dest) const
Definition: Types.h:825
vtkm::UInt64
unsigned long long UInt64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:207
vtkm::VecCConst::VecCConst
VecCConst(const vtkm::Vec< T, Size > &src)
Definition: Types.h:1397
vtkm::Int32
int32_t Int32
Base type to use for 32-bit signed integer numbers.
Definition: Types.h:181
vtkm::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:161
vtkm::Vec< T, 2 >::Vec
Vec(const T &value)
Definition: Types.h:903
vtkm::VecCConst::Components
const T *const Components
Definition: Types.h:1437
vtkm::VecC::Superclass
detail::VecCBase< T, VecC< T > > Superclass
Definition: Types.h:1277
vtkm::VecCConst::NumberOfComponents
vtkm::IdComponent NumberOfComponents
Definition: Types.h:1438
vtkm::VecC::NumberOfComponents
vtkm::IdComponent NumberOfComponents
Definition: Types.h:1356
vtkm::VecC::VecC
VecC(T &src)
Definition: Types.h:1310
vtkm::VecC
A Vec-like representation for short arrays.
Definition: Types.h:360
vtkm::Vec< T, 0 >::operator!=
bool operator!=(const Vec< T, NUM_COMPONENTS > &) const
Definition: Types.h:869
vtkm::VecCConst::GetNumberOfComponents
vtkm::IdComponent GetNumberOfComponents() const
Definition: Types.h:1431
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
vtkm::Vec< T, 0 >::operator=
Vec< ComponentType, NUM_COMPONENTS > & operator=(const Vec< ComponentType, NUM_COMPONENTS > &)
Definition: Types.h:850
vtkm::Vec< T, 0 >::Vec
Vec(const Vec< OtherType, NUM_COMPONENTS > &)
Definition: Types.h:845
vtkm::UInt16
uint16_t UInt16
Base type to use for 16-bit unsigned integer numbers.
Definition: Types.h:177
vtkm::VecC::VecC
VecC()
Definition: Types.h:1289
vtkm::Vec< T, 4 >::Vec
Vec(const Vec< OtherType, 4 > &src)
Definition: Types.h:1143
vtkm::Pair
A vtkm::Pair is essentially the same as an STL pair object except that the methods (constructors and ...
Definition: Pair.h:29
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53
vtkm::VecC::VecC
VecC(const VecC< T > &src)
Definition: Types.h:1317
vtkm::VecCConst::VecCConst
VecCConst(const T *array, vtkm::IdComponent size)
Definition: Types.h:1390
vtkm::Vec< T, 0 >::operator==
bool operator==(const Vec< T, NUM_COMPONENTS > &) const
Definition: Types.h:867
vtkm::Pair::second
SecondType second
The pair's second object.
Definition: Pair.h:55
vtkm::Vec< T, 0 >::ComponentType
T ComponentType
Definition: Types.h:838
vtkm::Negate
Definition: Types.h:340
vtkm::VecC::VecC
VecC(vtkm::Vec< T, Size > &src)
Definition: Types.h:1303