// VDE-1304:导出文本数值格式化分块并行的逐字节一致性回归。 // // 导出器(COLLADA/AMF/VRML/3MF)在顶点/三角形数 >= 20000 时按连续块并行 // 格式化数值,再按块序拼接。本测试用同一模型分别以强制串行 // (VDE_EXPORT_SERIAL=1)与默认并行路径导出,断言输出文件逐字节一致; // 另覆盖 ChunkWriter::append_fixed 极大值(固定格式 300+ 字符)的回退路径。 #include #include #include #include #include #include #include #include #include #include "foundation/fast_io.h" #include "vde/brep/mesh_to_brep.h" #include "vde/foundation/io_3mf.h" #include "vde/foundation/io_amf.h" #include "vde/foundation/io_collada.h" #include "vde/foundation/io_vrml.h" #include "vde/mesh/halfedge_mesh.h" namespace { using vde::brep::BrepModel; using vde::foundation::Point3D; using vde::mesh::HalfedgeMesh; /// nx*ny 四边形网格(2*nx*ny 三角形)。 HalfedgeMesh grid_mesh(int nx, int ny) { std::vector verts; verts.reserve(static_cast(nx + 1) * (ny + 1)); for (int j = 0; j <= ny; ++j) for (int i = 0; i <= nx; ++i) verts.emplace_back(i * 0.1, j * 0.1, 0.0); auto vid = [nx](int i, int j) { return j * (nx + 1) + i; }; std::vector> tris; tris.reserve(static_cast(nx) * ny * 2); for (int j = 0; j < ny; ++j) for (int i = 0; i < nx; ++i) { tris.push_back({vid(i, j), vid(i + 1, j), vid(i + 1, j + 1)}); tris.push_back({vid(i, j), vid(i + 1, j + 1), vid(i, j + 1)}); } HalfedgeMesh m; m.build_from_triangles(verts, tris); return m; } void set_export_serial(bool on) { #ifdef _WIN32 _putenv_s("VDE_EXPORT_SERIAL", on ? "1" : ""); #else if (on) setenv("VDE_EXPORT_SERIAL", "1", 1); else unsetenv("VDE_EXPORT_SERIAL"); #endif } std::string read_file(const std::string& path) { std::ifstream f(path, std::ios::binary); return std::string(std::istreambuf_iterator(f), std::istreambuf_iterator()); } std::size_t count_occurrences(const std::string& text, const std::string& needle) { std::size_t count = 0; for (std::size_t pos = 0; (pos = text.find(needle, pos)) != std::string::npos; pos += needle.size()) ++count; return count; } } // namespace // 45,000 三角 / 22,801 顶点:顶点数与三角形数都高于 20,000 的并行阈值。 TEST(ExportParallelFormat, ColladaSerialAndParallelIdentical) { const HalfedgeMesh hm = grid_mesh(150, 150); ASSERT_EQ(hm.num_faces(), 45000u); const BrepModel body = vde::brep::mesh_to_brep_model(hm); const std::string serial_path = VDE_TEST_TEMP_DIR "vde1304_collada_serial.dae"; const std::string parallel_path = VDE_TEST_TEMP_DIR "vde1304_collada_parallel.dae"; set_export_serial(true); ASSERT_TRUE(vde::foundation::export_collada({body}, serial_path)); set_export_serial(false); ASSERT_TRUE(vde::foundation::export_collada({body}, parallel_path)); const std::string serial = read_file(serial_path); const std::string parallel = read_file(parallel_path); EXPECT_FALSE(serial.empty()); EXPECT_EQ(serial, parallel); // COLLADA 的 positions 数取决于导出网格(内部可能按角点拆分), // 只校验三角形数与串/并行逐字节一致。 EXPECT_NE(serial.find("count=\"45000\""), std::string::npos); } TEST(ExportParallelFormat, AmfSerialAndParallelIdentical) { const HalfedgeMesh hm = grid_mesh(150, 150); const BrepModel body = vde::brep::mesh_to_brep_model(hm); const std::string serial_path = VDE_TEST_TEMP_DIR "vde1304_amf_serial.amf"; const std::string parallel_path = VDE_TEST_TEMP_DIR "vde1304_amf_parallel.amf"; set_export_serial(true); ASSERT_TRUE(vde::foundation::export_amf(body, serial_path)); set_export_serial(false); ASSERT_TRUE(vde::foundation::export_amf(body, parallel_path)); const std::string serial = read_file(serial_path); EXPECT_FALSE(serial.empty()); EXPECT_EQ(serial, read_file(parallel_path)); // VDE-1305 起 AMF 导出自动焊接顶点:150x150 四边形网格的 22,801 个位置 // (151*151)只写一次,而不是按三角形角点写 3*45000=135,000 次。 EXPECT_EQ(count_occurrences(serial, ""), 22801u); EXPECT_EQ(count_occurrences(serial, ""), 45000u); } TEST(ExportParallelFormat, VrmlSerialAndParallelIdentical) { const HalfedgeMesh hm = grid_mesh(150, 150); const BrepModel body = vde::brep::mesh_to_brep_model(hm); const std::string serial_path = VDE_TEST_TEMP_DIR "vde1304_vrml_serial.wrl"; const std::string parallel_path = VDE_TEST_TEMP_DIR "vde1304_vrml_parallel.wrl"; set_export_serial(true); ASSERT_TRUE(vde::foundation::export_vrml(body, serial_path)); set_export_serial(false); ASSERT_TRUE(vde::foundation::export_vrml(body, parallel_path)); const std::string serial = read_file(serial_path); EXPECT_FALSE(serial.empty()); EXPECT_EQ(serial, read_file(parallel_path)); EXPECT_EQ(count_occurrences(serial, " -1"), 45000u); } TEST(ExportParallelFormat, ThreeMfSerialAndParallelIdentical) { vde::foundation::ThreeMFMesh tm; tm.mesh = grid_mesh(150, 150); const std::vector meshes{tm}; const std::string serial_path = VDE_TEST_TEMP_DIR "vde1304_3mf_serial.3mf"; const std::string parallel_path = VDE_TEST_TEMP_DIR "vde1304_3mf_parallel.3mf"; set_export_serial(true); ASSERT_TRUE(vde::foundation::write_3mf(serial_path, meshes)); set_export_serial(false); ASSERT_TRUE(vde::foundation::write_3mf(parallel_path, meshes)); const std::string serial = read_file(serial_path); ASSERT_FALSE(serial.empty()); EXPECT_EQ(serial, read_file(parallel_path)); EXPECT_NE(serial.find("(out)); vde::foundation::io_detail::ChunkWriter w(out); w.append_fixed(1e300); w.append("|"); w.append_fixed(-1.7976931348623157e308); ASSERT_TRUE(w.finish()); } const std::string text = read_file(path); // 1e300 的定点表示约 308 字符、最大 double 约 316 字符,必须用足够大的缓冲。 char big[4096]; const int n1 = std::snprintf(big, sizeof(big), "%.6f", 1e300); ASSERT_GT(n1, 0); std::string expected(big, static_cast(n1)); expected.push_back('|'); const int n2 = std::snprintf(big, sizeof(big), "%.6f", -1.7976931348623157e308); ASSERT_GT(n2, 0); expected.append(big, static_cast(n2)); EXPECT_EQ(text, expected); }