// Predict where the pendulum will be, with no camera frames, and see what the choice of
// integrator and the process noise do to the prediction.
#include <cmath>
#include <cstdio>

#include "pendulum.h"
#include "trace.h"

// The same state and measurement with each of the library's integrators.
template <typename Integrator>
using Predictor = UKF::Core<State, Frame, Integrator>;

// An estimate with its uncertainty: standard deviations of the angle and of the rate.
struct Estimate {
    State state;
    State::CovarianceMatrix covariance;
};

Estimate estimate(real_t angle, real_t rate, real_t angle_sigma, real_t rate_sigma) {
    Estimate result;
    result.state.set_field<Angle>(angle);
    result.state.set_field<Rate>(rate);
    result.covariance.setZero();
    result.covariance(0, 0) = angle_sigma * angle_sigma;  // rad^2
    result.covariance(1, 1) = rate_sigma * rate_sigma;    // (rad/s)^2
    return result;
}

// Predict "seconds" ahead in equal steps, with process noise of intensity q for each step.
template <typename Integrator>
Estimate ahead(const Estimate& from, real_t seconds, int steps, real_t q) {
    Predictor<Integrator> filter;
    filter.state = from.state;
    filter.covariance = from.covariance;
    const real_t dt = seconds / steps;
    filter.process_noise_covariance = interval_noise(dt, q);
    for (int i = 0; i < steps; i++) {
        if (!filter.a_priori_step(dt)) std::printf("prediction failed\n");
    }
    return {filter.state, filter.covariance};
}

template <typename Integrator>
void compare(const char* name, const Estimate& from, Trace& trace, double id) {
    std::printf("%-5s", name);
    for (int steps : {1, 5, 10}) {
        const Estimate result = ahead<Integrator>(from, 0.5, steps, 0.0);
        std::printf("  %7.4f rad %7.4f rad/s", result.state.get_field<Angle>(),
                    result.state.get_field<Rate>());
        trace.row("integrators", {id, double(steps), result.state.get_field<Angle>(),
                                  result.state.get_field<Rate>(), result.covariance(0, 0),
                                  result.covariance(0, 1), result.covariance(1, 1)});
    }
    std::printf("\n");
}

int main(int argc, char** argv) {
    Trace trace(argc, argv);

    // Released about 52 degrees out, and known only roughly.
    const Estimate rough = estimate(0.9, 0.0, 0.15, 0.5);
    std::printf("Half a second ahead, in 1, 5 and 10 steps:\n");
    compare<UKF::IntegratorEuler>("Euler", rough, trace, 0);
    compare<UKF::IntegratorHeun>("Heun", rough, trace, 1);
    compare<UKF::IntegratorRK4>("RK4", rough, trace, 2);
    const Estimate reference = ahead<UKF::IntegratorRK4>(rough, 0.5, 100, 0.0);
    std::printf("RK4 in 100 steps: %7.4f rad %7.4f rad/s\n", reference.state.get_field<Angle>(),
                reference.state.get_field<Rate>());
    trace.value("reference_angle", reference.state.get_field<Angle>());
    trace.value("reference_rate", reference.state.get_field<Rate>());

    // The same release, known well: the camera has been tracking it.
    const Estimate tracked = estimate(0.9, 0.0, 0.025, 0.13);
    std::printf("\nStandard deviations as a gap grows, in steps of 0.05 s:\n");
    std::printf("gap      model trusted, q = 0      draughts allowed, q = 0.05\n");
    for (int frames : {0, 10, 20, 30, 40}) {
        const real_t gap = 0.05 * frames;
        const int steps = frames == 0 ? 1 : frames;
        const Estimate quiet = ahead<UKF::IntegratorRK4>(tracked, gap, steps, 0.0);
        const Estimate gusty = ahead<UKF::IntegratorRK4>(tracked, gap, steps, 0.05);
        std::printf("%.1f s    %.3f rad %.3f rad/s     %.3f rad %.3f rad/s\n", gap,
                    std::sqrt(quiet.covariance(0, 0)), std::sqrt(quiet.covariance(1, 1)),
                    std::sqrt(gusty.covariance(0, 0)), std::sqrt(gusty.covariance(1, 1)));
        trace.row("gap", {gap, std::sqrt(quiet.covariance(0, 0)), std::sqrt(quiet.covariance(1, 1)),
                          std::sqrt(gusty.covariance(0, 0)), std::sqrt(gusty.covariance(1, 1)),
                          gusty.state.get_field<Angle>(), gusty.state.get_field<Rate>(),
                          gusty.covariance(0, 1)});
    }
    return 0;
}
