// A camera that reports where the weight is in two directions, with an error that is larger
// along the direction of travel (motion blur) than across it. The two errors are then
// correlated, and the filter needs the whole measurement covariance to use the frame well.
#include <cmath>
#include <cstdio>
#include <cstdlib>

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

// One field holding both coordinates of the weight: sideways, then upward, in metres.
enum SpotKey { BobXY = 12 };
using Spot = UKF::FixedMeasurementVector<UKF::Field<BobXY, UKF::Vector<2>>>;

// Diagonal noise storage is the default. The last argument asks for a full matrix.
using DiagonalFilter = UKF::Core<State, Spot, UKF::IntegratorRK4>;
using DenseFilter = UKF::Core<State, Spot, UKF::IntegratorRK4, Spot::DenseCovarianceMatrix>;

namespace UKF {
template <>
template <>
UKF::Vector<2> Spot::expected_measurement<State, BobXY>(const State& state) {
    const real_t angle = state.get_field<Angle>();
    return UKF::Vector<2>(kLength * std::sin(angle), -kLength * std::cos(angle));
}
}  // namespace UKF

// The detector's error where the rod is at "angle": blur_sigma along the path of the
// weight, sharp_sigma across it. Rows and columns are sideways, then upward.
Spot::DenseCovarianceMatrix blurred(real_t angle, real_t blur_sigma, real_t sharp_sigma) {
    const UKF::Vector<2> along(std::cos(angle), std::sin(angle));    // direction of travel
    const UKF::Vector<2> across(std::sin(angle), -std::cos(angle));  // along the rod
    return blur_sigma * blur_sigma * along * along.transpose() +
           sharp_sigma * sharp_sigma * across * across.transpose();
}

template <typename Filter>
void start(Filter& filter) {
    filter.state << 0.6, -2.5;  // 0.6 rad out, swinging back at 2.5 rad/s
    filter.covariance.setZero();
    filter.covariance.diagonal() << 0.1 * 0.1, 0.5 * 0.5;
    filter.process_noise_covariance.setZero();
}

int main(int argc, char** argv) {
    Trace trace(argc, argv);
    const real_t blur_sigma = argument(argc, argv, 1, 0.04);  // m
    const real_t sharp_sigma = 0.005;  // m
    const real_t seen_at = 0.6;  // the frame is taken with the rod at 0.6 rad

    const Spot::DenseCovarianceMatrix noise = blurred(seen_at, blur_sigma, sharp_sigma);
    DenseFilter dense;
    start(dense);
    dense.measurement_covariance = noise;  // variances and the covariance between them

    DiagonalFilter diagonal;
    start(diagonal);
    diagonal.measurement_covariance.set_field<BobXY>(noise.diagonal());  // variances only

    // A frame which puts the weight 3 cm further along its path than expected.
    const UKF::Vector<2> along(std::cos(seen_at), std::sin(seen_at));
    Spot spot;
    spot.set_field<BobXY>(UKF::Vector<2>(kLength * std::sin(seen_at), -kLength * std::cos(seen_at)) + 0.03 * along);
    if (!dense.step(0.0, spot) || !diagonal.step(0.0, spot)) return 1;

    std::printf("measurement covariance (m^2): [%.6f %.6f; %.6f %.6f]\n", noise(0, 0), noise(0, 1),
                noise(1, 0), noise(1, 1));
    std::printf("whole matrix:   angle %.4f +/- %.4f rad\n", dense.state.get_field<Angle>(),
                std::sqrt(dense.covariance(0, 0)));
    std::printf("variances only: angle %.4f +/- %.4f rad\n", diagonal.state.get_field<Angle>(),
                std::sqrt(diagonal.covariance(0, 0)));
    trace.row("noise", {noise(0, 0), noise(0, 1), noise(1, 1)});
    trace.row("dense", {dense.state.get_field<Angle>(), std::sqrt(dense.covariance(0, 0))});
    trace.row("diagonal", {diagonal.state.get_field<Angle>(), std::sqrt(diagonal.covariance(0, 0))});
    trace.value("blur_sigma", blur_sigma);
    return 0;
}
