// The unscented transform on its own: push an uncertain angle through the camera model
// with the library's sigma points, once with its default weights and once with others.
#include <cmath>
#include <cstdio>
#include <cstdlib>

#include <Eigen/Core>
#include <Eigen/Geometry>
#include <UKF/Core.h>
#include <UKF/MeasurementVector.h>

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

constexpr real_t kLength = 0.5;  // m

// Two one-field states, so that each can have its own sigma-point settings.
enum Key { Angle, TunedAngle, BobX = 10 };
using Plain = UKF::StateVector<UKF::Field<Angle, real_t>>;
using Tuned = UKF::StateVector<UKF::Field<TunedAngle, real_t>>;
using Frame = UKF::FixedMeasurementVector<UKF::Field<BobX, real_t>>;

// Sigma-point settings belong to a state type. Specialise them before any filter uses it.
namespace UKF::Parameters {
template <> constexpr real_t AlphaSquared<Tuned> = 1.0;
template <> constexpr real_t Beta<Tuned> = 2.0;
template <> constexpr real_t Kappa<Tuned> = 2.0;
}  // Plain keeps the library's defaults: AlphaSquared 1, Beta 0, Kappa 3.

namespace UKF {
template <>
template <>
real_t Frame::expected_measurement<Plain, BobX>(const Plain& state) {
    return kLength * std::sin(state.get_field<Angle>());
}
template <>
template <>
real_t Frame::expected_measurement<Tuned, BobX>(const Tuned& state) {
    return kLength * std::sin(state.get_field<TunedAngle>());
}
}  // namespace UKF

// The mean and standard deviation of the camera reading for an uncertain angle.
template <typename StateType>
void transform(const char* name, real_t angle, real_t angle_sigma, Trace& trace) {
    StateType state;
    state << angle;
    // The points sit at the mean and at the mean plus or minus the scaled root of the covariance.
    typename StateType::CovarianceMatrix spread;
    spread(0, 0) = angle_sigma * std::sqrt(UKF::Parameters::ScaledDimension<StateType>);
    const auto angles = state.calculate_sigma_point_distribution(spread);

    Frame frame;
    const auto readings = frame.template calculate_sigma_point_distribution<StateType>(angles);
    const auto mean = frame.template calculate_sigma_point_mean<StateType>(readings);
    const auto deltas = mean.template calculate_sigma_point_deltas<StateType>(readings);
    const auto covariance = mean.template calculate_sigma_point_covariance<StateType>(deltas);
    std::printf("%-8s points at %.3f, %.3f, %.3f rad -> reading %.4f +/- %.4f m\n", name, angles(0, 0),
                angles(0, 1), angles(0, 2), mean(0), std::sqrt(covariance(0, 0)));
    trace.row(name, {angles(0, 0), angles(0, 1), angles(0, 2), mean(0), std::sqrt(covariance(0, 0))});
}

int main(int argc, char** argv) {
    Trace trace(argc, argv);
    const real_t angle = argument(argc, argv, 1, 1.2);        // rad
    const real_t angle_sigma = argument(argc, argv, 2, 0.3);  // rad
    std::printf("angle %.2f +/- %.2f rad through 0.5 sin(angle):\n", angle, angle_sigma);
    transform<Plain>("default", angle, angle_sigma, trace);
    transform<Tuned>("tuned", angle, angle_sigma, trace);
    std::printf("at the mean angle alone: %.4f m\n", kLength * std::sin(angle));
    return 0;
}
