This commit is contained in:
trung09122002
2026-09-19 15:33:52 +07:00
parent 48bd5377dc
commit 3b281174d0
22 changed files with 1913 additions and 119 deletions

View File

@@ -4,6 +4,9 @@
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <limits>
#include <vector>
#include <fstream>
#include <map>
#include <sstream>
@@ -736,6 +739,133 @@ StatusResponse RasterizeGazeboWorld(const GazeboWorld& world,
// ---------------------------------------------------------------------------
StatusResponse BuildWorldSegments(const GazeboWorld& world,
const WorldRasterOptions& opt,
const OccupancyGrid& grid,
WorldSegments* out) {
if (out == nullptr) {
return StatusResponse::Error(StatusCode::kInvalidArgument, "out == null");
}
out->seg.clear();
out->cell_items.clear();
out->cell_start.assign(static_cast<size_t>(grid.width) * grid.height + 1, 0);
out->width = grid.width;
out->height = grid.height;
out->resolution = grid.resolution;
out->origin_x = grid.origin.position.x;
out->origin_y = grid.origin.position.y;
const double res = grid.resolution;
const double ox = out->origin_x, oy = out->origin_y;
const int W = static_cast<int>(grid.width), H = static_cast<int>(grid.height);
// --- 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
// tam giac mat tren chieu xuong) khong gay hai: chung luon nam XA hon mat
// truoc doc theo tia, va phep cat lay t NHO NHAT.
std::map<std::string, std::vector<Tri>> cache;
for (const WorldModel& m : world.models) {
if (m.collision_obj.empty()) continue;
auto it = cache.find(m.model_name);
if (it == cache.end()) {
std::vector<Tri> tris;
const bool ok = IsDae(m.collision_obj)
? ReadDaeTriangles(m.collision_obj, &tris)
: ReadObjTriangles(m.collision_obj, &tris);
if (!ok) tris.clear();
it = cache.emplace(m.model_name, std::move(tris)).first;
}
if (it->second.empty()) continue;
const double cy = std::cos(m.yaw), sny = std::sin(m.yaw);
for (const Tri& t0 : it->second) {
double X[3], Y[3], Z[3];
for (int k = 0; k < 3; ++k) {
const double sx = t0.x[k] * m.sx, sy = t0.y[k] * m.sy;
X[k] = sx * cy - sy * sny + m.x;
Y[k] = sx * sny + sy * cy + m.y;
Z[k] = t0.z[k] * m.sz + m.z;
}
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;
for (int e = 0; e < 3; ++e) {
const int a = e, b = (e + 1) % 3;
out->seg.push_back(static_cast<float>(X[a]));
out->seg.push_back(static_cast<float>(Y[a]));
out->seg.push_back(static_cast<float>(X[b]));
out->seg.push_back(static_cast<float>(Y[b]));
}
}
}
const size_t n_seg = out->seg.size() / 4;
if (n_seg == 0) {
return StatusResponse::Error(StatusCode::kNotFound,
"khong dung duoc doan nao tu mesh va cham");
}
// --- Chi muc CSR: hai luot, dem roi dien ---
//
// Moi doan duoc ghi vao MOI O no cat qua, di bang DDA supercover. Dung bao
// loi cua doan se re hon nhung mot buc tuong cheo dai 5 m se dinh vao hang
// nghin o no khong he cham.
auto walk = [&](size_t s, const std::function<void(int, int)>& visit) {
double ax = out->seg[s * 4], ay = out->seg[s * 4 + 1];
double bx = out->seg[s * 4 + 2], by = out->seg[s * 4 + 3];
double px = (ax - ox) / res, py = (ay - oy) / res;
const double qx = (bx - ox) / res, qy = (by - oy) / res;
int cx = static_cast<int>(std::floor(px));
int cyi = static_cast<int>(std::floor(py));
const int ex = static_cast<int>(std::floor(qx));
const int ey = static_cast<int>(std::floor(qy));
const double dx = qx - px, dy = qy - py;
const int sx = dx > 0 ? 1 : (dx < 0 ? -1 : 0);
const int sy = dy > 0 ? 1 : (dy < 0 ? -1 : 0);
const double inf = std::numeric_limits<double>::infinity();
const double tdx = dx != 0.0 ? std::fabs(1.0 / dx) : inf;
const double tdy = dy != 0.0 ? std::fabs(1.0 / dy) : inf;
double tmx = dx != 0.0 ? ((dx > 0 ? (cx + 1 - px) : (px - cx)) * tdx) : inf;
double tmy = dy != 0.0 ? ((dy > 0 ? (cyi + 1 - py) : (py - cyi)) * tdy) : inf;
for (int guard = 0; guard < 4096; ++guard) {
if (cx >= 0 && cyi >= 0 && cx < W && cyi < H) visit(cx, cyi);
if (cx == ex && cyi == ey) break;
if (tmx < tmy) { tmx += tdx; cx += sx; }
else { tmy += tdy; cyi += sy; }
if (tmx > 1.0 && tmy > 1.0) {
if (cx >= 0 && cyi >= 0 && cx < W && cyi < H) visit(cx, cyi);
break;
}
}
};
for (size_t s = 0; s < n_seg; ++s) {
walk(s, [&](int cx, int cyi) {
out->cell_start[static_cast<size_t>(cyi) * grid.width + cx + 1]++;
});
}
for (size_t i = 1; i < out->cell_start.size(); ++i) {
out->cell_start[i] += out->cell_start[i - 1];
}
out->cell_items.assign(out->cell_start.back(), 0);
std::vector<uint32_t> fill(out->cell_start.begin(), out->cell_start.end() - 1);
for (size_t s = 0; s < n_seg; ++s) {
walk(s, [&](int cx, int cyi) {
out->cell_items[fill[static_cast<size_t>(cyi) * grid.width + cx]++] =
static_cast<uint32_t>(s);
});
}
VSLAM_INFO("world -> doan: " << n_seg << " doan, " << out->cell_items.size()
<< " muc chi muc (" << (out->seg.size() * 4 +
out->cell_items.size() * 4) / 1048576.0
<< " MB)");
return StatusResponse::Ok();
}
// ---------------------------------------------------------------------------
StatusResponse ExportWorldMeshes(const GazeboWorld& world, bool visual,
WorldMeshes* out) {
if (out == nullptr) {

View File

@@ -168,6 +168,10 @@ struct SimSource::Impl {
SimConfig cfg;
OccupancyGrid map;
GazeboWorld world;
// Doan 2D cua the gioi, de tra ve khoang cach TRUNG MAT TUONG thay vi trung
// bien o. Rong khi chay tu `--map` (chi co luoi, khong co mesh) -- khi do
// moi raycast giu nguyen ket qua luoi nhu cu.
WorldSegments segments;
SimLidar lidar;
SimMcu mcu;
@@ -213,6 +217,81 @@ struct SimSource::Impl {
explicit Impl(SimConfig c) : cfg(std::move(c)) {}
// --- Tinh chinh khoang cach bang doan 2D ----------------------------------
//
// ⚠️ DDA tra ve khoang cach toi MAT VAO cua o bi chiem, khong phai toi mat
// tuong. O do duoc danh dau khi tam giac di qua TAM o, nen mat vao ay nam
// lui vao trong tuong toi mot o day du.
//
// Do duoc o luoi 0.05 m: moi tia dung som, p50 = 3.7 cm, p90 = 5.6 cm so voi
// mat tuong nhin thay. Lam luoi min hon chi thu nho con so do chu khong xoa
// duoc no -- van la luong tu hoa.
//
// Nen: giu DDA de tim O trung (kinh, vuc, va cham deu van doc luoi), roi CAT
// chinh xac voi doan nam trong vung do.
//
// Tra ve NaN khi khong cat duoc gi -- khi do giu nguyen ket qua cua luoi.
// Do KHONG phai truong hop la: `--map` khong co mesh, va vai o bi chiem den
// tu `SetGlass`/tuong chen tay chu khong tu tam giac nao.
double RefineHit(double ox, double oy, double dx, double dy, double t_grid,
double max_range) const {
if (segments.empty()) return std::numeric_limits<double>::quiet_NaN();
const double res = segments.resolution;
const int W = static_cast<int>(segments.width);
const int H = static_cast<int>(segments.height);
// Quet vai o quanh cho DDA bao trung. Lui MOT o vi mat tuong that co the
// nam truoc mat vao (tuong cheo di qua o ma khong qua tam o do), va tien
// BA o vi no co the nam sau.
int cells[16][2];
int n_cells = 0;
for (int k = -2; k <= 6; ++k) {
const double t = t_grid + k * res * 0.5;
if (t < 0.0) continue;
const int cx = static_cast<int>(
std::floor((ox + dx * t - segments.origin_x) / res));
const int cy = static_cast<int>(
std::floor((oy + dy * t - segments.origin_y) / res));
if (cx < 0 || cy < 0 || cx >= W || cy >= H) continue;
bool dup = false;
for (int i = 0; i < n_cells; ++i) {
if (cells[i][0] == cx && cells[i][1] == cy) { dup = true; break; }
}
if (!dup && n_cells < 16) { cells[n_cells][0] = cx; cells[n_cells][1] = cy; n_cells++; }
}
double best = std::numeric_limits<double>::infinity();
for (int i = 0; i < n_cells; ++i) {
const size_t idx = static_cast<size_t>(cells[i][1]) * segments.width +
static_cast<size_t>(cells[i][0]);
for (uint32_t j = segments.cell_start[idx]; j < segments.cell_start[idx + 1];
++j) {
const uint32_t s = segments.cell_items[j];
const double ax = segments.seg[s * 4], ay = segments.seg[s * 4 + 1];
const double ex = segments.seg[s * 4 + 2] - ax;
const double ey = segments.seg[s * 4 + 3] - ay;
const double den = ex * dy - ey * dx;
if (std::fabs(den) < 1e-12) continue; // song song
const double wx = ax - ox, wy = ay - oy;
const double t = (ex * wy - ey * wx) / den;
if (t < 0.0 || t > max_range || t >= best) continue;
const double u = (dx * wy - dy * wx) / den;
if (u < 0.0 || u > 1.0) continue;
best = t;
}
}
if (!std::isfinite(best)) return std::numeric_limits<double>::quiet_NaN();
return best;
}
// Ket qua da tinh chinh, hoac chinh `t_grid` neu khong tinh chinh duoc.
double Refined(double ox, double oy, double angle, double t_grid,
double max_range) const {
const double r = RefineHit(ox, oy, std::cos(angle), std::sin(angle), t_grid,
max_range);
return std::isnan(r) ? t_grid : r;
}
// --- Raycast tren occupancy grid -----------------------------------------
// Thuat toan DDA: di tung o thay vi lay mau theo buoc co dinh. Lay mau theo
// buoc co dinh se BO SOT tuong mong khi buoc lon hon mot o, va do la loi im
@@ -260,7 +339,7 @@ struct SimSource::Impl {
static_cast<size_t>(cx);
const bool is_glass = cidx < glass.size() && glass[cidx] != 0;
if (cell >= 50 && !is_glass) {
return t * res;
return Refined(ox, oy, angle, t * res, max_range);
}
if (t_max_x < t_max_y) {
t = t_max_x;
@@ -393,7 +472,12 @@ struct SimSource::Impl {
// KHONG kiem glass: sieu am thay moi vat can, ke ca kinh.
if (map.data[static_cast<size_t>(cy) * map.width +
static_cast<size_t>(cx)] >= 50) {
return t;
// Cung tinh chinh nhu LiDAR. Voi sieu am con quan trong hon theo ti
// le: nguong `us_stop_distance` chi 0.30 m, nen lech 3 cm la 10 %.
//
// Buc KINH do `--sim-glass-at` chen tay vao luoi khong co tam giac
// nao, nen RefineHit() tra NaN va ta giu ket qua luoi -- dung.
return Refined(ox, oy, angle, t, max_range);
}
}
return std::numeric_limits<double>::infinity();
@@ -486,7 +570,12 @@ struct SimSource::Impl {
const double lo = fz, hi = fz + cfg.wall_height;
if ((z_enter >= lo && z_enter <= hi) || (z_exit >= lo && z_exit <= hi) ||
(z_enter > hi && z_exit < lo) || (z_enter < lo && z_exit > hi)) {
return std::max(t_enter, 0.0) * res;
// Chi tinh chinh theo mat phang XY: tuong trong dai z la thang dung,
// nen cat trong mat bang la du. Nhanh cham SAN o duoi da noi suy
// dung tu truoc, khong can dong toi.
const double t_xy = std::max(t_enter, 0.0) * res;
const double r = RefineHit(ox, oy, dx, dy, t_xy, max_range);
return std::isnan(r) ? t_xy : r;
}
}
@@ -935,6 +1024,16 @@ StatusResponse SimSource::Open() {
WorldRasterOptions wo;
const StatusResponse rs = RasterizeGazeboWorld(impl_->world, wo, &impl_->map);
if (!rs.ok()) return rs;
// Chi muc doan 2D, de khoang cach tra ve trung MAT TUONG chu khong trung
// bien o. Khong dung duoc thi van chay -- chi la moi tia dung som toi mot
// o, y nhu truoc day.
const StatusResponse ss =
BuildWorldSegments(impl_->world, wo, impl_->map, &impl_->segments);
if (!ss.ok()) {
VSLAM_WARN("sim: khong dung duoc chi muc doan (" << ss.message
<< ") -- raycast se luong tu hoa theo o "
<< impl_->map.resolution << " m");
}
} else if (impl_->cfg.map_yaml.empty()) {
impl_->map = MakeSyntheticWorld(impl_->cfg.synthetic_width,
impl_->cfg.synthetic_height,
@@ -986,6 +1085,10 @@ const std::vector<uint8_t>& SimSource::glass_mask() const { return impl_->glass;
const GazeboWorld& SimSource::world() const { return impl_->world; }
double SimSource::RangeForTest(double x, double y, double angle) const {
return impl_->Raycast(x, y, angle, impl_->cfg.lidar_range_max);
}
void SimSource::SetMapForTest(const OccupancyGrid& m) {
impl_->map = m;
impl_->floor_z.assign(static_cast<size_t>(m.width) * m.height, 0.0f);