#include #include #include #include #include #include #include #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 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 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(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(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::quiet_NaN(); const std::vector> 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_bloom_max_angle", &ReflectorPairProfile::tape_bloom_max_angle}, {"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::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, OptionalThresholdsAcceptNanAndRejectInvalidValues) { const double nan = std::numeric_limits::quiet_NaN(); const double inf = std::numeric_limits::infinity(); std::string error; auto test_profile = profile(); test_profile.tape_core_intensity = nan; test_profile.tape_plateau_min_fraction = nan; EXPECT_TRUE(test_profile.validate(error)) << error; for (double bad : {inf, -inf, test_profile.tape_min_intensity - 1.0}) { test_profile = profile(); test_profile.tape_core_intensity = bad; EXPECT_FALSE(test_profile.validate(error)); EXPECT_NE(error.find("tape_core_intensity"), std::string::npos) << error; } for (double bad : {inf, -inf, -0.1, 0.0, 1.01}) { test_profile = profile(); test_profile.tape_plateau_min_fraction = bad; EXPECT_FALSE(test_profile.validate(error)); EXPECT_NE(error.find("tape_plateau_min_fraction"), std::string::npos) << error; } } TEST(ReflectorPairProfile, UnsetCoreMatchesExplicitMinimum) { auto explicit_profile = profile(); explicit_profile.tape_core_intensity = explicit_profile.tape_min_intensity; auto default_profile = explicit_profile; default_profile.tape_core_intensity = std::numeric_limits::quiet_NaN(); ReflectorPairMatcher matcher; std::string error; ASSERT_TRUE(matcher.configure(default_profile, error)) << error; EXPECT_EQ(matcher.profile().tape_core_intensity, explicit_profile.tape_min_intensity); const wall_scan_sim::World world; const auto geometry = wall_scan_sim::geometry(kFrontStep); const auto expected = detect(explicit_profile, world, geometry); const auto actual = detect(default_profile, world, geometry); ASSERT_TRUE(expected.valid) << expected.reason; ASSERT_TRUE(actual.valid) << actual.reason; EXPECT_EQ(actual.marker.x, expected.marker.x); EXPECT_EQ(actual.marker.y, expected.marker.y); EXPECT_EQ(actual.marker.yaw, expected.marker.yaw); EXPECT_EQ(actual.target.x, expected.target.x); EXPECT_EQ(actual.target.y, expected.target.y); EXPECT_EQ(actual.target.yaw, expected.target.yaw); EXPECT_EQ(actual.widths, expected.widths); EXPECT_EQ(actual.spacing, expected.spacing); EXPECT_EQ(actual.wall_rms, expected.wall_rms); EXPECT_EQ(actual.wall_max_residual, expected.wall_max_residual); EXPECT_EQ(actual.wall_support, expected.wall_support); EXPECT_EQ(actual.edge_uncertainty, expected.edge_uncertainty); EXPECT_EQ(actual.candidate_count, expected.candidate_count); EXPECT_EQ(actual.valid_pairs, expected.valid_pairs); EXPECT_EQ(actual.tested_pairs, expected.tested_pairs); EXPECT_EQ(actual.rejected_runs, expected.rejected_runs); EXPECT_EQ(actual.reason, expected.reason); } TEST(ReflectorPairProfile, UnsetPlateauUsesOneAndRejectsBloomOutsideCore) { wall_scan_sim::World world; world.x = 1.2; world.bloom_left = 1; world.bloom_right = 2; auto explicit_profile = profile(); explicit_profile.tape_bloom_max_angle = 2 * kFrontStep; explicit_profile.tape_plateau_min_fraction = 1.0; auto default_profile = explicit_profile; default_profile.tape_plateau_min_fraction = std::numeric_limits::quiet_NaN(); ReflectorPairMatcher matcher; std::string error; ASSERT_TRUE(matcher.configure(default_profile, error)) << error; EXPECT_EQ(matcher.profile().tape_plateau_min_fraction, 1.0); const auto geometry = wall_scan_sim::geometry(kFrontStep); const auto expected = detect(explicit_profile, world, geometry); const auto actual = detect(default_profile, world, geometry); EXPECT_FALSE(expected.valid); EXPECT_FALSE(actual.valid); EXPECT_EQ(actual.reason, expected.reason); EXPECT_EQ(actual.candidate_count, expected.candidate_count); EXPECT_EQ(actual.rejected_runs, expected.rejected_runs); EXPECT_GT(actual.rejected_runs, 0u); explicit_profile.tape_plateau_min_fraction = 0.45; const auto relaxed = detect(explicit_profile, world, geometry); EXPECT_TRUE(relaxed.valid) << relaxed.reason; } 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::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"); // Một dải có vết lõm giữa: run bị chia thành hai lõi hẹp. Không được nhận thành một dải đủ rộng, // cũng không được ghép hai nửa với nhau hay với dải kia. 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 split = matcher.match(geometry,scan.ranges,scan.intensities,{},{}); EXPECT_FALSE(split.valid); EXPECT_EQ(split.valid_pairs, 0u); EXPECT_NE(split.reason, "ambiguous"); } 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::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::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::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::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::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::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"); } // Hình học người dùng đang thử (12/11/2026): dải rộng ~3 cm, tâm cách 9 cm, khe tường giữa 6 cm. // Dung sai width/spacing = 2 × cận lượng tử một mép tại đúng cự ly/góc của ca (mỗi mép lệch tối đa // một nửa bước chiếu lên tường, width và spacing cộng hai mép). constexpr double kS300SimStep = 0.0058824; ReflectorPairProfile narrowProfile(double edge_bound) { ReflectorPairProfile test_profile = profile(); test_profile.tape_width = 0.03; test_profile.tape_spacing = 0.09; test_profile.width_tolerance = std::max(2.0 * edge_bound, 0.002); test_profile.spacing_tolerance = std::max(2.0 * edge_bound, 0.002); test_profile.wall_min_span = 0.30; test_profile.wall_max_gap = 0.08; return test_profile; } wall_scan_sim::World narrowWorld(double distance, double angle_degrees) { wall_scan_sim::World world; world.x = distance; world.normal_yaw = kPi + angle_degrees * kPi / 180.0; world.tapes = {{-0.045, 0.03}, {0.045, 0.03}}; return world; } // Phủ khe giữa hai lõi bằng bloom phẳng (>= tape_min_intensity, < tape_core_intensity): hai dải // thành MỘT bright run. Trả về chỉ số tia đầu và cuối của khe đã phủ. std::array bridgeGapWithBloom(wall_scan_sim::Scan& scan, float core, float bloom) { std::size_t first_core_end = 0; std::size_t second_core_start = 0; bool in_first = false; bool first_done = false; for (std::size_t index = 0; index < scan.intensities.size(); ++index) { const bool is_core = scan.intensities[index] >= core; if (!first_done) { if (is_core) { in_first = true; first_core_end = index; } else if (in_first) { first_done = true; } } else if (is_core) { second_core_start = index; break; } } for (std::size_t index = first_core_end + 1; index < second_core_start; ++index) { scan.intensities[index] = bloom; } return {{first_core_end + 1, second_core_start - 1}}; } TEST(ReflectorPairNarrow, ThreeByNineCentimetreAcrossRangesAnglesAndPhases) { struct Case { double step; double distance; double angle_degrees; }; // S300 sim ở 1.5 m nhìn xiên 30° chỉ còn ~2.9 tia trên dải (< tape_min_points 3), nên không thử. const std::vector cases = { {kFrontStep, 0.5, 0.0}, {kFrontStep, 0.5, 30.0}, {kFrontStep, 1.0, 0.0}, {kFrontStep, 1.0, 30.0}, {kFrontStep, 1.5, 0.0}, {kFrontStep, 1.5, 30.0}, {kS300SimStep, 0.5, 0.0}, {kS300SimStep, 0.5, 30.0}, {kS300SimStep, 1.0, 0.0}, {kS300SimStep, 1.0, 30.0}, {kS300SimStep, 1.5, 0.0}}; for (const Case& test_case : cases) { for (const double phase : {0.0, 0.25, 0.5, 0.75}) { const auto world = narrowWorld(test_case.distance, test_case.angle_degrees); const double edge_bound = quantisationBound(world, test_case.step); const auto geometry = wall_scan_sim::geometry(test_case.step, phase * test_case.step); const auto result = detect(narrowProfile(edge_bound), world, geometry); ASSERT_TRUE(result.valid) << test_case.step << ' ' << test_case.distance << ' ' << test_case.angle_degrees << ' ' << phase << ' ' << result.reason; EXPECT_LE(std::hypot(result.marker.x - world.x, result.marker.y - world.y), edge_bound); EXPECT_LT(yawError(result.marker.yaw, world.normal_yaw), 1e-6); EXPECT_LE(std::abs(result.spacing - 0.09), 2.0 * edge_bound); EXPECT_LE(std::abs(result.widths[0] - 0.03), 2.0 * edge_bound); EXPECT_LE(std::abs(result.widths[1] - 0.03), 2.0 * edge_bound); } } } TEST(ReflectorPairNarrow, BloomBridgingTheGapIntoOneRunIsSplitAndDetected) { const auto world = narrowWorld(1.0, 0.0); const auto geometry = wall_scan_sim::geometry(kFrontStep); auto scan = wall_scan_sim::rayCast(world, geometry); const double edge_bound = quantisationBound(world, kFrontStep); auto test_profile = narrowProfile(edge_bound); bridgeGapWithBloom(scan, static_cast(test_profile.tape_core_intensity), 9000.0f); // Khe bị mask kín: support chỉ còn hai phía ngoài, khoảng trống là cả cặp cộng hai skirt. test_profile.tape_bloom_max_angle = 2.0 * kFrontStep; test_profile.wall_max_gap = 0.20; // Mỗi lõi (~7.6 tia) nhận thêm nửa khe (~7.6 tia) bloom: tỉ lệ lõi/run ~0.5. test_profile.tape_plateau_min_fraction = 0.4; ReflectorPairMatcher matcher; std::string error; ASSERT_TRUE(matcher.configure(test_profile, error)) << error; const auto result = matcher.match(geometry, scan.ranges, scan.intensities, {}, {}); ASSERT_TRUE(result.valid) << result.reason; EXPECT_EQ(result.candidate_count, 2u); EXPECT_LE(std::hypot(result.marker.x - world.x, result.marker.y - world.y), edge_bound); EXPECT_LE(std::abs(result.spacing - 0.09), 2.0 * edge_bound); } TEST(ReflectorPairNarrow, OccluderInsideMaskedGapStillRejects) { const auto world = narrowWorld(1.0, 0.0); const auto geometry = wall_scan_sim::geometry(kFrontStep); auto scan = wall_scan_sim::rayCast(world, geometry); auto test_profile = narrowProfile(quantisationBound(world, kFrontStep)); const auto gap = bridgeGapWithBloom(scan, static_cast(test_profile.tape_core_intensity), 9000.0f); // Ba tia giữa khe là một vật tối nằm trước tường 5 cm; skirt hai bên mask cả ba tia đó. const std::size_t middle = (gap[0] + gap[1]) / 2; for (std::size_t index = middle - 1; index <= middle + 1; ++index) { scan.intensities[index] = 1000.0f; scan.ranges[index] -= 0.05; } test_profile.tape_bloom_max_angle = 2.0 * kFrontStep; test_profile.wall_max_gap = 0.20; test_profile.tape_plateau_min_fraction = 0.4; ReflectorPairMatcher matcher; std::string error; ASSERT_TRUE(matcher.configure(test_profile, error)) << error; const auto result = matcher.match(geometry, scan.ranges, scan.intensities, {}, {}); EXPECT_FALSE(result.valid); EXPECT_EQ(result.reason, "wall_occlusion"); } TEST(ReflectorPairNarrow, ThirdStripAtNineCentimetresIsAmbiguous) { auto world = narrowWorld(1.0, 0.0); world.tapes = {{-0.09, 0.03}, {0.0, 0.03}, {0.09, 0.03}}; const auto result = detect(narrowProfile(quantisationBound(world, kFrontStep)), world, wall_scan_sim::geometry(kFrontStep)); EXPECT_FALSE(result.valid); EXPECT_EQ(result.reason, "ambiguous"); EXPECT_EQ(result.valid_pairs, 2u); } } // namespace int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }