/********************************************************************* * * Software License Agreement (BSD License) * * Author: DuongTD *********************************************************************/ #include "sbpl_lattice_planner/path_smoother.h" #include #include #include namespace sbpl_lattice_planner { namespace { constexpr double kEps = 1e-9; constexpr double kMaxFilletCornerDeg = 170.0; // beyond this tan(theta/2) explodes: turn in place double dist(const SmoothPose& a, const SmoothPose& b) { return std::hypot(b.x - a.x, b.y - a.y); } double wrapAngle(double a) { return std::remainder(a, 2.0 * M_PI); } } // namespace double pathLength(const std::vector& p) { double len = 0.0; for (std::size_t i = 1; i < p.size(); ++i) len += dist(p[i - 1], p[i]); return len; } PathSmoother::PathSmoother(const SmootherConfig& cfg, CellQuery cell, PoseQuery pose) : cfg_(cfg), cell_(std::move(cell)), pose_(std::move(pose)) { } bool PathSmoother::poseFree(double x, double y, double theta) const { switch (cell_(x, y)) { case CellClass::kBlocked: return false; case CellClass::kFree: return true; case CellClass::kNearObstacle: return pose_ ? pose_(x, y, theta) : false; } return false; } bool PathSmoother::segmentFree(const SmoothPose& a, const SmoothPose& b) const { // Two passes: the centre test is one cell lookup, the footprint test hundreds. Candidates that // fail on the centre (most of them during the downward scan) never pay for the footprint. const double len = dist(a, b); const double theta = std::atan2(b.y - a.y, b.x - a.x); const int n = std::max(1, static_cast(std::ceil(len / cfg_.check_step_m))); bool near = false; for (int s = 0; s <= n; ++s) { const double f = static_cast(s) / n; const CellClass c = cell_(a.x + (b.x - a.x) * f, a.y + (b.y - a.y) * f); if (c == CellClass::kBlocked) return false; near |= c == CellClass::kNearObstacle; } if (!near) return true; if (!pose_) return false; // Footprint at sample_step_m: two placements 5 cm apart overlap almost entirely, so no cell of // the swept area is skipped; check_step_m would double the cost for nothing. const int m = std::max(1, static_cast(std::ceil(len / cfg_.sample_step_m))); for (int s = 0; s <= m; ++s) { const double f = static_cast(s) / m; const double x = a.x + (b.x - a.x) * f; const double y = a.y + (b.y - a.y) * f; if (cell_(x, y) == CellClass::kNearObstacle && !pose_(x, y, theta)) return false; } return true; } bool PathSmoother::arcFree(double cx, double cy, double r, double a0, double sweep) const { const double side = sweep >= 0.0 ? 1.0 : -1.0; const int n = std::max(1, static_cast(std::ceil(std::fabs(sweep) * r / cfg_.check_step_m))); bool near = false; for (int s = 0; s <= n; ++s) { const double a = a0 + sweep * static_cast(s) / n; const CellClass c = cell_(cx + r * std::cos(a), cy + r * std::sin(a)); if (c == CellClass::kBlocked) return false; near |= c == CellClass::kNearObstacle; } if (!near) return true; if (!pose_) return false; const int m = std::max(1, static_cast(std::ceil(std::fabs(sweep) * r / cfg_.sample_step_m))); for (int s = 0; s <= m; ++s) { const double a = a0 + sweep * static_cast(s) / m; const double x = cx + r * std::cos(a); const double y = cy + r * std::sin(a); if (cell_(x, y) == CellClass::kNearObstacle && !pose_(x, y, wrapAngle(a + side * M_PI / 2.0))) return false; } return true; } void PathSmoother::appendArc(double cx, double cy, double r, double a0, double sweep, std::vector& out) const { const double side = sweep >= 0.0 ? 1.0 : -1.0; const int n = std::max(1, static_cast(std::ceil(std::fabs(sweep) * r / cfg_.sample_step_m))); for (int s = 0; s < n; ++s) { // end point belongs to the next piece const double a = a0 + sweep * static_cast(s) / n; out.push_back({cx + r * std::cos(a), cy + r * std::sin(a), wrapAngle(a + side * M_PI / 2.0)}); } } void PathSmoother::appendSegment(const SmoothPose& a, const SmoothPose& b, std::vector& out) const { const double len = dist(a, b); if (len < kEps) return; const double theta = std::atan2(b.y - a.y, b.x - a.x); const int n = std::max(1, static_cast(std::ceil(len / cfg_.sample_step_m))); for (int s = 0; s < n; ++s) { // end point belongs to the next piece const double f = static_cast(s) / n; out.push_back({a.x + (b.x - a.x) * f, a.y + (b.y - a.y) * f, theta}); } } bool PathSmoother::smooth(const std::vector& in, std::vector& out, SmootherStats* stats) const { out.clear(); SmootherStats st; st.in_pts = in.size(); st.in_len_m = pathLength(in); if (stats) *stats = st; if (in.size() < 3) return false; // 1. Distinct positions only (turn-in-place primitives repeat a position with new headings), // and refuse anything driven in reverse: a shortcut across a cusp would flip it to forward. std::vector pts; pts.reserve(in.size()); pts.push_back(in.front()); for (std::size_t i = 1; i < in.size(); ++i) { const SmoothPose& prev = pts.back(); const double dx = in[i].x - prev.x; const double dy = in[i].y - prev.y; if (std::hypot(dx, dy) < 1e-6) continue; // Heading at the START of the step decides direction: the last pose may carry the goal yaw, // which can differ from the arrival direction by more than 90 deg without any reversing. if (dx * std::cos(prev.theta) + dy * std::sin(prev.theta) < 0.0) return false; pts.push_back(in[i]); } if (pts.size() < 2) return false; std::vector cum(pts.size(), 0.0); for (std::size_t i = 1; i < pts.size(); ++i) cum[i] = cum[i - 1] + dist(pts[i - 1], pts[i]); // 2. String pulling: from each anchor take the farthest point (within max_shortcut_m of path // length) whose straight segment is free. Visibility is not monotonic in j, so scan downward // and take the first hit — coarsely (kScanStrideM) first, then refine inside the last stride // so a 30 m window costs ~80 segment tests instead of 600. If even the next point fails // (the lattice validated it by its own sampling of primitive cells, ours is finer) keep the // original step: never worse than the input. // Junctions (2026-09-22, sehc pillar field): a shortcut that lands on a stretch of untouched // lattice path has no room for a fillet there, so the chord must already point roughly along // the lattice heading (max_junction_deg) — otherwise the tracker sees a kink and crawls // (Priest: kappa 2.3-2.6 -> 0.2 m/s). Aligned end points are preferred; only when none is // visible does the farthest visible one win. constexpr double kScanStrideM = 0.4; const double spacing = pts.size() > 1 ? cum.back() / static_cast(pts.size() - 1) : 1.0; const std::size_t stride = std::max(1, static_cast(kScanStrideM / std::max(spacing, 1e-3))); const double max_junction = cfg_.max_junction_deg * M_PI / 180.0; const std::size_t last = pts.size() - 1; // Chord a->b vs the lattice heading stored at k. The two ends of the path are exempt: the start // heading is whatever free_start_heading assumed, the goal yaw is reached by turning in place. const auto aligned = [&](std::size_t a, std::size_t b, std::size_t k) { if (max_junction <= 0.0 || k == 0 || k == last) return true; const double chord = std::atan2(pts[b].y - pts[a].y, pts[b].x - pts[a].x); return std::fabs(wrapAngle(pts[k].theta - chord)) <= max_junction; }; std::vector anchors; anchors.push_back(0); std::size_t i = 0; bool prev_was_step = false; // anchor i is the end of an untouched lattice stretch std::vector visible; // per candidate j, cached across the two passes: -1 unknown while (i < last) { std::size_t j_max = i + 1; while (j_max < last && cum[j_max + 1] - cum[i] <= cfg_.max_shortcut_m) ++j_max; visible.assign(j_max - i + 1, -1); const auto is_visible = [&](std::size_t j) { signed char& v = visible[j - i]; if (v < 0) v = segmentFree(pts[i], pts[j]) ? 1 : 0; return v == 1; }; const auto ok = [&](std::size_t j, bool need_align) { if (!is_visible(j)) return false; if (!need_align) return true; return aligned(i, j, j) && (!prev_was_step || aligned(i, j, i)); }; // Coarse scan downward for the first acceptable j, then refine upward inside the last stride. const auto scan = [&](bool need_align) -> std::size_t { for (std::size_t j = j_max; j > i + 1; j = (j > i + 1 + stride) ? j - stride : i + 1) { if (!ok(j, need_align)) continue; std::size_t best = j; const std::size_t top = std::min(j_max, j + stride - 1); for (std::size_t jj = top; jj > j; --jj) { if (ok(jj, need_align)) { best = jj; break; } } return best; } return 0; }; std::size_t hit = scan(true); if (hit == 0) hit = scan(false); if (hit == 0) hit = i + 1; // fallback: the original step prev_was_step = (hit == i + 1); anchors.push_back(hit); i = hit; } st.anchors = static_cast(anchors.size()); // 3. Fillets at interior anchors. Each corner may use at most half of either adjacent segment // so two fillets never overlap; the radius shrinks to fit, below min_fillet_radius_m the // corner stays sharp (duplicate position, two headings = turn in place for the tracker). struct Fillet { bool on = false; SmoothPose a, b; // tangent points on the incoming / outgoing segment double cx = 0.0, cy = 0.0, r = 0.0, a0 = 0.0, sweep = 0.0; }; std::vector fillets(anchors.size()); const double min_corner = cfg_.min_corner_deg * M_PI / 180.0; const double max_corner = kMaxFilletCornerDeg * M_PI / 180.0; for (std::size_t k = 1; k + 1 < anchors.size(); ++k) { const SmoothPose& p0 = pts[anchors[k - 1]]; const SmoothPose& p1 = pts[anchors[k]]; const SmoothPose& p2 = pts[anchors[k + 1]]; const double l_in = dist(p0, p1); const double l_out = dist(p1, p2); if (l_in < kEps || l_out < kEps || cfg_.fillet_radius_m <= 0.0) continue; const double d1x = (p1.x - p0.x) / l_in, d1y = (p1.y - p0.y) / l_in; const double d2x = (p2.x - p1.x) / l_out, d2y = (p2.y - p1.y) / l_out; const double cross = d1x * d2y - d1y * d2x; const double theta = std::acos(std::max(-1.0, std::min(1.0, d1x * d2x + d1y * d2y))); if (theta < min_corner || theta > max_corner) continue; // straight-ish or U-turn const double tan_half = std::tan(theta / 2.0); double r = cfg_.fillet_radius_m; double t = r * tan_half; const double t_max = 0.5 * std::min(l_in, l_out); if (t > t_max) { r = t_max / tan_half; t = t_max; } if (r < cfg_.min_fillet_radius_m) { ++st.fillets_rejected; continue; } const double side = cross >= 0.0 ? 1.0 : -1.0; Fillet f; f.a = {p1.x - d1x * t, p1.y - d1y * t, std::atan2(d1y, d1x)}; f.b = {p1.x + d2x * t, p1.y + d2y * t, std::atan2(d2y, d2x)}; f.cx = f.a.x - side * d1y * r; // left normal of d1 is (-d1y, d1x) f.cy = f.a.y + side * d1x * r; f.r = r; f.a0 = std::atan2(f.a.y - f.cy, f.a.x - f.cx); f.sweep = side * theta; if (!arcFree(f.cx, f.cy, f.r, f.a0, f.sweep)) { ++st.fillets_rejected; continue; } f.on = true; fillets[k] = f; ++st.fillets; } // 4. Assemble: segment -> (arc | sharp corner) -> segment ... -> goal with the input's final yaw. SmoothPose cur = pts[anchors[0]]; cur.theta = std::atan2(pts[anchors[1]].y - cur.y, pts[anchors[1]].x - cur.x); for (std::size_t k = 1; k < anchors.size(); ++k) { const SmoothPose& v = pts[anchors[k]]; if (k + 1 == anchors.size()) { appendSegment(cur, v, out); break; } if (fillets[k].on) { const Fillet& f = fillets[k]; appendSegment(cur, f.a, out); appendArc(f.cx, f.cy, f.r, f.a0, f.sweep, out); cur = f.b; continue; } const SmoothPose& next = pts[anchors[k + 1]]; const double h_in = std::atan2(v.y - cur.y, v.x - cur.x); const double h_out = std::atan2(next.y - v.y, next.x - v.x); appendSegment(cur, v, out); if (std::fabs(wrapAngle(h_out - h_in)) >= min_corner) { out.push_back({v.x, v.y, h_in}); // arrive, then turn in place (next piece starts with h_out) } cur = v; cur.theta = h_out; } SmoothPose goal = in.back(); out.push_back(goal); st.out_pts = out.size(); st.out_len_m = pathLength(out); if (stats) *stats = st; return true; } } // namespace sbpl_lattice_planner