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

@@ -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);