sim: dia hinh 3D -- doc va san cao tu model vslam_terrain_*, robot di tren mat san, LiDAR tia 3D, ground truth 6-DoF, world slope_test (5 doc 5-30 do)

This commit is contained in:
trung09122002
2026-10-03 18:31:13 +07:00
parent be2aa17e3e
commit 26ee1a3b72
27 changed files with 2373 additions and 37 deletions

View File

@@ -155,6 +155,7 @@ StatusResponse LoadGazeboWorld(const std::string& world_path,
if (uri.empty()) { skip++; continue; }
m.model_name = uri.substr(uri.rfind('/') + 1);
m.terrain = m.model_name.rfind(kTerrainModelPrefix, 0) == 0;
std::string root;
const std::string dir = ResolveModelUri(uri, roots, &root);
@@ -597,6 +598,12 @@ bool ReadObjTriangles(const std::string& path, std::vector<Tri>* out) {
StatusResponse RasterizeGazeboWorld(const GazeboWorld& world,
const WorldRasterOptions& opt,
OccupancyGrid* out) {
return RasterizeGazeboWorld(world, opt, out, nullptr);
}
StatusResponse RasterizeGazeboWorld(const GazeboWorld& world,
const WorldRasterOptions& opt,
OccupancyGrid* out, WorldTerrain* terrain) {
if (!(opt.resolution > 0.0)) {
return StatusResponse::Error(StatusCode::kInvalidArgument,
"resolution phai duong");
@@ -675,15 +682,70 @@ StatusResponse RasterizeGazeboWorld(const GazeboWorld& world,
// o deu da biet -- cai chua duoc quet la chuyen cua ban do SLAM.
out->data.assign(static_cast<size_t>(w) * h, 0);
// --- Luot 2: to tam giac trong dai do cao ---
// --- Luot 1.5: dia hinh -> do cao san ---
//
// Do cao mat tren tai TAM o, noi suy trong tam giac. Tam giac dung thang
// (suy bien tren mat bang) la mat ben cua doc/bac, khong dinh nghia san nen
// bo qua. Chong nhau thi lay CAO NHAT: mot hop kin co ca mat day lan mat tren.
const size_t n_cells = static_cast<size_t>(w) * h;
std::vector<float> ground(n_cells, 0.0f);
std::vector<uint8_t> covered(n_cells, 0);
size_t n_terrain_models = 0, n_terrain_cells = 0;
for (const Loaded& L : loaded) {
if (!L.m->terrain) continue;
n_terrain_models++;
for (const Tri& t : L.tris) {
const double ax = t.x[0], ay = t.y[0];
const double bx = t.x[1], by = t.y[1];
const double cx = t.x[2], cy = t.y[2];
const double den = (by - cy) * (ax - cx) + (cx - bx) * (ay - cy);
if (std::fabs(den) < 1e-12) continue;
const int cx0 = std::max(0, static_cast<int>((std::min({ax, bx, cx}) - minx) / res));
const int cx1 = std::min(static_cast<int>(w) - 1,
static_cast<int>((std::max({ax, bx, cx}) - minx) / res));
const int cy0 = std::max(0, static_cast<int>((std::min({ay, by, cy}) - miny) / res));
const int cy1 = std::min(static_cast<int>(h) - 1,
static_cast<int>((std::max({ay, by, cy}) - miny) / res));
for (int gy = cy0; gy <= cy1; ++gy) {
for (int gx = cx0; gx <= cx1; ++gx) {
const double px = minx + (gx + 0.5) * res;
const double py = miny + (gy + 0.5) * res;
const double l1 = ((by - cy) * (px - cx) + (cx - bx) * (py - cy)) / den;
const double l2 = ((cy - ay) * (px - cx) + (ax - cx) * (py - cy)) / den;
const double l3 = 1.0 - l1 - l2;
if (l1 < -1e-9 || l2 < -1e-9 || l3 < -1e-9) continue;
const float z = static_cast<float>(l1 * t.z[0] + l2 * t.z[1] + l3 * t.z[2]);
const size_t idx = static_cast<size_t>(gy) * w + gx;
if (!covered[idx]) {
covered[idx] = 1;
ground[idx] = z;
n_terrain_cells++;
} else if (z > ground[idx]) {
ground[idx] = z;
}
}
}
}
}
// O khong co dia hinh la san 0, nen min/max luon tinh ca 0. World phang:
// fmin = fmax = 0 va luot 2 duoi day y het truoc.
float fmin = 0.0f, fmax = 0.0f;
for (float f : ground) { fmin = std::min(fmin, f); fmax = std::max(fmax, f); }
// --- Luot 2: to tam giac VAT CAN trong dai do cao, tinh tu san cuc bo ---
std::vector<float> top(n_cells, 0.0f);
size_t n_used = 0;
for (const Loaded& L : loaded) {
if (L.m->terrain) continue; // dia hinh la san, khong phai vat can
for (const Tri& t : L.tris) {
// Tam giac nam hoan toan ngoai dai thi bo. Cat dung tam giac la thua:
// mesh day dac nen mot tam giac bien bi bo van con hang xom phu.
//
// Day la phep loc THO theo dai cua ca ban do; dai dung cua tung o kiem
// ben duoi, theo san cua chinh o do.
const double tz0 = std::min({t.z[0], t.z[1], t.z[2]});
const double tz1 = std::max({t.z[0], t.z[1], t.z[2]});
if (tz1 < opt.z_min || tz0 > opt.z_max) continue;
if (tz1 < fmin + opt.z_min || tz0 > fmax + opt.z_max) continue;
n_used++;
// Bao loi cua tam giac, roi kiem tung o bang toa do trong tam.
@@ -721,7 +783,13 @@ StatusResponse RasterizeGazeboWorld(const GazeboWorld& world,
inside = l1 >= -1e-9 && l2 >= -1e-9 && l3 >= -1e-9;
}
if (inside) {
out->data[static_cast<size_t>(gy) * w + gx] = 100;
const size_t idx = static_cast<size_t>(gy) * w + gx;
// Dai z cua RIENG o nay. World phang: san 0, trung phep loc tho
// o tren nen khong bo them o nao.
const double f = ground[idx];
if (tz1 < f + opt.z_min || tz0 > f + opt.z_max) continue;
out->data[idx] = 100;
if (tz1 > top[idx]) top[idx] = static_cast<float>(tz1);
}
}
}
@@ -734,6 +802,18 @@ StatusResponse RasterizeGazeboWorld(const GazeboWorld& world,
<< minx << ", " << miny << "), " << n_mesh << " mesh, " << n_tri
<< " tam giac (" << n_used << " trong dai z ["
<< opt.z_min << ", " << opt.z_max << "]), " << occ << " o bi chiem");
if (n_terrain_models > 0) {
VSLAM_INFO("world -> dia hinh: " << n_terrain_models << " model, "
<< n_terrain_cells << " o, san tu " << fmin << " den " << fmax
<< " m; dai z cua vat can tinh tu san cuc bo");
}
if (terrain != nullptr) {
terrain->floor_z = std::move(ground);
terrain->wall_top = std::move(top);
terrain->terrain_cells = n_terrain_cells;
terrain->floor_min = fmin;
terrain->floor_max = fmax;
}
return StatusResponse::Ok();
}
@@ -742,7 +822,8 @@ StatusResponse RasterizeGazeboWorld(const GazeboWorld& world,
StatusResponse BuildWorldSegments(const GazeboWorld& world,
const WorldRasterOptions& opt,
const OccupancyGrid& grid,
WorldSegments* out) {
WorldSegments* out,
const std::vector<float>* floor_z) {
if (out == nullptr) {
return StatusResponse::Error(StatusCode::kInvalidArgument, "out == null");
}
@@ -759,6 +840,18 @@ StatusResponse BuildWorldSegments(const GazeboWorld& world,
const double ox = out->origin_x, oy = out->origin_y;
const int W = static_cast<int>(grid.width), H = static_cast<int>(grid.height);
// San cuc bo tai mot diem, de dai z truot theo dia hinh. Khong co ban do
// san thi la 0 -- y het truoc.
const bool have_floor =
floor_z != nullptr && floor_z->size() == static_cast<size_t>(W) * H;
auto floor_at = [&](double x, double y) -> double {
if (!have_floor) return 0.0;
const int fx = static_cast<int>(std::floor((x - ox) / res));
const int fy = static_cast<int>(std::floor((y - oy) / res));
if (fx < 0 || fy < 0 || fx >= W || fy >= H) return 0.0;
return (*floor_z)[static_cast<size_t>(fy) * grid.width + fx];
};
// --- Gom doan tu canh cua tam giac trong dai z ---
//
// Lay CA BA canh cua moi tam giac. Canh nam trong long vat the (vd canh cua
@@ -767,6 +860,7 @@ StatusResponse BuildWorldSegments(const GazeboWorld& world,
std::map<std::string, std::vector<Tri>> cache;
for (const WorldModel& m : world.models) {
if (m.collision_obj.empty()) continue;
if (m.terrain) continue; // dia hinh la san: tia cham no o raycast 3D
auto it = cache.find(m.model_name);
if (it == cache.end()) {
std::vector<Tri> tris;
@@ -789,7 +883,8 @@ StatusResponse BuildWorldSegments(const GazeboWorld& world,
}
const double zlo = std::min({Z[0], Z[1], Z[2]});
const double zhi = std::max({Z[0], Z[1], Z[2]});
if (zhi < opt.z_min || zlo > opt.z_max) continue;
const double f = floor_at((X[0] + X[1] + X[2]) / 3.0, (Y[0] + Y[1] + Y[2]) / 3.0);
if (zhi < f + opt.z_min || zlo > f + opt.z_max) continue;
for (int e = 0; e < 3; ++e) {
const int a = e, b = (e + 1) % 3;
out->seg.push_back(static_cast<float>(X[a]));