7#include <sleipnir/autodiff/variable.hpp>
9#include "trajopt/geometry/translation2.hpp"
11namespace trajopt::detail {
14template <
typename T,
typename U>
15decltype(
auto) line_point_squared_distance(
const Translation2<T>& line_start,
16 const Translation2<T>& line_end,
17 const Translation2<U>& point) {
18 using R =
decltype(std::declval<T>() + std::declval<U>());
20 auto lerp = [](R a, R b, R t) {
return a + t * (b - a); };
22 Translation2<R> l{line_end.x() - line_start.x(),
23 line_end.y() - line_start.y()};
24 Translation2<R> v{point.x() - line_start.x(), point.y() - line_start.y()};
26 auto t = v.dot(l) / l.squared_norm();
27 auto t_bounded = slp::max(slp::min(t, 1), 0);
29 Translation2<R> i{lerp(line_start.x(), line_end.x(), t_bounded),
30 lerp(line_start.y(), line_end.y(), t_bounded)};
31 return (i - point).squared_norm();