voxel: chi giu o >= 3 node thay; obstacle_z_min 0.15

This commit is contained in:
trung09122002
2026-09-25 15:57:22 +07:00
parent 185814f0ec
commit ccd6d5954a
7 changed files with 210 additions and 36 deletions

View File

@@ -569,7 +569,7 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
const StatusResponse r =
Reject(v, "slam.yaml voxel",
{"emit_size_m", "z_min", "z_max", "max_cells", "delta_max_ratio",
"obstacle_z_min", "obstacle_z_max"});
"obstacle_z_min", "obstacle_z_max", "min_node_hits"});
if (!r.ok()) return r;
out->voxel.emit_size_m = v.get<double>("emit_size_m", out->voxel.emit_size_m);
@@ -583,6 +583,13 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
v.get<double>("obstacle_z_min", out->voxel.obstacle_z_min);
out->voxel.obstacle_z_max =
v.get<double>("obstacle_z_max", out->voxel.obstacle_z_max);
const int hits = v.get<int>("min_node_hits",
static_cast<int>(out->voxel.min_node_hits));
if (hits < 1) {
return StatusResponse::Error(StatusCode::kInvalidArgument,
"slam.yaml voxel.min_node_hits phai >= 1");
}
out->voxel.min_node_hits = static_cast<size_t>(hits);
if (out->voxel.emit_size_m <= 0.0) {
return StatusResponse::Error(StatusCode::kInvalidArgument,

View File

@@ -61,6 +61,9 @@ struct VoxelConfig {
// tu mat san) bi danh dau la tuong. Xem slam.yaml khoi `voxel:`.
double obstacle_z_min = 0.10;
double obstacle_z_max = 0.55;
// Voxel phai duoc it nhat bay nhieu node khac nhau thay moi giu -- ap cho ca
// ban do 3D lan lop vat can 2D. 1 = giu moi voxel.
size_t min_node_hits = 1;
};
struct ThreadConfig {

View File

@@ -1104,6 +1104,7 @@ void Runtime::SlamLoop() {
vo.z_min = cfg_->voxel.z_min;
vo.z_max = cfg_->voxel.z_max;
vo.max_cells = cfg_->voxel.max_cells;
vo.min_node_hits = cfg_->voxel.min_node_hits;
VoxelGrid vg;
const auto t_vox = std::chrono::steady_clock::now();

View File

@@ -153,13 +153,23 @@ voxel:
# tu mat san) bi danh dau la tuong. Bat nhung thu LiDAR (0.2165 m) khong
# cat ngang: mat ghe, khung giuong, ke thap. Lam NGOAI RTAB-Map -- dua depth
# vao Grid/Sensor keo pose lech (do 2026-09-24: sai so TB 0.020 -> 0.052 m).
# 0.10: duoi day coi la san (nguong cua, day cap, nhieu gravity_align)
# 0.15: duoi day coi la san (nguong cua, day cap, nhieu depth + gravity_align)
# 0.55: noc robot ~0.46 m (mat ga camera 0.405 m tu san + camera) + ~9 cm
# du. ⚠️ Gan them do gi CAO hon camera len robot thi PHAI nang so nay.
# Chi THEM o bi chiem, khong xoa: vat bi doi di trong luc quet de lai vet.
obstacle_z_min: 0.10
#
# 0.15 (khong phai 0.10): san do tren robot that lech chuan 3.6 cm quanh 0
# (600 khung depth, 2026-09-25) -> 0.15 la ~4 sigma, nhieu san khong lot vao.
obstacle_z_min: 0.15
obstacle_z_max: 0.55
# Voxel phai duoc >= bay nhieu NODE khac nhau thay moi giu -- ap cho CA ban
# do 3D lan lop vat can 2D. Robot that 2026-09-25: camera gop 2048 o vat
# can, NHIEU HON 1379 o tuong cua LiDAR -- phan lon la diem bay o mep vat
# va nguoi di ngang, chi o 1-2 khung, ma lop nay khong bao gio xoa.
# Tuong va ban ghe dung yen thi nhieu node thay nen van giu.
min_node_hits: 3
# ============ KHOI RTAB-MAP ============
# Ghi de len mac dinh cua du an (xem BuildRtabmapParams). De trong thi dung
# mac dinh -- do la lua chon dung cho hau het truong hop.

View File

@@ -61,6 +61,10 @@ struct VoxelExportOptions {
// ca nghin node mot luc; khong gioi han thi lan xuat dau chan luong slam
// hang chuc giay. Ban do day dan qua cac lan xuat.
size_t max_new_nodes = 40;
// Voxel phai duoc it nhat bay nhieu NODE KHAC NHAU thay moi giu. Diem bay,
// nhieu depth va nguoi di ngang chi o 1-2 khung; tuong, ban ghe dung yen
// thi nhieu node thay. 1 = giu moi voxel (hanh vi cu).
size_t min_node_hits = 1;
};
// Diem cua tung node, o frame CUA NODE, da gom voxel. Tinh MOT lan roi giu:
@@ -104,6 +108,35 @@ class VoxelAccumulator {
size_t stride_ = 1;
};
// Dem so node KHAC NHAU thay moi voxel. Moi node dong gop mot voxel toi da
// MOT lan, du co bao nhieu diem roi vao do. Khong chua kieu RTAB-Map nao --
// test duoc nhu VoxelAccumulator.
class VoxelHitCounter {
public:
explicit VoxelHitCounter(const VoxelExportOptions& opt);
void BeginNode();
// Toa do trong frame MAP, met. Cung luat cat z / dai chi so voi
// VoxelAccumulator::AddPoint.
void AddPoint(double x, double y, double z);
void EndNode();
// Voxel co >= min_node_hits node -> dua qua VoxelAccumulator: giu nguyen
// luat giam mau khi cham tran, dau ra da sort.
void Finish(VoxelGrid* out);
size_t clamped() const { return clamped_; }
size_t stride() const { return stride_; }
private:
VoxelExportOptions opt_;
double inv_size_ = 10.0;
std::vector<uint64_t> node_; // khoa cua node dang gom
std::vector<uint64_t> all_; // khoa DA khu trung theo node, cua moi node
size_t clamped_ = 0;
size_t stride_ = 1;
};
// Quet toan bo pose da toi uu va dung lai tap voxel tu dau.
//
// `out->epoch` KHONG duoc dat o day -- nguoi goi quan ly vong doi epoch, vi

View File

@@ -52,6 +52,34 @@ inline uint64_t KeyOf(const VoxelIndex& k) {
return PackKey(k.x, k.y, k.z);
}
// Diem frame map -> khoa voxel. Dung CHUNG cho VoxelAccumulator va
// VoxelHitCounter, de hai noi luong tu hoa y het nhau.
// 0 = nhan, -1 = khong huu han, 1 = ngoai dai z, 2 = ngoai dai chi so.
inline int KeyForPoint(const VoxelExportOptions& opt, double inv_size,
double x, double y, double z, uint64_t* key) {
if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(z)) return -1;
if (z < opt.z_min || z > opt.z_max) return 1;
const double fx = std::floor(x * inv_size);
const double fy = std::floor(y * inv_size);
const double fz = std::floor(z * inv_size);
// Kiem TRUOC khi ep kieu: ep mot double khong lot int32 la hanh vi khong
// xac dinh, va o day no den tu du lieu cam bien chu khong tu hang so.
if (fx < kIdxMin || fx > kIdxMax || fy < kIdxMin || fy > kIdxMax ||
fz < kIdxMin || fz > kIdxMax) {
return 2;
}
const int32_t ix = static_cast<int32_t>(fx);
const int32_t iy = static_cast<int32_t>(fy);
const int32_t iz = static_cast<int32_t>(fz);
// Dang truyen la int16 -> chi so ngoai dai do la bo, khong phai kep: kep se
// dan mot buc tuong gia o ria ban do.
if (!FitsInt16(ix) || !FitsInt16(iy) || !FitsInt16(iz)) return 2;
*key = PackKey(ix, iy, iz);
return 0;
}
} // namespace
VoxelAccumulator::VoxelAccumulator(const VoxelExportOptions& opt) : opt_(opt) {
@@ -61,33 +89,14 @@ VoxelAccumulator::VoxelAccumulator(const VoxelExportOptions& opt) : opt_(opt) {
}
void VoxelAccumulator::AddPoint(double x, double y, double z) {
if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(z)) return;
if (z < opt_.z_min || z > opt_.z_max) {
++z_rejected_;
return;
uint64_t key = 0;
switch (KeyForPoint(opt_, inv_size_, x, y, z, &key)) {
case 0: break;
case 1: ++z_rejected_; return;
case 2: ++clamped_; return;
default: return;
}
const double fx = std::floor(x * inv_size_);
const double fy = std::floor(y * inv_size_);
const double fz = std::floor(z * inv_size_);
// Kiem TRUOC khi ep kieu: ep mot double khong lot int32 la hanh vi khong
// xac dinh, va o day no den tu du lieu cam bien chu khong tu hang so.
if (fx < kIdxMin || fx > kIdxMax || fy < kIdxMin || fy > kIdxMax ||
fz < kIdxMin || fz > kIdxMax) {
++clamped_;
return;
}
const int32_t ix = static_cast<int32_t>(fx);
const int32_t iy = static_cast<int32_t>(fy);
const int32_t iz = static_cast<int32_t>(fz);
// Dang truyen la int16 -> chi so ngoai dai do la bo, khong phai kep: kep se
// dan mot buc tuong gia o ria ban do.
if (!FitsInt16(ix) || !FitsInt16(iy) || !FitsInt16(iz)) {
++clamped_;
return;
}
scratch_.push_back(PackKey(ix, iy, iz));
scratch_.push_back(key);
if (scratch_.size() >= opt_.flush_every) Flush();
}
@@ -149,6 +158,54 @@ void VoxelAccumulator::Finish(VoxelGrid* out) {
// (x, y, z) nho phep lech +2^20 -- nen out->cells cung da sap xep.
}
VoxelHitCounter::VoxelHitCounter(const VoxelExportOptions& opt) : opt_(opt) {
if (opt_.emit_size_m <= 0.0) opt_.emit_size_m = 0.10;
inv_size_ = 1.0 / opt_.emit_size_m;
}
void VoxelHitCounter::BeginNode() { node_.clear(); }
void VoxelHitCounter::AddPoint(double x, double y, double z) {
uint64_t key = 0;
const int r = KeyForPoint(opt_, inv_size_, x, y, z, &key);
if (r == 0) node_.push_back(key);
else if (r == 2) ++clamped_;
}
void VoxelHitCounter::EndNode() {
// Khu trung TRONG node: nhieu diem cua cung mot node roi vao mot voxel chi
// la mot lan "thay".
std::sort(node_.begin(), node_.end());
node_.erase(std::unique(node_.begin(), node_.end()), node_.end());
all_.insert(all_.end(), node_.begin(), node_.end());
node_.clear();
}
void VoxelHitCounter::Finish(VoxelGrid* out) {
std::sort(all_.begin(), all_.end());
// Cat z da lam o AddPoint. Tat no o day: tam voxel co the roi ra ngoai
// [z_min, z_max] khi z_min khong trung bien voxel, va voxel se mat oan.
VoxelExportOptions o = opt_;
o.z_min = -1e9;
o.z_max = 1e9;
VoxelAccumulator acc(o);
const size_t need = std::max<size_t>(opt_.min_node_hits, 1);
const double s = opt_.emit_size_m;
for (size_t i = 0; i < all_.size();) {
size_t j = i + 1;
while (j < all_.size() && all_[j] == all_[i]) ++j;
if (j - i >= need) {
int32_t x = 0, y = 0, z = 0;
UnpackKey(all_[i], &x, &y, &z);
// Tam voxel -> luong tu hoa lai ra DUNG chi so do.
acc.AddPoint((x + 0.5) * s, (y + 0.5) * s, (z + 0.5) * s);
}
i = j;
}
acc.Finish(out);
stride_ = acc.stride();
}
void DiffVoxelGrids(const std::vector<VoxelIndex>& prev,
const std::vector<VoxelIndex>& now,
std::vector<VoxelIndex>* added,
@@ -241,7 +298,11 @@ StatusResponse ExportVoxelGrid(rtabmap::Rtabmap& slam,
it = poses.count(it->first) ? std::next(it) : cache->nodes.erase(it);
}
// min_node_hits > 1: dem so node thay moi voxel (loc diem bay, nguoi di
// ngang). = 1: duong cu, re hon.
const bool count_hits = opt.min_node_hits > 1;
VoxelAccumulator acc(opt);
VoxelHitCounter hits(opt);
size_t nodes_used = 0, fetched = 0, pending = 0, no_points = 0;
for (const auto& kv : poses) {
@@ -275,12 +336,16 @@ StatusResponse ExportVoxelGrid(rtabmap::Rtabmap& slam,
const float r10 = T.r21(), r11 = T.r22(), r12 = T.r23(), ty = T.y();
const float r20 = T.r31(), r21 = T.r32(), r22 = T.r33(), tz = T.z();
if (count_hits) hits.BeginNode();
for (size_t i = 0; i + 2 < pts.size(); i += 3) {
const float px = pts[i], py = pts[i + 1], pz = pts[i + 2];
acc.AddPoint(r00 * px + r01 * py + r02 * pz + tx,
r10 * px + r11 * py + r12 * pz + ty,
r20 * px + r21 * py + r22 * pz + tz);
const double wx = r00 * px + r01 * py + r02 * pz + tx;
const double wy = r10 * px + r11 * py + r12 * pz + ty;
const double wz = r20 * px + r21 * py + r22 * pz + tz;
if (count_hits) hits.AddPoint(wx, wy, wz);
else acc.AddPoint(wx, wy, wz);
}
if (count_hits) hits.EndNode();
++nodes_used;
}
@@ -292,20 +357,26 @@ StatusResponse ExportVoxelGrid(rtabmap::Rtabmap& slam,
"hoac khong co camera");
}
acc.Finish(out);
if (count_hits) hits.Finish(out);
else acc.Finish(out);
const size_t stride = count_hits ? hits.stride() : acc.stride();
const size_t clamped = count_hits ? hits.clamped() : acc.clamped();
// Mot dong, mot lan moi 30 giay. `khong co diem` cao bat thuong nghia la
// getNodeData khong tra depth -- thu dau tien can nhin khi ban do 3D rong.
VSLAM_INFO_THROTTLE(30.0, "slam: voxel " << out->cells.size() << " o tu "
<< nodes_used << " node (dang nap " << pending << ", "
<< no_points << " node khong co diem"
<< (count_hits ? ", loc >= " + std::to_string(opt.min_node_hits) +
" node thay"
: std::string())
<< (out->truncated
? ", GIAM MAU 1/" + std::to_string(acc.stride()) +
? ", GIAM MAU 1/" + std::to_string(stride) +
" -- cham tran max_cells"
: std::string())
<< ")");
if (acc.clamped() > 0) {
VSLAM_WARN_THROTTLE(30.0, "slam: " << acc.clamped()
if (clamped > 0) {
VSLAM_WARN_THROTTLE(30.0, "slam: " << clamped
<< " diem ngoai dai chi so voxel -- bo qua");
}
return StatusResponse::Ok();

View File

@@ -333,3 +333,52 @@ TEST(NodeCloud, KhongCoDepthThiRong) {
NodeCloudFromSensorData(data, Opt(0.05), &pts);
EXPECT_TRUE(pts.empty());
}
TEST(VoxelHitCounter, ChiGiuVoxelDuNodeThay) {
// Voxel (0,0,5) duoc 3 node thay -> giu. Voxel (10,0,5) chi node dau thay
// -- kieu mot diem bay -> bo.
VoxelExportOptions o = Opt(0.10);
o.min_node_hits = 3;
VoxelHitCounter h(o);
for (int n = 0; n < 3; ++n) {
h.BeginNode();
h.AddPoint(0.05, 0.05, 0.55);
if (n == 0) h.AddPoint(1.05, 0.05, 0.55);
h.EndNode();
}
VoxelGrid g;
h.Finish(&g);
ASSERT_EQ(g.cells.size(), 1u);
EXPECT_TRUE(Has(g.cells, 0, 0, 5));
}
TEST(VoxelHitCounter, MotNodeNhieuDiemVanChiTinhMotLan) {
// 10 diem cung mot voxel nhung chi tu MOT node -> van la 1 lan thay.
VoxelExportOptions o = Opt(0.10);
o.min_node_hits = 2;
VoxelHitCounter h(o);
h.BeginNode();
for (int i = 0; i < 10; ++i) h.AddPoint(0.05, 0.05, 0.55);
h.EndNode();
VoxelGrid g;
h.Finish(&g);
EXPECT_TRUE(g.cells.empty());
}
TEST(VoxelHitCounter, CatZVanDungNhuVoxelAccumulator) {
// Diem ngoai [z_min, z_max] bi bo ngay tu AddPoint, du duoc nhieu node thay.
VoxelExportOptions o = Opt(0.10); // z_min 0.05, z_max 2.00
o.min_node_hits = 2;
VoxelHitCounter h(o);
for (int n = 0; n < 2; ++n) {
h.BeginNode();
h.AddPoint(0.05, 0.05, 0.01); // duoi z_min
h.AddPoint(0.05, 0.05, 2.50); // tren z_max
h.AddPoint(0.05, 0.05, 1.05); // trong dai
h.EndNode();
}
VoxelGrid g;
h.Finish(&g);
ASSERT_EQ(g.cells.size(), 1u);
EXPECT_TRUE(Has(g.cells, 0, 0, 10));
}