sim: camera ve bang GPU (lib tuy chon vslam_sim_gl, EGL + GLES3 + assimp) -- anh IR co texture that + depth khop tung pixel, vslam_node --sim-gl, tool vslam_render_preview

This commit is contained in:
trung09122002
2026-10-03 19:13:50 +07:00
parent 0dd2546ab1
commit f9fd18b5aa
17 changed files with 1329 additions and 22 deletions

View File

@@ -224,6 +224,12 @@ struct SimSource::Impl {
// dung chung se bi khung sau ghi de giua chung.
std::shared_ptr<BufferPool<std::vector<uint8_t>>> depth_pool =
BufferPool<std::vector<uint8_t>>::Create(8);
// Renderer GPU tuy chon (SetCameraRenderer). null = raycast CPU, khong co IR.
std::shared_ptr<SimCameraRenderer> renderer;
std::vector<uint8_t> gl_ir;
std::vector<float> gl_depth;
bool gl_fail_logged = false;
std::shared_ptr<ReplayClock> clock;
// Trang thai that cua robot.
@@ -1052,6 +1058,61 @@ struct SimSource::Impl {
cz_w += rz;
}
// Huong (don vi) trong THE GIOI cua mot huong trong frame quang hoc.
// Quang hoc -> than robot: +Z_quang = +X_than, +X_quang = -Y_than,
// +Y_quang = -Z_than. Roi chuc xuong `camera_pitch`, roi xoay theo than.
// Raycast CPU va renderer GPU dung CHUNG ham nay, nen tia cua hai duong
// trung nhau tung pixel.
auto to_world = [&](double bx0, double by0, double bz0, double* wx, double* wy,
double* wz) {
if (terrain_on) {
// Chuc camera tren than (quanh y than), roi xoay theo than.
const double mx = bx0 * ccp + bz0 * csp;
const double mz = -bx0 * csp + bz0 * ccp;
R_body.Apply(mx, by0, mz, wx, wy, wz);
} else {
// Nghieng ngang cua than (roll) truoc, roi chuc xuong (pitch).
const double by1 = by0 * cr_b - bz0 * sr_b;
const double bz1 = by0 * sr_b + bz0 * cr_b;
const double px = bx0 * cp + bz1 * sp;
*wz = -bx0 * sp + bz1 * cp;
const double py = by1;
*wx = px * cy_r - py * sy_r;
*wy = px * sy_r + py * cy_r;
}
};
// Renderer GPU: ve CA khung mot lan (depth + IR), vong lap ben duoi chi
// ap nhieu. Ve hong thi khung nay quay ve raycast CPU.
bool use_gl = false;
if (renderer) {
SimCameraView view;
// Cot j cua R_wc = truc j cua frame quang hoc trong world.
double ax[3][3];
to_world(0.0, -1.0, 0.0, &ax[0][0], &ax[0][1], &ax[0][2]); // +X quang
to_world(0.0, 0.0, -1.0, &ax[1][0], &ax[1][1], &ax[1][2]); // +Y quang
to_world(1.0, 0.0, 0.0, &ax[2][0], &ax[2][1], &ax[2][2]); // +Z quang
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) view.R_wc[i * 3 + j] = ax[j][i];
}
view.t_wc[0] = cx_w;
view.t_wc[1] = cy_w;
view.t_wc[2] = cz_w;
view.fx = fx;
view.fy = fy;
view.cx = ppx;
view.cy = ppy;
view.width = static_cast<int>(w);
view.height = static_cast<int>(h);
view.far_m = std::max(cfg.depth_far, 1.0) + 1.0;
use_gl = renderer->Render(view, &gl_ir, &gl_depth);
if (!use_gl && !gl_fail_logged) {
gl_fail_logged = true;
VSLAM_WARN("sim: renderer GPU ve hong -- camera quay ve raycast CPU, KHONG co anh IR");
}
}
// Khong dung mot `normal_distribution` chung: sigma phu thuoc KHOANG
// CACH cua tung diem anh, nen phan phoi duoc dung ngay trong vong lap.
std::uniform_real_distribution<double> uni01(0.0, 1.0);
@@ -1064,28 +1125,16 @@ struct SimSource::Impl {
const double oz = 1.0;
const double inv = 1.0 / std::sqrt(ox * ox + oy * oy + oz * oz);
// Quang hoc -> than robot: +Z_quang = +X_than, +X_quang = -Y_than,
// +Y_quang = -Z_than. Roi chuc xuong `camera_pitch`, roi xoay theo yaw.
const double bx0 = oz * inv, by0 = -ox * inv, bz0 = -oy * inv;
double wx, wy, pz;
if (terrain_on) {
// Chuc camera tren than (quanh y than), roi xoay theo than.
const double mx = bx0 * ccp + bz0 * csp;
const double mz = -bx0 * csp + bz0 * ccp;
R_body.Apply(mx, by0, mz, &wx, &wy, &pz);
double d;
if (use_gl) {
// GPU tra thang Z-DEPTH; 0 = khong trung gi.
const float zg = gl_depth[static_cast<size_t>(v) * w + u];
d = zg > 0.0f ? static_cast<double>(zg) : std::numeric_limits<double>::infinity();
} else {
// Nghieng ngang cua than (roll) truoc, roi chuc xuong (pitch).
const double by1 = by0 * cr_b - bz0 * sr_b;
const double bz1 = by0 * sr_b + bz0 * cr_b;
const double px = bx0 * cp + bz1 * sp;
pz = -bx0 * sp + bz1 * cp;
const double py = by1;
wx = px * cy_r - py * sy_r;
wy = px * sy_r + py * cy_r;
double wx, wy, pz;
to_world(oz * inv, -ox * inv, -oy * inv, &wx, &wy, &pz);
d = RaycastDepth(cx_w, cy_w, cz_w, wx, wy, pz, cfg.depth_far);
}
double d = RaycastDepth(cx_w, cy_w, cz_w, wx, wy, pz, cfg.depth_far);
uint16_t mm = 0; // 0 = khong do duoc, dung quy uoc cua RealSense
// Depth dropout: D410 that mat diem o be mat toi, be mat bong, va o
@@ -1110,7 +1159,9 @@ struct SimSource::Impl {
// dung ra phang o z=0 bi cliff_detect khop thanh z0 = -0.254 m,
// doc +0.114 -- va 87/1903 tia trong cua so roi duoi nguong, tuc
// BAO VUC GIA tren san hoan toan phang, MAD 0.037 van lot cong.
d *= inv;
//
// GPU da tra Z-depth roi -- nhan them la sai lan nua.
if (!use_gl) d *= inv;
// Nhieu depth theo MO HINH STEREO, khong phai hang so.
//
@@ -1173,6 +1224,21 @@ struct SimSource::Impl {
img->depth_scale = 0.001f;
img->data = depth_buf.data();
img->keep_alive = buf;
if (use_gl) {
// Anh IR cung kich thuoc, cung K va cung stamp voi depth -- giong
// RealSense: hai anh IR va depth thang hang tung pixel.
auto ibuf = depth_pool->Acquire();
ibuf->assign(gl_ir.begin(), gl_ir.end());
auto ir = std::make_shared<Image>();
ir->header = img->header;
ir->width = w;
ir->height = h;
ir->step = w;
ir->format = PixelFormat::kMono8;
ir->data = ibuf->data();
ir->keep_alive = ibuf;
out->infra1 = ir;
}
out->header = img->header;
out->depth = img;
@@ -1656,6 +1722,11 @@ void SimSource::SetFloorRamp(double x0, double y0, double x1, double y1,
bool SimSource::terrain_active() const { return impl_->terrain_on; }
void SimSource::SetCameraRenderer(std::shared_ptr<SimCameraRenderer> r) {
impl_->renderer = std::move(r);
impl_->gl_fail_logged = false;
}
void SimSource::SetGroundTruthCallback(GroundTruthCallback cb, double rate_hz) {
impl_->gt_cb = std::move(cb);
impl_->gt_period = rate_hz > 0.0 ? 1.0 / rate_hz : 0.0;