#ifndef CALC_HPP_
#define CALC_HPP_

#include "data.hpp"

#include <cmath>
#include <numeric>
#include <vector>


namespace calc {


template <typename iter_t>
double norm(iter_t first, iter_t last)
{
  return std::sqrt(std::inner_product(first, last, first, 0.0));
}


std::pair<Position, Position>
position_segment(std::vector<Position> const& position, double time);

void interpolate_positions(std::vector<Magnetic>& magnetics, std::vector<Position> const& truth);
void crop_temporal_inersection(std::vector<Magnetic>& magnetics, std::vector<Position> const& truth);

std::tuple<double, double, double, double>
spatial_extent(std::vector<Magnetic> const& mag);

template<typename grid_t, typename avg_grid_t>
void average_grid(grid_t& grid, avg_grid_t& average_grid)
{
  for (size_t x = 0; x < grid.size_x(); x++) {
    for (size_t y = 0; y < grid.size_y(); y++) {
      auto const& cell = grid[x][y];
      if (cell.size() > 0) {
        double const sum = std::accumulate(cell.cbegin(), cell.cend(), 0.0);
        size_t const n = cell.size();
        average_grid[x][y] = sum / n;
      }
    }
  }
}

} // namespace calc

#endif // CALC_HPP_
