43 #include <ilqgames/cost/signed_distance_cost.h> 44 #include <ilqgames/utils/types.h> 46 #include <glog/logging.h> 51 float SignedDistanceCost::Evaluate(
const VectorXf& input)
const {
52 CHECK_LT(xdim1_, input.size());
53 CHECK_LT(ydim1_, input.size());
54 CHECK_LT(xdim2_, input.size());
55 CHECK_LT(ydim2_, input.size());
58 const float dx = input(xdim1_) - input(xdim2_);
59 const float dy = input(ydim1_) - input(ydim2_);
60 const float cost = nominal_ - std::hypot(dx, dy);
62 return (less_is_positive_) ? cost : -cost;
65 void SignedDistanceCost::Quadraticize(
const VectorXf& input, MatrixXf* hess,
66 VectorXf* grad)
const {
67 CHECK_LT(xdim1_, input.size());
68 CHECK_LT(ydim1_, input.size());
69 CHECK_LT(xdim2_, input.size());
70 CHECK_LT(ydim2_, input.size());
75 CHECK_EQ(input.size(), hess->rows());
76 CHECK_EQ(input.size(), hess->cols());
77 CHECK_EQ(input.size(), grad->size());
80 const float s = (less_is_positive_) ? 1.0 : -1.0;
81 const float delta_x = input(xdim1_) - input(xdim2_);
82 const float delta_y = input(ydim1_) - input(ydim2_);
83 const float norm = std::hypot(delta_x, delta_y);
84 const float norm_3 = norm * norm * norm;
86 const float dx1 = -s * delta_x / norm;
87 const float dy1 = -s * delta_y / norm;
88 const float ddx1 = -s * delta_y * delta_y / norm_3;
89 const float ddy1 = -s * delta_x * delta_x / norm_3;
90 const float dx1dy1 = s * delta_x * delta_y / norm_3;
92 (*grad)(xdim1_) += dx1;
93 (*grad)(ydim1_) += dy1;
94 (*grad)(xdim2_) -= dx1;
95 (*grad)(ydim2_) -= dy1;
97 (*hess)(xdim1_, xdim1_) += ddx1;
98 (*hess)(ydim1_, ydim1_) += ddy1;
99 (*hess)(xdim1_, ydim1_) += dx1dy1;
100 (*hess)(ydim1_, xdim1_) += dx1dy1;
101 (*hess)(xdim2_, xdim2_) += ddx1;
102 (*hess)(ydim2_, ydim2_) += ddy1;
103 (*hess)(xdim2_, ydim2_) += dx1dy1;
104 (*hess)(ydim2_, xdim2_) += dx1dy1;
105 (*hess)(xdim1_, xdim2_) -= ddx1;
106 (*hess)(xdim1_, ydim2_) -= dx1dy1;
107 (*hess)(ydim1_, xdim2_) -= dx1dy1;
108 (*hess)(ydim1_, ydim2_) -= ddy1;
109 (*hess)(xdim2_, xdim1_) -= ddx1;
110 (*hess)(xdim2_, ydim1_) -= dx1dy1;
111 (*hess)(ydim2_, xdim1_) -= dx1dy1;
112 (*hess)(ydim2_, ydim1_) -= ddy1;