linear_grid.hpp
Go to the documentation of this file.
1 // Copyright 2023 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BELUGA_SENSOR_DATA_LINEAR_GRID_HPP
16 #define BELUGA_SENSOR_DATA_LINEAR_GRID_HPP
17 
18 #include <cstdint>
19 #include <optional>
20 #include <vector>
21 
23 
24 #include <Eigen/Core>
25 
31 namespace beluga {
32 
54 
63 template <typename Derived>
64 class BaseLinearGrid2 : public BaseDenseGrid2<Derived> {
65  public:
67  /*
68  * \param xi Grid cell x-axis coordinate.
69  * \param yi Grid cell y-axis coordinate.
70  */
71  [[nodiscard]] std::size_t index_at(int xi, int yi) const {
72  return static_cast<std::size_t>(yi) * this->self().width() + static_cast<std::size_t>(xi);
73  }
74 
76 
79  [[nodiscard]] std::size_t index_at(const Eigen::Vector2i& pi) const { return this->self().index_at(pi.x(), pi.y()); }
80 
82 
84 
88  [[nodiscard]] Eigen::Vector2d coordinates_at(std::size_t index) const {
89  return this->self().coordinates_at(Eigen::Vector2i{
90  static_cast<int>(index % this->self().width()), static_cast<int>(index / this->self().width())});
91  }
92 
94 
96 
100  [[nodiscard]] auto data_at(std::size_t index) const {
101  return index < this->self().size() ? std::make_optional(this->self().data()[index]) : std::nullopt;
102  }
103 
105 
107 
111  [[nodiscard]] auto neighborhood4(std::size_t index) const {
112  auto result = std::vector<std::size_t>{};
113  const std::size_t xi = index % this->self().width();
114  const std::size_t yi = index / this->self().width();
115  if (xi < (this->self().width() - 1)) {
116  result.push_back(index + 1);
117  }
118  if (yi < (this->self().height() - 1)) {
119  result.push_back(index + this->self().width());
120  }
121  if (xi > 0) {
122  result.push_back(index - 1);
123  }
124  if (yi > 0) {
125  result.push_back(index - this->self().width());
126  }
127  return result;
128  }
129 };
130 
131 } // namespace beluga
132 
133 #endif
result
result[0]
beluga::BaseLinearGrid2
Linear 2D grid base type.
Definition: linear_grid.hpp:64
beluga::BaseLinearGrid2::index_at
std::size_t index_at(const Eigen::Vector2i &pi) const
Computes index for given grid cell coordinates.
Definition: linear_grid.hpp:79
beluga::BaseDenseGrid2
Dense 2D grid base type.
Definition: dense_grid.hpp:83
dense_grid.hpp
Concepts and abstract implementations of dense grids.
beluga::BaseLinearGrid2::neighborhood4
auto neighborhood4(std::size_t index) const
Computes 4-connected neighborhood for cell.
Definition: linear_grid.hpp:111
beluga::BaseLinearGrid2::index_at
std::size_t index_at(int xi, int yi) const
Computes index for given grid cell coordinates.
Definition: linear_grid.hpp:71
beluga::BaseLinearGrid2::data_at
auto data_at(std::size_t index) const
Gets cell data, if included.
Definition: linear_grid.hpp:100
beluga::BaseLinearGrid2::coordinates_at
Eigen::Vector2d coordinates_at(std::size_t index) const
Compute plane coordinates given a grid cell index.
Definition: linear_grid.hpp:88
beluga
The main Beluga namespace.
Definition: 3d_embedding.hpp:21


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53