// Follow one camera frame through the filter: what each sigma point expects the camera to
// report, how far the frame is from that expectation, and what the correction does.
#include <cmath>
#include <cstdio>

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

int main(int argc, char** argv) {
    Trace trace(argc, argv);
    const real_t dt = 0.05, camera_sigma = 0.03, gust_intensity = 0.05;

    // The estimate when the camera lost sight of the weight, a quarter of a second ago.
    Tracker filter;
    filter.state.set_field<Angle>(0.80);
    filter.state.set_field<Rate>(-1.0);
    filter.covariance.setZero();
    filter.covariance(0, 0) = 0.10 * 0.10;
    filter.covariance(1, 1) = 0.60 * 0.60;
    filter.process_noise_covariance = interval_noise(dt, gust_intensity);
    filter.measurement_covariance.set_field<BobX>(camera_sigma * camera_sigma);

    // Five frames were missed, so predict five times with nothing to compare against.
    for (int missed = 0; missed < 5; missed++) {
        if (!filter.a_priori_step(dt)) return 1;
    }
    const real_t predicted_angle = filter.state.get_field<Angle>();
    const real_t predicted_rate = filter.state.get_field<Rate>();
    std::printf("predicted: angle %.3f +/- %.3f rad, rate %.2f +/- %.2f rad/s, correlation %.2f\n",
                predicted_angle, std::sqrt(filter.covariance(0, 0)), predicted_rate,
                std::sqrt(filter.covariance(1, 1)),
                filter.covariance(0, 1) / std::sqrt(filter.covariance(0, 0) * filter.covariance(1, 1)));
    trace.row("predicted", {predicted_angle, predicted_rate, filter.covariance(0, 0),
                            filter.covariance(0, 1), filter.covariance(1, 1)});

    // What would the camera report for each sigma point of the prediction? The library
    // answers by calling expected_measurement once for every column.
    const auto& points = filter.get_sigma_points();
    const auto expected = Frame().calculate_sigma_point_distribution<State>(points);
    for (int i = 0; i < points.cols(); i++) {
        const State point(points.col(i));
        std::printf("sigma point %d: angle %.3f rad, rate %5.2f rad/s -> expects %.3f m\n", i,
                    point.get_field<Angle>(), point.get_field<Rate>(), expected(0, i));
        trace.row("points", {point.get_field<Angle>(), point.get_field<Rate>(), expected(0, i)});
    }

    // The frame that arrives, and how it differs from the predicted reading.
    Frame frame;
    frame.set_field<BobX>(0.26);
    if (!filter.innovation_step(frame)) return 1;
    const real_t innovation = filter.innovation.get_field<BobX>();
    const real_t innovation_sigma = std::sqrt(filter.innovation_covariance(0, 0));
    std::printf("frame %.3f m, predicted reading %.3f m, innovation %.3f m (expected spread %.3f m)\n",
                frame.get_field<BobX>(), frame.get_field<BobX>() - innovation, innovation, innovation_sigma);

    if (!filter.a_posteriori_step()) return 1;
    std::printf("corrected: angle %.3f +/- %.3f rad, rate %.2f +/- %.2f rad/s\n",
                filter.state.get_field<Angle>(), std::sqrt(filter.covariance(0, 0)),
                filter.state.get_field<Rate>(), std::sqrt(filter.covariance(1, 1)));
    std::printf("the angle moved by %.3f rad and the rate, which no sensor measured, by %.2f rad/s\n",
                filter.state.get_field<Angle>() - predicted_angle,
                filter.state.get_field<Rate>() - predicted_rate);
    trace.row("corrected", {filter.state.get_field<Angle>(), filter.state.get_field<Rate>(),
                            filter.covariance(0, 0), filter.covariance(0, 1), filter.covariance(1, 1),
                            innovation, innovation_sigma});
    return 0;
}
