Skip to content

Commit 78c0c32

Browse files
committed
fixed geometry utils adding 2d
Signed-off-by: silanus23 <[email protected]>
1 parent 38d7ba3 commit 78c0c32

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

nav2_util/include/nav2_util/geometry_utils.hpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,16 @@ inline int find_next_matching_goal_in_waypoint_statuses(
191191
/**
192192
* @brief Find the distance to a point
193193
* @param global_pose Robot's current or planned position
194-
* @param target
194+
* @param target target point
195195
* @return int
196196
*/
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)
199200
{
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);
203204
}
204205

205206
/**
@@ -210,29 +211,34 @@ inline double distanceToPoint(const geometry_msgs::msg::PoseStamped &global_pose
210211
* @return int
211212
*/
212213
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)
216217
{
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;
220221

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);
225226

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);
228233

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;
231236

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);
235240
}
241+
236242
} // namespace geometry_utils
237243
} // namespace nav2_util
238244

0 commit comments

Comments
 (0)