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:
@@ -9,6 +9,7 @@ find_package(yaml-cpp REQUIRED)
|
||||
vslam_add_library(vslam_sim
|
||||
PUBLIC_HDRS
|
||||
sim_source.h
|
||||
sim_camera_renderer.h
|
||||
occupancy_map_io.h
|
||||
gazebo_world.h
|
||||
PUBLIC_DEPS
|
||||
|
||||
51
libs/vslam_sim/include/vslam/sim_camera_renderer.h
Normal file
51
libs/vslam_sim/include/vslam/sim_camera_renderer.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef VSLAM_SIM_CAMERA_RENDERER_H
|
||||
#define VSLAM_SIM_CAMERA_RENDERER_H
|
||||
|
||||
// Ve anh camera cho bo mo phong.
|
||||
//
|
||||
// Interface nay KHONG phu thuoc do hoa: vslam_sim chi biet no, con cai dat GPU
|
||||
// (EGL + OpenGL ES, texture that cua model) nam o lib TUY CHON vslam_sim_gl.
|
||||
// Nho vay bo mo phong van build va chay tren Jetson / CI khong co GPU -- khi
|
||||
// khong co renderer thi camera mo phong ve depth bang raycast CPU nhu cu va
|
||||
// khong co anh IR.
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace vslam {
|
||||
|
||||
// Tu the va noi suy cua MOT khung anh.
|
||||
//
|
||||
// Frame QUANG HOC theo quy uoc anh (giong RealSense va depth cua sim): +X
|
||||
// phai, +Y xuong, +Z toi truoc. Pixel (u, v) co TAM tai toa do nguyen, kieu
|
||||
// OpenCV: tia qua pixel u di theo ((u - cx) / fx, (v - cy) / fy, 1).
|
||||
struct SimCameraView {
|
||||
// Hang-chinh. Cot j la truc j cua frame quang hoc, viet trong world.
|
||||
double R_wc[9] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
|
||||
double t_wc[3] = {0, 0, 0}; // tam camera trong world, m
|
||||
double fx = 0, fy = 0, cx = 0, cy = 0;
|
||||
int width = 0, height = 0;
|
||||
double near_m = 0.05, far_m = 30.0;
|
||||
};
|
||||
|
||||
class SimCameraRenderer {
|
||||
public:
|
||||
virtual ~SimCameraRenderer() = default;
|
||||
|
||||
// Ve mot khung. `ir`: width*height byte, anh xam, HANG TREN CUNG TRUOC.
|
||||
// `depth_z`: width*height float, Z-DEPTH (hinh chieu len truc quang) theo
|
||||
// met; 0 = tia khong trung gi. Tra false neu khong ve duoc.
|
||||
//
|
||||
// ⚠️ Context GPU gan voi LUONG tao ra no: moi lan goi phai tren CUNG mot
|
||||
// luong (luong vong mo phong).
|
||||
virtual bool Render(const SimCameraView& view, std::vector<uint8_t>* ir,
|
||||
std::vector<float>* depth_z) = 0;
|
||||
|
||||
// Mo ta ngan cho log, vd "NVIDIA GeForce RTX 4060 (EGL, GLES 3.2)".
|
||||
virtual std::string Describe() const = 0;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_SIM_CAMERA_RENDERER_H
|
||||
@@ -42,6 +42,7 @@
|
||||
|
||||
#include "vslam/clock.h"
|
||||
#include "vslam/gazebo_world.h"
|
||||
#include "vslam/sim_camera_renderer.h"
|
||||
#include "vslam/driver_interfaces.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
@@ -366,6 +367,12 @@ class SimSource {
|
||||
using GroundTruthCallback = std::function<void(UnixTime, const SimPose3&)>;
|
||||
void SetGroundTruthCallback(GroundTruthCallback cb, double rate_hz = 0.0);
|
||||
|
||||
// Ve camera bang renderer ngoai (GPU, vslam_sim_gl): depth VA anh IR
|
||||
// (`CameraFrame::infra1`) tu mesh co texture, thay raycast CPU. null = raycast
|
||||
// CPU nhu cu, khong co anh IR. Goi truoc Run(); renderer duoc goi tren luong
|
||||
// vong mo phong.
|
||||
void SetCameraRenderer(std::shared_ptr<SimCameraRenderer> r);
|
||||
|
||||
// Danh dau mot vung hinh chu nhat (toa do THE GIOI, met) la KINH.
|
||||
//
|
||||
// O kinh phai la o BI CHIEM trong ban do (data >= 50) -- kinh la vat can
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user