Files
vslam_cpp/test/test_binary_codec.cc
trung09122002 0a1b1b13f1 update
2026-09-23 13:36:48 +07:00

374 lines
11 KiB
C++

#include "vslam/binary_codec.h"
#include <gtest/gtest.h>
#include <cmath>
#include <limits>
using namespace vslam;
namespace {
LaserScan MakeScan() {
LaserScan s;
s.angle_min = -2.356194f;
s.angle_max = 2.356194f;
s.angle_increment = static_cast<float>(2.0 * 2.356194 / 1080.0);
s.range_min = 0.05f;
s.range_max = 40.0f;
s.ranges.resize(1081, 3.0f);
return s;
}
} // namespace
TEST(BinaryCodec, HeaderDungDinhDang) {
const LaserScan s = MakeScan();
std::string buf;
const UnixTime t = UnixTime::fromNSec(1737000000123456789LL);
EncodeScan(s, t, &buf);
ASSERT_GE(buf.size(), kBinaryHeaderSize);
EXPECT_EQ(buf.compare(0, 4, "VSL1"), 0);
BinaryHeader h;
ASSERT_TRUE(DecodeHeader(buf, 0, &h));
EXPECT_EQ(h.type, BinaryType::kScan);
EXPECT_EQ(h.version, kBinaryVersion);
EXPECT_EQ(h.stamp_ns, 1737000000123456789ULL);
}
TEST(BinaryCodec, ScanKichThuocDungDuKien) {
const LaserScan s = MakeScan();
std::string buf;
EncodeScan(s, UnixTime::fromSec(1.0), &buf);
// 16 header + 16 (4 float) + 4 (n) + 1081*2 = 2198
EXPECT_EQ(buf.size(), 16u + 16u + 4u + 1081u * 2u);
EXPECT_EQ(buf.size(), 2198u);
}
TEST(BinaryCodec, ScanGiuDungBaNguNghiaCuaTia) {
// Day la test quan trong nhat cua file. Ba gia tri nay KHAC NHAU:
// NaN = khong do duoc (duoi range_min)
// inf = do duoc nhung khong co phan xa
// range_max+1.0 = bi sector mask cat vi than robot che
// Lan lon chung lam robot hoac dung im (SCAN_SPARSE) hoac dam vao tuong.
LaserScan s = MakeScan();
s.ranges[0] = std::numeric_limits<float>::quiet_NaN();
s.ranges[1] = std::numeric_limits<float>::infinity();
s.ranges[2] = s.range_max + 1.0f; // sentinel
s.ranges[3] = 12.345f;
std::string buf;
EncodeScan(s, UnixTime::fromSec(1.0), &buf);
LaserScan out;
ASSERT_TRUE(DecodeScan(buf, &out));
ASSERT_EQ(out.ranges.size(), s.ranges.size());
EXPECT_TRUE(std::isnan(out.ranges[0]));
EXPECT_TRUE(std::isinf(out.ranges[1]));
EXPECT_FALSE(std::isinf(out.ranges[2]));
EXPECT_NEAR(out.ranges[2], s.range_max + 1.0f, 1e-3); // van la sentinel
EXPECT_NEAR(out.ranges[3], 12.345f, 0.001f); // sai so luong tu 1 mm
}
TEST(BinaryCodec, ScanKepGiaTriQuaLon) {
LaserScan s = MakeScan();
s.range_max = 70000.0f; // vo ly, nhung khong duoc lam tran im lang
s.ranges[0] = 66000.0f;
std::string buf;
EncodeScan(s, UnixTime::fromSec(1.0), &buf);
LaserScan out;
ASSERT_TRUE(DecodeScan(buf, &out));
EXPECT_LE(out.ranges[0], 65.6f); // kep ve kRangeMaxValid, khong quay vong
}
TEST(BinaryCodec, MapFullRoundTrip) {
OccupancyGrid m;
m.width = 40;
m.height = 30;
m.resolution = 0.05f;
m.origin.position.x = -1.25;
m.origin.position.y = -2.5;
m.data.assign(static_cast<size_t>(m.width) * m.height, -1);
for (uint32_t x = 5; x < 35; ++x) m.data[10 * m.width + x] = 100;
for (uint32_t x = 5; x < 35; ++x) m.data[11 * m.width + x] = 0;
std::string buf;
EncodeMapFull(m, UnixTime::fromSec(2.0), &buf);
OccupancyGrid out;
ASSERT_TRUE(DecodeMapFull(buf, &out));
EXPECT_EQ(out.width, m.width);
EXPECT_EQ(out.height, m.height);
EXPECT_NEAR(out.resolution, m.resolution, 1e-6);
EXPECT_NEAR(out.origin.position.x, m.origin.position.x, 1e-5);
EXPECT_NEAR(out.origin.position.y, m.origin.position.y, 1e-5);
EXPECT_EQ(out.data, m.data);
}
TEST(BinaryCodec, MapFullNenTotTrenBanDoThua) {
OccupancyGrid m;
m.width = 2000;
m.height = 2000;
m.resolution = 0.05f;
m.data.assign(4000000u, -1);
// Mot hanh lang: dieu nay giong ban do that hon la toan -1.
for (uint32_t y = 990; y < 1010; ++y) {
for (uint32_t x = 200; x < 1800; ++x) m.data[y * m.width + x] = 0;
}
std::string buf;
EncodeMapFull(m, UnixTime::fromSec(1.0), &buf);
// Tho la 4 MB. RLE phai dua xuong duoi 100 kB.
EXPECT_LT(buf.size(), 100u * 1024u);
OccupancyGrid out;
ASSERT_TRUE(DecodeMapFull(buf, &out));
EXPECT_EQ(out.data, m.data);
}
// --- Ban do voxel 3D -------------------------------------------------------
namespace {
VoxelIndex VK(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;
}
VoxelGrid MakeGrid(size_t n, uint32_t epoch = 7) {
VoxelGrid g;
g.resolution = 0.10f;
g.epoch = epoch;
for (size_t i = 0; i < n; ++i) {
g.cells.push_back(VK(static_cast<int>(i), 0, 1));
}
return g;
}
} // namespace
TEST(BinaryCodec, VoxelFullRoundTripMotManh) {
VoxelGrid g = MakeGrid(3);
g.cells[1] = VK(-5, -32768, 32767); // bien cua i16, ca hai dau
std::string buf;
EncodeVoxelFull(g, UnixTime::fromSec(2.0), 0, g.cells.size(), 0, 1, &buf);
VoxelChunkInfo info;
std::vector<VoxelIndex> out;
ASSERT_TRUE(DecodeVoxelFull(buf, &info, &out));
EXPECT_EQ(info.epoch, 7u);
EXPECT_EQ(info.total, 3u);
EXPECT_EQ(info.chunk_index, 0);
EXPECT_EQ(info.chunk_count, 1);
ASSERT_EQ(out.size(), 3u);
// Dau AM phai song sot qua duong truyen -- day la bug kinh dien cua i16.
EXPECT_EQ(out[1].x, -5);
EXPECT_EQ(out[1].y, -32768);
EXPECT_EQ(out[1].z, 32767);
}
TEST(BinaryCodec, VoxelFullChiaManhVaRapLai) {
const VoxelGrid g = MakeGrid(1000);
// 64 byte/manh -> 32 byte con lai sau hai header -> 5 o moi manh.
const uint32_t chunk_bytes = 16 + 32 + 5 * 6;
const size_t per = VoxelCellsPerChunk(chunk_bytes);
ASSERT_EQ(per, 5u);
const uint16_t chunks = VoxelChunkCount(g, chunk_bytes);
EXPECT_EQ(chunks, 200);
std::vector<VoxelIndex> rap;
for (uint16_t i = 0; i < chunks; ++i) {
std::string buf;
EncodeVoxelFull(g, UnixTime::fromSec(1.0), static_cast<size_t>(i) * per, per,
i, chunks, &buf);
VoxelChunkInfo info;
ASSERT_TRUE(DecodeVoxelFull(buf, &info, &rap));
EXPECT_EQ(info.total, 1000u);
EXPECT_EQ(info.chunk_count, chunks);
}
ASSERT_EQ(rap.size(), 1000u);
for (size_t i = 0; i < rap.size(); ++i) EXPECT_EQ(rap[i].x, static_cast<int>(i));
}
TEST(BinaryCodec, VoxelFullManhCuoiNganHon) {
const VoxelGrid g = MakeGrid(7);
const uint32_t chunk_bytes = 16 + 32 + 5 * 6; // 5 o/manh -> 5 + 2
EXPECT_EQ(VoxelChunkCount(g, chunk_bytes), 2);
std::string buf;
EncodeVoxelFull(g, UnixTime::fromSec(1.0), 5, 5, 1, 2, &buf); // xin 5, con 2
VoxelChunkInfo info;
std::vector<VoxelIndex> out;
ASSERT_TRUE(DecodeVoxelFull(buf, &info, &out));
EXPECT_EQ(out.size(), 2u) << "khong duoc doc qua duoi mang";
EXPECT_EQ(info.total, 7u);
}
TEST(BinaryCodec, VoxelFullBanDoRongVanCoMotManh) {
// Client phai nhan duoc MOT khung de biet ban do gio rong, khong thi no giu
// mai tap cu.
const VoxelGrid g = MakeGrid(0);
EXPECT_EQ(VoxelChunkCount(g, 16384), 1);
std::string buf;
EncodeVoxelFull(g, UnixTime::fromSec(1.0), 0, 0, 0, 1, &buf);
VoxelChunkInfo info;
std::vector<VoxelIndex> out;
ASSERT_TRUE(DecodeVoxelFull(buf, &info, &out));
EXPECT_EQ(info.total, 0u);
EXPECT_TRUE(out.empty());
}
TEST(BinaryCodec, VoxelDeltaRoundTrip) {
const std::vector<VoxelIndex> added = {VK(1, 2, 3), VK(-1, -2, -3)};
const std::vector<VoxelIndex> removed = {VK(9, 9, 9)};
std::string buf;
EncodeVoxelDelta(7, 42, added, removed, UnixTime::fromSec(3.0), &buf);
EXPECT_EQ(buf.size(), 16u + 16u + 3u * 6u);
uint32_t epoch = 0, seq = 0;
std::vector<VoxelIndex> a, r;
ASSERT_TRUE(DecodeVoxelDelta(buf, &epoch, &seq, &a, &r));
EXPECT_EQ(epoch, 7u);
EXPECT_EQ(seq, 42u);
ASSERT_EQ(a.size(), 2u);
EXPECT_EQ(a[1].x, -1);
EXPECT_EQ(a[1].z, -3);
ASSERT_EQ(r.size(), 1u);
EXPECT_EQ(r[0].y, 9);
}
TEST(BinaryCodec, VoxelDeltaRongVanHopLe) {
std::string buf;
EncodeVoxelDelta(1, 0, {}, {}, UnixTime::fromSec(1.0), &buf);
uint32_t epoch = 0, seq = 0;
std::vector<VoxelIndex> a, r;
ASSERT_TRUE(DecodeVoxelDelta(buf, &epoch, &seq, &a, &r));
EXPECT_TRUE(a.empty());
EXPECT_TRUE(r.empty());
}
TEST(BinaryCodec, VoxelKhungCutBiTuChoi) {
// Khung bi cat giua chung phai tra false chu khong doc qua duoi bo dem.
VoxelGrid g = MakeGrid(10);
std::string buf;
EncodeVoxelFull(g, UnixTime::fromSec(1.0), 0, 10, 0, 1, &buf);
buf.resize(buf.size() - 7);
VoxelChunkInfo info;
std::vector<VoxelIndex> out;
EXPECT_FALSE(DecodeVoxelFull(buf, &info, &out));
}
TEST(BinaryCodec, CloudGiamMauVaBoNaN) {
PointCloudXYZ c;
for (int i = 0; i < 10000; ++i) {
c.points.push_back(PointXYZ{static_cast<float>(i) * 0.001f, 0.0f, 0.5f});
}
c.points[5] = PointXYZ{std::numeric_limits<float>::quiet_NaN(), 0.0f, 0.0f};
std::string buf;
EncodeCloud(c, UnixTime::fromSec(1.0), /*max_points=*/1000, 0.01f, Pose2D{}, &buf);
// 16 header + 20 (origin3 + scale + n) + n*6
ASSERT_GE(buf.size(), 36u);
const size_t n = (buf.size() - 36u) / 6u;
EXPECT_LE(n, 1000u);
EXPECT_GT(n, 900u);
}
TEST(BinaryCodec, CloudRongVanHopLe) {
PointCloudXYZ c;
std::string buf;
EncodeCloud(c, UnixTime::fromSec(1.0), 1000, 0.01f, Pose2D{}, &buf);
EXPECT_EQ(buf.size(), 36u);
BinaryHeader h;
ASSERT_TRUE(DecodeHeader(buf, 0, &h));
EXPECT_EQ(h.type, BinaryType::kCloud);
}
// Diem phai DI THEO delta, neu khong thi phep bu do tre khong ton tai that.
//
// `delta` la `pose_now^-1 * pose_capture` (xem Runtime::WebLoop), nen robot
// TIEN 0.5 m ke tu luc chup cho ra delta.x = -0.5: diem tung o 1.0 m truoc
// mui gio chi con cach 0.5 m.
TEST(BinaryCodec, CloudApDeltaTinhTien) {
PointCloudXYZ c;
c.points.push_back(PointXYZ{1.0f, 0.0f, 0.0f});
std::string buf;
EncodeCloud(c, UnixTime::fromSec(1.0), 100, 0.01f, Pose2D{-0.5, 0.0, 0.0}, &buf);
ASSERT_EQ(buf.size(), 42u); // 16 header + 20 + 1*6
auto i16 = [&buf](size_t off) {
return static_cast<int16_t>(static_cast<uint8_t>(buf[off]) |
(static_cast<uint8_t>(buf[off + 1]) << 8));
};
EXPECT_NEAR(i16(36) * 0.01, 0.5, 1e-6);
EXPECT_NEAR(i16(38) * 0.01, 0.0, 1e-6);
}
// Xoay, khong chi tinh tien: quay tai cho la truong hop lam cloud va scan
// tach nhau ro nhat, va mot delta chi co tinh tien se khong bat duoc no.
TEST(BinaryCodec, CloudApDeltaXoay) {
PointCloudXYZ c;
c.points.push_back(PointXYZ{1.0f, 0.0f, 0.5f});
std::string buf;
// Robot da quay PHAI 90 do ke tu luc chup -> diem truoc mui gio o ben TRAI.
EncodeCloud(c, UnixTime::fromSec(1.0), 100, 0.01f,
Pose2D{0.0, 0.0, M_PI / 2.0}, &buf);
ASSERT_EQ(buf.size(), 42u);
auto i16 = [&buf](size_t off) {
return static_cast<int16_t>(static_cast<uint8_t>(buf[off]) |
(static_cast<uint8_t>(buf[off + 1]) << 8));
};
EXPECT_NEAR(i16(36) * 0.01, 0.0, 1e-6);
EXPECT_NEAR(i16(38) * 0.01, 1.0, 1e-6);
// z khong bi delta cham vao.
EXPECT_NEAR(i16(40) * 0.01, 0.5, 1e-6);
}
TEST(BinaryCodec, PathThuaHoa) {
std::vector<Pose2D> path;
for (int i = 0; i < 1000; ++i) {
path.push_back(Pose2D{static_cast<double>(i) * 0.01, 0.0, 0.0}); // buoc 1 cm
}
std::string buf;
EncodePath(path, UnixTime::fromSec(1.0), /*min_step_m=*/0.1, &buf);
const size_t n = (buf.size() - 16u - 4u) / 8u;
// Buoc 1 cm, loc 10 cm -> khoang 100 diem thay vi 1000.
EXPECT_LE(n, 110u);
EXPECT_GE(n, 90u);
}
TEST(BinaryCodec, PathLuonGiuDiemCuoi) {
// Diem cuoi la vi tri hien tai. Mat no thi duong di trong nhu dang tut hau.
std::vector<Pose2D> path = {{0.0, 0.0, 0.0}, {0.01, 0.0, 0.0}, {0.02, 0.0, 0.0}};
std::string buf;
EncodePath(path, UnixTime::fromSec(1.0), 1.0, &buf);
const size_t n = (buf.size() - 20u) / 8u;
EXPECT_EQ(n, 2u); // diem dau + diem cuoi
}
TEST(BinaryCodec, KhungHongBiTuChoi) {
BinaryHeader h;
EXPECT_FALSE(DecodeHeader("", 0, &h));
EXPECT_FALSE(DecodeHeader("XXXX123456789012", 0, &h)); // sai magic
LaserScan s;
EXPECT_FALSE(DecodeScan("VSL1", &s)); // cut
}