Files
ViewDesignEngine/tests/capi/test_capi_split.cpp
T
hm 88653eff2f feat(plugin): P1 微内核 — 官方格式插件 + C API 拆分 + 模块模式/依赖守卫(1.0.46)
- P1-1:VDE_BUILD_IO 开关;C API 拆分 vde_kernel_capi(不链接 vde_io)/vde_io_capi/旧 vde_capi 聚合;
  kernel SDK 导出 vde::kernel_capi + vde::kernel::capi 别名;kernel-only 配置/构建/install/consumer 验证;
  src/capi/vde_capi_io.cpp、tests/capi/test_capi_split.cpp 新增;
- P1-2:插件 ABI 1.1(VdePluginHost 尾部追加 body_from_mesh,旧宿主/旧插件按 struct_size 兼容);
  官方格式插件 plugins/official/{stl,obj,ply}(纯 C ABI、不链接内核;STL 读写、OBJ/PLY 只读;与内置导入
  面数/体积/bbox 一致到 1e-9);import_cad 插件回退(内置优先 + VDE_IMPORT_CAD_PREFER_PLUGIN 显式);
  新增 tests/plugin/test_plugin_format_official.cpp 10 用例;
- P1-3:VDE_MODULES_MODE(static/hybrid 生效;shared/plugin 预留 WARNING);6 个业务模块独立开关;
  vde::io/vde::app 聚合 + vde::engine deprecated;cmake/check_dependency_direction.cmake +
  VDE_DependencyDirection_Guard CTest(六配置 PASS、负向可检出);
- 文档:plugin-development §10/§11、architecture §2.2–2.4/§7、design §7/§8 P1 进展;
- 版本 1.0.46(CMake/version.h/test_version/README/docs index/module-manual/CHANGELOG 同步)。

验证:全量构建 0 error;全量 ctest 7369 注册、7274/7274 PASS(0 failed,25 skipped,300.51s,-E Benchmark);
插件 ctest 54/54、官方插件 10/10、test_capi_split 10/10(kernel 7+io 3);kernel-only / no-cam / 业务全关 /
static / shared 配置均通过;依赖守卫 PASS。
2026-10-28 14:46:31 +08:00

