Skip to content

Commit 4768016

Browse files
committed
cross_product
Signed-off-by: silanus23 <[email protected]>
1 parent 83a9d50 commit 4768016

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

nav2_util/include/nav2_util/geometry_utils.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,35 @@ inline double distance_to_segment(
282282
return std::hypot(dx_proj, dy_proj);
283283
}
284284

285+
/**
286+
* @brief Computes the 2D cross product between the vector from start to end and the vector from start to point.
287+
* The sign of this calculation's result can be used to determine which side are you on of the track.
288+
*
289+
* See: https://en.wikipedia.org/wiki/Cross_product
290+
*
291+
* @param point The point to check relative to the segment.
292+
* @param start The starting pose of the segment.
293+
* @param end The ending pose of the segment.
294+
* @return The signed 2D cross product value.
295+
*/
296+
inline double cross_product_2d(
297+
const geometry_msgs::msg::Point & point,
298+
const geometry_msgs::msg::Pose & start,
299+
const geometry_msgs::msg::Pose & end)
300+
{
301+
const auto & p = point;
302+
const auto & a = start.position;
303+
const auto & b = end.position;
304+
305+
const double path_vec_x = b.x - a.x;
306+
const double path_vec_y = b.y - a.y;
307+
308+
const double robot_vec_x = p.x - a.x;
309+
const double robot_vec_y = p.y - a.y;
310+
311+
return (path_vec_x * robot_vec_y) - (path_vec_y * robot_vec_x);
312+
}
313+
285314
} // namespace geometry_utils
286315
} // namespace nav2_util
287316

0 commit comments

Comments
 (0)