diff --git a/include/openspace/util/histogram.h b/include/openspace/util/histogram.h index 1fb951ef26..0cc61cd99e 100644 --- a/include/openspace/util/histogram.h +++ b/include/openspace/util/histogram.h @@ -33,19 +33,29 @@ class Histogram { public: Histogram(); - Histogram(float minBin, float maxBin, int numBins); - Histogram(float minBin, float maxBin, int numBins, float *data); + Histogram(float minValue, float maxValue, int numBins); + Histogram(float minValue, float maxValue, int numBins, float *data); Histogram(Histogram&& other); ~Histogram(); Histogram& operator=(Histogram&& other); int numBins() const; - float minBin() const; - float maxBin() const; + float minValue() const; + float maxValue() const; bool isValid() const; - bool add(float bin, float value); + /** + * Enter value into the histogram. The add method takes the given + * value, works out which bin this corresponds to, and increments + * this bin by 'repeat'. + * + * @param value The Value to insert into the histogram + * @param repeat How many times you want to insert it + * + * @return Returns true if succesful insertion, otherwise return false + */ + bool add(float value, float repeat = 1.0f); bool add(const Histogram& histogram); bool addRectangle(float lowBin, float highBin, float value); @@ -63,8 +73,8 @@ public: private: int _numBins; - float _minBin; - float _maxBin; + float _minValue; + float _maxValue; float* _data; std::vector _equalizer; diff --git a/src/util/histogram.cpp b/src/util/histogram.cpp index a788f71399..e3ff296cfe 100644 --- a/src/util/histogram.cpp +++ b/src/util/histogram.cpp @@ -35,15 +35,15 @@ namespace { namespace openspace { Histogram::Histogram() - : _minBin(0) - , _maxBin(0) + : _minValue(0) + , _maxValue(0) , _numBins(-1) , _numValues(0) , _data(nullptr) {} -Histogram::Histogram(float minBin, float maxBin, int numBins) - : _minBin(minBin) - , _maxBin(maxBin) +Histogram::Histogram(float minValue, float maxValue, int numBins) + : _minValue(minValue) + , _maxValue(maxValue) , _numBins(numBins) , _numValues(0) , _data(nullptr) { @@ -54,16 +54,16 @@ Histogram::Histogram(float minBin, float maxBin, int numBins) } } -Histogram::Histogram(float minBin, float maxBin, int numBins, float *data) - : _minBin(minBin) - , _maxBin(maxBin) +Histogram::Histogram(float minValue, float maxValue, int numBins, float *data) + : _minValue(minValue) + , _maxValue(maxValue) , _numBins(numBins) , _numValues(0) , _data(data) {} Histogram::Histogram(Histogram&& other) { - _minBin = other._minBin; - _maxBin = other._maxBin; + _minValue = other._minValue; + _maxValue = other._maxValue; _numBins = other._numBins; _numValues = other._numValues; _data = other._data; @@ -71,8 +71,8 @@ Histogram::Histogram(Histogram&& other) { } Histogram& Histogram::operator=(Histogram&& other) { - _minBin = other._minBin; - _maxBin = other._maxBin; + _minValue = other._minValue; + _maxValue = other._maxValue; _numBins = other._numBins; _numValues = other._numValues; _data = other._data; @@ -92,12 +92,12 @@ int Histogram::numBins() const { return _numBins; } -float Histogram::minBin() const { - return _minBin; +float Histogram::minValue() const { + return _minValue; } -float Histogram::maxBin() const { - return _maxBin; +float Histogram::maxValue() const { + return _maxValue; } bool Histogram::isValid() const { @@ -105,23 +105,22 @@ bool Histogram::isValid() const { } -bool Histogram::add(float bin, float value) { - if (bin < _minBin || bin > _maxBin) { +bool Histogram::add(float value, float repeat) { + if (value < _minValue || value > _maxValue) { // Out of range return false; } - float normalizedBin = (bin - _minBin) / (_maxBin - _minBin); // [0.0, 1.0] - int binIndex = floor(normalizedBin * _numBins); // [0, _numBins] - if (binIndex == _numBins) binIndex--; // [0, _numBins[ + float normalizedValue = (value - _minValue) / (_maxValue - _minValue); // [0.0, 1.0] + int binIndex = std::min( floor(normalizedValue * _numBins), _numBins - 1.0 ); // [0, _numBins - 1] - _data[binIndex] += value; - _numValues++; + _data[binIndex] += repeat; + _numValues += repeat; return true; } bool Histogram::add(const Histogram& histogram) { - if (_minBin == histogram.minBin() && _maxBin == histogram.maxBin() && _numBins == histogram.numBins()) { + if (_minValue == histogram.minValue() && _maxValue == histogram.maxValue() && _numBins == histogram.numBins()) { const float* data = histogram.data(); for (int i = 0; i < _numBins; i++) { @@ -142,13 +141,13 @@ bool Histogram::addRectangle(float lowBin, float highBin, float value) { if (lowBin > highBin) { std::swap(lowBin, highBin); } - if (lowBin < _minBin || highBin > _maxBin) { + if (lowBin < _minValue || highBin > _maxValue) { // Out of range return false; } - float normalizedLowBin = (lowBin - _minBin) / (_maxBin - _minBin); - float normalizedHighBin = (highBin - _minBin) / (_maxBin - _minBin); + float normalizedLowBin = (lowBin - _minValue) / (_maxValue - _minValue); + float normalizedHighBin = (highBin - _minValue) / (_maxValue - _minValue); float lowBinIndex = normalizedLowBin * _numBins; float highBinIndex = normalizedHighBin * _numBins; @@ -174,7 +173,7 @@ bool Histogram::addRectangle(float lowBin, float highBin, float value) { float Histogram::interpolate(float bin) const { - float normalizedBin = (bin - _minBin) / (_maxBin - _minBin); + float normalizedBin = (bin - _minValue) / (_maxValue - _minValue); float binIndex = normalizedBin * _numBins - 0.5; // Center float interpolator = binIndex - floor(binIndex); @@ -236,7 +235,7 @@ void Histogram::generateEqualizer(){ * Will return a equalized histogram */ Histogram Histogram::equalize(){ - Histogram equalizedHistogram(_minBin, _maxBin, _numBins); + Histogram equalizedHistogram(_minValue, _maxValue, _numBins); for(int i = 0; i < _numBins; i++){ equalizedHistogram._data[(int)_equalizer[i]] += _data[i]; @@ -246,16 +245,16 @@ Histogram Histogram::equalize(){ } /* - * Given a value within the domain of this histogram (_minBin < value < maxBin), + * Given a value within the domain of this histogram (_minValue < value < maxValue), * this method will use its equalizer to return a histogram equalized result. */ float Histogram::equalize(float value){ - if (value < _minBin || value > _maxBin) { - LWARNING("Equalized value is is not within domain of histogram. min: " + std::to_string(_minBin) + " max: " + std::to_string(_maxBin) + " val: " + std::to_string(value)); + if (value < _minValue || value > _maxValue) { + LWARNING("Equalized value is is not within domain of histogram. min: " + std::to_string(_minValue) + " max: " + std::to_string(_maxValue) + " val: " + std::to_string(value)); } - float normalizedValue = (value-_minBin)/(_maxBin-_minBin); + float normalizedValue = (value-_minValue)/(_maxValue-_minValue); int bin = floor(normalizedValue * _numBins); - // If value == _maxBins then bin == _numBins, which is a invalid index. + // If value == _maxValues then bin == _numBins, which is a invalid index. bin = std::min(_numBins-1, bin); return _equalizer[bin]; @@ -272,10 +271,10 @@ float Histogram::entropy(){ void Histogram::print() const { std::cout << "number of bins: " << _numBins << std::endl - << "range: " << _minBin << " - " << _maxBin << std::endl << std::endl; + << "range: " << _minValue << " - " << _maxValue << std::endl << std::endl; for (int i = 0; i < _numBins; i++) { - float low = _minBin + float(i) / _numBins * (_maxBin - _minBin); - float high = low + (_maxBin - _minBin) / float(_numBins); + float low = _minValue + float(i) / _numBins * (_maxValue - _minValue); + float high = low + (_maxValue - _minValue) / float(_numBins); std::cout << i << " [" << low << ", " << high << "]" << " " << _data[i] << std::endl; }