315 lines
12 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.
// P1-1 C API 拆分回归:vde_kernel_capi(不链接 io)/ vde_io_capi(格式 IO)
//
// 判别性目标:
// · 本测试可执行文件只链接 vde_kernel_capi(+ 内核模块),**不链接 vde_io**:
// kernel-only 构建(-DVDE_BUILD_IO=OFF)下也能构建并运行内核几何子集;
// · io 启用时额外链接 vde_io_capi 并验证 STEP/IGES、多格式网格读写与二进制
// 序列化;io 关闭时这些用例被编译排除(VDE_CAPI_SPLIT_HAS_IO 未定义);
// · 旧 ABI 兼容(旧 test_capi* 由聚合 vde_capi 继续覆盖)。
#include <gtest/gtest.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include "vde/capi/vde_capi.h"
#ifndef VDE_TEST_TEMP_DIR
#define VDE_TEST_TEMP_DIR "./"
#endif
namespace {
std::string temp_path(const char* name) {
return std::string(VDE_TEST_TEMP_DIR) + name;
}
struct Context {
VdeHandle handle = vde_create();
~Context() {
if (handle) vde_destroy(handle);
}
};
void remove_file(const std::string& path) { std::remove(path.c_str()); }
} // namespace
// ═══════════════════════════════════════════════════════════
// kernel C API(vde_kernel_capi,不依赖 vde_io)
// ═══════════════════════════════════════════════════════════
TEST(CapiKernelSplitTest, ContextVersionAndErrors) {
Context ctx;
ASSERT_NE(ctx.handle, nullptr);
const char* version = vde_version();
ASSERT_NE(version, nullptr);
EXPECT_GT(std::strlen(version), 0u);
// 非法调用写 last_error 且不崩溃
(void)vde_body_create_box(nullptr, 1.0, 1.0, 1.0);
const char* error = vde_last_error(ctx.handle);
EXPECT_NE(error, nullptr);
vde_free_buffer(nullptr);
}
TEST(CapiKernelSplitTest, MeshLifecycleAndQuery) {
Context ctx;
ASSERT_NE(ctx.handle, nullptr);
VdeBodyHandle box = vde_body_create_box(ctx.handle, 2.0, 2.0, 2.0);
ASSERT_NE(box, nullptr);
EXPECT_EQ(vde_body_get_face_count(box), 6);
EXPECT_EQ(vde_body_get_edge_count(box), 12);
VdeMeshHandle mesh = vde_body_to_mesh(ctx.handle, box, 0.05);
ASSERT_NE(mesh, nullptr);
EXPECT_GT(vde_mesh_num_vertices(mesh), 0u);
EXPECT_GT(vde_mesh_num_faces(mesh), 0u);
double x = 0, y = 0, z = 0;
vde_mesh_get_vertex(mesh, 0, &x, &y, &z);
EXPECT_LE(std::fabs(x), 1.0 + 1e-9);
double minx = 0, miny = 0, minz = 0, maxx = 0, maxy = 0, maxz = 0;
vde_mesh_get_bounds(mesh, &minx, &miny, &minz, &maxx, &maxy, &maxz);
EXPECT_NEAR(maxx - minx, 2.0, 1e-6);
EXPECT_NEAR(maxy - miny, 2.0, 1e-6);
EXPECT_NEAR(maxz - minz, 2.0, 1e-6);
VdeMeshHandle simplified = vde_mesh_simplify(ctx.handle, mesh, 0.5);
ASSERT_NE(simplified, nullptr);
EXPECT_LE(vde_mesh_num_faces(simplified), vde_mesh_num_faces(mesh));
VdeMeshHandle smoothed = vde_mesh_smooth(ctx.handle, mesh, 1);
ASSERT_NE(smoothed, nullptr);
vde_mesh_free(smoothed);
vde_mesh_free(simplified);
vde_mesh_free(mesh);
vde_body_free(box);
}
TEST(CapiKernelSplitTest, BodyBooleanCloneFreeArray) {
Context ctx;
ASSERT_NE(ctx.handle, nullptr);
VdeBodyHandle a = vde_body_create_box(ctx.handle, 2.0, 2.0, 2.0);
ASSERT_NE(a, nullptr);
VdeBodyHandle b = vde_body_create_sphere(ctx.handle, 1.2, 16, 8);
ASSERT_NE(b, nullptr);
VdeBodyHandle clone = vde_body_clone(ctx.handle, a);
ASSERT_NE(clone, nullptr);
EXPECT_EQ(vde_body_get_face_count(clone), 6);
VdeBodyHandle united = vde_body_boolean(ctx.handle, a, b, 0);
ASSERT_NE(united, nullptr);
EXPECT_GT(vde_body_get_face_count(united), 0);
VdeBodyHandle* array = new VdeBodyHandle[2];
array[0] = clone;
array[1] = united;
vde_body_free_array(array, 2); // 释放数组 + 两个句柄
vde_body_free(a);
vde_body_free(b);
}
TEST(CapiKernelSplitTest, DisplayMeshWireframeAndBodyInfo) {
Context ctx;
ASSERT_NE(ctx.handle, nullptr);
VdeBodyHandle box = vde_body_create_box(ctx.handle, 2.0, 2.0, 2.0);
ASSERT_NE(box, nullptr);
VdeDisplayMesh dm{};
const int kernel_flags = VDE_DISPLAY_NORMALS | VDE_DISPLAY_FACE_IDS |
VDE_DISPLAY_SURFACE_KINDS | VDE_DISPLAY_SMOOTH_GROUPS;
ASSERT_EQ(vde_body_to_display_mesh(ctx.handle, box, 0.002, 1, 10.0,
kernel_flags, &dm),
VDE_CAPI_OK);
EXPECT_EQ(dm.num_triangles, 12u);
ASSERT_NE(dm.positions, nullptr);
ASSERT_NE(dm.indices, nullptr);
ASSERT_NE(dm.triangle_face_ids, nullptr);
ASSERT_NE(dm.triangle_surface_kinds, nullptr);
vde_free_buffer(reinterpret_cast<uint8_t*>(dm.positions));
vde_free_buffer(reinterpret_cast<uint8_t*>(dm.normals));
vde_free_buffer(reinterpret_cast<uint8_t*>(dm.indices));
vde_free_buffer(reinterpret_cast<uint8_t*>(dm.triangle_face_ids));
vde_free_buffer(reinterpret_cast<uint8_t*>(dm.triangle_surface_kinds));
VdeWireframe wf{};
ASSERT_EQ(vde_body_to_wireframe(ctx.handle, box, 0.01, 0, 1, &wf), VDE_CAPI_OK);
EXPECT_EQ(wf.num_segments, 12u);
vde_free_buffer(reinterpret_cast<uint8_t*>(wf.positions));
vde_free_buffer(reinterpret_cast<uint8_t*>(wf.segments));
vde_free_buffer(reinterpret_cast<uint8_t*>(wf.segment_edge_ids));
// 面/边显示信息(内核 display_mesh 实现)
VdeFaceInfo face{};
ASSERT_EQ(vde_body_get_face_info(box, 0, &face), VDE_CAPI_OK);
EXPECT_EQ(face.surface_kind, static_cast<int>(VDE_SURFACE_PLANE));
VdeEdgeInfo edge{};
ASSERT_EQ(vde_body_get_edge_info(box, 0, &edge), VDE_CAPI_OK);
EXPECT_EQ(edge.kind, static_cast<int>(VDE_EDGE_LINE));
double* points = nullptr;
size_t count = 0;
ASSERT_EQ(vde_body_get_edge_polyline(ctx.handle, box, 0, 0.01, 0, &points, &count),
VDE_CAPI_OK);
EXPECT_GE(count, 2u);
vde_free_buffer(reinterpret_cast<uint8_t*>(points));
vde_body_free(box);
}
TEST(CapiKernelSplitTest, SdfAndCollision) {
Context ctx;
ASSERT_NE(ctx.handle, nullptr);
VdeSdfHandle sphere = vde_sdf_sphere(ctx.handle, 1.0);
ASSERT_NE(sphere, nullptr);
VdeMeshHandle mesh = vde_sdf_to_mesh(ctx.handle, sphere, 16);
ASSERT_NE(mesh, nullptr);
EXPECT_GT(vde_mesh_num_vertices(mesh), 0u);
vde_mesh_free(mesh);
vde_sdf_free(sphere);
// 两球相交 + 相离
EXPECT_EQ(vde_collision_gjk_spheres(ctx.handle, 0, 0, 0, 1.0, 1.5, 0, 0, 1.0), 1);
EXPECT_EQ(vde_collision_gjk_spheres(ctx.handle, 0, 0, 0, 1.0, 5.0, 0, 0, 1.0), 0);
}
TEST(CapiKernelSplitTest, AssemblyHandle) {
Context ctx;
ASSERT_NE(ctx.handle, nullptr);
VdeAssemblyHandle assembly = vde_assembly_create(ctx.handle, "split");
ASSERT_NE(assembly, nullptr);
VdeBodyHandle box = vde_body_create_box(ctx.handle, 1.0, 1.0, 1.0);
ASSERT_NE(box, nullptr);
EXPECT_EQ(vde_assembly_add_part(assembly, "part0", box), 1);
EXPECT_EQ(vde_assembly_part_count(assembly), 1);
vde_assembly_free(assembly);
vde_body_free(box);
}
TEST(CapiKernelSplitTest, KernelInvalidHandlesDoNotCrash) {
Context ctx;
ASSERT_NE(ctx.handle, nullptr);
vde_mesh_free(nullptr); // void 返回,仅确保不崩
vde_body_free(nullptr);
vde_sdf_free(nullptr);
vde_assembly_free(nullptr);
EXPECT_EQ(vde_body_get_face_count(nullptr), 0);
EXPECT_EQ(vde_body_get_edge_count(nullptr), 0);
VdeFaceInfo face{};
EXPECT_NE(vde_body_get_face_info(nullptr, 0, &face), VDE_CAPI_OK);
VdeWireframe wf{};
EXPECT_NE(vde_body_to_wireframe(ctx.handle, nullptr, 0.01, 0, 0, &wf), VDE_CAPI_OK);
}
// ═══════════════════════════════════════════════════════════
// io C API(vde_io_capi):io 启用时编译/运行
// ═══════════════════════════════════════════════════════════
#ifdef VDE_CAPI_SPLIT_HAS_IO
TEST(CapiIoSplitTest, MeshObjStlPlyRoundTrip) {
Context ctx;
ASSERT_NE(ctx.handle, nullptr);
VdeBodyHandle box = vde_body_create_box(ctx.handle, 2.0, 2.0, 2.0);
ASSERT_NE(box, nullptr);
VdeMeshHandle mesh = vde_body_to_mesh(ctx.handle, box, 0.05);
ASSERT_NE(mesh, nullptr);
const size_t triangles = vde_mesh_num_faces(mesh);
const size_t vertices = vde_mesh_num_vertices(mesh);
const char* formats[] = {"obj", "stl", "ply"};
for (const char* format : formats) {
const std::string path = temp_path((std::string("test_capi_split_mesh.") +
format).c_str());
remove_file(path);
ASSERT_EQ(vde_save_mesh(ctx.handle, mesh, path.c_str(), format), 1)
<< format << " 写出失败: " << vde_last_error(ctx.handle);
VdeMeshHandle loaded = vde_load_mesh(ctx.handle, path.c_str());
ASSERT_NE(loaded, nullptr) << format << " 读回失败: " << vde_last_error(ctx.handle);
EXPECT_GT(vde_mesh_num_vertices(loaded), 0u) << format;
vde_mesh_free(loaded);
remove_file(path);
}
EXPECT_GT(vertices, 0u);
EXPECT_GT(triangles, 0u);
// 不存在的文件:返回 NULL 且 last_error 非空(不崩溃)
VdeMeshHandle missing = vde_load_mesh(ctx.handle,
temp_path("test_capi_split_missing.obj").c_str());
EXPECT_EQ(missing, nullptr);
EXPECT_GT(std::strlen(vde_last_error(ctx.handle)), 0u);
vde_mesh_free(mesh);
vde_body_free(box);
}
TEST(CapiIoSplitTest, StepAndIgesRoundTrip) {
Context ctx;
ASSERT_NE(ctx.handle, nullptr);
VdeBodyHandle box = vde_body_create_box(ctx.handle, 3.0, 4.0, 5.0);
ASSERT_NE(box, nullptr);
const std::string step_path = temp_path("test_capi_split_roundtrip.stp");
remove_file(step_path);
ASSERT_EQ(vde_body_save_step(ctx.handle, &box, 1, step_path.c_str()), 1);
VdeBodyHandle* step_bodies = nullptr;
const int step_count = vde_body_load_step(ctx.handle, step_path.c_str(), &step_bodies);
EXPECT_GE(step_count, 1);
if (step_count > 0 && step_bodies) {
EXPECT_GE(vde_body_get_face_count(step_bodies[0]), 1);
vde_body_free_array(step_bodies, step_count);
}
remove_file(step_path);
const std::string iges_path = temp_path("test_capi_split_roundtrip.igs");
remove_file(iges_path);
ASSERT_EQ(vde_body_save_iges(ctx.handle, &box, 1, iges_path.c_str()), 1);
VdeBodyHandle* iges_bodies = nullptr;
const int iges_count = vde_body_load_iges(ctx.handle, iges_path.c_str(), &iges_bodies);
EXPECT_GE(iges_count, 1);
if (iges_count > 0 && iges_bodies) {
EXPECT_GE(vde_body_get_face_count(iges_bodies[0]), 1);
vde_body_free_array(iges_bodies, iges_count);
}
remove_file(iges_path);
// 不存在的文件:0 个实体,out 置空(不崩溃)
VdeBodyHandle* missing = nullptr;
EXPECT_EQ(vde_body_load_step(ctx.handle,
temp_path("test_capi_split_absent.stp").c_str(),
&missing),
0);
EXPECT_EQ(missing, nullptr);
vde_body_free(box);
}
TEST(CapiIoSplitTest, MeshSerializationRoundTrip) {
Context ctx;
ASSERT_NE(ctx.handle, nullptr);
VdeBodyHandle box = vde_body_create_box(ctx.handle, 1.0, 2.0, 3.0);
ASSERT_NE(box, nullptr);
VdeMeshHandle mesh = vde_body_to_mesh(ctx.handle, box, 0.05);
ASSERT_NE(mesh, nullptr);
const size_t vertices = vde_mesh_num_vertices(mesh);
const size_t triangles = vde_mesh_num_faces(mesh);
uint8_t* data = nullptr;
const size_t size = vde_serialize_mesh(ctx.handle, mesh, &data);
ASSERT_NE(data, nullptr);
ASSERT_GT(size, 0u);
VdeMeshHandle loaded = vde_deserialize_mesh(ctx.handle, data, size);
vde_free_buffer(data);
ASSERT_NE(loaded, nullptr);
EXPECT_EQ(vde_mesh_num_vertices(loaded), vertices);
EXPECT_EQ(vde_mesh_num_faces(loaded), triangles);
// 非法 buffer:nullptr(不抛异常穿越 C 边界)
EXPECT_EQ(vde_deserialize_mesh(ctx.handle, nullptr, 0), nullptr);
vde_mesh_free(loaded);
vde_mesh_free(mesh);
vde_body_free(box);
}
#endif // VDE_CAPI_SPLIT_HAS_IO