// Doc `.world` cua Gazebo, raster hoa mesh va cham thanh luoi chiem dung. // // Muc dich: dung the gioi mo phong TU CHINH file world, thay vi tu mot `.db` // da quet. Nho vay luoi cho sim, mesh cho khung 3D va tu the do dac deu o MOT // he toa do -- xem ghi chu trong `vslam/gazebo_world.h`. // // vslam_world_export --world X.world --out Y.pgm [--res 0.05] // [--z-min 0.05] [--z-max 1.20] [--models DIR] #include #include #include #include #include "vslam/gazebo_world.h" #include "vslam/occupancy_map_io.h" int main(int argc, char** argv) { std::string world, out; std::vector model_paths; vslam::WorldRasterOptions opt; bool list_only = false; bool bbox = false; std::string meshes_out, colors_json; bool meshes_visual = false; for (int i = 1; i < argc; ++i) { const std::string a = argv[i]; auto next = [&]() -> std::string { return i + 1 < argc ? argv[++i] : ""; }; if (a == "--world") world = next(); else if (a == "--out") out = next(); else if (a == "--res") opt.resolution = std::atof(next().c_str()); else if (a == "--z-min") opt.z_min = std::atof(next().c_str()); else if (a == "--z-max") opt.z_max = std::atof(next().c_str()); else if (a == "--models") model_paths.push_back(next()); else if (a == "--list") list_only = true; else if (a == "--bbox") bbox = true; else if (a == "--meshes") meshes_out = next(); else if (a == "--meshes-visual") meshes_visual = true; else if (a == "--colors") colors_json = next(); else if (a == "--help") { std::printf( "cach dung: %s --world X.world --out Y.pgm [--res 0.05]\n" " [--z-min 0.05] [--z-max 1.20] [--models DIR] [--list]\n" "\n" " --list chi liet ke model phan giai duoc roi thoat\n" " --models co the lap lai; khong co thi dung $GAZEBO_MODEL_PATH\n" " roi ~/.gazebo/models\n" " --meshes F.bin xuat hinh hoc cho khung 3D thay vi luoi\n" " --meshes-visual dung mesh HIEN THI thay vi mesh va cham\n" " --colors F.json bang mau tu texture (gen_model_colors.py)\n", argv[0]); return 0; } else { std::fprintf(stderr, "tham so la: %s\n", a.c_str()); return 2; } } if (world.empty()) { std::fprintf(stderr, "can --world X.world\n"); return 2; } vslam::GazeboWorld gw; size_t skipped = 0; { const vslam::StatusResponse st = vslam::LoadGazeboWorld(world, model_paths, &gw, &skipped); if (!st.ok()) { std::fprintf(stderr, "%s\n", st.message.c_str()); return 1; } } if (list_only) { std::printf("%zu model, bo qua %zu\n", gw.models.size(), skipped); for (const auto& m : gw.models) { std::printf(" %-44s (%7.2f, %7.2f, %5.2f) yaw %6.2f %s\n", m.model_name.c_str(), m.x, m.y, m.z, m.yaw, m.collision_obj.empty() ? "KHONG MESH" : ""); } return 0; } // --- Che do chan doan: bao loi TUNG model, de tim mesh van xa --- // // Mot mesh dat sai don vi hoac sai truc lam bao loi ca the gioi phinh ra // hang tram met, va tu bao loi tong thi khong biet thu pham la ai. if (bbox) { for (const auto& m : gw.models) { vslam::GazeboWorld one; one.models.push_back(m); vslam::OccupancyGrid g; if (!vslam::RasterizeGazeboWorld(one, opt, &g).ok()) continue; const double w = g.width * g.resolution, h = g.height * g.resolution; if (w > 60.0 || h > 60.0) { std::printf(" %-46s %7.1f x %7.1f m goc (%8.1f, %8.1f)\n", m.model_name.c_str(), w, h, g.origin.position.x, g.origin.position.y); } } return 0; } if (!meshes_out.empty()) { vslam::WorldMeshes wm; const vslam::StatusResponse st = vslam::ExportWorldMeshes(gw, meshes_visual, &wm); if (!st.ok()) { std::fprintf(stderr, "%s\n", st.message.c_str()); return 1; } size_t colored = 0; if (!colors_json.empty()) { colored = vslam::ApplyModelColors(colors_json, &wm); } std::string blob; vslam::EncodeWorldMeshes(wm, &blob); FILE* f = std::fopen(meshes_out.c_str(), "wb"); if (f == nullptr) { std::fprintf(stderr, "khong ghi duoc %s\n", meshes_out.c_str()); return 1; } std::fwrite(blob.data(), 1, blob.size(), f); std::fclose(f); size_t ntri = 0; for (const auto& g : wm.geoms) ntri += g.tri.size() / 9; std::printf("mesh (%s): %zu hinh hoc (%zu co mau), %zu the hien, " "%zu tam giac, %.2f MB -> %s\n", meshes_visual ? "hien thi" : "va cham", wm.geoms.size(), colored, wm.instances.size(), ntri, blob.size() / 1048576.0, meshes_out.c_str()); return 0; } if (out.empty()) { std::fprintf(stderr, "can --out Y.pgm (hoac dung --list)\n"); return 2; } vslam::OccupancyGrid grid; { const vslam::StatusResponse st = vslam::RasterizeGazeboWorld(gw, opt, &grid); if (!st.ok()) { std::fprintf(stderr, "%s\n", st.message.c_str()); return 1; } } std::string yaml = out; const size_t dot = yaml.rfind('.'); if (dot != std::string::npos) yaml = yaml.substr(0, dot); yaml += ".yaml"; const vslam::StatusResponse w = vslam::SaveOccupancyMap(grid, yaml); if (!w.ok()) { std::fprintf(stderr, "ghi ban do: %s\n", w.message.c_str()); return 1; } std::printf("ban do: %ux%u @ %.3f m/o -> %s\n", grid.width, grid.height, static_cast(grid.resolution), yaml.c_str()); return 0; }