174 lines
5.9 KiB
C++
174 lines
5.9 KiB
C++
// AMF/VRML/DXF 格式往返测试
|
||
#include <gtest/gtest.h>
|
||
#include "vde/brep/brep.h"
|
||
#include "vde/brep/modeling.h"
|
||
#include "vde/foundation/io_amf.h"
|
||
#include "vde/foundation/io_vrml.h"
|
||
#include "vde/foundation/io_dxf.h"
|
||
#include <fstream>
|
||
#include "vde/brep/measure.h"
|
||
#include "vde/brep/brep_validate.h"
|
||
#include "vde/foundation/io_collada.h"
|
||
#include "vde/foundation/io_3mf.h"
|
||
#include "vde/brep/mesh_to_brep.h"
|
||
#include <cstdio>
|
||
#include <string>
|
||
#include <cmath>
|
||
|
||
using namespace vde::brep;
|
||
using namespace vde::foundation;
|
||
|
||
static BrepModel box() { return make_box(10,10,10); }
|
||
|
||
static void expect_box_brep(const BrepModel& b) {
|
||
const auto vr = validate(b);
|
||
EXPECT_EQ(b.num_vertices(), 8u);
|
||
EXPECT_EQ(b.num_edges(), 18u);
|
||
EXPECT_EQ(b.num_faces(), 12u);
|
||
EXPECT_TRUE(vr.valid);
|
||
EXPECT_GE(vr.watertightness, 0.999);
|
||
const double v = volume(b);
|
||
EXPECT_TRUE(std::isfinite(v));
|
||
EXPECT_NEAR(v, 1000.0, 1000.0 * 1e-6);
|
||
}
|
||
|
||
static std::string t(const char* e){return std::string(VDE_TEST_TEMP_DIR "vde_a.")+e;}
|
||
|
||
// ── AMF ──
|
||
TEST(AMF, Roundtrip) {
|
||
ASSERT_TRUE(export_amf(box(), t("amf")));
|
||
auto body = import_amf(t("amf"));
|
||
ASSERT_GT(body.num_faces(), 0) << "AMF import returned no faces";
|
||
// Phase4C:焊接顶点/共享边/真实平面曲面(旧实现 V=24/E=36 且 body_volume 崩溃)
|
||
expect_box_brep(body);
|
||
}
|
||
|
||
TEST(AMF, ExportMulti) {
|
||
AmfMesh am;
|
||
am.name = "test";
|
||
am.mesh.build_from_triangles({{0, 0, 0}, {1, 0, 0}, {0, 1, 0}}, {{0, 1, 2}});
|
||
EXPECT_TRUE(export_amf_multi({am}, t("amf_multi")));
|
||
}
|
||
|
||
// ── VRML ──
|
||
TEST(VRML, Roundtrip) {
|
||
ASSERT_TRUE(export_vrml(box(), t("wrl")));
|
||
auto body = import_vrml(t("wrl"));
|
||
ASSERT_GT(body.num_faces(), 0) << "VRML import returned no faces";
|
||
expect_box_brep(body);
|
||
}
|
||
|
||
TEST(VRML, ExportMulti) {
|
||
VrmlMeshData vm;
|
||
vm.mesh.build_from_triangles({{0, 0, 0}, {1, 0, 0}, {0, 1, 0}}, {{0, 1, 2}});
|
||
EXPECT_TRUE(export_vrml_multi({vm}, t("wrl_multi")));
|
||
}
|
||
|
||
// ── Collada ──
|
||
TEST(Collada, Roundtrip) {
|
||
std::vector<BrepModel> bodies{box()};
|
||
ASSERT_TRUE(export_collada(bodies, t("dae")));
|
||
auto imps = import_collada(t("dae"));
|
||
ASSERT_FALSE(imps.empty()) << "Collada import returned no bodies";
|
||
expect_box_brep(imps[0]);
|
||
}
|
||
|
||
// ── DXF ──
|
||
TEST(DXF, ImportLine) {
|
||
// 最小合法 DXF — 一条 LINE
|
||
const char* dxf =
|
||
"0\nSECTION\n2\nENTITIES\n0\nLINE\n"
|
||
"10\n0.0\n20\n0.0\n30\n0.0\n"
|
||
"11\n10.0\n21\n0.0\n31\n0.0\n"
|
||
"0\nENDSEC\n0\nEOF\n";
|
||
auto doc = import_dxf_string(dxf);
|
||
EXPECT_GT(doc.entities.size(), 0u) << "DXF LINE parse failed";
|
||
}
|
||
|
||
TEST(DXF, ImportCircle) {
|
||
const char* dxf =
|
||
"0\nSECTION\n2\nENTITIES\n0\nCIRCLE\n"
|
||
"10\n5.0\n20\n5.0\n30\n0.0\n40\n3.0\n"
|
||
"0\nENDSEC\n0\nEOF\n";
|
||
auto doc = import_dxf_string(dxf);
|
||
EXPECT_GT(doc.entities.size(), 0u);
|
||
}
|
||
|
||
TEST(DXF, ImportPolyline) {
|
||
const char* dxf =
|
||
"0\nSECTION\n2\nENTITIES\n0\nLWPOLYLINE\n"
|
||
"90\n3\n70\n1\n"
|
||
"10\n0.0\n20\n0.0\n"
|
||
"10\n5.0\n20\n0.0\n"
|
||
"10\n5.0\n20\n5.0\n"
|
||
"0\nENDSEC\n0\nEOF\n";
|
||
auto doc = import_dxf_string(dxf);
|
||
EXPECT_GT(doc.entities.size(), 0u);
|
||
}
|
||
|
||
// ── VDE-1300:文本导出快速格式化(std::to_chars)语义回归 ──
|
||
// 旧实现用 std::ostream(general 精度 6 / DXF fixed 精度 6),新实现改用
|
||
// std::to_chars;下列用例锁死「6 位有效数字 / 6 位小数」的导出文本口径,
|
||
// 防止格式化替换悄悄改变输出精度。
|
||
static std::string read_export_text(const std::string& path) {
|
||
std::ifstream in(path, std::ios::binary);
|
||
return std::string((std::istreambuf_iterator<char>(in)),
|
||
std::istreambuf_iterator<char>());
|
||
}
|
||
|
||
/// 顶点含 1/3(需要 6 位有效数字舍入)的单三角网格
|
||
static vde::mesh::HalfedgeMesh third_mesh() {
|
||
vde::mesh::HalfedgeMesh m;
|
||
m.build_from_triangles({{1.0 / 3.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}},
|
||
{{0, 1, 2}});
|
||
return m;
|
||
}
|
||
|
||
TEST(AMF, NumericFormattingMatchesLegacySixSignificantDigits) {
|
||
AmfMesh am;
|
||
am.mesh = third_mesh();
|
||
const std::string path = t("amf_fmt");
|
||
ASSERT_TRUE(export_amf_multi({am}, path));
|
||
const std::string content = read_export_text(path);
|
||
EXPECT_NE(content.find("<x>0.333333</x>"), std::string::npos)
|
||
<< "AMF 顶点 x 未按 6 位有效数字输出";
|
||
EXPECT_EQ(content.find("0.3333333"), std::string::npos)
|
||
<< "AMF 输出出现超出旧 ostream 精度的数字";
|
||
}
|
||
|
||
TEST(VRML, NumericFormattingMatchesLegacySixSignificantDigits) {
|
||
VrmlMeshData vm;
|
||
vm.mesh = third_mesh();
|
||
const std::string path = t("wrl_fmt");
|
||
ASSERT_TRUE(export_vrml_multi({vm}, path));
|
||
const std::string content = read_export_text(path);
|
||
EXPECT_NE(content.find("0.333333"), std::string::npos);
|
||
EXPECT_EQ(content.find("0.3333333"), std::string::npos);
|
||
}
|
||
|
||
TEST(Collada, NumericFormattingMatchesLegacySixSignificantDigits) {
|
||
auto body = mesh_to_brep_model(third_mesh());
|
||
ASSERT_GT(body.num_faces(), 0u);
|
||
ColladaExportOptions opt;
|
||
opt.deflection = 0.5;
|
||
opt.adaptive = false;
|
||
opt.max_triangles = 0;
|
||
const std::string path = t("dae_fmt");
|
||
ASSERT_TRUE(export_collada({body}, path, {}, opt));
|
||
const std::string content = read_export_text(path);
|
||
EXPECT_NE(content.find("0.333333"), std::string::npos);
|
||
EXPECT_EQ(content.find("0.3333333"), std::string::npos);
|
||
}
|
||
|
||
TEST(ThreeMF, NumericFormattingMatchesLegacySixSignificantDigits) {
|
||
ThreeMFMesh mesh;
|
||
mesh.mesh = third_mesh();
|
||
mesh.name = "fmt";
|
||
const std::string path = t("3mf_fmt");
|
||
ASSERT_TRUE(write_3mf(path, {mesh}, "probe"));
|
||
const std::string content = read_export_text(path);
|
||
// 3MF 是 ZIP 包:model XML 以 stored 方式逐字节可见
|
||
EXPECT_NE(content.find("<vertex x=\"0.333333\""), std::string::npos);
|
||
EXPECT_EQ(content.find("0.3333333"), std::string::npos);
|
||
}
|