Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
path_utils.cpp
1 // Copyright (c) 2025 Berkan Tali
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 #include "nav2_util/path_utils.hpp"
16 
17 #include <limits>
18 #include <cmath>
19 #include <stdexcept>
20 
21 #include "nav2_util/geometry_utils.hpp"
22 namespace nav2_util
23 {
24 
25 PathSearchResult distance_from_path(
26  const nav_msgs::msg::Path & path,
27  const geometry_msgs::msg::Pose & robot_pose,
28  const size_t start_index,
29  const double search_window_length)
30 {
31  PathSearchResult result;
32  result.closest_segment_index = start_index;
33  result.distance = std::numeric_limits<double>::max();
34 
35  if (path.poses.empty()) {
36  return result;
37  }
38 
39  if (path.poses.size() == 1) {
40  result.distance = nav2_util::geometry_utils::euclidean_distance(
41  robot_pose, path.poses.front().pose);
42  result.closest_segment_index = 0;
43  return result;
44  }
45 
46  if (start_index >= path.poses.size()) {
47  throw std::runtime_error(
48  "Invalid operation: requested start index (" + std::to_string(start_index) +
49  ") is greater than or equal to path size (" + std::to_string(path.poses.size()) +
50  "). Application is not properly managing state.");
51  }
52 
53  double distance_traversed = 0.0;
54  for (size_t i = start_index; i < path.poses.size() - 1; ++i) {
55  if (distance_traversed > search_window_length) {
56  break;
57  }
58 
59  const double current_distance = geometry_utils::distance_to_path_segment(
60  robot_pose.position,
61  path.poses[i].pose,
62  path.poses[i + 1].pose);
63 
64  if (current_distance < result.distance) {
65  result.distance = current_distance;
66  result.closest_segment_index = i;
67  }
68 
69  distance_traversed += geometry_utils::euclidean_distance(
70  path.poses[i],
71  path.poses[i + 1]);
72  }
73 
74  const auto & segment_start = path.poses[result.closest_segment_index];
75  const auto & segment_end = path.poses[result.closest_segment_index + 1];
76 
77  // Obtain the signed direction of the cross track error
78  const double cross_product = geometry_utils::cross_product_2d(
79  robot_pose.position, segment_start.pose, segment_end.pose);
80  result.distance *= (cross_product >= 0.0 ? 1.0 : -1.0);
81 
82  return result;
83 }
84 
85 } // namespace nav2_util