reloc: LocalMatch + bench --split-id/--vpr-csv + phan tich ghep VPR/LiDAR; robot that: 85 -> 108 chot dung, 0 sai

This commit is contained in:
trung09122002
2026-09-29 10:26:00 +07:00
parent ca3b7854ea
commit 399a46139e
8 changed files with 549 additions and 92 deletions

View File

@@ -206,6 +206,119 @@ std::vector<Point> DownsamplePoints(const std::vector<Point>& pts, double res,
return out;
}
namespace {
// Luoi tho + dam diem da can tam -- dung chung cho GlobalMatch va LocalMatch.
struct Coarse {
double res = 0.05;
int k = 1; // o tho = k x k o min
double rc = 0.8; // canh o tho, met
int Wc = 0, Hc = 0;
std::vector<float> Lc; // max-pool truong likelihood
std::vector<uint8_t> Fc; // o tho co it nhat mot o min TRONG
Point cen; // tam dam diem (frame odom)
std::vector<Point> P; // diem da tru tam
double step = 0.0; // buoc goc, rad
int n_ang = 0;
int R = 0; // ban kinh histogram do lech, o tho
// Tam cua khoi tho b, met tuong doi goc luoi. O min i co tam i*res.
double BlockCenter(int b) const { return (b * k + (k - 1) / 2.0) * res; }
bool FreeCell(int r, int c) const { return Fc[static_cast<size_t>(r) * Wc + c] != 0; }
};
Coarse PrepareCoarse(const LikelihoodField& field, const std::vector<Point>& pts,
const GlobalLocalizerConfig& cfg) {
Coarse cz;
cz.res = field.resolution();
// Phai la MAX chu khong phai trung binh: muc tho de SANG LOC, bo sot mot
// dinh that o day thi khong con co hoi tinh chinh no nua.
cz.k = std::max(1, static_cast<int>(std::lround(cfg.coarse_res / cz.res)));
cz.rc = cz.res * cz.k;
const int W = field.width(), H = field.height();
cz.Wc = (W + cz.k - 1) / cz.k;
cz.Hc = (H + cz.k - 1) / cz.k;
cz.Lc.assign(static_cast<size_t>(cz.Wc) * cz.Hc, 0.0f);
cz.Fc.assign(static_cast<size_t>(cz.Wc) * cz.Hc, 0);
for (int r = 0; r < H; ++r) {
for (int c = 0; c < W; ++c) {
const size_t ci = static_cast<size_t>(r / cz.k) * cz.Wc + (c / cz.k);
cz.Lc[ci] = std::max(cz.Lc[ci], field.At(r, c));
if (field.Free(r, c)) cz.Fc[ci] = 1;
}
}
// Xoay quanh TAM dam diem cho on dinh so hoc.
for (const Point& p : pts) { cz.cen.x += p.x; cz.cen.y += p.y; }
cz.cen.x /= static_cast<double>(pts.size());
cz.cen.y /= static_cast<double>(pts.size());
cz.P.resize(pts.size());
double maxn = 0.0;
for (size_t i = 0; i < pts.size(); ++i) {
cz.P[i].x = pts[i].x - cz.cen.x;
cz.P[i].y = pts[i].y - cz.cen.y;
maxn = std::max(maxn, std::hypot(cz.P[i].x, cz.P[i].y));
}
cz.step = cfg.angle_step_deg * kDegToRad;
cz.n_ang = std::max(1, static_cast<int>(std::lround(2.0 * M_PI / cz.step)));
cz.R = static_cast<int>(std::ceil(maxn / cz.rc)) + 1;
return cz;
}
struct AnglePeak { double score; int r, c; double th; };
// Diem THO cua goc thu `ia` tai moi o `allowed(r, c)` (o khac = 0).
//
// Ban ROS dung FFT; o day cong truc tiep tren cac do lech DA GOP (histogram do
// lech o tho) -- cung ket qua (khong cuon vong), khong can FFT.
template <class Allowed>
AnglePeak ScoreAngle(const Coarse& cz, int ia, Allowed allowed, std::vector<float>* sc) {
struct Off { int dr, dc; float n; };
const double th = ia * cz.step;
const double ct = std::cos(th), st = std::sin(th);
const int HW = 2 * cz.R + 1;
std::vector<float> hist(static_cast<size_t>(HW) * HW, 0.0f);
for (const Point& p : cz.P) {
const int dc = static_cast<int>(std::lround((ct * p.x - st * p.y) / cz.rc));
const int dr = static_cast<int>(std::lround((st * p.x + ct * p.y) / cz.rc));
hist[static_cast<size_t>(dr + cz.R) * HW + (dc + cz.R)] += 1.0f;
}
std::vector<Off> offs;
for (int dr = -cz.R; dr <= cz.R; ++dr) {
for (int dc = -cz.R; dc <= cz.R; ++dc) {
const float n = hist[static_cast<size_t>(dr + cz.R) * HW + (dc + cz.R)];
if (n > 0.0f) offs.push_back({dr, dc, n});
}
}
sc->assign(static_cast<size_t>(cz.Wc) * cz.Hc, 0.0f);
AnglePeak pk{-1.0, 0, 0, th};
for (int r = 0; r < cz.Hc; ++r) {
for (int c = 0; c < cz.Wc; ++c) {
if (!allowed(r, c)) continue;
double s = 0.0;
for (const Off& o : offs) {
const int rr = r + o.dr, cc = c + o.dc;
if (rr < 0 || cc < 0 || rr >= cz.Hc || cc >= cz.Wc) continue;
s += o.n * cz.Lc[static_cast<size_t>(rr) * cz.Wc + cc];
}
(*sc)[static_cast<size_t>(r) * cz.Wc + c] = static_cast<float>(s);
if (s > pk.score) pk = AnglePeak{s, r, c, th};
}
}
return pk;
}
// (x, y, th) dat TAM dam diem vao cho nao tren luoi. Doi sang T_map_odom:
// p_map = R(th) * p_odom + t, t = (x, y) + goc_luoi - R(th) * tam
Pose2D ToMap(const Refined& rf, const Point& cen, const LikelihoodField& f) {
const double ct = std::cos(rf.th), st = std::sin(rf.th);
return Pose2D{rf.x + f.origin_x() - (ct * cen.x - st * cen.y),
rf.y + f.origin_y() - (st * cen.x + ct * cen.y),
NormalizeAngle(rf.th)};
}
} // namespace
RelocMatch GlobalMatch(const LikelihoodField& field,
const std::vector<Point>& pts_odom,
const GlobalLocalizerConfig& cfg) {
@@ -217,86 +330,25 @@ RelocMatch GlobalMatch(const LikelihoodField& field,
out.points = pts.size();
if (pts.empty()) return out;
// --- Luoi tho: max-pool k x k ---
// Phai la MAX chu khong phai trung binh: muc tho de SANG LOC, bo sot mot
// dinh that o day thi khong con co hoi tinh chinh no nua.
const int k = std::max(1, static_cast<int>(std::lround(cfg.coarse_res / res)));
const double rc = res * k;
const int W = field.width(), H = field.height();
const int Wc = (W + k - 1) / k, Hc = (H + k - 1) / k;
std::vector<float> Lc(static_cast<size_t>(Wc) * Hc, 0.0f);
std::vector<uint8_t> Fc(static_cast<size_t>(Wc) * Hc, 0);
for (int r = 0; r < H; ++r) {
for (int c = 0; c < W; ++c) {
const size_t ci = static_cast<size_t>(r / k) * Wc + (c / k);
Lc[ci] = std::max(Lc[ci], field.At(r, c));
if (field.Free(r, c)) Fc[ci] = 1;
}
}
// Tam cua khoi tho (r, c), met tuong doi goc luoi. O min i co tam i*res.
auto block_center = [&](int b) { return (b * k + (k - 1) / 2.0) * res; };
// Xoay quanh TAM dam diem cho on dinh so hoc.
Point cen;
for (const Point& p : pts) { cen.x += p.x; cen.y += p.y; }
cen.x /= static_cast<double>(pts.size());
cen.y /= static_cast<double>(pts.size());
std::vector<Point> P(pts.size());
double maxn = 0.0;
for (size_t i = 0; i < pts.size(); ++i) {
P[i].x = pts[i].x - cen.x;
P[i].y = pts[i].y - cen.y;
maxn = std::max(maxn, std::hypot(P[i].x, P[i].y));
}
const Coarse cz = PrepareCoarse(field, pts, cfg);
const int k = cz.k;
const double rc = cz.rc;
const int Wc = cz.Wc, Hc = cz.Hc;
const std::vector<Point>& P = cz.P;
const double step = cz.step;
const int n_ang = cz.n_ang;
auto block_center = [&](int b) { return cz.BlockCenter(b); };
// --- Quet tho: MOI goc x MOI o trong ---
// Ban ROS dung FFT; o day cong truc tiep tren cac do lech DA GOP (histogram
// do lech o tho) -- cung ket qua (khong cuon vong), khong can FFT.
const double step = cfg.angle_step_deg * kDegToRad;
const int n_ang = std::max(1, static_cast<int>(std::lround(2.0 * M_PI / step)));
const int R = static_cast<int>(std::ceil(maxn / rc)) + 1;
const int HW = 2 * R + 1;
struct Off { int dr, dc; float n; };
struct AnglePeak { double score; int r, c; double th; };
// ⚠️ Rang buoc "o trong" dat tren TAM dam diem, dung nhu ban ROS
// (`sc[~Fc] = 0` tren luoi dat tam). Dam may 300 do quanh robot co tam gan
// robot nen gan nhu tuong duong.
std::vector<std::vector<float>> scores(n_ang);
std::vector<AnglePeak> peaks;
peaks.reserve(n_ang);
std::vector<float> hist(static_cast<size_t>(HW) * HW);
std::vector<Off> offs;
for (int ia = 0; ia < n_ang; ++ia) {
const double th = ia * step;
const double ct = std::cos(th), st = std::sin(th);
std::fill(hist.begin(), hist.end(), 0.0f);
for (const Point& p : P) {
const int dc = static_cast<int>(std::lround((ct * p.x - st * p.y) / rc));
const int dr = static_cast<int>(std::lround((st * p.x + ct * p.y) / rc));
hist[static_cast<size_t>(dr + R) * HW + (dc + R)] += 1.0f;
}
offs.clear();
for (int dr = -R; dr <= R; ++dr) {
for (int dc = -R; dc <= R; ++dc) {
const float n = hist[static_cast<size_t>(dr + R) * HW + (dc + R)];
if (n > 0.0f) offs.push_back({dr, dc, n});
}
}
std::vector<float>& sc = scores[ia];
sc.assign(static_cast<size_t>(Wc) * Hc, 0.0f);
AnglePeak pk{-1.0, 0, 0, th};
for (int r = 0; r < Hc; ++r) {
for (int c = 0; c < Wc; ++c) {
// Robot phai dung trong vung TRONG.
if (!Fc[static_cast<size_t>(r) * Wc + c]) continue;
double s = 0.0;
for (const Off& o : offs) {
const int rr = r + o.dr, cc = c + o.dc;
if (rr < 0 || cc < 0 || rr >= Hc || cc >= Wc) continue;
s += o.n * Lc[static_cast<size_t>(rr) * Wc + cc];
}
sc[static_cast<size_t>(r) * Wc + c] = static_cast<float>(s);
if (s > pk.score) pk = AnglePeak{s, r, c, th};
}
}
peaks.push_back(pk);
peaks.push_back(ScoreAngle(cz, ia, [&](int r, int c) { return cz.FreeCell(r, c); },
&scores[ia]));
}
std::sort(peaks.begin(), peaks.end(),
[](const AnglePeak& a, const AnglePeak& b) { return a.score > b.score; });
@@ -389,15 +441,7 @@ RelocMatch GlobalMatch(const LikelihoodField& field,
}
}
// (x, y, th) dat TAM dam diem vao cho nao tren luoi. Doi sang T_map_odom:
// p_map = R(th) * p_odom + t, t = (x, y) + goc_luoi - R(th) * tam
auto to_map = [&](const Refined& rf) {
const double ct = std::cos(rf.th), st = std::sin(rf.th);
return Pose2D{rf.x + field.origin_x() - (ct * cen.x - st * cen.y),
rf.y + field.origin_y() - (st * cen.x + ct * cen.y),
NormalizeAngle(rf.th)};
};
for (const Refined& rf : refined) out.hyps.push_back({to_map(rf), rf.score});
for (const Refined& rf : refined) out.hyps.push_back({ToMap(rf, cz.cen, field), rf.score});
out.map_from_odom = out.hyps[0].map_from_odom;
out.s1 = best.score;
out.s2 = s2;
@@ -406,6 +450,58 @@ RelocMatch GlobalMatch(const LikelihoodField& field,
return out;
}
RelocHypothesis LocalMatch(const LikelihoodField& field,
const std::vector<Point>& pts_odom, const Pose2D& odom_now,
const Point& center, double radius,
const GlobalLocalizerConfig& cfg) {
RelocHypothesis out;
out.score = -1.0;
if (field.empty() || pts_odom.empty()) return out;
const std::vector<Point> pts =
DownsamplePoints(pts_odom, field.resolution(), cfg.max_points);
if (pts.empty()) return out;
const Coarse cz = PrepareCoarse(field, pts, cfg);
// Rang buoc dat tren vi tri ROBOT, khong phai tam dam diem: o hanh lang tam
// lech robot nhieu met. Voi goc th, robot roi dung `center` khi tam dat tai
// center - R(th) * (robot_odom - tam)
// (met tuong doi goc luoi). Nua o tho cong them de khong cat hut o bien.
const double bx = odom_now.x - cz.cen.x, by = odom_now.y - cz.cen.y;
const double cx = center.x - field.origin_x(), cy = center.y - field.origin_y();
const double r2 = (radius + 0.5 * cz.rc) * (radius + 0.5 * cz.rc);
std::vector<AnglePeak> peaks;
std::vector<float> sc;
for (int ia = 0; ia < cz.n_ang; ++ia) {
const double th = ia * cz.step;
const double ct = std::cos(th), st = std::sin(th);
const double px = cx - (ct * bx - st * by), py = cy - (st * bx + ct * by);
const AnglePeak pk = ScoreAngle(
cz, ia,
[&](int r, int c) {
if (!cz.FreeCell(r, c)) return false;
const double dx = cz.BlockCenter(c) - px, dy = cz.BlockCenter(r) - py;
return dx * dx + dy * dy <= r2;
},
&sc);
if (pk.score > 0.0) peaks.push_back(pk);
}
if (peaks.empty()) return out;
std::sort(peaks.begin(), peaks.end(),
[](const AnglePeak& a, const AnglePeak& b) { return a.score > b.score; });
// Tinh chinh vai dinh dau (cung ly do nhu GlobalMatch: so o muc tho la sai).
Refined best;
for (size_t i = 0; i < peaks.size() && i < 3; ++i) {
const Refined rf = Refine(field, cz.P, cz.BlockCenter(peaks[i].c),
cz.BlockCenter(peaks[i].r), peaks[i].th, cz.rc, cfg);
if (rf.score > best.score) best = rf;
}
if (best.score < 0.0) return out;
out.map_from_odom = ToMap(best, cz.cen, field);
out.score = best.score;
return out;
}
// ---------------------------------------------------------------------------
// Tich luy qua nhieu vong
// ---------------------------------------------------------------------------

View File

@@ -140,7 +140,8 @@ StatusResponse ExportOccupancyGrid(rtabmap::Rtabmap& slam, OccupancyGrid* out) {
StatusResponse ExportOccupancyGridFromDb(const std::string& db_path,
double cell_size, double range_max,
OccupancyGrid* out) {
OccupancyGrid* out,
const std::set<int>* only_ids) {
std::unique_ptr<rtabmap::DBDriver> driver(rtabmap::DBDriver::create());
bool opened = false;
try {
@@ -153,7 +154,13 @@ StatusResponse ExportOccupancyGridFromDb(const std::string& db_path,
return StatusResponse::Error(StatusCode::kNotFound, "khong mo duoc DB: " + db_path);
}
const std::map<int, rtabmap::Transform> poses = driver->loadOptimizedPoses();
const std::map<int, rtabmap::Transform> all_poses = driver->loadOptimizedPoses();
// Chi node DA DUNG moi duoc dua vao update(): pose thua khong co luoi trong
// cache thi vo hai, nhung de ro rang la ban do dung tu tap nao.
std::map<int, rtabmap::Transform> poses;
for (const auto& kv : all_poses) {
if (only_ids == nullptr || only_ids->count(kv.first) != 0) poses.insert(kv);
}
rtabmap::LocalGridCache cache;
int with_grid = 0;