Files
ViewDesignEngine/tests/foundation/test_export_parallel_format.cpp
hm fb28f2ead7 fix(infra,foundation): welded AMF/3MF export + LockFreeStack concurrency fix (1.0.159)
VDE-1305: AMF/3MF export vertex exact welding (24-byte bit-pattern key, no tolerance);
300k facet AMF 171.3 MB -> 57.1 MB, export median 1.54x (best 1.98x); 3MF export now
parallel tessellation (>=256 faces). Readback geometry bit-identical.

VDE-1306: LockFreeStack concurrent try_pop use-after-free (head node deleted by another
consumer between load and node->next read; 2/8 processes crashed 0xC0000005 under
8x20k stress). Replaced Treiber stack with spinlock + vector in both infra/core copies;
API and non-blocking empty-pop semantics unchanged, class name kept. Reproducer now
0 crashes (160k MPMC cycles); single-thread push+pop 29.26 -> 6.57 ns/op-pair.

Tests: registered 8,195 -> 8,201 (5 weld/export + 1 concurrency stress); full CTest
8,201 items: 8,166 PASS / 0 failed / 35 env-corpus SKIP, 446.95 s; full build 374
targets, 0 warnings. Reports: 189 (VDE-1305), 190 (VDE-1306), 188 (1.0.158 verifier).
Release checks: vde_release.py --check 14/14 PASS, --report-check PASS.
2026-11-05 07:49:54 +08:00

182 lines
7.0 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// VDE-1304:导出文本数值格式化分块并行的逐字节一致性回归。
//
// 导出器(COLLADA/AMF/VRML/3MF)在顶点/三角形数 >= 20000 时按连续块并行
// 格式化数值,再按块序拼接。本测试用同一模型分别以强制串行
// (VDE_EXPORT_SERIAL=1)与默认并行路径导出,断言输出文件逐字节一致;
// 另覆盖 ChunkWriter::append_fixed 极大值(固定格式 300+ 字符)的回退路径。
#include <gtest/gtest.h>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
#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<Point3D> verts;
verts.reserve(static_cast<std::size_t>(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<std::array<int, 3>> tris;
tris.reserve(static_cast<std::size_t>(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<char>(f),
std::istreambuf_iterator<char>());
}
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, "<vertex>"), 22801u);
EXPECT_EQ(count_occurrences(serial, "<triangle>"), 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<vde::foundation::ThreeMFMesh> 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("<triangle v1="), std::string::npos);
}
// ChunkWriter::append_fixed:固定格式极大值超出旧 64 字节缓冲时不得静默丢值。
TEST(ExportParallelFormat, AppendFixedLargeValueNotDropped) {
const std::string path = VDE_TEST_TEMP_DIR "vde1304_append_fixed.txt";
{
std::ofstream out(path, std::ios::binary);
ASSERT_TRUE(static_cast<bool>(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<std::size_t>(n1));
expected.push_back('|');
const int n2 = std::snprintf(big, sizeof(big), "%.6f",
-1.7976931348623157e308);
ASSERT_GT(n2, 0);
expected.append(big, static_cast<std::size_t>(n2));
EXPECT_EQ(text, expected);
}