#include "agvmapdocument.h" #include "agvmaprasterizer.h" #include #include #include #include #include #include #include #include #include #include namespace { void require(bool condition, const char *message) { if (!condition) qFatal("AGV map rasterizer test failed: %s", message); } QString text(const QJsonObject &root) { return QString::fromUtf8(QJsonDocument(root).toJson(QJsonDocument::Compact)); } QJsonObject position(double x, double y) { return {{"x", x}, {"y", y}}; } QJsonObject fixture(bool camel) { const auto key = [camel](const char *snake, const char *mixed) { return QString::fromLatin1(camel ? mixed : snake); }; const QJsonObject home{{key("class_name", "className"), "LandMark"}, {key("instance_name", "instanceName"), "HOME"}, {"description", QStringLiteral("\"起点1\"")}, {"pos", position(0, 0)}}; const QJsonObject end{{key("class_name", "className"), "ChargePoint"}, {key("instance_name", "instanceName"), "END"}, {"description", QStringLiteral("充电点")}, {"pos", position(4, 0)}, {"dir", 0.3}}; const QJsonObject curve{{key("class_name", "className"), "BezierPath"}, {key("instance_name", "instanceName"), "HOME-END"}, {key("start_pos", "startPos"), home}, {key("end_pos", "endPos"), end}, {key("control_pos1", "controlPos1"), position(1, 2)}, {key("control_pos2", "controlPos2"), position(3, 2)}}; return {{"header", QJsonObject{{key("map_type", "mapType"), "2D-Map"}, {key("map_name", "mapName"), "raster-test"}, {"resolution", 0.5}, {key("min_pos", "minPos"), position(-1, -2)}, {key("max_pos", "maxPos"), position(4, 5)}}}, {key("normal_pos_list", "normalPosList"), QJsonArray{position(-3, -4), position(6, 7), QJsonObject{{"y", 1}}, QJsonObject()}}, {key("advanced_point_list", "advancedPointList"), QJsonArray{home, end}}, {key("advanced_curve_list", "advancedCurveList"), QJsonArray{curve}}}; } void requireScanVisible(const QJsonArray &scan, const QImage &image, const QVariantMap &metadata) { const double resolution = metadata.value("resolution").toDouble(); const double scale = metadata.value("renderScale").toDouble(); const double originX = metadata.value("originX").toDouble(); const double originY = metadata.value("originY").toDouble(); for (const QJsonValue &value : scan) { const QJsonObject point = value.toObject(); const double x = (point.value("x").toDouble() - originX) / resolution * scale; const double y = (point.value("y").toDouble() - originY) / resolution * scale; require(x >= 0.0 && x < image.width() && y >= 0.0 && y < image.height(), "every scan sample, including both extrema, falls inside the raster extent"); const QPoint pixel(std::max(0, std::min(image.width() - 1, static_cast(std::floor(x)))), std::max(0, std::min(image.height() - 1, static_cast(std::floor(image.height() - y))))); require(image.pixelColor(pixel).alpha() > 0, "every occupied sample remains visible after aggregation"); } } int occupiedCount(const QImage &image) { int count = 0; for (int y = 0; y < image.height(); ++y) { const QRgb *line = reinterpret_cast(image.constScanLine(y)); for (int x = 0; x < image.width(); ++x) count += qAlpha(line[x]) > 0; } return count; } void testNamingAndCompleteBounds(bool camel) { const QJsonObject root = fixture(camel); const QString content = text(root); QImage image; QVariantMap metadata; QString error; require(renderAgvSmap2d(content, image, metadata, error), "snake/camel SMAP renders successfully"); require(error.isEmpty() && image.format() == QImage::Format_ARGB32_Premultiplied, "successful output has transparent ARGB format and no stale error"); require(metadata.value("originX").toDouble() == -3.0 && metadata.value("originY").toDouble() == -4.0 && metadata.value("originTheta").toDouble() == 0.0, "world AABB includes scan outliers outside the header"); require(metadata.value("sourceWidth").toInt() == 19 && metadata.value("sourceHeight").toInt() == 23 && metadata.value("renderScale").toDouble() == 1.0 && image.size() == QSize(19, 23), "inclusive extrema occupy cells without changing small-map resolution"); require(metadata.value("mapId").toString() == "raster-test" && metadata.value("dimension").toInt() == 2 && metadata.value("source").toString() == "raw-smap", "metadata identifies the SMAP map and coordinate system"); requireScanVisible(root.value(camel ? "normalPosList" : "normal_pos_list").toArray(), image, metadata); require(occupiedCount(image) < image.width() * image.height() / 10, "unobserved/free cells remain transparent rather than being invented as occupancy"); AgvMapDocument document; require(document.load(content, &error), "fixture is an editable map"); require(metadata.value("stations").toList() == document.stations() && metadata.value("routes").toList() == document.routes(), "names, types, route endpoint IDs and sampled Bezier geometry use the existing document reader"); const QVariantList stations = metadata.value("stations").toList(); require(stations.first().toMap().value("name").toString() == QStringLiteral("\"起点1\"") && stations.last().toMap().value("type").toString() == "ChargePoint", "literal station names and charging-point type survive rendering"); const QVariantList routePoints = metadata.value("routes").toList().first().toMap().value("points").toList(); require(routePoints.size() == 33 && routePoints.at(16).toMap().value("y").toDouble() > 1.0, "curved route geometry is preserved instead of replacing it with a straight line"); } void testLargeMapAndHeaderCoverage() { QJsonObject root = fixture(false); QJsonObject header = root.value("header").toObject(); header.insert("resolution", 0.001); header.insert("min_pos", position(-10000, -5000)); header.insert("max_pos", position(10000, 5000)); root.insert("header", header); const QJsonArray scan{position(-10000, -5000), position(10000, 5000), position(-10000, 5000), position(10000, -5000), position(0, 0)}; root.insert("normal_pos_list", scan); QImage image; QVariantMap metadata; QString error; require(renderAgvSmap2d(text(root), image, metadata, error), "large virtual grid renders without allocating the source grid"); require(image.width() == 1600 && image.height() <= 1000 && image.sizeInBytes() <= 1600 * 1000 * 4, "image allocation is bounded to 1600 by 1000 ARGB pixels"); require(metadata.value("sourceWidth").toInt() == 20000001 && metadata.value("sourceHeight").toInt() == 10000001 && metadata.value("renderScale").toDouble() < 1.0, "original dimensions and uniform render scale are retained"); requireScanVisible(scan, image, metadata); require(occupiedCount(image) == 5, "large-map aggregation keeps all separated corner and center samples"); // A larger header must remain covered even when all points are near HOME. root.insert("normal_pos_list", QJsonArray{position(0, 0)}); require(renderAgvSmap2d(text(root), image, metadata, error), "header-only margins render successfully"); require(metadata.value("originX").toDouble() == -10000.0 && metadata.value("originY").toDouble() == -5000.0 && metadata.value("sourceWidth").toInt() == 20000001, "scan bounds never crop larger header extents"); } void testSemanticOnlyAndOmittedArrays() { QJsonObject root = fixture(true); root.remove("normalPosList"); QJsonObject header = root.value("header").toObject(); header.remove("minPos"); header.remove("maxPos"); root.insert("header", header); QImage image; QVariantMap metadata; QString error; require(renderAgvSmap2d(text(root), image, metadata, error), "semantic-only map can render without scan/bounds"); require(metadata.value("semanticOnly").toBool() && occupiedCount(image) == 0 && metadata.value("stations").toList().size() == 2 && metadata.value("routes").toList().size() == 1, "semantic-only output stays transparent and retains navigation objects"); require(image.width() > 1 && image.height() > 1, "semantic positions provide useful map extents"); root.remove("advancedPointList"); root.remove("advancedCurveList"); require(renderAgvSmap2d(text(root), image, metadata, error), "empty protobuf semantic arrays may be omitted"); require(image.size() == QSize(1, 1) && occupiedCount(image) == 0 && metadata.value("stations").toList().isEmpty(), "empty valid map has a minimal transparent frame"); } void reject(const QString &content) { QImage image(2, 2, QImage::Format_ARGB32); image.fill(Qt::white); QVariantMap metadata{{"oldMap", "stale"}}; QString error; require(!renderAgvSmap2d(content, image, metadata, error), "invalid SMAP is rejected"); require(image.isNull() && metadata.isEmpty() && !error.isEmpty(), "failure clears old image/metadata and explains the error"); } void testInvalidInputs() { reject("not JSON"); QJsonObject root = fixture(false); QJsonObject header = root.value("header").toObject(); header.insert("resolution", 0); root.insert("header", header); reject(text(root)); header.insert("resolution", "0.5"); root.insert("header", header); reject(text(root)); root = fixture(false); root.insert("normal_pos_list", QJsonObject()); reject(text(root)); root.insert("normal_pos_list", QJsonArray{QJsonObject{{"x", QJsonValue(QJsonValue::Null)}, {"y", 0}}}); reject(text(root)); root = fixture(false); header = root.value("header").toObject(); header.insert("min_pos", position(5, 6)); root.insert("header", header); reject(text(root)); header.remove("max_pos"); root.insert("header", header); reject(text(root)); header.insert("min_pos", position(-1e308, -1e308)); header.insert("max_pos", position(1e308, 1e308)); header.insert("resolution", 1e-300); root.insert("header", header); reject(text(root)); } } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); testNamingAndCompleteBounds(false); testNamingAndCompleteBounds(true); testLargeMapAndHeaderCoverage(); testSemanticOnlyAndOmittedArrays(); testInvalidInputs(); if (argc > 1) { QFile source(QString::fromLocal8Bit(argv[1])); require(source.open(QIODevice::ReadOnly), "optional real SMAP fixture opens locally"); const QString content = QString::fromUtf8(source.readAll()); QImage image; QVariantMap metadata; QString error; require(renderAgvSmap2d(content, image, metadata, error), "real downloaded camelCase SMAP renders"); const QJsonObject root = QJsonDocument::fromJson(content.toUtf8()).object(); const QJsonArray scan = root.value(root.contains("normalPosList") ? "normalPosList" : "normal_pos_list").toArray(); requireScanVisible(scan, image, metadata); require(image.width() <= 1600 && image.height() <= 1000 && occupiedCount(image) > 0, "real scan produces a bounded non-empty occupancy image"); AgvMapDocument document; require(document.load(content, &error) && metadata.value("stations").toList() == document.stations() && metadata.value("routes").toList() == document.routes(), "real SMAP station and route metadata matches the editor exactly"); if (argc > 2) require(image.save(QString::fromLocal8Bit(argv[2])), "real SMAP raster preview saved"); qInfo().nospace() << "PASS real SMAP: image=" << image.width() << "x" << image.height() << " resolution=" << metadata.value("resolution").toDouble() << " scan=" << scan.size() << " stations=" << document.stations().size() << " routes=" << document.routes().size(); } qInfo("PASS: SMAP snake/camel raster, complete/extreme bounds, memory limit, semantic-only maps, invalid-input cleanup and document metadata"); return 0; }