Files
ViewDesignEngine/tests/foundation/test_vda.cpp
T

260 lines
9.0 KiB
C++
Raw 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.
#include "vde/foundation/io_vda.h"
#include "vde/foundation/format_detect.h"
#include "vde/brep/format_io.h"
#include <gtest/gtest.h>
#include <cstdio>
#include <fstream>
#include <string>
#include <vector>
using namespace vde::foundation;
using namespace vde::brep;
namespace {
/// 合成 VDA-FS 文本写盘(不依赖真实语料;WP-108-05 减少 provided-skip)。
std::string write_vda_fixture(const char* name, const std::string& text) {
const std::string path = std::string(VDE_TEST_TEMP_DIR) + name;
std::ofstream out(path, std::ios::binary | std::ios::trunc);
out << text;
out.close();
return path;
}
/// 合成文本头部后的填充行:避免 detect_iges() 把任意文本第 73 列误判为 IGES
/// 卡片的节区字母(VDA 的 magic 探测在 IGES 之后,需保持 80 列探测点非 S/G/D/P/T)。
std::string vda_filler_line() { return std::string(1200, 'x') + "\n"; }
/// 基础合成文件:HDR + CURVE(4 段闭合方形)+ CONS + FACE(边界回退面)+ SURF(双线性面)+ FACE。
const std::string kSyntheticVda =
"VDAFS 2.0 TEST\n" + vda_filler_line() +
"HDR1 = HEADER / VDAFS 2.0 SYNTHETIC\n"
"CUR1 = CURVE / 4\n"
"0 0.25 0.5 0.75 1\n"
"2 0 0 0 10 0 0\n"
"2 10 0 0 10 10 0\n"
"2 10 10 0 0 10 0\n"
"2 0 10 0 0 0 0\n"
"CNS1 = CONS / CUR1\n"
"FACE1 = FACE /\n"
"CNS1\n"
"SURF1 = SURF / 1 1\n"
"0 1 0 1\n"
"2 2\n"
"0 0 1 1\n"
"0 1 0 1\n"
"0 0 0 0\n"
"FACE2 = FACE / SURF1\n";
/// 仅 CURVE/CONS/FACE(无 SURF):覆盖 DEGRADED 边界扇三角化路径。
const std::string kSyntheticCurveOnly =
"VDAFS 2.0 TEST\n" + vda_filler_line() +
"HDR1 = HEADER / VDAFS 2.0 SYNTHETIC CURVE ONLY\n"
"CUR1 = CURVE / 3\n"
"0 0.333 0.666 1\n"
"2 0 0 0 4 0 0\n"
"2 4 0 0 0 3 0\n"
"2 0 3 0 0 0 0\n"
"CNS1 = CONS / CUR1\n"
"FACE1 = FACE /\n"
"CNS1\n";
bool warning_contains(const std::vector<std::string>& warnings, const std::string& needle) {
for (const auto& w : warnings)
if (w.find(needle) != std::string::npos) return true;
return false;
}
} // namespace
TEST(Vda, SyntheticHeaderCurveConsSurfaceFaceImport) {
const std::string path = write_vda_fixture("wp108_vda_full.vda", kSyntheticVda);
VdaImportResult r = import_vda(path);
std::remove(path.c_str());
ASSERT_TRUE(r.success) << r.error;
EXPECT_EQ(r.bodies.size(), 2u); // FACE1 边界面 + FACE2 SURF 参数面
size_t faces = 0;
for (const auto& b : r.bodies) faces += b.num_faces();
EXPECT_GT(faces, 0u);
EXPECT_TRUE(warning_contains(r.warnings, "SURF 参数面已精确采样")) << r.warnings.front();
EXPECT_TRUE(warning_contains(r.warnings, "未做 FACE trim"));
}
TEST(Vda, SyntheticCurveOnlyFallsBackToBoundaryPatch) {
const std::string path = write_vda_fixture("wp108_vda_curve.vda", kSyntheticCurveOnly);
VdaImportResult r = import_vda(path);
std::remove(path.c_str());
ASSERT_TRUE(r.success) << r.error;
EXPECT_EQ(r.bodies.size(), 1u);
EXPECT_GT(r.bodies[0].num_faces(), 0u);
EXPECT_TRUE(warning_contains(r.warnings, "DEGRADED")) << r.warnings.front();
}
TEST(Vda, ImportCadDetectsSyntheticVda) {
const std::string path = write_vda_fixture("wp108_vda_cad.vda", kSyntheticVda);
CadImportResult r = import_cad(path);
std::remove(path.c_str());
EXPECT_TRUE(r.ok) << (r.messages.empty() ? "" : r.messages.front());
EXPECT_EQ(r.detected.format, vde::io::FileFormat::Vda);
EXPECT_FALSE(r.bodies.empty());
}
TEST(Vda, BadInputWithoutRecordsFails) {
const std::string path = write_vda_fixture("wp108_vda_bad.vda", "not a VDA file\n");
VdaImportResult r = import_vda(path);
std::remove(path.c_str());
EXPECT_FALSE(r.success);
EXPECT_NE(r.error.find("未找到 VDAFS 记录"), std::string::npos) << r.error;
}
TEST(Vda, FaceWithoutConsReportsWarningAndFails) {
const std::string path = write_vda_fixture(
"wp108_vda_nocons.vda",
"VDAFS 2.0 TEST\nHDR1 = HEADER / VDAFS 2.0\nFACE1 = FACE /\n");
VdaImportResult r = import_vda(path);
std::remove(path.c_str());
EXPECT_FALSE(r.success);
EXPECT_NE(r.error.find("无可用的边界曲线"), std::string::npos) << r.error;
EXPECT_TRUE(warning_contains(r.warnings, "FACE 无 CONS 引用"));
}
TEST(Vda, MalformedCurveWarnsAndDoesNotCrash) {
const std::string path = write_vda_fixture(
"wp108_vda_curvebad.vda",
"VDAFS 2.0 TEST\nCUR1 = CURVE / 0\n2 0 0 0 1 0 0\nFACE1 = FACE /\nCNS1\n"
"CNS1 = CONS / CUR1\n");
VdaImportResult r = import_vda(path);
std::remove(path.c_str());
EXPECT_TRUE(warning_contains(r.warnings, "CURVE 段数非法"));
EXPECT_FALSE(r.success);
}
TEST(Vda, MalformedSurfaceFallsBackToBoundaryFace) {
const std::string path = write_vda_fixture(
"wp108_vda_surf_bad.vda",
"VDAFS 2.0 TEST\nHDR1 = HEADER / VDAFS 2.0\n"
"CUR1 = CURVE / 3\n0 0.25 0.5 1\n2 0 0 0 4 0 0\n2 4 0 0 0 3 0\n2 0 3 0 0 0 0\n"
"CNS1 = CONS / CUR1\n"
"SURF1 = SURF /\n"
"FACE1 = FACE /\nCNS1\n");
VdaImportResult r = import_vda(path);
std::remove(path.c_str());
ASSERT_TRUE(r.success) << r.error;
EXPECT_TRUE(warning_contains(r.warnings, "SURF 解析失败")) << r.warnings.front();
EXPECT_EQ(r.bodies.size(), 1u);
}
TEST(Vda, MissingFileFailsClearly) {
VdaImportResult r = import_vda(std::string(VDE_TEST_TEMP_DIR) + "no_such_file_108.vda");
EXPECT_FALSE(r.success);
EXPECT_NE(r.error.find("无法打开"), std::string::npos) << r.error;
}
TEST(Vda, PlanarSurfWithConsUsesBoundaryPatch) {
const std::string fixture =
"VDAFS 2.0 TEST\n" + vda_filler_line() +
"HDR1 = HEADER / VDAFS 2.0 PLANAR TRIM\n"
"CUR1 = CURVE / 4\n"
"0 0.25 0.5 0.75 1\n"
"2 0 0 0 4 0 0\n"
"2 4 0 0 4 4 0\n"
"2 4 4 0 0 4 0\n"
"2 0 4 0 0 0 0\n"
"CNS1 = CONS / CUR1\n"
"SURF1 = SURF / 1 1\n"
"0 1 0 1\n"
"2 2\n"
"0 0 1 1\n"
"0 10 0 10\n"
"0 0 0 0\n"
"FACE1 = FACE /\n"
"CNS1\n"
"SURF1\n";
const std::string path = write_vda_fixture("wp108_vda_planar_trim.vda", fixture);
VdaImportResult r = import_vda(path);
std::remove(path.c_str());
ASSERT_TRUE(r.success) << r.error;
ASSERT_EQ(r.bodies.size(), 1u);
double max_x = 0.0;
for (const auto& v : r.bodies[0].all_vertices()) max_x = std::max(max_x, v.point.x());
EXPECT_LE(max_x, 4.1);
EXPECT_TRUE(warning_contains(r.warnings, "平面 FACE 使用 CONS 边界 patch"));
}
TEST(Vda, CurvedSurfWithPlanarConsIsClipped) {
const std::string fixture =
"VDAFS 2.0 TEST\n" + vda_filler_line() +
"HDR1 = HEADER / VDAFS 2.0 CURVED TRIM\n"
"CUR1 = CURVE / 4\n"
"0 0.25 0.5 0.75 1\n"
"2 2 2 0 4 2 0\n"
"2 4 2 0 4 4 0\n"
"2 4 4 0 2 4 0\n"
"2 2 4 0 2 2 0\n"
"CNS1 = CONS / CUR1\n"
"SURF1 = SURF / 1 1\n"
"0 1 0 1\n"
"3 3\n"
"0 5 10 0 5 10 0 5 10\n"
"0 0 0 5 5 5 10 10 10\n"
"0 2.5 10 0 2.5 10 0 2.5 10\n"
"FACE1 = FACE /\n"
"CNS1\n"
"SURF1\n";
const std::string path = write_vda_fixture("wp108_vda_curved_trim.vda", fixture);
VdaImportResult r = import_vda(path);
std::remove(path.c_str());
ASSERT_TRUE(r.success) << r.error;
ASSERT_EQ(r.bodies.size(), 1u);
double min_x = 1e9, max_x = -1e9, max_y = -1e9;
for (const auto& v : r.bodies[0].all_vertices()) {
min_x = std::min(min_x, v.point.x());
max_x = std::max(max_x, v.point.x());
max_y = std::max(max_y, v.point.y());
}
std::printf("curved trim bbox x=[%.3f,%.3f] max_y=%.3f\n", min_x, max_x, max_y);
EXPECT_GE(min_x, 1.9);
EXPECT_LE(max_x, 4.1);
EXPECT_LE(max_y, 4.1);
EXPECT_TRUE(warning_contains(r.warnings, "曲面 FACE 使用边界裁剪近似"));
}
TEST(Vda, CurvedSurfWithNonPlanarConsUsesBoundaryPatch) {
const std::string fixture =
"VDAFS 2.0 TEST\n" + vda_filler_line() +
"HDR1 = HEADER / VDAFS 2.0 CURVED NONPLANAR\n"
"CUR1 = CURVE / 4\n"
"0 0.25 0.5 0.75 1\n"
"2 2 2 0 4 2 0\n"
"2 4 2 0 4 4 2\n"
"2 4 4 2 2 4 0\n"
"2 2 4 0 2 2 0\n"
"CNS1 = CONS / CUR1\n"
"SURF1 = SURF / 1 1\n"
"0 1 0 1\n"
"3 3\n"
"0 5 10 0 5 10 0 5 10\n"
"0 0 0 5 5 5 10 10 10\n"
"0 2.5 10 0 2.5 10 0 2.5 10\n"
"FACE1 = FACE /\n"
"CNS1\n"
"SURF1\n";
const std::string path = write_vda_fixture("wp108_vda_nonplanar_int.vda", fixture);
VdaImportResult r = import_vda(path);
std::remove(path.c_str());
ASSERT_TRUE(r.success) << r.error;
ASSERT_EQ(r.bodies.size(), 1u);
double min_x = 1e9, max_x = -1e9;
for (const auto& v : r.bodies[0].all_vertices()) {
min_x = std::min(min_x, v.point.x());
max_x = std::max(max_x, v.point.x());
}
EXPECT_GE(min_x, 1.9);
EXPECT_LE(max_x, 4.1);
EXPECT_TRUE(warning_contains(r.warnings, "非平面边界 patch 近似"));
}