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

@@ -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)