56 bool operator()(
const T*
const x_ptr,
const T*
const y_ptr,
T* out_cost)
const
59 const Eigen::Transform<T, 2, 1> tfT = convertTransform<T>(map_.origin());
62 const T cell =
T(cell_size_);
63 const T half_cell = cell /
T(2.0);
66 const T vX = (localPosition.x() - half_cell -
T(map_.getLocalSceneBounds().min.x())) / cell;
67 const T vY = (localPosition.y() - half_cell -
T(map_.getLocalSceneBounds().min.y())) / cell;
71 const T vX_shift = vX -
T(0.01);
72 const T vY_shift = vY -
T(0.01);
74 int iXlow =
static_cast<int>(toFloat(ceres::floor(vX_shift)));
75 int iXhigh =
static_cast<int>(toFloat(ceres::ceil(vX_shift)));
76 int iYlow =
static_cast<int>(toFloat(ceres::floor(vY_shift)));
77 int iYhigh =
static_cast<int>(toFloat(ceres::ceil(vY_shift)));
80 const int maxX =
static_cast<int>(map_.getGrid().rows()) - 1;
81 const int maxY =
static_cast<int>(map_.getGrid().cols()) - 1;
82 iXlow = std::clamp(iXlow, 0, maxX);
83 iXhigh = std::clamp(iXhigh, 0, maxX);
84 iYlow = std::clamp(iYlow, 0, maxY);
85 iYhigh = std::clamp(iYhigh, 0, maxY);
88 const double d_ll = queryCell(iXlow, iYlow);
89 const double d_hl = queryCell(iXhigh, iYlow);
90 const double d_lh = queryCell(iXlow, iYhigh);
91 const double d_hh = queryCell(iXhigh, iYhigh);
96 const T w00 = (
T(iXhigh) - vX) * (
T(iYhigh) - vY);
97 const T w10 = (vX -
T(iXlow)) * (
T(iYhigh) - vY);
98 const T w01 = (
T(iXhigh) - vX) * (vY -
T(iYlow));
99 const T w11 = (vX -
T(iXlow)) * (vY -
T(iYlow));
101 T d_bilinear = w00 *
T(d_ll) + w10 *
T(d_hl) + w01 *
T(d_lh) + w11 *
T(d_hh);
105 const T sumW = w00 + w10 + w01 + w11;
106 if (ceres::abs(sumW) <
T(1e-6))
108 const std::array<std::pair<int, int>, 4> idxs = {{{iXlow, iYlow},
112 const std::array<double, 4> vals = {d_ll, d_hl, d_lh, d_hh};
113 double bestVal = vals[0];
114 double bestDist = std::numeric_limits<double>::max();
115 for (
int k = 0; k < 4; ++k)
117 const double dx =
static_cast<double>(idxs[k].first) - toFloat(vX);
118 const double dy =
static_cast<double>(idxs[k].second) - toFloat(vY);
119 const double dist = dx * dx + dy * dy;
126 d_bilinear =
T(bestVal);
130 T d_conservative = d_bilinear - half_cell;
133 const double d_min = std::min({d_ll, d_hl, d_lh, d_hh});
134 if (d_min <= 0.0 && d_conservative > -half_cell)
136 d_conservative = -half_cell;
139 out_cost[0] = d_conservative;
169 static Eigen::Transform<T, 2, 1> convertTransform(
const Eigen::Transform<float, 2, 1>& tf)