272 lines
9.9 KiB
C++
272 lines
9.9 KiB
C++
// Doc `.world` cua Gazebo -> luoi chiem dung + hinh hoc 3D.
|
|
//
|
|
// ================== VI SAO BAI KIEM NAY TON TAI ==================
|
|
// Moi loi tim ra khi dung duong nay deu IM LANG: khong ngoai le, khong log,
|
|
// chi la kich thuoc sai. Va chung khong bu tru nhau -- mot toa nha 25 x 51 m
|
|
// da lan luot ra 262 x 297 m, roi 263 x 146 m, roi 25 x 3 m CAO 56 m, moi lan
|
|
// vi mot ly do khac.
|
|
//
|
|
// Fixture `test/data/world/mini.world` co BA hop CUNG kich thuoc that
|
|
// (2.0 x 1.0 x 0.5 m) khai bao theo ba cach khac nhau. Ca ba phai cho cung
|
|
// mot dau vet tren luoi. Neu mot quy uoc bi bo qua, chi rieng hop do sai --
|
|
// va bai kiem chi ra ngay thu pham, khac han mot phep do tong.
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "vslam/gazebo_world.h"
|
|
|
|
namespace {
|
|
|
|
std::string DataDir() {
|
|
const char* e = std::getenv("VSLAM_TEST_DATA_DIR");
|
|
return e != nullptr ? std::string(e) : std::string("test/data");
|
|
}
|
|
|
|
vslam::GazeboWorld LoadMini(size_t* skipped) {
|
|
vslam::GazeboWorld w;
|
|
const std::string dir = DataDir() + "/world";
|
|
const vslam::StatusResponse st =
|
|
vslam::LoadGazeboWorld(dir + "/mini.world", {dir + "/models"}, &w,
|
|
skipped);
|
|
EXPECT_TRUE(st.ok()) << st.message;
|
|
return w;
|
|
}
|
|
|
|
// Bao loi cua cac o bi chiem thuoc mot vung x, tinh bang met.
|
|
struct Extent {
|
|
double x0 = 0, x1 = 0, y0 = 0, y1 = 0;
|
|
size_t cells = 0;
|
|
bool empty() const { return cells == 0; }
|
|
double w() const { return x1 - x0; }
|
|
double h() const { return y1 - y0; }
|
|
};
|
|
|
|
Extent OccupiedIn(const vslam::OccupancyGrid& g, double xlo, double xhi,
|
|
double ylo = -1e18, double yhi = 1e18) {
|
|
Extent e;
|
|
const double res = g.resolution;
|
|
double x0 = 1e18, x1 = -1e18, y0 = 1e18, y1 = -1e18;
|
|
for (uint32_t cy = 0; cy < g.height; ++cy) {
|
|
for (uint32_t cx = 0; cx < g.width; ++cx) {
|
|
if (g.data[static_cast<size_t>(cy) * g.width + cx] < 50) continue;
|
|
const double wx = g.origin.position.x + (cx + 0.5) * res;
|
|
const double wy = g.origin.position.y + (cy + 0.5) * res;
|
|
if (wx < xlo || wx > xhi) continue;
|
|
if (wy < ylo || wy > yhi) continue;
|
|
x0 = std::min(x0, wx); x1 = std::max(x1, wx);
|
|
y0 = std::min(y0, wy); y1 = std::max(y1, wy);
|
|
e.cells++;
|
|
}
|
|
}
|
|
if (e.cells > 0) { e.x0 = x0; e.x1 = x1; e.y0 = y0; e.y1 = y1; }
|
|
return e;
|
|
}
|
|
|
|
vslam::OccupancyGrid Raster(const vslam::GazeboWorld& w) {
|
|
vslam::OccupancyGrid g;
|
|
vslam::WorldRasterOptions o;
|
|
o.z_min = 0.05;
|
|
o.z_max = 1.20;
|
|
o.pad = 1.0;
|
|
EXPECT_TRUE(vslam::RasterizeGazeboWorld(w, o, &g).ok());
|
|
return g;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// Pose vo ly phai bi BO, khong phai duoc raster ra ngoai vu tru.
|
|
//
|
|
// `hospital.world` co san ba model mang z = -7.57769e+08: vat roi xuyen san
|
|
// trong mot phien Gazebo nao do roi world duoc luu lai. Giu chung lai khong
|
|
// lam luoi sai (tam giac roi ngoai dai z) nhung lam moi phep tinh bao loi vo
|
|
// nghia va cap phat instance 3D cho vat khong bao gio thay duoc.
|
|
TEST(GazeboWorld, BoPoseVoLy) {
|
|
size_t skipped = 0;
|
|
const vslam::GazeboWorld w = LoadMini(&skipped);
|
|
ASSERT_EQ(w.models.size(), 5u);
|
|
EXPECT_EQ(skipped, 1u);
|
|
for (const auto& m : w.models) {
|
|
EXPECT_LT(std::fabs(m.z), 1000.0) << m.name;
|
|
}
|
|
}
|
|
|
|
// `<scale>` cua model.sdf phai duoc ap.
|
|
//
|
|
// Bo qua the nay bien mot chiec xe day 2.4 m thanh 236 m. Hop `box_cm` ve
|
|
// bang centimet va mang <scale>0.01</scale>; khong ap thi no ra 200 x 100 m.
|
|
TEST(GazeboWorld, ApScaleCuaSdf) {
|
|
const vslam::OccupancyGrid g = Raster(LoadMini(nullptr));
|
|
const Extent e = OccupiedIn(g, 3.0, 7.0);
|
|
ASSERT_FALSE(e.empty());
|
|
EXPECT_NEAR(e.w(), 2.0, 0.15);
|
|
EXPECT_NEAR(e.h(), 1.0, 0.15);
|
|
}
|
|
|
|
// `<matrix>` cua node COLLADA phai duoc ap, va duoi `.DAE` VIET HOA phai doc
|
|
// duoc.
|
|
//
|
|
// Hai bay nay cung nam tren mot hop: bo qua duoi hoa thi file roi vao bo doc
|
|
// OBJ va bien mat hoan toan (0 o bi chiem); doc duoc nhung bo qua <matrix>
|
|
// thi ti le sai 10 lan.
|
|
TEST(GazeboWorld, ApMatrixVaDuoiFileVietHoa) {
|
|
const vslam::OccupancyGrid g = Raster(LoadMini(nullptr));
|
|
const Extent e = OccupiedIn(g, 8.0, 12.0);
|
|
ASSERT_FALSE(e.empty()) << "hop .DAE bien mat -- kiem so duoi file";
|
|
EXPECT_NEAR(e.w(), 2.0, 0.15);
|
|
EXPECT_NEAR(e.h(), 1.0, 0.15);
|
|
}
|
|
|
|
// Ba cach khai bao khac nhau phai cho CUNG mot ket qua.
|
|
//
|
|
// Day moi la phep kiem that su: tung hop dung kich thuoc van co the do ba loi
|
|
// khac nhau bu tru nhau. Bat chung phai bang nhau thi khong the bu tru.
|
|
TEST(GazeboWorld, BaQuyUocChoCungKetQua) {
|
|
const vslam::OccupancyGrid g = Raster(LoadMini(nullptr));
|
|
// ⚠️ Phai chan CA y: hop thu tu nam o (0, 6), cung dai x voi hop dau. Cua
|
|
// so chi theo x om ca hai va cho h() = 7.5 -- phep do khong con la cua
|
|
// MOT hop nua.
|
|
const Extent met = OccupiedIn(g, -2.0, 2.0, -2.0, 2.0);
|
|
const Extent cm = OccupiedIn(g, 3.0, 7.0);
|
|
const Extent mtx = OccupiedIn(g, 8.0, 12.0);
|
|
const Extent zup = OccupiedIn(g, 13.0, 17.0);
|
|
ASSERT_FALSE(met.empty());
|
|
ASSERT_FALSE(cm.empty());
|
|
ASSERT_FALSE(mtx.empty());
|
|
ASSERT_FALSE(zup.empty());
|
|
for (const Extent& e : {cm, mtx, zup}) {
|
|
EXPECT_NEAR(met.w(), e.w(), 0.1);
|
|
EXPECT_NEAR(met.h(), e.h(), 0.1);
|
|
}
|
|
}
|
|
|
|
// Yaw cua the hien phai duoc ap: hop thu tu quay 90 do nen dai va rong doi
|
|
// cho nhau.
|
|
TEST(GazeboWorld, ApYawCuaTheHien) {
|
|
const vslam::OccupancyGrid g = Raster(LoadMini(nullptr));
|
|
const Extent thang = OccupiedIn(g, -2.0, 2.0, -2.0, 2.0); // (0, 0), yaw 0
|
|
const Extent quay = OccupiedIn(g, -2.0, 2.0, 4.0, 8.0); // (0, 6), yaw 90
|
|
ASSERT_FALSE(thang.empty());
|
|
ASSERT_FALSE(quay.empty());
|
|
|
|
// Cung mot hop 2.0 x 1.0 m: quay 90 do thi dai va rong DOI CHO nhau.
|
|
EXPECT_NEAR(thang.w(), 2.0, 0.15);
|
|
EXPECT_NEAR(thang.h(), 1.0, 0.15);
|
|
EXPECT_NEAR(quay.w(), 1.0, 0.15);
|
|
EXPECT_NEAR(quay.h(), 2.0, 0.15);
|
|
|
|
// Va no phai nam dung cho: tam o y = 6.
|
|
EXPECT_NEAR((quay.y0 + quay.y1) / 2.0, 6.0, 0.1);
|
|
}
|
|
|
|
// `<up_axis>` phai bi BO QUA: no noi ve he toa do cua bo XUAT, khong phai cua
|
|
// du lieu trong file.
|
|
//
|
|
// `box_zup` khai Y_UP nhung du lieu da o Z-up va node khong xoay gi. Day dung
|
|
// la truong hop `..._walls_visual` geometry ID43, con ban VA CHAM cua chinh
|
|
// buc tuong do lai la Y-up that co node xoay. Cung mot buc tuong, hai file,
|
|
// hai cach -- nen khong the quyet dinh bang mot co chung cho ca file, va cung
|
|
// khong the doan tu ma tran node ("node don vi thi lat" sai chinh o day: node
|
|
// don vi nghia la KHONG CAN doi, khong phai CHUA doi).
|
|
//
|
|
// Ap nham lam buc tuong 3 m thanh thanh dung cao 56 m giua san.
|
|
TEST(GazeboWorld, BoQuaUpAxis) {
|
|
const vslam::OccupancyGrid g = Raster(LoadMini(nullptr));
|
|
const Extent e = OccupiedIn(g, 13.0, 17.0);
|
|
ASSERT_FALSE(e.empty());
|
|
EXPECT_NEAR(e.w(), 2.0, 0.15);
|
|
EXPECT_NEAR(e.h(), 1.0, 0.15);
|
|
}
|
|
|
|
// Chieu cao phai dung, khong chi mat bang.
|
|
//
|
|
// Mot hop bi lat truc van co the co mat bang dung ma cao gap hang chuc lan --
|
|
// va `RasterizeGazeboWorld` chi lay dai z [0.05, 1.20] nen no khong bao gio
|
|
// thay chieu cao. Phai do tren HINH HOC, khong tren luoi.
|
|
TEST(GazeboWorld, ChieuCaoHinhHocDung) {
|
|
vslam::WorldMeshes wm;
|
|
ASSERT_TRUE(vslam::ExportWorldMeshes(LoadMini(nullptr), true, &wm).ok());
|
|
for (const auto& g : wm.geoms) {
|
|
double zlo = 1e18, zhi = -1e18, xlo = 1e18, xhi = -1e18;
|
|
for (size_t i = 0; i + 2 < g.tri.size(); i += 3) {
|
|
xlo = std::min<double>(xlo, g.tri[i]);
|
|
xhi = std::max<double>(xhi, g.tri[i]);
|
|
zlo = std::min<double>(zlo, g.tri[i + 2]);
|
|
zhi = std::max<double>(zhi, g.tri[i + 2]);
|
|
}
|
|
EXPECT_NEAR(zhi - zlo, 0.5, 0.05) << g.model_name << ": cao sai";
|
|
EXPECT_NEAR(xhi - xlo, 2.0, 0.05) << g.model_name << ": rong sai";
|
|
}
|
|
}
|
|
|
|
// Hinh hoc 3D phai gop theo TEN MODEL: 5 the hien nhung chi 4 model khac nhau.
|
|
TEST(GazeboWorld, GopHinhHocTheoTenModel) {
|
|
vslam::WorldMeshes wm;
|
|
const vslam::StatusResponse st =
|
|
vslam::ExportWorldMeshes(LoadMini(nullptr), /*visual=*/true, &wm);
|
|
ASSERT_TRUE(st.ok()) << st.message;
|
|
EXPECT_EQ(wm.geoms.size(), 4u);
|
|
EXPECT_EQ(wm.instances.size(), 5u);
|
|
for (const auto& g : wm.geoms) {
|
|
EXPECT_EQ(g.tri.size() % 9, 0u);
|
|
EXPECT_GT(g.tri.size(), 0u);
|
|
}
|
|
}
|
|
|
|
// Khoi nhi phan phai giai ma lai duoc dung y nguyen.
|
|
//
|
|
// Dem NOI DUNG chu khong dem su kien: mot bo ma hoa tra ve khoi RONG co cung
|
|
// moi chi so van hanh voi mot bo dung.
|
|
TEST(GazeboWorld, MaHoaGiaiMaKhoiNhiPhan) {
|
|
vslam::WorldMeshes wm;
|
|
ASSERT_TRUE(vslam::ExportWorldMeshes(LoadMini(nullptr), true, &wm).ok());
|
|
|
|
std::string blob;
|
|
vslam::EncodeWorldMeshes(wm, &blob);
|
|
ASSERT_GE(blob.size(), 16u);
|
|
EXPECT_EQ(blob.compare(0, 4, "VSLW"), 0);
|
|
|
|
auto u32 = [&blob](size_t o) {
|
|
return static_cast<uint32_t>(
|
|
(static_cast<unsigned char>(blob[o])) |
|
|
(static_cast<unsigned char>(blob[o + 1]) << 8) |
|
|
(static_cast<unsigned char>(blob[o + 2]) << 16) |
|
|
(static_cast<unsigned char>(blob[o + 3]) << 24));
|
|
};
|
|
EXPECT_EQ(u32(4), 1u); // version
|
|
EXPECT_EQ(u32(8), wm.geoms.size());
|
|
EXPECT_EQ(u32(12), wm.instances.size());
|
|
|
|
// Duyet het khoi: kich thuoc phai khop chinh xac den byte cuoi.
|
|
size_t o = 16;
|
|
for (const auto& g : wm.geoms) {
|
|
const uint32_t name_len = u32(o); o += 4;
|
|
EXPECT_EQ(blob.compare(o, name_len, g.model_name), 0);
|
|
o += name_len + 3; // + r, g, b
|
|
const uint32_t n_tri = u32(o); o += 4;
|
|
EXPECT_EQ(n_tri, g.tri.size() / 9);
|
|
o += static_cast<size_t>(n_tri) * 36;
|
|
}
|
|
o += wm.instances.size() * 20;
|
|
EXPECT_EQ(o, blob.size()) << "kich thuoc khoi khong khop -- bo ma hoa va "
|
|
"bo giai ma da lech nhau";
|
|
}
|
|
|
|
// `EncodeWorldMeshes` phai GHI DE `out`, khong noi them.
|
|
//
|
|
// ⚠️ Chinh loi nay tung sinh mot file `.vsr` 17 GB va lam day o dia: bon bo
|
|
// ma hoa dung chung mot bo dem ma khong `clear()`.
|
|
TEST(GazeboWorld, MaHoaGhiDeKhongNoiThem) {
|
|
vslam::WorldMeshes wm;
|
|
ASSERT_TRUE(vslam::ExportWorldMeshes(LoadMini(nullptr), true, &wm).ok());
|
|
std::string blob;
|
|
vslam::EncodeWorldMeshes(wm, &blob);
|
|
const size_t once = blob.size();
|
|
vslam::EncodeWorldMeshes(wm, &blob);
|
|
EXPECT_EQ(blob.size(), once);
|
|
}
|