include/beluga/algorithm/raycasting.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_ALGORITHM_RAYCASTING_HPP
16 #define BELUGA_ALGORITHM_RAYCASTING_HPP
17 
18 #include <algorithm>
19 #include <optional>
20 #include <utility>
21 
23 
24 #include <range/v3/view/all.hpp>
25 #include <range/v3/view/take_while.hpp>
26 
27 #include <sophus/se2.hpp>
28 #include <sophus/so2.hpp>
29 
35 namespace beluga {
36 
38 
43 template <class OccupancyGrid, typename Algorithm = Bresenham2i>
44 class Ray2d {
45  public:
47 
51  explicit Ray2d(const OccupancyGrid& grid, const Sophus::SE2d& source_pose, double max_range) noexcept
52  : Ray2d(grid, Algorithm{}, source_pose, max_range) {}
53 
55 
62  explicit Ray2d(
63  const OccupancyGrid& grid,
64  Algorithm algorithm,
65  const Sophus::SE2d& source_pose,
66  double max_range) noexcept
67  : grid_(grid),
68  algorithm_(std::move(algorithm)),
69  source_pose_in_local_frame_(grid_.origin().inverse() * source_pose),
71  max_range_(max_range) {}
72 
74 
79  [[nodiscard]] auto trace(const Sophus::SO2d& bearing) const {
80  // Don't do the multiplication in SE2, it's slower
81  const auto& r1 = source_pose_in_local_frame_.so2();
82  const auto& t1 = source_pose_in_local_frame_.translation();
83  const auto t2 = bearing.unit_complex() * max_range_;
84  const auto far_end_pose_in_local_frame = Sophus::SE2d{r1, r1 * t2 + t1};
85  const auto far_end_cell = grid_.cell_near(far_end_pose_in_local_frame.translation());
86  const auto cell_is_valid = [this](const auto& cell) { return grid_.contains(cell); };
87  return algorithm_(source_cell_, far_end_cell) | ranges::views::take_while(cell_is_valid);
88  }
89 
91 
97  [[nodiscard]] std::optional<double> cast(const Sophus::SO2d& bearing) const {
98  for (const auto& cell : trace(bearing)) {
99  if (!grid_.free_at(cell)) {
100  const auto source_position = grid_.coordinates_at(source_cell_);
101  const auto cell_position = grid_.coordinates_at(cell);
102  const auto distance = (cell_position - source_position).norm();
103  return std::make_optional(std::min(distance, max_range_));
104  }
105  }
106  return std::nullopt;
107  }
108 
109  private:
110  const OccupancyGrid& grid_;
111  const Algorithm algorithm_;
113  const Eigen::Vector2i source_cell_;
114  const double max_range_;
115 };
116 
117 } // namespace beluga
118 
119 #endif
Sophus::SO2
beluga::Ray2d::source_pose_in_local_frame_
const Sophus::SE2d source_pose_in_local_frame_
Definition: include/beluga/algorithm/raycasting.hpp:112
beluga::Ray2d::algorithm_
const Algorithm algorithm_
Definition: include/beluga/algorithm/raycasting.hpp:111
beluga::Ray2d::max_range_
const double max_range_
Definition: include/beluga/algorithm/raycasting.hpp:114
beluga::Ray2d::cast
std::optional< double > cast(const Sophus::SO2d &bearing) const
Casts ray along a given direction.
Definition: include/beluga/algorithm/raycasting.hpp:97
Sophus::SE2::so2
SOPHUS_FUNC SO2Member & so2()
bresenham.hpp
Implementation of Bresenham-like raytracing algorithms.
se2.hpp
beluga::Ray2d::source_cell_
const Eigen::Vector2i source_cell_
Definition: include/beluga/algorithm/raycasting.hpp:113
beluga::Ray2d::Ray2d
Ray2d(const OccupancyGrid &grid, const Sophus::SE2d &source_pose, double max_range) noexcept
Constructs 2D ray with default ray tracing algorithm.
Definition: include/beluga/algorithm/raycasting.hpp:51
Sophus::SE2
beluga::Ray2d::grid_
const OccupancyGrid & grid_
Definition: include/beluga/algorithm/raycasting.hpp:110
so2.hpp
beluga::Ray2d::trace
auto trace(const Sophus::SO2d &bearing) const
Computes ray trace along a given direction.
Definition: include/beluga/algorithm/raycasting.hpp:79
Sophus::SE2::translation
SOPHUS_FUNC TranslationMember & translation()
beluga::Ray2d::Ray2d
Ray2d(const OccupancyGrid &grid, Algorithm algorithm, const Sophus::SE2d &source_pose, double max_range) noexcept
Constructs 2D ray with an specific ray tracing algorithm.
Definition: include/beluga/algorithm/raycasting.hpp:62
beluga
The main Beluga namespace.
Definition: 3d_embedding.hpp:21
beluga::Ray2d
Castable 2D ray.
Definition: include/beluga/algorithm/raycasting.hpp:44
Sophus::SO2::unit_complex
SOPHUS_FUNC ComplexMember const & unit_complex() const


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