Files
vslam_cpp/libs/vslam_sim/include/vslam/gazebo_world.h
trung09122002 48bd5377dc upload
2026-09-18 18:14:42 +07:00

153 lines
5.8 KiB
C++

#ifndef VSLAM_GAZEBO_WORLD_H
#define VSLAM_GAZEBO_WORLD_H
// Doc file `.world` cua Gazebo va dung the gioi mo phong tu no.
//
// ================== VI SAO DOC WORLD CHU KHONG DOC .db ==================
// Luoi chiem dung xuat tu `map_hospital.db` co goc o cho robot BAT DAU phien
// quet, con file world o he toa do RIENG cua Gazebo. Do duoc: world trai rong
// x -12.4..12.4, y -31.9..19.5; luoi tu .db la x -17.1..9.2, y -49.6..8.5 --
// lech ~18 m theo y.
//
// Nen khong the "dan do dac cua world len luoi tu .db". Doc THANG world lam
// nguon su that thi ca ba thu -- luoi cho sim, mesh cho khung 3D, va tu the
// do dac -- deu o MOT he toa do, va khong can mot phep can chinh nao.
//
// ================== KHONG CAN PARSER XML DAY DU ==================
// Cau truc `.world` cua Gazebo rat deu:
//
// <model name="X">
// <include>
// <uri>model://Ten</uri>
// <static>true</static>
// <pose>x y z roll pitch yaw</pose>
// </include>
// </model>
//
// va `model.sdf` cua tung model chi tro toi mot file `.obj`. Mot bo quet the
// ~100 dong doc duoc ca hai, va tranh keo them mot dep XML vao `vslam_sim` --
// lib nay hien chi phu thuoc `vslam_common`.
#include <cstdint>
#include <string>
#include <vector>
#include "vslam/vslam_common.h"
namespace vslam {
// Mot the hien model trong world.
struct WorldModel {
std::string name; // ten the hien, vd "MetalCabinet_3"
std::string model_name; // ten model, vd "MetalCabinet"
// Duong dan tuyet doi toi mesh va chat lieu; rong neu khong tim thay.
std::string visual_obj;
std::string collision_obj;
// Tu the trong he toa do WORLD.
double x = 0.0, y = 0.0, z = 0.0;
double roll = 0.0, pitch = 0.0, yaw = 0.0;
// ⚠️ He so ti le cua `<mesh><scale>` trong model.sdf.
//
// File OBJ khong khai don vi, nen Gazebo lay ti le TU SDF. Trong bo model
// nay 27 model dung `<scale>`, phan lon la 0.01 (mesh ve bang CENTIMET) va
// co ca 0.00817 va `1 1 0.95`.
//
// Bo qua the nay lam mot chiec xe day 2.4 m thanh 236 m -- da do:
// `CGMClassic` mot minh ra bao loi 239 x 111 m, va ca the gioi phinh tu
// 25x51 m len 263x146 m.
double sx = 1.0, sy = 1.0, sz = 1.0;
};
struct GazeboWorld {
std::string world_path;
std::vector<WorldModel> models;
// Thu muc goc chua cac model da phan giai, de web phuc vu file tinh.
std::vector<std::string> model_roots;
};
// Doc `.world` va phan giai `model://` qua `model_paths`.
//
// `model_paths`: danh sach thu muc, moi cai chua cac thu muc model con (giong
// GAZEBO_MODEL_PATH). Rong thi thu $GAZEBO_MODEL_PATH roi ~/.gazebo/models.
//
// Model khong phan giai duoc KHONG lam ham that bai -- `ground_plane` va `sun`
// la built-in cua Gazebo, khong co tren dia, va mot the gioi thieu hai cai do
// van dung duoc. Chung duoc bo qua va dem vao `skipped`.
StatusResponse LoadGazeboWorld(const std::string& world_path,
const std::vector<std::string>& model_paths,
GazeboWorld* out, size_t* skipped = nullptr);
// Raster hoa mesh VA CHAM cua world thanh luoi chiem dung 2D.
//
// Chi lay tam giac nam trong dai do cao [z_min, z_max] so voi SAN: do la dai
// ma LiDAR 2D thuc su quet. Lay het moi do cao se bien ca mat san thanh vat
// can, va lay qua cao se bo lot ban ghe.
//
// `resolution` met/o. `pad` la vien trong them quanh bao loi, tinh bang met.
struct WorldRasterOptions {
double resolution = 0.05;
double z_min = 0.05; // tren mat san mot chut, de khong quet trung san
double z_max = 1.20; // duoi tran, tren dinh phan lon do dac
double pad = 1.0;
};
StatusResponse RasterizeGazeboWorld(const GazeboWorld& world,
const WorldRasterOptions& opt,
OccupancyGrid* out);
// ---------------------------------------------------------------------------
// Xuat hinh hoc cho khung 3D
// ---------------------------------------------------------------------------
//
// Khung 3D khong tai duoc file mesh goc: trinh duyet khong co bo doc COLLADA
// offline, va 149 texture PNG cua bo model nay tong cong 74 MB -- gui tung ay
// moi lan mo trang la khong chap nhan duoc.
//
// Nen phia C++ (von DA co bo doc) gui thang TAM GIAC. Hinh hoc duoc gop theo
// TEN MODEL chu khong theo the hien: world co 181 the hien nhung chi ~69 model
// khac nhau, nen 42 chiec ghe chi ton mot ban hinh hoc.
struct WorldMeshGeom {
std::string model_name;
// Mau than vat the, lay tu TEXTURE cua chinh model (xem
// tools/gen_model_colors.py). Mac dinh la xam men su -- dung cho
// KitchenSink va Toilet, hai model duy nhat trong world khong co texture.
uint8_t r = 214, g = 214, b = 210;
// 9 float moi tam giac (x,y,z) x 3, trong he MODEL: da ap `<scale>` nhung
// CHUA ap yaw/tinh tien cua the hien -- do la viec cua instance.
std::vector<float> tri;
};
struct WorldMeshInstance {
uint32_t geom = 0;
float x = 0, y = 0, z = 0, yaw = 0;
};
struct WorldMeshes {
std::vector<WorldMeshGeom> geoms;
std::vector<WorldMeshInstance> instances;
};
// `visual = true` lay mesh HIEN THI (nhieu tam giac, dep hon); false lay mesh
// VA CHAM, von it tam giac va khop chinh xac voi luoi chiem dung.
StatusResponse ExportWorldMeshes(const GazeboWorld& world, bool visual,
WorldMeshes* out);
// Gan mau cho tung hinh hoc tu bang `model_colors.json`.
//
// Thieu file hoac thieu model trong file KHONG phai loi: vat the do giu mau
// mac dinh. Tra ve so hinh hoc da duoc gan mau.
size_t ApplyModelColors(const std::string& json_path, WorldMeshes* io);
// Dong goi `WorldMeshes` thanh khoi nhi phan cho trinh duyet.
//
// "VSLW" | u32 version | u32 n_geom | u32 n_inst
// moi geom : u32 name_len | name | u8 r,g,b | u32 n_tri | n_tri*9 f32
// moi inst : u32 geom | f32 x,y,z,yaw
void EncodeWorldMeshes(const WorldMeshes& in, std::string* out);
} // namespace vslam
#endif // VSLAM_GAZEBO_WORLD_H