Visual Computing Library
Loading...
Searching...
No Matches
histogram.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MATH_HISTOGRAM_H
24#define VCL_MATH_HISTOGRAM_H
25
26#include <vclib/types.h>
27
28#include <algorithm>
29#include <cmath>
30#include <vector>
31
32namespace vcl {
33
34// TODO: improve documentation for this class
35
44template<typename ScalarType>
46{
47 std::vector<ScalarType> mHist; // Counters for bins
48 std::vector<ScalarType> mRanges; // Range for bins
49
50 ScalarType mMinRange = 0; // Minimum range value
51 ScalarType mMaxRange = 1; // Maximum range value
52
53 ScalarType mMin = std::numeric_limits<ScalarType>::max();
54 ScalarType mMax = std::numeric_limits<ScalarType>::lowest();
55
56 // Number of valid bins stored between mMinRange and mMaxRange
57 uint mBinNumber = 0;
58
59 ScalarType mCnt = 0; // Number of accumulated values
60 ScalarType mSum = 0; // Sum of values
61 ScalarType mRMS = 0; // Root mean square
62
63public:
64 Histogram() = default;
65
77 ScalarType minRangeValue,
78 ScalarType maxRangeValue,
79 uint nBins,
80 ScalarType gamma = 1.0) :
81 mMinRange(minRangeValue), mMaxRange(maxRangeValue),
82 mBinNumber(nBins)
83 {
84 mHist.resize(nBins + 2, 0);
85
86 mRanges.resize(nBins + 3);
87
88 mRanges[0] = -std::numeric_limits<ScalarType>::max();
89 mRanges[nBins + 2] = std::numeric_limits<ScalarType>::max();
90
91 double delta = (mMaxRange - mMinRange);
92 if (gamma == 1) {
93 for (uint i = 0; i <= nBins; ++i)
94 mRanges[i + 1] = mMinRange + delta * ScalarType(i) / nBins;
95 }
96 else {
97 for (uint i = 0; i <= nBins; ++i)
98 mRanges[i + 1] =
99 mMinRange + delta * std::pow(ScalarType(i) / nBins, gamma);
100 }
101 }
102
106 void clear()
107 {
108 mHist.clear();
109 mRanges.clear();
110 mCnt = 0;
111 mSum = 0;
112 mRMS = 0;
113 mBinNumber = 0;
114 mMinRange = 0;
115 mMaxRange = 1;
116 mMin = std::numeric_limits<ScalarType>::max();
117 mMax = std::numeric_limits<ScalarType>::lowest();
118 }
119
126 void addValue(ScalarType value, ScalarType increment = 1.0)
127 {
128 uint pos = binIndex(value);
129 if (value < mMin)
130 mMin = value;
131 if (value > mMax)
132 mMax = value;
133 assert((pos >= 0) && (pos <= mBinNumber + 1));
134 mHist[pos] += increment;
135 mCnt += increment;
136 mSum += value * increment;
137 mRMS += (value * value) * increment;
138 }
139
144 ScalarType minRangeValue() const { return mMinRange; }
145
150 ScalarType maxRangeValue() const { return mMaxRange; }
151
156 ScalarType sumValues() const { return mSum; }
157
162 ScalarType numberValues() const { return mCnt; }
163
168 ScalarType minValue() const { return mMin; }
169
174 ScalarType maxValue() const { return mMax; }
175
181 ScalarType maxBinCount() const
182 {
183 return *(std::max_element(mHist.begin(), mHist.end()));
184 }
185
191 ScalarType maxBinCountInRange() const
192 {
193 return *(std::max_element(mHist.begin() + 1, mHist.end() - 1));
194 }
195
200 uint binsNumber() const { return mBinNumber; }
201
202 ScalarType binCount(uint ind) const { return mHist[ind]; }
203
204 ScalarType binLowerBound(uint ind) const { return mRanges[ind]; }
205
206 ScalarType binUpperBound(uint ind) const { return mRanges[ind + 1]; }
207
208 ScalarType binOfValueCount(ScalarType value) const
209 {
210 return mHist[binIndex(value)];
211 }
212
213 ScalarType binOfValueCount(ScalarType value, ScalarType width) const
214 {
215 return rangeCount(value - width / 2.0, value + width / 2.0);
216 }
217
218 ScalarType binOfValueWidth(ScalarType value)
219 {
220 uint pos = BinIndex(value);
221 return mRanges[pos + 1] - mRanges[pos];
222 }
223
224 ScalarType rangeCount(ScalarType rangeMin, ScalarType rangeMax) const
225 {
226 uint firstBin = binIndex(rangeMin);
227 uint lastBin = binIndex(rangeMax);
228
229 ScalarType sum = 0;
230 for (uint i = firstBin; i <= lastBin; ++i)
231 sum += mHist[i];
232 return sum;
233 }
234
242 ScalarType percentile(ScalarType frac) const
243 {
244 if (mHist.size() == 0)
245 return 0;
246
247 // check percentile range
248 assert(frac >= 0 && frac <= 1);
249
250 ScalarType sum = mCnt * frac;
251 ScalarType partsum = 0;
252
253 uint i = 0;
254 while (partsum < sum) {
255 partsum += mHist[i];
256 ++i;
257 }
258
259 return mRanges[i + 1];
260 }
261
266 ScalarType average() const { return mSum / mCnt; }
267
272 ScalarType rootMeanSquare() const { return std::sqrt(mRMS / mCnt); }
273
278 ScalarType variance() const
279 {
280 return std::abs(mRMS / mCnt - average() * average());
281 }
282
287 ScalarType standardDeviation() const { return std::sqrt(variance()); }
288
289private:
293 uint binIndex(ScalarType elem) const
294 {
295 // lower_bound returns the furthermost iterator i in [first, last) such
296 // that, for every iterator j in [first, i), *j < value. E.g. An
297 // iterator pointing to the first element "not less than" val, or end()
298 // if every element is less than val.
299 auto it = std::lower_bound(mRanges.begin(), mRanges.end(), elem);
300
301 assert(it != mRanges.begin());
302 assert(it != mRanges.end());
303
304 uint pos = it - mRanges.begin();
305 pos -= 1;
306
307 assert(mRanges[pos] < elem);
308 assert(elem <= mRanges[pos + 1]);
309 return pos;
310 }
311};
312
313/* Specialization Aliases */
314
315using Histogramf = Histogram<float>;
316using Histogramd = Histogram<double>;
317
318} // namespace vcl
319
320#endif // VCL_MATH_HISTOGRAM_H
The Histogram class allows to collect a set of values and then compute some statistics like average,...
Definition histogram.h:46
ScalarType percentile(ScalarType frac) const
Returns the value corresponding to a given percentile of the data.
Definition histogram.h:242
ScalarType numberValues() const
Number of values inserted in the histogram.
Definition histogram.h:162
ScalarType sumValues() const
Total sum of inserted values.
Definition histogram.h:156
ScalarType rootMeanSquare() const
Returns the Root Mean Square of the data.
Definition histogram.h:272
ScalarType minRangeValue() const
Minimum value of the range where the histogram is defined.
Definition histogram.h:144
ScalarType maxBinCountInRange() const
Max number of values among all bins between the minRangeValue and maxRangeValue.
Definition histogram.h:191
ScalarType variance() const
Returns the variance of the data.
Definition histogram.h:278
uint binsNumber() const
Number of intervals in the histogram.
Definition histogram.h:200
ScalarType maxRangeValue() const
Maximum value of the range where the histogram is defined.
Definition histogram.h:150
ScalarType maxBinCount() const
Max number of values among all bins (including the two infinity bounded bins)
Definition histogram.h:181
void addValue(ScalarType value, ScalarType increment=1.0)
Add a new value to the histogram.
Definition histogram.h:126
ScalarType minValue() const
Minimum value that has been added to the histogram.
Definition histogram.h:168
Histogram(ScalarType minRangeValue, ScalarType maxRangeValue, uint nBins, ScalarType gamma=1.0)
Set the histogram values.
Definition histogram.h:76
void clear()
Clears the histogram.
Definition histogram.h:106
ScalarType standardDeviation() const
Returns the standard deviation of the data.
Definition histogram.h:287
ScalarType average() const
Returns the average of the data.
Definition histogram.h:266
uint binIndex(ScalarType elem) const
Returns the index of the bin which contains a given element.
Definition histogram.h:293
ScalarType maxValue() const
Maximum value that has been added to the histogram.
Definition histogram.h:174
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43