// Unit tests for PathSmoother on synthetic worlds (no costmap, no SBPL). #include #include #include #include "sbpl_lattice_planner/distance_map.h" #include "sbpl_lattice_planner/path_smoother.h" using sbpl_lattice_planner::CellClass; using sbpl_lattice_planner::PathSmoother; using sbpl_lattice_planner::SmoothPose; using sbpl_lattice_planner::SmootherConfig; using sbpl_lattice_planner::SmootherStats; namespace { struct Rect { double x0, y0, x1, y1; bool contains(double x, double y, double margin) const { return x >= x0 - margin && x <= x1 + margin && y >= y0 - margin && y <= y1 + margin; } }; /// World of axis-aligned obstacles. Centre blocked within `inscribed` of a rectangle, footprint /// check required within `circumscribed`; footprint = 1.17 x 0.96 m box (T800) tested by corners. struct World { std::vector obstacles; double inscribed = 0.48; double circumscribed = 0.76; CellClass cell(double x, double y) const { CellClass c = CellClass::kFree; for (const Rect& r : obstacles) { if (r.contains(x, y, inscribed)) return CellClass::kBlocked; if (r.contains(x, y, circumscribed)) c = CellClass::kNearObstacle; } return c; } bool pose(double x, double y, double th) const { const double hl = 0.583, hw = 0.48; const double c = std::cos(th), s = std::sin(th); const double cx[4] = {hl, hl, -hl, -hl}; const double cy[4] = {-hw, hw, hw, -hw}; for (int i = 0; i < 4; ++i) { const double px = x + cx[i] * c - cy[i] * s; const double py = y + cx[i] * s + cy[i] * c; for (const Rect& r : obstacles) if (r.contains(px, py, 0.0)) return false; } return true; } PathSmoother smoother(SmootherConfig cfg = SmootherConfig()) const { cfg.enabled = true; return PathSmoother(cfg, [this](double x, double y) { return cell(x, y); }, [this](double x, double y, double th) { return pose(x, y, th); }); } /// Every output pose must pass the same rule the smoother promised to honour. void expectFree(const std::vector& p) const { for (const SmoothPose& q : p) { const CellClass c = cell(q.x, q.y); EXPECT_NE(c, CellClass::kBlocked) << "blocked at (" << q.x << "," << q.y << ")"; if (c == CellClass::kNearObstacle) { EXPECT_TRUE(pose(q.x, q.y, q.theta)) << "footprint at (" << q.x << "," << q.y << ")"; } } } }; /// Dense polyline through `v` at 5 cm, heading = segment direction (what the lattice gives). std::vector dense(const std::vector>& v, double step = 0.05) { std::vector out; for (std::size_t i = 1; i < v.size(); ++i) { const double dx = v[i].first - v[i - 1].first, dy = v[i].second - v[i - 1].second; const double len = std::hypot(dx, dy), th = std::atan2(dy, dx); const int n = std::max(1, static_cast(std::ceil(len / step))); for (int s = 0; s < n; ++s) out.push_back({v[i - 1].first + dx * s / n, v[i - 1].second + dy * s / n, th}); } const auto& e = v.back(); out.push_back({e.first, e.second, out.empty() ? 0.0 : out.back().theta}); return out; } double maxStep(const std::vector& p) { double m = 0.0; for (std::size_t i = 1; i < p.size(); ++i) m = std::max(m, std::hypot(p[i].x - p[i - 1].x, p[i].y - p[i - 1].y)); return m; } double maxHeadingJump(const std::vector& p) { double m = 0.0; for (std::size_t i = 1; i < p.size(); ++i) m = std::max(m, std::fabs(std::remainder(p[i].theta - p[i - 1].theta, 2 * M_PI))); return m; } } // namespace TEST(PathSmoother, ZigzagInOpenSpaceBecomesOneSegment) { World w; // nothing in it std::vector> v; double x = 0, y = 0; v.push_back({x, y}); for (int k = 0; k < 20; ++k) { // 0 / 22.5 deg alternating, 0.5 m legs — the lattice staircase const double th = k % 2 ? 22.5 * M_PI / 180.0 : 0.0; x += 0.5 * std::cos(th); y += 0.5 * std::sin(th); v.push_back({x, y}); } auto in = dense(v); in.back().theta = std::atan2(y, x); // goal yaw along the chord, so one heading end to end std::vector out; SmootherStats st; ASSERT_TRUE(w.smoother().smooth(in, out, &st)); EXPECT_EQ(st.anchors, 2); EXPECT_EQ(st.fillets, 0); EXPECT_NEAR(st.out_len_m, std::hypot(x, y), 1e-6); EXPECT_LT(st.out_len_m, st.in_len_m); EXPECT_LE(maxStep(out), 0.05 + 1e-9); EXPECT_LT(maxHeadingJump(out), 1e-9); // one heading all the way EXPECT_NEAR(out.front().x, 0.0, 1e-9); EXPECT_NEAR(out.back().x, x, 1e-9); EXPECT_NEAR(out.back().y, y, 1e-9); w.expectFree(out); } TEST(PathSmoother, WallForcesCornerWithTangentFillet) { World w; w.obstacles.push_back({2.0, 1.0, 8.0, 9.0}); const auto in = dense({{0, 0}, {10, 0}, {10, 10}}); std::vector out; SmootherStats st; ASSERT_TRUE(w.smoother().smooth(in, out, &st)); EXPECT_EQ(st.anchors, 3); EXPECT_EQ(st.fillets, 1); EXPECT_EQ(st.fillets_rejected, 0); EXPECT_LT(st.out_len_m, st.in_len_m); // Tangent-continuous: no heading jump larger than one arc sample (step / R). EXPECT_LT(maxHeadingJump(out), 0.05 / 1.06 + 1e-6); EXPECT_LE(maxStep(out), 0.05 + 1e-6); EXPECT_NEAR(out.back().x, 10.0, 1e-9); EXPECT_NEAR(out.back().y, 10.0, 1e-9); w.expectFree(out); } TEST(PathSmoother, FilletThatCollidesFallsBackToSharpCorner) { World w; w.obstacles.push_back({2.0, 1.0, 8.0, 9.0}); w.obstacles.push_back({9.0, 0.7, 9.4, 1.2}); // post inside the corner the arc would cut const auto in = dense({{0, 0}, {10, 0}, {10, 10}}); std::vector out; SmootherStats st; ASSERT_TRUE(w.smoother().smooth(in, out, &st)); EXPECT_EQ(st.fillets, 0); EXPECT_GE(st.fillets_rejected, 1); // Sharp corner = duplicate position carrying two headings (turn in place for the tracker). bool dup = false; for (std::size_t i = 1; i < out.size(); ++i) if (std::hypot(out[i].x - out[i - 1].x, out[i].y - out[i - 1].y) < 1e-9 && std::fabs(std::remainder(out[i].theta - out[i - 1].theta, 2 * M_PI)) > 0.5) dup = true; EXPECT_TRUE(dup); w.expectFree(out); } TEST(PathSmoother, ReverseMotionLeavesPathUntouched) { World w; auto in = dense({{0, 0}, {5, 0}}); for (SmoothPose& p : in) p.theta = M_PI; // heading opposite to travel: driving backwards std::vector out; EXPECT_FALSE(w.smoother().smooth(in, out)); EXPECT_TRUE(out.empty()); } TEST(PathSmoother, EndpointsAndGoalYawPreserved) { World w; auto in = dense({{0, 0}, {3, 4}, {6, 0}}); in.back().theta = 1.234; // goal yaw std::vector out; ASSERT_TRUE(w.smoother().smooth(in, out)); EXPECT_NEAR(out.front().x, 0.0, 1e-9); EXPECT_NEAR(out.front().y, 0.0, 1e-9); EXPECT_NEAR(out.front().theta, 0.0, 1e-9); // straight to (6,0): open space, one segment EXPECT_NEAR(out.back().x, 6.0, 1e-9); EXPECT_NEAR(out.back().y, 0.0, 1e-9); EXPECT_NEAR(out.back().theta, 1.234, 1e-9); } TEST(PathSmoother, FootprintCheckBlocksShortcutTheCentreWouldAllow) { // Narrow slot: centre fits (kNearObstacle) but the 0.96 m wide footprint does not when driving // along y = 0 between two posts 0.7 m apart. The detour above them must survive. World w; w.inscribed = 0.3; // posts 0.7 m apart leave the centre line unblocked w.circumscribed = 0.8; w.obstacles.push_back({4.0, -2.0, 6.0, -0.35}); w.obstacles.push_back({4.0, 0.35, 6.0, 2.0}); const auto in = dense({{0, 0}, {3, 0}, {3, 3.5}, {7, 3.5}, {7, 0}, {10, 0}}); std::vector out; SmootherStats st; ASSERT_TRUE(w.smoother().smooth(in, out, &st)); EXPECT_GT(st.out_len_m, 10.0 + 1.0); // did not cut straight through the slot EXPECT_LT(st.out_len_m, st.in_len_m); // but did shortcut the rectangular detour w.expectFree(out); } TEST(PathSmoother, JunctionPrefersLatticeAlignedEndPoint) { // East leg (0,0)->(10,0), north leg (10,0)->(10,5) between a wall at x >= 11.2 and a block at // x <= 9.3 above y = 2.5. From (0,0) the farthest visible point is (10, ~2.4) on the north leg, // but that chord meets the north heading at ~77 deg. With the junction rule the smoother stops // at (10,0) (chord along the east heading, then a fillet); with the rule off it takes the // farthest visible point and the first output heading is the 13 deg chord. World w; w.obstacles.push_back({11.2, -1.0, 12.5, 3.0}); w.obstacles.push_back({8.0, 2.5, 9.3, 6.0}); const auto in = dense({{0, 0}, {10, 0}, {10, 5}}); SmootherConfig cfg; cfg.max_junction_deg = 20.0; std::vector out; SmootherStats st; ASSERT_TRUE(w.smoother(cfg).smooth(in, out, &st)); EXPECT_EQ(st.anchors, 3); EXPECT_EQ(st.fillets, 1); EXPECT_NEAR(out.front().theta, 0.0, 1e-6); w.expectFree(out); cfg.max_junction_deg = 0.0; ASSERT_TRUE(w.smoother(cfg).smooth(in, out, &st)); EXPECT_GT(out.front().theta, 0.15); // chord straight to the north leg w.expectFree(out); } TEST(DistanceMap, SquaredEuclideanToNearestObstacle) { // 7x5 grid, obstacles at (1,1) and (5,3): every cell must report the true squared distance. const int w = 7, h = 5; std::vector obs(w * h, 0); obs[1 * w + 1] = 1; obs[3 * w + 5] = 1; std::vector d2; sbpl_lattice_planner::squaredDistanceTransform(obs, w, h, d2); ASSERT_EQ(d2.size(), static_cast(w * h)); for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) { const float a = static_cast((x - 1) * (x - 1) + (y - 1) * (y - 1)); const float b = static_cast((x - 5) * (x - 5) + (y - 3) * (y - 3)); EXPECT_FLOAT_EQ(d2[y * w + x], std::min(a, b)) << "at (" << x << "," << y << ")"; } // No obstacle at all: everything far away. std::fill(obs.begin(), obs.end(), 0); sbpl_lattice_planner::squaredDistanceTransform(obs, w, h, d2); for (float v : d2) EXPECT_GE(v, 1.0e6f); } int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }