503 lines
17 KiB
C++
503 lines
17 KiB
C++
// VoxelAccumulator va DiffVoxelGrids: phan THUAN cua duong xuat ban do 3D.
|
|
//
|
|
// Khong can mot `Rtabmap` dang chay, va do la chu y: `ExportOccupancyGrid`
|
|
// den nay khong co unit test nao chinh vi no can mot `Rtabmap` dang chay.
|
|
// Phan tinh toan tach ra thi kiem duoc bang so. Test NodeCloud o cuoi dung
|
|
// kieu RTAB-Map (SensorData, CameraModel) nhung tu dung, khong can Rtabmap.
|
|
|
|
#include "vslam/voxel_export.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <opencv2/core.hpp>
|
|
#include <rtabmap/core/CameraModel.h>
|
|
#include <rtabmap/core/SensorData.h>
|
|
#include <rtabmap/core/Transform.h>
|
|
|
|
#include <algorithm>
|
|
#include <vector>
|
|
|
|
using namespace vslam;
|
|
|
|
namespace {
|
|
|
|
VoxelExportOptions Opt(double emit = 0.10) {
|
|
VoxelExportOptions o;
|
|
o.emit_size_m = emit;
|
|
o.z_min = 0.05;
|
|
o.z_max = 2.00;
|
|
return o;
|
|
}
|
|
|
|
VoxelIndex K(int x, int y, int z) {
|
|
VoxelIndex k;
|
|
k.x = static_cast<int16_t>(x);
|
|
k.y = static_cast<int16_t>(y);
|
|
k.z = static_cast<int16_t>(z);
|
|
return k;
|
|
}
|
|
|
|
bool Has(const std::vector<VoxelIndex>& v, int x, int y, int z) {
|
|
for (const VoxelIndex& k : v) {
|
|
if (k.x == x && k.y == y && k.z == z) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Cung thu tu ma Finish() hua: theo khoa goi, tuc (x, y, z) tu nhien.
|
|
bool DaSapXep(const std::vector<VoxelIndex>& v) {
|
|
for (size_t i = 1; i < v.size(); ++i) {
|
|
const VoxelIndex& a = v[i - 1];
|
|
const VoxelIndex& b = v[i];
|
|
if (a.x != b.x) { if (a.x > b.x) return false; continue; }
|
|
if (a.y != b.y) { if (a.y > b.y) return false; continue; }
|
|
if (a.z >= b.z) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(VoxelAccumulator, GomDiemTrungVaoMotO) {
|
|
VoxelAccumulator acc(Opt(0.10));
|
|
// Ba diem trong cung mot o 10 cm.
|
|
acc.AddPoint(1.01, 2.02, 0.51);
|
|
acc.AddPoint(1.09, 2.09, 0.59);
|
|
acc.AddPoint(1.05, 2.05, 0.55);
|
|
VoxelGrid g;
|
|
acc.Finish(&g);
|
|
|
|
ASSERT_EQ(g.cells.size(), 1u);
|
|
EXPECT_TRUE(Has(g.cells, 10, 20, 5));
|
|
EXPECT_FLOAT_EQ(g.resolution, 0.10f);
|
|
EXPECT_FALSE(g.truncated);
|
|
}
|
|
|
|
TEST(VoxelAccumulator, ChiSoAmDungHuong) {
|
|
// floor(), khong phai cat ve 0: -0.05 m phai roi vao o -1 chu khong phai 0.
|
|
// Nham cho nay thi nua ban do phia am bi gap len goc toa do.
|
|
VoxelAccumulator acc(Opt(0.10));
|
|
acc.AddPoint(-0.05, -1.25, 0.10);
|
|
VoxelGrid g;
|
|
acc.Finish(&g);
|
|
ASSERT_EQ(g.cells.size(), 1u);
|
|
EXPECT_EQ(g.cells[0].x, -1);
|
|
EXPECT_EQ(g.cells[0].y, -13);
|
|
EXPECT_EQ(g.cells[0].z, 1);
|
|
}
|
|
|
|
TEST(VoxelAccumulator, CatSanVaTran) {
|
|
VoxelAccumulator acc(Opt(0.10));
|
|
acc.AddPoint(0.0, 0.0, 0.00); // san -> bo (z < z_min)
|
|
acc.AddPoint(0.0, 0.0, 0.04); // van duoi nguong -> bo
|
|
acc.AddPoint(0.0, 0.0, 1.00); // giu
|
|
acc.AddPoint(0.0, 0.0, 2.50); // tran -> bo
|
|
VoxelGrid g;
|
|
acc.Finish(&g);
|
|
|
|
ASSERT_EQ(g.cells.size(), 1u);
|
|
EXPECT_EQ(g.cells[0].z, 10);
|
|
EXPECT_EQ(acc.z_rejected(), 3u);
|
|
}
|
|
|
|
TEST(VoxelAccumulator, DauRaDaSapXep) {
|
|
VoxelAccumulator acc(Opt(0.10));
|
|
// Nhet vao theo thu tu lung tung.
|
|
const double pts[][3] = {{5.0, 1.0, 1.0}, {-3.0, 2.0, 0.5}, {5.0, 0.5, 1.0},
|
|
{0.0, 0.0, 1.5}, {-3.0, 2.0, 1.5}};
|
|
for (const auto& p : pts) acc.AddPoint(p[0], p[1], p[2]);
|
|
VoxelGrid g;
|
|
acc.Finish(&g);
|
|
|
|
EXPECT_EQ(g.cells.size(), 5u);
|
|
EXPECT_TRUE(DaSapXep(g.cells)) << "DiffVoxelGrids dua vao dung tinh chat nay";
|
|
}
|
|
|
|
TEST(VoxelAccumulator, ChamTranThiGiamMauDeuChuKhongCatCut) {
|
|
// ⚠️ Bai kiem NAY la bai kiem quan trong nhat trong file.
|
|
//
|
|
// Ban dau Finish() lay `max_cells` phan tu DAU cua danh sach da sort. Khoa
|
|
// sort theo x truoc, nen no chat mot LAT KHONG GIAN: nguyen mot canh cua toa
|
|
// nha bien mat trong khi phan con lai van day du. Nguoi dung phat hien bang
|
|
// mat ("goc tren ben trai quet roi ma gio mat"), khong mot log nao bao.
|
|
//
|
|
// Bat bien phai giu: ban do MONG di deu, KHONG cut mot dau.
|
|
VoxelExportOptions o = Opt(0.10);
|
|
o.max_cells = 5;
|
|
VoxelAccumulator acc(o);
|
|
for (int i = 0; i < 20; ++i) acc.AddPoint(i * 1.0, 0.0, 1.0); // x = 0..19 m
|
|
VoxelGrid g;
|
|
acc.Finish(&g);
|
|
|
|
EXPECT_TRUE(g.truncated) << "giam mau ma khong bao la kieu hong te nhat";
|
|
EXPECT_EQ(acc.stride(), 4u); // ceil(20 / 5)
|
|
ASSERT_EQ(g.cells.size(), 5u);
|
|
|
|
// Chi so = met / 0.05... o day emit 0.10 nen x = 0, 4, 8, 12, 16 m -> 0, 40,
|
|
// 80, 120, 160. Hai DAU deu con: do la dieu phep cat cut khong lam duoc.
|
|
EXPECT_EQ(g.cells.front().x, 0) << "mat dau ban do";
|
|
EXPECT_EQ(g.cells.back().x, 160) << "mat duoi ban do -- dung loi cu quay lai";
|
|
EXPECT_TRUE(DaSapXep(g.cells)) << "giam mau khong duoc pha thu tu";
|
|
}
|
|
|
|
TEST(VoxelAccumulator, DuoiTranThiKhongDongGi) {
|
|
VoxelExportOptions o = Opt(0.10);
|
|
o.max_cells = 1000;
|
|
VoxelAccumulator acc(o);
|
|
for (int i = 0; i < 20; ++i) acc.AddPoint(i * 1.0, 0.0, 1.0);
|
|
VoxelGrid g;
|
|
acc.Finish(&g);
|
|
|
|
EXPECT_EQ(g.cells.size(), 20u);
|
|
EXPECT_FALSE(g.truncated);
|
|
EXPECT_EQ(acc.stride(), 1u);
|
|
}
|
|
|
|
TEST(VoxelAccumulator, FlushNhieuLanVanRaCungKetQua) {
|
|
// flush_every nho ep duong set_union chay nhieu lan; ket qua phai y het khi
|
|
// gom mot lan.
|
|
VoxelExportOptions o = Opt(0.10);
|
|
o.flush_every = 3;
|
|
VoxelAccumulator many(o);
|
|
VoxelAccumulator once(Opt(0.10));
|
|
for (int i = 0; i < 25; ++i) {
|
|
const double x = (i % 7) * 0.31;
|
|
many.AddPoint(x, 0.0, 1.0);
|
|
once.AddPoint(x, 0.0, 1.0);
|
|
}
|
|
VoxelGrid a, b;
|
|
many.Finish(&a);
|
|
once.Finish(&b);
|
|
|
|
ASSERT_EQ(a.cells.size(), b.cells.size());
|
|
for (size_t i = 0; i < a.cells.size(); ++i) {
|
|
EXPECT_EQ(a.cells[i].x, b.cells[i].x);
|
|
EXPECT_EQ(a.cells[i].y, b.cells[i].y);
|
|
EXPECT_EQ(a.cells[i].z, b.cells[i].z);
|
|
}
|
|
}
|
|
|
|
TEST(VoxelAccumulator, DiemNgoaiDaiBiBoChuKhongKep) {
|
|
// Kep se dan mot buc tuong gia o ria ban do. Bo la dung.
|
|
VoxelAccumulator acc(Opt(0.10));
|
|
acc.AddPoint(1e9, 0.0, 1.0);
|
|
acc.AddPoint(0.0, 0.0, 1.0);
|
|
VoxelGrid g;
|
|
acc.Finish(&g);
|
|
|
|
EXPECT_EQ(g.cells.size(), 1u);
|
|
EXPECT_EQ(acc.clamped(), 1u);
|
|
}
|
|
|
|
TEST(DiffVoxelGrids, ThemVaBot) {
|
|
const std::vector<VoxelIndex> prev = {K(0, 0, 1), K(1, 0, 1), K(2, 0, 1)};
|
|
const std::vector<VoxelIndex> now = {K(1, 0, 1), K(2, 0, 1), K(3, 0, 1)};
|
|
std::vector<VoxelIndex> added, removed;
|
|
DiffVoxelGrids(prev, now, &added, &removed);
|
|
|
|
ASSERT_EQ(added.size(), 1u);
|
|
EXPECT_EQ(added[0].x, 3);
|
|
ASSERT_EQ(removed.size(), 1u);
|
|
EXPECT_EQ(removed[0].x, 0);
|
|
}
|
|
|
|
TEST(DiffVoxelGrids, QuetTienThiChiCoThem) {
|
|
// Mapping binh thuong: tap moi la sieu tap -> khong co o nao bi bo, delta nho.
|
|
const std::vector<VoxelIndex> prev = {K(0, 0, 1), K(1, 0, 1)};
|
|
const std::vector<VoxelIndex> now = {K(0, 0, 1), K(1, 0, 1), K(2, 0, 1)};
|
|
std::vector<VoxelIndex> added, removed;
|
|
DiffVoxelGrids(prev, now, &added, &removed);
|
|
|
|
EXPECT_EQ(added.size(), 1u);
|
|
EXPECT_TRUE(removed.empty());
|
|
}
|
|
|
|
TEST(DiffVoxelGrids, TapRong) {
|
|
std::vector<VoxelIndex> added, removed;
|
|
const std::vector<VoxelIndex> some = {K(0, 0, 1)};
|
|
|
|
DiffVoxelGrids({}, some, &added, &removed);
|
|
EXPECT_EQ(added.size(), 1u);
|
|
EXPECT_TRUE(removed.empty());
|
|
|
|
DiffVoxelGrids(some, {}, &added, &removed);
|
|
EXPECT_TRUE(added.empty());
|
|
EXPECT_EQ(removed.size(), 1u);
|
|
}
|
|
|
|
TEST(DiffVoxelGrids, SauVongDongThiDoiGanHet) {
|
|
// Toi uu lai do thi dich moi pose -> phep tru van dung, chi la khong lon.
|
|
// Day la ca de nguoi goi phat hien va nhay sang gui lai ca tap (doi epoch).
|
|
std::vector<VoxelIndex> prev, now;
|
|
for (int i = 0; i < 100; ++i) {
|
|
prev.push_back(K(i, 0, 1));
|
|
now.push_back(K(i, 5, 1)); // ca ban do dich 0.5 m theo y
|
|
}
|
|
std::vector<VoxelIndex> added, removed;
|
|
DiffVoxelGrids(prev, now, &added, &removed);
|
|
|
|
EXPECT_EQ(added.size(), 100u);
|
|
EXPECT_EQ(removed.size(), 100u);
|
|
const double ratio = static_cast<double>(added.size() + removed.size()) /
|
|
static_cast<double>(now.size());
|
|
EXPECT_GT(ratio, 0.35) << "vuot nguong -> nguoi goi phai doi epoch";
|
|
}
|
|
|
|
TEST(NodeCloud, DepthRaDiemOFrameBase) {
|
|
// Tuong phang cach camera 2 m; camera cao 0.36 m so voi base, nhin +x.
|
|
// Diem phai co x ~ 2 (frame base), KHONG phai z ~ 2 (frame quang hoc) --
|
|
// ca ban do 3D dua vao gia thiet cloudFromSensorData ap localTransform.
|
|
const int w = 64, h = 48;
|
|
cv::Mat depth(h, w, CV_32FC1, cv::Scalar(2.0f));
|
|
cv::Mat gray = cv::Mat::zeros(h, w, CV_8UC1);
|
|
const rtabmap::Transform base_to_optical =
|
|
rtabmap::Transform(0, 0, 0.36f, 0, 0, 0) *
|
|
rtabmap::CameraModel::opticalRotation();
|
|
rtabmap::CameraModel model(50.0, 50.0, w / 2.0, h / 2.0, base_to_optical,
|
|
0.0, cv::Size(w, h));
|
|
rtabmap::SensorData data(gray, depth, model, 1, 0.0);
|
|
|
|
VoxelExportOptions opt = Opt(0.05);
|
|
opt.depth_decimation = 1;
|
|
std::vector<float> pts;
|
|
NodeCloudFromSensorData(data, opt, &pts);
|
|
ASSERT_FALSE(pts.empty());
|
|
ASSERT_EQ(pts.size() % 3, 0u);
|
|
for (size_t i = 0; i < pts.size(); i += 3) {
|
|
EXPECT_NEAR(pts[i], 2.0, opt.emit_size_m);
|
|
EXPECT_GE(pts[i + 2], opt.z_min - opt.emit_size_m);
|
|
EXPECT_LE(pts[i + 2], opt.z_max + opt.emit_size_m);
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
|
|
// Luoi 2D toan o TRONG, khong xoay, goc duoi-trai tai (ox, oy).
|
|
OccupancyGrid Luoi(uint32_t w, uint32_t h, double ox, double oy) {
|
|
OccupancyGrid g;
|
|
g.resolution = 0.05f;
|
|
g.width = w;
|
|
g.height = h;
|
|
g.origin.position.x = ox;
|
|
g.origin.position.y = oy;
|
|
g.origin.orientation.w = 1.0;
|
|
g.data.assign(static_cast<size_t>(w) * h, 0);
|
|
return g;
|
|
}
|
|
|
|
VoxelGrid Voxels(std::vector<VoxelIndex> cells) {
|
|
VoxelGrid v;
|
|
v.resolution = 0.05f;
|
|
v.cells = std::move(cells);
|
|
return v;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(MarkVoxelObstacles, ChiLayDaiDoCaoCuaRobot) {
|
|
// Goc luoi (-0.5, -0.5): kiem ca phep tru goc, khong chi phep chia.
|
|
OccupancyGrid g = Luoi(20, 20, -0.5, -0.5);
|
|
const VoxelGrid v = Voxels({
|
|
K(3, 4, 6), // z = 0.325 m -> trong dai [0.10, 0.55] -> tuong
|
|
K(5, 5, 16), // z = 0.825 m -> tren noc robot -> bo
|
|
K(6, 6, 1), // z = 0.075 m -> san -> bo
|
|
});
|
|
EXPECT_EQ(MarkVoxelObstacles(v, 0.10, 0.55, &g), 1u);
|
|
// x = 0.175 -> (0.175 + 0.5) / 0.05 = 13; y = 0.225 -> 14.
|
|
EXPECT_EQ(g.data[14 * 20 + 13], 100);
|
|
// K(5,5,*) roi vao o (15, 15), K(6,6,*) vao o (16, 16).
|
|
EXPECT_EQ(g.data[15 * 20 + 15], 0) << "voxel 0.825 m khong duoc danh dau";
|
|
EXPECT_EQ(g.data[16 * 20 + 16], 0) << "voxel 0.075 m khong duoc danh dau";
|
|
}
|
|
|
|
TEST(MarkVoxelObstacles, NgoaiLuoiThiBoQua) {
|
|
OccupancyGrid g = Luoi(20, 20, 0.0, 0.0);
|
|
const VoxelGrid v = Voxels({K(100, 100, 6), K(-3, 2, 6), K(2, -1, 6)});
|
|
EXPECT_EQ(MarkVoxelObstacles(v, 0.10, 0.55, &g), 0u);
|
|
for (int8_t c : g.data) EXPECT_EQ(c, 0);
|
|
}
|
|
|
|
TEST(MarkVoxelObstacles, ODaLaTuongThiKhongDemLai) {
|
|
OccupancyGrid g = Luoi(20, 20, 0.0, 0.0);
|
|
g.data[4 * 20 + 3] = 100; // tuong tu LiDAR
|
|
g.data[5 * 20 + 3] = -1; // chua biet -> van danh dau duoc
|
|
const VoxelGrid v = Voxels({K(3, 4, 6), K(3, 5, 6)});
|
|
EXPECT_EQ(MarkVoxelObstacles(v, 0.10, 0.55, &g), 1u);
|
|
EXPECT_EQ(g.data[4 * 20 + 3], 100);
|
|
EXPECT_EQ(g.data[5 * 20 + 3], 100);
|
|
}
|
|
|
|
TEST(NodeCloud, KhongCoDepthThiRong) {
|
|
// Node quet voi camera_3d=false: khong co depth -> rong, khong nem loi.
|
|
rtabmap::SensorData data;
|
|
std::vector<float> pts{1.0f, 2.0f, 3.0f};
|
|
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));
|
|
}
|
|
|
|
// ---------------- Lan nhin moi nhat quyet dinh (07/10) ----------------
|
|
// 3 nguoi dung tren duong luc quet thanh vat can VINH VIEN: lop camera chi
|
|
// them, khong bao gio xoa. VoxelCarver hoi cac node SAU lan cuoi voxel duoc
|
|
// thay: nhin xuyen qua -> xoa; con thay -> giu; bi che -> khong biet.
|
|
|
|
namespace {
|
|
|
|
// Camera cao 0.4 m nhin +x, anh nho 80x60, f = 60 px. Tuong o x = 3 m.
|
|
// `nguoi`: khoi depth 1.5 m giua anh. `vat_gan` > 0: khoi do o khoang cach nay
|
|
// (che tam nhin).
|
|
NodeDepthView Canh(bool nguoi, double vat_gan = 0.0) {
|
|
NodeDepthView v;
|
|
v.w = 80;
|
|
v.h = 60;
|
|
v.fx = v.fy = 60.0f;
|
|
v.cx = 39.5f;
|
|
v.cy = 29.5f;
|
|
const float cam[12] = {0, 0, 1, 0, -1, 0, 0, 0, 0, -1, 0, 0.4f}; // base <- optical
|
|
std::copy(cam, cam + 12, v.cam);
|
|
v.depth_mm.assign(80 * 60, 3000);
|
|
for (int y = 22; y < 38; ++y) {
|
|
for (int x = 32; x < 48; ++x) {
|
|
if (nguoi) v.depth_mm[y * 80 + x] = 1500;
|
|
if (vat_gan > 0.0) v.depth_mm[y * 80 + x] = static_cast<uint16_t>(vat_gan * 1000.0);
|
|
}
|
|
}
|
|
return v;
|
|
}
|
|
|
|
const float kPoseGoc[12] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0};
|
|
|
|
} // namespace
|
|
|
|
TEST(VoxelCarver, NguoiDaDiThiXoa) {
|
|
// Node 0-2 thay nguoi o x = 1.5 m; node 3-5 nhin thang qua cho do toi tuong.
|
|
std::vector<NodeDepthView> v;
|
|
for (int n = 0; n < 6; ++n) v.push_back(Canh(n < 3));
|
|
VoxelCarver c(3.5, 2);
|
|
for (int n = 0; n < 6; ++n) c.AddView(n, kPoseGoc, &v[n]);
|
|
EXPECT_TRUE(c.Gone(1.5, 0.0, 0.4, 2)); // lan cuoi thay: node 2
|
|
// Tuong: lan cuoi thay la node 5, khong con node nao sau -> giu.
|
|
EXPECT_FALSE(c.Gone(3.0, 0.0, 0.4, 5));
|
|
}
|
|
|
|
TEST(VoxelCarver, LanNhinMoiNhatQuyetDinh) {
|
|
// Node 3-4 nhin xuyen qua nhung node 5 (MOI NHAT) lai thay nguoi -> giu.
|
|
std::vector<NodeDepthView> v;
|
|
for (int n = 0; n < 6; ++n) v.push_back(Canh(n < 3 || n == 5));
|
|
VoxelCarver c(3.5, 2);
|
|
for (int n = 0; n < 6; ++n) c.AddView(n, kPoseGoc, &v[n]);
|
|
EXPECT_FALSE(c.Gone(1.5, 0.0, 0.4, 2));
|
|
}
|
|
|
|
TEST(VoxelCarver, BiCheThiKhongXoa) {
|
|
// Node sau co vat GAN hon (0.8 m) che tam nhin: khong biet -> khong xoa.
|
|
std::vector<NodeDepthView> v;
|
|
for (int n = 0; n < 6; ++n) v.push_back(n < 3 ? Canh(true) : Canh(false, 0.8));
|
|
VoxelCarver c(3.5, 2);
|
|
for (int n = 0; n < 6; ++n) c.AddView(n, kPoseGoc, &v[n]);
|
|
EXPECT_FALSE(c.Gone(1.5, 0.0, 0.4, 2));
|
|
}
|
|
|
|
TEST(VoxelCarver, NgoaiKhungNhinThiKhongTinh) {
|
|
std::vector<NodeDepthView> v;
|
|
for (int n = 0; n < 6; ++n) v.push_back(Canh(false));
|
|
VoxelCarver c(3.5, 2);
|
|
for (int n = 0; n < 6; ++n) c.AddView(n, kPoseGoc, &v[n]);
|
|
EXPECT_FALSE(c.Gone(-1.0, 0.0, 0.4, 0)); // sau lung camera
|
|
EXPECT_FALSE(c.Gone(5.0, 0.0, 0.4, 0)); // xa hon ban kinh 3.5 m
|
|
EXPECT_FALSE(c.Gone(1.0, 3.0, 0.4, 0)); // ngoai goc nhin
|
|
}
|
|
|
|
TEST(VoxelHitCounter, XoaVoxelMaNodeSauNhinXuyenQua) {
|
|
// 6 node deu thay tuong (3, 0, 0.4); node 0-2 thay them nguoi (1.5, 0, 0.4).
|
|
// Node 3-5 nhin xuyen qua cho nguoi -> nguoi bi xoa, tuong giu.
|
|
VoxelExportOptions o = Opt(0.05);
|
|
o.min_node_hits = 3;
|
|
VoxelHitCounter h(o);
|
|
std::vector<NodeDepthView> v;
|
|
for (int n = 0; n < 6; ++n) v.push_back(Canh(n < 3));
|
|
VoxelCarver c(3.5, 2);
|
|
for (int n = 0; n < 6; ++n) {
|
|
c.AddView(n, kPoseGoc, &v[n]);
|
|
h.BeginNode(n);
|
|
h.AddPoint(3.01, 0.01, 0.41);
|
|
if (n < 3) h.AddPoint(1.51, 0.01, 0.41);
|
|
h.EndNode();
|
|
}
|
|
VoxelGrid g;
|
|
h.Finish(&g, &c);
|
|
EXPECT_EQ(h.carved(), 1u);
|
|
ASSERT_EQ(g.cells.size(), 1u);
|
|
EXPECT_TRUE(Has(g.cells, 60, 0, 8)); // tuong
|
|
}
|
|
|
|
TEST(NodeDepthView, LayMinMoiKhoiVaChiaTieuCu) {
|
|
// Depth 320x240 nen 3 m, MOT pixel vat gan 1 m: khoi 4x4 chua no phai la
|
|
// 1 m (than trong -- chan nguoi vai pixel van la "thay vat").
|
|
const int w = 320, h = 240;
|
|
cv::Mat depth(h, w, CV_16UC1, cv::Scalar(3000));
|
|
depth.at<uint16_t>(121, 161) = 1000;
|
|
cv::Mat gray = cv::Mat::zeros(h, w, CV_8UC1);
|
|
const rtabmap::Transform base_to_optical =
|
|
rtabmap::Transform(0, 0, 0.4f, 0, 0, 0) * rtabmap::CameraModel::opticalRotation();
|
|
rtabmap::CameraModel model(300.0, 300.0, 159.5, 119.5, base_to_optical, 0.0,
|
|
cv::Size(w, h));
|
|
rtabmap::SensorData data(gray, depth, model, 1, 0.0);
|
|
NodeDepthView v;
|
|
NodeDepthViewFromSensorData(data, 4, &v);
|
|
ASSERT_EQ(v.w, 80);
|
|
ASSERT_EQ(v.h, 60);
|
|
EXPECT_NEAR(v.fx, 75.0, 1e-3);
|
|
EXPECT_NEAR(v.cx, 39.5, 1e-3);
|
|
EXPECT_EQ(v.depth_mm[30 * 80 + 40], 1000);
|
|
EXPECT_EQ(v.depth_mm[0], 3000);
|
|
EXPECT_NEAR(v.cam[11], 0.4, 1e-6);
|
|
}
|