43 #include <ilqgames/cost/quadratic_polyline2_cost.h> 44 #include <ilqgames/cost/time_invariant_cost.h> 45 #include <ilqgames/geometry/polyline2.h> 46 #include <ilqgames/utils/types.h> 52 float QuadraticPolyline2Cost::Evaluate(
const VectorXf& input)
const {
53 CHECK_LT(xidx_, input.size());
54 CHECK_LT(yidx_, input.size());
57 float signed_squared_distance;
59 polyline_.ClosestPoint(Point2(input(xidx_), input(yidx_)),
nullptr,
nullptr,
60 &signed_squared_distance, &is_endpoint);
65 signed_squared_distance = 0.0;
68 return 0.5 * weight_ * std::abs(signed_squared_distance);
71 void QuadraticPolyline2Cost::Quadraticize(
const VectorXf& input, MatrixXf* hess,
72 VectorXf* grad)
const {
73 CHECK_LT(xidx_, input.size());
74 CHECK_LT(yidx_, input.size());
78 CHECK_EQ(input.size(), hess->rows());
79 CHECK_EQ(input.size(), hess->cols());
80 CHECK_EQ(input.size(), grad->size());
83 const Point2 current_position(input(xidx_), input(yidx_));
87 LineSegment2 segment(Point2(0.0, 0.0), Point2(1.0, 1.0));
88 const Point2 closest_point = polyline_.ClosestPoint(
89 current_position, &is_vertex, &segment,
nullptr, &is_endpoint);
92 if (is_endpoint)
return;
99 float dx = weight_ * (current_position.x() - closest_point.x());
100 float dy = weight_ * (current_position.y() - closest_point.y());
103 const Point2 relative = current_position - segment.FirstPoint();
104 const Point2& unit_segment = segment.UnitDirection();
107 ddx = weight_ * unit_segment.y() * unit_segment.y();
108 ddy = weight_ * unit_segment.x() * unit_segment.x();
109 dxdy = -weight_ * unit_segment.x() * unit_segment.y();
112 const float w_cross = weight_ * (relative.x() * unit_segment.y() -
113 relative.y() * unit_segment.x());
115 dx = w_cross * unit_segment.y();
116 dy = -w_cross * unit_segment.x();
119 (*grad)(xidx_) += dx;
120 (*grad)(yidx_) += dy;
122 (*hess)(xidx_, xidx_) += ddx;
123 (*hess)(yidx_, yidx_) += ddy;
124 (*hess)(xidx_, yidx_) += dxdy;
125 (*hess)(yidx_, xidx_) += dxdy;