VTK-m  2.0
FilterField.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 
11 #ifndef vtk_m_filter_FilterField_h
12 #define vtk_m_filter_FilterField_h
13 
14 #include <vtkm/filter/Filter.h>
15 
16 namespace vtkm
17 {
18 namespace filter
19 {
20 
21 class VTKM_FILTER_CORE_EXPORT FilterField : public vtkm::filter::Filter
22 {
23 public:
24  FilterField() { this->SetActiveCoordinateSystem(0); }
25 
26  VTKM_CONT
27  void SetOutputFieldName(const std::string& name) { this->OutputFieldName = name; }
28 
29  VTKM_CONT
30  const std::string& GetOutputFieldName() const { return this->OutputFieldName; }
31 
35  VTKM_CONT
37  const std::string& name,
39  {
40  this->SetActiveField(0, name, association);
41  }
42 
44  vtkm::IdComponent index,
45  const std::string& name,
47  {
48  auto index_st = static_cast<std::size_t>(index);
49  ResizeIfNeeded(index_st);
50  this->ActiveFieldNames[index_st] = name;
51  this->ActiveFieldAssociation[index_st] = association;
52  }
53 
54  VTKM_CONT const std::string& GetActiveFieldName(vtkm::IdComponent index = 0) const
55  {
56  VTKM_ASSERT((index >= 0) &&
57  (index < static_cast<vtkm::IdComponent>(this->ActiveFieldNames.size())));
58  return this->ActiveFieldNames[index];
59  }
60 
62  vtkm::IdComponent index = 0) const
63  {
64  return this->ActiveFieldAssociation[index];
65  }
67 
72  VTKM_CONT
74  {
75  this->SetActiveCoordinateSystem(0, coord_idx);
76  }
77 
78  VTKM_CONT
80  {
81  auto index_st = static_cast<std::size_t>(index);
82  ResizeIfNeeded(index_st);
83  this->ActiveCoordinateSystemIndices[index_st] = coord_idx;
84  }
85 
86  VTKM_CONT
88  {
89  return this->GetActiveCoordinateSystemIndex(0);
90  }
91 
92  VTKM_CONT
94  {
95  auto index_st = static_cast<std::size_t>(index);
96  return this->ActiveCoordinateSystemIndices[index_st];
97  }
99 
103  VTKM_CONT
105 
106  VTKM_CONT
108  {
109  auto index_st = static_cast<std::size_t>(index);
110  this->ResizeIfNeeded(index_st);
111  this->UseCoordinateSystemAsField[index] = val;
112  }
113 
114  VTKM_CONT
116  {
117  VTKM_ASSERT((index >= 0) &&
118  (index < static_cast<vtkm::IdComponent>(this->ActiveFieldNames.size())));
119  return this->UseCoordinateSystemAsField[index];
120  }
122 
123 protected:
124  VTKM_CONT
126  {
127  return this->GetFieldFromDataSet(0, input);
128  }
129 
130  VTKM_CONT
132  const vtkm::cont::DataSet& input) const
133  {
134  if (this->UseCoordinateSystemAsField[index])
135  {
136  // Note that we cannot use input.GetCoordinateSystem because that does not return
137  // a reference to a field. Instead, get the field name for the coordinate system
138  // and return the field.
139  const std::string& coordSystemName =
140  input.GetCoordinateSystemName(this->GetActiveCoordinateSystemIndex(index));
141  return input.GetPointField(coordSystemName);
142  }
143  else
144  {
145  return input.GetField(this->GetActiveFieldName(index),
146  this->GetActiveFieldAssociation(index));
147  }
148  }
149 
150  template <typename Functor, typename... Args>
152  Functor&& functor,
153  Args&&... args) const
154  {
155  fieldArray
157  std::forward<Functor>(functor), std::forward<Args>(args)...);
158  }
159 
160  template <typename Functor, typename... Args>
162  Functor&& functor,
163  Args&&... args) const
164  {
165  this->CastAndCallScalarField(
166  field.GetData(), std::forward<Functor>(functor), std::forward<Args>(args)...);
167  }
168 
169 
170 private:
171  template <vtkm::IdComponent VecSize>
172  struct ScalarToVec
173  {
174  template <typename T>
176  };
177 
178 protected:
179  template <vtkm::IdComponent VecSize, typename Functor, typename... Args>
181  Functor&& functor,
182  Args&&... args) const
183  {
184  using VecList =
186  fieldArray.CastAndCallForTypesWithFloatFallback<VecList, VTKM_DEFAULT_STORAGE_LIST>(
187  std::forward<Functor>(functor), std::forward<Args>(args)...);
188  }
189 
190  template <vtkm::IdComponent VecSize, typename Functor, typename... Args>
192  Functor&& functor,
193  Args&&... args) const
194  {
195  this->CastAndCallVecField<VecSize>(
196  field.GetData(), std::forward<Functor>(functor), std::forward<Args>(args)...);
197  }
198 
211  VTKM_CONT vtkm::cont::DataSet CreateResultField(const vtkm::cont::DataSet& inDataSet,
212  const vtkm::cont::Field& resultField) const;
213 
231  const vtkm::cont::DataSet& inDataSet,
232  const std::string& resultFieldName,
233  vtkm::cont::Field::Association resultFieldAssociation,
234  const vtkm::cont::UnknownArrayHandle& resultFieldArray) const
235  {
236  return this->CreateResultField(
237  inDataSet, vtkm::cont::Field{ resultFieldName, resultFieldAssociation, resultFieldArray });
238  }
239 
255  const vtkm::cont::DataSet& inDataSet,
256  const std::string& resultFieldName,
257  const vtkm::cont::UnknownArrayHandle& resultFieldArray) const
258  {
259  return this->CreateResultField(inDataSet,
260  vtkm::cont::Field{ resultFieldName,
262  resultFieldArray });
263  }
264 
280  const vtkm::cont::DataSet& inDataSet,
281  const std::string& resultFieldName,
282  const vtkm::cont::UnknownArrayHandle& resultFieldArray) const
283  {
284  return this->CreateResultField(inDataSet,
285  vtkm::cont::Field{ resultFieldName,
287  resultFieldArray });
288  }
289 
290 private:
291  void ResizeIfNeeded(size_t index_st);
292 
293  std::string OutputFieldName;
294 
295  std::vector<std::string> ActiveFieldNames;
296  std::vector<vtkm::cont::Field::Association> ActiveFieldAssociation;
297  std::vector<bool> UseCoordinateSystemAsField;
298  std::vector<vtkm::Id> ActiveCoordinateSystemIndices;
299 };
300 } // namespace filter
301 } // namespace vtkm
302 
303 #endif // vtk_m_filter_FilterField_h
vtkm::filter::FilterField::UseCoordinateSystemAsField
std::vector< bool > UseCoordinateSystemAsField
Definition: FilterField.h:297
vtkm::filter::FilterField::CastAndCallVecField
VTKM_CONT void CastAndCallVecField(const vtkm::cont::Field &field, Functor &&functor, Args &&... args) const
Definition: FilterField.h:191
vtkm::filter::FilterField::SetActiveField
VTKM_CONT void SetActiveField(const std::string &name, vtkm::cont::Field::Association association=vtkm::cont::Field::Association::Any)
Definition: FilterField.h:36
vtkm::filter::FilterField::GetActiveFieldAssociation
VTKM_CONT vtkm::cont::Field::Association GetActiveFieldAssociation(vtkm::IdComponent index=0) const
Definition: FilterField.h:61
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
vtkm::cont::UnknownArrayHandle::CastAndCallForTypesWithFloatFallback
VTKM_CONT void CastAndCallForTypesWithFloatFallback(Functor &&functor, Args &&... args) const
Call a functor using the underlying array type with a float cast fallback.
Definition: UnknownArrayHandle.h:1051
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::UnknownArrayHandle
An ArrayHandle of an unknown value type and storage.
Definition: UnknownArrayHandle.h:406
vtkm::cont::DataSet
Definition: DataSet.h:34
vtkm::cont::DataSet::GetCoordinateSystemName
const VTKM_CONT std::string & GetCoordinateSystemName(vtkm::Id index=0) const
vtkm::filter::FilterField::GetUseCoordinateSystemAsField
VTKM_CONT bool GetUseCoordinateSystemAsField(vtkm::IdComponent index=0) const
Definition: FilterField.h:115
vtkm::filter::FilterField::CastAndCallScalarField
VTKM_CONT void CastAndCallScalarField(const vtkm::cont::Field &field, Functor &&functor, Args &&... args) const
Definition: FilterField.h:161
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::cont::Field::GetData
const vtkm::cont::UnknownArrayHandle & GetData() const
vtkm::filter::FilterField::GetFieldFromDataSet
const VTKM_CONT vtkm::cont::Field & GetFieldFromDataSet(const vtkm::cont::DataSet &input) const
Definition: FilterField.h:125
vtkm::filter::FilterField
Definition: FilterField.h:21
vtkm::filter::FilterField::GetActiveFieldName
const VTKM_CONT std::string & GetActiveFieldName(vtkm::IdComponent index=0) const
Definition: FilterField.h:54
vtkm::ListTransform
typename detail::ListTransformImpl< List, Transform >::type ListTransform
Constructs a list containing all types in a source list applied to a transform template.
Definition: List.h:592
vtkm::filter::FilterField::SetActiveCoordinateSystem
VTKM_CONT void SetActiveCoordinateSystem(vtkm::Id coord_idx)
Definition: FilterField.h:73
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::filter::FilterField::SetOutputFieldName
VTKM_CONT void SetOutputFieldName(const std::string &name)
Definition: FilterField.h:27
vtkm::filter::FilterField::OutputFieldName
std::string OutputFieldName
Definition: FilterField.h:293
vtkm::filter::FilterField::SetActiveField
void SetActiveField(vtkm::IdComponent index, const std::string &name, vtkm::cont::Field::Association association=vtkm::cont::Field::Association::Any)
Definition: FilterField.h:43
vtkm::filter::FilterField::CastAndCallScalarField
VTKM_CONT void CastAndCallScalarField(const vtkm::cont::UnknownArrayHandle &fieldArray, Functor &&functor, Args &&... args) const
Definition: FilterField.h:151
vtkm::filter::Filter
base class for all filters.
Definition: Filter.h:218
vtkm::filter::FilterField::SetUseCoordinateSystemAsField
VTKM_CONT void SetUseCoordinateSystemAsField(vtkm::IdComponent index, bool val)
Definition: FilterField.h:107
vtkm::cont::Field::Association::Any
@ Any
vtkm::cont::Field::Association::Points
@ Points
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
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:179
vtkm::filter::FilterField::ActiveFieldAssociation
std::vector< vtkm::cont::Field::Association > ActiveFieldAssociation
Definition: FilterField.h:296
vtkm::filter::FilterField::GetActiveCoordinateSystemIndex
VTKM_CONT vtkm::Id GetActiveCoordinateSystemIndex(vtkm::IdComponent index) const
Definition: FilterField.h:93
vtkm::filter::FilterField::CastAndCallVecField
VTKM_CONT void CastAndCallVecField(const vtkm::cont::UnknownArrayHandle &fieldArray, Functor &&functor, Args &&... args) const
Definition: FilterField.h:180
vtkm::filter::FilterField::FilterField
FilterField()
Definition: FilterField.h:24
vtkm::filter::FilterField::ScalarToVec
Definition: FilterField.h:172
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::filter::FilterField::ActiveCoordinateSystemIndices
std::vector< vtkm::Id > ActiveCoordinateSystemIndices
Definition: FilterField.h:298
vtkm::filter::FilterField::CreateResultFieldCell
VTKM_CONT vtkm::cont::DataSet CreateResultFieldCell(const vtkm::cont::DataSet &inDataSet, const std::string &resultFieldName, const vtkm::cont::UnknownArrayHandle &resultFieldArray) const
Create the output data set for DoExecute
Definition: FilterField.h:279
vtkm::cont::Field::Association::Cells
@ Cells
vtkm::filter::FilterField::CreateResultFieldPoint
VTKM_CONT vtkm::cont::DataSet CreateResultFieldPoint(const vtkm::cont::DataSet &inDataSet, const std::string &resultFieldName, const vtkm::cont::UnknownArrayHandle &resultFieldArray) const
Create the output data set for DoExecute
Definition: FilterField.h:254
vtkm::filter::FilterField::SetActiveCoordinateSystem
VTKM_CONT void SetActiveCoordinateSystem(vtkm::IdComponent index, vtkm::Id coord_idx)
Definition: FilterField.h:79
vtkm::filter::FilterField::GetOutputFieldName
const VTKM_CONT std::string & GetOutputFieldName() const
Definition: FilterField.h:30
vtkm::cont::DataSet::GetField
const VTKM_CONT vtkm::cont::Field & GetField(vtkm::Id index) const
Retrieves a field by index.
Definition: DataSet.h:75
vtkm::filter::FilterField::GetActiveCoordinateSystemIndex
VTKM_CONT vtkm::Id GetActiveCoordinateSystemIndex() const
Definition: FilterField.h:87
vtkm::filter::FilterField::GetFieldFromDataSet
const VTKM_CONT vtkm::cont::Field & GetFieldFromDataSet(vtkm::IdComponent index, const vtkm::cont::DataSet &input) const
Definition: FilterField.h:131
vtkm::filter::FilterField::CreateResultField
VTKM_CONT vtkm::cont::DataSet CreateResultField(const vtkm::cont::DataSet &inDataSet, const std::string &resultFieldName, vtkm::cont::Field::Association resultFieldAssociation, const vtkm::cont::UnknownArrayHandle &resultFieldArray) const
Create the output data set for DoExecute
Definition: FilterField.h:230
vtkm::filter::FilterField::ActiveFieldNames
std::vector< std::string > ActiveFieldNames
Definition: FilterField.h:295
vtkm::filter::FilterField::SetUseCoordinateSystemAsField
VTKM_CONT void SetUseCoordinateSystemAsField(bool val)
Definition: FilterField.h:104
vtkm::TypeListFieldScalar
vtkm::List< vtkm::Float32, vtkm::Float64 > TypeListFieldScalar
A list containing types used for scalar fields.
Definition: TypeList.h:47
Filter.h