294 lines
12 KiB
C++
294 lines
12 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 <map>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
#include "vslam/height_map.h"
|
|
#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;
|
|
// 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;
|
|
|
|
// Di tren doc: z cua tung node (do thi z) + mat san. Co ca hai thi diem dat
|
|
// o z THAT (z node + z cuc bo), va dai z_min..z_max tinh tu SAN tai cho diem
|
|
// do (mat doc khong lot vao ban do nhu vat can). Null = duong cu (moi node
|
|
// o z = 0, dai tinh tu san duoi node).
|
|
const std::map<int, double>* node_z = nullptr;
|
|
const FloorModel* floor = nullptr;
|
|
// Che do doc: thu cac o 2D (kich thuoc emit_size_m, khoa PackFloorCell) noi
|
|
// camera THAY MAT SAN (|cao - san| <= floor_band). Dung cho
|
|
// ClearLidarOnSeenRamps. Null = khong thu.
|
|
std::vector<uint64_t>* floor_cells = nullptr;
|
|
double floor_band = 0.05;
|
|
|
|
// "Lan nhin MOI NHAT quyet dinh" (07/10): voxel ma >= clear_after_views node
|
|
// SAU lan thay cuoi nhin XUYEN qua no (depth xa hon voxel + sai so depth),
|
|
// va khong node nao trong so do con thay -> XOA. Nguoi dung tren duong luc
|
|
// quet roi di -> quay lai quet la mat. 0 = tat: chi them, khong bao gio xoa
|
|
// (hanh vi truoc 07/10). Can min_node_hits >= 1 bat ky -- tu bat dem node.
|
|
size_t clear_after_views = 0;
|
|
// Chi hoi node co camera trong ban kinh nay (m): xa hon depth nhieu.
|
|
double clear_range_m = 3.5;
|
|
};
|
|
|
|
// Khoa o 2D (ix, iy) cua floor_cells.
|
|
inline uint64_t PackFloorCell(int32_t ix, int32_t iy) {
|
|
return (static_cast<uint64_t>(static_cast<uint32_t>(ix)) << 32) | static_cast<uint32_t>(iy);
|
|
}
|
|
|
|
// 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.
|
|
//
|
|
// Anh depth NHO cua mot node + camera cua no, de hoi "node nay co nhin XUYEN
|
|
// qua diem X khong" (VoxelCarver). Moi o lay depth NHO NHAT cua khoi pixel:
|
|
// than trong -- mot chan nguoi chiem vai pixel van lam o do "thay vat", khong
|
|
// thanh "xuyen qua".
|
|
struct NodeDepthView {
|
|
int w = 0, h = 0;
|
|
float fx = 0.0f, fy = 0.0f, cx = 0.0f, cy = 0.0f; // theo anh NHO
|
|
float cam[12] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}; // base <- optical, 3x4 theo hang
|
|
std::vector<uint16_t> depth_mm; // 0 = khong do duoc
|
|
bool empty() const { return depth_mm.empty(); }
|
|
};
|
|
|
|
struct VoxelNodeCache {
|
|
struct Node {
|
|
std::vector<float> pts; // x,y,z,x,y,z... frame node, da gom voxel
|
|
NodeDepthView view; // chi khi clear_after_views > 0
|
|
};
|
|
std::unordered_map<int, Node> nodes;
|
|
// Diem node da cat theo dai z RONG (che do doc). Doi che do thi xoa cache.
|
|
bool wide = false;
|
|
// Cache co anh depth nho cua node chua. Doi tuy chon thi xoa cache.
|
|
bool views = false;
|
|
};
|
|
|
|
// Anh depth nho cua node tu SensorData (depth + camera model dau tien).
|
|
// `block`: canh khoi pixel. Khong co depth -> `out` rong.
|
|
void NodeDepthViewFromSensorData(const rtabmap::SensorData& data, int block,
|
|
NodeDepthView* out);
|
|
|
|
// "Nhin xuyen qua" (07/10): hoi cac node SAU lan cuoi mot voxel duoc thay xem
|
|
// vat do CON khong. Xet node MOI NHAT truoc -- lan nhin moi nhat quyet dinh:
|
|
// depth tai cho voxel ~ khoang cach toi voxel -> van con, GIU;
|
|
// depth XA hon voxel + sai so -> nhin xuyen qua;
|
|
// depth GAN hon -> bi che, khong biet, bo qua.
|
|
// Khong chua kieu RTAB-Map -- test duoc nhu VoxelAccumulator.
|
|
class VoxelCarver {
|
|
public:
|
|
VoxelCarver(double range_m, size_t need_views);
|
|
|
|
// `order` TANG dan theo thoi gian. base_to_map: pose node da toi uu, 3x4 theo
|
|
// hang (z da cong z node neu co). `view` phai song toi khi xong Gone().
|
|
void AddView(int order, const float base_to_map[12], const NodeDepthView* view);
|
|
|
|
// true = vat da roi di: >= need lan xuyen qua tu cac node sau `last_order`
|
|
// truoc khi gap mot node con thay no.
|
|
bool Gone(double x, double y, double z, int last_order);
|
|
|
|
size_t views() const { return views_.size(); }
|
|
|
|
private:
|
|
struct View {
|
|
int order;
|
|
float r[9]; // map <- optical, theo hang
|
|
float t[3];
|
|
const NodeDepthView* v;
|
|
};
|
|
const std::vector<int>& Near(int32_t cx, int32_t cy);
|
|
|
|
double range_;
|
|
size_t need_;
|
|
std::vector<View> views_;
|
|
std::unordered_map<uint64_t, std::vector<int>> cells_; // o 1 m -> chi so view
|
|
std::unordered_map<uint64_t, std::vector<int>> near_; // cache Near()
|
|
};
|
|
|
|
// 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;
|
|
};
|
|
|
|
// 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);
|
|
|
|
// `order`: thu tu thoi gian cua node (tang dan) -- VoxelCarver hoi cac node
|
|
// SAU lan cuoi voxel duoc thay. Am = tu dem.
|
|
void BeginNode(int order = -1);
|
|
// 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. `carver` khac null: bo them
|
|
// voxel ma cac node sau lan thay cuoi nhin xuyen qua.
|
|
void Finish(VoxelGrid* out, VoxelCarver* carver = nullptr);
|
|
|
|
size_t clamped() const { return clamped_; }
|
|
size_t stride() const { return stride_; }
|
|
size_t carved() const { return carved_; }
|
|
|
|
private:
|
|
struct Hit {
|
|
uint64_t key;
|
|
int32_t order;
|
|
bool operator<(const Hit& o) const { return key != o.key ? key < o.key : order < o.order; }
|
|
};
|
|
VoxelExportOptions opt_;
|
|
double inv_size_ = 10.0;
|
|
int order_ = -1;
|
|
std::vector<uint64_t> node_; // khoa cua node dang gom
|
|
std::vector<Hit> all_; // khoa DA khu trung theo node, cua moi node
|
|
size_t clamped_ = 0;
|
|
size_t stride_ = 1;
|
|
size_t carved_ = 0;
|
|
};
|
|
|
|
// 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).
|
|
//
|
|
// `floor` (di tren doc): do cao tinh tu SAN TAI CHO voxel (FloorModel) thay vi
|
|
// tu z = 0 -- mat doc va san tang tren khong con bi danh la tuong. Null =
|
|
// duong cu.
|
|
size_t MarkVoxelObstacles(const VoxelGrid& vg, double z_min, double z_max,
|
|
OccupancyGrid* grid, const FloorModel* floor = nullptr);
|
|
|
|
// Di tren doc: TREN DOC, lop LiDAR nhuong cho camera.
|
|
//
|
|
// LiDAR ngang thay mat doc len tu xa (> 4 m, ngoai tam depth) nhu mot buc
|
|
// tuong -- tuong gia vao node TRUOC khi camera kip thay mat doc, va cac scan
|
|
// sau BO tia cham doc (NaN) nen khong tia nao xoa duoc no. O day: o LiDAR bi
|
|
// chiem ma (1) node ban do gan nhat (trong `max_node_dist`) dang TREN DOC
|
|
// (|pitch| >= clamp) va (2) camera da THAY MAT SAN o do (floor_cells) -> tra
|
|
// ve trong. Vat can THAT tren doc van duoc MarkVoxelObstacles danh lai (goi
|
|
// SAU ham nay). Camera chua thay thi giu nguyen -- khong co bang chung.
|
|
// Tra ve so o da xoa.
|
|
size_t ClearLidarOnSeenRamps(OccupancyGrid* grid, const std::vector<uint64_t>& floor_cells,
|
|
double cell_size, const FloorModel& floor, double clamp_rad,
|
|
double max_node_dist);
|
|
|
|
// 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
|