VTK-m  2.0
worklet/PointAverage.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_worklet_PointAverage_h
12 #define vtk_m_worklet_PointAverage_h
13 
15 
16 #include <vtkm/VecTraits.h>
17 
18 namespace vtkm
19 {
20 namespace worklet
21 {
22 
23 //simple functor that returns the average point value of a given
24 //cell based field.
26 {
27 public:
28  using ControlSignature = void(CellSetIn cellset,
29  FieldInCell inCellField,
30  FieldOutPoint outPointField);
31  using ExecutionSignature = void(CellCount, _2, _3);
32  using InputDomain = _1;
33 
34  template <typename CellValueVecType, typename OutType>
35  VTKM_EXEC void operator()(const vtkm::IdComponent& numCells,
36  const CellValueVecType& cellValues,
37  OutType& average) const
38  {
39  using CellValueType = typename CellValueVecType::ComponentType;
40  using InVecSize =
41  std::integral_constant<vtkm::IdComponent, vtkm::VecTraits<CellValueType>::NUM_COMPONENTS>;
42  using OutVecSize =
43  std::integral_constant<vtkm::IdComponent, vtkm::VecTraits<OutType>::NUM_COMPONENTS>;
44  using SameLengthVectors = typename std::is_same<InVecSize, OutVecSize>::type;
45 
46 
48  if (numCells != 0)
49  {
50  this->DoAverage(numCells, cellValues, average, SameLengthVectors());
51  }
52  }
53 
54 private:
55  template <typename CellValueVecType, typename OutType>
56  VTKM_EXEC void DoAverage(const vtkm::IdComponent& numCells,
57  const CellValueVecType& cellValues,
58  OutType& average,
59  std::true_type) const
60  {
61  using OutComponentType = typename vtkm::VecTraits<OutType>::ComponentType;
62  OutType sum = OutType(cellValues[0]);
63  for (vtkm::IdComponent cellIndex = 1; cellIndex < numCells; ++cellIndex)
64  {
65  // OutType constructor is for when OutType is a Vec.
66  // static_cast is for when OutType is a small int that gets promoted to int32.
67  sum = static_cast<OutType>(sum + OutType(cellValues[cellIndex]));
68  }
69 
70  // OutType constructor is for when OutType is a Vec.
71  // static_cast is for when OutType is a small int that gets promoted to int32.
72  average = static_cast<OutType>(sum / OutType(static_cast<OutComponentType>(numCells)));
73  }
74 
75  template <typename CellValueVecType, typename OutType>
77  const CellValueVecType& vtkmNotUsed(cellValues),
78  OutType& vtkmNotUsed(average),
79  std::false_type) const
80  {
81  this->RaiseError("PointAverage called with mismatched Vec sizes for PointAverage.");
82  }
83 };
84 }
85 } // namespace vtkm::worklet
86 
87 #endif // vtk_m_worklet_PointAverage_h
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::worklet::WorkletVisitPointsWithCells::FieldInCell
FieldInIncident FieldInCell
Definition: WorkletMapTopology.h:278
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::worklet::PointAverage::InputDomain
_1 InputDomain
Definition: worklet/PointAverage.h:32
vtkm::worklet::PointAverage::DoAverage
VTKM_EXEC void DoAverage(const vtkm::IdComponent &vtkmNotUsed(numCells), const CellValueVecType &vtkmNotUsed(cellValues), OutType &vtkmNotUsed(average), std::false_type) const
Definition: worklet/PointAverage.h:76
vtkm::worklet::PointAverage
Definition: worklet/PointAverage.h:25
vtkm::worklet::WorkletVisitPointsWithCells::CellCount
IncidentElementCount CellCount
Definition: WorkletMapTopology.h:286
vtkm::worklet::WorkletVisitPointsWithCells
Base class for worklets that map from Cells to Points.
Definition: WorkletMapTopology.h:274
vtkm::worklet::PointAverage::ExecutionSignature
void(CellCount, _2, _3) ExecutionSignature
Definition: worklet/PointAverage.h:31
vtkm::worklet::PointAverage::ControlSignature
void(CellSetIn cellset, FieldInCell inCellField, FieldOutPoint outPointField) ControlSignature
Definition: worklet/PointAverage.h:30
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::worklet::WorkletVisitPointsWithCells::FieldOutPoint
FieldOut FieldOutPoint
Definition: WorkletMapTopology.h:282
vtkm::VecTraits::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:73
vtkm::worklet::PointAverage::DoAverage
VTKM_EXEC void DoAverage(const vtkm::IdComponent &numCells, const CellValueVecType &cellValues, OutType &average, std::true_type) const
Definition: worklet/PointAverage.h:56
WorkletMapTopology.h
vtkm::worklet::PointAverage::operator()
VTKM_EXEC void operator()(const vtkm::IdComponent &numCells, const CellValueVecType &cellValues, OutType &average) const
Definition: worklet/PointAverage.h:35
vtkm::TypeTraits::ZeroInitialization
static VTKM_EXEC_CONT T ZeroInitialization()
Definition: TypeTraits.h:75
VecTraits.h
vtkm::exec::FunctorBase::RaiseError
VTKM_EXEC void RaiseError(const char *message) const
Definition: FunctorBase.h:40