feat(reflector_pair): split multi-core bright runs and allow an empty gap between strips

Narrow strips (3 cm wide, 9 cm between centres) leave a 6 cm wall gap that
bloom can fill, so one bright run may hold both strip cores and the middle
wall group may have no dark rays at all.

- Split a bright run with several cores at the darkest ray between them
  (middle of an equal-minimum region); each core becomes its own candidate.
  Ties were rejected before.
- Compare the middle wall group only when it has at least
  wall_min_points_each_side dark rays; the outer groups are still required.
- Document wall_max_gap / wall_merge_distance for the masked narrow gap.
- Tests: 3/9 cm strips at 0.5-1.5 m, a gap bridged by bloom, an occluder in
  the masked gap, a third strip making the result ambiguous.
This commit is contained in:
2026-11-12 13:44:04 +07:00
parent 7ce7c7db52
commit 95c7df9055
5 changed files with 258 additions and 42 deletions

View File

@@ -270,8 +270,11 @@ số cặp và `reason`. Range 0 không được dùng ở cả hai giá trị `
trong vùng support thì P1 từ chối. Trục +X marker là pháp tuyến tường hướng về cảm biến. Target dùng offset theo
marker; handler sau này cộng π cho hướng forward. Không phát TF hoặc vận tốc.
Tách bright run/core theo hai ngưỡng, loại core thiếu hoặc hòa, mask toàn bộ run và bloom, fit TLS
các điểm tối trong cửa sổ hai bên. Cổng so chênh lệch residual trung bình giữa trái, giữa và phải
Tách bright run/core theo hai ngưỡng, loại core thiếu, mask toàn bộ run và bloom, fit TLS
các điểm tối trong cửa sổ hai bên. Hai dải đặt sát (ví dụ rộng 3 cm, tâm cách 9 cm) có thể bị bloom
nối thành một bright run: run được chia tại tia tối nhất giữa hai lõi, mỗi lõi một candidate với
phần bloom phía nó — `tape_plateau_min_fraction` phải tính cả nửa khe đó. Cổng so chênh lệch residual
trung bình giữa trái và phải (và vùng giữa khi khe còn ít nhất `wall_min_points_each_side` tia tối)
trên cùng đường TLS dùng `wall_merge_distance`; không fit góc riêng trên hai đoạn ngắn. Cửa sổ
dừng trước tia invalid và có thể tiếp tục mở về phía còn support. Cổng residual, gap và support kiểm vật
chắn; các tia mép giao với đường tường, không dùng range sáng làm vị trí. Range sáng chỉ được dùng

View File

@@ -43,7 +43,8 @@ struct ReflectorPairLimits
* - `no_intensity_data`, `intensity_size_mismatch`: thiếu hoặc sai kích thước intensities.
* - `capacity_scan_rays`, `capacity_candidates`, `capacity_pairs`: vượt ngân sách tương ứng.
* - `insufficient_candidates`, `insufficient_candidates_or_core`: thiếu run hoặc core hợp lệ.
* - `wall_support`: thiếu support hai phía, vùng giữa, hoặc span cần thiết.
* - `wall_support`: thiếu support ở một phía ngoài hoặc không đủ span. Khe giữa hai dải được phép
* trống (bloom che kín khe hẹp).
* - `wall_occluded_or_invalid`, `wall_gap_or_occlusion`: tia không hợp lệ hoặc gap trong cửa sổ.
* - `wall_degenerate`, `wall_span`: TLS suy biến hoặc span chiếu trên tường quá ngắn.
* - `wall_residual_or_occlusion`, `wall_not_same_line`: residual hoặc sai lệch nhóm vượt ngưỡng.
@@ -79,7 +80,8 @@ struct ReflectorPairDetection
/**
* @brief Dò hai dải phẳng trên một tường bằng một scan ranges/intensities thô.
*
* Kiểm geometry → phân đoạn run/core và mask bloom → fit TLS tường từ tia tối trong cửa sổ
* Kiểm geometry → phân đoạn run/core và mask bloom (một run chứa nhiều lõi được chia tại tia tối
* nhất giữa hai lõi, mỗi lõi một candidate) → fit TLS tường từ tia tối trong cửa sổ
* đủ support → kiểm residual từng vùng, gap và vật chắn → giao tia mép với tường → kiểm width,
* spacing, prior và ROI → yêu cầu đúng một cặp. Pháp tuyến hướng về sensor; offset tạo target.
* Range sáng chỉ dùng kiểm đồng mặt, không làm tọa độ tường hoặc mép. Không tự lật thứ tự tia;
@@ -137,6 +139,13 @@ private:
std::size_t core_last = 0;
};
/// Một đoạn lõi liên tiếp (>= tape_core_intensity) trong một bright run, chỉ số tia bao hai đầu.
struct CoreSegment
{
std::size_t first = 0;
std::size_t last = 0;
};
struct Line
{
double nx = 0.0;
@@ -156,6 +165,7 @@ private:
bool configured_ = false;
std::vector<Ray> rays_;
std::vector<Candidate> candidates_;
std::vector<CoreSegment> cores_;
std::vector<std::size_t> support_;
std::vector<std::size_t> left_support_;
std::vector<std::size_t> right_support_;

View File

@@ -68,6 +68,8 @@ struct ReflectorPairProfile
/**
* [m] Khoảng trống tối đa giữa hai tia support tối liên tiếp. Mask loại cả dải và bloom, nên
* giá trị phải bao phủ width dải cộng cả hai skirt bloom ở cự ly xa nhất, cộng lượng tử/nhiễu.
* Khi khe giữa hai dải hẹp và bị mask kín, khoảng trống là cả cặp: `tape_spacing + tape_width`
* cộng hai skirt.
* Tăng giúp nối tường qua tape nhưng cũng có thể che khuất một lỗ hoặc vật chắn lớn hơn.
*/
double wall_max_gap = std::numeric_limits<double>::quiet_NaN();
@@ -77,7 +79,9 @@ struct ReflectorPairProfile
/**
* [m] Chênh lệch phần dư trung bình có dấu tối đa giữa các vùng trái/giữa/phải so với TLS chung.
* Tăng chịu nhiễu hơn nhưng có thể gộp hai mặt khác nhau. Cũng góp vào dung sai kiểm range sáng.
* Vùng giữa chỉ được so khi có ít nhất `wall_min_points_each_side` tia tối (khe hẹp bị bloom che
* thì bỏ qua). Tăng chịu nhiễu hơn nhưng có thể gộp hai mặt khác nhau. Cũng góp vào dung sai
* kiểm range sáng.
*/
double wall_merge_distance = std::numeric_limits<double>::quiet_NaN();

View File

@@ -273,19 +273,25 @@ bool ReflectorPairMatcher::evaluatePair(const Candidate& first_candidate, const
++middle_count;
}
}
if (middle_count == 0)
{
return fail("wall_support");
}
left_residual /= static_cast<double>(left_support_.size());
right_residual /= static_cast<double>(right_support_.size());
middle_residual /= static_cast<double>(middle_count);
if (std::abs(left_residual - middle_residual) > profile_.wall_merge_distance ||
std::abs(right_residual - middle_residual) > profile_.wall_merge_distance ||
std::abs(left_residual - right_residual) > profile_.wall_merge_distance)
if (std::abs(left_residual - right_residual) > profile_.wall_merge_distance)
{
return fail("wall_not_same_line");
}
// Khe giữa hai dải có thể rất hẹp (dải 3 cm cách tâm 9 cm chỉ chừa 6 cm) và bloom có thể che kín
// nó. Nhóm giữa chỉ vào cổng đồng mặt khi đủ tia như mỗi phía ngoài: vài tia thì trung bình của
// chúng quá nhiễu để so. Tia bị che trong khe vẫn qua cổng `bright_off_wall` / `wall_occlusion`
// ở trên, nên vật chắn trong khe vẫn bị loại.
if (middle_count >= profile_.wall_min_points_each_side)
{
middle_residual /= static_cast<double>(middle_count);
if (std::abs(left_residual - middle_residual) > profile_.wall_merge_distance ||
std::abs(right_residual - middle_residual) > profile_.wall_merge_distance)
{
return fail("wall_not_same_line");
}
}
const double tangent_x = -wall.ny;
const double tangent_y = wall.nx;
double support_min = std::numeric_limits<double>::infinity();
@@ -482,9 +488,10 @@ ReflectorPairDetection ReflectorPairMatcher::match(
{
rays_[j].masked = true;
}
std::size_t best_start = 0;
std::size_t best_len = 0;
std::size_t ties = 0;
// Mọi đoạn lõi (>= tape_core_intensity) liên tiếp trong run. Hai dải đặt sát nhau (3 cm cách
// tâm 9 cm) có thể bị bloom nối thành MỘT bright run chứa hai lõi; mỗi lõi phải thành một
// candidate riêng, nếu không cặp thật bị loại như một dải "hai lõi".
cores_.clear();
for (std::size_t j = first; j <= last;)
{
if (intensities[j] < profile_.tape_core_intensity)
@@ -497,35 +504,69 @@ ReflectorPairDetection ReflectorPairMatcher::match(
{
++j;
}
const std::size_t length = j - start;
if (length > best_len)
{
best_start = start;
best_len = length;
ties = 1;
}
else if (length == best_len)
{
++ties;
}
cores_.push_back(CoreSegment{start, j - 1});
}
if (best_len < profile_.tape_min_points || ties != 1 ||
static_cast<double>(best_len) / static_cast<double>(last - first + 1) <
profile_.tape_plateau_min_fraction || best_start == 0 ||
best_start + best_len >= geometry.size || !rays_[best_start - 1].valid ||
!rays_[best_start + best_len].valid ||
intensities[best_start - 1] >= profile_.tape_core_intensity ||
intensities[best_start + best_len] >= profile_.tape_core_intensity)
if (cores_.empty())
{
++result.rejected_runs;
continue;
}
if (candidates_.size() >= limits.max_candidates)
// Chia run tại tia tối nhất giữa hai lõi kề nhau: mỗi lõi giữ phần bloom về phía nó. Một dải
// có vết lõm giữa sẽ thành hai lõi hẹp; cổng width ở bước ghép cặp loại chúng.
std::size_t sub_first = first;
for (std::size_t k = 0; k < cores_.size(); ++k)
{
result.candidate_count = candidates_.size() + 1;
return fail("capacity_candidates");
std::size_t sub_last = last;
if (k + 1 < cores_.size())
{
// Tia tối nhất của khe; nhiều tia cùng mức tối (bloom phẳng, bão hòa) thì lấy giữa vùng đó
// để hai lõi chia đều phần bloom, không dồn cả khe cho một bên.
const std::size_t gap_first = cores_[k].last + 1;
const std::size_t gap_last = cores_[k + 1].first - 1;
float darkest = intensities[gap_first];
for (std::size_t j = gap_first; j <= gap_last; ++j)
{
darkest = std::min(darkest, intensities[j]);
}
std::size_t darkest_first = gap_last;
std::size_t darkest_last = gap_first;
for (std::size_t j = gap_first; j <= gap_last; ++j)
{
if (intensities[j] == darkest)
{
darkest_first = std::min(darkest_first, j);
darkest_last = std::max(darkest_last, j);
}
}
sub_last = darkest_first + (darkest_last - darkest_first) / 2;
}
const std::size_t core_first = cores_[k].first;
const std::size_t core_last = cores_[k].last;
const std::size_t core_length = core_last - core_first + 1;
const std::size_t sub_length = sub_last - sub_first + 1;
const bool accepted =
core_length >= profile_.tape_min_points &&
static_cast<double>(core_length) / static_cast<double>(sub_length) >=
profile_.tape_plateau_min_fraction &&
core_first > 0 && core_last + 1 < geometry.size && rays_[core_first - 1].valid &&
rays_[core_last + 1].valid &&
intensities[core_first - 1] < profile_.tape_core_intensity &&
intensities[core_last + 1] < profile_.tape_core_intensity;
if (!accepted)
{
++result.rejected_runs;
}
else
{
if (candidates_.size() >= limits.max_candidates)
{
result.candidate_count = candidates_.size() + 1;
return fail("capacity_candidates");
}
candidates_.push_back(Candidate{sub_first, sub_last, core_first, core_last});
}
sub_first = sub_last + 1;
}
candidates_.push_back(Candidate{first, last, best_start, best_start + best_len - 1});
}
result.candidate_count = candidates_.size();
if (candidates_.size() < 2)

View File

@@ -367,7 +367,8 @@ TEST(ReflectorPairBloom, CoreTiesAndInseparableOrHighUncertaintyReject)
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 ý.
// 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);
@@ -398,9 +399,10 @@ TEST(ReflectorPairBloom, CoreTiesAndInseparableOrHighUncertaintyReject)
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");
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)
@@ -679,6 +681,162 @@ TEST(ReflectorPairWindow, WallEndBeforeEnoughSupportRejects)
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<std::size_t, 2> 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<Case> 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<float>(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<float>(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)