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

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