VTK-m  2.0
ComputeNDHistogram.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_ComputeNDHistogram_h
12 #define vtk_m_worklet_ComputeNDHistogram_h
13 
14 #include <vtkm/cont/Algorithm.h>
16 
19 
20 namespace vtkm
21 {
22 namespace worklet
23 {
24 namespace histogram
25 {
26 // GCC creates false positive warnings for signed/unsigned char* operations.
27 // This occurs because the values are implicitly casted up to int's for the
28 // operation, and than casted back down to char's when return.
29 // This causes a false positive warning, even when the values is within
30 // the value types range
31 #if defined(VTKM_GCC)
32 #pragma GCC diagnostic push
33 #pragma GCC diagnostic ignored "-Wconversion"
34 #endif // gcc
35 template <typename T>
36 T compute_delta(T fieldMinValue, T fieldMaxValue, vtkm::Id num)
37 {
38  using VecType = vtkm::VecTraits<T>;
39  const T fieldRange = fieldMaxValue - fieldMinValue;
40  return fieldRange / static_cast<typename VecType::ComponentType>(num);
41 }
42 #if defined(VTKM_GCC)
43 #pragma GCC diagnostic pop
44 #endif // gcc
45 
46 // For each value set the bin it should be in
47 template <typename FieldType>
49 {
50 public:
51  using ControlSignature = void(FieldIn value, FieldIn binIndexIn, FieldOut binIndexOut);
52  using ExecutionSignature = void(_1, _2, _3);
53  using InputDomain = _1;
54 
58 
59  VTKM_CONT
60  SetHistogramBin(vtkm::Id numberOfBins0, vtkm::Float64 minValue0, vtkm::Float64 delta0)
61  : numberOfBins(numberOfBins0)
62  , minValue(minValue0)
63  , delta(delta0)
64  {
65  }
66 
67  VTKM_EXEC
68  void operator()(const FieldType& value, const vtkm::Id& binIndexIn, vtkm::Id& binIndexOut) const
69  {
70  const vtkm::Float64 fvalue = static_cast<vtkm::Float64>(value);
71  vtkm::Id localBinIdx = static_cast<vtkm::Id>((fvalue - minValue) / delta);
72  if (localBinIdx < 0)
73  localBinIdx = 0;
74  else if (localBinIdx >= numberOfBins)
75  localBinIdx = numberOfBins - 1;
76 
77  binIndexOut = binIndexIn * numberOfBins + localBinIdx;
78  }
79 };
80 
82 {
83 public:
84  VTKM_CONT
86  vtkm::Id& _numOfBins,
87  vtkm::Range& _minMax,
88  vtkm::Float64& _binDelta)
89  : Bin1DIdx(_bin1DIdx)
90  , NumOfBins(_numOfBins)
91  , MinMax(_minMax)
92  , BinDelta(_binDelta)
93  , RangeProvided(false)
94  {
95  }
96 
97  VTKM_CONT
99  vtkm::Id& _numOfBins,
100  vtkm::Range& _minMax,
101  vtkm::Float64& _binDelta,
102  bool _rangeProvided)
103  : Bin1DIdx(_bin1DIdx)
104  , NumOfBins(_numOfBins)
105  , MinMax(_minMax)
106  , BinDelta(_binDelta)
107  , RangeProvided(_rangeProvided)
108  {
109  }
110 
111  template <typename T, typename Storage>
113  {
114  if (!RangeProvided)
115  {
116  const vtkm::Vec<T, 2> initValue(vtkm::cont::ArrayGetValue(0, field));
117  vtkm::Vec<T, 2> minMax =
119  MinMax.Min = static_cast<vtkm::Float64>(minMax[0]);
120  MinMax.Max = static_cast<vtkm::Float64>(minMax[1]);
121  }
123 
126  setHistogramBinDispatcher(binWorklet);
127  setHistogramBinDispatcher.Invoke(field, Bin1DIdx, Bin1DIdx);
128  }
129 
130 private:
136 };
137 
138 // Convert N-dims bin index into 1D index
140 {
141 public:
142  using ControlSignature = void(FieldIn bin1DIndexIn,
143  FieldOut bin1DIndexOut,
144  FieldOut oneVariableIndexOut);
145  using ExecutionSignature = void(_1, _2, _3);
146  using InputDomain = _1;
147 
149 
150  VTKM_CONT
151  explicit ConvertHistBinToND(vtkm::Id numberOfBins0)
152  : numberOfBins(numberOfBins0)
153  {
154  }
155 
156  VTKM_EXEC
157  void operator()(const vtkm::Id& bin1DIndexIn,
158  vtkm::Id& bin1DIndexOut,
159  vtkm::Id& oneVariableIndexOut) const
160  {
161  oneVariableIndexOut = bin1DIndexIn % numberOfBins;
162  bin1DIndexOut = (bin1DIndexIn - oneVariableIndexOut) / numberOfBins;
163  }
164 };
165 }
166 }
167 } // namespace vtkm::worklet
168 
169 #endif // vtk_m_worklet_ComputeNDHistogram_h
vtkm::cont::ArrayHandle< vtkm::Id >
vtkm::worklet::histogram::SetHistogramBin::ControlSignature
void(FieldIn value, FieldIn binIndexIn, FieldOut binIndexOut) ControlSignature
Definition: ComputeNDHistogram.h:51
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm::worklet::histogram::ComputeBins::MinMax
vtkm::Range & MinMax
Definition: ComputeNDHistogram.h:133
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
WorkletMapField.h
vtkm::MinAndMax
Binary Predicate that takes two arguments argument x, and y and returns a vtkm::Vec<T,...
Definition: BinaryOperators.h:112
vtkm::worklet::WorkletMapField::FieldOut
A control signature tag for output fields.
Definition: WorkletMapField.h:60
vtkm::worklet::histogram::ComputeBins::BinDelta
vtkm::Float64 & BinDelta
Definition: ComputeNDHistogram.h:134
vtkm::worklet::histogram::ComputeBins::Bin1DIdx
vtkm::cont::ArrayHandle< vtkm::Id > & Bin1DIdx
Definition: ComputeNDHistogram.h:131
vtkm::cont::ArrayGetValue
VTKM_CONT T ArrayGetValue(vtkm::Id id, const vtkm::cont::ArrayHandle< T, S > &data)
Obtain a small set of values from an ArrayHandle with minimal device transfers.
Definition: ArrayGetValues.h:264
vtkm::worklet::histogram::SetHistogramBin::SetHistogramBin
VTKM_CONT SetHistogramBin(vtkm::Id numberOfBins0, vtkm::Float64 minValue0, vtkm::Float64 delta0)
Definition: ComputeNDHistogram.h:60
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
DispatcherMapField.h
vtkm::worklet::histogram::SetHistogramBin::operator()
VTKM_EXEC void operator()(const FieldType &value, const vtkm::Id &binIndexIn, vtkm::Id &binIndexOut) const
Definition: ComputeNDHistogram.h:68
vtkm::worklet::histogram::ComputeBins::ComputeBins
VTKM_CONT ComputeBins(vtkm::cont::ArrayHandle< vtkm::Id > &_bin1DIdx, vtkm::Id &_numOfBins, vtkm::Range &_minMax, vtkm::Float64 &_binDelta, bool _rangeProvided)
Definition: ComputeNDHistogram.h:98
vtkm::worklet::histogram::ConvertHistBinToND
Definition: ComputeNDHistogram.h:139
vtkm::worklet::histogram::ConvertHistBinToND::ConvertHistBinToND
VTKM_CONT ConvertHistBinToND(vtkm::Id numberOfBins0)
Definition: ComputeNDHistogram.h:151
vtkm::worklet::histogram::compute_delta
T compute_delta(T fieldMinValue, T fieldMaxValue, vtkm::Id num)
Definition: ComputeNDHistogram.h:36
vtkm::worklet::DispatcherMapField
Dispatcher for worklets that inherit from WorkletMapField.
Definition: DispatcherMapField.h:25
vtkm::worklet::histogram::SetHistogramBin::minValue
vtkm::Float64 minValue
Definition: ComputeNDHistogram.h:56
Algorithm.h
vtkm::worklet::histogram::ComputeBins::NumOfBins
vtkm::Id & NumOfBins
Definition: ComputeNDHistogram.h:132
vtkm::worklet::WorkletMapField::FieldIn
A control signature tag for input fields.
Definition: WorkletMapField.h:49
vtkm::worklet::histogram::SetHistogramBin::delta
vtkm::Float64 delta
Definition: ComputeNDHistogram.h:57
vtkm::Vec< T, 2 >
Definition: Types.h:859
vtkm::worklet::histogram::SetHistogramBin
Definition: ComputeNDHistogram.h:48
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::worklet::histogram::SetHistogramBin::InputDomain
_1 InputDomain
Definition: ComputeNDHistogram.h:53
vtkm::worklet::histogram::SetHistogramBin::ExecutionSignature
void(_1, _2, _3) ExecutionSignature
Definition: ComputeNDHistogram.h:52
ArrayGetValues.h
vtkm::worklet::histogram::ConvertHistBinToND::InputDomain
_1 InputDomain
Definition: ComputeNDHistogram.h:146
vtkm::worklet::histogram::ConvertHistBinToND::operator()
VTKM_EXEC void operator()(const vtkm::Id &bin1DIndexIn, vtkm::Id &bin1DIndexOut, vtkm::Id &oneVariableIndexOut) const
Definition: ComputeNDHistogram.h:157
vtkm::Range::Min
vtkm::Float64 Min
Definition: Range.h:33
vtkm::cont::Algorithm::Reduce
static VTKM_CONT U Reduce(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CIn > &input, U initialValue)
Definition: Algorithm.h:656
vtkm::Float64
double Float64
Definition: Types.h:155
vtkm::worklet::histogram::SetHistogramBin::numberOfBins
vtkm::Id numberOfBins
Definition: ComputeNDHistogram.h:55
vtkm::Range::Max
vtkm::Float64 Max
Definition: Range.h:34
vtkm::VecTraits< T >
vtkm::worklet::histogram::ComputeBins::ComputeBins
VTKM_CONT ComputeBins(vtkm::cont::ArrayHandle< vtkm::Id > &_bin1DIdx, vtkm::Id &_numOfBins, vtkm::Range &_minMax, vtkm::Float64 &_binDelta)
Definition: ComputeNDHistogram.h:85
vtkm::worklet::histogram::ComputeBins::RangeProvided
bool RangeProvided
Definition: ComputeNDHistogram.h:135
vtkm::worklet::histogram::ConvertHistBinToND::ExecutionSignature
void(_1, _2, _3) ExecutionSignature
Definition: ComputeNDHistogram.h:145
vtkm::worklet::histogram::ComputeBins
Definition: ComputeNDHistogram.h:81
vtkm::worklet::histogram::ConvertHistBinToND::ControlSignature
void(FieldIn bin1DIndexIn, FieldOut bin1DIndexOut, FieldOut oneVariableIndexOut) ControlSignature
Definition: ComputeNDHistogram.h:144
vtkm::worklet::WorkletMapField
Base class for worklets that do a simple mapping of field arrays.
Definition: WorkletMapField.h:38
vtkm::worklet::histogram::ComputeBins::operator()
VTKM_CONT void operator()(const vtkm::cont::ArrayHandle< T, Storage > &field) const
Definition: ComputeNDHistogram.h:112
vtkm::worklet::histogram::ConvertHistBinToND::numberOfBins
vtkm::Id numberOfBins
Definition: ComputeNDHistogram.h:148
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31