VTK-m  2.0
cont/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>
19 #include <vtkm/cont/CastAndCall.h>
21 
22 namespace vtkm
23 {
24 namespace cont
25 {
26 
27 
31 class VTKM_CONT_EXPORT Field
32 {
33 public:
34  enum struct Association
35  {
36  Any,
37  WholeDataSet,
38  Points,
39  Cells,
40  Partitions,
41  Global,
42  };
43 
44  VTKM_CONT
45  Field() = default;
46 
47  VTKM_CONT
48  Field(std::string name, Association association, const vtkm::cont::UnknownArrayHandle& data);
49 
50  template <typename T, typename Storage>
51  VTKM_CONT Field(std::string name,
52  Association association,
54  : Field(name, association, vtkm::cont::UnknownArrayHandle{ data })
55  {
56  }
57 
58  Field(const vtkm::cont::Field& src);
59  Field(vtkm::cont::Field&& src) noexcept;
60 
61  VTKM_CONT virtual ~Field();
62 
63  VTKM_CONT Field& operator=(const vtkm::cont::Field& src);
64  VTKM_CONT Field& operator=(vtkm::cont::Field&& src) noexcept;
65 
66  VTKM_CONT bool IsCellField() const { return this->FieldAssociation == Association::Cells; }
67  VTKM_CONT bool IsPointField() const { return this->FieldAssociation == Association::Points; }
69  {
70  return this->FieldAssociation == Association::WholeDataSet;
71  }
73  {
74  return this->FieldAssociation == Association::Partitions;
75  }
76  VTKM_CONT bool IsGlobalField() const { return this->FieldAssociation == Association::Global; }
77 
80  VTKM_CONT bool IsSupportedType() const;
81 
82  VTKM_CONT vtkm::Id GetNumberOfValues() const { return this->Data.GetNumberOfValues(); }
83 
84  VTKM_CONT const std::string& GetName() const { return this->Name; }
85  VTKM_CONT Association GetAssociation() const { return this->FieldAssociation; }
86  const vtkm::cont::UnknownArrayHandle& GetData() const;
88 
89  VTKM_CONT const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange() const;
90 
91  VTKM_CONT void GetRange(vtkm::Range* range) const;
92 
107  VTKM_CONT vtkm::cont::UnknownArrayHandle GetDataAsDefaultFloat() const;
108 
124  VTKM_CONT vtkm::cont::UnknownArrayHandle GetDataWithExpectedTypes() const;
125 
141  VTKM_CONT void ConvertToExpected();
142 
143  VTKM_CONT void SetData(const vtkm::cont::UnknownArrayHandle& newdata);
144 
145  template <typename T, typename StorageTag>
147  {
148  this->SetData(vtkm::cont::UnknownArrayHandle(newdata));
149  }
150 
151  VTKM_CONT
152  virtual void PrintSummary(std::ostream& out) const;
153 
154  VTKM_CONT
156  {
157  this->Data.ReleaseResourcesExecution();
158  this->Range.ReleaseResourcesExecution();
159  }
160 
161 private:
162  std::string Name;
163 
164  Association FieldAssociation = Association::Any;
167  mutable bool ModifiedFlag = true;
168 };
169 
170 template <typename Functor, typename... Args>
171 void CastAndCall(const vtkm::cont::Field& field, Functor&& f, Args&&... args)
172 {
173  vtkm::cont::CastAndCall(field.GetData(), std::forward<Functor>(f), std::forward<Args>(args)...);
174 }
175 
176 
178 template <typename T>
180 vtkm::cont::Field make_Field(std::string name,
181  Field::Association association,
182  const T* data,
183  vtkm::Id size,
184  vtkm::CopyFlag copy)
185 {
186  return vtkm::cont::Field(name, association, vtkm::cont::make_ArrayHandle(data, size, copy));
187 }
188 
189 template <typename T>
190 vtkm::cont::Field make_Field(std::string name,
191  Field::Association association,
192  const std::vector<T>& data,
193  vtkm::CopyFlag copy)
194 {
195  return vtkm::cont::Field(name, association, vtkm::cont::make_ArrayHandle(data, copy));
196 }
197 
198 template <typename T>
200  Field::Association association,
201  std::vector<T>&& data)
202 {
203  return vtkm::cont::Field(name, association, vtkm::cont::make_ArrayHandleMove(std::move(data)));
204 }
205 
206 template <typename T>
207 vtkm::cont::Field make_Field(std::string name,
208  Field::Association association,
209  std::vector<T>&& data,
211 {
212  return make_FieldMove(name, association, std::move(data));
213 }
214 
215 template <typename T>
216 vtkm::cont::Field make_Field(std::string name,
217  Field::Association association,
218  std::initializer_list<T>&& data)
219 {
220  return make_FieldMove(name, association, vtkm::cont::make_ArrayHandle(std::move(data)));
221 }
222 
224 
226 template <typename T, typename S>
228 {
230 }
231 
233 inline vtkm::cont::Field make_FieldPoint(std::string name,
234  const vtkm::cont::UnknownArrayHandle& data)
235 {
237 }
238 
240 template <typename T, typename S>
242 {
244 }
245 
246 
248 inline vtkm::cont::Field make_FieldCell(std::string name,
249  const vtkm::cont::UnknownArrayHandle& data)
250 {
252 }
253 
254 } // namespace cont
255 } // namespace vtkm
256 
257 
258 namespace vtkm
259 {
260 namespace cont
261 {
262 namespace internal
263 {
264 template <>
265 struct DynamicTransformTraits<vtkm::cont::Field>
266 {
267  using DynamicTag = vtkm::cont::internal::DynamicTransformTagCastAndCall;
268 };
269 } // namespace internal
270 } // namespace cont
271 } // namespace vtkm
272 
273 //=============================================================================
274 // Specializations of serialization related classes
276 
277 namespace mangled_diy_namespace
278 {
279 
280 template <>
281 struct VTKM_CONT_EXPORT Serialization<vtkm::cont::Field>
282 {
283  static VTKM_CONT void save(BinaryBuffer& bb, const vtkm::cont::Field& field);
284  static VTKM_CONT void load(BinaryBuffer& bb, vtkm::cont::Field& field);
285 };
286 
287 } // diy
289 
290 #endif //vtk_m_cont_Field_h
vtkm::cont::Field::IsPointField
VTKM_CONT bool IsPointField() const
Definition: cont/Field.h:67
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
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
vtkm::cont::Field::Association
Association
Definition: cont/Field.h:34
vtkm::cont::make_Field
vtkm::cont::Field make_Field(std::string name, Field::Association association, const T *data, vtkm::Id size, vtkm::CopyFlag copy)
Convenience functions to build fields from C style arrays and std::vector.
Definition: cont/Field.h:180
vtkm::cont::Field::Field
VTKM_CONT Field(std::string name, Association association, const vtkm::cont::ArrayHandle< T, Storage > &data)
Definition: cont/Field.h:51
vtkm::cont::UnknownArrayHandle
An ArrayHandle of an unknown value type and storage.
Definition: UnknownArrayHandle.h:406
vtkm::cont::Field::GetAssociation
VTKM_CONT Association GetAssociation() const
Definition: cont/Field.h:85
vtkm::cont::Field::Range
vtkm::cont::ArrayHandle< vtkm::Range > Range
Definition: cont/Field.h:166
vtkm::cont::Field::Name
std::string Name
name of field
Definition: cont/Field.h:162
vtkm::cont::CastAndCall
void CastAndCall(const DynamicObject &dynamicObject, Functor &&f, Args &&... args)
A Generic interface to CastAndCall.
Definition: CastAndCall.h:47
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::Field::GetData
const vtkm::cont::UnknownArrayHandle & GetData() const
vtkm::cont::Field::Data
vtkm::cont::UnknownArrayHandle Data
Definition: cont/Field.h:165
vtkm::cont::Field::ReleaseResourcesExecution
virtual VTKM_CONT void ReleaseResourcesExecution()
Definition: cont/Field.h:155
vtkm::cont::Field::SetData
VTKM_CONT void SetData(const vtkm::cont::ArrayHandle< T, StorageTag > &newdata)
Definition: cont/Field.h:146
vtkm::cont::make_FieldCell
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: cont/Field.h:241
vtkm::cont::make_ArrayHandleMove
VTKM_CONT vtkm::cont::ArrayHandleBasic< T > make_ArrayHandleMove(T *&array, vtkm::Id numberOfValues, vtkm::cont::internal::BufferInfo::Deleter deleter=internal::SimpleArrayDeleter< T >, vtkm::cont::internal::BufferInfo::Reallocater reallocater=internal::SimpleArrayReallocater< T >)
A convenience function to move a user-allocated array into an ArrayHandle.
Definition: ArrayHandleBasic.h:241
UnknownArrayHandle.h
vtkm::cont::Field::IsPartitionsField
VTKM_CONT bool IsPartitionsField() const
Definition: cont/Field.h:72
CastAndCall.h
vtkm::cont::Field
A Field encapsulates an array on some piece of the mesh, such as the points, a cell set,...
Definition: cont/Field.h:31
Range.h
vtkm::cont::Field::Association::Points
@ Points
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::cont::Field::IsWholeDataSetField
VTKM_CONT bool IsWholeDataSetField() const
Definition: cont/Field.h:68
vtkm::cont::Field::IsCellField
VTKM_CONT bool IsCellField() const
Definition: cont/Field.h:66
vtkm::cont::Field::GetName
const VTKM_CONT std::string & GetName() const
Definition: cont/Field.h:84
vtkm::cont::Field::GetNumberOfValues
VTKM_CONT vtkm::Id GetNumberOfValues() const
Definition: cont/Field.h:82
vtkm::cont::Field::Association::Cells
@ Cells
vtkm::CopyFlag
CopyFlag
Definition: Flags.h:16
vtkm::cont::make_FieldMove
vtkm::cont::Field make_FieldMove(std::string name, Field::Association association, std::vector< T > &&data)
Definition: cont/Field.h:199
vtkm::cont::make_FieldPoint
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: cont/Field.h:227
vtkm::cont::Field::IsGlobalField
VTKM_CONT bool IsGlobalField() const
Definition: cont/Field.h:76
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31