VTK-m  1.8
DataSet.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_DataSet_h
11 #define vtk_m_cont_DataSet_h
12 
13 #include <vtkm/cont/vtkm_cont_export.h>
14 
15 #include <vtkm/cont/ArrayHandle.h>
18 #include <vtkm/cont/Field.h>
21 
22 namespace vtkm
23 {
24 namespace cont
25 {
26 
27 class VTKM_CONT_EXPORT DataSet
28 {
29 public:
30  VTKM_CONT void Clear();
31 
33  VTKM_CONT vtkm::Id GetNumberOfCells() const;
34 
39  VTKM_CONT vtkm::Id GetNumberOfPoints() const;
40 
41  VTKM_CONT void AddField(const Field& field);
42 
43  VTKM_CONT
44  const vtkm::cont::Field& GetField(vtkm::Id index) const;
45 
46  VTKM_CONT
47  vtkm::cont::Field& GetField(vtkm::Id index);
48 
49  VTKM_CONT
50  bool HasField(const std::string& name,
52  {
53  bool found = false;
54  this->FindFieldIndex(name, assoc, found);
55  return found;
56  }
57 
58  VTKM_CONT
59  bool HasCellField(const std::string& name) const
60  {
61  bool found = false;
62  this->FindFieldIndex(name, vtkm::cont::Field::Association::Cells, found);
63  return found;
64  }
65 
66  VTKM_CONT
67  bool HasPointField(const std::string& name) const
68  {
69  bool found = false;
70  this->FindFieldIndex(name, vtkm::cont::Field::Association::Points, found);
71  return found;
72  }
73 
74 
77  VTKM_CONT
78  vtkm::Id GetFieldIndex(
79  const std::string& name,
81 
84 
85  VTKM_CONT
87  const std::string& name,
89  {
90  return this->GetField(this->GetFieldIndex(name, assoc));
91  }
92 
93  VTKM_CONT
95  const std::string& name,
97  {
98  return this->GetField(this->GetFieldIndex(name, assoc));
99  }
101 
104 
105  VTKM_CONT
106  const vtkm::cont::Field& GetCellField(const std::string& name) const
107  {
108  return this->GetField(name, vtkm::cont::Field::Association::Cells);
109  }
110 
111  VTKM_CONT
112  vtkm::cont::Field& GetCellField(const std::string& name)
113  {
114  return this->GetField(name, vtkm::cont::Field::Association::Cells);
115  }
117 
120 
121  VTKM_CONT
122  const vtkm::cont::Field& GetPointField(const std::string& name) const
123  {
124  return this->GetField(name, vtkm::cont::Field::Association::Points);
125  }
126 
127  VTKM_CONT
128  vtkm::cont::Field& GetPointField(const std::string& name)
129  {
130  return this->GetField(name, vtkm::cont::Field::Association::Points);
131  }
133 
134  VTKM_CONT
135  void AddPointField(const std::string& fieldName, const vtkm::cont::UnknownArrayHandle& field)
136  {
137  this->AddField(make_FieldPoint(fieldName, field));
138  }
139 
140  template <typename T, typename Storage>
141  VTKM_CONT void AddPointField(const std::string& fieldName,
143  {
144  this->AddField(make_FieldPoint(fieldName, field));
145  }
146 
147  template <typename T>
148  VTKM_CONT void AddPointField(const std::string& fieldName, const std::vector<T>& field)
149  {
150  this->AddField(
152  }
153 
154  template <typename T>
155  VTKM_CONT void AddPointField(const std::string& fieldName, const T* field, const vtkm::Id& n)
156  {
157  this->AddField(
159  }
160 
161  //Cell centered field
162  VTKM_CONT
163  void AddCellField(const std::string& fieldName, const vtkm::cont::UnknownArrayHandle& field)
164  {
165  this->AddField(make_FieldCell(fieldName, field));
166  }
167 
168  template <typename T, typename Storage>
169  VTKM_CONT void AddCellField(const std::string& fieldName,
171  {
172  this->AddField(make_FieldCell(fieldName, field));
173  }
174 
175  template <typename T>
176  VTKM_CONT void AddCellField(const std::string& fieldName, const std::vector<T>& field)
177  {
178  this->AddField(
180  }
181 
182  template <typename T>
183  VTKM_CONT void AddCellField(const std::string& fieldName, const T* field, const vtkm::Id& n)
184  {
185  this->AddField(
187  }
188 
189 
190  VTKM_CONT
192  {
193  this->CoordSystems.push_back(cs);
194  }
195 
196  VTKM_CONT
197  bool HasCoordinateSystem(const std::string& name) const
198  {
199  return this->GetCoordinateSystemIndex(name) >= 0;
200  }
201 
202  VTKM_CONT
203  const vtkm::cont::CoordinateSystem& GetCoordinateSystem(vtkm::Id index = 0) const;
204 
205  VTKM_CONT
206  vtkm::cont::CoordinateSystem& GetCoordinateSystem(vtkm::Id index = 0);
207 
211  VTKM_CONT
212  vtkm::Id GetCoordinateSystemIndex(const std::string& name) const;
213 
216 
217  VTKM_CONT
218  const vtkm::cont::CoordinateSystem& GetCoordinateSystem(const std::string& name) const;
219 
220  VTKM_CONT
221  vtkm::cont::CoordinateSystem& GetCoordinateSystem(const std::string& name);
223 
226  VTKM_CONT
227  std::vector<vtkm::cont::CoordinateSystem> GetCoordinateSystems() const
228  {
229  return this->CoordSystems;
230  }
231 
232  template <typename CellSetType>
233  VTKM_CONT void SetCellSet(const CellSetType& cellSet)
234  {
236  this->CellSet = vtkm::cont::UnknownCellSet(cellSet);
237  }
238 
239  VTKM_CONT
240  const vtkm::cont::UnknownCellSet& GetCellSet() const { return this->CellSet; }
241 
242  VTKM_CONT
244 
245  VTKM_CONT
247  {
248  return static_cast<vtkm::IdComponent>(this->Fields.size());
249  }
250 
251  VTKM_CONT
253  {
254  return static_cast<vtkm::IdComponent>(this->CoordSystems.size());
255  }
256 
259  VTKM_CONT
260  void CopyStructure(const vtkm::cont::DataSet& source);
261 
275  VTKM_CONT void ConvertToExpected();
276 
277  VTKM_CONT
278  void PrintSummary(std::ostream& out) const;
279 
280 private:
282  {
283  using Key = std::pair<std::string, vtkm::cont::Field::Association>;
284 
285  template <typename T>
286  bool operator()(const T& a, const T& b) const
287  {
288  if (a.first == b.first)
289  return a.second < b.second && a.second != vtkm::cont::Field::Association::Any &&
291 
292  return a.first < b.first;
293  }
294  };
295 
296  std::vector<vtkm::cont::CoordinateSystem> CoordSystems;
297  std::map<FieldCompare::Key, vtkm::cont::Field, FieldCompare> Fields;
299 
300  VTKM_CONT
301  vtkm::Id FindFieldIndex(const std::string& name,
302  vtkm::cont::Field::Association association,
303  bool& found) const;
304 };
305 
306 } // namespace cont
307 } // namespace vtkm
308 
309 //=============================================================================
310 // Specializations of serialization related classes
312 namespace vtkm
313 {
314 namespace cont
315 {
316 
317 template <typename FieldTypeList = VTKM_DEFAULT_TYPE_LIST,
318  typename CellSetTypesList = VTKM_DEFAULT_CELL_SET_LIST>
319 struct SerializableDataSet
320 {
321  SerializableDataSet() = default;
322 
323  explicit SerializableDataSet(const vtkm::cont::DataSet& dataset)
324  : DataSet(dataset)
325  {
326  }
327 
328  vtkm::cont::DataSet DataSet;
329 };
330 }
331 } // vtkm::cont
332 
333 namespace mangled_diy_namespace
334 {
335 
336 template <typename FieldTypeList, typename CellSetTypesList>
337 struct Serialization<vtkm::cont::SerializableDataSet<FieldTypeList, CellSetTypesList>>
338 {
339 private:
340  using Type = vtkm::cont::SerializableDataSet<FieldTypeList, CellSetTypesList>;
341 
342 public:
343  static VTKM_CONT void save(BinaryBuffer& bb, const Type& serializable)
344  {
345  const auto& dataset = serializable.DataSet;
346 
347  vtkm::IdComponent numberOfCoordinateSystems = dataset.GetNumberOfCoordinateSystems();
348  vtkmdiy::save(bb, numberOfCoordinateSystems);
349  for (vtkm::IdComponent i = 0; i < numberOfCoordinateSystems; ++i)
350  {
351  vtkmdiy::save(bb, dataset.GetCoordinateSystem(i));
352  }
353 
354  vtkmdiy::save(bb, dataset.GetCellSet().ResetCellSetList(CellSetTypesList{}));
355 
356  vtkm::IdComponent numberOfFields = dataset.GetNumberOfFields();
357  vtkmdiy::save(bb, numberOfFields);
358  for (vtkm::IdComponent i = 0; i < numberOfFields; ++i)
359  {
360  vtkmdiy::save(bb, dataset.GetField(i));
361  }
362  }
363 
364  static VTKM_CONT void load(BinaryBuffer& bb, Type& serializable)
365  {
366  auto& dataset = serializable.DataSet;
367  dataset = {}; // clear
368 
369  vtkm::IdComponent numberOfCoordinateSystems = 0;
370  vtkmdiy::load(bb, numberOfCoordinateSystems);
371  for (vtkm::IdComponent i = 0; i < numberOfCoordinateSystems; ++i)
372  {
374  vtkmdiy::load(bb, coords);
375  dataset.AddCoordinateSystem(coords);
376  }
377 
379  vtkmdiy::load(bb, cells);
380  dataset.SetCellSet(cells);
381 
382  vtkm::IdComponent numberOfFields = 0;
383  vtkmdiy::load(bb, numberOfFields);
384  for (vtkm::IdComponent i = 0; i < numberOfFields; ++i)
385  {
386  vtkm::cont::Field field;
387  vtkmdiy::load(bb, field);
388  dataset.AddField(field);
389  }
390  }
391 };
392 
393 } // diy
395 
396 #endif //vtk_m_cont_DataSet_h
vtkm::cont::DataSet::GetCellSet
VTKM_CONT vtkm::cont::UnknownCellSet & GetCellSet()
Definition: DataSet.h:243
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:287
ArrayHandle.h
vtkm::cont::DataSet::HasPointField
VTKM_CONT bool HasPointField(const std::string &name) const
Definition: DataSet.h:67
vtkm::cont::DataSet::AddCellField
VTKM_CONT void AddCellField(const std::string &fieldName, const std::vector< T > &field)
Definition: DataSet.h:176
vtkm
VTKM_NO_DEPRECATED_VIRTUAL.
Definition: Algorithms.h:18
vtkm::cont::DataSet::AddPointField
VTKM_CONT void AddPointField(const std::string &fieldName, const vtkm::cont::ArrayHandle< T, Storage > &field)
Definition: DataSet.h:141
VTKM_IS_KNOWN_OR_UNKNOWN_CELL_SET
#define VTKM_IS_KNOWN_OR_UNKNOWN_CELL_SET(T)
Definition: UnknownCellSet.h:356
UnknownCellSet.h
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
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:188
vtkm::cont::DataSet::GetCoordinateSystems
VTKM_CONT std::vector< vtkm::cont::CoordinateSystem > GetCoordinateSystems() const
Returns an std::vector of CoordinateSystems held in this DataSet.
Definition: DataSet.h:227
vtkm::cont::UnknownArrayHandle
An ArrayHandle of an unknown value type and storage.
Definition: UnknownArrayHandle.h:397
vtkm::cont::UncertainCellSet
A CellSet of an uncertain type.
Definition: UncertainCellSet.h:38
vtkm::cont::DataSet
Definition: DataSet.h:27
vtkm::cont::UnknownCellSet
A CellSet of an unknown type.
Definition: UnknownCellSet.h:50
vtkm::cont::DataSet::AddCellField
VTKM_CONT void AddCellField(const std::string &fieldName, const vtkm::cont::UnknownArrayHandle &field)
Definition: DataSet.h:163
CoordinateSystem.h
vtkm::cont::DataSet::FieldCompare::operator()
bool operator()(const T &a, const T &b) const
Definition: DataSet.h:286
mangled_diy_namespace
Definition: Particle.h:251
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::CoordinateSystem
Definition: CoordinateSystem.h:128
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:265
vtkm::cont::DataSet::AddPointField
VTKM_CONT void AddPointField(const std::string &fieldName, const T *field, const vtkm::Id &n)
Definition: DataSet.h:155
vtkm::cont::DataSet::HasCellField
VTKM_CONT bool HasCellField(const std::string &name) const
Definition: DataSet.h:59
UnknownArrayHandle.h
vtkm::cont::DataSet::CoordSystems
std::vector< vtkm::cont::CoordinateSystem > CoordSystems
Definition: DataSet.h:296
vtkm::cont::DataSet::CellSet
vtkm::cont::UnknownCellSet CellSet
Definition: DataSet.h:298
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
vtkm::cont::DataSet::GetNumberOfCoordinateSystems
VTKM_CONT vtkm::IdComponent GetNumberOfCoordinateSystems() const
Definition: DataSet.h:252
vtkm::cont::Field::Association::Any
@ Any
vtkm::cont::Field::Association::Points
@ Points
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::DataSet::AddCellField
VTKM_CONT void AddCellField(const std::string &fieldName, const T *field, const vtkm::Id &n)
Definition: DataSet.h:183
vtkm::cont::DataSet::GetPointField
const VTKM_CONT vtkm::cont::Field & GetPointField(const std::string &name) const
Returns the first point field that matches the provided name.
Definition: DataSet.h:122
vtkm::cont::CellSet
Definition: CellSet.h:24
vtkm::CopyFlag::On
@ On
vtkm::cont::DataSet::AddCoordinateSystem
VTKM_CONT void AddCoordinateSystem(const vtkm::cont::CoordinateSystem &cs)
Definition: DataSet.h:191
vtkm::cont::DataSet::HasCoordinateSystem
VTKM_CONT bool HasCoordinateSystem(const std::string &name) const
Definition: DataSet.h:197
vtkm::cont::DataSet::AddPointField
VTKM_CONT void AddPointField(const std::string &fieldName, const std::vector< T > &field)
Definition: DataSet.h:148
vtkm::cont::DataSet::FieldCompare
Definition: DataSet.h:281
vtkm::cont::Field::Association::Cells
@ Cells
ErrorBadValue.h
Field.h
vtkm::cont::DataSet::GetCellField
VTKM_CONT vtkm::cont::Field & GetCellField(const std::string &name)
Definition: DataSet.h:112
vtkm::cont::DataSet::SetCellSet
VTKM_CONT void SetCellSet(const CellSetType &cellSet)
Definition: DataSet.h:233
vtkm::cont::DataSet::GetNumberOfFields
VTKM_CONT vtkm::IdComponent GetNumberOfFields() const
Definition: DataSet.h:246
vtkm::cont::DataSet::AddCellField
VTKM_CONT void AddCellField(const std::string &fieldName, const vtkm::cont::ArrayHandle< T, Storage > &field)
Definition: DataSet.h:169
vtkm::cont::DataSet::GetField
const VTKM_CONT vtkm::cont::Field & GetField(const std::string &name, vtkm::cont::Field::Association assoc=vtkm::cont::Field::Association::Any) const
Returns the field that matches the provided name and association Will throw an exception if no match ...
Definition: DataSet.h:86
vtkm::cont::DataSet::HasField
VTKM_CONT bool HasField(const std::string &name, vtkm::cont::Field::Association assoc=vtkm::cont::Field::Association::Any) const
Definition: DataSet.h:50
vtkm::cont::DataSet::GetField
VTKM_CONT vtkm::cont::Field & GetField(const std::string &name, vtkm::cont::Field::Association assoc=vtkm::cont::Field::Association::Any)
Definition: DataSet.h:94
vtkm::cont::DataSet::FieldCompare::Key
std::pair< std::string, vtkm::cont::Field::Association > Key
Definition: DataSet.h:283
vtkm::cont::DataSet::GetCellSet
const VTKM_CONT vtkm::cont::UnknownCellSet & GetCellSet() const
Definition: DataSet.h:240
vtkm::cont::DataSet::GetPointField
VTKM_CONT vtkm::cont::Field & GetPointField(const std::string &name)
Definition: DataSet.h:128
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:251
vtkm::cont::DataSet::Fields
std::map< FieldCompare::Key, vtkm::cont::Field, FieldCompare > Fields
Definition: DataSet.h:297
vtkm::cont::DataSet::GetCellField
const VTKM_CONT vtkm::cont::Field & GetCellField(const std::string &name) const
Returns the first cell field that matches the provided name.
Definition: DataSet.h:106
vtkm::cont::DataSet::AddPointField
VTKM_CONT void AddPointField(const std::string &fieldName, const vtkm::cont::UnknownArrayHandle &field)
Definition: DataSet.h:135