update
This commit is contained in:
@@ -4,12 +4,15 @@
|
||||
#include <rtabmap/core/Rtabmap.h>
|
||||
#include <rtabmap/core/SensorData.h>
|
||||
#include <rtabmap/core/Transform.h>
|
||||
#include <rtabmap/core/util3d.h>
|
||||
|
||||
#include <opencv2/core.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <iterator>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "vslam/log.h"
|
||||
#include "vslam/node_constants.h"
|
||||
@@ -113,12 +116,27 @@ void VoxelAccumulator::Finish(VoxelGrid* out) {
|
||||
out->origin_y = 0.0;
|
||||
out->origin_z = 0.0;
|
||||
out->header.frame_id = kMapFrame;
|
||||
out->truncated = acc_.size() > opt_.max_cells;
|
||||
|
||||
const size_t n = std::min(acc_.size(), opt_.max_cells);
|
||||
// ⚠️ Cham tran thi GIAM MAU DEU, tuyet doi khong cat cut danh sach.
|
||||
//
|
||||
// `acc_` sort theo khoa goi, ma khoa goi sort theo x truoc -- nen lay
|
||||
// `max_cells` phan tu DAU la chat mot lat khong gian: nguyen mot canh cua
|
||||
// toa nha bien mat khoi ban do trong khi phan con lai van day du. Da mac
|
||||
// dung loi nay: nguoi dung bao "goc tren ben trai quet roi ma gio mat".
|
||||
//
|
||||
// Giam mau deu thi ban do GIU NGUYEN hinh dang, chi thua di. Cung khuon mau
|
||||
// ma EncodeCloud dung cho `cloud_max_points` (binary_codec.cc) -- buoc deu,
|
||||
// khong cat duoi.
|
||||
const size_t total = acc_.size();
|
||||
stride_ = 1;
|
||||
if (opt_.max_cells > 0 && total > opt_.max_cells) {
|
||||
stride_ = (total + opt_.max_cells - 1) / opt_.max_cells;
|
||||
}
|
||||
out->truncated = stride_ > 1;
|
||||
|
||||
out->cells.clear();
|
||||
out->cells.reserve(n);
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
out->cells.reserve(total / stride_ + 1);
|
||||
for (size_t i = 0; i < total; i += stride_) {
|
||||
int32_t x = 0, y = 0, z = 0;
|
||||
UnpackKey(acc_[i], &x, &y, &z);
|
||||
VoxelIndex k;
|
||||
@@ -154,8 +172,33 @@ void DiffVoxelGrids(const std::vector<VoxelIndex>& prev,
|
||||
for (; j < now.size(); ++j) added->push_back(now[j]);
|
||||
}
|
||||
|
||||
void NodeCloudFromSensorData(const rtabmap::SensorData& data,
|
||||
const VoxelExportOptions& opt,
|
||||
std::vector<float>* out) {
|
||||
out->clear();
|
||||
if (data.depthOrRightRaw().empty() || data.cameraModels().empty()) return;
|
||||
// cloudFromSensorData ap localTransform cua camera (base <- optical), nen
|
||||
// diem ra o frame base cua node -- cung frame voi pose cua no.
|
||||
const auto cloud = rtabmap::util3d::cloudFromSensorData(
|
||||
data, opt.depth_decimation, static_cast<float>(opt.depth_max_m),
|
||||
static_cast<float>(opt.depth_min_m));
|
||||
if (!cloud) return;
|
||||
VoxelAccumulator acc(opt);
|
||||
for (const auto& p : cloud->points) acc.AddPoint(p.x, p.y, p.z);
|
||||
VoxelGrid vg;
|
||||
acc.Finish(&vg);
|
||||
const double s = opt.emit_size_m;
|
||||
out->reserve(vg.cells.size() * 3);
|
||||
for (const VoxelIndex& k : vg.cells) {
|
||||
out->push_back(static_cast<float>((k.x + 0.5) * s));
|
||||
out->push_back(static_cast<float>((k.y + 0.5) * s));
|
||||
out->push_back(static_cast<float>((k.z + 0.5) * s));
|
||||
}
|
||||
}
|
||||
|
||||
StatusResponse ExportVoxelGrid(rtabmap::Rtabmap& slam,
|
||||
const VoxelExportOptions& opt, VoxelGrid* out) {
|
||||
const VoxelExportOptions& opt,
|
||||
VoxelNodeCache* cache, VoxelGrid* out) {
|
||||
const std::map<int, rtabmap::Transform>& poses = slam.getLocalOptimizedPoses();
|
||||
if (poses.empty()) {
|
||||
return StatusResponse::Error(StatusCode::kFailedPrecondition,
|
||||
@@ -167,35 +210,37 @@ StatusResponse ExportVoxelGrid(rtabmap::Rtabmap& slam,
|
||||
"rtabmap chua co Memory");
|
||||
}
|
||||
|
||||
// Bo node khong con trong do thi (bi gop/xoa) -- khong thi cache phinh mai.
|
||||
for (auto it = cache->nodes.begin(); it != cache->nodes.end();) {
|
||||
it = poses.count(it->first) ? std::next(it) : cache->nodes.erase(it);
|
||||
}
|
||||
|
||||
VoxelAccumulator acc(opt);
|
||||
size_t nodes_with_grid = 0;
|
||||
int channels_seen = 0;
|
||||
size_t nodes_used = 0, fetched = 0, pending = 0, no_points = 0;
|
||||
|
||||
for (const auto& kv : poses) {
|
||||
if (kv.second.isNull()) continue;
|
||||
rtabmap::SensorData data = mem->getNodeData(kv.first, false, false, false, true);
|
||||
// ⚠️ BAT BUOC. Thieu dong nay thi gridObstacleCellsRaw() tra ma tran RONG
|
||||
// o moi node va ban do 3D im lang rong khong.
|
||||
data.uncompressData();
|
||||
if (data.gridCellSize() <= 0.0f) continue;
|
||||
|
||||
const cv::Mat& obs = data.gridObstacleCellsRaw();
|
||||
if (obs.empty()) continue;
|
||||
if (obs.depth() != CV_32F) {
|
||||
return StatusResponse::Error(
|
||||
StatusCode::kInternal,
|
||||
"o luoi cuc bo khong phai float32 (depth=" +
|
||||
std::to_string(obs.depth()) + ") -- phien ban RTAB-Map la");
|
||||
auto it = cache->nodes.find(kv.first);
|
||||
if (it == cache->nodes.end()) {
|
||||
if (fetched >= opt.max_new_nodes) {
|
||||
++pending;
|
||||
continue;
|
||||
}
|
||||
++fetched;
|
||||
// images=true, con lai false: chi can depth + camera model.
|
||||
rtabmap::SensorData data = mem->getNodeData(kv.first, true, false, false, false);
|
||||
// ⚠️ BAT BUOC. Thieu dong nay thi depthOrRightRaw() tra ma tran RONG o
|
||||
// moi node va ban do 3D im lang rong khong.
|
||||
data.uncompressData();
|
||||
std::vector<float> pts;
|
||||
NodeCloudFromSensorData(data, opt, &pts);
|
||||
it = cache->nodes.emplace(kv.first, std::move(pts)).first;
|
||||
}
|
||||
|
||||
const int ch = obs.channels();
|
||||
if (ch < 2) {
|
||||
return StatusResponse::Error(
|
||||
StatusCode::kInternal,
|
||||
"o luoi cuc bo co " + std::to_string(ch) +
|
||||
" kenh, cho doi 2 (XY) hoac 3/4 (XYZ[+RGB])");
|
||||
const std::vector<float>& pts = it->second;
|
||||
if (pts.empty()) {
|
||||
++no_points;
|
||||
continue;
|
||||
}
|
||||
if (channels_seen == 0) channels_seen = ch;
|
||||
|
||||
// Pose da toi uu: node -> map. Dung ma tran 3x4 dung san, 12 phep nhan
|
||||
// cho moi diem, thay vi dung mot rtabmap::Transform cho tung diem.
|
||||
@@ -204,37 +249,34 @@ 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();
|
||||
|
||||
const int n = obs.cols * obs.rows;
|
||||
const float* p = obs.ptr<float>();
|
||||
for (int i = 0; i < n; ++i) {
|
||||
const float px = p[0];
|
||||
const float py = p[1];
|
||||
// 2 kenh = luoi PHANG (DB quet voi Grid/3D=false). Van ve duoc, chi la
|
||||
// ra mot lop ngang -- Init() da canh bao truong hop nay.
|
||||
const float pz = (ch >= 3) ? p[2] : 0.0f;
|
||||
p += ch;
|
||||
|
||||
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);
|
||||
}
|
||||
++nodes_with_grid;
|
||||
++nodes_used;
|
||||
}
|
||||
|
||||
if (nodes_with_grid == 0) {
|
||||
if (nodes_used == 0) {
|
||||
return StatusResponse::Error(
|
||||
StatusCode::kFailedPrecondition,
|
||||
"khong node nao co luoi cuc bo -- RGBD/CreateOccupancyGrid tat, hoac "
|
||||
"DB quet truoc khi bat camera_3d");
|
||||
pending > 0 ? "dang nap depth cua node"
|
||||
: "khong node nao co depth -- DB quet voi camera_3d=false, "
|
||||
"hoac khong co camera");
|
||||
}
|
||||
|
||||
acc.Finish(out);
|
||||
|
||||
// Mot dong, mot lan moi 30 giay: so kenh la thu duy nhat trong ca duong nay
|
||||
// khong doc duoc tu header RTAB-Map, nen no phai hien ra o log that.
|
||||
// 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_with_grid << " node (" << channels_seen
|
||||
<< " kenh/o" << (out->truncated ? ", DA CAT BOT" : "")
|
||||
<< nodes_used << " node (dang nap " << pending << ", "
|
||||
<< no_points << " node khong co diem"
|
||||
<< (out->truncated
|
||||
? ", GIAM MAU 1/" + std::to_string(acc.stride()) +
|
||||
" -- cham tran max_cells"
|
||||
: std::string())
|
||||
<< ")");
|
||||
if (acc.clamped() > 0) {
|
||||
VSLAM_WARN_THROTTLE(30.0, "slam: " << acc.clamped()
|
||||
|
||||
Reference in New Issue
Block a user