VTK-m  2.2
ArrayHandleConstant.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_ArrayHandleConstant_h
11 #define vtk_m_cont_ArrayHandleConstant_h
12 
15 
17 
18 #include <vtkm/Range.h>
19 #include <vtkm/VecFlat.h>
20 #include <vtkm/VectorAnalysis.h>
21 
22 namespace vtkm
23 {
24 namespace cont
25 {
26 
28 {
29 };
30 
31 namespace internal
32 {
33 
34 template <typename ValueType>
35 struct VTKM_ALWAYS_EXPORT ConstantFunctor
36 {
38  ConstantFunctor(const ValueType& value = ValueType())
39  : Value(value)
40  {
41  }
42 
44  ValueType operator()(vtkm::Id vtkmNotUsed(index)) const { return this->Value; }
45 
46 private:
47  ValueType Value;
48 };
49 
50 template <typename T>
51 using StorageTagConstantSuperclass =
53 
54 template <typename T>
55 struct Storage<T, vtkm::cont::StorageTagConstant> : Storage<T, StorageTagConstantSuperclass<T>>
56 {
57 };
58 
59 } // namespace internal
60 
69 template <typename T>
70 class ArrayHandleConstant : public vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagConstant>
71 {
72 public:
76 
78  VTKM_CONT
79  ArrayHandleConstant(T value, vtkm::Id numberOfValues = 0)
80  : Superclass(internal::FunctorToArrayHandleImplicitBuffers(internal::ConstantFunctor<T>(value),
81  numberOfValues))
82  {
83  }
84 
89  VTKM_CONT T GetValue() const { return this->ReadPortal().GetFunctor()(0); }
90 };
91 
94 template <typename T>
96 {
97  return vtkm::cont::ArrayHandleConstant<T>(value, numberOfValues);
98 }
99 
100 namespace internal
101 {
102 
103 template <>
104 struct VTKM_CONT_EXPORT ArrayExtractComponentImpl<vtkm::cont::StorageTagConstant>
105 {
106  template <typename T>
108  vtkm::IdComponent componentIndex,
109  vtkm::CopyFlag allowCopy) const
110  {
111  if (allowCopy != vtkm::CopyFlag::On)
112  {
114  "Cannot extract component of ArrayHandleConstant without copying. "
115  "(However, the whole array does not need to be copied.)");
116  }
117 
118  vtkm::cont::ArrayHandleConstant<T> srcArray = src;
119 
120  vtkm::VecFlat<T> vecValue{ srcArray.GetValue() };
121 
122  // Make a basic array with one entry (the constant value).
123  auto basicArray = vtkm::cont::make_ArrayHandle({ vecValue[componentIndex] });
124 
125  // Set up a modulo = 1 so all indices go to this one value.
126  return vtkm::cont::make_ArrayHandleStride(basicArray, src.GetNumberOfValues(), 1, 0, 1, 1);
127  }
128 };
129 
130 template <typename S>
131 struct ArrayRangeComputeImpl;
132 
133 template <>
134 struct VTKM_CONT_EXPORT ArrayRangeComputeImpl<vtkm::cont::StorageTagConstant>
135 {
136  template <typename T>
139  const vtkm::cont::ArrayHandle<vtkm::UInt8>& maskArray,
140  bool computeFiniteRange,
141  vtkm::cont::DeviceAdapterId devId) const
142  {
143  bool allMasked = false;
144  if (maskArray.GetNumberOfValues() != 0)
145  {
146  // Find if there is atleast one value that is not masked
147  auto ids = GetFirstAndLastUnmaskedIndices(maskArray, devId);
148  allMasked = (ids[1] < ids[0]);
149  }
150 
151  auto value = vtkm::make_VecFlat(input.ReadPortal().Get(0));
152 
154  result.Allocate(value.GetNumberOfComponents());
155  auto resultPortal = result.WritePortal();
156  for (vtkm::IdComponent index = 0; index < value.GetNumberOfComponents(); ++index)
157  {
158  auto comp = static_cast<vtkm::Float64>(value[index]);
159  if (allMasked || (computeFiniteRange && !vtkm::IsFinite(comp)))
160  {
161  resultPortal.Set(index, vtkm::Range{});
162  }
163  else
164  {
165  resultPortal.Set(index, vtkm::Range{ comp, comp });
166  }
167  }
168  return result;
169  }
170 };
171 
172 template <typename S>
173 struct ArrayRangeComputeMagnitudeImpl;
174 
175 template <>
176 struct VTKM_CONT_EXPORT ArrayRangeComputeMagnitudeImpl<vtkm::cont::StorageTagConstant>
177 {
178  template <typename T>
179  VTKM_CONT vtkm::Range operator()(
181  const vtkm::cont::ArrayHandle<vtkm::UInt8>& maskArray,
182  bool computeFiniteRange,
183  vtkm::cont::DeviceAdapterId devId) const
184  {
185  if (maskArray.GetNumberOfValues() != 0)
186  {
187  // Find if there is atleast one value that is not masked
188  auto ids = GetFirstAndLastUnmaskedIndices(maskArray, devId);
189  if (ids[1] < ids[0])
190  {
191  return vtkm::Range{};
192  }
193  }
194 
195  auto value = input.ReadPortal().Get(0);
196  vtkm::Float64 rangeValue = vtkm::Magnitude(vtkm::make_VecFlat(value));
197  return (computeFiniteRange && !vtkm::IsFinite(rangeValue))
198  ? vtkm::Range{}
199  : vtkm::Range{ rangeValue, rangeValue };
200  }
201 };
202 
203 } // namespace internal
204 
205 }
206 } // vtkm::cont
207 
208 //=============================================================================
209 // Specializations of serialization related classes
211 namespace vtkm
212 {
213 namespace cont
214 {
215 
216 template <typename T>
217 struct SerializableTypeString<vtkm::cont::ArrayHandleConstant<T>>
218 {
219  static VTKM_CONT const std::string& Get()
220  {
221  static std::string name = "AH_Constant<" + SerializableTypeString<T>::Get() + ">";
222  return name;
223  }
224 };
225 
226 template <typename T>
227 struct SerializableTypeString<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagConstant>>
228  : SerializableTypeString<vtkm::cont::ArrayHandleConstant<T>>
229 {
230 };
231 }
232 } // vtkm::cont
233 
234 namespace mangled_diy_namespace
235 {
236 
237 template <typename T>
238 struct Serialization<vtkm::cont::ArrayHandleConstant<T>>
239 {
240 private:
243 
244 public:
245  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
246  {
247  vtkmdiy::save(bb, obj.GetNumberOfValues());
248  vtkmdiy::save(bb, obj.ReadPortal().Get(0));
249  }
250 
251  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
252  {
253  vtkm::Id count = 0;
254  vtkmdiy::load(bb, count);
255 
256  T value;
257  vtkmdiy::load(bb, value);
258 
259  obj = vtkm::cont::make_ArrayHandleConstant(value, count);
260  }
261 };
262 
263 template <typename T>
264 struct Serialization<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagConstant>>
265  : Serialization<vtkm::cont::ArrayHandleConstant<T>>
266 {
267 };
268 
269 } // diy
271 
272 #endif //vtk_m_cont_ArrayHandleConstant_h
VecFlat.h
vtkm::make_VecFlat
vtkm::VecFlat< T > make_VecFlat(const T &vec)
Converts a Vec-like object to a VecFlat.
Definition: VecFlat.h:281
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayExtractComponent.h
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
vtkm::cont::ArrayHandleConstant::GetValue
T GetValue() const
Returns the constant value stored in this array.
Definition: ArrayHandleConstant.h:89
vtkm::cont::ArrayHandle::GetNumberOfValues
vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:468
vtkm::cont::make_ArrayHandleConstant
vtkm::cont::ArrayHandleConstant< T > make_ArrayHandleConstant(T value, vtkm::Id numberOfValues)
make_ArrayHandleConstant is convenience function to generate an ArrayHandleImplicit.
Definition: ArrayHandleConstant.h:95
mangled_diy_namespace
Definition: Particle.h:351
vtkm::cont::ArrayHandleConstant::ArrayHandleConstant
ArrayHandleConstant(T value, vtkm::Id numberOfValues=0)
Construct a constant array containing the given value.
Definition: ArrayHandleConstant.h:79
vtkm::cont::StorageTagConstant
Definition: ArrayHandleConstant.h:27
VectorAnalysis.h
vtkm::cont::make_ArrayHandleStride
vtkm::cont::ArrayHandleStride< T > make_ArrayHandleStride(const vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagBasic > &array, vtkm::Id numValues, vtkm::Id stride, vtkm::Id offset, vtkm::Id modulo=0, vtkm::Id divisor=1)
Create an array by adding a stride to a basic array.
Definition: ArrayHandleStride.h:419
vtkm::cont::ArrayHandleConstant::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagConstant >) >::type Superclass
Definition: ArrayHandleConstant.h:75
VTKM_CONT_EXPORT
#define VTKM_CONT_EXPORT
Definition: vtkm_cont_export.h:44
Range.h
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::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:224
vtkm::cont::ArrayHandleConstant
An array handle with a constant value.
Definition: ArrayHandleConstant.h:70
vtkm::CopyFlag::On
@ On
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagConstant >::ReadPortal
ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:433
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::Magnitude
detail::FloatingPointReturnType< T >::Type Magnitude(const T &x)
Returns the magnitude of a vector.
Definition: VectorAnalysis.h:100
vtkm::cont::ErrorBadValue
This class is thrown when a VTKm function or method encounters an invalid value that inhibits progres...
Definition: ErrorBadValue.h:25
vtkm::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:161
vtkm::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:17
vtkm::cont::ArrayHandle::Allocate
void Allocate(vtkm::Id numberOfValues, vtkm::CopyFlag preserve, vtkm::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:490
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
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
ArrayHandleImplicit.h
ArrayRangeComputeUtils.h
vtkm::cont::ArrayHandle::WritePortal
WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:454
vtkm::cont::ArrayHandleImplicit
An ArrayHandle that computes values on the fly.
Definition: ArrayHandleImplicit.h:179
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31