640 lines
27 KiB
C++
640 lines
27 KiB
C++
// P1-2 官方格式插件(STL/OBJ/PLY)+ import_cad 插件分派集成测试
|
||
//
|
||
// 覆盖:
|
||
// 1) 官方插件发现/加载/能力注册(manifest + 动态库);
|
||
// 2) 三种格式经插件导入 vs 内置 import_cad 导入的体积/bbox/面数对比;
|
||
// 3) STL 插件写出 → 内置/插件再导入往返;
|
||
// 4) import_cad 回退:未知扩展名 + probe 分派、已知扩展名保持内置优先、
|
||
// 无宿主服务时保持原错误、VDE_IMPORT_CAD_PREFER_PLUGIN 显式选择插件;
|
||
// 5) 宿主 P1 body_from_mesh 服务的参数校验与错误路径。
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <algorithm>
|
||
#include <cmath>
|
||
#include <cstdint>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <fstream>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "vde/brep/format_io.h"
|
||
#include "vde/brep/measure.h"
|
||
#include "vde/plugin/geometry_host_services.h"
|
||
#include "vde/plugin/plugin_manager.h"
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
|
||
#ifndef VDE_PLUGIN_OFFICIAL_DIR
|
||
#define VDE_PLUGIN_OFFICIAL_DIR ""
|
||
#endif
|
||
#ifndef VDE_PLUGIN_SAMPLE_DIR
|
||
#define VDE_PLUGIN_SAMPLE_DIR ""
|
||
#endif
|
||
|
||
namespace {
|
||
|
||
using vde::brep::BrepModel;
|
||
using vde::brep::CadImportResult;
|
||
using vde::plugin::FormatImportResult;
|
||
using vde::plugin::PluginManager;
|
||
using vde::plugin::PluginKind;
|
||
|
||
constexpr const char* kStlId = "com.vde.official.stl";
|
||
constexpr const char* kObjId = "com.vde.official.obj";
|
||
constexpr const char* kPlyId = "com.vde.official.ply";
|
||
constexpr const char* k3mfId = "com.vde.official.3mf";
|
||
constexpr const char* kGltfId = "com.vde.official.gltf";
|
||
|
||
std::string temp_path(const char* name) {
|
||
return std::string(VDE_TEST_TEMP_DIR) + name;
|
||
}
|
||
|
||
bool write_text_file(const std::string& path, const std::string& text) {
|
||
std::ofstream out(path, std::ios::binary | std::ios::trunc);
|
||
if (!out.good()) return false;
|
||
out.write(text.data(), static_cast<std::streamsize>(text.size()));
|
||
out.close();
|
||
return out.good();
|
||
}
|
||
|
||
bool set_process_env(const char* name, const char* value) {
|
||
#ifdef _WIN32
|
||
// MSVC: _putenv_s(name, nullptr) 触发 CRT invalid-parameter(进程 abort);空串表示删除变量。
|
||
return _putenv_s(name, value ? value : "") == 0;
|
||
#else
|
||
return value ? (setenv(name, value, 1) == 0) : (unsetenv(name) == 0);
|
||
#endif
|
||
}
|
||
|
||
// ── 立方体网格(8 顶点 / 12 三角面,外法向)──
|
||
struct CubeMesh {
|
||
std::vector<double> positions; // 3×8
|
||
std::vector<uint32_t> indices; // 3×12
|
||
};
|
||
|
||
CubeMesh make_cube(double w, double h, double d) {
|
||
const double x = w * 0.5, y = h * 0.5, z = d * 0.5;
|
||
CubeMesh mesh;
|
||
mesh.positions = {
|
||
-x, -y, -z, // 0
|
||
x, -y, -z, // 1
|
||
x, y, -z, // 2
|
||
-x, y, -z, // 3
|
||
-x, -y, z, // 4
|
||
x, -y, z, // 5
|
||
x, y, z, // 6
|
||
-x, y, z, // 7
|
||
};
|
||
mesh.indices = {
|
||
0, 2, 1, 0, 3, 2, // -z
|
||
4, 5, 6, 4, 6, 7, // +z
|
||
0, 1, 5, 0, 5, 4, // -y
|
||
1, 2, 6, 1, 6, 5, // +x
|
||
2, 3, 7, 2, 7, 6, // +y
|
||
3, 0, 4, 3, 4, 7, // -x
|
||
};
|
||
return mesh;
|
||
}
|
||
|
||
std::string cube_to_stl_ascii(const CubeMesh& mesh, const char* name = "vde_cube") {
|
||
std::string out = "solid ";
|
||
out += name;
|
||
out += "\n";
|
||
char line[256];
|
||
for (size_t t = 0; t + 2 < mesh.indices.size(); t += 3) {
|
||
const uint32_t a = mesh.indices[t];
|
||
const uint32_t b = mesh.indices[t + 1];
|
||
const uint32_t c = mesh.indices[t + 2];
|
||
const double* pa = &mesh.positions[3 * a];
|
||
const double* pb = &mesh.positions[3 * b];
|
||
const double* pc = &mesh.positions[3 * c];
|
||
const double ux = pb[0] - pa[0], uy = pb[1] - pa[1], uz = pb[2] - pa[2];
|
||
const double vx = pc[0] - pa[0], vy = pc[1] - pa[1], vz = pc[2] - pa[2];
|
||
double nx = uy * vz - uz * vy;
|
||
double ny = uz * vx - ux * vz;
|
||
double nz = ux * vy - uy * vx;
|
||
const double len = std::sqrt(nx * nx + ny * ny + nz * nz);
|
||
if (len > 1e-30) { nx /= len; ny /= len; nz /= len; }
|
||
std::snprintf(line, sizeof(line), " facet normal %.12g %.12g %.12g\n", nx, ny, nz);
|
||
out += line;
|
||
out += " outer loop\n";
|
||
for (const double* p : {pa, pb, pc}) {
|
||
std::snprintf(line, sizeof(line), " vertex %.17g %.17g %.17g\n",
|
||
p[0], p[1], p[2]);
|
||
out += line;
|
||
}
|
||
out += " endloop\n endfacet\n";
|
||
}
|
||
out += "endsolid ";
|
||
out += name;
|
||
out += "\n";
|
||
return out;
|
||
}
|
||
|
||
std::string cube_to_obj(const CubeMesh& mesh) {
|
||
std::string out = "# VDE official OBJ test fixture\n";
|
||
char line[256];
|
||
for (size_t i = 0; i < mesh.positions.size() / 3; ++i) {
|
||
std::snprintf(line, sizeof(line), "v %.17g %.17g %.17g\n",
|
||
mesh.positions[3 * i], mesh.positions[3 * i + 1],
|
||
mesh.positions[3 * i + 2]);
|
||
out += line;
|
||
}
|
||
for (size_t t = 0; t + 2 < mesh.indices.size(); t += 3) {
|
||
std::snprintf(line, sizeof(line), "f %u %u %u\n", mesh.indices[t] + 1,
|
||
mesh.indices[t + 1] + 1, mesh.indices[t + 2] + 1);
|
||
out += line;
|
||
}
|
||
return out;
|
||
}
|
||
|
||
std::string cube_to_ply_ascii(const CubeMesh& mesh) {
|
||
std::string out;
|
||
out += "ply\nformat ascii 1.0\n";
|
||
out += "element vertex " + std::to_string(mesh.positions.size() / 3) + "\n";
|
||
out += "property float x\nproperty float y\nproperty float z\n";
|
||
out += "element face " + std::to_string(mesh.indices.size() / 3) + "\n";
|
||
out += "property list uchar int vertex_indices\n";
|
||
out += "end_header\n";
|
||
char line[256];
|
||
for (size_t i = 0; i < mesh.positions.size() / 3; ++i) {
|
||
std::snprintf(line, sizeof(line), "%.17g %.17g %.17g\n",
|
||
mesh.positions[3 * i], mesh.positions[3 * i + 1],
|
||
mesh.positions[3 * i + 2]);
|
||
out += line;
|
||
}
|
||
for (size_t t = 0; t + 2 < mesh.indices.size(); t += 3) {
|
||
std::snprintf(line, sizeof(line), "3 %u %u %u\n", mesh.indices[t],
|
||
mesh.indices[t + 1], mesh.indices[t + 2]);
|
||
out += line;
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// 构造"截断二进制 STL":80B 零头 + u32 声明 12 → 实际 11 条完整记录(少 1 字节/1 条),
|
||
// 内置 detect_format_file 因尺寸不匹配无法识别(无内置路径),插件 probe 仍可识别并容错读取。
|
||
std::string truncated_binary_stl(const CubeMesh& mesh, uint32_t declared_count) {
|
||
std::vector<unsigned char> bytes(84, 0);
|
||
std::memcpy(bytes.data() + 80, &declared_count, 4);
|
||
const uint32_t written = declared_count > 0 ? declared_count - 1 : 0;
|
||
for (uint32_t t = 0; t < written && (t * 3 + 2) < mesh.indices.size(); ++t) {
|
||
const uint32_t ia = mesh.indices[t * 3 + 0];
|
||
const uint32_t ib = mesh.indices[t * 3 + 1];
|
||
const uint32_t ic = mesh.indices[t * 3 + 2];
|
||
float record[12] = {0, 0, 1};
|
||
const uint32_t ids[3] = {ia, ib, ic};
|
||
for (int k = 0; k < 3; ++k) {
|
||
record[3 + 3 * k + 0] = static_cast<float>(mesh.positions[3 * ids[k] + 0]);
|
||
record[3 + 3 * k + 1] = static_cast<float>(mesh.positions[3 * ids[k] + 1]);
|
||
record[3 + 3 * k + 2] = static_cast<float>(mesh.positions[3 * ids[k] + 2]);
|
||
}
|
||
const unsigned char* raw = reinterpret_cast<const unsigned char*>(record);
|
||
bytes.insert(bytes.end(), raw, raw + sizeof(record));
|
||
bytes.push_back(0);
|
||
bytes.push_back(0);
|
||
}
|
||
return std::string(reinterpret_cast<const char*>(bytes.data()), bytes.size());
|
||
}
|
||
|
||
// ── 测量结果(内置 BrepModel / 插件句柄统一口径)──
|
||
struct Measures {
|
||
double volume = 0.0;
|
||
double area = 0.0;
|
||
int faces = -1;
|
||
double bmin[3] = {0.0, 0.0, 0.0};
|
||
double bmax[3] = {0.0, 0.0, 0.0};
|
||
};
|
||
|
||
Measures measure_model(const BrepModel& model) {
|
||
Measures m;
|
||
m.volume = vde::brep::volume(model);
|
||
m.area = vde::brep::surface_area(model);
|
||
m.faces = static_cast<int>(model.num_faces());
|
||
const auto box = model.bounds();
|
||
m.bmin[0] = box.min().x();
|
||
m.bmin[1] = box.min().y();
|
||
m.bmin[2] = box.min().z();
|
||
m.bmax[0] = box.max().x();
|
||
m.bmax[1] = box.max().y();
|
||
m.bmax[2] = box.max().z();
|
||
return m;
|
||
}
|
||
|
||
Measures measure_handle(const VdePluginHost& host, VdeBody body) {
|
||
Measures m;
|
||
VdeBodyInfo info{};
|
||
info.struct_size = sizeof(VdeBodyInfo);
|
||
if (host.body_query(body, &info) != VDE_PLUGIN_OK) return m;
|
||
m.volume = info.volume;
|
||
m.area = info.surface_area;
|
||
m.faces = static_cast<int>(info.num_faces);
|
||
for (int i = 0; i < 3; ++i) {
|
||
m.bmin[i] = info.bbox_min[i];
|
||
m.bmax[i] = info.bbox_max[i];
|
||
}
|
||
return m;
|
||
}
|
||
|
||
void destroy_bodies(const VdePluginHost& host, std::vector<VdeBody>& bodies) {
|
||
for (VdeBody body : bodies) {
|
||
if (host.body_destroy) host.body_destroy(body);
|
||
}
|
||
bodies.clear();
|
||
}
|
||
|
||
class PluginFormatFixture : public ::testing::Test {
|
||
protected:
|
||
void SetUp() override { host_ = vde::plugin::make_geometry_host_services(); }
|
||
void TearDown() override {
|
||
PluginManager::instance().clear();
|
||
vde::plugin::release_geometry_host_services(&host_);
|
||
set_process_env("VDE_IMPORT_CAD_PREFER_PLUGIN", nullptr);
|
||
for (const auto& path : cleanup_paths_) std::remove(path.c_str());
|
||
cleanup_paths_.clear();
|
||
}
|
||
|
||
/// 重置管理器:可选宿主服务、可选扫描官方插件目录
|
||
void reset_manager(bool with_host_services, bool scan_official) {
|
||
PluginManager& pm = PluginManager::instance();
|
||
pm.clear();
|
||
if (with_host_services) {
|
||
pm.set_host_services(host_);
|
||
} else {
|
||
pm.clear_host_services();
|
||
}
|
||
pm.clear_search_paths();
|
||
if (scan_official && VDE_PLUGIN_OFFICIAL_DIR[0] != '\0') {
|
||
pm.add_search_path(VDE_PLUGIN_OFFICIAL_DIR);
|
||
}
|
||
pm.scan_search_paths();
|
||
}
|
||
|
||
bool load_official_plugins(std::string* error) {
|
||
PluginManager& pm = PluginManager::instance();
|
||
for (const char* id : {kStlId, kObjId, kPlyId, k3mfId, kGltfId}) {
|
||
if (pm.is_loaded(id)) continue;
|
||
if (!pm.load(id, error)) return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
std::string write_fixture(const char* filename, const std::string& content) {
|
||
const std::string path = temp_path(filename);
|
||
EXPECT_TRUE(write_text_file(path, content)) << path;
|
||
cleanup_paths_.push_back(path);
|
||
return path;
|
||
}
|
||
|
||
VdePluginHost host_{};
|
||
std::vector<std::string> cleanup_paths_;
|
||
};
|
||
|
||
/// 打印内置 vs 插件导入的对比数字(供验收/报告取证)
|
||
void log_compat(const char* format, const Measures& builtin, const Measures& plugin) {
|
||
std::printf(
|
||
"[compat] %-3s builtin: faces=%d volume=%.12g bbox=[%.9g,%.9g]x[%.9g,%.9g]x[%.9g,%.9g]\n"
|
||
"[compat] %-3s plugin : faces=%d volume=%.12g bbox=[%.9g,%.9g]x[%.9g,%.9g]x[%.9g,%.9g]\n",
|
||
format, builtin.faces, builtin.volume, builtin.bmin[0], builtin.bmax[0],
|
||
builtin.bmin[1], builtin.bmax[1], builtin.bmin[2], builtin.bmax[2], format,
|
||
plugin.faces, plugin.volume, plugin.bmin[0], plugin.bmax[0], plugin.bmin[1],
|
||
plugin.bmax[1], plugin.bmin[2], plugin.bmax[2]);
|
||
}
|
||
|
||
void expect_measures_close(const Measures& a, const Measures& b) {
|
||
EXPECT_EQ(a.faces, b.faces) << "面数应一致";
|
||
const double scale = std::max({std::abs(a.volume), std::abs(b.volume), 1e-12});
|
||
EXPECT_LE(std::abs(a.volume - b.volume) / scale, 1e-9) << a.volume << " vs " << b.volume;
|
||
for (int i = 0; i < 3; ++i) {
|
||
EXPECT_NEAR(a.bmin[i], b.bmin[i], 1e-9) << "bbox_min[" << i << "]";
|
||
EXPECT_NEAR(a.bmax[i], b.bmax[i], 1e-9) << "bbox_max[" << i << "]";
|
||
}
|
||
}
|
||
|
||
} // namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 1. 官方插件发现 / 加载 / 能力注册
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST_F(PluginFormatFixture, OfficialPluginsDiscoveredAndLoaded) {
|
||
ASSERT_NE(VDE_PLUGIN_OFFICIAL_DIR[0], '\0') << "缺少 VDE_PLUGIN_OFFICIAL_DIR 定义";
|
||
reset_manager(true, true);
|
||
PluginManager& pm = PluginManager::instance();
|
||
|
||
bool found_stl = false, found_obj = false, found_ply = false;
|
||
for (const auto& manifest : pm.plugins()) {
|
||
if (manifest.kind != PluginKind::Format) continue;
|
||
if (manifest.id == kStlId) found_stl = true;
|
||
if (manifest.id == kObjId) found_obj = true;
|
||
if (manifest.id == kPlyId) found_ply = true;
|
||
EXPECT_EQ(manifest.abi_major, VDE_PLUGIN_ABI_MAJOR);
|
||
EXPECT_LE(manifest.abi_minor, VDE_PLUGIN_ABI_MINOR);
|
||
}
|
||
EXPECT_TRUE(found_stl);
|
||
EXPECT_TRUE(found_obj);
|
||
EXPECT_TRUE(found_ply);
|
||
|
||
std::string error;
|
||
ASSERT_TRUE(load_official_plugins(&error)) << error;
|
||
|
||
const auto formats = pm.formats();
|
||
auto has_format = [&](const std::string& id) {
|
||
for (const auto& format : formats)
|
||
if (format.format_id == id) return true;
|
||
return false;
|
||
};
|
||
EXPECT_TRUE(has_format("stl"));
|
||
EXPECT_TRUE(has_format("obj"));
|
||
EXPECT_TRUE(has_format("ply"));
|
||
EXPECT_TRUE(pm.capabilities().has("format:stl.read"));
|
||
EXPECT_TRUE(pm.capabilities().has("format:stl.write"));
|
||
EXPECT_TRUE(pm.capabilities().has("format:obj.read"));
|
||
EXPECT_TRUE(pm.capabilities().has("format:ply.read"));
|
||
// P4-26:OBJ 插件补齐写能力并声明 format:obj.write
|
||
EXPECT_TRUE(pm.capabilities().has("format:obj.write"));
|
||
// P3-3:PLY 插件补齐二进制读写并声明 format:ply.write
|
||
EXPECT_TRUE(pm.capabilities().has("format:ply.write"));
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 2. 插件导入 vs 内置 import_cad 导入(体积/bbox/面数)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST_F(PluginFormatFixture, StlImportMatchesBuiltinImport) {
|
||
reset_manager(true, true);
|
||
std::string error;
|
||
ASSERT_TRUE(load_official_plugins(&error)) << error;
|
||
const CubeMesh cube = make_cube(20.0, 30.0, 40.0);
|
||
const std::string path = write_fixture("p1_official_stl.stl", cube_to_stl_ascii(cube));
|
||
|
||
CadImportResult builtin = vde::brep::import_cad(path);
|
||
ASSERT_TRUE(builtin.ok) << (builtin.messages.empty() ? "" : builtin.messages[0]);
|
||
ASSERT_EQ(builtin.bodies.size(), 1u);
|
||
const Measures expected = measure_model(builtin.bodies[0]);
|
||
EXPECT_NEAR(expected.volume, 20.0 * 30.0 * 40.0, 1e-6);
|
||
|
||
FormatImportResult plugin;
|
||
ASSERT_TRUE(PluginManager::instance().import_file(path, &plugin))
|
||
<< PluginManager::instance().last_error();
|
||
ASSERT_EQ(plugin.bodies.size(), 1u);
|
||
ASSERT_EQ(plugin.format_id, "stl");
|
||
const Measures actual = measure_handle(host_, plugin.bodies[0]);
|
||
log_compat("stl", expected, actual);
|
||
expect_measures_close(expected, actual);
|
||
destroy_bodies(host_, plugin.bodies);
|
||
}
|
||
|
||
TEST_F(PluginFormatFixture, ObjImportMatchesBuiltinImport) {
|
||
reset_manager(true, true);
|
||
std::string error;
|
||
ASSERT_TRUE(load_official_plugins(&error)) << error;
|
||
const CubeMesh cube = make_cube(2.0, 4.0, 6.0);
|
||
const std::string path = write_fixture("p1_official_obj.obj", cube_to_obj(cube));
|
||
|
||
CadImportResult builtin = vde::brep::import_cad(path);
|
||
ASSERT_TRUE(builtin.ok) << (builtin.messages.empty() ? "" : builtin.messages[0]);
|
||
ASSERT_EQ(builtin.bodies.size(), 1u);
|
||
const Measures expected = measure_model(builtin.bodies[0]);
|
||
EXPECT_NEAR(expected.volume, 48.0, 1e-6);
|
||
|
||
FormatImportResult plugin;
|
||
ASSERT_TRUE(PluginManager::instance().import_file(path, &plugin))
|
||
<< PluginManager::instance().last_error();
|
||
ASSERT_EQ(plugin.bodies.size(), 1u);
|
||
ASSERT_EQ(plugin.format_id, "obj");
|
||
const Measures obj_measures = measure_handle(host_, plugin.bodies[0]);
|
||
log_compat("obj", expected, obj_measures);
|
||
expect_measures_close(expected, obj_measures);
|
||
destroy_bodies(host_, plugin.bodies);
|
||
}
|
||
|
||
TEST_F(PluginFormatFixture, PlyImportMatchesBuiltinImport) {
|
||
reset_manager(true, true);
|
||
std::string error;
|
||
ASSERT_TRUE(load_official_plugins(&error)) << error;
|
||
const CubeMesh cube = make_cube(3.0, 3.0, 3.0);
|
||
const std::string path = write_fixture("p1_official_ply.ply", cube_to_ply_ascii(cube));
|
||
|
||
CadImportResult builtin = vde::brep::import_cad(path);
|
||
ASSERT_TRUE(builtin.ok) << (builtin.messages.empty() ? "" : builtin.messages[0]);
|
||
ASSERT_EQ(builtin.bodies.size(), 1u);
|
||
const Measures expected = measure_model(builtin.bodies[0]);
|
||
EXPECT_NEAR(expected.volume, 27.0, 1e-6);
|
||
|
||
FormatImportResult plugin;
|
||
ASSERT_TRUE(PluginManager::instance().import_file(path, &plugin))
|
||
<< PluginManager::instance().last_error();
|
||
ASSERT_EQ(plugin.bodies.size(), 1u);
|
||
ASSERT_EQ(plugin.format_id, "ply");
|
||
const Measures ply_measures = measure_handle(host_, plugin.bodies[0]);
|
||
log_compat("ply", expected, ply_measures);
|
||
expect_measures_close(expected, ply_measures);
|
||
destroy_bodies(host_, plugin.bodies);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 3. STL 插件写出 → 内置/插件再导入往返
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST_F(PluginFormatFixture, StlWriteReadRoundTripMatchesBuiltin) {
|
||
reset_manager(true, true);
|
||
std::string error;
|
||
ASSERT_TRUE(load_official_plugins(&error)) << error;
|
||
const CubeMesh cube = make_cube(10.0, 20.0, 30.0);
|
||
const std::string input = write_fixture("p1_stl_roundtrip_in.stl",
|
||
cube_to_stl_ascii(cube));
|
||
|
||
CadImportResult builtin = vde::brep::import_cad(input);
|
||
ASSERT_TRUE(builtin.ok);
|
||
ASSERT_EQ(builtin.bodies.size(), 1u);
|
||
const Measures expected = measure_model(builtin.bodies[0]);
|
||
|
||
FormatImportResult imported;
|
||
ASSERT_TRUE(PluginManager::instance().import_file(input, &imported))
|
||
<< PluginManager::instance().last_error();
|
||
ASSERT_EQ(imported.bodies.size(), 1u);
|
||
|
||
const std::string exported = temp_path("p1_stl_roundtrip_out.stl");
|
||
cleanup_paths_.push_back(exported);
|
||
ASSERT_TRUE(PluginManager::instance().export_file(exported, imported.bodies, "stl"))
|
||
<< PluginManager::instance().last_error();
|
||
destroy_bodies(host_, imported.bodies);
|
||
|
||
// 往返后:内置导入与插件导入都应给出与原始一致的体积/bbox/面数
|
||
CadImportResult reimported_builtin = vde::brep::import_cad(exported);
|
||
ASSERT_TRUE(reimported_builtin.ok);
|
||
ASSERT_EQ(reimported_builtin.bodies.size(), 1u);
|
||
expect_measures_close(expected, measure_model(reimported_builtin.bodies[0]));
|
||
|
||
FormatImportResult reimported_plugin;
|
||
ASSERT_TRUE(PluginManager::instance().import_file(exported, &reimported_plugin))
|
||
<< PluginManager::instance().last_error();
|
||
ASSERT_EQ(reimported_plugin.bodies.size(), 1u);
|
||
expect_measures_close(expected, measure_handle(host_, reimported_plugin.bodies[0]));
|
||
destroy_bodies(host_, reimported_plugin.bodies);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 4. import_cad 插件回退分派
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST_F(PluginFormatFixture, ImportCadFallsBackToPluginForUnknownContent) {
|
||
reset_manager(true, true);
|
||
std::string error;
|
||
ASSERT_TRUE(load_official_plugins(&error)) << error;
|
||
const CubeMesh cube = make_cube(2.0, 2.0, 2.0);
|
||
// 未知扩展名 + 内置 detect 无法识别的截断二进制 STL:只有插件 probe 能识别
|
||
const std::string path = write_fixture("p1_stl_unknown.fmt",
|
||
truncated_binary_stl(cube, 12));
|
||
CadImportResult result = vde::brep::import_cad(path);
|
||
ASSERT_TRUE(result.ok) << (result.messages.empty() ? "" : result.messages[0]);
|
||
ASSERT_EQ(result.bodies.size(), 1u);
|
||
bool plugin_dispatch_reported = false;
|
||
for (const auto& message : result.messages) {
|
||
if (message.find("插件分派") != std::string::npos) plugin_dispatch_reported = true;
|
||
}
|
||
EXPECT_TRUE(plugin_dispatch_reported);
|
||
}
|
||
|
||
|
||
TEST_F(PluginFormatFixture, ImportCadKeepsNativePathForKnownExtension) {
|
||
reset_manager(true, true);
|
||
std::string error;
|
||
ASSERT_TRUE(load_official_plugins(&error)) << error;
|
||
const CubeMesh cube = make_cube(2.0, 2.0, 2.0);
|
||
const std::string path = write_fixture("p1_stl_native.stl", cube_to_stl_ascii(cube));
|
||
|
||
CadImportResult result = vde::brep::import_cad(path);
|
||
ASSERT_TRUE(result.ok);
|
||
ASSERT_EQ(result.bodies.size(), 1u);
|
||
EXPECT_NEAR(vde::brep::volume(result.bodies[0]), 8.0, 1e-6);
|
||
for (const auto& message : result.messages) {
|
||
EXPECT_EQ(message.find("插件分派"), std::string::npos)
|
||
<< "已知扩展名应走内置快速路径,不应触发插件分派: " << message;
|
||
}
|
||
}
|
||
|
||
TEST_F(PluginFormatFixture, ImportCadWithoutHostServicesKeepsOriginalError) {
|
||
reset_manager(false, false);
|
||
const CubeMesh cube = make_cube(2.0, 2.0, 2.0);
|
||
// 完全无法识别的文本内容:无宿主服务时必须保持原 "无法识别格式" 错误
|
||
const std::string unknown = write_fixture("p1_unknown_no_host.fmt",
|
||
"not a cad file: hello world\n");
|
||
CadImportResult unknown_result = vde::brep::import_cad(unknown);
|
||
EXPECT_FALSE(unknown_result.ok);
|
||
bool original_message = false;
|
||
for (const auto& message : unknown_result.messages) {
|
||
if (message.find("无法识别格式") != std::string::npos) original_message = true;
|
||
}
|
||
EXPECT_TRUE(original_message) << "无宿主服务时必须保持原错误文案";
|
||
|
||
// 已知内置格式路径不受影响
|
||
const std::string stl = write_fixture("p1_stl_no_host.stl", cube_to_stl_ascii(cube));
|
||
CadImportResult builtin = vde::brep::import_cad(stl);
|
||
EXPECT_TRUE(builtin.ok);
|
||
}
|
||
|
||
TEST_F(PluginFormatFixture, ImportCadPreferPluginEnvForcesPluginDispatch) {
|
||
reset_manager(true, true);
|
||
std::string error;
|
||
ASSERT_TRUE(load_official_plugins(&error)) << error;
|
||
const CubeMesh cube = make_cube(2.0, 2.0, 2.0);
|
||
const std::string path = write_fixture("p1_stl_prefer.stl", cube_to_stl_ascii(cube));
|
||
|
||
ASSERT_TRUE(set_process_env("VDE_IMPORT_CAD_PREFER_PLUGIN", "1"));
|
||
CadImportResult result = vde::brep::import_cad(path);
|
||
set_process_env("VDE_IMPORT_CAD_PREFER_PLUGIN", nullptr);
|
||
ASSERT_TRUE(result.ok) << (result.messages.empty() ? "" : result.messages[0]);
|
||
ASSERT_EQ(result.bodies.size(), 1u);
|
||
EXPECT_NEAR(vde::brep::volume(result.bodies[0]), 8.0, 1e-6);
|
||
bool plugin_dispatch_reported = false;
|
||
for (const auto& message : result.messages) {
|
||
if (message.find("插件分派") != std::string::npos) plugin_dispatch_reported = true;
|
||
}
|
||
EXPECT_TRUE(plugin_dispatch_reported);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 5. 宿主 P1 body_from_mesh 服务
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST_F(PluginFormatFixture, HostBodyFromMeshValidatesInput) {
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
EXPECT_EQ(host_.body_from_mesh(nullptr, 0, nullptr, 0), nullptr);
|
||
EXPECT_GT(std::strlen(vde::plugin::geometry_host_last_error()), 0u);
|
||
|
||
const CubeMesh cube = make_cube(2.0, 2.0, 2.0);
|
||
// 越界索引 → nullptr + 错误信息(不崩溃)
|
||
std::vector<uint32_t> bad_indices = cube.indices;
|
||
bad_indices[0] = 9999;
|
||
vde::plugin::clear_geometry_host_last_error();
|
||
EXPECT_EQ(host_.body_from_mesh(cube.positions.data(), cube.positions.size() / 3,
|
||
bad_indices.data(), bad_indices.size() / 3),
|
||
nullptr);
|
||
EXPECT_GT(std::strlen(vde::plugin::geometry_host_last_error()), 0u);
|
||
|
||
// 正常网格 → 有效实体(12 面,体积 8)
|
||
ASSERT_NE(host_.body_from_mesh, nullptr);
|
||
VdeBody body = host_.body_from_mesh(cube.positions.data(),
|
||
cube.positions.size() / 3,
|
||
cube.indices.data(), cube.indices.size() / 3);
|
||
ASSERT_NE(body, nullptr) << vde::plugin::geometry_host_last_error();
|
||
const Measures measures = measure_handle(host_, body);
|
||
EXPECT_EQ(measures.faces, 12);
|
||
EXPECT_NEAR(measures.volume, 8.0, 1e-9);
|
||
EXPECT_NEAR(measures.bmin[0], -1.0, 1e-9);
|
||
EXPECT_NEAR(measures.bmax[2], 1.0, 1e-9);
|
||
host_.body_destroy(body);
|
||
}
|
||
|
||
TEST_F(PluginFormatFixture, Official3mfReadWrite) {
|
||
ASSERT_NE(VDE_PLUGIN_OFFICIAL_DIR[0], '\0');
|
||
reset_manager(true, true);
|
||
std::string error;
|
||
ASSERT_TRUE(load_official_plugins(&error)) << error;
|
||
|
||
const CubeMesh cube = make_cube(2.0, 3.0, 4.0);
|
||
VdeBody body = host_.body_from_mesh(cube.positions.data(), cube.positions.size() / 3,
|
||
cube.indices.data(), cube.indices.size() / 3);
|
||
ASSERT_NE(body, nullptr);
|
||
const std::string path = temp_path("vde_official_3mf_roundtrip.3mf");
|
||
cleanup_paths_.push_back(path);
|
||
ASSERT_TRUE(PluginManager::instance().export_file(path, {body}, "3mf"))
|
||
<< PluginManager::instance().last_error();
|
||
host_.body_destroy(body);
|
||
|
||
FormatImportResult imported;
|
||
ASSERT_TRUE(PluginManager::instance().import_file(path, &imported))
|
||
<< PluginManager::instance().last_error();
|
||
EXPECT_EQ(imported.format_id, "3mf");
|
||
EXPECT_FALSE(imported.bodies.empty());
|
||
destroy_bodies(host_, imported.bodies);
|
||
}
|
||
|
||
|
||
TEST_F(PluginFormatFixture, Official3mfTransformAndUnit) {
|
||
ASSERT_NE(VDE_PLUGIN_OFFICIAL_DIR[0], '\0');
|
||
reset_manager(true, true);
|
||
std::string error;
|
||
ASSERT_TRUE(load_official_plugins(&error)) << error;
|
||
|
||
const std::string path =
|
||
std::string(VDE_TEST_DATA_DIR) + "/tests/data/fixtures/3mf/transform_inch.3mf";
|
||
FormatImportResult imported;
|
||
ASSERT_TRUE(PluginManager::instance().import_file(path, &imported))
|
||
<< PluginManager::instance().last_error();
|
||
ASSERT_EQ(imported.format_id, "3mf");
|
||
ASSERT_EQ(imported.bodies.size(), 1u);
|
||
|
||
VdeBodyInfo info{};
|
||
info.struct_size = static_cast<uint32_t>(sizeof(info));
|
||
ASSERT_EQ(host_.body_query(imported.bodies[0], &info), VDE_PLUGIN_OK);
|
||
EXPECT_NEAR(info.bbox_min[0], 304.8, 1e-6);
|
||
EXPECT_NEAR(info.bbox_min[1], 76.2, 1e-6);
|
||
EXPECT_NEAR(info.bbox_min[2], 101.6, 1e-6);
|
||
EXPECT_NEAR(info.bbox_max[0], 330.2, 1e-6);
|
||
EXPECT_NEAR(info.bbox_max[1], 101.6, 1e-6);
|
||
EXPECT_NEAR(info.bbox_max[2], 101.6, 1e-6);
|
||
destroy_bodies(host_, imported.bodies);
|
||
}
|