#include "voronoi_route/bundle_io.h" #include #include #include #include #include #include #include namespace voronoi_route { namespace { bool ensureDir(const std::string& dir) { // mkdir -p: the bundle dir normally sits inside a map folder that already exists, but a tool // pointed at a fresh --out must not fail on the parent. std::string cur; for (size_t i = 0; i <= dir.size(); ++i) { if (i < dir.size() && dir[i] != '/') continue; cur = dir.substr(0, i); if (cur.empty()) continue; struct stat st; if (::stat(cur.c_str(), &st) == 0) { if (!S_ISDIR(st.st_mode)) return false; continue; } if (::mkdir(cur.c_str(), 0775) != 0) return false; } return true; } std::string hexOf(uint64_t v) { std::ostringstream o; o << std::hex << std::setw(16) << std::setfill('0') << v; return o.str(); } bool parseHex(const std::string& s, uint64_t* out) { if (s.empty() || !out) return false; char* end = nullptr; const unsigned long long v = std::strtoull(s.c_str(), &end, 16); if (end == nullptr || *end != '\0') return false; *out = v; return true; } bool writePgm8(const std::string& path, const std::vector& data, int w, int h) { std::ofstream f(path, std::ios::binary); if (!f) return false; f << "P5\n" << w << " " << h << "\n255\n"; f.write(reinterpret_cast(data.data()), static_cast(data.size())); return static_cast(f); } bool writePgm16(const std::string& path, const std::vector& data, int w, int h) { std::ofstream f(path, std::ios::binary); if (!f) return false; f << "P5\n" << w << " " << h << "\n65535\n"; std::vector be(data.size() * 2); for (size_t i = 0; i < data.size(); ++i) { be[2 * i] = static_cast(data[i] >> 8); be[2 * i + 1] = static_cast(data[i] & 0xFF); } f.write(reinterpret_cast(be.data()), static_cast(be.size())); return static_cast(f); } } // namespace bool saveBundle(const VoronoiRoute& vr, const BundleMeta& meta, const std::string& dir, bool with_gvd, bool with_dist, std::string* err) { if (!vr.ready()) { if (err) *err = "graph not built"; return false; } if (!ensureDir(dir)) { if (err) *err = "cannot create " + dir; return false; } const int w = vr.field().sizeX(), h = vr.field().sizeY(); const auto& st = vr.stats(); const auto& cfg = vr.config(); nlohmann::json j; j["version"] = meta.version; j["source"] = meta.source; j["map_hash"] = hexOf(meta.map_hash ? meta.map_hash : vr.occupancyHash()); j["info"] = {{"width", w}, {"height", h}, {"resolution", meta.resolution}, {"origin", {meta.origin_x, meta.origin_y, meta.origin_yaw}}}; j["params"] = {{"min_edge_length_cells", cfg.min_edge_length_cells}, {"spur_clearance_ratio", cfg.spur_clearance_ratio}, {"min_component_length_cells", cfg.min_component_length_cells}}; j["stats"] = {{"nodes", st.nodes}, {"edges", st.edges}, {"components", st.components}, {"gvd_cells", st.gvd_cells}, {"obstacle_cells", st.obstacle_cells}, {"field_ms", st.field_ms}, {"extract_ms", st.extract_ms}}; j["files"] = {{"gvd", with_gvd ? "voronoi_gvd.pgm" : ""}, {"dist", with_dist ? "voronoi_dist.pgm" : ""}}; nlohmann::json nodes = nlohmann::json::array(); for (const auto& nd : vr.graph().nodes()) nodes.push_back({nd.cell.x, nd.cell.y}); j["nodes"] = std::move(nodes); nlohmann::json edges = nlohmann::json::array(); for (const auto& e : vr.graph().edges()) { nlohmann::json cells = nlohmann::json::array(); for (const auto& c : e.cells) cells.push_back({c.x, c.y}); edges.push_back({{"u", e.u}, {"v", e.v}, {"length_cells", e.length_cells}, {"min_clearance_cells", e.min_clearance_cells}, {"cells", std::move(cells)}}); } j["edges"] = std::move(edges); { std::ofstream f(dir + "/voronoi.json"); if (!f) { if (err) *err = "cannot write " + dir + "/voronoi.json"; return false; } f << j.dump(); } if (with_gvd) { std::vector gvd(static_cast(w) * h, 0); for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) if (vr.field().isVoronoi(x, y)) gvd[static_cast(y) * w + x] = 255; if (!writePgm8(dir + "/voronoi_gvd.pgm", gvd, w, h)) { if (err) *err = "cannot write voronoi_gvd.pgm"; return false; } } if (with_dist) { std::vector dist(static_cast(w) * h, 0); for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) { const double d = vr.field().distanceCells(x, y) * 10.0; dist[static_cast(y) * w + x] = static_cast(std::min(65535.0, std::max(0.0, d))); } if (!writePgm16(dir + "/voronoi_dist.pgm", dist, w, h)) { if (err) *err = "cannot write voronoi_dist.pgm"; return false; } } return true; } bool loadBundleMeta(const std::string& dir, BundleMeta* meta, std::string* err) { std::ifstream f(dir + "/voronoi.json"); if (!f) { if (err) *err = "no voronoi.json in " + dir; return false; } try { nlohmann::json j; f >> j; BundleMeta m; m.version = j.value("version", 0u); m.source = j.value("source", ""); if (!parseHex(j.value("map_hash", ""), &m.map_hash)) { if (err) *err = "bad map_hash"; return false; } const auto& info = j.at("info"); m.width = info.value("width", 0); m.height = info.value("height", 0); m.resolution = info.value("resolution", 0.0); const auto& o = info.at("origin"); m.origin_x = o.at(0).get(); m.origin_y = o.at(1).get(); m.origin_yaw = o.size() > 2 ? o.at(2).get() : 0.0; if (meta) *meta = m; return true; } catch (const std::exception& e) { if (err) *err = std::string("voronoi.json: ") + e.what(); return false; } } bool appendRouteToBundle(const std::string& dir, const Polyline& route, size_t mask_cells, std::string* err) { nlohmann::json j; { std::ifstream f(dir + "/voronoi.json"); if (!f) { if (err) *err = "no voronoi.json in " + dir; return false; } try { f >> j; } catch (const std::exception& e) { if (err) *err = e.what(); return false; } } nlohmann::json rc = nlohmann::json::array(); for (const auto& c : route) rc.push_back({c.x, c.y}); j["route"] = {{"cells", std::move(rc)}, {"mask_cells", mask_cells}}; std::ofstream f(dir + "/voronoi.json"); if (!f) { if (err) *err = "cannot write voronoi.json"; return false; } f << j.dump(); return true; } } // namespace voronoi_route