VTK-m  2.0
worklet/Threshold.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 vtkm_m_worklet_Threshold_h
11 #define vtkm_m_worklet_Threshold_h
12 
16 
17 #include <vtkm/cont/Algorithm.h>
18 #include <vtkm/cont/ArrayHandle.h>
22 #include <vtkm/cont/Field.h>
25 
26 #include <vtkm/UnaryPredicates.h>
27 
28 namespace vtkm
29 {
30 namespace worklet
31 {
32 
33 class Threshold
34 {
35 public:
36  template <typename UnaryPredicate>
38  {
39  public:
40  using ControlSignature = void(CellSetIn cellset, FieldInPoint scalars, FieldOutCell passFlags);
41 
42  using ExecutionSignature = _3(_2, PointCount);
43 
44  VTKM_CONT
46  : Predicate()
48  {
49  }
50 
51  VTKM_CONT
52  explicit ThresholdByPointField(const UnaryPredicate& predicate, bool allPointsMustPass)
53  : Predicate(predicate)
54  , AllPointsMustPass(allPointsMustPass)
55  {
56  }
57 
58  template <typename ScalarsVecType>
59  VTKM_EXEC bool operator()(const ScalarsVecType& scalars, vtkm::Id count) const
60  {
61  bool pass = this->AllPointsMustPass ? true : false;
62  for (vtkm::IdComponent i = 0; i < count; ++i)
63  {
64  if (this->AllPointsMustPass)
65  {
66  pass &= this->Predicate(scalars[i]);
67  }
68  else
69  {
70  pass |= this->Predicate(scalars[i]);
71  }
72  }
73 
74  return pass;
75  }
76 
77  private:
78  UnaryPredicate Predicate;
80  };
81 
82  template <typename Operator>
84  {
85  public:
87  using ExecitionSignature = void(_1, _2);
88 
89  VTKM_CONT
90  explicit CombinePassFlagsWorklet(const Operator& combine)
91  : Combine(combine)
92  {
93  }
94 
95  VTKM_EXEC void operator()(bool& combined, bool incoming) const
96  {
97  combined = this->Combine(combined, incoming);
98  }
99 
100  private:
101  Operator Combine;
102  };
103 
104  template <typename Operator>
105  void CombinePassFlags(const vtkm::cont::ArrayHandle<bool>& passFlagsIn, const Operator& combine)
106  {
107  if (this->PassFlags.GetNumberOfValues() == 0) // Is initialization needed?
108  {
109  this->PassFlags = passFlagsIn;
110  }
111  else
112  {
115  dispatcher.Invoke(this->PassFlags, passFlagsIn);
116  }
117  this->PassFlagsModified = true;
118  }
119 
120  // special no-op combine operator for combining `PassFlags` results of incremental runs
121  struct NoOp
122  {
123  };
124 
126  {
127  this->PassFlags = passFlagsIn;
128  this->PassFlagsModified = true;
129  }
130 
135  template <typename ValueType,
136  typename StorageType,
137  typename UnaryPredicate,
138  typename PassFlagsCombineOp>
142  const UnaryPredicate& predicate,
143  bool allPointsMustPass, // only considered when field association is `Points`
144  const PassFlagsCombineOp& passFlagsCombineOp)
145  {
147 
148  switch (fieldType)
149  {
151  {
152  using ThresholdWorklet = ThresholdByPointField<UnaryPredicate>;
153 
154  ThresholdWorklet worklet(predicate, allPointsMustPass);
155  DispatcherMapTopology<ThresholdWorklet> dispatcher(worklet);
156  dispatcher.Invoke(cellSet, field, passFlags);
157  break;
158  }
160  {
162  passFlags);
163  break;
164  }
165  default:
166  throw vtkm::cont::ErrorBadValue("Expecting point or cell field.");
167  }
168 
169  this->CombinePassFlags(passFlags, passFlagsCombineOp);
170  }
171 
173  {
174  if (this->PassFlagsModified)
175  {
178  this->PassFlags,
179  this->ValidCellIds);
180  this->PassFlagsModified = false;
181  }
182  return this->ValidCellIds;
183  }
184 
186  {
188 
189  CastAndCall(cellSet, [&](auto concrete) {
192  });
193 
194  return output;
195  }
196 
197  // Invert the results stored in this worklet's state
199  {
202  this->PassFlagsModified = true;
203  }
204 
205  template <typename ValueType, typename StorageType, typename UnaryPredicate>
207  const vtkm::cont::UnknownCellSet& cellSet,
210  const UnaryPredicate& predicate,
211  bool allPointsMustPass = false, // only considered when field association is `Points`
212  bool invert = false)
213  {
214  this->RunIncremental(cellSet, field, fieldType, predicate, allPointsMustPass, NoOp{});
215  if (invert)
216  {
217  this->InvertResults();
218  }
219  return this->GenerateResultCellSet(cellSet);
220  }
221 
222 private:
224 
225  mutable bool PassFlagsModified = true;
227 };
228 }
229 } // namespace vtkm::worklet
230 
231 #endif // vtkm_m_worklet_Threshold_h
vtkm::worklet::Threshold::ThresholdByPointField::ExecutionSignature
_3(_2, PointCount) ExecutionSignature
Definition: worklet/Threshold.h:42
vtkm::cont::ArrayHandle::GetNumberOfValues
VTKM_CONT vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:448
vtkm::worklet::Threshold::CombinePassFlagsWorklet::ControlSignature
void(FieldInOut, FieldIn) ControlSignature
Definition: worklet/Threshold.h:86
vtkm::cont::ArrayHandle< bool >
vtkm::worklet::Threshold::Run
vtkm::cont::UnknownCellSet Run(const vtkm::cont::UnknownCellSet &cellSet, const vtkm::cont::ArrayHandle< ValueType, StorageType > &field, vtkm::cont::Field::Association fieldType, const UnaryPredicate &predicate, bool allPointsMustPass=false, bool invert=false)
Definition: worklet/Threshold.h:206
ArrayHandle.h
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm::worklet::Threshold::CombinePassFlagsWorklet::CombinePassFlagsWorklet
VTKM_CONT CombinePassFlagsWorklet(const Operator &combine)
Definition: worklet/Threshold.h:90
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::worklet::Threshold::CombinePassFlags
void CombinePassFlags(const vtkm::cont::ArrayHandle< bool > &passFlagsIn, const Operator &combine)
Definition: worklet/Threshold.h:105
WorkletMapField.h
vtkm::worklet::Threshold::ThresholdByPointField::Predicate
UnaryPredicate Predicate
Definition: worklet/Threshold.h:78
UnknownCellSet.h
UnaryPredicates.h
vtkm::worklet::WorkletVisitCellsWithPoints::PointCount
IncidentElementCount PointCount
Definition: WorkletMapTopology.h:267
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::worklet::Threshold::ThresholdByPointField::AllPointsMustPass
bool AllPointsMustPass
Definition: worklet/Threshold.h:79
ArrayHandleTransform.h
vtkm::worklet::Threshold::RunIncremental
void RunIncremental(const vtkm::cont::UnknownCellSet &cellSet, const vtkm::cont::ArrayHandle< ValueType, StorageType > &field, vtkm::cont::Field::Association fieldType, const UnaryPredicate &predicate, bool allPointsMustPass, const PassFlagsCombineOp &passFlagsCombineOp)
Incrementally run the worklet on the given parameters.
Definition: worklet/Threshold.h:139
vtkm::cont::Algorithm::Copy
static VTKM_CONT bool Copy(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CIn > &input, vtkm::cont::ArrayHandle< U, COut > &output)
Definition: Algorithm.h:410
vtkm::LogicalNot
Predicate that takes a single argument x, and returns True if and only if x is false.
Definition: UnaryPredicates.h:45
vtkm::cont::UnknownCellSet
A CellSet of an unknown type.
Definition: UnknownCellSet.h:48
UncertainCellSet.h
vtkm::cont::CastAndCall
void CastAndCall(const DynamicObject &dynamicObject, Functor &&f, Args &&... args)
A Generic interface to CastAndCall.
Definition: CastAndCall.h:47
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
CellDeepCopy.h
vtkm::worklet::Threshold::CombinePassFlagsWorklet::Combine
Operator Combine
Definition: worklet/Threshold.h:101
vtkm::worklet::Threshold::CombinePassFlagsWorklet::ExecitionSignature
void(_1, _2) ExecitionSignature
Definition: worklet/Threshold.h:87
vtkm::worklet::Threshold::GetValidCellIds
vtkm::cont::ArrayHandle< vtkm::Id > GetValidCellIds() const
Definition: worklet/Threshold.h:172
vtkm::worklet::Threshold::ThresholdByPointField::ThresholdByPointField
VTKM_CONT ThresholdByPointField(const UnaryPredicate &predicate, bool allPointsMustPass)
Definition: worklet/Threshold.h:52
vtkm::worklet::DispatcherMapField
Dispatcher for worklets that inherit from WorkletMapField.
Definition: DispatcherMapField.h:25
vtkm::cont::make_ArrayHandleTransform
VTKM_CONT vtkm::cont::ArrayHandleTransform< HandleType, FunctorType > make_ArrayHandleTransform(HandleType handle, FunctorType functor)
make_ArrayHandleTransform is convenience function to generate an ArrayHandleTransform.
Definition: ArrayHandleTransform.h:474
Algorithm.h
vtkm::worklet::DispatcherMapTopology
Dispatcher for worklets that inherit from WorkletMapTopology.
Definition: DispatcherMapTopology.h:31
vtkm::worklet::WorkletMapField::FieldIn
A control signature tag for input fields.
Definition: WorkletMapField.h:49
ArrayHandleIndex.h
vtkm::worklet::WorkletVisitCellsWithPoints
Base class for worklets that map from Points to Cells.
Definition: WorkletMapTopology.h:255
vtkm::worklet::Threshold::ValidCellIds
vtkm::cont::ArrayHandle< vtkm::Id > ValidCellIds
Definition: worklet/Threshold.h:226
vtkm::cont::Algorithm::CopyIf
static VTKM_CONT void CopyIf(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CIn > &input, const vtkm::cont::ArrayHandle< U, CStencil > &stencil, vtkm::cont::ArrayHandle< T, COut > &output)
Definition: Algorithm.h:435
vtkm::worklet::Threshold::ThresholdByPointField::operator()
VTKM_EXEC bool operator()(const ScalarsVecType &scalars, vtkm::Id count) const
Definition: worklet/Threshold.h:59
vtkm::cont::Field::Association::Points
@ Points
vtkm::worklet::WorkletVisitCellsWithPoints::FieldInPoint
FieldInIncident FieldInPoint
Definition: WorkletMapTopology.h:259
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::worklet::Threshold::CombinePassFlags
void CombinePassFlags(const vtkm::cont::ArrayHandle< bool > &passFlagsIn, NoOp)
Definition: worklet/Threshold.h:125
vtkm::worklet::WorkletMapField::FieldInOut
A control signature tag for input-output (in-place) fields.
Definition: WorkletMapField.h:71
vtkm::worklet::Threshold::PassFlagsModified
bool PassFlagsModified
Definition: worklet/Threshold.h:225
vtkm::worklet::Threshold::InvertResults
void InvertResults()
Definition: worklet/Threshold.h:198
vtkm::worklet::Threshold::ThresholdByPointField::ThresholdByPointField
VTKM_CONT ThresholdByPointField()
Definition: worklet/Threshold.h:45
vtkm::worklet::Threshold
Definition: worklet/Threshold.h:33
vtkm::worklet::Threshold::GenerateResultCellSet
vtkm::cont::UnknownCellSet GenerateResultCellSet(const vtkm::cont::UnknownCellSet &cellSet)
Definition: worklet/Threshold.h:185
CellSetPermutation.h
vtkm::worklet::Threshold::ThresholdByPointField
Definition: worklet/Threshold.h:37
vtkm::worklet::CellDeepCopy::Run
static VTKM_CONT void Run(const InCellSetType &inCellSet, vtkm::cont::CellSetExplicit< ShapeStorage, ConnectivityStorage, OffsetsStorage > &outCellSet, vtkm::Id numberOfPoints)
Definition: CellDeepCopy.h:68
vtkm::worklet::Threshold::PassFlags
vtkm::cont::ArrayHandle< bool > PassFlags
Definition: worklet/Threshold.h:223
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::worklet::Threshold::NoOp
Definition: worklet/Threshold.h:121
vtkm::cont::Field::Association::Cells
@ Cells
Field.h
vtkm::worklet::Threshold::CombinePassFlagsWorklet
Definition: worklet/Threshold.h:83
vtkm::worklet::Threshold::CombinePassFlagsWorklet::operator()
VTKM_EXEC void operator()(bool &combined, bool incoming) const
Definition: worklet/Threshold.h:95
vtkm::worklet::Threshold::ThresholdByPointField::ControlSignature
void(CellSetIn cellset, FieldInPoint scalars, FieldOutCell passFlags) ControlSignature
Definition: worklet/Threshold.h:40
vtkm::cont::make_CellSetPermutation
vtkm::cont::CellSetPermutation< OriginalCellSet, PermutationArrayHandleType > make_CellSetPermutation(const PermutationArrayHandleType &cellIndexMap, const OriginalCellSet &cellSet)
Definition: CellSetPermutation.h:549
WorkletMapTopology.h
vtkm::cont::ArrayHandleIndex
An implicit array handle containing the its own indices.
Definition: ArrayHandleIndex.h:54
vtkm::worklet::WorkletMapField
Base class for worklets that do a simple mapping of field arrays.
Definition: WorkletMapField.h:38
vtkm::worklet::WorkletVisitCellsWithPoints::FieldOutCell
FieldOut FieldOutCell
Definition: WorkletMapTopology.h:263