SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
Averaged.h
Go to the documentation of this file.
1 /**
2  * \file Averaged.h
3  * \authors Marcus Hudritsch
4  * \date July 2014
5  * \authors Marcus Hudritsch
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #ifndef AVERAGED_H
12 #define AVERAGED_H
13 
14 #include <string>
15 #include <vector>
16 #include <assert.h>
17 
18 using std::string;
19 using std::vector;
20 
21 namespace Utils
22 {
23 //-----------------------------------------------------------------------------
24 //! Averaged template class provides an average value from a fixed size array.
25 /*!The Average template class provides a simple moving average value
26  continuously averaged from a fixed size vector. The template class can be
27  used for any template type T that provides the following operators:
28  =, -, +, T* float
29 */
30 template<class T>
31 class Averaged
32 {
33 public:
35  Averaged(int numValues, T initValue = 0)
36  {
37  init(numValues, initValue);
38  }
39 
40  //! Initializes the average value array to a given value
41  void init(int numValues, T initValue)
42  {
43  assert(numValues > 0 && "Num. of values must be greater than zero");
44  _values.clear();
45  _values.resize(numValues, initValue);
46  _oneOverNumValues = 1.0f / (float)_values.size();
47  _sum = initValue * numValues;
48  _average = initValue;
50  }
51 
52  //! Sets the current value in the value array and builds the average
53  void set(T value)
54  {
55  assert(_values.size() > 0 && "_value vector not initialized");
56 
57  // Shortcut for no averaging
58  if (_values.size() == 1)
59  _sum = _average = value;
60  else
61  {
62  if (_currentValueIndex == _values.size())
64 
65  // Correct the sum continuously
67  _values[_currentValueIndex] = value;
69  _average = _sum * _oneOverNumValues; // avoid division
71  }
72  }
73 
74  T average() { return _average; }
75  size_t size() { return _values.size(); }
76 
77 private:
78  float _oneOverNumValues{}; //!< multiplier instead of divider
79  vector<T> _values; //!< value array
80  int _currentValueIndex{}; //!< current value index within _values
81  T _sum; //!< sum of all values
82  T _average; //!< average value
83 };
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
87 };
88 #endif
Averaged template class provides an average value from a fixed size array.
Definition: Averaged.h:32
size_t size()
Definition: Averaged.h:75
T _sum
sum of all values
Definition: Averaged.h:81
void init(int numValues, T initValue)
Initializes the average value array to a given value.
Definition: Averaged.h:41
vector< T > _values
value array
Definition: Averaged.h:79
float _oneOverNumValues
multiplier instead of divider
Definition: Averaged.h:78
void set(T value)
Sets the current value in the value array and builds the average.
Definition: Averaged.h:53
T _average
average value
Definition: Averaged.h:82
int _currentValueIndex
current value index within _values
Definition: Averaged.h:80
Averaged(int numValues, T initValue=0)
Definition: Averaged.h:35
Utils provides utilities for string & file handling, logging and math functions.
Definition: Averaged.h:22
Utils::Averaged< float > AvgFloat
Definition: Averaged.h:85