388 lines
14 KiB
C++
388 lines
14 KiB
C++
// Unit tests on synthetic grids. Coordinates are image-style: x = column, y = row.
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cmath>
|
|
#include <vector>
|
|
|
|
#include "voronoi_route/voronoi_route.h"
|
|
|
|
namespace {
|
|
|
|
using voronoi_route::Cell;
|
|
using voronoi_route::VoronoiRoute;
|
|
|
|
struct Grid {
|
|
int w, h;
|
|
std::vector<uint8_t> occ;
|
|
Grid(int w_, int h_) : w(w_), h(h_), occ(static_cast<size_t>(w_) * h_, 0) {}
|
|
void set(int x, int y, uint8_t v = 1) { occ[static_cast<size_t>(y) * w + x] = v; }
|
|
void rect(int x0, int y0, int x1, int y1, uint8_t v = 1)
|
|
{
|
|
for (int y = y0; y <= y1; ++y)
|
|
for (int x = x0; x <= x1; ++x) set(x, y, v);
|
|
}
|
|
void frame() { rect(0, 0, w - 1, 0); rect(0, h - 1, w - 1, h - 1); rect(0, 0, 0, h - 1); rect(w - 1, 0, w - 1, h - 1); }
|
|
};
|
|
|
|
// Straight corridor: walls at rows [0..4] and [h-5..h-1], free in between, closed ends.
|
|
Grid corridor(int w = 200, int h = 40)
|
|
{
|
|
Grid g(w, h);
|
|
g.rect(0, 0, w - 1, 4);
|
|
g.rect(0, h - 5, w - 1, h - 1);
|
|
g.rect(0, 0, 4, h - 1);
|
|
g.rect(w - 5, 0, w - 1, h - 1);
|
|
return g;
|
|
}
|
|
|
|
// T: horizontal corridor plus a vertical stem going down from the middle.
|
|
Grid tee()
|
|
{
|
|
Grid g(200, 120);
|
|
g.rect(0, 0, 199, 119); // all wall
|
|
g.rect(5, 40, 194, 79, 0); // horizontal free band
|
|
g.rect(85, 79, 114, 114, 0); // vertical stem
|
|
return g;
|
|
}
|
|
|
|
// Ring: free band around a solid block.
|
|
Grid ring()
|
|
{
|
|
Grid g(160, 160);
|
|
g.rect(0, 0, 159, 159);
|
|
g.rect(5, 5, 154, 154, 0);
|
|
g.rect(45, 45, 114, 114); // block
|
|
return g;
|
|
}
|
|
|
|
int degreeOf(const VoronoiRoute& vr, int node)
|
|
{
|
|
return static_cast<int>(vr.graph().nodes()[node].edges.size());
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(VoronoiRoute, CorridorIsOneEdgeBetweenTwoDeadEnds)
|
|
{
|
|
Grid g = corridor();
|
|
VoronoiRoute vr;
|
|
vr.rebuild(g.occ, g.w, g.h);
|
|
ASSERT_TRUE(vr.ready());
|
|
const auto& st = vr.stats();
|
|
EXPECT_EQ(st.components, 1);
|
|
EXPECT_GE(st.edges, 1);
|
|
// Longest edge runs most of the corridor.
|
|
float longest = 0;
|
|
for (const auto& e : vr.graph().edges()) longest = std::max(longest, e.length_cells);
|
|
EXPECT_GT(longest, 150.0f);
|
|
// Diagram sits on the centre row.
|
|
int on_centre = 0, total = 0;
|
|
for (int x = 20; x < g.w - 20; ++x)
|
|
for (int y = 5; y < g.h - 5; ++y)
|
|
if (vr.field().isVoronoi(x, y))
|
|
{
|
|
++total;
|
|
if (std::abs(y - (g.h - 1) / 2) <= 1) ++on_centre;
|
|
}
|
|
EXPECT_GT(total, 0);
|
|
EXPECT_GT(on_centre, total * 0.8);
|
|
}
|
|
|
|
TEST(VoronoiRoute, TeeHasAJunction)
|
|
{
|
|
Grid g = tee();
|
|
VoronoiRoute::Config cfg;
|
|
cfg.min_edge_length_cells = 8;
|
|
VoronoiRoute vr(cfg);
|
|
vr.rebuild(g.occ, g.w, g.h);
|
|
ASSERT_TRUE(vr.ready());
|
|
int junctions = 0, leaves = 0;
|
|
for (size_t i = 0; i < vr.graph().nodes().size(); ++i)
|
|
{
|
|
const int d = degreeOf(vr, static_cast<int>(i));
|
|
if (d >= 3) ++junctions;
|
|
if (d == 1) ++leaves;
|
|
}
|
|
EXPECT_GE(junctions, 1);
|
|
EXPECT_GE(leaves, 3);
|
|
EXPECT_EQ(vr.stats().components, 1);
|
|
}
|
|
|
|
TEST(VoronoiRoute, RingIsConnectedAndRoutesTheShortWay)
|
|
{
|
|
Grid g = ring();
|
|
VoronoiRoute vr;
|
|
vr.rebuild(g.occ, g.w, g.h);
|
|
ASSERT_TRUE(vr.ready());
|
|
EXPECT_EQ(vr.stats().components, 1);
|
|
// Two points on the top band, close to each other: route must not go around the block.
|
|
const auto a = vr.attach(Cell(60, 25));
|
|
const auto b = vr.attach(Cell(100, 25));
|
|
ASSERT_TRUE(a.ok);
|
|
ASSERT_TRUE(b.ok);
|
|
const auto route = vr.query(a, b);
|
|
ASSERT_FALSE(route.empty());
|
|
EXPECT_EQ(route.front(), Cell(60, 25));
|
|
EXPECT_EQ(route.back(), Cell(100, 25));
|
|
EXPECT_LT(route.size(), 120u);
|
|
}
|
|
|
|
TEST(VoronoiRoute, ShortSpurIsPrunedLongOneKept)
|
|
{
|
|
Grid g = corridor(200, 60);
|
|
// Long dead-end pocket off the corridor (kept), and a tiny notch (pruned).
|
|
g.rect(60, 55, 80, 59, 0); // notch 5 deep — spur way below 8 cells? it barely forms one
|
|
g.rect(140, 40, 150, 59, 0); // pocket 20 deep
|
|
VoronoiRoute::Config cfg;
|
|
cfg.min_edge_length_cells = 8;
|
|
VoronoiRoute vr(cfg);
|
|
vr.rebuild(g.occ, g.w, g.h);
|
|
ASSERT_TRUE(vr.ready());
|
|
for (const auto& e : vr.graph().edges())
|
|
{
|
|
const bool leaf = degreeOf(vr, e.u) == 1 || degreeOf(vr, e.v) == 1;
|
|
if (leaf && e.u != e.v)
|
|
{
|
|
EXPECT_GE(e.length_cells, 8.0f) << "spur survived pruning";
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST(VoronoiRoute, AttachFailsInsideSealedRoom)
|
|
{
|
|
Grid g = corridor();
|
|
g.rect(100, 10, 120, 30); // solid block
|
|
g.rect(105, 15, 115, 25, 0); // hollow inside, sealed
|
|
VoronoiRoute::Config cfg;
|
|
cfg.attach_max_radius_cells = 50;
|
|
VoronoiRoute vr(cfg);
|
|
vr.rebuild(g.occ, g.w, g.h);
|
|
const auto a = vr.attach(Cell(110, 20));
|
|
// Either no GVD inside the sealed pocket (attach fails) or a tiny isolated diagram there;
|
|
// what must never happen is a path that crosses the wall.
|
|
if (a.ok)
|
|
{
|
|
for (const auto& c : a.path) EXPECT_FALSE(vr.field().isOccupied(c.x, c.y));
|
|
}
|
|
}
|
|
|
|
TEST(VoronoiRoute, CorridorMaskCoversRouteAndRespectsWidth)
|
|
{
|
|
Grid g = corridor(300, 60);
|
|
VoronoiRoute vr;
|
|
vr.rebuild(g.occ, g.w, g.h);
|
|
const auto a = vr.attach(Cell(20, 30));
|
|
const auto b = vr.attach(Cell(280, 30));
|
|
ASSERT_TRUE(a.ok && b.ok);
|
|
const auto route = vr.query(a, b);
|
|
ASSERT_FALSE(route.empty());
|
|
voronoi_route::CorridorParams p;
|
|
p.half_width_min_cells = 6;
|
|
p.clearance_scale = 0.0;
|
|
p.half_width_max_cells = 6;
|
|
size_t n = 0;
|
|
const auto mask = vr.corridorMask(route, p, &n);
|
|
EXPECT_GT(n, 0u);
|
|
for (const auto& c : route) EXPECT_EQ(mask[static_cast<size_t>(c.y) * g.w + c.x], 1);
|
|
EXPECT_EQ(mask[static_cast<size_t>(8) * g.w + 150], 0); // 22 rows from centre: outside
|
|
EXPECT_EQ(mask[static_cast<size_t>(30) * g.w + 150], 1); // on the line
|
|
}
|
|
|
|
TEST(VoronoiRoute, SyncOccupancySplitsAndRejoins)
|
|
{
|
|
Grid g = corridor(200, 40);
|
|
VoronoiRoute::Config cfg;
|
|
cfg.graph_rebuild_min_changed_cells = 1;
|
|
VoronoiRoute vr(cfg);
|
|
vr.rebuild(g.occ, g.w, g.h);
|
|
const uint64_t h0 = vr.occupancyHash();
|
|
EXPECT_EQ(vr.stats().components, 1);
|
|
|
|
Grid blocked = g;
|
|
blocked.rect(100, 5, 101, 34); // wall across the corridor
|
|
const size_t changed = vr.syncOccupancy(blocked.occ, g.w, g.h);
|
|
EXPECT_EQ(changed, 60u);
|
|
EXPECT_NE(vr.occupancyHash(), h0);
|
|
EXPECT_GE(vr.stats().components, 2);
|
|
const auto a = vr.attach(Cell(30, 20));
|
|
const auto b = vr.attach(Cell(170, 20));
|
|
ASSERT_TRUE(a.ok && b.ok);
|
|
EXPECT_TRUE(vr.query(a, b).empty());
|
|
|
|
vr.syncOccupancy(g.occ, g.w, g.h);
|
|
EXPECT_EQ(vr.occupancyHash(), h0);
|
|
EXPECT_EQ(vr.stats().components, 1);
|
|
const auto a2 = vr.attach(Cell(30, 20));
|
|
const auto b2 = vr.attach(Cell(170, 20));
|
|
EXPECT_FALSE(vr.query(a2, b2).empty());
|
|
}
|
|
|
|
TEST(VoronoiRoute, SyncWithWholeDifferentMapMatchesFreshRebuild)
|
|
{
|
|
Grid a = tee();
|
|
Grid b = ring();
|
|
ASSERT_EQ(a.w, 200);
|
|
// Same size grids: pad ring into 200x120 by cropping rows.
|
|
Grid b2(200, 120);
|
|
b2.rect(0, 0, 199, 119);
|
|
b2.rect(5, 5, 194, 114, 0);
|
|
b2.rect(60, 40, 139, 79);
|
|
VoronoiRoute vr;
|
|
vr.rebuild(a.occ, a.w, a.h);
|
|
vr.syncOccupancy(b2.occ, b2.w, b2.h);
|
|
EXPECT_TRUE(vr.stats().last_sync_full_rebuild);
|
|
VoronoiRoute fresh;
|
|
fresh.rebuild(b2.occ, b2.w, b2.h);
|
|
EXPECT_EQ(vr.occupancyHash(), fresh.occupancyHash());
|
|
EXPECT_EQ(vr.graph().nodes().size(), fresh.graph().nodes().size());
|
|
EXPECT_EQ(vr.graph().edges().size(), fresh.graph().edges().size());
|
|
for (size_t i = 0; i < vr.graph().edges().size(); ++i)
|
|
EXPECT_EQ(vr.graph().edges()[i].cells, fresh.graph().edges()[i].cells);
|
|
}
|
|
|
|
TEST(VoronoiRoute, ExtractionIsDeterministic)
|
|
{
|
|
Grid g = tee();
|
|
VoronoiRoute a, b;
|
|
a.rebuild(g.occ, g.w, g.h);
|
|
b.rebuild(g.occ, g.w, g.h);
|
|
ASSERT_EQ(a.graph().edges().size(), b.graph().edges().size());
|
|
for (size_t i = 0; i < a.graph().edges().size(); ++i)
|
|
{
|
|
EXPECT_EQ(a.graph().edges()[i].u, b.graph().edges()[i].u);
|
|
EXPECT_EQ(a.graph().edges()[i].v, b.graph().edges()[i].v);
|
|
EXPECT_EQ(a.graph().edges()[i].cells, b.graph().edges()[i].cells);
|
|
}
|
|
}
|
|
|
|
TEST(VoronoiRoute, HashChangesWhenOneCellFlips)
|
|
{
|
|
Grid g = corridor();
|
|
const uint64_t h0 = voronoi_route::hashObstacleMask(g.occ);
|
|
g.set(100, 20, 1);
|
|
EXPECT_NE(h0, voronoi_route::hashObstacleMask(g.occ));
|
|
}
|
|
|
|
// ---- obstacle rule = StaticLayer::interpretValue + allow_unknown -------------------------------
|
|
#include "voronoi_route/map_convert.h"
|
|
|
|
TEST(MapConvert, ClassifyMirrorsStaticLayer)
|
|
{
|
|
using voronoi_route::CostClass;
|
|
voronoi_route::StaticLayerRule r; // track_unknown true, lethal 100, unknown -1, trinary
|
|
EXPECT_EQ(voronoi_route::classifyGridValue(-1, r), CostClass::kNoInformation);
|
|
EXPECT_EQ(voronoi_route::classifyGridValue(100, r), CostClass::kLethal);
|
|
EXPECT_EQ(voronoi_route::classifyGridValue(99, r), CostClass::kFree);
|
|
EXPECT_EQ(voronoi_route::classifyGridValue(0, r), CostClass::kFree);
|
|
r.track_unknown_space = false;
|
|
EXPECT_EQ(voronoi_route::classifyGridValue(-1, r), CostClass::kFree);
|
|
r.trinary_costmap = false;
|
|
EXPECT_EQ(voronoi_route::classifyGridValue(50, r), CostClass::kScaled);
|
|
r.lethal_cost_threshold = 50;
|
|
EXPECT_EQ(voronoi_route::classifyGridValue(50, r), CostClass::kLethal);
|
|
}
|
|
|
|
TEST(MapConvert, UnknownPolicyDecidesNoInformation)
|
|
{
|
|
const int8_t grid[4] = {100, -1, 0, 50}; // one row, ROS layout == image layout for h = 1
|
|
voronoi_route::ObstacleRule rule; // traversable
|
|
auto m = voronoi_route::occupancyFromGrid(grid, 4, 1, rule);
|
|
EXPECT_EQ((std::vector<uint8_t>{1, 0, 0, 0}), m);
|
|
rule.unknown = voronoi_route::UnknownPolicy::kObstacle;
|
|
m = voronoi_route::occupancyFromGrid(grid, 4, 1, rule);
|
|
EXPECT_EQ((std::vector<uint8_t>{1, 1, 0, 0}), m);
|
|
}
|
|
|
|
TEST(MapConvert, GrayAndGridAndCostmapAgree)
|
|
{
|
|
// Pixel -> map_server value -> class must equal feeding the value directly, and equal the
|
|
// costmap chars the static layer would write (254 / 255 / other).
|
|
const uint8_t gray[3] = {0, 128, 255}; // black, mid, white
|
|
const int8_t grid[3] = {100, -1, 0}; // what map_server makes of them
|
|
const unsigned char cost[3] = {254, 255, 0}; // what StaticLayer makes of the grid
|
|
voronoi_route::MapServerThresholds thr;
|
|
for (auto pol : {voronoi_route::UnknownPolicy::kTraversable, voronoi_route::UnknownPolicy::kObstacle})
|
|
{
|
|
voronoi_route::ObstacleRule rule;
|
|
rule.unknown = pol;
|
|
const auto a = voronoi_route::occupancyFromGray(gray, 3, 1, thr, rule);
|
|
const auto b = voronoi_route::occupancyFromGrid(grid, 3, 1, rule);
|
|
const auto c = voronoi_route::occupancyFromCostmap(cost, 3, 1, pol, false);
|
|
EXPECT_EQ(a, b);
|
|
EXPECT_EQ(b, c);
|
|
EXPECT_EQ(voronoi_route::hashObstacleMask(a), voronoi_route::hashObstacleMask(c));
|
|
}
|
|
}
|
|
|
|
TEST(MapConvert, ImageRuleAveragesAlphaLikeMapServer)
|
|
{
|
|
// ROS map_server (trinary) averages every channel, alpha included: (79,79,79,255) -> 123 ->
|
|
// occ 0.52 -> UNKNOWN, not occupied; (0,0,0,255) -> 63.75 -> occ 0.75 -> occupied;
|
|
// (205,205,205,255) -> 217.5 -> occ 0.15 -> FREE (what the robot really sees on an RGBA map).
|
|
const uint8_t rgba[4 * 4] = {79, 79, 79, 255, 0, 0, 0, 255, 205, 205, 205, 255, 254, 254, 254, 255};
|
|
voronoi_route::MapServerThresholds thr;
|
|
voronoi_route::ObstacleRule rule;
|
|
voronoi_route::MapConvertStats st;
|
|
const auto m = voronoi_route::occupancyFromImage(rgba, 4, 1, 4, thr, rule, &st);
|
|
EXPECT_EQ((std::vector<uint8_t>{0, 1, 0, 0}), m);
|
|
EXPECT_EQ(st.occupied, 1u);
|
|
EXPECT_EQ(st.unknown, 1u);
|
|
EXPECT_EQ(st.free, 2u);
|
|
// Same pixels as 1-channel luminance would call the first one occupied — the two must differ.
|
|
const uint8_t gray[4] = {79, 0, 205, 254};
|
|
const auto g = voronoi_route::occupancyFromGray(gray, 4, 1, thr, rule);
|
|
EXPECT_EQ((std::vector<uint8_t>{1, 1, 0, 0}), g);
|
|
}
|
|
|
|
TEST(MapConvert, RuleStringRoundTrip)
|
|
{
|
|
voronoi_route::ObstacleRule r;
|
|
r.layer.lethal_cost_threshold = 90;
|
|
r.layer.track_unknown_space = false;
|
|
r.unknown = voronoi_route::UnknownPolicy::kObstacle;
|
|
voronoi_route::ObstacleRule back;
|
|
ASSERT_TRUE(voronoi_route::parseObstacleRuleString(voronoi_route::obstacleRuleString(r), &back));
|
|
EXPECT_EQ(back.layer.lethal_cost_threshold, 90);
|
|
EXPECT_FALSE(back.layer.track_unknown_space);
|
|
EXPECT_EQ(back.unknown, voronoi_route::UnknownPolicy::kObstacle);
|
|
EXPECT_FALSE(voronoi_route::parseObstacleRuleString("map_server:occupied", nullptr));
|
|
r.roi_margin_cells = 40;
|
|
ASSERT_TRUE(voronoi_route::parseObstacleRuleString(voronoi_route::obstacleRuleString(r), &back));
|
|
EXPECT_EQ(back.roi_margin_cells, 40);
|
|
}
|
|
|
|
TEST(MapConvert, RoiTurnsOutsideOfObstacleBoxIntoObstacle)
|
|
{
|
|
const int w = 50, h = 40;
|
|
std::vector<uint8_t> m(static_cast<size_t>(w) * h, 0);
|
|
for (int x = 20; x < 30; ++x) m[static_cast<size_t>(15) * w + x] = 1; // one wall segment
|
|
for (int x = 20; x < 30; ++x) m[static_cast<size_t>(25) * w + x] = 1;
|
|
const size_t turned = voronoi_route::applyObstacleRoi(m, w, h, 2);
|
|
EXPECT_GT(turned, 0u);
|
|
EXPECT_EQ(m[static_cast<size_t>(20) * w + 25], 0); // inside the box: untouched
|
|
EXPECT_EQ(m[static_cast<size_t>(0) * w + 0], 1); // far corner: outside
|
|
EXPECT_EQ(m[static_cast<size_t>(13) * w + 18], 0); // (18,13) = box corner grown by 2: inside, untouched
|
|
EXPECT_EQ(m[static_cast<size_t>(12) * w + 25], 1); // one row past the margin: outside
|
|
// fewer than min_obstacle_cells -> no-op
|
|
std::vector<uint8_t> tiny(static_cast<size_t>(w) * h, 0);
|
|
tiny[5] = 1;
|
|
EXPECT_EQ(voronoi_route::applyObstacleRoi(tiny, w, h, 2), 0u);
|
|
}
|
|
|
|
TEST(VoronoiRoute, GridBorderIsNeverVoronoi)
|
|
{
|
|
// Lone block in open space: DynamicVoronoi leaves the untouched outer ring flagged; the wrapper
|
|
// must hide it, and the graph must not pick it up as a frame around the map.
|
|
Grid g(120, 80);
|
|
g.rect(50, 30, 69, 49);
|
|
VoronoiRoute vr;
|
|
vr.rebuild(g.occ, g.w, g.h);
|
|
int border = 0;
|
|
for (int x = 0; x < g.w; ++x) border += vr.field().isVoronoi(x, 0) + vr.field().isVoronoi(x, g.h - 1);
|
|
for (int y = 0; y < g.h; ++y) border += vr.field().isVoronoi(0, y) + vr.field().isVoronoi(g.w - 1, y);
|
|
EXPECT_EQ(border, 0);
|
|
for (const auto& e : vr.graph().edges())
|
|
for (const auto& c : e.cells)
|
|
EXPECT_TRUE(c.x > 0 && c.y > 0 && c.x < g.w - 1 && c.y < g.h - 1);
|
|
}
|