feat(reflector_pair): add ReflectorPairMatcher for two flat reflective strips on a wall
Pure geometry core for the upcoming wall reflector detection action. The wall line is fitted from dark rays only; each strip edge is the half-step bearing between the intensity core and its neighbour, intersected with that line, so the range bias of bright returns never enters the pose. Exactly one valid pair is required; two or more report ambiguous. Synthetic tests cover geometry, quantisation, bloom, bright range bias, realistic range noise, wall ends, occluders, ambiguity and malformed input.
This commit is contained in:
688
test/reflector_pair_matcher_test.cpp
Normal file
688
test/reflector_pair_matcher_test.cpp
Normal file
@@ -0,0 +1,688 @@
|
||||
#include <dock_pattern_detector/reflector_pair_matcher.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "wall_scan_sim.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
using dock_pattern_detector::ReflectorPairDetection;
|
||||
using dock_pattern_detector::ReflectorPairLimits;
|
||||
using dock_pattern_detector::ReflectorPairMatcher;
|
||||
using dock_pattern_detector::ReflectorPairProfile;
|
||||
using dock_pattern_detector::ReflectorPose2D;
|
||||
constexpr double kPi = 3.14159265358979323846;
|
||||
constexpr double kFrontStep = 0.003926990554;
|
||||
constexpr double kNanoStep = 0.0058824;
|
||||
|
||||
// TEST-ONLY: không phải kích thước hay hiệu chỉnh của trạm thật.
|
||||
ReflectorPairProfile profile()
|
||||
{
|
||||
ReflectorPairProfile test_profile;
|
||||
test_profile.tape_width = 0.05;
|
||||
test_profile.width_tolerance = 0.025;
|
||||
test_profile.tape_spacing = 0.40;
|
||||
test_profile.spacing_tolerance = 0.025;
|
||||
test_profile.tape_min_intensity = 8000;
|
||||
test_profile.tape_core_intensity = 15000;
|
||||
test_profile.tape_bloom_max_angle = 0.0;
|
||||
test_profile.tape_plateau_min_fraction = 0.5;
|
||||
test_profile.edge_bias_bound = 0.0;
|
||||
test_profile.max_edge_uncertainty = 0.04;
|
||||
test_profile.wall_min_span = 0.65;
|
||||
test_profile.wall_max_rms = 0.006;
|
||||
test_profile.wall_max_residual = 0.02;
|
||||
test_profile.wall_max_gap = 0.15;
|
||||
test_profile.wall_merge_distance = 0.02;
|
||||
test_profile.offset_x = 0.6;
|
||||
return test_profile;
|
||||
}
|
||||
|
||||
ReflectorPairDetection detect(ReflectorPairProfile test_profile, const wall_scan_sim::World& world,
|
||||
const robot_line_extraction::ScanGeometry& geometry,
|
||||
ReflectorPairLimits limits = {}, ReflectorPose2D scan_in_prior = {})
|
||||
{
|
||||
ReflectorPairMatcher matcher;
|
||||
std::string error;
|
||||
if (!matcher.configure(test_profile, error))
|
||||
{
|
||||
ADD_FAILURE() << error;
|
||||
return {};
|
||||
}
|
||||
limits.max_range = 6.0;
|
||||
const auto scan = wall_scan_sim::rayCast(world, geometry);
|
||||
return matcher.match(geometry, scan.ranges, scan.intensities, limits, scan_in_prior);
|
||||
}
|
||||
|
||||
double yawError(double actual_yaw, double expected_yaw)
|
||||
{
|
||||
const double difference = actual_yaw - expected_yaw;
|
||||
return std::abs(std::atan2(std::sin(difference), std::cos(difference)));
|
||||
}
|
||||
|
||||
// Cận lượng tử dùng worst-case đạo hàm trong nửa bước quanh mọi mép vật lý; không lấy dung sai gate.
|
||||
double quantisationBound(const wall_scan_sim::World& world, double step)
|
||||
{
|
||||
const double nx = std::cos(world.normal_yaw);
|
||||
const double ny = std::sin(world.normal_yaw);
|
||||
const double tx = -ny;
|
||||
const double ty = nx;
|
||||
const double rho = std::abs(nx * world.x + ny * world.y);
|
||||
double min_cos = 1.0;
|
||||
for (const auto& tape : world.tapes)
|
||||
{
|
||||
for (const double sign : {-1.0, 1.0})
|
||||
{
|
||||
const double v = tape.center + sign * tape.width / 2.0;
|
||||
const double x = world.x + tx * v;
|
||||
const double y = world.y + ty * v;
|
||||
const double incidence = std::acos(std::clamp(rho / std::hypot(x, y), 0.0, 1.0));
|
||||
min_cos = std::min(min_cos, std::cos(incidence + std::abs(step) / 2.0));
|
||||
}
|
||||
}
|
||||
return rho * std::abs(step) / (2.0 * min_cos * min_cos) + 1e-9;
|
||||
}
|
||||
|
||||
// Xây lại support từ số tia hai phía của detection để tính phương sai fit từ hình học thực tế.
|
||||
// Xấp xỉ tuyến tính nhiễu độc lập: Var(offset tại s) <= sigma²(1/N + (s-mean)²/Sxx),
|
||||
// Var(yaw) <= sigma²/Sxx. Hệ số 6 là cận thống kê cho seed thử, không phải cận Gaussian tuyệt đối.
|
||||
std::array<double, 2> fitNoiseBounds(const wall_scan_sim::World& world,
|
||||
const robot_line_extraction::ScanGeometry& geometry,
|
||||
const ReflectorPairDetection& detection,
|
||||
double minimum_cosine)
|
||||
{
|
||||
const auto scan = wall_scan_sim::rayCast(world, geometry);
|
||||
std::size_t first_bright = geometry.size;
|
||||
std::size_t last_bright = 0;
|
||||
for (std::size_t index = 0; index < geometry.size; ++index)
|
||||
{
|
||||
if (scan.intensities[index] >= 8000)
|
||||
{
|
||||
first_bright = std::min(first_bright, index);
|
||||
last_bright = index;
|
||||
}
|
||||
}
|
||||
if (first_bright == geometry.size)
|
||||
{
|
||||
return {{0.0, 0.0}};
|
||||
}
|
||||
std::size_t first = first_bright;
|
||||
std::size_t last = last_bright;
|
||||
for (std::size_t count = 0; count < detection.wall_support[0] && first > 0;)
|
||||
{
|
||||
--first;
|
||||
if (std::isfinite(scan.ranges[first]) && scan.intensities[first] < 8000)
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
for (std::size_t count = 0; count < detection.wall_support[1] && last + 1 < geometry.size;)
|
||||
{
|
||||
++last;
|
||||
if (std::isfinite(scan.ranges[last]) && scan.intensities[last] < 8000)
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
const double tangent_x = -std::sin(world.normal_yaw);
|
||||
const double tangent_y = std::cos(world.normal_yaw);
|
||||
std::vector<double> along;
|
||||
for (std::size_t index = first; index <= last; ++index)
|
||||
{
|
||||
if (!std::isfinite(scan.ranges[index]) || scan.intensities[index] >= 8000)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
const double angle = geometry.angle_min + index * geometry.angle_increment;
|
||||
along.push_back(scan.ranges[index] *
|
||||
(tangent_x * std::cos(angle) + tangent_y * std::sin(angle)));
|
||||
}
|
||||
if (along.size() < 3)
|
||||
{
|
||||
return {{0.0, 0.0}};
|
||||
}
|
||||
double mean = 0.0;
|
||||
for (const double position : along)
|
||||
{
|
||||
mean += position;
|
||||
}
|
||||
mean /= static_cast<double>(along.size());
|
||||
double sum_squared = 0.0;
|
||||
for (const double position : along)
|
||||
{
|
||||
sum_squared += (position - mean) * (position - mean);
|
||||
}
|
||||
if (sum_squared <= 0.0)
|
||||
{
|
||||
return {{0.0, 0.0}};
|
||||
}
|
||||
double max_offset = 0.0;
|
||||
const double center_along = tangent_x * world.x + tangent_y * world.y;
|
||||
for (const auto& tape : world.tapes)
|
||||
{
|
||||
for (const double sign : {-1.0, 1.0})
|
||||
{
|
||||
max_offset = std::max(max_offset,
|
||||
std::abs(center_along + tape.center + sign * tape.width / 2.0 - mean));
|
||||
}
|
||||
}
|
||||
const double position_sigma = world.noise_sigma *
|
||||
std::sqrt(1.0 / static_cast<double>(along.size()) + max_offset * max_offset / sum_squared) /
|
||||
minimum_cosine;
|
||||
const double yaw_sigma = world.noise_sigma / (std::sqrt(sum_squared) * minimum_cosine);
|
||||
return {{6.0 * position_sigma, 6.0 * yaw_sigma}};
|
||||
}
|
||||
|
||||
TEST(ReflectorPairProfile, RequiredFieldsRejectUnsetNonfiniteAndOutOfRange)
|
||||
{
|
||||
const double nan = std::numeric_limits<double>::quiet_NaN();
|
||||
const std::vector<std::pair<const char*, double ReflectorPairProfile::*>> fields = {
|
||||
{"tape_width", &ReflectorPairProfile::tape_width}, {"width_tolerance", &ReflectorPairProfile::width_tolerance},
|
||||
{"tape_spacing", &ReflectorPairProfile::tape_spacing}, {"spacing_tolerance", &ReflectorPairProfile::spacing_tolerance},
|
||||
{"tape_min_intensity", &ReflectorPairProfile::tape_min_intensity}, {"tape_core_intensity", &ReflectorPairProfile::tape_core_intensity},
|
||||
{"tape_bloom_max_angle", &ReflectorPairProfile::tape_bloom_max_angle},
|
||||
{"tape_plateau_min_fraction", &ReflectorPairProfile::tape_plateau_min_fraction},
|
||||
{"edge_bias_bound", &ReflectorPairProfile::edge_bias_bound}, {"max_edge_uncertainty", &ReflectorPairProfile::max_edge_uncertainty},
|
||||
{"wall_min_span", &ReflectorPairProfile::wall_min_span}, {"wall_max_rms", &ReflectorPairProfile::wall_max_rms},
|
||||
{"wall_max_residual", &ReflectorPairProfile::wall_max_residual}, {"wall_max_gap", &ReflectorPairProfile::wall_max_gap},
|
||||
{"wall_merge_distance", &ReflectorPairProfile::wall_merge_distance}, {"offset_x", &ReflectorPairProfile::offset_x}};
|
||||
for (const auto& field : fields)
|
||||
{
|
||||
for (double bad : {nan, std::numeric_limits<double>::infinity(), -1.0})
|
||||
{
|
||||
auto test_profile = profile();
|
||||
test_profile.*field.second = bad;
|
||||
std::string error;
|
||||
EXPECT_FALSE(test_profile.validate(error)) << field.first;
|
||||
EXPECT_NE(error.find(field.first), std::string::npos) << error;
|
||||
}
|
||||
}
|
||||
ReflectorPairProfile unset;
|
||||
std::string error;
|
||||
EXPECT_FALSE(unset.validate(error));
|
||||
EXPECT_NE(error.find("tape_width"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(ReflectorPairProfile, ValidationErrorIncludesValueAndRange)
|
||||
{
|
||||
auto test_profile = profile();
|
||||
test_profile.offset_x = -2.5;
|
||||
std::string error;
|
||||
EXPECT_FALSE(test_profile.validate(error));
|
||||
EXPECT_NE(error.find("offset_x"), std::string::npos);
|
||||
EXPECT_NE(error.find("-2.5"), std::string::npos);
|
||||
EXPECT_NE(error.find("> 0"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(ReflectorPairProfile, CrossKeyAndConditionalPriorValidation)
|
||||
{
|
||||
std::string error;
|
||||
auto test_profile = profile();
|
||||
EXPECT_TRUE(test_profile.validate(error));
|
||||
test_profile.has_prior = true;
|
||||
EXPECT_FALSE(test_profile.validate(error));
|
||||
EXPECT_NE(error.find("prior_position_tolerance"), std::string::npos);
|
||||
test_profile.prior_position_tolerance = 0.1;
|
||||
EXPECT_FALSE(test_profile.validate(error));
|
||||
EXPECT_NE(error.find("prior_yaw_tolerance"), std::string::npos);
|
||||
test_profile.prior_yaw_tolerance = 0.2;
|
||||
EXPECT_TRUE(test_profile.validate(error));
|
||||
test_profile = profile();
|
||||
test_profile.tape_spacing = test_profile.tape_width;
|
||||
EXPECT_FALSE(test_profile.validate(error));
|
||||
test_profile = profile();
|
||||
test_profile.wall_min_span = test_profile.tape_spacing;
|
||||
EXPECT_FALSE(test_profile.validate(error));
|
||||
test_profile = profile();
|
||||
test_profile.tape_core_intensity = test_profile.tape_min_intensity - 1;
|
||||
EXPECT_FALSE(test_profile.validate(error));
|
||||
test_profile = profile();
|
||||
test_profile.wall_max_residual = test_profile.wall_max_rms / 2;
|
||||
EXPECT_FALSE(test_profile.validate(error));
|
||||
test_profile = profile();
|
||||
test_profile.width_tolerance = test_profile.tape_width;
|
||||
EXPECT_FALSE(test_profile.validate(error));
|
||||
test_profile = profile();
|
||||
test_profile.tape_min_points = 2;
|
||||
EXPECT_FALSE(test_profile.validate(error));
|
||||
test_profile = profile();
|
||||
test_profile.wall_min_points_each_side = 2;
|
||||
EXPECT_FALSE(test_profile.validate(error));
|
||||
}
|
||||
|
||||
TEST(ReflectorPairGeometry, DistancesAnglesAndPhase)
|
||||
{
|
||||
for (double distance : {0.5, 1.0, 1.5, 2.0})
|
||||
{
|
||||
for (double degrees : {-50.0, -30.0, 0.0, 30.0, 50.0})
|
||||
{
|
||||
for (double phase : {0.0, 0.25, 0.5, 0.75})
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.x = distance;
|
||||
world.normal_yaw = kPi + degrees * kPi / 180.0;
|
||||
const auto geometry = wall_scan_sim::geometry(kFrontStep, phase * kFrontStep);
|
||||
const auto detection = detect(profile(), world, geometry);
|
||||
ASSERT_TRUE(detection.valid) << distance << ' ' << degrees << ' ' << phase << ' ' << detection.reason;
|
||||
const double bound = quantisationBound(world, kFrontStep);
|
||||
EXPECT_LE(std::hypot(detection.marker.x - world.x, detection.marker.y - world.y), bound);
|
||||
EXPECT_LT(yawError(detection.marker.yaw, world.normal_yaw), 1e-6);
|
||||
EXPECT_LE(std::abs(detection.spacing - 0.4), 2.0 * bound);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ReflectorPairGeometry, NanoScaleAndReverseRayOrder)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.x = 1.5;
|
||||
world.wall_intensity = 205;
|
||||
world.tape_intensity = 255;
|
||||
auto test_profile = profile();
|
||||
test_profile.tape_min_intensity = 230;
|
||||
test_profile.tape_core_intensity = 245;
|
||||
const auto forward_order = detect(test_profile, world, wall_scan_sim::geometry(kNanoStep));
|
||||
const auto reverse_order = detect(test_profile, world, wall_scan_sim::geometry(kNanoStep, 0.0, true));
|
||||
ASSERT_TRUE(forward_order.valid) << forward_order.reason;
|
||||
ASSERT_TRUE(reverse_order.valid) << reverse_order.reason;
|
||||
EXPECT_NEAR(forward_order.marker.x, reverse_order.marker.x, 1e-9);
|
||||
EXPECT_NEAR(forward_order.marker.y, reverse_order.marker.y, 1e-9);
|
||||
EXPECT_LT(yawError(forward_order.marker.yaw, reverse_order.marker.yaw), 1e-9);
|
||||
EXPECT_LE(std::hypot(forward_order.marker.x-world.x, forward_order.marker.y-world.y), quantisationBound(world, kNanoStep));
|
||||
}
|
||||
|
||||
TEST(ReflectorPairGeometry, TargetOffsetsAndPriorFrameYawWrap)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
auto test_profile = profile();
|
||||
test_profile.offset_y = 0.12;
|
||||
test_profile.offset_yaw = 0.15;
|
||||
const ReflectorPose2D scan_in_prior{0.3, -0.2, 0.1};
|
||||
const auto detection = detect(test_profile, world, wall_scan_sim::geometry(kFrontStep), {}, scan_in_prior);
|
||||
ASSERT_TRUE(detection.valid) << detection.reason;
|
||||
EXPECT_NEAR(detection.target.x, detection.marker.x + test_profile.offset_x*std::cos(detection.marker.yaw) - test_profile.offset_y*std::sin(detection.marker.yaw), 1e-9);
|
||||
EXPECT_NEAR(detection.target.y, detection.marker.y + test_profile.offset_x*std::sin(detection.marker.yaw) + test_profile.offset_y*std::cos(detection.marker.yaw), 1e-9);
|
||||
EXPECT_LT(yawError(detection.target.yaw, detection.marker.yaw + test_profile.offset_yaw), 1e-9);
|
||||
EXPECT_LT(yawError(detection.marker.yaw, -kPi + 0.1), 1e-9);
|
||||
EXPECT_NEAR(detection.marker.x, scan_in_prior.x + std::cos(scan_in_prior.yaw), quantisationBound(world,kFrontStep));
|
||||
}
|
||||
|
||||
TEST(ReflectorPairGeometry, RejectsParallelEdgesAndInvalidTransform)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.normal_yaw = kPi + 50*kPi/180;
|
||||
auto test_profile = profile();
|
||||
test_profile.min_ray_wall_cos = 0.95;
|
||||
const auto detection = detect(test_profile,world,wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_FALSE(detection.valid);
|
||||
EXPECT_EQ(detection.reason, "edge_parallel");
|
||||
ReflectorPose2D scan_in_prior;
|
||||
scan_in_prior.yaw = std::numeric_limits<double>::quiet_NaN();
|
||||
EXPECT_EQ(detect(profile(),world,wall_scan_sim::geometry(kFrontStep),{},scan_in_prior).reason, "invalid_limits_or_transform");
|
||||
}
|
||||
|
||||
TEST(ReflectorPairBloom, BrightRangeBiasAndAsymmetricBloom)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.x = 1.2;
|
||||
world.bloom_left = 1;
|
||||
world.bloom_right = 2;
|
||||
auto test_profile = profile();
|
||||
test_profile.tape_bloom_max_angle = 2*kFrontStep;
|
||||
test_profile.tape_plateau_min_fraction = 0.45;
|
||||
const auto geometry = wall_scan_sim::geometry(kFrontStep);
|
||||
const auto ref = detect(test_profile,world,geometry);
|
||||
ASSERT_TRUE(ref.valid) << ref.reason;
|
||||
for (double bias : {-0.01, -0.02, -0.03, 0.03})
|
||||
{
|
||||
world.bright_range_bias = bias;
|
||||
const auto detection = detect(test_profile,world,geometry);
|
||||
ASSERT_TRUE(detection.valid) << detection.reason;
|
||||
EXPECT_NEAR(detection.marker.x, ref.marker.x, 1e-10);
|
||||
EXPECT_NEAR(detection.marker.y, ref.marker.y, 1e-10);
|
||||
EXPECT_LE(std::hypot(detection.marker.x-world.x,detection.marker.y-world.y), quantisationBound(world,kFrontStep));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ReflectorPairBloom, CoreTiesAndInseparableOrHighUncertaintyReject)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.bloom_left = 4;
|
||||
world.bloom_right = 4;
|
||||
auto test_profile = profile();
|
||||
test_profile.tape_plateau_min_fraction = 0.95;
|
||||
EXPECT_FALSE(detect(test_profile,world,wall_scan_sim::geometry(kFrontStep)).valid);
|
||||
world.bloom_left = 0;
|
||||
world.bloom_right = 0;
|
||||
test_profile = profile();
|
||||
test_profile.edge_bias_bound = 0.03;
|
||||
test_profile.max_edge_uncertainty = 0.02;
|
||||
const auto detection = detect(test_profile,world,wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_FALSE(detection.valid);
|
||||
EXPECT_EQ(detection.reason, "edge_uncertainty");
|
||||
// Hai core dài bằng nhau trong một run: không chọn tùy ý.
|
||||
test_profile = profile();
|
||||
const auto geometry = wall_scan_sim::geometry(kFrontStep);
|
||||
auto scan = wall_scan_sim::rayCast(world,geometry);
|
||||
for (std::size_t index = 1; index + 1 < scan.ranges.size(); ++index)
|
||||
{
|
||||
if (scan.intensities[index] >= test_profile.tape_core_intensity &&
|
||||
scan.intensities[index - 1] < test_profile.tape_min_intensity)
|
||||
{
|
||||
std::size_t end = index;
|
||||
while (end < scan.intensities.size() &&
|
||||
scan.intensities[end] >= test_profile.tape_core_intensity)
|
||||
{
|
||||
++end;
|
||||
}
|
||||
ASSERT_GE(end - index, 7u);
|
||||
for (std::size_t ray = index; ray < end; ++ray)
|
||||
{
|
||||
scan.intensities[ray] = 9000;
|
||||
}
|
||||
for (std::size_t ray = 0; ray < 3; ++ray)
|
||||
{
|
||||
scan.intensities[index + ray] = 20000;
|
||||
scan.intensities[end - 1 - ray] = 20000;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
ReflectorPairMatcher matcher;
|
||||
std::string error;
|
||||
ASSERT_TRUE(matcher.configure(test_profile,error));
|
||||
const auto tied = matcher.match(geometry,scan.ranges,scan.intensities,{},{});
|
||||
EXPECT_FALSE(tied.valid);
|
||||
EXPECT_EQ(tied.reason, "insufficient_candidates_or_core");
|
||||
}
|
||||
|
||||
TEST(ReflectorPairPairing, WrongWidthSpacingAndOcclusionReject)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
const auto geometry = wall_scan_sim::geometry(kFrontStep);
|
||||
world.tapes[1].center = 0.30;
|
||||
auto detection = detect(profile(),world,geometry);
|
||||
EXPECT_FALSE(detection.valid);
|
||||
EXPECT_EQ(detection.reason,"spacing");
|
||||
world.tapes[1].center = 0.2;
|
||||
world.tapes[1].width = 0.09;
|
||||
detection = detect(profile(),world,geometry);
|
||||
EXPECT_FALSE(detection.valid);
|
||||
EXPECT_EQ(detection.reason,"width");
|
||||
world.tapes[1].width = 0.05;
|
||||
world.boxes.push_back({0.5,0.6,0.08,0.16,1000});
|
||||
detection = detect(profile(),world,geometry);
|
||||
EXPECT_FALSE(detection.valid);
|
||||
}
|
||||
|
||||
TEST(ReflectorPairPairing, OccluderWithinWallSupportRejects)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.boxes.push_back({0.75,0.8,-0.05,0.05,1000});
|
||||
const auto detection = detect(profile(),world,wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_FALSE(detection.valid);
|
||||
EXPECT_TRUE(detection.reason == "wall_gap_or_occlusion" || detection.reason == "wall_residual_or_occlusion" ||
|
||||
detection.reason == "wall_not_same_line") << detection.reason;
|
||||
}
|
||||
|
||||
TEST(ReflectorPairPairing, BrightObjectInFrontOfTapeIsNotSameWall)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.boxes.push_back({0.50, 0.51, 0.087, 0.113, 20000});
|
||||
const auto detection = detect(profile(),world,wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_FALSE(detection.valid);
|
||||
EXPECT_EQ(detection.reason, "bright_off_wall");
|
||||
}
|
||||
|
||||
TEST(ReflectorPairPairing, ThreeBrightRunsAndTwoPairsAreAmbiguous)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.tapes = {{-0.4,0.05},{0.0,0.05},{0.4,0.05}};
|
||||
auto detection = detect(profile(),world,wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_FALSE(detection.valid);
|
||||
EXPECT_EQ(detection.reason,"ambiguous");
|
||||
EXPECT_EQ(detection.valid_pairs,2u);
|
||||
world.tapes = {{-0.7,0.05},{-0.3,0.05},{0.3,0.05},{0.7,0.05}};
|
||||
detection = detect(profile(),world,wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_FALSE(detection.valid);
|
||||
EXPECT_EQ(detection.reason,"ambiguous");
|
||||
EXPECT_GE(detection.valid_pairs,2u);
|
||||
}
|
||||
|
||||
TEST(ReflectorPairPairing, PriorAndFanOperateOnMarkerNotTarget)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
auto test_profile = profile();
|
||||
test_profile.has_prior = true;
|
||||
test_profile.prior_x = 1.0;
|
||||
test_profile.prior_y = 0.0;
|
||||
test_profile.prior_yaw = kPi;
|
||||
test_profile.prior_position_tolerance = 0.05;
|
||||
test_profile.prior_yaw_tolerance = 0.1;
|
||||
auto detection = detect(test_profile,world,wall_scan_sim::geometry(kFrontStep));
|
||||
ASSERT_TRUE(detection.valid) << detection.reason;
|
||||
test_profile.prior_y = 0.5;
|
||||
detection = detect(test_profile,world,wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_FALSE(detection.valid);
|
||||
EXPECT_EQ(detection.reason,"prior");
|
||||
test_profile = profile();
|
||||
test_profile.search_center_yaw = kPi;
|
||||
test_profile.search_fov = 0.2;
|
||||
detection = detect(test_profile,world,wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_FALSE(detection.valid);
|
||||
EXPECT_EQ(detection.reason,"search_fov");
|
||||
}
|
||||
|
||||
TEST(ReflectorPairInputs, EmptyMismatchedAndInvalidRays)
|
||||
{
|
||||
const auto geometry = wall_scan_sim::geometry(kFrontStep);
|
||||
wall_scan_sim::World world;
|
||||
auto scan = wall_scan_sim::rayCast(world,geometry);
|
||||
ReflectorPairMatcher matcher;
|
||||
std::string error;
|
||||
ASSERT_TRUE(matcher.configure(profile(),error));
|
||||
EXPECT_EQ(matcher.match(geometry,scan.ranges,{}, {},{}).reason,"no_intensity_data");
|
||||
auto wrong = scan.intensities;
|
||||
wrong.pop_back();
|
||||
EXPECT_EQ(matcher.match(geometry,scan.ranges,wrong,{},{}).reason,"intensity_size_mismatch");
|
||||
robot_line_extraction::ScanGeometry empty;
|
||||
EXPECT_FALSE(matcher.match(empty,{}, {},{},{}).valid);
|
||||
std::fill(scan.ranges.begin(),scan.ranges.end(),std::numeric_limits<double>::quiet_NaN());
|
||||
EXPECT_FALSE(matcher.match(geometry,scan.ranges,scan.intensities,{},{}).valid);
|
||||
scan = wall_scan_sim::rayCast(world,geometry);
|
||||
for (std::size_t index = 1; index < scan.ranges.size(); ++index)
|
||||
{
|
||||
if (scan.intensities[index] >= 15000 && scan.intensities[index - 1] < 15000)
|
||||
{
|
||||
scan.ranges[index - 1] = std::numeric_limits<double>::infinity();
|
||||
break;
|
||||
}
|
||||
}
|
||||
EXPECT_FALSE(matcher.match(geometry,scan.ranges,scan.intensities,{},{}).valid);
|
||||
scan = wall_scan_sim::rayCast(world,geometry);
|
||||
for (std::size_t index = 1; index < scan.ranges.size(); ++index)
|
||||
{
|
||||
if (scan.intensities[index] >= 15000 && scan.intensities[index - 1] < 15000)
|
||||
{
|
||||
scan.intensities[index - 1] = std::numeric_limits<float>::quiet_NaN();
|
||||
break;
|
||||
}
|
||||
}
|
||||
EXPECT_FALSE(matcher.match(geometry,scan.ranges,scan.intensities,{},{}).valid);
|
||||
}
|
||||
|
||||
TEST(ReflectorPairInputs, ZeroRangesInfiniteIntensityAndShortWallReject)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
const auto geometry=wall_scan_sim::geometry(kFrontStep);
|
||||
auto scan=wall_scan_sim::rayCast(world,geometry);
|
||||
ReflectorPairMatcher matcher;
|
||||
std::string error;
|
||||
ASSERT_TRUE(matcher.configure(profile(),error));
|
||||
for (auto& range : scan.ranges)
|
||||
{
|
||||
range = 0.0;
|
||||
}
|
||||
EXPECT_FALSE(matcher.match(geometry,scan.ranges,scan.intensities,{},{}).valid);
|
||||
scan=wall_scan_sim::rayCast(world,geometry);
|
||||
for (auto& intensity : scan.intensities)
|
||||
{
|
||||
if (intensity >= 15000)
|
||||
{
|
||||
intensity = std::numeric_limits<float>::infinity();
|
||||
}
|
||||
}
|
||||
EXPECT_FALSE(matcher.match(geometry,scan.ranges,scan.intensities,{},{}).valid);
|
||||
world.half_length=0.23;
|
||||
EXPECT_FALSE(detect(profile(),world,geometry).valid);
|
||||
}
|
||||
|
||||
TEST(ReflectorPairInputs, RejectsCapacityAndPreservesPreviousValidProfile)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.tapes={{-0.4,0.05},{0,0.05},{0.4,0.05}};
|
||||
auto geometry=wall_scan_sim::geometry(kFrontStep);
|
||||
auto scan=wall_scan_sim::rayCast(world,geometry);
|
||||
ReflectorPairMatcher matcher;
|
||||
std::string error;
|
||||
ASSERT_TRUE(matcher.configure(profile(),error));
|
||||
auto bad=profile();
|
||||
bad.tape_width=0;
|
||||
EXPECT_FALSE(matcher.configure(bad,error));
|
||||
EXPECT_TRUE(matcher.configured());
|
||||
ReflectorPairLimits limits;
|
||||
limits.max_scan_rays=100;
|
||||
EXPECT_EQ(matcher.match(geometry,scan.ranges,scan.intensities,limits,{}).reason,"capacity_scan_rays");
|
||||
limits.max_scan_rays=4096;
|
||||
limits.max_candidates=2;
|
||||
EXPECT_EQ(matcher.match(geometry,scan.ranges,scan.intensities,limits,{}).reason,"capacity_candidates");
|
||||
limits.max_candidates=16;
|
||||
limits.max_pairs=1;
|
||||
EXPECT_EQ(matcher.match(geometry,scan.ranges,scan.intensities,limits,{}).reason,"capacity_pairs");
|
||||
}
|
||||
|
||||
TEST(ReflectorPairInputs, DeterministicNoiseWithinQuantisationPlusNoiseAllowance)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.noise_sigma=0.0002;
|
||||
const auto detection=detect(profile(),world,wall_scan_sim::geometry(kFrontStep));
|
||||
ASSERT_TRUE(detection.valid) << detection.reason;
|
||||
// Nhiễu Gaussian không có cận tất định: 6σ ở đây là giả định riêng cho mẫu seed cố định.
|
||||
const double bound=quantisationBound(world,kFrontStep)+6*world.noise_sigma;
|
||||
EXPECT_LE(std::hypot(detection.marker.x-world.x,detection.marker.y-world.y),bound);
|
||||
}
|
||||
|
||||
TEST(ReflectorPairNoise, FlatWallAtRealisticNoiseLevels)
|
||||
{
|
||||
// TEST-ONLY: RMS = 2σ, max residual = 5σ; cổng nhóm = 1.2σ, tối thiểu 25 tia mỗi phía.
|
||||
// Cận 6σ dùng N, Sxx và vị trí mép từ support thật (fitNoiseBounds), có tính góc nhìn.
|
||||
// Đây là cận thống kê tuyến tính cho seed cố định, không phải bảo đảm Gaussian tuyệt đối.
|
||||
for (const double sigma : {0.005, 0.010})
|
||||
{
|
||||
for (const double distance : {1.0, 2.0})
|
||||
{
|
||||
for (const double angle_degrees : {0.0, 30.0})
|
||||
{
|
||||
for (const unsigned int seed : {3u, 7u, 19u, 42u})
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.x = distance;
|
||||
world.normal_yaw = kPi + angle_degrees * kPi / 180.0;
|
||||
world.noise_sigma = sigma;
|
||||
world.seed = seed;
|
||||
auto test_profile = profile();
|
||||
test_profile.wall_min_points_each_side = 25;
|
||||
test_profile.wall_max_rms = 2.0 * sigma;
|
||||
test_profile.wall_max_residual = 5.0 * sigma;
|
||||
test_profile.wall_merge_distance = 1.2 * sigma;
|
||||
const auto result = detect(test_profile, world, wall_scan_sim::geometry(kFrontStep));
|
||||
ASSERT_TRUE(result.valid) << sigma << ' ' << distance << ' ' << angle_degrees << ' '
|
||||
<< seed << ' ' << result.reason;
|
||||
const auto geometry = wall_scan_sim::geometry(kFrontStep);
|
||||
const auto noise_bounds = fitNoiseBounds(world, geometry, result,
|
||||
test_profile.min_ray_wall_cos);
|
||||
ASSERT_GT(noise_bounds[0], 0.0);
|
||||
ASSERT_GT(noise_bounds[1], 0.0);
|
||||
EXPECT_LE(std::hypot(result.marker.x - world.x, result.marker.y - world.y),
|
||||
quantisationBound(world, kFrontStep) + noise_bounds[0]);
|
||||
EXPECT_LE(yawError(result.marker.yaw, world.normal_yaw), noise_bounds[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ReflectorPairNoise, ThreeCentimetrePlaneStepRejectsWithSameNoiseProfile)
|
||||
{
|
||||
for (const double sigma : {0.005, 0.010})
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.x = 1.0;
|
||||
world.step_start = 0.225;
|
||||
world.step_depth = 0.03;
|
||||
world.noise_sigma = sigma;
|
||||
world.seed = 7;
|
||||
auto test_profile = profile();
|
||||
test_profile.wall_min_points_each_side = 25;
|
||||
test_profile.wall_max_rms = 2.0 * sigma;
|
||||
test_profile.wall_max_residual = 5.0 * sigma;
|
||||
test_profile.wall_merge_distance = 1.2 * sigma;
|
||||
const auto result = detect(test_profile, world, wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_FALSE(result.valid);
|
||||
EXPECT_TRUE(result.reason == "wall_not_same_line" ||
|
||||
result.reason == "wall_residual_or_occlusion") << result.reason;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ReflectorPairWindow, WallEndAfterEnoughSupportKeepsOtherSideGrowing)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.max_along = 0.30;
|
||||
auto test_profile = profile();
|
||||
test_profile.wall_min_span = 0.90;
|
||||
const auto result = detect(test_profile, world, wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_TRUE(result.valid) << result.reason;
|
||||
}
|
||||
|
||||
TEST(ReflectorPairWindow, InvalidRayInsideSupportStillRejects)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
const auto geometry = wall_scan_sim::geometry(kFrontStep);
|
||||
auto scan = wall_scan_sim::rayCast(world, geometry);
|
||||
const auto center_ray = static_cast<std::size_t>(std::lround(-geometry.angle_min /
|
||||
geometry.angle_increment));
|
||||
ASSERT_LT(center_ray, scan.ranges.size());
|
||||
ASSERT_LT(scan.intensities[center_ray], 8000.0f);
|
||||
scan.ranges[center_ray] = std::numeric_limits<double>::quiet_NaN();
|
||||
ReflectorPairMatcher matcher;
|
||||
std::string error;
|
||||
ASSERT_TRUE(matcher.configure(profile(), error)) << error;
|
||||
const auto result = matcher.match(geometry, scan.ranges, scan.intensities, {}, {});
|
||||
EXPECT_FALSE(result.valid);
|
||||
EXPECT_EQ(result.reason, "wall_occluded_or_invalid");
|
||||
}
|
||||
|
||||
TEST(ReflectorPairWindow, WallEndBeforeEnoughSupportRejects)
|
||||
{
|
||||
wall_scan_sim::World world;
|
||||
world.max_along = 0.235;
|
||||
auto test_profile = profile();
|
||||
test_profile.wall_min_span = 0.90;
|
||||
const auto result = detect(test_profile, world, wall_scan_sim::geometry(kFrontStep));
|
||||
EXPECT_FALSE(result.valid);
|
||||
EXPECT_EQ(result.reason, "wall_support");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
202
test/wall_scan_sim.h
Normal file
202
test/wall_scan_sim.h
Normal file
@@ -0,0 +1,202 @@
|
||||
#ifndef DOCK_PATTERN_DETECTOR_TEST_WALL_SCAN_SIM_H_
|
||||
#define DOCK_PATTERN_DETECTOR_TEST_WALL_SCAN_SIM_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
#include <robot_line_extraction/types.h>
|
||||
|
||||
namespace wall_scan_sim
|
||||
{
|
||||
struct Interval
|
||||
{
|
||||
double center = 0.0;
|
||||
double width = 0.05;
|
||||
};
|
||||
|
||||
struct Box
|
||||
{
|
||||
double min_x;
|
||||
double max_x;
|
||||
double min_y;
|
||||
double max_y;
|
||||
float intensity = 1000.0f;
|
||||
};
|
||||
|
||||
struct Scan
|
||||
{
|
||||
std::vector<double> ranges;
|
||||
std::vector<float> intensities;
|
||||
};
|
||||
|
||||
struct World
|
||||
{
|
||||
double x = 1.0;
|
||||
double y = 0.0;
|
||||
double normal_yaw = 3.14159265358979323846;
|
||||
double half_length = 2.0;
|
||||
double min_along = -std::numeric_limits<double>::infinity();
|
||||
double max_along = std::numeric_limits<double>::infinity();
|
||||
double step_start = std::numeric_limits<double>::infinity();
|
||||
double step_depth = 0.0;
|
||||
float wall_intensity = 1000.0f;
|
||||
float tape_intensity = 20000.0f;
|
||||
float bloom_intensity = 9000.0f;
|
||||
std::vector<Interval> tapes{{-0.2, 0.05}, {0.2, 0.05}};
|
||||
std::vector<Box> boxes;
|
||||
int bloom_left = 0;
|
||||
int bloom_right = 0;
|
||||
double bright_range_bias = 0.0;
|
||||
double noise_sigma = 0.0;
|
||||
unsigned int seed = 7;
|
||||
};
|
||||
|
||||
inline robot_line_extraction::ScanGeometry geometry(double step, double phase = 0.0,
|
||||
bool reversed = false)
|
||||
{
|
||||
robot_line_extraction::ScanGeometry scan_geometry;
|
||||
scan_geometry.size = static_cast<std::size_t>(std::ceil(2.0 * 3.14159265358979323846 / step));
|
||||
scan_geometry.angle_min = -3.14159265358979323846 + phase;
|
||||
scan_geometry.angle_increment = step;
|
||||
if (reversed)
|
||||
{
|
||||
scan_geometry.angle_min += (scan_geometry.size - 1) * step;
|
||||
scan_geometry.angle_increment = -step;
|
||||
}
|
||||
return scan_geometry;
|
||||
}
|
||||
|
||||
inline Scan rayCast(const World& world, const robot_line_extraction::ScanGeometry& scan_geometry)
|
||||
{
|
||||
Scan scan;
|
||||
const double infinity = std::numeric_limits<double>::infinity();
|
||||
scan.ranges.assign(scan_geometry.size, infinity);
|
||||
scan.intensities.assign(scan_geometry.size, 0.0f);
|
||||
const double normal_x = std::cos(world.normal_yaw);
|
||||
const double normal_y = std::sin(world.normal_yaw);
|
||||
const double tangent_x = -normal_y;
|
||||
const double tangent_y = normal_x;
|
||||
const double rho = normal_x * world.x + normal_y * world.y;
|
||||
std::mt19937 random(world.seed);
|
||||
std::normal_distribution<double> noise(0.0, world.noise_sigma);
|
||||
std::vector<bool> is_tape(scan_geometry.size, false);
|
||||
std::vector<bool> wall_hit(scan_geometry.size, false);
|
||||
for (std::size_t index = 0; index < scan_geometry.size; ++index)
|
||||
{
|
||||
const double angle = scan_geometry.angle_min + index * scan_geometry.angle_increment;
|
||||
const double ray_x = std::cos(angle);
|
||||
const double ray_y = std::sin(angle);
|
||||
const double denominator = normal_x * ray_x + normal_y * ray_y;
|
||||
if (std::abs(denominator) > 1e-12)
|
||||
{
|
||||
double range = rho / denominator;
|
||||
double along = tangent_x * (range * ray_x - world.x) + tangent_y * (range * ray_y - world.y);
|
||||
if (along >= world.step_start)
|
||||
{
|
||||
range = (rho - world.step_depth) / denominator;
|
||||
along = tangent_x * (range * ray_x - world.x) + tangent_y * (range * ray_y - world.y);
|
||||
}
|
||||
if (range > 0.0 && std::abs(along) <= world.half_length &&
|
||||
along >= world.min_along && along <= world.max_along)
|
||||
{
|
||||
scan.ranges[index] = range;
|
||||
wall_hit[index] = true;
|
||||
scan.intensities[index] = world.wall_intensity;
|
||||
for (const auto& tape : world.tapes)
|
||||
{
|
||||
if (std::abs(along - tape.center) <= tape.width / 2.0)
|
||||
{
|
||||
is_tape[index] = true;
|
||||
scan.intensities[index] = world.tape_intensity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Ray casting với hộp chắn, lấy giao điểm gần nhất theo phương pháp slabs.
|
||||
for (const auto& box : world.boxes)
|
||||
{
|
||||
double near = 0.0;
|
||||
double far = infinity;
|
||||
const auto slab = [&near, &far](double component, double minimum, double maximum)
|
||||
{
|
||||
if (std::abs(component) < 1e-12)
|
||||
{
|
||||
return minimum <= 0.0 && maximum >= 0.0;
|
||||
}
|
||||
double first = minimum / component;
|
||||
double last = maximum / component;
|
||||
if (first > last)
|
||||
{
|
||||
std::swap(first, last);
|
||||
}
|
||||
near = std::max(near, first);
|
||||
far = std::min(far, last);
|
||||
return far >= near;
|
||||
};
|
||||
if (slab(ray_x, box.min_x, box.max_x) && slab(ray_y, box.min_y, box.max_y) &&
|
||||
near > 0.0 && near < scan.ranges[index])
|
||||
{
|
||||
scan.ranges[index] = near;
|
||||
scan.intensities[index] = box.intensity;
|
||||
is_tape[index] = false;
|
||||
wall_hit[index] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (std::size_t index = 0; index < scan_geometry.size;)
|
||||
{
|
||||
if (!is_tape[index])
|
||||
{
|
||||
++index;
|
||||
continue;
|
||||
}
|
||||
const std::size_t first = index;
|
||||
while (index < scan_geometry.size && is_tape[index])
|
||||
{
|
||||
++index;
|
||||
}
|
||||
const std::size_t last = index - 1;
|
||||
for (int offset = 1; offset <= world.bloom_left && static_cast<std::size_t>(offset) <= first;
|
||||
++offset)
|
||||
{
|
||||
if (wall_hit[first - offset] && !is_tape[first - offset])
|
||||
{
|
||||
scan.intensities[first - offset] = world.wall_intensity +
|
||||
(world.bloom_intensity - world.wall_intensity) *
|
||||
static_cast<float>(world.bloom_left - offset + 1) / world.bloom_left;
|
||||
}
|
||||
}
|
||||
for (int offset = 1; offset <= world.bloom_right && last + offset < scan_geometry.size;
|
||||
++offset)
|
||||
{
|
||||
if (wall_hit[last + offset] && !is_tape[last + offset])
|
||||
{
|
||||
scan.intensities[last + offset] = world.wall_intensity +
|
||||
(world.bloom_intensity - world.wall_intensity) *
|
||||
static_cast<float>(world.bloom_right - offset + 1) / world.bloom_right;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (std::size_t index = 0; index < scan_geometry.size; ++index)
|
||||
{
|
||||
if (!std::isfinite(scan.ranges[index]))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (is_tape[index])
|
||||
{
|
||||
scan.ranges[index] += world.bright_range_bias;
|
||||
}
|
||||
if (world.noise_sigma > 0.0)
|
||||
{
|
||||
scan.ranges[index] += noise(random);
|
||||
}
|
||||
}
|
||||
return scan;
|
||||
}
|
||||
} // namespace wall_scan_sim
|
||||
#endif // DOCK_PATTERN_DETECTOR_TEST_WALL_SCAN_SIM_H_
|
||||
Reference in New Issue
Block a user