299 lines
10 KiB
C++
299 lines
10 KiB
C++
// route_graph — unit tests on a hand-built graph (no map files), plus the hash/rule/bundle paths.
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <unistd.h>
|
|
|
|
#include <route_graph/bundle_json.h>
|
|
#include <route_graph/corridor.h>
|
|
#include <route_graph/graph.h>
|
|
#include <route_graph/map_hash.h>
|
|
|
|
namespace
|
|
{
|
|
|
|
using route_graph::Cell;
|
|
using route_graph::Graph;
|
|
|
|
// Straight cell run from a to b (axis-aligned), both ends included.
|
|
void appendRun(robot_sensor_msgs::RouteGraph& m, Cell a, Cell b)
|
|
{
|
|
const int dx = (b.x > a.x) - (b.x < a.x), dy = (b.y > a.y) - (b.y < a.y);
|
|
Cell c = a;
|
|
while (true)
|
|
{
|
|
m.cell_x.push_back(c.x);
|
|
m.cell_y.push_back(c.y);
|
|
if (c == b) break;
|
|
c.x += dx;
|
|
c.y += dy;
|
|
}
|
|
}
|
|
|
|
void addEdge(robot_sensor_msgs::RouteGraph& m, int u, int v, float clearance)
|
|
{
|
|
const Cell a(m.node_x[u], m.node_y[u]), b(m.node_x[v], m.node_y[v]);
|
|
const std::size_t before = m.cell_x.size();
|
|
appendRun(m, a, b);
|
|
m.edge_u.push_back(u);
|
|
m.edge_v.push_back(v);
|
|
m.edge_length_cells.push_back(static_cast<float>(m.cell_x.size() - before - 1));
|
|
m.edge_min_clearance_cells.push_back(clearance);
|
|
m.edge_cell_offset.push_back(static_cast<uint32_t>(m.cell_x.size()));
|
|
}
|
|
|
|
// A(10,10) -- B(50,10) -- C(50,40), plus an isolated D(80,80) -- E(90,80). 100 x 100 grid.
|
|
robot_sensor_msgs::RouteGraph fixture()
|
|
{
|
|
robot_sensor_msgs::RouteGraph m;
|
|
m.version = route_graph::kSupportedVersion;
|
|
m.width = m.height = 100;
|
|
m.resolution = 0.05;
|
|
m.node_x = {10, 50, 50, 80, 90};
|
|
m.node_y = {10, 10, 40, 80, 80};
|
|
m.edge_cell_offset.push_back(0);
|
|
addEdge(m, 0, 1, 8.0f); // A-B, 40 cells
|
|
addEdge(m, 1, 2, 4.0f); // B-C, 30 cells
|
|
addEdge(m, 3, 4, 6.0f); // D-E, 10 cells
|
|
return m;
|
|
}
|
|
|
|
const auto kAllPassable = [](int, int) { return true; };
|
|
|
|
} // namespace
|
|
|
|
TEST(Graph, FromMsgValidates)
|
|
{
|
|
Graph g;
|
|
std::string err;
|
|
auto m = fixture();
|
|
ASSERT_TRUE(g.fromMsg(m, &err)) << err;
|
|
EXPECT_EQ(g.nodes().size(), 5u);
|
|
EXPECT_EQ(g.edges().size(), 3u);
|
|
EXPECT_EQ(g.componentCount(), 2);
|
|
|
|
m.version = 99;
|
|
EXPECT_FALSE(g.fromMsg(m, &err));
|
|
EXPECT_TRUE(g.empty());
|
|
|
|
m = fixture();
|
|
m.edge_cell_offset.back() += 1; // does not span the cell arrays
|
|
EXPECT_FALSE(g.fromMsg(m, &err));
|
|
|
|
m = fixture();
|
|
m.cell_x[0] = 11; // edge 0 no longer starts at node u's cell
|
|
EXPECT_FALSE(g.fromMsg(m, &err));
|
|
EXPECT_NE(err.find("does not start at u"), std::string::npos);
|
|
}
|
|
|
|
TEST(Graph, AttachFindsNearestGraphCellAndPath)
|
|
{
|
|
Graph g;
|
|
ASSERT_TRUE(g.fromMsg(fixture(), nullptr));
|
|
const auto a = g.attach(Cell(12, 14), 50, kAllPassable);
|
|
ASSERT_TRUE(a.ok);
|
|
EXPECT_EQ(a.edge, 0);
|
|
EXPECT_EQ(a.index, 2u); // (12,10) is the third cell of A-B
|
|
ASSERT_GE(a.path.size(), 2u);
|
|
EXPECT_EQ(a.path.front(), Cell(12, 14));
|
|
EXPECT_EQ(a.path.back(), Cell(12, 10));
|
|
|
|
// Walls everywhere except the query cell: nothing reachable.
|
|
const auto blocked = g.attach(Cell(12, 14), 50, [](int, int) { return false; });
|
|
EXPECT_FALSE(blocked.ok);
|
|
// Radius too small to reach row 10 from row 30.
|
|
EXPECT_FALSE(g.attach(Cell(12, 30), 5, kAllPassable).ok);
|
|
}
|
|
|
|
TEST(Graph, RouteGoesThroughJunctionAndCarriesClearance)
|
|
{
|
|
Graph g;
|
|
ASSERT_TRUE(g.fromMsg(fixture(), nullptr));
|
|
const auto s = g.attach(Cell(12, 12), 50, kAllPassable); // near A
|
|
const auto t = g.attach(Cell(52, 38), 50, kAllPassable); // near C
|
|
ASSERT_TRUE(s.ok && t.ok);
|
|
const auto r = g.route(s, t);
|
|
ASSERT_FALSE(r.empty());
|
|
EXPECT_EQ(r.cells.front(), Cell(12, 12));
|
|
EXPECT_EQ(r.cells.back(), Cell(52, 38));
|
|
EXPECT_EQ(r.cells.size(), r.clearance_cells.size());
|
|
// Passes through B.
|
|
bool via_b = false;
|
|
for (const auto& c : r.cells) via_b |= (c == Cell(50, 10));
|
|
EXPECT_TRUE(via_b);
|
|
// 38 along A-B + 28 along B-C + 2 + 2 attach steps.
|
|
EXPECT_NEAR(r.length_cells, 70.0f, 1.0f);
|
|
// Clearance follows the owning edge: 8 on A-B cells, 4 on B-C cells.
|
|
EXPECT_FLOAT_EQ(r.clearance_cells[5], 8.0f);
|
|
EXPECT_FLOAT_EQ(r.clearance_cells[r.clearance_cells.size() - 5], 4.0f);
|
|
}
|
|
|
|
TEST(Graph, RouteOnSameEdgeIsTheDirectSlice)
|
|
{
|
|
Graph g;
|
|
ASSERT_TRUE(g.fromMsg(fixture(), nullptr));
|
|
const auto s = g.attach(Cell(20, 10), 5, kAllPassable);
|
|
const auto t = g.attach(Cell(40, 10), 5, kAllPassable);
|
|
ASSERT_TRUE(s.ok && t.ok);
|
|
const auto r = g.route(s, t);
|
|
ASSERT_EQ(r.cells.size(), 21u);
|
|
EXPECT_NEAR(r.length_cells, 20.0f, 0.01f);
|
|
// Reverse direction walks the edge backwards.
|
|
const auto rr = g.route(t, s);
|
|
ASSERT_EQ(rr.cells.size(), 21u);
|
|
EXPECT_EQ(rr.cells.front(), Cell(40, 10));
|
|
EXPECT_EQ(rr.cells.back(), Cell(20, 10));
|
|
}
|
|
|
|
TEST(Graph, NarrowEdgesAreSkippedForTheCallersClearance)
|
|
{
|
|
Graph g;
|
|
ASSERT_TRUE(g.fromMsg(fixture(), nullptr));
|
|
// B-C has clearance 4: with a robot needing 5 cells it is not a route, and (50, 40) near C
|
|
// cannot even attach to it — the BFS walks on to the nearest wide edge (A-B at (50, 10)).
|
|
const auto s = g.attach(Cell(10, 10), 5, kAllPassable, 5.0f);
|
|
const auto t = g.attach(Cell(50, 40), 50, kAllPassable, 5.0f);
|
|
ASSERT_TRUE(s.ok && t.ok);
|
|
EXPECT_EQ(t.edge, 0);
|
|
EXPECT_EQ(t.path.back(), Cell(50, 10));
|
|
const auto r = g.route(s, t, 5.0f);
|
|
ASSERT_FALSE(r.empty());
|
|
for (const auto& c : r.cells) EXPECT_LE(c.y, 40);
|
|
// Same attachments, but a route() asking for more clearance than A-B has -> nothing.
|
|
EXPECT_TRUE(g.route(s, t, 9.0f).empty());
|
|
}
|
|
|
|
TEST(Graph, NoRouteAcrossComponents)
|
|
{
|
|
Graph g;
|
|
ASSERT_TRUE(g.fromMsg(fixture(), nullptr));
|
|
const auto s = g.attach(Cell(10, 10), 5, kAllPassable);
|
|
const auto t = g.attach(Cell(85, 80), 5, kAllPassable);
|
|
ASSERT_TRUE(s.ok && t.ok);
|
|
EXPECT_TRUE(g.route(s, t).empty());
|
|
}
|
|
|
|
TEST(Corridor, MaskCoversRouteAndScalesWithClearance)
|
|
{
|
|
Graph g;
|
|
ASSERT_TRUE(g.fromMsg(fixture(), nullptr));
|
|
const auto s = g.attach(Cell(10, 10), 5, kAllPassable);
|
|
const auto t = g.attach(Cell(50, 40), 5, kAllPassable);
|
|
const auto r = g.route(s, t);
|
|
ASSERT_FALSE(r.empty());
|
|
route_graph::CorridorParams p;
|
|
p.half_width_min_cells = 3;
|
|
p.clearance_scale = 1.0;
|
|
p.half_width_max_cells = 20;
|
|
std::size_t n = 0;
|
|
const auto mask = route_graph::corridorMask(r, 100, 100, p, &n);
|
|
ASSERT_EQ(mask.size(), 10000u);
|
|
EXPECT_EQ(n, static_cast<std::size_t>(std::count(mask.begin(), mask.end(), 1)));
|
|
for (const auto& c : r.cells) EXPECT_EQ(mask[c.y * 100 + c.x], 1) << c.x << "," << c.y;
|
|
// A-B has clearance 8 -> radius 8: (30, 17) inside, (30, 19) outside. B-C clearance 4: (47, 25)
|
|
// inside, (44, 25) outside. Discs are stamped every r/2 cells, so the union scallops slightly
|
|
// inside r between stamps — probe one cell in from the nominal boundary.
|
|
EXPECT_EQ(mask[17 * 100 + 30], 1);
|
|
EXPECT_EQ(mask[19 * 100 + 30], 0);
|
|
EXPECT_EQ(mask[25 * 100 + 47], 1);
|
|
EXPECT_EQ(mask[25 * 100 + 44], 0);
|
|
// Far corner untouched.
|
|
EXPECT_EQ(mask[90 * 100 + 90], 0);
|
|
}
|
|
|
|
TEST(MapHash, RuleRoundTripAndPrefix)
|
|
{
|
|
route_graph::ObstacleRule r;
|
|
ASSERT_TRUE(route_graph::parseSourceRule(
|
|
"costmap:lethal>=100;unknown_value=-1;track_unknown=1;trinary=1;unknown=traversable;roi=10", &r));
|
|
EXPECT_EQ(r.lethal_cost_threshold, 100);
|
|
EXPECT_FALSE(r.unknown_is_obstacle);
|
|
EXPECT_EQ(r.roi_margin_cells, 10);
|
|
ASSERT_TRUE(route_graph::parseSourceRule(
|
|
"lethal>=50;unknown_value=-1;track_unknown=0;trinary=0;unknown=obstacle;roi=-1", &r));
|
|
EXPECT_TRUE(r.unknown_is_obstacle);
|
|
EXPECT_FALSE(r.track_unknown_space);
|
|
EXPECT_EQ(r.roi_margin_cells, -1);
|
|
EXPECT_FALSE(route_graph::parseSourceRule("map_server:occupied", &r));
|
|
}
|
|
|
|
TEST(MapHash, CostmapMaskIsImageOrderedAndRoiApplied)
|
|
{
|
|
// 4 x 3 costmap, row 0 = bottom. Lethal at (1, 0) (bottom row) and unknown at (3, 2) (top row).
|
|
unsigned char cm[12] = {0};
|
|
cm[0 * 4 + 1] = 254;
|
|
cm[2 * 4 + 3] = 255;
|
|
route_graph::ObstacleRule r; // unknown traversable, no ROI
|
|
auto m = route_graph::obstacleMaskFromCostmap(cm, 4, 3, r);
|
|
ASSERT_EQ(m.size(), 12u);
|
|
EXPECT_EQ(m[2 * 4 + 1], 1); // image row 2 = costmap row 0
|
|
EXPECT_EQ(m[0 * 4 + 3], 0); // unknown not an obstacle
|
|
r.unknown_is_obstacle = true;
|
|
m = route_graph::obstacleMaskFromCostmap(cm, 4, 3, r);
|
|
EXPECT_EQ(m[0 * 4 + 3], 1);
|
|
|
|
// ROI: 10 x 10 mask with a 3 x 3 block in the middle, margin 1 -> everything outside 5 x 5 box is obstacle.
|
|
std::vector<uint8_t> mask(100, 0);
|
|
for (int y = 4; y <= 6; ++y)
|
|
for (int x = 4; x <= 6; ++x) mask[y * 10 + x] = 1;
|
|
const auto turned = route_graph::applyObstacleRoi(mask, 10, 10, 1, /*min_obstacle_cells=*/1);
|
|
EXPECT_EQ(turned, 100u - 25u);
|
|
EXPECT_EQ(mask[3 * 10 + 3], 0);
|
|
EXPECT_EQ(mask[2 * 10 + 3], 1);
|
|
}
|
|
|
|
TEST(MapHash, HashIsStableAndHexRoundTrips)
|
|
{
|
|
std::vector<uint8_t> a(37, 0), b(37, 0);
|
|
a[5] = 1;
|
|
EXPECT_EQ(route_graph::hashObstacleMask(a), route_graph::hashObstacleMask(a));
|
|
EXPECT_NE(route_graph::hashObstacleMask(a), route_graph::hashObstacleMask(b));
|
|
b.push_back(0); // size is part of the hash
|
|
EXPECT_NE(route_graph::hashObstacleMask(std::vector<uint8_t>(37, 0)), route_graph::hashObstacleMask(b));
|
|
const uint64_t h = route_graph::hashObstacleMask(a);
|
|
uint64_t back = 0;
|
|
ASSERT_TRUE(route_graph::hexToHash(route_graph::hashToHex(h), &back));
|
|
EXPECT_EQ(back, h);
|
|
EXPECT_FALSE(route_graph::hexToHash("xyz", &back));
|
|
}
|
|
|
|
TEST(BundleJson, LoadsAndFlipsRows)
|
|
{
|
|
// Image order: node (2, 0) is the TOP row of a 5-row map -> costmap row 4.
|
|
const std::string path = "/tmp/route_graph_test_" + std::to_string(::getpid()) + ".json";
|
|
{
|
|
std::ofstream f(path);
|
|
f << R"({"version":1,"source":"costmap:lethal>=100;unknown_value=-1;track_unknown=1;trinary=1;unknown=traversable;roi=-1",
|
|
"map_hash":"00000000000000ff",
|
|
"info":{"width":6,"height":5,"resolution":0.1,"origin":[-1.0,-2.0,0.0]},
|
|
"nodes":[[2,0],[2,3]],
|
|
"edges":[{"u":0,"v":1,"length_cells":3.0,"min_clearance_cells":2.5,"cells":[[2,0],[2,1],[2,2],[2,3]]}]})";
|
|
}
|
|
robot_sensor_msgs::RouteGraph msg;
|
|
std::string err;
|
|
ASSERT_TRUE(route_graph::loadBundleJson(path, &msg, &err)) << err;
|
|
std::remove(path.c_str());
|
|
EXPECT_EQ(msg.width, 6u);
|
|
EXPECT_EQ(msg.height, 5u);
|
|
EXPECT_DOUBLE_EQ(msg.origin_x, -1.0);
|
|
EXPECT_EQ(msg.map_hash, 255u);
|
|
ASSERT_EQ(msg.node_y.size(), 2u);
|
|
EXPECT_EQ(msg.node_y[0], 4);
|
|
EXPECT_EQ(msg.node_y[1], 1);
|
|
ASSERT_EQ(msg.cell_y.size(), 4u);
|
|
EXPECT_EQ(msg.cell_y[0], 4);
|
|
EXPECT_EQ(msg.cell_y[3], 1);
|
|
Graph g;
|
|
EXPECT_TRUE(g.fromMsg(msg, &err)) << err;
|
|
|
|
EXPECT_FALSE(route_graph::loadBundleJson("/nonexistent/voronoi.json", &msg, &err));
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|