13
13
// limitations under the License.
14
14
15
15
#include " nav2_util/path_utils.hpp"
16
+
16
17
#include < limits>
17
18
#include < cmath>
19
+ #include < stdexcept>
20
+
18
21
#include " nav2_util/geometry_utils.hpp"
19
22
namespace nav2_util
20
23
{
@@ -29,26 +32,32 @@ PathSearchResult distance_from_path(
29
32
result.closest_segment_index = start_index;
30
33
result.distance = std::numeric_limits<double >::max ();
31
34
32
- if (path.poses .size () < 2 ) {
33
- if (path.poses .empty ()) {
34
- return result;
35
- }
36
- result.distance = nav2_util::geometry_utils::euclidean_distance (
37
- robot_pose.pose , path.poses .front ().pose );
38
- result.closest_segment_index = 0 ;
35
+ if (path.poses .empty ()) {
39
36
return result;
40
37
}
41
38
42
- if (start_index >= path.poses .size () - 1 ) {
39
+ if (path.poses .size () == 1 ) {
43
40
result.distance = nav2_util::geometry_utils::euclidean_distance (
44
- robot_pose.pose ,
45
- path.poses .back ().pose );
46
- result.closest_segment_index = path.poses .size () - 1 ;
41
+ robot_pose.pose , path.poses .front ().pose );
42
+ result.closest_segment_index = 0 ;
47
43
return result;
48
44
}
49
45
50
- double distance_traversed = 0.0 ;
46
+ if (start_index >= path.poses .size ()) {
47
+ throw std::invalid_argument (
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
+ }
51
52
53
+ if (path.header .frame_id != robot_pose.header .frame_id ) {
54
+ throw std::invalid_argument (
55
+ " Invalid input, path frame (" + (path.header .frame_id ) +
56
+ " ) is not the same frame as robot frame (" + (robot_pose.header .frame_id ) +
57
+ " ). Use the same frame for both pose and path." );
58
+ }
59
+
60
+ double distance_traversed = 0.0 ;
52
61
for (size_t i = start_index; i < path.poses .size () - 1 ; ++i) {
53
62
if (distance_traversed > search_window_length) {
54
63
break ;
0 commit comments