Files
vslam_cpp/test/test_sim_gl.cc

183 lines
6.8 KiB
C++

// Ve camera mo phong bang GPU (vslam_sim_gl).
//
// May khong co GPU dung duoc (CI, container) thi cac bai nay SKIP, khong do:
// renderer GPU la tuy chon, va bo mo phong van chay bang raycast CPU.
//
// Khoa:
// * depth GPU dung khoang cach toi mm (tia qua pixel giua);
// * hop bao mesh doc bang assimp khop bo doc world cua vslam_sim (OBJ met,
// OBJ cm + <scale>, COLLADA co <matrix>, COLLADA khai Y_UP) -- lech quy
// uoc la anh va luoi chiem dung noi hai chuyen khac nhau;
// * bo mo phong dung renderer: co anh IR cung kich thuoc depth, va depth GPU
// KHOP depth raycast CPU tren san / doc / tuong.
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <memory>
#include <string>
#include <vector>
#include "vslam/gazebo_world.h"
#include "vslam/sim_gl_renderer.h"
#include "vslam/sim_source.h"
using namespace vslam;
namespace {
std::string WorldDir() {
const char* e = std::getenv("VSLAM_TEST_DATA_DIR");
return (e != nullptr ? std::string(e) : std::string("test/data")) + "/world";
}
GazeboWorld Load(const std::string& name) {
GazeboWorld w;
const StatusResponse st =
LoadGazeboWorld(WorldDir() + "/" + name, {WorldDir() + "/models"}, &w);
EXPECT_TRUE(st.ok()) << st.message;
return w;
}
// Camera dat tai (x, y, z), nhin theo yaw, khong chuc.
SimCameraView View(double x, double y, double z, double yaw, int w, int h, double fx) {
const double cy = std::cos(yaw), sy = std::sin(yaw);
SimCameraView v;
// Cot: X quang = phai = -trai, Y quang = xuong, Z quang = truoc.
const double cols[3][3] = {{sy, -cy, 0.0}, {0.0, 0.0, -1.0}, {cy, sy, 0.0}};
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) v.R_wc[i * 3 + j] = cols[j][i];
}
v.t_wc[0] = x; v.t_wc[1] = y; v.t_wc[2] = z;
v.width = w; v.height = h;
v.fx = v.fy = fx;
v.cx = w / 2.0; v.cy = h / 2.0;
return v;
}
// Renderer cho world, hoac nullptr (kem ly do) neu may khong ve duoc.
std::shared_ptr<SimCameraRenderer> MakeRenderer(const GazeboWorld& w, std::string* why) {
GlRendererConfig cfg = DefaultGlRendererConfig();
auto r = CreateGlCameraRenderer(w, cfg, why);
if (!r) return nullptr;
std::vector<uint8_t> ir;
std::vector<float> d;
if (!r->Render(View(0, 0, 1, 0, 8, 8, 4.0), &ir, &d)) {
*why = "khong co GPU dung duoc (xem log sim_gl)";
return nullptr;
}
return r;
}
} // namespace
TEST(SimGl, DepthDungKhoangCachToiMm) {
// mini.world: hop box_m 2.0 x 1.0 x 0.5 m tam o goc. Mesh CHI co day, nap
// va hai mat +-y (khong co mat +-x), nen nhin vao mat y = -0.5: camera o
// y = -2.5, cao giua hop, nhin +y -- pixel giua thay mat do o Z = 2.0.
const GazeboWorld w = Load("mini.world");
std::string why;
auto r = MakeRenderer(w, &why);
if (!r) GTEST_SKIP() << why;
std::vector<uint8_t> ir;
std::vector<float> d;
const int W = 160, H = 120;
ASSERT_TRUE(r->Render(View(0.0, -2.5, 0.25, M_PI / 2, W, H, 120.0), &ir, &d));
ASSERT_EQ(d.size(), static_cast<size_t>(W) * H);
EXPECT_NEAR(d[static_cast<size_t>(H / 2) * W + W / 2], 2.0, 1e-3);
// Goc tren trai nhin len troi: khong trung gi.
EXPECT_EQ(d[0], 0.0f);
EXPECT_EQ(ir[0], 0);
}
TEST(SimGl, HopBaoAssimpKhopBoDocWorld) {
const GazeboWorld w = Load("mini.world");
WorldMeshes ref;
ASSERT_TRUE(ExportWorldMeshes(w, /*visual=*/true, &ref).ok());
size_t checked = 0;
for (const WorldMeshInstance& in : ref.instances) {
// Hop bao THE GIOI cua the hien theo bo doc world: tam giac he model
// (da ap <scale>) -> yaw -> tinh tien.
const WorldMeshGeom& g = ref.geoms[in.geom];
double rmn[3] = {1e18, 1e18, 1e18}, rmx[3] = {-1e18, -1e18, -1e18};
const double c = std::cos(in.yaw), s = std::sin(in.yaw);
for (size_t k = 0; k + 2 < g.tri.size(); k += 3) {
const double p[3] = {g.tri[k] * c - g.tri[k + 1] * s + in.x,
g.tri[k] * s + g.tri[k + 1] * c + in.y, g.tri[k + 2] + in.z};
for (int a = 0; a < 3; ++a) { rmn[a] = std::min(rmn[a], p[a]); rmx[a] = std::max(rmx[a], p[a]); }
}
// The hien tuong ung trong world (cung ten model, cung vi tri).
for (const WorldModel& m : w.models) {
if (m.model_name != g.model_name || std::fabs(m.x - in.x) > 1e-6 ||
std::fabs(m.y - in.y) > 1e-6) {
continue;
}
double amn[3], amx[3];
ASSERT_TRUE(AssimpModelBounds(m, amn, amx)) << m.name;
for (int a = 0; a < 3; ++a) {
EXPECT_NEAR(amn[a], rmn[a], 0.01) << m.name << " truc " << a;
EXPECT_NEAR(amx[a], rmx[a], 0.01) << m.name << " truc " << a;
}
checked++;
break;
}
}
EXPECT_GE(checked, 4u) << "phai kiem du OBJ met, OBJ cm, DAE matrix, DAE Y_UP";
}
TEST(SimGl, BoMoPhongCoAnhIrVaDepthKhopRaycastCpu) {
// Cung tu the tren world doc: mot sim ve bang GPU, mot sim raycast CPU. Tat
// nhieu depth de so thang. Hinh hoc CPU la 2.5D (tuong ep tu luoi) nen chi
// so TRUNG VI -- san, mat doc va tuong phai khop toi vai cm.
auto make = [](bool gpu, std::string* why, CameraFrame* out) -> bool {
SimConfig c;
c.speed = 0.0;
c.world_path = WorldDir() + "/ramp.world";
c.world_model_paths = {WorldDir() + "/models"};
c.enable_depth_camera = true;
c.depth_disparity_noise_px = 0.0;
c.depth_noise_floor = 0.0;
c.camera_z = 0.445;
c.camera_x = 0.095;
c.camera_pitch = 0.0;
c.wheel_radius = 0.0865;
c.start_x = -1.5;
SimSource sim(c);
if (!sim.Open().ok()) { *why = "sim khong mo duoc"; return false; }
if (gpu) {
auto r = MakeRenderer(sim.world(), why);
if (!r) return false;
sim.SetCameraRenderer(r);
}
bool have = false;
sim.camera()->SetCallback([&](const CameraFrame& f) { *out = f; have = true; });
sim.camera()->Open();
sim.RunFor(0.1);
if (!have) *why = "khong co khung camera";
return have;
};
std::string why;
CameraFrame gpu, cpu;
if (!make(true, &why, &gpu)) GTEST_SKIP() << why;
ASSERT_TRUE(make(false, &why, &cpu)) << why;
ASSERT_TRUE(gpu.infra1 != nullptr) << "renderer GPU phai cho anh IR";
EXPECT_TRUE(cpu.infra1 == nullptr) << "raycast CPU khong co anh IR";
ASSERT_EQ(gpu.infra1->width, gpu.depth->width);
ASSERT_EQ(gpu.infra1->height, gpu.depth->height);
const size_t n = static_cast<size_t>(gpu.depth->width) * gpu.depth->height;
std::vector<double> diff;
for (size_t i = 0; i < n; ++i) {
const uint16_t a = static_cast<uint16_t>(gpu.depth->data[2 * i] | (gpu.depth->data[2 * i + 1] << 8));
const uint16_t b = static_cast<uint16_t>(cpu.depth->data[2 * i] | (cpu.depth->data[2 * i + 1] << 8));
if (a == 0 || b == 0) continue;
diff.push_back(std::fabs(static_cast<double>(a) - b) * 0.001);
}
ASSERT_GT(diff.size(), n / 4) << "qua it diem co depth o ca hai";
std::nth_element(diff.begin(), diff.begin() + diff.size() / 2, diff.end());
EXPECT_LT(diff[diff.size() / 2], 0.02) << "trung vi |depth GPU - depth CPU|";
}