#include #include #include #include #include #include #include #include using namespace robot_costmap_2d; namespace { DirectionGeometry geometry() { return {4, 4, 1.0, 0.0, 0.0, "map"}; } robot_nav_msgs::OccupancyGrid grid() { robot_nav_msgs::OccupancyGrid g; g.header.frame_id = "/map"; g.info.width = 4; g.info.height = 4; g.info.resolution = 1; g.data.assign(16, 0); return g; } auto field(const robot_nav_msgs::OccupancyGrid& g) { return DirectionField::create(g, geometry(), M_PI / 2.0); } using Cell = std::pair; std::vector cells(double x0, double y0, double x1, double y1) { std::vector out; EXPECT_TRUE(traverseGridSegment(x0, y0, x1, y1, [&](int64_t x, int64_t y) { out.emplace_back(x, y); return true; })); return out; } } // namespace TEST(DirectionContract, CodesAndSizes) { auto g = grid(); ASSERT_TRUE(field(g)); EXPECT_FALSE(field(g)->hasConstraints()); for (int code = 0; code <= 8; ++code) { g.data[0] = code; auto f = field(g); ASSERT_TRUE(f); EXPECT_EQ(f->code(0, 0), code); } for (int code : {-128, -2, -1, 9, 100, 127}) { g.data[0] = code; EXPECT_EQ(validateDirectionGrid(g, geometry()).error, DirectionValidationError::kCode); } g.data.clear(); auto f = field(g); ASSERT_TRUE(f); EXPECT_FALSE(f->hasConstraints()); EXPECT_EQ(f->code(3, 3), 0); g.data.assign(15, 0); EXPECT_EQ(validateDirectionGrid(g, geometry()).error, DirectionValidationError::kSize); g.info.width = UINT32_MAX; g.info.height = UINT32_MAX; EXPECT_EQ(validateDirectionGrid(g, geometry()).error, DirectionValidationError::kOverflow); } TEST(DirectionContract, CheckedCellAccess) { auto g = grid(); g.data[15] = 8; const auto f = field(g); ASSERT_TRUE(f); uint8_t code = 99; EXPECT_TRUE(f->codeAt(3, 3, &code)); EXPECT_EQ(code, 8); EXPECT_TRUE(f->codeAt(0, 0, &code)); EXPECT_EQ(code, 0); code = 99; EXPECT_FALSE(f->codeAt(-1, 0, &code)); EXPECT_FALSE(f->codeAt(0, -1, &code)); EXPECT_FALSE(f->codeAt(4, 0, &code)); EXPECT_FALSE(f->codeAt(0, 4, &code)); EXPECT_FALSE(f->codeAt(INT64_MAX, INT64_MAX, &code)); EXPECT_FALSE(f->codeAt(0, 0, nullptr)); EXPECT_EQ(code, 99); g.data.clear(); const auto empty = field(g); ASSERT_TRUE(empty); EXPECT_TRUE(empty->codeAt(3, 3, &code)); EXPECT_EQ(code, 0); code = 99; EXPECT_FALSE(empty->codeAt(4, 3, &code)); EXPECT_EQ(code, 99); } TEST(DirectionContract, GeometryAndNonFinite) { auto g = grid(); EXPECT_TRUE(field(g)); g.header.frame_id = ""; EXPECT_EQ(validateDirectionGrid(g, geometry()).error, DirectionValidationError::kFrame); g.header.frame_id = "other"; EXPECT_FALSE(field(g)); g = grid(); g.info.width = 3; EXPECT_FALSE(field(g)); g = grid(); g.info.resolution = 0.5; EXPECT_FALSE(field(g)); g = grid(); g.info.origin.position.x = 0.01; EXPECT_FALSE(field(g)); g = grid(); g.info.origin.position.y = 0.01; EXPECT_FALSE(field(g)); g = grid(); g.info.origin.orientation.z = 0.1; g.info.origin.orientation.w = 1; EXPECT_FALSE(field(g)); g = grid(); g.info.origin.orientation.w = 1; EXPECT_TRUE(field(g)); g.info.origin.orientation.w = -1; EXPECT_TRUE(field(g)); g = grid(); EXPECT_TRUE(field(g)); // Default zero quaternion. g.info.origin.position.x = std::numeric_limits::quiet_NaN(); EXPECT_FALSE(field(g)); g = grid(); g.info.resolution = std::numeric_limits::infinity(); EXPECT_FALSE(field(g)); g = grid(); g.info.origin.position.z = std::numeric_limits::infinity(); EXPECT_FALSE(field(g)); g = grid(); g.info.origin.orientation.w = std::numeric_limits::quiet_NaN(); EXPECT_FALSE(field(g)); } TEST(DirectionRule, AllDirectionsAndBoundaries) { constexpr int vx[] = {1, 1, 0, -1, -1, -1, 0, 1}; constexpr int vy[] = {0, 1, 1, 1, 0, -1, -1, -1}; for (int k = 0; k < 8; ++k) { auto g = grid(); g.data.assign(16, k + 1); auto f = field(g); ASSERT_TRUE(f); const double x = 1.5; const double y = 1.5; EXPECT_EQ(f->checkSegment(x, y, x + 0.2 * vx[k], y + 0.2 * vy[k]), SegmentResult::kAllowed); EXPECT_EQ(f->checkSegment(x, y, x - 0.2 * vx[k], y - 0.2 * vy[k]), SegmentResult::kViolation); EXPECT_EQ(f->checkSegment(x, y, x - 0.2 * vy[k], y + 0.2 * vx[k]), SegmentResult::kAllowed); EXPECT_EQ(f->checkSegment(x, y, x + 0.2 * vy[k], y - 0.2 * vx[k]), SegmentResult::kAllowed); EXPECT_EQ(f->checkSegment(x, y, x, y), SegmentResult::kAllowed); } auto g = grid(); g.data.assign(16, 1); auto f = field(g); EXPECT_EQ(f->checkSegment(1.5, 1.5, 1.5 - 1e-6, 1.7), SegmentResult::kViolation); EXPECT_EQ(f->checkSegment(1.5, 1.5, 1.5 + 1e-6, 1.7), SegmentResult::kAllowed); EXPECT_EQ(allowedDirectionMask(-1e-11, 1.0, M_PI / 2.0) & 1, 1); EXPECT_EQ(allowedDirectionMask(-1e-6, 1.0, M_PI / 2.0) & 1, 0); } TEST(DirectionRule, MaskMatchesDotProducts) { constexpr int vx[] = {1, 1, 0, -1, -1, -1, 0, 1}; constexpr int vy[] = {0, 1, 1, 1, 0, -1, -1, -1}; for (int x = -12; x <= 12; ++x) { for (int y = -12; y <= 12; ++y) { auto mask = allowedDirectionMask(x, y, M_PI / 2.0); for (int k = 0; k < 8; ++k) EXPECT_EQ(bool(mask & (1u << k)), vx[k] * x + vy[k] * y >= 0); } } } TEST(DirectionRule, RightAngleMatchesLegacySweep) { constexpr int vx[] = {1, 1, 0, -1, -1, -1, 0, 1}; constexpr int vy[] = {0, 1, 1, 1, 0, -1, -1, -1}; const auto check = [&](double dx, double dy) { uint8_t reference = 0; const long double x = dx, y = dy; const long double tolerance = 1e-9L * (std::abs(x) + std::abs(y)); for (int k = 0; k < 8; ++k) if (vx[k] * x + vy[k] * y >= -tolerance) reference |= uint8_t(1u << k); EXPECT_EQ(allowedDirectionMask(dx, dy, M_PI / 2.0), reference) << dx << "," << dy; }; for (double magnitude : {1e-100, 0.01, 1.0, 1e100}) { for (int degree = 0; degree < 360; ++degree) check(magnitude * std::cos(degree * M_PI / 180.0), magnitude * std::sin(degree * M_PI / 180.0)); for (int k = 0; k < 8; ++k) check(magnitude * vx[k], magnitude * vy[k]); check(-1.0000000005e-9 * magnitude, magnitude); // Near the legacy tolerance boundary. } check(0, 0); EXPECT_EQ(allowedDirectionMask(std::numeric_limits::infinity(), 0, M_PI / 2.0), 0); EXPECT_EQ(allowedDirectionMask(0, std::numeric_limits::quiet_NaN(), M_PI / 2.0), 0); } TEST(DirectionRule, ConfigurableAngularBoundaries) { const auto mask = allowedDirectionMask(1, 0.5, M_PI / 4.0); EXPECT_NE(mask & (1u << 0), 0); EXPECT_NE(mask & (1u << 1), 0); EXPECT_EQ(mask & (1u << 2), 0); const auto boundary = allowedDirectionMask(1, 1, M_PI / 4.0); EXPECT_NE(boundary & (1u << 0), 0); EXPECT_NE(boundary & (1u << 2), 0); EXPECT_EQ(allowedDirectionMask(1, 1.01, M_PI / 4.0) & 1u, 0); EXPECT_EQ(allowedDirectionMask(0, 1, M_PI / 6.0) & 1u, 0); EXPECT_EQ(allowedDirectionMask(0, 0, M_PI / 6.0), 255); } TEST(DirectionRule, FieldUsesItsDeviationForCrossing) { auto g = grid(); g.data.assign(16, 3); // +Y zone, crossed along +X. const auto wide = DirectionField::create(g, geometry(), M_PI / 2.0); const auto narrow = DirectionField::create(g, geometry(), M_PI / 4.0); ASSERT_TRUE(wide && narrow); EXPECT_DOUBLE_EQ(narrow->maxDeviationRad(), M_PI / 4.0); EXPECT_EQ(wide->checkSegment(0.5, 1.5, 3.5, 1.5), SegmentResult::kAllowed); EXPECT_EQ(narrow->checkSegment(0.5, 1.5, 3.5, 1.5), SegmentResult::kViolation); } TEST(DirectionDDA, OwnedEdgesCornersEndpointsAndReverse) { EXPECT_EQ(cells(1, 0.2, 1, 2.8), (std::vector{{1, 0}, {1, 1}, {1, 2}})); EXPECT_EQ(cells(1, 2.8, 1, 0.2), (std::vector{{1, 2}, {1, 1}, {1, 0}})); EXPECT_EQ(cells(0.5, 0.5, 2.5, 2.5), (std::vector{{0, 0}, {1, 1}, {2, 2}})); EXPECT_EQ(cells(0, 0, 6, 4), (std::vector{{0, 0}, {1, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 2}, {4, 3}, {5, 3}})); EXPECT_EQ(cells(0.5, 0.5, 2.0, 0.5), (std::vector{{0, 0}, {1, 0}})); EXPECT_EQ(cells(2.0, 0.5, 0.5, 0.5), (std::vector{{1, 0}, {0, 0}})); for (auto p : std::vector>{{0.2, 0.3, 3.8, 2.7}, {1, 0.2, 1, 3.5}, {0.5, 0.5, 3.5, 3.5}}) { auto a = cells(p[0], p[1], p[2], p[3]); auto b = cells(p[2], p[3], p[0], p[1]); std::reverse(b.begin(), b.end()); EXPECT_EQ(a, b); EXPECT_EQ(std::set(a.begin(), a.end()).size(), a.size()); } } TEST(DirectionDDA, QueriesCrossZonesAndRejectOutside) { auto g = grid(); g.data[1] = 5; g.data[2] = 5; auto f = field(g); SegmentViolation v{}; EXPECT_EQ(f->checkSegment(0.2, 0.5, 3.5, 0.5, &v), SegmentResult::kViolation); EXPECT_EQ(v.x, 1u); EXPECT_EQ(v.code, 5); EXPECT_EQ(f->checkSegment(3.5, 0.5, 0.2, 0.5), SegmentResult::kAllowed); EXPECT_EQ(f->checkSegment(0.2, 0.5, 1.0, 0.5), SegmentResult::kAllowed); EXPECT_EQ(f->checkSegment(0.2, 0.5, 4.0, 0.5), SegmentResult::kOutOfGrid); EXPECT_EQ(f->checkSegment(-0.1, 0.5, 0.1, 0.5), SegmentResult::kOutOfGrid); EXPECT_EQ(f->checkSegment(4, 0.5, 4, 0.5), SegmentResult::kOutOfGrid); }