@@ -191,15 +191,16 @@ inline int find_next_matching_goal_in_waypoint_statuses(
191
191
/* *
192
192
* @brief Find the distance to a point
193
193
* @param global_pose Robot's current or planned position
194
- * @param target
194
+ * @param target target point
195
195
* @return int
196
196
*/
197
- inline double distanceToPoint (const geometry_msgs::msg::PoseStamped &global_pose,
198
- const geometry_msgs::msg::PoseStamped &target)
197
+ inline double distanceToPoint (
198
+ const geometry_msgs::msg::PoseStamped & point1,
199
+ const geometry_msgs::msg::PoseStamped & point2)
199
200
{
200
- const double xDist = global_pose .pose .position .x - target .pose .position .x ;
201
- const double yDist = global_pose .pose .position .y - target .pose .position .y ;
202
- return std::hypot (xDist,yDist );
201
+ const double dx = point1 .pose .position .x - point2 .pose .position .x ;
202
+ const double dy = point1 .pose .position .y - point2 .pose .position .y ;
203
+ return std::hypot (dx, dy );
203
204
}
204
205
205
206
/* *
@@ -210,29 +211,34 @@ inline double distanceToPoint(const geometry_msgs::msg::PoseStamped &global_pose
210
211
* @return int
211
212
*/
212
213
inline double distanceToSegment (
213
- const geometry_msgs::msg::PoseStamped & point,
214
- const geometry_msgs::msg::PoseStamped & start,
215
- const geometry_msgs::msg::PoseStamped & end)
214
+ const geometry_msgs::msg::PoseStamped & point,
215
+ const geometry_msgs::msg::PoseStamped & start,
216
+ const geometry_msgs::msg::PoseStamped & end)
216
217
{
217
- const auto & p = point.pose .position ;
218
- const auto & a = start.pose .position ;
219
- const auto & b = end.pose .position ;
218
+ const auto & p = point.pose .position ;
219
+ const auto & a = start.pose .position ;
220
+ const auto & b = end.pose .position ;
220
221
221
- const double seg_len_sq = distanceToPoint (start, end) ;
222
- if (seg_len_sq <= 1e-8 ) {
223
- return std::sqrt ( distanceToPoint (start, end));
224
- }
222
+ const double dx_seg = b. x - a. x ;
223
+ const double dy_seg = b. y - a. y ;
224
+
225
+ const double seg_len_sq = (dx_seg * dx_seg) + (dy_seg * dy_seg);
225
226
226
- const double dot = ((p.x - a.x ) * (b.x - a.x )) + ((p.y - a.y ) * (b.y - a.y ));
227
- const double t = std::clamp (dot / seg_len_sq, 0.0 , 1.0 );
227
+ if (seg_len_sq <= 1e-9 ) {
228
+ return distanceToPoint (point, start);
229
+ }
230
+
231
+ const double dot = ((p.x - a.x ) * dx_seg) + ((p.y - a.y ) * dy_seg);
232
+ const double t = std::clamp (dot / seg_len_sq, 0.0 , 1.0 );
228
233
229
- const double proj_x = a.x + t * (b. x - a. x ) ;
230
- const double proj_y = a.y + t * (b. y - a. y ) ;
234
+ const double proj_x = a.x + t * dx_seg ;
235
+ const double proj_y = a.y + t * dy_seg ;
231
236
232
- const double dx_proj = p.x - proj_x;
233
- const double dy_proj = p.y - proj_y;
234
- return std::hypot (dx_proj,dy_proj);
237
+ const double dx_proj = p.x - proj_x;
238
+ const double dy_proj = p.y - proj_y;
239
+ return std::hypot (dx_proj, dy_proj);
235
240
}
241
+
236
242
} // namespace geometry_utils
237
243
} // namespace nav2_util
238
244
0 commit comments