Files
vslam_cpp/libs/vslam_slam/include/vslam/voxel_export.h
trung09122002 0093df0508 update
2026-09-25 09:04:37 +07:00

142 lines
5.4 KiB
C++

#ifndef VSLAM_VOXEL_EXPORT_H
#define VSLAM_VOXEL_EXPORT_H
// Xuat BAN DO VOXEL 3D tu do thi RTAB-Map, cho khung 3D cua web UI.
//
// Khac occupancy_export.cc o mot cho ban le: o day KHONG dung
// rtabmap::OccupancyGrid. `BuildFromCache` ep Grid/3D="false" ngay trong ham
// (occupancy_export.cc) va do la dung cho duong 2D -- no chieu moi thu xuong
// mot mat phang. Ban do 3D lay DEPTH cua tung node, chieu thanh diem MOT lan
// (cache theo id node), roi moi lan xuat chi bien doi cac diem do bang pose
// DA TOI UU. Khong dung luoi cuc bo 3D cua RTAB-Map: dung no trong process()
// keo pose lech (xem BuildRtabmapParams).
//
// ⚠️ Chinh vi dung pose da toi uu ma ban do 3D tu NAN lai sau moi vong dong:
// khong co buoc "sua ban do" nao ca, ta dung lai tu dau tu do thi moi. Doi
// lai, sau mot lan toi uu thi gan nhu MOI voxel doi chi so -- xem `epoch`
// trong VoxelGrid va luat 35% o phia web.
//
// ⚠️ `data.uncompressData()` la BAT BUOC truoc khi doc depthOrRightRaw().
// Quen no thi moi node tra ve ma tran RONG, va trieu chung la "1990 node, 0 o"
// -- y het cai bay da co trong occupancy_export.h.
#include <cstddef>
#include <cstdint>
#include <unordered_map>
#include <vector>
#include "vslam/vslam_common.h"
namespace rtabmap {
class Rtabmap;
class SensorData;
}
namespace vslam {
struct VoxelExportOptions {
// Kich thuoc o PHAT. Doc lap voi Grid/CellSize (0.05) cua SLAM: lam tho
// them mot bac cat bon lan so voxel ma mat rat it hinh dang.
double emit_size_m = 0.10;
// Cat san va tran. z do tu base_footprint = mat san.
double z_min = 0.05;
double z_max = 2.00;
// Tran SO O PHAT. Cham tran thi GIAM MAU DEU (buoc `stride`), khong cat cut
// -- ban do giu nguyen hinh dang, chi thua di. Xem Finish().
size_t max_cells = 150000;
// Gom bao nhieu khoa thi sort+unique mot lan. Doi bo nho dinh lay so lan
// tron: 2M khoa = 16 MB tam thoi.
size_t flush_every = 2000000;
// Chieu depth cua node thanh diem. Cung dai voi depth_filter (pipeline.yaml
// 0.22-4.0 m): xa hon 4 m nhieu cua D410 tang theo binh phuong.
double depth_min_m = 0.22;
double depth_max_m = 4.0;
// Bang Grid/DepthDecimation (4) ma luoi 3D cu da dung.
int depth_decimation = 4;
// So node MOI toi da doi depth -> diem trong MOT lan xuat. Localization nap
// 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;
};
// Diem cua tung node, o frame CUA NODE, da gom voxel. Tinh MOT lan roi giu:
// depth cua node khong doi, chi pose cua no doi sau moi lan toi uu.
// CHI luong slam cham vao.
struct VoxelNodeCache {
std::unordered_map<int, std::vector<float>> nodes; // id -> x,y,z,x,y,z...
};
// Gom diem -> tap voxel. KHONG chua kieu nao cua RTAB-Map, nen test duoc ma
// khong can mot Rtabmap dang chay -- do la ly do no tach rieng khoi
// ExportVoxelGrid.
class VoxelAccumulator {
public:
explicit VoxelAccumulator(const VoxelExportOptions& opt);
// Toa do trong frame MAP, met.
void AddPoint(double x, double y, double z);
// Sort + unique lan cuoi roi do ra `out`. `out->cells` da sap xep.
void Finish(VoxelGrid* out);
// So diem bi bo vi ra ngoai dai int16. Khac 0 nghia la ban do vuot +-3 km
// hoac resolution bi dat sai.
size_t clamped() const { return clamped_; }
// So diem bi cat vi nam ngoai [z_min, z_max].
size_t z_rejected() const { return z_rejected_; }
// Buoc giam mau da dung o lan Finish() gan nhat. 1 = khong giam.
size_t stride() const { return stride_; }
private:
void Flush();
VoxelExportOptions opt_;
double inv_size_ = 10.0;
std::vector<uint64_t> scratch_;
std::vector<uint64_t> acc_; // luon o trang thai da sort+unique
size_t clamped_ = 0;
size_t z_rejected_ = 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
// chi ho moi biet tap truoc do la gi.
StatusResponse ExportVoxelGrid(rtabmap::Rtabmap& slam,
const VoxelExportOptions& opt,
VoxelNodeCache* cache, VoxelGrid* out);
// Depth trong `data` (da uncompressData) -> diem o frame base cua node, cat
// theo depth_min/max va z_min/z_max, gom theo emit_size_m. Rong neu node
// khong co depth. Tach rieng de test duoc ma khong can Rtabmap.
void NodeCloudFromSensorData(const rtabmap::SensorData& data,
const VoxelExportOptions& opt,
std::vector<float>* out);
// Danh dau o 2D bi chiem duoi moi voxel co tam trong [z_min, z_max] (tinh tu
// mat san). La LOP VAT CAN tu camera cho nhung thu LiDAR (0.22 m) khong cat
// ngang: mat ghe, khung giuong, ke thap. Chi THEM o bi chiem, khong xoa.
// Tra ve so o moi danh dau (truoc do trong/chua biet).
size_t MarkVoxelObstacles(const VoxelGrid& vg, double z_min, double z_max,
OccupancyGrid* grid);
// Hieu hai tap DA SAP XEP: `added` co trong `now` ma khong co trong `prev`,
// `removed` thi nguoc lai. O(n + m), mot luot.
//
// Tach ra de test duoc va de nguoi goi quyet dinh gui delta hay gui lai ca
// tap dua tren ti le thay doi.
void DiffVoxelGrids(const std::vector<VoxelIndex>& prev,
const std::vector<VoxelIndex>& now,
std::vector<VoxelIndex>* added,
std::vector<VoxelIndex>* removed);
} // namespace vslam
#endif // VSLAM_VOXEL_EXPORT_H