VTK-m  2.0
NDimsHistogram.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_NDimsHistogram_h
12 #define vtk_m_worklet_NDimsHistogram_h
13 
14 #include <vtkm/Math.h>
15 #include <vtkm/cont/Algorithm.h>
16 #include <vtkm/cont/ArrayCopy.h>
17 #include <vtkm/cont/ArrayHandle.h>
19 #include <vtkm/cont/DataSet.h>
24 
25 #include <vtkm/cont/Field.h>
26 
27 namespace vtkm
28 {
29 namespace worklet
30 {
31 
33 {
34 public:
35  void SetNumOfDataPoints(vtkm::Id _numDataPoints)
36  {
37  NumDataPoints = _numDataPoints;
38 
39  // Initialize bin1DIndex array
41  vtkm::cont::ArrayCopy(constant0Array, Bin1DIndex);
42  }
43 
44  // Add a field and the bin number for this field
45  // Return: rangeOfRange is min max value of this array
46  // binDelta is delta of a bin
47  template <typename HandleType>
48  void AddField(const HandleType& fieldArray,
49  vtkm::Id numberOfBins,
50  vtkm::Range& rangeOfValues,
51  vtkm::Float64& binDelta)
52  {
53  NumberOfBins.push_back(numberOfBins);
54 
55  if (fieldArray.GetNumberOfValues() != NumDataPoints)
56  {
57  throw vtkm::cont::ErrorBadValue("Array lengths does not match");
58  }
59  else
60  {
62  fieldArray.ResetTypes(vtkm::TypeListScalarAll{}, VTKM_DEFAULT_STORAGE_LIST{}),
63  vtkm::worklet::histogram::ComputeBins(Bin1DIndex, numberOfBins, rangeOfValues, binDelta));
64  }
65  }
66 
67  // Add a field and the bin number for this field along with specific range of the data
68  // Return: binDelta is delta of a bin
69  template <typename HandleType>
70  void AddField(const HandleType& fieldArray,
71  vtkm::Id numberOfBins,
72  vtkm::Range& rangeOfValues,
73  vtkm::Float64& binDelta,
74  bool rangeProvided)
75  {
76  NumberOfBins.push_back(numberOfBins);
77 
78  if (fieldArray.GetNumberOfValues() != NumDataPoints)
79  {
80  throw vtkm::cont::ErrorBadValue("Array lengths does not match");
81  }
82  else
83  {
84  CastAndCall(fieldArray.ResetTypes(vtkm::TypeListScalarAll()),
86  Bin1DIndex, numberOfBins, rangeOfValues, binDelta, rangeProvided));
87  }
88  }
89 
90  // Execute N-Dim histogram worklet to get N-Dims histogram from input fields
91  // Input arguments:
92  // binId: returned bin id of NDims-histogram, binId has n arrays, if length of fieldName is n
93  // freqs: returned frequency(count) array
94  // Note: the ND-histogram is returned in the fashion of sparse representation.
95  // (no zero frequency in freqs array)
96  // the length of all arrays in binId and freqs array must be the same
97  // if the length of fieldNames is n (compute a n-dimensional hisotgram)
98  // freqs[i] is the frequency of the bin with bin Ids{ binId[0][i], binId[1][i], ... binId[n-1][i] }
99  void Run(std::vector<vtkm::cont::ArrayHandle<vtkm::Id>>& binId,
101  {
102  binId.resize(NumberOfBins.size());
103 
104  // Sort the resulting bin(1D) array for counting
106 
107  // Count frequency of each bin
110 
111  //convert back to multi variate binId
112  for (vtkm::Id i = static_cast<vtkm::Id>(NumberOfBins.size()) - 1; i >= 0; i--)
113  {
114  const vtkm::Id nFieldBins = NumberOfBins[static_cast<size_t>(i)];
115  vtkm::worklet::histogram::ConvertHistBinToND binWorklet(nFieldBins);
117  convertHistBinToNDDispatcher(binWorklet);
118  size_t vectorId = static_cast<size_t>(i);
119  convertHistBinToNDDispatcher.Invoke(Bin1DIndex, Bin1DIndex, binId[vectorId]);
120  }
121  }
122 
123 private:
124  std::vector<vtkm::Id> NumberOfBins;
127 };
128 }
129 } // namespace vtkm::worklet
130 
131 #endif // vtk_m_worklet_NDimsHistogram_h
vtkm::cont::ArrayHandle< vtkm::Id >
ArrayHandle.h
vtkm::worklet::NDimsHistogram::Bin1DIndex
vtkm::cont::ArrayHandle< vtkm::Id > Bin1DIndex
Definition: NDimsHistogram.h:125
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
WorkletMapField.h
vtkm::cont::Algorithm::Sort
static VTKM_CONT void Sort(vtkm::cont::DeviceAdapterId devId, vtkm::cont::ArrayHandle< T, Storage > &values)
Definition: Algorithm.h:965
vtkm::worklet::NDimsHistogram::NumberOfBins
std::vector< vtkm::Id > NumberOfBins
Definition: NDimsHistogram.h:124
vtkm::cont::CastAndCall
void CastAndCall(const DynamicObject &dynamicObject, Functor &&f, Args &&... args)
A Generic interface to CastAndCall.
Definition: CastAndCall.h:47
vtkm::worklet::NDimsHistogram::SetNumOfDataPoints
void SetNumOfDataPoints(vtkm::Id _numDataPoints)
Definition: NDimsHistogram.h:35
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
ArrayCopy.h
DispatcherMapField.h
ComputeNDHistogram.h
vtkm::Add
Definition: Types.h:222
vtkm::worklet::histogram::ConvertHistBinToND
Definition: ComputeNDHistogram.h:139
vtkm::worklet::DispatcherMapField
Dispatcher for worklets that inherit from WorkletMapField.
Definition: DispatcherMapField.h:25
Math.h
Algorithm.h
vtkm::cont::ArrayCopy
void ArrayCopy(const SourceArrayType &source, DestArrayType &destination)
Does a deep copy from one array to another array.
Definition: ArrayCopy.h:142
vtkm::worklet::NDimsHistogram::Run
void Run(std::vector< vtkm::cont::ArrayHandle< vtkm::Id >> &binId, vtkm::cont::ArrayHandle< vtkm::Id > &freqs)
Definition: NDimsHistogram.h:99
vtkm::cont::ArrayHandleConstant
An array handle with a constant value.
Definition: ArrayHandleConstant.h:63
vtkm::worklet::NDimsHistogram
Definition: NDimsHistogram.h:32
vtkm::cont::Algorithm::ReduceByKey
static VTKM_CONT void ReduceByKey(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CKeyIn > &keys, const vtkm::cont::ArrayHandle< U, CValIn > &values, vtkm::cont::ArrayHandle< T, CKeyOut > &keys_output, vtkm::cont::ArrayHandle< U, CValOut > &values_output, BinaryFunctor binary_functor)
Definition: Algorithm.h:697
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::NDimsHistogram::AddField
void AddField(const HandleType &fieldArray, vtkm::Id numberOfBins, vtkm::Range &rangeOfValues, vtkm::Float64 &binDelta)
Definition: NDimsHistogram.h:48
ArrayHandleCounting.h
ErrorBadValue.h
vtkm::worklet::NDimsHistogram::AddField
void AddField(const HandleType &fieldArray, vtkm::Id numberOfBins, vtkm::Range &rangeOfValues, vtkm::Float64 &binDelta, bool rangeProvided)
Definition: NDimsHistogram.h:70
Field.h
vtkm::List
Definition: List.h:34
vtkm::Float64
double Float64
Definition: Types.h:155
vtkm::worklet::NDimsHistogram::NumDataPoints
vtkm::Id NumDataPoints
Definition: NDimsHistogram.h:126
vtkm::worklet::histogram::ComputeBins
Definition: ComputeNDHistogram.h:81
DataSet.h
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31