Program Listing for File regular_grid.hpp

Return to documentation for file (include/beluga/sensor/data/regular_grid.hpp)

// Copyright 2023 Ekumen, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef BELUGA_SENSOR_DATA_REGULAR_GRID_HPP
#define BELUGA_SENSOR_DATA_REGULAR_GRID_HPP

#include <cmath>

#include <ciabatta/ciabatta.hpp>

#include <range/v3/view/transform.hpp>

#include <Eigen/Core>
#include <beluga/eigen_compatibility.hpp>

namespace beluga {


template <typename Derived, int NDim>
class BaseRegularGrid : public ciabatta::ciabatta_top<Derived> {
 public:

  [[nodiscard]] Eigen::Vector<int, NDim> cell_near(const Eigen::Vector<double, NDim>& p) const {
    const auto inv_resolution = 1. / this->self().resolution();
    return (p * inv_resolution).array().floor().template cast<int>();
  }


  [[nodiscard]] Eigen::Vector<double, NDim> coordinates_at(const Eigen::Vector<int, NDim>& pi) const {
    return (pi.template cast<double>() + Eigen::Vector<double, NDim>::Ones() * 0.5) * this->self().resolution();
  }


  template <class Range>
  [[nodiscard]] auto coordinates_for(Range&& cells) const {
    return cells | ranges::views::transform([this](const auto& cell) { return this->self().coordinates_at(cell); });
  }
};
template <typename Derived>
using BaseRegularGrid2 = BaseRegularGrid<Derived, 2>;

template <typename Derived>
using BaseRegularGrid3 = BaseRegularGrid<Derived, 3>;

}  // namespace beluga

#endif