771 lines
31 KiB
C++
771 lines
31 KiB
C++
/**
|
||
* @file test_capi_display.cpp
|
||
* @brief Phase6 C API 显示级网格/线框/B-Rep 面边信息测试
|
||
*
|
||
* 覆盖:box display mesh(6 面/12 三角/法线/面 id/曲面类型)、cylinder 线框
|
||
* 圆边(kind=Circle、radius)、face/edge info、错误参数/空句柄不崩。
|
||
*/
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <cmath>
|
||
#include <cstdio>
|
||
#include <cstdint>
|
||
#include <cstring>
|
||
#include <set>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "vde/brep/brep.h"
|
||
#include "vde/capi/vde_capi.h"
|
||
|
||
namespace {
|
||
|
||
void free_display(VdeDisplayMesh& dm) {
|
||
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));
|
||
dm = VdeDisplayMesh{};
|
||
}
|
||
|
||
void free_wireframe(VdeWireframe& wf) {
|
||
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));
|
||
wf = VdeWireframe{};
|
||
}
|
||
|
||
double point_distance(const double* pts, size_t i, size_t j) {
|
||
const double dx = pts[i * 3 + 0] - pts[j * 3 + 0];
|
||
const double dy = pts[i * 3 + 1] - pts[j * 3 + 1];
|
||
const double dz = pts[i * 3 + 2] - pts[j * 3 + 2];
|
||
return std::sqrt(dx * dx + dy * dy + dz * dz);
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(CapiDisplayTest, BoxDisplayMeshHasSixFacesAndTwelveTriangles) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
EXPECT_EQ(vde_body_get_face_count(box), 6);
|
||
|
||
VdeDisplayMesh dm{};
|
||
const int rc = vde_body_to_display_mesh(
|
||
ctx, box, 0.002, 1, 10.0,
|
||
VDE_DISPLAY_NORMALS | VDE_DISPLAY_FACE_IDS | VDE_DISPLAY_SURFACE_KINDS,
|
||
&dm);
|
||
ASSERT_EQ(rc, VDE_CAPI_OK) << vde_last_error(ctx);
|
||
EXPECT_EQ(dm.num_triangles, 12u); // 6 planar faces × 2 triangles
|
||
EXPECT_GT(dm.num_vertices, 0u);
|
||
ASSERT_NE(dm.positions, nullptr);
|
||
ASSERT_NE(dm.normals, nullptr);
|
||
ASSERT_NE(dm.indices, nullptr);
|
||
ASSERT_NE(dm.triangle_face_ids, nullptr);
|
||
ASSERT_NE(dm.triangle_surface_kinds, nullptr);
|
||
|
||
for (size_t i = 0; i < dm.num_triangles; ++i) {
|
||
EXPECT_GE(dm.triangle_face_ids[i], 0);
|
||
EXPECT_LT(dm.triangle_face_ids[i], 6);
|
||
EXPECT_EQ(dm.triangle_surface_kinds[i], static_cast<int>(VDE_SURFACE_PLANE));
|
||
}
|
||
// all indices in range + normals unit length
|
||
for (size_t i = 0; i < dm.num_triangles * 3; ++i) {
|
||
EXPECT_LT(dm.indices[i], dm.num_vertices);
|
||
}
|
||
for (size_t i = 0; i < dm.num_vertices; ++i) {
|
||
const double len = std::sqrt(dm.normals[i * 3 + 0] * dm.normals[i * 3 + 0] +
|
||
dm.normals[i * 3 + 1] * dm.normals[i * 3 + 1] +
|
||
dm.normals[i * 3 + 2] * dm.normals[i * 3 + 2]);
|
||
EXPECT_NEAR(len, 1.0, 1e-9);
|
||
EXPECT_LE(std::fabs(dm.positions[i * 3 + 0]), 1.0 + 1e-9);
|
||
EXPECT_LE(std::fabs(dm.positions[i * 3 + 1]), 1.0 + 1e-9);
|
||
EXPECT_LE(std::fabs(dm.positions[i * 3 + 2]), 1.0 + 1e-9);
|
||
}
|
||
free_display(dm);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayTest, BoxFaceAndEdgeInfo) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
ASSERT_EQ(vde_body_get_face_count(box), 6);
|
||
ASSERT_EQ(vde_body_get_edge_count(box), 12);
|
||
|
||
for (int i = 0; i < 6; ++i) {
|
||
VdeFaceInfo fi{};
|
||
ASSERT_EQ(vde_body_get_face_info(box, i, &fi), VDE_CAPI_OK);
|
||
EXPECT_EQ(fi.surface_kind, static_cast<int>(VDE_SURFACE_PLANE));
|
||
EXPECT_GE(fi.bbox[3] - fi.bbox[0], 0.0);
|
||
EXPECT_GE(fi.bbox[4] - fi.bbox[1], 0.0);
|
||
EXPECT_GE(fi.bbox[5] - fi.bbox[2], 0.0);
|
||
}
|
||
for (int i = 0; i < 12; ++i) {
|
||
VdeEdgeInfo ei{};
|
||
ASSERT_EQ(vde_body_get_edge_info(box, i, &ei), VDE_CAPI_OK);
|
||
EXPECT_EQ(ei.kind, static_cast<int>(VDE_EDGE_LINE));
|
||
}
|
||
VdeFaceInfo bad{};
|
||
EXPECT_EQ(vde_body_get_face_info(box, 6, &bad), VDE_CAPI_ERR_OUT_OF_RANGE);
|
||
VdeEdgeInfo bad_edge{};
|
||
EXPECT_EQ(vde_body_get_edge_info(box, 12, &bad_edge), VDE_CAPI_ERR_OUT_OF_RANGE);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayTest, BoxWireframeHasTwelveSegments) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
VdeWireframe wf{};
|
||
const int rc = vde_body_to_wireframe(ctx, box, 0.01, 0, 0, &wf);
|
||
ASSERT_EQ(rc, VDE_CAPI_OK) << vde_last_error(ctx);
|
||
EXPECT_EQ(wf.num_segments, 12u);
|
||
EXPECT_EQ(wf.num_points, 24u);
|
||
ASSERT_NE(wf.positions, nullptr);
|
||
ASSERT_NE(wf.segments, nullptr);
|
||
ASSERT_NE(wf.segment_edge_ids, nullptr);
|
||
for (size_t i = 0; i < wf.num_segments; ++i) {
|
||
const uint32_t a = wf.segments[i * 2 + 0];
|
||
const uint32_t b = wf.segments[i * 2 + 1];
|
||
EXPECT_LT(a, wf.num_points);
|
||
EXPECT_LT(b, wf.num_points);
|
||
EXPECT_NE(a, b);
|
||
EXPECT_NEAR(point_distance(wf.positions, a, b), 2.0, 1e-9);
|
||
}
|
||
free_wireframe(wf);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayTest, CylinderDisplayWireframeAndCircleEdge) {
|
||
VdeHandle ctx = vde_create();
|
||
VdeBodyHandle cyl = vde_body_create_cylinder(ctx, 4.0, 12.0, 0);
|
||
ASSERT_NE(cyl, nullptr);
|
||
EXPECT_GE(vde_body_get_face_count(cyl), 3);
|
||
EXPECT_GT(vde_body_get_edge_count(cyl), 0);
|
||
|
||
bool found_cylinder_face = false;
|
||
for (int i = 0; i < vde_body_get_face_count(cyl); ++i) {
|
||
VdeFaceInfo fi{};
|
||
ASSERT_EQ(vde_body_get_face_info(cyl, i, &fi), VDE_CAPI_OK);
|
||
if (fi.surface_kind == static_cast<int>(VDE_SURFACE_CYLINDER)) found_cylinder_face = true;
|
||
}
|
||
EXPECT_TRUE(found_cylinder_face);
|
||
|
||
int circle_edge = -1;
|
||
for (int i = 0; i < vde_body_get_edge_count(cyl); ++i) {
|
||
VdeEdgeInfo ei{};
|
||
ASSERT_EQ(vde_body_get_edge_info(cyl, i, &ei), VDE_CAPI_OK);
|
||
if (ei.kind == static_cast<int>(VDE_EDGE_CIRCLE)) {
|
||
circle_edge = i;
|
||
EXPECT_NEAR(ei.radius, 4.0, 1e-6);
|
||
}
|
||
}
|
||
ASSERT_GE(circle_edge, 0);
|
||
|
||
// 圆边折线:所有点应在半径 4 的圆上,且弦高 ≤ 容差
|
||
double* pts = nullptr;
|
||
size_t npts = 0;
|
||
const int rc = vde_body_get_edge_polyline(ctx, cyl, circle_edge, 0.01, 0, &pts, &npts);
|
||
ASSERT_EQ(rc, VDE_CAPI_OK) << vde_last_error(ctx);
|
||
ASSERT_NE(pts, nullptr);
|
||
EXPECT_GT(npts, 8u);
|
||
for (size_t i = 0; i < npts; ++i) {
|
||
const double r = std::sqrt(pts[i * 3 + 0] * pts[i * 3 + 0] +
|
||
pts[i * 3 + 1] * pts[i * 3 + 1]);
|
||
EXPECT_NEAR(r, 4.0, 1e-6);
|
||
}
|
||
// 弦高(sagitta)≤ 容差:弦中点距圆周的径向差
|
||
for (size_t i = 0; i + 1 < npts; ++i) {
|
||
const double mx = 0.5 * (pts[i * 3 + 0] + pts[(i + 1) * 3 + 0]);
|
||
const double my = 0.5 * (pts[i * 3 + 1] + pts[(i + 1) * 3 + 1]);
|
||
const double r_mid = std::sqrt(mx * mx + my * my);
|
||
EXPECT_LE(4.0 - r_mid, 0.01 + 1e-9) << "sagitta exceeds chord tolerance at " << i;
|
||
}
|
||
vde_free_buffer(reinterpret_cast<uint8_t*>(pts));
|
||
|
||
VdeDisplayMesh dm{};
|
||
ASSERT_EQ(vde_body_to_display_mesh(ctx, cyl, 0.002, 1, 10.0,
|
||
VDE_DISPLAY_NORMALS | VDE_DISPLAY_FACE_IDS |
|
||
VDE_DISPLAY_SURFACE_KINDS,
|
||
&dm),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_GT(dm.num_triangles, 0u);
|
||
ASSERT_NE(dm.normals, nullptr);
|
||
free_display(dm);
|
||
|
||
VdeWireframe wf{};
|
||
ASSERT_EQ(vde_body_to_wireframe(ctx, cyl, 0.01, 0, 0, &wf), VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_GT(wf.num_segments, 0u);
|
||
free_wireframe(wf);
|
||
vde_body_free(cyl);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayTest, InvalidArgsAndErrorsDoNotCrash) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeDisplayMesh dm{};
|
||
EXPECT_NE(vde_body_to_display_mesh(ctx, nullptr, 0.002, 1, 10.0, 0, &dm),
|
||
VDE_CAPI_OK);
|
||
EXPECT_FALSE(std::string(vde_last_error(ctx)).empty());
|
||
|
||
VdeWireframe wf{};
|
||
EXPECT_NE(vde_body_to_wireframe(ctx, nullptr, 0.01, 0, 0, &wf), VDE_CAPI_OK);
|
||
EXPECT_NE(vde_body_to_wireframe(ctx, nullptr, 0.01, 0, 0, nullptr), VDE_CAPI_OK);
|
||
EXPECT_EQ(vde_body_get_face_count(nullptr), 0);
|
||
EXPECT_EQ(vde_body_get_edge_count(nullptr), 0);
|
||
double* pts = nullptr;
|
||
size_t count = 0;
|
||
EXPECT_NE(vde_body_get_edge_polyline(ctx, nullptr, 0, 0.01, 0, &pts, &count),
|
||
VDE_CAPI_OK);
|
||
EXPECT_EQ(pts, nullptr);
|
||
EXPECT_EQ(count, 0u);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-1235 第二轮:扩展 C API(VdeDisplayMeshEx / 扩展线框 / LOD)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
namespace {
|
||
|
||
void free_display_ex(VdeDisplayMeshEx& dm) {
|
||
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));
|
||
vde_free_buffer(reinterpret_cast<uint8_t*>(dm.triangle_smooth_groups));
|
||
vde_free_buffer(reinterpret_cast<uint8_t*>(dm.failed_faces));
|
||
dm = VdeDisplayMeshEx{};
|
||
}
|
||
|
||
void free_wireframe_ex(VdeWireframeEx& wf) {
|
||
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_info));
|
||
wf = VdeWireframeEx{};
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(CapiDisplayExTest, BoxDisplayMeshExSmoothGroupsAndOpenEdges) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
|
||
VdeDisplayMeshEx dm{};
|
||
const int flags = VDE_DISPLAY_NORMALS | VDE_DISPLAY_FACE_IDS |
|
||
VDE_DISPLAY_SURFACE_KINDS | VDE_DISPLAY_SMOOTH_GROUPS;
|
||
ASSERT_EQ(vde_body_to_display_mesh_ex(ctx, box, 0.002, 1, 10.0, flags, &dm),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_EQ(dm.num_triangles, 12u);
|
||
EXPECT_EQ(dm.num_failed_faces, 0u);
|
||
EXPECT_EQ(dm.failed_faces, nullptr);
|
||
EXPECT_EQ(dm.open_edge_count, 0u); // 封闭 box 无开口边
|
||
EXPECT_DOUBLE_EQ(dm.open_edge_length, 0.0);
|
||
ASSERT_NE(dm.positions, nullptr);
|
||
ASSERT_NE(dm.normals, nullptr);
|
||
ASSERT_NE(dm.indices, nullptr);
|
||
ASSERT_NE(dm.triangle_face_ids, nullptr);
|
||
ASSERT_NE(dm.triangle_surface_kinds, nullptr);
|
||
ASSERT_NE(dm.triangle_smooth_groups, nullptr);
|
||
|
||
std::set<int32_t> groups;
|
||
for (size_t i = 0; i < dm.num_triangles; ++i) {
|
||
groups.insert(dm.triangle_smooth_groups[i]);
|
||
EXPECT_GE(dm.triangle_face_ids[i], 0);
|
||
EXPECT_LT(dm.triangle_face_ids[i], 6);
|
||
EXPECT_EQ(dm.triangle_surface_kinds[i],
|
||
static_cast<int>(VDE_SURFACE_PLANE));
|
||
}
|
||
EXPECT_EQ(groups.size(), 6u); // 6 个平面面 = 6 个独立平滑组
|
||
for (size_t i = 0; i < dm.num_triangles * 3; ++i)
|
||
EXPECT_LT(dm.indices[i], dm.num_vertices);
|
||
free_display_ex(dm);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayExTest, LodQualityOrdering) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle sphere = vde_body_create_sphere(ctx, 1.0, 16, 8);
|
||
ASSERT_NE(sphere, nullptr);
|
||
|
||
const int flags = VDE_DISPLAY_NORMALS | VDE_DISPLAY_SMOOTH_GROUPS;
|
||
VdeDisplayMeshEx draft{};
|
||
VdeDisplayMeshEx normal{};
|
||
VdeDisplayMeshEx high{};
|
||
ASSERT_EQ(vde_body_to_display_mesh_lod(ctx, sphere, VDE_DISPLAY_QUALITY_DRAFT,
|
||
flags, &draft),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
ASSERT_EQ(vde_body_to_display_mesh_lod(ctx, sphere,
|
||
VDE_DISPLAY_QUALITY_NORMAL, flags,
|
||
&normal),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
ASSERT_EQ(vde_body_to_display_mesh_lod(ctx, sphere, VDE_DISPLAY_QUALITY_HIGH,
|
||
flags, &high),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_LT(draft.num_triangles, normal.num_triangles);
|
||
EXPECT_LT(normal.num_triangles, high.num_triangles);
|
||
ASSERT_NE(normal.triangle_smooth_groups, nullptr);
|
||
|
||
VdeDisplayMeshEx bad{};
|
||
EXPECT_EQ(vde_body_to_display_mesh_lod(ctx, sphere, 9, flags, &bad),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_FALSE(std::string(vde_last_error(ctx)).empty());
|
||
free_display_ex(draft);
|
||
free_display_ex(normal);
|
||
free_display_ex(high);
|
||
free_display_ex(bad);
|
||
vde_body_free(sphere);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayExTest, WireframeExBoxAllCrease) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
VdeWireframeEx wf{};
|
||
ASSERT_EQ(vde_body_to_wireframe_ex(ctx, box, 0.01, 0, 1, 2.0, &wf),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_EQ(wf.num_segments, 12u);
|
||
ASSERT_NE(wf.positions, nullptr);
|
||
ASSERT_NE(wf.segments, nullptr);
|
||
ASSERT_NE(wf.segment_info, nullptr);
|
||
for (size_t i = 0; i < wf.num_segments; ++i) {
|
||
EXPECT_EQ(wf.segment_info[i].cls,
|
||
static_cast<int>(VDE_WIREFRAME_CREASE));
|
||
EXPECT_NEAR(wf.segment_info[i].dihedral_deg, 90.0, 1e-6);
|
||
EXPECT_GE(wf.segment_info[i].edge_id, 0);
|
||
EXPECT_LT(wf.segment_info[i].edge_id, 12);
|
||
EXPECT_GE(wf.segment_info[i].adjacent_faces[0], 0);
|
||
EXPECT_GE(wf.segment_info[i].adjacent_faces[1], 0);
|
||
EXPECT_NE(wf.segment_info[i].adjacent_faces[0],
|
||
wf.segment_info[i].adjacent_faces[1]);
|
||
EXPECT_LT(wf.segments[i * 2 + 0], wf.num_points);
|
||
EXPECT_LT(wf.segments[i * 2 + 1], wf.num_points);
|
||
EXPECT_NE(wf.segments[i * 2 + 0], wf.segments[i * 2 + 1]);
|
||
}
|
||
free_wireframe_ex(wf);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayExTest, WireframeExCylinderSeamTangent) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle cyl = vde_body_create_cylinder(ctx, 1.0, 2.0, 0);
|
||
ASSERT_NE(cyl, nullptr);
|
||
VdeWireframeEx wf{};
|
||
ASSERT_EQ(vde_body_to_wireframe_ex(ctx, cyl, 1e-3, 0, 1, 2.0, &wf),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
ASSERT_GT(wf.num_segments, 0u);
|
||
int tangent = 0;
|
||
int crease = 0;
|
||
for (size_t i = 0; i < wf.num_segments; ++i) {
|
||
const int cls = wf.segment_info[i].cls;
|
||
if (cls == static_cast<int>(VDE_WIREFRAME_TANGENT)) {
|
||
++tangent;
|
||
EXPECT_LT(wf.segment_info[i].dihedral_deg, 1e-2);
|
||
} else if (cls == static_cast<int>(VDE_WIREFRAME_CREASE)) {
|
||
++crease;
|
||
}
|
||
}
|
||
EXPECT_GE(tangent, 1); // 解析圆柱 90° 分片侧缝
|
||
EXPECT_GE(crease, 1); // 侧壁-盖面环边
|
||
free_wireframe_ex(wf);
|
||
vde_body_free(cyl);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayExTest, AllowPartialReportsFailedFaces) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
// 白盒注入损坏拓扑:引用不存在的环 id(真实损坏 B-Rep 场景)
|
||
auto* body = reinterpret_cast<vde::brep::BrepModel*>(box);
|
||
const int bad_face = body->add_face(0, {99999});
|
||
ASSERT_EQ(bad_face, 6);
|
||
|
||
VdeDisplayMeshEx dm{};
|
||
const int flags = VDE_DISPLAY_FACE_IDS | VDE_DISPLAY_ALLOW_PARTIAL;
|
||
ASSERT_EQ(vde_body_to_display_mesh_ex(ctx, box, 0.002, 1, 10.0, flags, &dm),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_EQ(dm.num_triangles, 12u);
|
||
EXPECT_EQ(dm.num_failed_faces, 1u);
|
||
ASSERT_NE(dm.failed_faces, nullptr);
|
||
EXPECT_EQ(dm.failed_faces[0], bad_face);
|
||
free_display_ex(dm);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayExTest, InvalidArgsReturnErrorCodes) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
|
||
VdeDisplayMeshEx dm{};
|
||
EXPECT_EQ(vde_body_to_display_mesh_ex(ctx, nullptr, 0.002, 1, 10.0, 0, &dm),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_EQ(vde_body_to_display_mesh_ex(nullptr, box, 0.002, 1, 10.0, 0, &dm),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_EQ(vde_body_to_display_mesh_ex(ctx, box, 0.002, 1, 10.0, 0, nullptr),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_EQ(vde_body_to_display_mesh_lod(ctx, nullptr, 1, 0, &dm),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
|
||
VdeWireframeEx wf{};
|
||
EXPECT_EQ(vde_body_to_wireframe_ex(ctx, nullptr, 0.01, 0, 0, 2.0, &wf),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_EQ(vde_body_to_wireframe_ex(ctx, box, 0.01, 0, 0, 2.0, nullptr),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_FALSE(std::string(vde_last_error(ctx)).empty());
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// R3 补全:VdeDisplayOptions / VdeWireframeOptions(opt 入口)
|
||
// 覆盖:LOD 档位排序、opt=NULL 与旧 ABI 几何一致、open_edge 焊接容差、
|
||
// wireframe merge_tolerance、相对弦高、错误参数错误码。
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
namespace {
|
||
|
||
VdeDisplayOptions make_display_opt(int quality, size_t expected_faces, int flags,
|
||
double open_edge_weld_tolerance = -1.0) {
|
||
VdeDisplayOptions o{};
|
||
o.chord_tolerance = 0.002;
|
||
o.relative_to_bbox = 1;
|
||
o.angle_tolerance_deg = 10.0;
|
||
o.flags = flags;
|
||
o.quality = quality;
|
||
o.expected_faces = expected_faces;
|
||
o.open_edge_weld_tolerance = open_edge_weld_tolerance;
|
||
return o;
|
||
}
|
||
|
||
VdeWireframeOptions make_wireframe_opt(double chord_tolerance, int relative_to_bbox,
|
||
int include_hidden, double tangent_angle_deg,
|
||
double merge_tolerance) {
|
||
VdeWireframeOptions o{};
|
||
o.chord_tolerance = chord_tolerance;
|
||
o.relative_to_bbox = relative_to_bbox;
|
||
o.include_hidden = include_hidden;
|
||
o.tangent_angle_deg = tangent_angle_deg;
|
||
o.merge_tolerance = merge_tolerance;
|
||
return o;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(CapiDisplayOptTest, DisplayOptQualityOrderingAndExpectedFaces) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle sphere = vde_body_create_sphere(ctx, 1.0, 16, 8);
|
||
ASSERT_NE(sphere, nullptr);
|
||
const int flags = VDE_DISPLAY_NORMALS | VDE_DISPLAY_SMOOTH_GROUPS;
|
||
|
||
auto build = [&](int quality, size_t expected, VdeDisplayMeshEx& out) {
|
||
const VdeDisplayOptions o =
|
||
make_display_opt(quality, expected, flags, -1.0);
|
||
return vde_body_to_display_mesh_opt(ctx, sphere, &o, &out);
|
||
};
|
||
VdeDisplayMeshEx draft{};
|
||
VdeDisplayMeshEx normal{};
|
||
VdeDisplayMeshEx high{};
|
||
ASSERT_EQ(build(0, 0, draft), VDE_CAPI_OK) << vde_last_error(ctx);
|
||
ASSERT_EQ(build(1, 0, normal), VDE_CAPI_OK) << vde_last_error(ctx);
|
||
ASSERT_EQ(build(2, 0, high), VDE_CAPI_OK) << vde_last_error(ctx);
|
||
EXPECT_GT(draft.num_triangles, 0u);
|
||
EXPECT_LT(draft.num_triangles, normal.num_triangles);
|
||
EXPECT_LT(normal.num_triangles, high.num_triangles);
|
||
|
||
// expected_faces=0 的档位入口与既有 LOD C API 同口径(均按 body 面数预算)
|
||
VdeDisplayMeshEx lod{};
|
||
ASSERT_EQ(vde_body_to_display_mesh_lod(ctx, sphere, VDE_DISPLAY_QUALITY_NORMAL,
|
||
flags, &lod),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_EQ(normal.num_triangles, lod.num_triangles);
|
||
// expected_faces 作为预算依据被接受且不劣化(<= 明细预算)
|
||
VdeDisplayMeshEx tiny{};
|
||
ASSERT_EQ(build(2, 1, tiny), VDE_CAPI_OK) << vde_last_error(ctx);
|
||
EXPECT_LE(tiny.num_triangles, high.num_triangles);
|
||
|
||
// 非法 quality
|
||
VdeDisplayMeshEx bad{};
|
||
const VdeDisplayOptions bo = make_display_opt(3, 0, flags);
|
||
EXPECT_EQ(vde_body_to_display_mesh_opt(ctx, sphere, &bo, &bad),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_FALSE(std::string(vde_last_error(ctx)).empty());
|
||
std::printf("[R3] display_opt quality: draft=%zu normal=%zu high=%zu "
|
||
"(lod normal=%zu) tris\n",
|
||
draft.num_triangles, normal.num_triangles, high.num_triangles,
|
||
lod.num_triangles);
|
||
free_display_ex(draft);
|
||
free_display_ex(normal);
|
||
free_display_ex(high);
|
||
free_display_ex(lod);
|
||
free_display_ex(tiny);
|
||
free_display_ex(bad);
|
||
vde_body_free(sphere);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayOptTest, DisplayOptNullMatchesLegacyGeometry) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
|
||
// 旧 ABI(flags=0、chord=0.002 相对、angle=10°)
|
||
VdeDisplayMesh legacy{};
|
||
ASSERT_EQ(vde_body_to_display_mesh(ctx, box, 0.002, 1, 10.0, 0, &legacy),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
// opt=NULL → 文档化默认值,应与旧函数几何逐位一致
|
||
VdeDisplayMeshEx opt{};
|
||
ASSERT_EQ(vde_body_to_display_mesh_opt(ctx, box, nullptr, &opt), VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_EQ(opt.num_vertices, legacy.num_vertices);
|
||
EXPECT_EQ(opt.num_triangles, legacy.num_triangles);
|
||
ASSERT_NE(opt.positions, nullptr);
|
||
ASSERT_NE(legacy.positions, nullptr);
|
||
for (size_t i = 0; i < legacy.num_vertices * 3; ++i)
|
||
EXPECT_EQ(opt.positions[i], legacy.positions[i]) << "vertex component " << i;
|
||
ASSERT_NE(opt.indices, nullptr);
|
||
ASSERT_NE(legacy.indices, nullptr);
|
||
for (size_t i = 0; i < legacy.num_triangles * 3; ++i)
|
||
EXPECT_EQ(opt.indices[i], legacy.indices[i]) << "index " << i;
|
||
EXPECT_EQ(opt.normals, nullptr); // flags=0 ⇒ 不输出法线
|
||
EXPECT_EQ(opt.failed_faces, nullptr);
|
||
EXPECT_EQ(opt.num_failed_faces, 0u);
|
||
std::printf("[R3] display_opt(NULL) == legacy: %zu verts / %zu tris (bitwise)\n",
|
||
opt.num_vertices, opt.num_triangles);
|
||
free_display_ex(opt);
|
||
free_display(legacy);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayOptTest, DisplayOptOpenEdgeWeldTolerance) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
|
||
// 白盒注入一个与 box 分离的孤立四边形面(带裁剪域 ⇒ 恰好 2 三角形、4 条
|
||
// 只属于一个三角形的边界边)→ 开口边诊断应当报告 4 条。
|
||
auto* body = reinterpret_cast<vde::brep::BrepModel*>(box);
|
||
const vde::curves::NurbsSurface quad_surf = vde::curves::NurbsSurface::plane(
|
||
vde::core::Point3D(5.0, 0.0, 0.0), vde::core::Vector3D(1.0, 0.0, 0.0),
|
||
vde::core::Vector3D(0.0, 1.0, 0.0), 2.0, 2.0);
|
||
const int sid = body->add_surface(quad_surf);
|
||
const int q0 = body->add_vertex(vde::core::Point3D(5.0, 0.0, 0.0));
|
||
const int q1 = body->add_vertex(vde::core::Point3D(7.0, 0.0, 0.0));
|
||
const int q2 = body->add_vertex(vde::core::Point3D(7.0, 2.0, 0.0));
|
||
const int q3 = body->add_vertex(vde::core::Point3D(5.0, 2.0, 0.0));
|
||
const int qe0 = body->add_edge(q0, q1);
|
||
const int qe1 = body->add_edge(q1, q2);
|
||
const int qe2 = body->add_edge(q2, q3);
|
||
const int qe3 = body->add_edge(q3, q0);
|
||
const int qlp = body->add_loop({qe0, qe1, qe2, qe3}, true);
|
||
const int qfid = body->add_face(sid, {qlp});
|
||
ASSERT_EQ(qfid, 6);
|
||
body->set_face_trimmed_surface(
|
||
qfid, body->add_trimmed_surface(
|
||
vde::brep::TrimmedSurface::from_rect(quad_surf, 0.0, 1.0, 0.0, 1.0)));
|
||
|
||
auto run = [&](double weld) {
|
||
const VdeDisplayOptions o = make_display_opt(-1, 0, 0, weld);
|
||
VdeDisplayMeshEx dm{};
|
||
EXPECT_EQ(vde_body_to_display_mesh_opt(ctx, box, &o, &dm), VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
return dm;
|
||
};
|
||
VdeDisplayMeshEx def = run(-1.0); // 默认焊接容差 → 开口边诊断开启
|
||
EXPECT_EQ(def.num_triangles, 14u); // box 12 + 孤立面 2
|
||
EXPECT_EQ(def.open_edge_count, 4u);
|
||
EXPECT_GT(def.open_edge_length, 0.0);
|
||
|
||
VdeDisplayMeshEx off = run(0.0); // 0 = 关闭诊断
|
||
EXPECT_EQ(off.open_edge_count, 0u);
|
||
EXPECT_DOUBLE_EQ(off.open_edge_length, 0.0);
|
||
|
||
VdeDisplayMeshEx explicitv = run(1e-3); // 显式容差(相对 bbox diag)
|
||
EXPECT_EQ(explicitv.open_edge_count, 4u);
|
||
std::printf("[R3] display_opt open_edge: default=%zu off=%zu explicit=%zu\n",
|
||
def.open_edge_count, off.open_edge_count, explicitv.open_edge_count);
|
||
free_display_ex(def);
|
||
free_display_ex(off);
|
||
free_display_ex(explicitv);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
|
||
TEST(CapiDisplayOptTest, NoStitchTjunctionsFlagIsAccepted) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
VdeDisplayMeshEx dm{};
|
||
const int flags = VDE_DISPLAY_NORMALS | VDE_DISPLAY_NO_STITCH_TJ;
|
||
ASSERT_EQ(vde_body_to_display_mesh_ex(ctx, box, 0.002, 1, 10.0, flags, &dm),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_EQ(dm.num_triangles, 12u);
|
||
EXPECT_EQ(dm.open_edge_count, 0u);
|
||
free_display_ex(dm);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayOptTest, WireframeOptMergeTolerance) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
|
||
// 白盒:添加一条与边 0 完全重合的重复边(同端点、同中点)
|
||
auto* body = reinterpret_cast<vde::brep::BrepModel*>(box);
|
||
const int vs = body->edge(0).v_start;
|
||
const int ve = body->edge(0).v_end;
|
||
const int dupe = body->add_edge(vs, ve);
|
||
ASSERT_EQ(dupe, 12);
|
||
ASSERT_EQ(vde_body_get_edge_count(box), 13);
|
||
|
||
auto run = [&](double merge) {
|
||
const VdeWireframeOptions o = make_wireframe_opt(0.01, 0, 1, 2.0, merge);
|
||
VdeWireframeEx wf{};
|
||
EXPECT_EQ(vde_body_to_wireframe_opt(ctx, box, &o, &wf), VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
return wf;
|
||
};
|
||
VdeWireframeEx off = run(0.0); // 0 = 关闭去重/过滤
|
||
EXPECT_EQ(off.num_segments, 13u); // 12 原始边 + 1 重复边
|
||
VdeWireframeEx auto_dedup = run(-1.0); // <0 = 自动(bbox diag × 1e-6)
|
||
EXPECT_EQ(auto_dedup.num_segments, 12u);
|
||
VdeWireframeEx no_opt{};
|
||
ASSERT_EQ(vde_body_to_wireframe_opt(ctx, box, nullptr, &no_opt), VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_EQ(no_opt.num_segments, auto_dedup.num_segments);
|
||
// 与既有 Ex 函数(内部固定自动去重)一致
|
||
VdeWireframeEx ex{};
|
||
ASSERT_EQ(vde_body_to_wireframe_ex(ctx, box, 0.01, 0, 1, 2.0, &ex),
|
||
VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_EQ(ex.num_segments, auto_dedup.num_segments);
|
||
std::printf("[R3] wireframe_opt merge: off=%zu auto=%zu\n",
|
||
off.num_segments, auto_dedup.num_segments);
|
||
free_wireframe_ex(off);
|
||
free_wireframe_ex(auto_dedup);
|
||
free_wireframe_ex(no_opt);
|
||
free_wireframe_ex(ex);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayOptTest, WireframeOptRelativeChordAndHidden) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle cyl = vde_body_create_cylinder(ctx, 4.0, 12.0, 0);
|
||
ASSERT_NE(cyl, nullptr);
|
||
|
||
auto run = [&](double chord, int relative, int hidden) {
|
||
const VdeWireframeOptions o =
|
||
make_wireframe_opt(chord, relative, hidden, 2.0, -1.0);
|
||
VdeWireframeEx wf{};
|
||
EXPECT_EQ(vde_body_to_wireframe_opt(ctx, cyl, &o, &wf), VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
return wf;
|
||
};
|
||
// 相对弦高 0.1 = 0.1×bbox diag(>1 模型单位)⇒ 比绝对 0.1 采样更稀
|
||
VdeWireframeEx rel = run(0.1, 1, 0);
|
||
VdeWireframeEx abs = run(0.1, 0, 0);
|
||
EXPECT_GT(rel.num_segments, 0u);
|
||
EXPECT_LT(rel.num_segments, abs.num_segments);
|
||
// include_hidden 在解析圆柱上(面全部可见)不改变段数
|
||
VdeWireframeEx hidden = run(0.01, 0, 1);
|
||
VdeWireframeEx visible = run(0.01, 0, 0);
|
||
EXPECT_EQ(hidden.num_segments, visible.num_segments);
|
||
std::printf("[R3] wireframe_opt chord: rel=%zu abs=%zu hidden=%zu visible=%zu\n",
|
||
rel.num_segments, abs.num_segments, hidden.num_segments,
|
||
visible.num_segments);
|
||
free_wireframe_ex(rel);
|
||
free_wireframe_ex(abs);
|
||
free_wireframe_ex(hidden);
|
||
free_wireframe_ex(visible);
|
||
vde_body_free(cyl);
|
||
vde_destroy(ctx);
|
||
}
|
||
|
||
TEST(CapiDisplayOptTest, OptInvalidArgsAndErrorCodes) {
|
||
VdeHandle ctx = vde_create();
|
||
ASSERT_NE(ctx, nullptr);
|
||
VdeBodyHandle box = vde_body_create_box(ctx, 2.0, 2.0, 2.0);
|
||
ASSERT_NE(box, nullptr);
|
||
|
||
VdeDisplayMeshEx dm{};
|
||
const VdeDisplayOptions d = make_display_opt(-1, 0, 0);
|
||
EXPECT_EQ(vde_body_to_display_mesh_opt(ctx, nullptr, &d, &dm),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_EQ(vde_body_to_display_mesh_opt(nullptr, box, &d, &dm),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_EQ(vde_body_to_display_mesh_opt(ctx, box, &d, nullptr),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_FALSE(std::string(vde_last_error(ctx)).empty());
|
||
|
||
VdeWireframeEx wf{};
|
||
const VdeWireframeOptions w = make_wireframe_opt(0.01, 0, 0, 2.0, -1.0);
|
||
EXPECT_EQ(vde_body_to_wireframe_opt(ctx, nullptr, &w, &wf),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
EXPECT_EQ(vde_body_to_wireframe_opt(ctx, box, &w, nullptr),
|
||
VDE_CAPI_ERR_INVALID_ARGUMENT);
|
||
|
||
// 全零字段(chord/angle/merge 均为 0):走默认值/关闭语义,不应报错崩溃
|
||
VdeDisplayOptions dz{};
|
||
dz.quality = -1;
|
||
dz.open_edge_weld_tolerance = -1.0;
|
||
VdeDisplayMeshEx dmz{};
|
||
ASSERT_EQ(vde_body_to_display_mesh_opt(ctx, box, &dz, &dmz), VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_GT(dmz.num_triangles, 0u);
|
||
VdeWireframeOptions wz{};
|
||
VdeWireframeEx wfz{};
|
||
ASSERT_EQ(vde_body_to_wireframe_opt(ctx, box, &wz, &wfz), VDE_CAPI_OK)
|
||
<< vde_last_error(ctx);
|
||
EXPECT_GT(wfz.num_segments, 0u);
|
||
// 新数组仍由 vde_free_buffer 释放(helper 内部逐个释放,无泄漏路径)
|
||
free_display_ex(dmz);
|
||
free_wireframe_ex(wfz);
|
||
vde_body_free(box);
|
||
vde_destroy(ctx);
|
||
}
|
||
|