Program Listing for File linear_grid.hpp

Return to documentation for file (include/beluga/sensor/data/linear_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_LINEAR_GRID_HPP
#define BELUGA_SENSOR_DATA_LINEAR_GRID_HPP

#include <cstdint>
#include <optional>
#include <vector>

#include <beluga/sensor/data/dense_grid.hpp>

#include <Eigen/Core>

namespace beluga {


template <typename Derived>
class BaseLinearGrid2 : public BaseDenseGrid2<Derived> {
 public:
  /*
   * \param xi Grid cell x-axis coordinate.
   * \param yi Grid cell y-axis coordinate.
   */
  [[nodiscard]] std::size_t index_at(int xi, int yi) const {
    return static_cast<std::size_t>(yi) * this->self().width() + static_cast<std::size_t>(xi);
  }


  [[nodiscard]] std::size_t index_at(const Eigen::Vector2i& pi) const { return this->self().index_at(pi.x(), pi.y()); }

  using BaseDenseGrid2<Derived>::coordinates_at;


  [[nodiscard]] Eigen::Vector2d coordinates_at(std::size_t index) const {
    return this->self().coordinates_at(Eigen::Vector2i{
        static_cast<int>(index % this->self().width()), static_cast<int>(index / this->self().width())});
  }

  using BaseDenseGrid2<Derived>::data_at;


  [[nodiscard]] auto data_at(std::size_t index) const {
    return index < this->self().size() ? std::make_optional(this->self().data()[index]) : std::nullopt;
  }

  using BaseDenseGrid2<Derived>::neighborhood4;


  [[nodiscard]] auto neighborhood4(std::size_t index) const {
    auto result = std::vector<std::size_t>{};
    const std::size_t xi = index % this->self().width();
    const std::size_t yi = index / this->self().width();
    if (xi < (this->self().width() - 1)) {
      result.push_back(index + 1);
    }
    if (yi < (this->self().height() - 1)) {
      result.push_back(index + this->self().width());
    }
    if (xi > 0) {
      result.push_back(index - 1);
    }
    if (yi > 0) {
      result.push_back(index - this->self().width());
    }
    return result;
  }
};

}  // namespace beluga

#endif