Build system: fix VDE_BUILD_TYPE, MinGW big-obj, cross-platform temp dir Core fixes: Matrix4D inverse, ExactPredicates sign, GJK overflow, VdeFormat weights, VRML/Collada IO Test fixes: ObjectPool, Transaction paths, /tmp replacement, Matrix4D expectations Result: 292/295 fast tests pass (99%)
359 lines
11 KiB
C++
359 lines
11 KiB
C++
#include <gtest/gtest.h>
|
||
|
||
extern "C" {
|
||
#include "vde/capi/vde_capi.h"
|
||
}
|
||
|
||
#include <cstdio>
|
||
#include <cstring>
|
||
#include <cmath>
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// C API 全面覆盖测试
|
||
// 测试: vde_create_model / vde_destroy_model / vde_add_cube
|
||
// vde_export_step / vde_import_step / vde_get_face_count
|
||
// vde_get_face_area / vde_get_bounding_box / vde_get_version
|
||
// 映射到实际 C API:
|
||
// create_model→vde_create, destroy_model→vde_destroy
|
||
// add_cube→vde_body_create_box, export_step→vde_body_save_step
|
||
// import_step→vde_body_load_step, get_face_count→vde_mesh_num_faces
|
||
// get_face_area→网格面数量/数据, get_bounding_box→vde_mesh_get_bounds
|
||
// get_version→vde_version
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
// ── 1. vde_create_model / vde_destroy_model ─────────────────
|
||
|
||
TEST(CapiModelTest, CreateAndDestroy) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
vde_destroy(ctx);
|
||
// 销毁后不应 crash,重复销毁也是
|
||
}
|
||
|
||
TEST(CapiModelTest, MultipleContextsIndependent) {
|
||
VdeHandle ctx1 = vde_create();
|
||
VdeHandle ctx2 = vde_create();
|
||
ASSERT_NE(ctx1, nullptr);
|
||
ASSERT_NE(ctx2, nullptr);
|
||
EXPECT_NE(ctx1, ctx2);
|
||
vde_destroy(ctx1);
|
||
vde_destroy(ctx2);
|
||
}
|
||
|
||
TEST(CapiModelTest, DestroyNullIsSafe) {
|
||
// NULL 销毁应安全
|
||
vde_destroy(nullptr);
|
||
SUCCEED();
|
||
}
|
||
|
||
// ── 2. vde_add_cube → vde_body_create_box ─────────────────
|
||
|
||
TEST(CapiModelTest, CreateBox) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 3.0, 4.0);
|
||
ASSERT_NE(box, nullptr);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiModelTest, CreateBoxThenToMesh) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 1.0, 1.0, 1.0);
|
||
ASSERT_NE(box, nullptr);
|
||
|
||
VdeMeshHandle mesh = vde_body_to_mesh(ctx, box, 0.1);
|
||
ASSERT_NE(mesh, nullptr);
|
||
EXPECT_GT(vde_mesh_num_vertices(mesh), 0u);
|
||
EXPECT_GT(vde_mesh_num_faces(mesh), 0u);
|
||
|
||
vde_mesh_free(mesh);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiModelTest, CreateBoxMultipleSizes) {
|
||
VdeHandle ctx = vde_create();
|
||
|
||
// 正方体
|
||
VdeBodyHandle cube = vde_body_create_box(ctx, 1.0, 1.0, 1.0);
|
||
ASSERT_NE(cube, nullptr);
|
||
VdeMeshHandle m1 = vde_body_to_mesh(ctx, cube, 0.1);
|
||
EXPECT_GT(vde_mesh_num_vertices(m1), 0u);
|
||
|
||
// 扁平体
|
||
VdeBodyHandle flat = vde_body_create_box(ctx, 10.0, 0.1, 5.0);
|
||
ASSERT_NE(flat, nullptr);
|
||
VdeMeshHandle m2 = vde_body_to_mesh(ctx, flat, 0.1);
|
||
EXPECT_GT(vde_mesh_num_vertices(m2), 0u);
|
||
|
||
vde_mesh_free(m1);
|
||
vde_mesh_free(m2);
|
||
vde_body_free(cube);
|
||
vde_body_free(flat);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
// ── 3. vde_import_step / vde_export_step ──────────────────
|
||
|
||
TEST(CapiModelTest, LoadStepInvalidPath) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle* bodies = nullptr;
|
||
int count = vde_body_load_step(ctx, "/nonexistent/file.stp", &bodies);
|
||
EXPECT_EQ(count, 0);
|
||
EXPECT_NE(vde_last_error(ctx), nullptr);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiModelTest, SaveStepBasic) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 1.0, 1.0, 1.0);
|
||
|
||
VdeBodyHandle bodies[] = {box};
|
||
int ok = vde_body_save_step(ctx, bodies, 1, VDE_TEST_TEMP_DIR "test_vde_capi_export.stp");
|
||
EXPECT_EQ(ok, 1);
|
||
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
std::remove(VDE_TEST_TEMP_DIR "test_vde_capi_export.stp");
|
||
}
|
||
|
||
TEST(CapiModelTest, StepRoundtrip) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 3.0, 4.0);
|
||
ASSERT_NE(box, nullptr);
|
||
|
||
VdeBodyHandle bodies[] = {box};
|
||
int ok = vde_body_save_step(ctx, bodies, 1, VDE_TEST_TEMP_DIR "test_capi_roundtrip.stp");
|
||
EXPECT_EQ(ok, 1);
|
||
|
||
// 尝试重新加载
|
||
VdeBodyHandle* loaded = nullptr;
|
||
int count = vde_body_load_step(ctx, VDE_TEST_TEMP_DIR "test_capi_roundtrip.stp", &loaded);
|
||
if (count > 0) {
|
||
EXPECT_GE(count, 1);
|
||
vde_body_free_array(loaded, count);
|
||
}
|
||
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
std::remove(VDE_TEST_TEMP_DIR "test_capi_roundtrip.stp");
|
||
}
|
||
|
||
// ── 4. vde_get_face_count → vde_mesh_num_faces ────────────
|
||
|
||
TEST(CapiModelTest, GetFaceCount) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 1.0, 1.0, 1.0);
|
||
VdeMeshHandle mesh = vde_body_to_mesh(ctx, box, 0.1);
|
||
|
||
size_t nf = vde_mesh_num_faces(mesh);
|
||
size_t nv = vde_mesh_num_vertices(mesh);
|
||
|
||
EXPECT_GT(nf, 0u);
|
||
EXPECT_GT(nv, 0u);
|
||
// 欧拉公式: V - E + F = 2 (对于凸多面体)
|
||
// 对于三角形网格: 3F = 2E (每条边共享两个面,边界除外)
|
||
// 对于封闭网格: V - 3F/2 + F = 2 → V - F/2 = 2 → V ≈ F/2 + 2
|
||
// 不严格要求,仅检查合理性
|
||
EXPECT_GT(nv + nf, 0u); // 至少非空
|
||
|
||
vde_mesh_free(mesh);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiModelTest, FaceCountDifferentSizes) {
|
||
VdeHandle ctx = vde_create();
|
||
|
||
// 大盒子应有更多面(更精细 tessellation)
|
||
VdeBodyHandle big = vde_body_create_box(ctx, 10.0, 10.0, 10.0);
|
||
VdeMeshHandle m1 = vde_body_to_mesh(ctx, big, 0.5);
|
||
size_t nf1 = vde_mesh_num_faces(m1);
|
||
|
||
VdeBodyHandle small = vde_body_create_box(ctx, 0.5, 0.5, 0.5);
|
||
VdeMeshHandle m2 = vde_body_to_mesh(ctx, small, 0.5);
|
||
size_t nf2 = vde_mesh_num_faces(m2);
|
||
|
||
EXPECT_GT(nf1, 0u);
|
||
EXPECT_GT(nf2, 0u);
|
||
|
||
vde_mesh_free(m1);
|
||
vde_mesh_free(m2);
|
||
vde_body_free(big);
|
||
vde_body_free(small);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
// ── 5. vde_get_face_area ──────────────────────────────────
|
||
|
||
TEST(CapiModelTest, FaceDataAccess) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 1.0, 1.0, 1.0);
|
||
VdeMeshHandle mesh = vde_body_to_mesh(ctx, box, 0.1);
|
||
|
||
size_t nf = vde_mesh_num_faces(mesh);
|
||
ASSERT_GT(nf, 0u);
|
||
|
||
// 读取第一个面的顶点索引
|
||
int* indices = nullptr;
|
||
int n = vde_mesh_get_face(mesh, 0, &indices);
|
||
if (n > 0 && indices) {
|
||
EXPECT_EQ(n, 3); // 三角面
|
||
|
||
// 通过顶点索引读取坐标验证三角形面积非零
|
||
double x0, y0, z0, x1, y1, z1, x2, y2, z2;
|
||
vde_mesh_get_vertex(mesh, indices[0], &x0, &y0, &z0);
|
||
vde_mesh_get_vertex(mesh, indices[1], &x1, &y1, &z1);
|
||
vde_mesh_get_vertex(mesh, indices[2], &x2, &y2, &z2);
|
||
|
||
// 计算三角形面积
|
||
double ax = x1 - x0, ay = y1 - y0, az = z1 - z0;
|
||
double bx = x2 - x0, by = y2 - y0, bz = z2 - z0;
|
||
double cx = ay*bz - az*by;
|
||
double cy = az*bx - ax*bz;
|
||
double cz = ax*by - ay*bx;
|
||
double area = 0.5 * std::sqrt(cx*cx + cy*cy + cz*cz);
|
||
EXPECT_GT(area, 0.0);
|
||
|
||
vde_free_buffer(reinterpret_cast<uint8_t*>(indices));
|
||
}
|
||
|
||
vde_mesh_free(mesh);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
// ── 6. vde_get_bounding_box ───────────────────────────────
|
||
|
||
TEST(CapiModelTest, GetBoundingBox) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 3.0, 4.0); // 注意: make_box 以原点为中心 → 范围 [-w/2,w/2]...
|
||
|
||
VdeMeshHandle mesh = vde_body_to_mesh(ctx, box, 0.1);
|
||
ASSERT_NE(mesh, nullptr);
|
||
|
||
double min_x, min_y, min_z, max_x, max_y, max_z;
|
||
vde_mesh_get_bounds(mesh, &min_x, &min_y, &min_z, &max_x, &max_y, &max_z);
|
||
|
||
// 包围盒应包含几何体
|
||
EXPECT_LE(min_x, max_x);
|
||
EXPECT_LE(min_y, max_y);
|
||
EXPECT_LE(min_z, max_z);
|
||
|
||
// 边长应接近 box 尺寸: w=2,h=3,d=4
|
||
double dx = max_x - min_x;
|
||
double dy = max_y - min_y;
|
||
double dz = max_z - min_z;
|
||
|
||
EXPECT_NEAR(dx, 2.0, 0.5);
|
||
EXPECT_NEAR(dy, 3.0, 0.5);
|
||
EXPECT_NEAR(dz, 4.0, 0.5);
|
||
|
||
vde_mesh_free(mesh);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiModelTest, BoundingBoxNonTrivial) {
|
||
VdeHandle ctx = vde_create();
|
||
|
||
// 圆柱体
|
||
VdeBodyHandle cyl = vde_body_create_cylinder(ctx, 1.0, 3.0, 32);
|
||
VdeMeshHandle mesh = vde_body_to_mesh(ctx, cyl, 0.05);
|
||
|
||
double min_x, min_y, min_z, max_x, max_y, max_z;
|
||
vde_mesh_get_bounds(mesh, &min_x, &min_y, &min_z, &max_x, &max_y, &max_z);
|
||
|
||
// 半径=1.0, 高度方向沿 Y 轴
|
||
double dx = max_x - min_x;
|
||
double dy = max_y - min_y;
|
||
double dz = max_z - min_z;
|
||
|
||
EXPECT_NEAR(dx, 2.0, 0.3);
|
||
EXPECT_NEAR(dy, 3.0, 0.3);
|
||
EXPECT_NEAR(dz, 2.0, 0.3);
|
||
|
||
vde_mesh_free(mesh);
|
||
vde_body_free(cyl);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
// ── 7. vde_get_version ────────────────────────────────────
|
||
|
||
TEST(CapiModelTest, GetVersion) {
|
||
const char* ver = vde_version();
|
||
ASSERT_NE(ver, nullptr);
|
||
EXPECT_GT(strlen(ver), 0u);
|
||
|
||
// 应至少包含 '.' (semver)
|
||
EXPECT_NE(strchr(ver, '.'), nullptr);
|
||
}
|
||
|
||
TEST(CapiModelTest, VersionIsConsistent) {
|
||
// 多次调用返回一致的版本
|
||
const char* v1 = vde_version();
|
||
const char* v2 = vde_version();
|
||
EXPECT_STREQ(v1, v2);
|
||
}
|
||
|
||
// ── 8. vde_get_face_count for mesh ────────────────────────
|
||
|
||
TEST(CapiModelTest, MeshFaceCountConsistency) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
VdeMeshHandle mesh = vde_body_to_mesh(ctx, box, 0.05);
|
||
|
||
size_t nf = vde_mesh_num_faces(mesh);
|
||
size_t nv = vde_mesh_num_vertices(mesh);
|
||
EXPECT_GT(nf, 0u);
|
||
EXPECT_GT(nv, 0u);
|
||
|
||
// 封闭三角网格: 3F ≈ 2E, E ≈ 3/2 V (近似)
|
||
// 对于 box: F ≈ 2V
|
||
EXPECT_GT(nf, nv / 2); // 面数不应远少于顶点
|
||
|
||
vde_mesh_free(mesh);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
// ── 9. 综合集成测试 ───────────────────────────────────────
|
||
|
||
TEST(CapiModelTest, FullWorkflow) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
|
||
// Step 1: 创建立方体
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
|
||
// Step 2: 转换为网格
|
||
VdeMeshHandle mesh = vde_body_to_mesh(ctx, box, 0.1);
|
||
ASSERT_NE(mesh, nullptr);
|
||
|
||
// Step 3: 检查网格信息
|
||
EXPECT_GT(vde_mesh_num_vertices(mesh), 0u);
|
||
EXPECT_GT(vde_mesh_num_faces(mesh), 0u);
|
||
|
||
// Step 4: 包围盒
|
||
double x0, y0, z0, x1, y1, z1;
|
||
vde_mesh_get_bounds(mesh, &x0, &y0, &z0, &x1, &y1, &z1);
|
||
EXPECT_LE(x0, x1);
|
||
EXPECT_LE(y0, y1);
|
||
EXPECT_LE(z0, z1);
|
||
|
||
// Step 5: 导出 STEP
|
||
VdeBodyHandle bodies[] = {box};
|
||
int saved = vde_body_save_step(ctx, bodies, 1, VDE_TEST_TEMP_DIR "test_capi_workflow.stp");
|
||
EXPECT_EQ(saved, 1);
|
||
|
||
// Step 6: 版本
|
||
EXPECT_NE(vde_version(), nullptr);
|
||
|
||
// Cleanup
|
||
vde_mesh_free(mesh);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
std::remove(VDE_TEST_TEMP_DIR "test_capi_workflow.stp");
|
||
}
|