// VDE data-exchange completion (A-9/Phase4): point-cloud writers regression. // LAS 1.4 / LAZ 1.2 (LASzip pointwise-chunked) / PTX / E57 round-trips. #include #include #include #include #include #include #include #include #include #include "vde/foundation/utf8_path.h" #include "vde/mesh/point_cloud.h" using namespace vde::mesh; using namespace vde::core; namespace { std::string pcpath(const char* name) { return std::string(VDE_TEST_TEMP_DIR) + "/" + name; } PointCloud sample_cloud() { PointCloud c; c.vertices = {Point3D(0.123456789, -4.5, 1000.000001), Point3D(1.0, 2.0, 3.0), Point3D(-999.125, 0.5, -0.25), Point3D(1234.5678, -2345.6789, 0.000123)}; c.colors = {{{255, 0, 0}}, {{0, 255, 0}}, {{0, 0, 255}}, {{12, 34, 56}}}; c.intensities = {100.0, 65535.0, 0.0, 1234.0}; return c; } // ── LAZ 结构校验小工具(不依赖外部 LASzip)── std::vector read_bytes(const std::string& path) { std::ifstream in(path, std::ios::binary); return std::vector((std::istreambuf_iterator(in)), std::istreambuf_iterator()); } void write_bytes(const std::string& path, const std::vector& b) { std::ofstream out(path, std::ios::binary); out.write(reinterpret_cast(b.data()), static_cast(b.size())); } uint16_t le16(const std::vector& b, size_t o) { return static_cast(b[o] | (static_cast(b[o + 1]) << 8)); } uint32_t le32(const std::vector& b, size_t o) { return static_cast(b[o]) | (static_cast(b[o + 1]) << 8) | (static_cast(b[o + 2]) << 16) | (static_cast(b[o + 3]) << 24); } uint64_t le64(const std::vector& b, size_t o) { uint64_t v = 0; for (int i = 0; i < 8; ++i) v |= static_cast(b[o + i]) << (8 * i); return v; } /// LASzip VLR 在文件中的 Data 偏移(要求 header_size=227 的 LAS 1.2 文件) size_t laz_vlr_payload_offset(const std::vector& b) { const size_t hsize = le16(b, 94); return hsize + 54; // VLR header: reserved2 + user_id16 + record_id2 + len2 + desc32 } } // namespace TEST(PointCloudIoA9, LasRoundTrip) { const PointCloud c = sample_cloud(); const std::string p = pcpath("pc_a9_roundtrip.las"); std::remove(p.c_str()); auto wr = c.save_las(p); ASSERT_TRUE(wr.ok()) << wr.message; ASSERT_TRUE(std::ifstream(p, std::ios::binary).good()); PointCloud back = PointCloud::load_las(p); ASSERT_EQ(back.size(), c.size()); for (size_t i = 0; i < c.size(); ++i) { EXPECT_LE((back.vertices[i] - c.vertices[i]).norm(), 1e-6); if (back.has_colors()) { EXPECT_EQ(back.colors[i][0], c.colors[i][0]); EXPECT_EQ(back.colors[i][1], c.colors[i][1]); EXPECT_EQ(back.colors[i][2], c.colors[i][2]); } } } TEST(PointCloudIoA9, PtxRoundTrip) { const PointCloud c = sample_cloud(); const std::string p = pcpath("pc_a9_roundtrip.ptx"); std::remove(p.c_str()); auto wr = c.save_ptx(p); ASSERT_TRUE(wr.ok()) << wr.message; PointCloud back = PointCloud::load_ptx(p); ASSERT_EQ(back.size(), c.size()); for (size_t i = 0; i < c.size(); ++i) { EXPECT_LE((back.vertices[i] - c.vertices[i]).norm(), 1e-6); ASSERT_TRUE(back.has_colors()); EXPECT_EQ(back.colors[i][0], c.colors[i][0]); EXPECT_EQ(back.colors[i][1], c.colors[i][1]); EXPECT_EQ(back.colors[i][2], c.colors[i][2]); } } TEST(PointCloudIoA9, E57WriteRoundTripWithColorAndIntensity) { // A-9c:E57 最小写出(ASTM-E57 头 + XML prototype + 原始二进制点记录) // → load_e57 回读:坐标无损(double)、颜色/强度精确。 const PointCloud c = sample_cloud(); const std::string p = pcpath("pc_a9_roundtrip.e57"); std::remove(p.c_str()); auto wr = c.save_e57(p); ASSERT_TRUE(wr.ok()) << wr.message; ASSERT_TRUE(std::ifstream(p, std::ios::binary).good()); PointCloud back = PointCloud::load_e57(p); ASSERT_EQ(back.size(), c.size()); for (size_t i = 0; i < c.size(); ++i) { EXPECT_LE((back.vertices[i] - c.vertices[i]).norm(), 1e-12); ASSERT_TRUE(back.has_colors()); EXPECT_EQ(back.colors[i][0], c.colors[i][0]); EXPECT_EQ(back.colors[i][1], c.colors[i][1]); EXPECT_EQ(back.colors[i][2], c.colors[i][2]); ASSERT_TRUE(back.has_intensity()); EXPECT_DOUBLE_EQ(back.intensities[i], c.intensities[i]); } std::remove(p.c_str()); } TEST(PointCloudIoA9, E57WriteRoundTripWithNormals) { PointCloud c = sample_cloud(); c.normals = {Vector3D(0, 0, 1), Vector3D(1, 0, 0), Vector3D(0, 1, 0), Vector3D(-1, 0, 0)}; const std::string p = pcpath("pc_a9_roundtrip_normals.e57"); std::remove(p.c_str()); auto wr = c.save_e57(p); ASSERT_TRUE(wr.ok()) << wr.message; PointCloud back = PointCloud::load_e57(p); ASSERT_EQ(back.size(), c.size()); ASSERT_EQ(back.normals.size(), c.normals.size()); for (size_t i = 0; i < c.size(); ++i) { EXPECT_LE((back.vertices[i] - c.vertices[i]).norm(), 1e-12); EXPECT_LE((back.normals[i] - c.normals[i]).norm(), 1e-12); } std::remove(p.c_str()); } TEST(PointCloudIoA9, LazWriteStructureIsValidLaszip) { // Phase4-LAZ:真实 LAZ 1.2(LASzip VLR 22204 + compressor 2 pointwise-chunked) const PointCloud c = sample_cloud(); const std::string p = pcpath("pc_laz_structure.laz"); std::remove(p.c_str()); auto wr = c.save_laz(p); ASSERT_TRUE(wr.ok()) << wr.message; EXPECT_NE(wr.message.find("compressor=2"), std::string::npos) << wr.message; const auto b = read_bytes(p); ASSERT_GT(b.size(), 227u + 54u + 34u); EXPECT_EQ(std::memcmp(b.data(), "LASF", 4), 0); EXPECT_EQ(b[24], 1); // LAS version major EXPECT_EQ(b[25], 2); // LAS version minor const size_t hsize = le16(b, 94); EXPECT_EQ(hsize, 227u); const uint32_t pd_off = le32(b, 96); const uint32_t nvlr = le32(b, 100); const uint8_t pdrf = b[104]; const uint16_t rlen = le16(b, 105); const uint32_t npts = le32(b, 107); EXPECT_EQ(nvlr, 1u); EXPECT_EQ(static_cast(pdrf & 0x80), 0x80); // bit7 = compressed EXPECT_EQ(static_cast(pdrf & 0x3F), 2); // PDRF 2 (RGB) EXPECT_EQ(rlen, 26u); EXPECT_EQ(npts, static_cast(c.size())); // LASzip VLR const size_t payload = laz_vlr_payload_offset(b); EXPECT_EQ(std::string(reinterpret_cast(b.data() + hsize + 2), 14), std::string("laszip encoded")); EXPECT_EQ(le16(b, hsize + 18), 22204u); // record id const uint16_t vlen = le16(b, hsize + 20); EXPECT_EQ(vlen, 46u); // 34 + 2 items * 6 EXPECT_EQ(le16(b, payload + 0), 2u); // compressor = chunked EXPECT_EQ(le16(b, payload + 2), 0u); // coder = arithmetic EXPECT_EQ(le32(b, payload + 12), 50000u); // chunk size EXPECT_EQ(le16(b, payload + 32), 2u); // num items EXPECT_EQ(le16(b, payload + 34), 6u); // POINT10 EXPECT_EQ(le16(b, payload + 36), 20u); EXPECT_EQ(le16(b, payload + 38), 2u); // item version 2 EXPECT_EQ(le16(b, payload + 40), 8u); // RGB12 EXPECT_EQ(le16(b, payload + 42), 6u); EXPECT_EQ(le16(b, payload + 44), 2u); // 压缩数据块:chunk table offset + chunk table 版本/块数 const uint64_t cto = le64(b, pd_off); EXPECT_GT(cto, static_cast(pd_off)); EXPECT_LT(cto + 8, b.size()); EXPECT_EQ(le32(b, static_cast(cto)), 0u); // chunk table version 0 EXPECT_EQ(le32(b, static_cast(cto) + 4), 1u); // 4 points -> 1 chunk std::remove(p.c_str()); } TEST(PointCloudIoA9, LazRoundTripCoordinatesColorsIntensity) { const PointCloud c = sample_cloud(); const std::string p = pcpath("pc_laz_roundtrip.laz"); std::remove(p.c_str()); auto wr = c.save_laz(p); ASSERT_TRUE(wr.ok()) << wr.message; PointCloud back = PointCloud::load_las(p); // 引擎 reader 自动识别 LAZ ASSERT_EQ(back.size(), c.size()); for (size_t i = 0; i < c.size(); ++i) { EXPECT_LE((back.vertices[i] - c.vertices[i]).norm(), 1e-6); ASSERT_TRUE(back.has_colors()); EXPECT_EQ(back.colors[i][0], c.colors[i][0]); EXPECT_EQ(back.colors[i][1], c.colors[i][1]); EXPECT_EQ(back.colors[i][2], c.colors[i][2]); EXPECT_DOUBLE_EQ(back.intensities[i], c.intensities[i]); } std::remove(p.c_str()); } TEST(PointCloudIoA9, LazRoundTripMultiChunk) { // > 50000 点 → 多 chunk;验证 chunk 边界、chunk table 与坐标/颜色/强度回读 PointCloud c; const int n = 60000; c.vertices.reserve(static_cast(n)); c.colors.reserve(static_cast(n)); c.intensities.reserve(static_cast(n)); for (int i = 0; i < n; ++i) { const double t = static_cast(i); c.vertices.emplace_back(std::sin(t * 0.001) * 100.0, std::cos(t * 0.002) * 50.0, t * 0.01); c.colors.push_back({static_cast(i % 256), static_cast((i * 3) % 256), static_cast((i * 7) % 256)}); c.intensities.push_back(static_cast(i % 65536)); } const std::string p = pcpath("pc_laz_multichunk.laz"); std::remove(p.c_str()); auto wr = c.save_laz(p); ASSERT_TRUE(wr.ok()) << wr.message; EXPECT_NE(wr.message.find("2 chunk"), std::string::npos) << wr.message; { const auto b = read_bytes(p); const uint32_t pd_off = le32(b, 96); const uint64_t cto = le64(b, pd_off); EXPECT_EQ(le32(b, static_cast(cto)), 0u); EXPECT_EQ(le32(b, static_cast(cto) + 4), 2u); } PointCloud back = PointCloud::load_las(p); ASSERT_EQ(back.size(), static_cast(n)); double max_coord = 0.0; int color_bad = 0, inten_bad = 0; for (int i = 0; i < n; ++i) { max_coord = std::max(max_coord, (back.vertices[i] - c.vertices[i]).norm()); if (back.colors[i] != c.colors[i]) ++color_bad; if (back.intensities[i] != c.intensities[i]) ++inten_bad; } EXPECT_LE(max_coord, 1e-6); EXPECT_EQ(color_bad, 0); EXPECT_EQ(inten_bad, 0); std::remove(p.c_str()); } TEST(PointCloudIoA9, LazWithoutColorsUsesPointFormat0) { PointCloud c; c.vertices = {Point3D(1, 2, 3), Point3D(4.5, -5.25, 6.125)}; c.intensities = {7.0, 4095.0}; const std::string p = pcpath("pc_laz_nocolor.laz"); std::remove(p.c_str()); auto wr = c.save_laz(p); ASSERT_TRUE(wr.ok()) << wr.message; { const auto b = read_bytes(p); EXPECT_EQ(static_cast(b[104] & 0x3F), 0); // PDRF 0 EXPECT_EQ(static_cast(b[104] & 0x80), 0x80); EXPECT_EQ(le16(b, 105), 20u); } PointCloud back = PointCloud::load_las(p); ASSERT_EQ(back.size(), 2u); EXPECT_LE((back.vertices[0] - Point3D(1, 2, 3)).norm(), 1e-6); EXPECT_LE((back.vertices[1] - Point3D(4.5, -5.25, 6.125)).norm(), 1e-6); EXPECT_DOUBLE_EQ(back.intensities[0], 7.0); EXPECT_DOUBLE_EQ(back.intensities[1], 4095.0); std::remove(p.c_str()); } TEST(PointCloudIoA9, LazUnsupportedOrCorruptGivesDiagnostic) { const PointCloud c = sample_cloud(); const std::string good = pcpath("pc_laz_diag_src.laz"); auto wr = c.save_laz(good); ASSERT_TRUE(wr.ok()) << wr.message; const auto b = read_bytes(good); // 1) 不支持的 compressor(改为 3 = layered chunked,PDRF0-5 不适用) { auto bad = b; const size_t payload = laz_vlr_payload_offset(bad); bad[payload] = 3; bad[payload + 1] = 0; const std::string p = pcpath("pc_laz_bad_compressor.laz"); write_bytes(p, bad); try { PointCloud::load_las(p); ADD_FAILURE() << "expected diagnostic for unsupported compressor"; } catch (const std::runtime_error& e) { EXPECT_NE(std::string(e.what()).find("compressor"), std::string::npos) << e.what(); } std::remove(p.c_str()); } // 2) 截断文件 → 明确诊断(不崩溃) { auto bad = b; bad.resize(std::min(bad.size(), 400)); const std::string p = pcpath("pc_laz_truncated.laz"); write_bytes(p, bad); EXPECT_THROW(PointCloud::load_las(p), std::runtime_error); std::remove(p.c_str()); } std::remove(good.c_str()); } TEST(PointCloudIoA9, E57EmptyWriteInducesErrorNoFile) { PointCloud empty; const std::string ep = pcpath("pc_a9_empty.e57"); std::remove(ep.c_str()); auto er = empty.save_e57(ep); EXPECT_FALSE(er.ok()); EXPECT_FALSE(std::ifstream(ep, std::ios::binary).good()); // 空点云写 LAZ 同样 Error(不落盘) const std::string lp = pcpath("pc_a9_empty.laz"); std::remove(lp.c_str()); auto lr = empty.save_laz(lp); EXPECT_FALSE(lr.ok()); EXPECT_FALSE(std::ifstream(lp, std::ios::binary).good()); } TEST(PointCloudIoA9, EmptyCloudWritesErrorNoFile) { PointCloud empty; const std::string lp = pcpath("pc_a9_empty.las"); const std::string pp = pcpath("pc_a9_empty.ptx"); std::remove(lp.c_str()); std::remove(pp.c_str()); EXPECT_FALSE(empty.save_las(lp).ok()); EXPECT_FALSE(empty.save_ptx(pp).ok()); EXPECT_FALSE(std::ifstream(lp, std::ios::binary).good()); EXPECT_FALSE(std::ifstream(pp).good()); } TEST(PointCloudIoA9, LasWithoutColorsUsesPointFormat0) { PointCloud c; c.vertices = {Point3D(1, 2, 3), Point3D(4, 5, 6)}; const std::string p = pcpath("pc_a9_nocolor.las"); auto wr = c.save_las(p); ASSERT_TRUE(wr.ok()) << wr.message; auto back = PointCloud::load_las(p); ASSERT_EQ(back.size(), 2u); EXPECT_LE((back.vertices[0] - Point3D(1, 2, 3)).norm(), 1e-6); EXPECT_LE((back.vertices[1] - Point3D(4, 5, 6)).norm(), 1e-6); } // ═══════════════════════════════════════════════════════════════ // A48/Wave20: UTF-8 文件路径回归(Windows ANSI 代码页) // ═══════════════════════════════════════════════════════════════ // LAS 写出/读回的文件打开点必须经 path_from_utf8/宽字符 API,否则 CP936 下 // 中文文件名文件存在也打开失败;ASCII 路径行为逐字节不变。 TEST(PointCloudIoA48, Utf8PathLasRoundTrip) { const std::string path = std::string(VDE_TEST_TEMP_DIR) + "点云_①.las"; const std::filesystem::path fs_path = vde::foundation::path_from_utf8(path); std::error_code ec; std::filesystem::remove(fs_path, ec); const PointCloud c = sample_cloud(); auto wr = c.save_las(path); ASSERT_TRUE(wr.ok()) << wr.message; ASSERT_TRUE(std::filesystem::exists(fs_path)); PointCloud back = PointCloud::load_las(path); ASSERT_EQ(back.size(), c.size()); for (size_t i = 0; i < c.size(); ++i) { EXPECT_LE((back.vertices[i] - c.vertices[i]).norm(), 1e-6); ASSERT_TRUE(back.has_colors()); EXPECT_EQ(back.colors[i][0], c.colors[i][0]); EXPECT_EQ(back.colors[i][1], c.colors[i][1]); EXPECT_EQ(back.colors[i][2], c.colors[i][2]); ASSERT_TRUE(back.has_intensity()); EXPECT_DOUBLE_EQ(back.intensities[i], c.intensities[i]); } std::filesystem::remove(fs_path, ec); }