VTK-m  1.4
Field.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_Field_h
11 #define vtk_m_cont_Field_h
12 
13 #include <vtkm/cont/vtkm_cont_export.h>
14 
15 #include <vtkm/Range.h>
16 #include <vtkm/Types.h>
17 
18 #include <vtkm/cont/ArrayHandle.h>
21 #include <vtkm/cont/ArrayRangeCompute.hxx>
23 
24 namespace vtkm
25 {
26 namespace cont
27 {
28 
29 namespace internal
30 {
31 
32 struct ComputeRange
33 {
34  template <typename ArrayHandleType>
35  void operator()(const ArrayHandleType& input, vtkm::cont::ArrayHandle<vtkm::Range>& range) const
36  {
37  range = vtkm::cont::ArrayRangeCompute(input);
38  }
39 };
40 
41 } // namespace internal
42 
43 
47 class VTKM_CONT_EXPORT Field
48 {
49 public:
50  enum struct Association
51  {
52  ANY,
53  WHOLE_MESH,
54  POINTS,
55  CELL_SET
56  };
57 
58  VTKM_CONT
59  Field() = default;
60 
61  VTKM_CONT
62  Field(std::string name, Association association, const vtkm::cont::VariantArrayHandle& data);
63 
64  template <typename T, typename Storage>
65  VTKM_CONT Field(std::string name,
66  Association association,
68  : Field(name, association, vtkm::cont::VariantArrayHandle{ data })
69  {
70  }
71 
72  Field(const vtkm::cont::Field& src);
73  Field(vtkm::cont::Field&& src) noexcept;
74 
75  VTKM_CONT virtual ~Field();
76 
77  VTKM_CONT Field& operator=(const vtkm::cont::Field& src);
78  VTKM_CONT Field& operator=(vtkm::cont::Field&& src) noexcept;
79 
80  VTKM_CONT const std::string& GetName() const { return this->Name; }
81  VTKM_CONT Association GetAssociation() const { return this->FieldAssociation; }
82  const vtkm::cont::VariantArrayHandle& GetData() const;
84 
85  VTKM_CONT bool IsFieldCell() const { return this->FieldAssociation == Association::CELL_SET; }
86  VTKM_CONT bool IsFieldPoint() const { return this->FieldAssociation == Association::POINTS; }
87 
88  VTKM_CONT vtkm::Id GetNumberOfValues() const { return this->Data.GetNumberOfValues(); }
89 
90  template <typename TypeList>
91  VTKM_CONT void GetRange(vtkm::Range* range, TypeList) const
92  {
93  this->GetRangeImpl(TypeList());
94  const vtkm::Id length = this->Range.GetNumberOfValues();
95  for (vtkm::Id i = 0; i < length; ++i)
96  {
97  range[i] = this->Range.GetPortalConstControl().Get(i);
98  }
99  }
100 
101  template <typename TypeList>
103  {
104  return this->GetRangeImpl(TypeList());
105  }
106 
107  VTKM_CONT
109  {
110  return this->GetRangeImpl(VTKM_DEFAULT_TYPE_LIST_TAG());
111  };
112 
113  VTKM_CONT void GetRange(vtkm::Range* range) const
114  {
115  return this->GetRange(range, VTKM_DEFAULT_TYPE_LIST_TAG());
116  };
117 
118  template <typename T, typename StorageTag>
120  {
121  this->Data = newdata;
122  this->ModifiedFlag = true;
123  }
124 
125  VTKM_CONT
127  {
128  this->Data = newdata;
129  this->ModifiedFlag = true;
130  }
131 
132  VTKM_CONT
133  virtual void PrintSummary(std::ostream& out) const;
134 
135  VTKM_CONT
137  {
138  this->Data.ReleaseResourcesExecution();
139  this->Range.ReleaseResourcesExecution();
140  }
141 
142 private:
143  std::string Name;
144 
145  Association FieldAssociation = Association::ANY;
148  mutable bool ModifiedFlag = true;
149 
150  template <typename TypeList>
152  {
153  VTKM_IS_LIST_TAG(TypeList);
154 
155  if (this->ModifiedFlag)
156  {
158  this->Data.ResetTypes(TypeList()), internal::ComputeRange{}, this->Range);
159  this->ModifiedFlag = false;
160  }
161 
162  return this->Range;
163  }
164 };
165 
166 template <typename Functor, typename... Args>
167 void CastAndCall(const vtkm::cont::Field& field, Functor&& f, Args&&... args)
168 {
169  vtkm::cont::CastAndCall(field.GetData(), std::forward<Functor>(f), std::forward<Args>(args)...);
170 }
171 
172 
174 template <typename T>
176 vtkm::cont::Field make_Field(std::string name,
177  Field::Association association,
178  const T* data,
179  vtkm::Id size,
181 {
182  return vtkm::cont::Field(name, association, vtkm::cont::make_ArrayHandle(data, size, copy));
183 }
184 
185 template <typename T>
186 vtkm::cont::Field make_Field(std::string name,
187  Field::Association association,
188  const std::vector<T>& data,
190 {
191  return vtkm::cont::Field(name, association, vtkm::cont::make_ArrayHandle(data, copy));
192 }
193 
195 
197 template <typename T, typename S>
199 {
201 }
202 
204 inline vtkm::cont::Field make_FieldPoint(std::string name,
205  const vtkm::cont::VariantArrayHandle& data)
206 {
208 }
209 
211 template <typename T, typename S>
213 {
215 }
216 
217 
219 inline vtkm::cont::Field make_FieldCell(std::string name,
220  const vtkm::cont::VariantArrayHandle& data)
221 {
223 }
224 
225 } // namespace cont
226 } // namespace vtkm
227 
228 
229 namespace vtkm
230 {
231 namespace cont
232 {
233 namespace internal
234 {
235 template <>
236 struct DynamicTransformTraits<vtkm::cont::Field>
237 {
238  using DynamicTag = vtkm::cont::internal::DynamicTransformTagCastAndCall;
239 };
240 } // namespace internal
241 } // namespace cont
242 } // namespace vtkm
243 
244 //=============================================================================
245 // Specializations of serialization related classes
247 namespace vtkm
248 {
249 namespace cont
250 {
251 template <typename TypeList = VTKM_DEFAULT_TYPE_LIST_TAG>
252 struct SerializableField
253 {
254  SerializableField() = default;
255 
256  explicit SerializableField(const vtkm::cont::Field& field)
257  : Field(field)
258  {
259  }
260 
262 };
263 } // namespace cont
264 } // namespace vtkm
265 
266 namespace mangled_diy_namespace
267 {
268 
269 template <typename TypeList>
270 struct Serialization<vtkm::cont::SerializableField<TypeList>>
271 {
272 private:
273  using Type = vtkm::cont::SerializableField<TypeList>;
274 
275 public:
276  static VTKM_CONT void save(BinaryBuffer& bb, const Type& serializable)
277  {
278  const auto& field = serializable.Field;
279 
280  vtkmdiy::save(bb, field.GetName());
281  vtkmdiy::save(bb, static_cast<int>(field.GetAssociation()));
282  vtkmdiy::save(bb, field.GetData().ResetTypes(TypeList{}));
283  }
284 
285  static VTKM_CONT void load(BinaryBuffer& bb, Type& serializable)
286  {
287  auto& field = serializable.Field;
288 
289  std::string name;
290  vtkmdiy::load(bb, name);
291  int assocVal = 0;
292  vtkmdiy::load(bb, assocVal);
293 
294  auto assoc = static_cast<vtkm::cont::Field::Association>(assocVal);
296  vtkmdiy::load(bb, data);
297  field = vtkm::cont::Field(name, assoc, vtkm::cont::VariantArrayHandle(data));
298  }
299 };
300 
301 } // diy
303 
304 #endif //vtk_m_cont_Field_h
VTKM_CONT Association GetAssociation() const
Definition: Field.h:81
VTKM_CONT const vtkm::cont::ArrayHandle< vtkm::Range > & GetRange(TypeList) const
Definition: Field.h:102
#define VTKM_CONT
Definition: ExportMacros.h:50
#define VTKM_DEFAULT_TYPE_LIST_TAG
Definition: TypeListTag.h:14
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
void CastAndCall(const vtkm::cont::ArrayHandleVirtualCoordinates &coords, Functor &&f, Args &&...args)
Definition: ArrayHandleVirtualCoordinates.h:46
virtual VTKM_CONT void ReleaseResourcesExecution()
Definition: Field.h:136
VTKM_CONT VariantArrayHandleBase< NewTypeList > ResetTypes(NewTypeList=NewTypeList()) const
Changes the types to try casting to when resolving this variant array, which is specified with a list...
Definition: VariantArrayHandle.h:192
vtkm::cont::ArrayHandle< vtkm::Range > Range
Definition: Field.h:147
VTKM_CONT vtkm::cont::ArrayHandle< vtkm::Range > ArrayRangeCompute(const ArrayHandleType &input, vtkm::cont::DeviceAdapterId device=vtkm::cont::DeviceAdapterTagAny())
Compute the range of the data in an array handle.
vtkm::cont::Field make_Field(std::string name, Field::Association association, const T *data, vtkm::Id size, vtkm::CopyFlag copy=vtkm::CopyFlag::Off)
Convenience functions to build fields from C style arrays and std::vector.
Definition: Field.h:176
VTKM_CONT const std::string & GetName() const
Definition: Field.h:80
Represent a continuous scalar range of values.
Definition: Range.h:30
vtkm::cont::Field make_FieldPoint(std::string name, const vtkm::cont::ArrayHandle< T, S > &data)
Convenience function to build point fields from vtkm::cont::ArrayHandle.
Definition: Field.h:198
#define VTKM_IS_LIST_TAG(tag)
Checks that the argument is a proper list tag.
Definition: ListTag.h:39
VTKM_CONT void SetData(const vtkm::cont::ArrayHandle< T, StorageTag > &newdata)
Definition: Field.h:119
VTKM_CONT bool IsFieldCell() const
Definition: Field.h:85
VTKM_CONT void SetData(const vtkm::cont::VariantArrayHandle &newdata)
Definition: Field.h:126
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:188
A Field encapsulates an array on some piece of the mesh, such as the points, a cell set...
Definition: Field.h:47
VTKM_CONT void GetRange(vtkm::Range *range) const
Definition: Field.h:113
VTKM_CONT vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagBasic > make_ArrayHandle(const T *array, vtkm::Id length, vtkm::CopyFlag copy=vtkm::CopyFlag::Off)
A convenience function for creating an ArrayHandle from a standard C array.
Definition: ArrayHandle.h:527
VTKM_CONT void GetRange(vtkm::Range *range, TypeList) const
Definition: Field.h:91
VTKM_CONT bool IsFieldPoint() const
Definition: Field.h:86
Groups connected points that have the same field value.
Definition: BinaryOperators.h:16
CopyFlag
Definition: Flags.h:16
vtkm::cont::Field make_FieldCell(std::string name, const vtkm::cont::ArrayHandle< T, S > &data)
Convenience function to build cell fields from vtkm::cont::ArrayHandle.
Definition: Field.h:212
const vtkm::cont::VariantArrayHandle & GetData() const
VTKM_CONT const vtkm::cont::ArrayHandle< vtkm::Range > & GetRangeImpl(TypeList) const
Definition: Field.h:151
VTKM_CONT Field(std::string name, Association association, const vtkm::cont::ArrayHandle< T, Storage > &data)
Definition: Field.h:65
std::string Name
name of field
Definition: Field.h:143
VTKM_CONT const vtkm::cont::ArrayHandle< vtkm::Range > & GetRange() const
Definition: Field.h:108
Association
Definition: Field.h:50
VTKM_CONT vtkm::Id GetNumberOfValues() const
Definition: Field.h:88
vtkm::cont::VariantArrayHandle Data
Definition: Field.h:146
Manages an array-worth of data.
Definition: ArrayHandle.h:249