47 #include <ilqgames/constraint/proximity_constraint.h> 48 #include <ilqgames/utils/types.h> 50 #include <glog/logging.h> 56 float ProximityConstraint::Evaluate(
const VectorXf& input)
const {
57 const float dx = input(xidx1_) - input(xidx2_);
58 const float dy = input(yidx1_) - input(yidx2_);
59 const float value = std::hypot(dx, dy) - threshold_;
61 return (keep_within_) ? value : -value;
64 void ProximityConstraint::Quadraticize(Time t,
const VectorXf& input,
65 MatrixXf* hess, VectorXf* grad)
const {
68 CHECK_EQ(hess->rows(), input.size());
69 CHECK_EQ(hess->cols(), input.size());
70 CHECK_EQ(grad->size(), input.size());
73 const float dx = input(xidx1_) - input(xidx2_);
74 const float dy = input(yidx1_) - input(yidx2_);
75 const float prox = std::hypot(dx, dy);
76 const float sign = (keep_within_) ? 1.0 : -1.0;
77 const float g = sign * (prox - threshold_);
80 const float rel_dx = dx / prox;
81 const float rel_dy = dy / prox;
83 float grad_x1 = sign * rel_dx;
84 float grad_y1 = sign * rel_dy;
85 float hess_x1x1 = sign * (1.0 - rel_dx * rel_dx) / prox;
86 float hess_y1y1 = sign * (1.0 - rel_dy * rel_dy) / prox;
87 float hess_x1y1 = -sign * rel_dx * rel_dy / prox;
89 ModifyDerivatives(t, g, &grad_x1, &hess_x1x1, &grad_y1, &hess_y1y1,
92 (*grad)(xidx1_) += grad_x1;
93 (*grad)(xidx2_) -= grad_x1;
95 (*grad)(yidx1_) += grad_y1;
96 (*grad)(yidx2_) -= grad_y1;
98 (*hess)(xidx1_, xidx1_) += hess_x1x1;
99 (*hess)(xidx1_, xidx2_) -= hess_x1x1;
100 (*hess)(xidx2_, xidx1_) -= hess_x1x1;
101 (*hess)(xidx2_, xidx2_) += hess_x1x1;
103 (*hess)(yidx1_, yidx1_) += hess_y1y1;
104 (*hess)(yidx1_, yidx2_) -= hess_y1y1;
105 (*hess)(yidx2_, yidx1_) -= hess_y1y1;
106 (*hess)(yidx2_, yidx2_) += hess_y1y1;
108 (*hess)(xidx1_, yidx1_) += hess_x1y1;
109 (*hess)(xidx1_, yidx2_) -= hess_x1y1;
110 (*hess)(xidx2_, yidx1_) -= hess_x1y1;
111 (*hess)(xidx2_, yidx2_) += hess_x1y1;
112 (*hess)(yidx1_, xidx1_) += hess_x1y1;
113 (*hess)(yidx1_, xidx2_) -= hess_x1y1;
114 (*hess)(yidx2_, xidx1_) -= hess_x1y1;
115 (*hess)(yidx2_, xidx2_) += hess_x1y1;