mirror of
https://github.com/overte-org/overte.git
synced 2025-06-23 10:59:39 +02:00
MOve the movingAverage class to shared next to SimpleMovingAverage
This commit is contained in:
parent
eae316100a
commit
a8872d065b
2 changed files with 30 additions and 25 deletions
|
@ -24,6 +24,8 @@
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <SimpleMovingAverage.h>
|
||||||
|
|
||||||
#include <controllers/InputDevice.h>
|
#include <controllers/InputDevice.h>
|
||||||
#include <controllers/StandardControls.h>
|
#include <controllers/StandardControls.h>
|
||||||
|
|
||||||
|
@ -95,31 +97,6 @@ private:
|
||||||
float _lastDistance;
|
float _lastDistance;
|
||||||
bool _useSixenseFilter = true;
|
bool _useSixenseFilter = true;
|
||||||
|
|
||||||
template <class T, int MAX_NUM_SAMPLES> class MovingAverage {
|
|
||||||
public:
|
|
||||||
using Samples = std::list< T >;
|
|
||||||
Samples samples;
|
|
||||||
T average;
|
|
||||||
|
|
||||||
void clear() {
|
|
||||||
samples.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isAverageValid() const { return !samples.empty(); }
|
|
||||||
|
|
||||||
void addSample(T sample) {
|
|
||||||
samples.push_front(sample);
|
|
||||||
int numSamples = samples.size();
|
|
||||||
|
|
||||||
if (numSamples < MAX_NUM_SAMPLES) {
|
|
||||||
average = (sample + average * float(numSamples - 1)) / float(numSamples);
|
|
||||||
} else {
|
|
||||||
T tail = samples.back();
|
|
||||||
samples.pop_back();
|
|
||||||
average = average + (sample - tail) / float(numSamples);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static const int MAX_NUM_AVERAGING_SAMPLES = 50; // At ~100 updates per seconds this means averaging over ~.5s
|
static const int MAX_NUM_AVERAGING_SAMPLES = 50; // At ~100 updates per seconds this means averaging over ~.5s
|
||||||
using Samples = std::pair< MovingAverage< glm::vec3, MAX_NUM_AVERAGING_SAMPLES>, MovingAverage< glm::vec4, MAX_NUM_AVERAGING_SAMPLES> >;
|
using Samples = std::pair< MovingAverage< glm::vec3, MAX_NUM_AVERAGING_SAMPLES>, MovingAverage< glm::vec4, MAX_NUM_AVERAGING_SAMPLES> >;
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#define hifi_SimpleMovingAverage_h
|
#define hifi_SimpleMovingAverage_h
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
class SimpleMovingAverage {
|
class SimpleMovingAverage {
|
||||||
public:
|
public:
|
||||||
|
@ -40,4 +41,31 @@ private:
|
||||||
float ONE_MINUS_WEIGHTING;
|
float ONE_MINUS_WEIGHTING;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <class T, int MAX_NUM_SAMPLES> class MovingAverage {
|
||||||
|
public:
|
||||||
|
using Samples = std::list< T >;
|
||||||
|
Samples samples;
|
||||||
|
T average;
|
||||||
|
|
||||||
|
void clear() {
|
||||||
|
samples.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isAverageValid() const { return !samples.empty(); }
|
||||||
|
|
||||||
|
void addSample(T sample) {
|
||||||
|
samples.push_front(sample);
|
||||||
|
int numSamples = samples.size();
|
||||||
|
|
||||||
|
if (numSamples < MAX_NUM_SAMPLES) {
|
||||||
|
average = (sample + average * (float)(numSamples - 1)) / (float)(numSamples);
|
||||||
|
} else {
|
||||||
|
T tail = samples.back();
|
||||||
|
samples.pop_back();
|
||||||
|
average = average + (sample - tail) / (float)(numSamples);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#endif // hifi_SimpleMovingAverage_h
|
#endif // hifi_SimpleMovingAverage_h
|
||||||
|
|
Loading…
Reference in a new issue