summaryrefslogtreecommitdiffhomepage
path: root/facetracknoir/lerp.hpp
blob: 0123832abb9e05b18b7d9898cf355e6eb34fce50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include "facetracknoir/timer.hpp"
#include <algorithm>
#include <cmath>

class lerp {
private:
    static const constexpr double eps = 1e-2;
    double last[2][6], cam[6], dt;
    Timer t;
public:
    lerp() :
        last { {0,0,0,0,0,0}, {0,0,0,0,0,0} }, cam {0,0,0,0,0,0}, dt(1)
    {
    }
    bool idempotentp(const double* input)
    {
        for (int i = 0; i < 6; i++)
        {
            double diff = fabs(cam[i] - input[i]);
            if (diff > eps)
                return false;
        }
        return true;
    }

    void write(const double* cam_, const double* input, double* output)
    {
        const double q = t.elapsed();
        const double d = q/dt;

        bool idem = idempotentp(cam_);

        if (!idem)
        {
            dt = q;
            t.start();
        }

        const double c = std::max(std::min(1.0, d), 0.0);

        if (!idem)
            for (int i = 0; i < 6; i++)
            {
                last[1][i] = last[0][i];
                last[0][i] = input[i];
                cam[i] = cam_[i];
            }

        for (int i = 0; i < 6; i++)
            output[i] = last[1][i] + (last[0][i] - last[1][i]) * c;
    }

    void get_state(double* state)
    {
        for (int i = 0; i < 6; i++)
            state[i] = last[0][i];
    }
};