527 lines
22 KiB
C++
527 lines
22 KiB
C++
// Phase6-Display:显示级 API(DisplayMesh / Wireframe / B-Rep 信息)测试。
|
||
#include <gtest/gtest.h>
|
||
|
||
#include "vde/brep/brep.h"
|
||
#include "vde/brep/modeling.h"
|
||
#include "vde/mesh/display_mesh.h"
|
||
|
||
#include <algorithm>
|
||
#include <array>
|
||
#include <cmath>
|
||
#include <map>
|
||
#include <set>
|
||
#include <vector>
|
||
|
||
using namespace vde;
|
||
using namespace vde::brep;
|
||
using namespace vde::mesh;
|
||
using namespace vde::core;
|
||
|
||
namespace {
|
||
|
||
DisplayTessOptions tiny_options(int threads = 0) {
|
||
DisplayTessOptions o;
|
||
o.chord_tolerance = 0.002;
|
||
o.angle_tolerance_deg = 10.0;
|
||
o.num_threads = threads;
|
||
return o;
|
||
}
|
||
|
||
std::map<int32_t, size_t> triangles_per_face(const DisplayMesh& mesh) {
|
||
std::map<int32_t, size_t> counts;
|
||
for (int32_t fid : mesh.triangle_face_id) counts[fid]++;
|
||
return counts;
|
||
}
|
||
|
||
size_t count_face_type(const DisplayMesh& mesh, FaceType type) {
|
||
size_t n = 0;
|
||
const int32_t want = static_cast<int32_t>(type);
|
||
for (int32_t kind : mesh.triangle_surface_kind)
|
||
if (kind == want) ++n;
|
||
return n;
|
||
}
|
||
|
||
double tri_area(const Point3D& a, const Point3D& b, const Point3D& c) {
|
||
return 0.5 * (b - a).cross(c - a).norm();
|
||
}
|
||
|
||
} // namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// DisplayMesh
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(DisplayMeshTest, BoxPlanarFacesTwoTrianglesExactNormals) {
|
||
auto box = make_box(2.0, 2.0, 2.0);
|
||
auto mesh = build_display_mesh(box, tiny_options(4));
|
||
ASSERT_FALSE(mesh.indices.empty());
|
||
EXPECT_TRUE(mesh.diagnostic.empty());
|
||
|
||
ASSERT_EQ(mesh.normals.size(), mesh.positions.size());
|
||
for (const auto& n : mesh.normals) EXPECT_NEAR(n.norm(), 1.0, 1e-12);
|
||
|
||
// 6 个平面面,每面恰好 2 个三角形(扇形三角化)
|
||
EXPECT_EQ(mesh.num_triangles(), 12u);
|
||
const auto per_face = triangles_per_face(mesh);
|
||
ASSERT_EQ(per_face.size(), 6u);
|
||
for (const auto& [fid, cnt] : per_face) {
|
||
EXPECT_EQ(cnt, 2u) << "face " << fid;
|
||
}
|
||
EXPECT_EQ(count_face_type(mesh, FaceType::Plane), 12u);
|
||
|
||
// 法线精确沿坐标轴,且三角形朝向与法线一致
|
||
for (size_t ti = 0; ti < mesh.num_triangles(); ++ti) {
|
||
const auto& idx = mesh.indices[ti];
|
||
const Point3D a = mesh.positions[idx[0]];
|
||
const Point3D b = mesh.positions[idx[1]];
|
||
const Point3D c = mesh.positions[idx[2]];
|
||
const Vector3D fn = (b - a).cross(c - a).normalized();
|
||
for (int k = 0; k < 3; ++k) {
|
||
const auto& n = mesh.normals[idx[static_cast<size_t>(k)]];
|
||
EXPECT_GT(fn.dot(n), 0.999999);
|
||
const double ax = std::abs(n.x()), ay = std::abs(n.y()), az = std::abs(n.z());
|
||
EXPECT_NEAR(ax + ay + az, 1.0, 1e-12);
|
||
}
|
||
EXPECT_GT(tri_area(a, b, c), 0.0);
|
||
}
|
||
EXPECT_EQ(mesh.triangle_face_id.size(), mesh.num_triangles());
|
||
EXPECT_EQ(mesh.triangle_surface_kind.size(), mesh.num_triangles());
|
||
}
|
||
|
||
TEST(DisplayMeshTest, CylinderSideSmoothNormalsCapsPlanar) {
|
||
auto cyl = make_cylinder(1.0, 2.0, 0); // 精确解析圆柱:1 侧面 + 2 平面盖
|
||
auto mesh = build_display_mesh(cyl, tiny_options(8));
|
||
ASSERT_FALSE(mesh.indices.empty());
|
||
EXPECT_TRUE(mesh.diagnostic.empty());
|
||
EXPECT_GT(count_face_type(mesh, FaceType::Cylinder), 0u);
|
||
EXPECT_GT(count_face_type(mesh, FaceType::Plane), 0u);
|
||
|
||
// 仅检查属于圆柱侧面三角形的顶点(排除盖面边界顶点)
|
||
std::set<std::uint32_t> side_vertices;
|
||
for (size_t ti = 0; ti < mesh.num_triangles(); ++ti) {
|
||
if (mesh.triangle_surface_kind[ti] != static_cast<int32_t>(FaceType::Cylinder))
|
||
continue;
|
||
for (int k = 0; k < 3; ++k) side_vertices.insert(mesh.indices[ti][static_cast<size_t>(k)]);
|
||
}
|
||
EXPECT_GT(side_vertices.size(), 0u);
|
||
for (std::uint32_t vi : side_vertices) {
|
||
const auto& p = mesh.positions[vi];
|
||
const double r = std::hypot(p.x(), p.y());
|
||
EXPECT_NEAR(r, 1.0, 1e-9) << "v" << vi;
|
||
const Vector3D radial(p.x(), p.y(), 0.0);
|
||
EXPECT_GT(mesh.normals[vi].dot(radial.normalized()), 0.99) << "v" << vi;
|
||
}
|
||
|
||
for (size_t ti = 0; ti < mesh.num_triangles(); ++ti) {
|
||
if (mesh.triangle_surface_kind[ti] != static_cast<int32_t>(FaceType::Plane))
|
||
continue;
|
||
for (int k = 0; k < 3; ++k) {
|
||
const auto& n = mesh.normals[mesh.indices[ti][static_cast<size_t>(k)]];
|
||
EXPECT_NEAR(std::abs(n.z()), 1.0, 1e-12);
|
||
}
|
||
}
|
||
|
||
// 侧面三角形质心到解析圆柱面距离 ≤ 2×绝对弦高
|
||
const double diag = (cyl.bounds().max() - cyl.bounds().min()).norm();
|
||
const double defl = 0.002 * diag;
|
||
double max_dev = 0.0;
|
||
for (size_t ti = 0; ti < mesh.num_triangles(); ++ti) {
|
||
if (mesh.triangle_surface_kind[ti] != static_cast<int32_t>(FaceType::Cylinder))
|
||
continue;
|
||
const int fid = mesh.triangle_face_id[ti];
|
||
const int sid = cyl.face(fid).surface_id;
|
||
const Point3D a = mesh.positions[mesh.indices[ti][0]];
|
||
const Point3D b = mesh.positions[mesh.indices[ti][1]];
|
||
const Point3D c = mesh.positions[mesh.indices[ti][2]];
|
||
const Point3D centroid = (a + b + c) / 3.0;
|
||
max_dev = std::max(max_dev, cyl.surface(sid).distance_to(centroid));
|
||
}
|
||
EXPECT_LE(max_dev, 2.0 * defl) << "cylinder chord dev=" << max_dev;
|
||
}
|
||
|
||
TEST(DisplayMeshTest, SphereVerticesOnSurfaceSmoothNormalsChordTol) {
|
||
auto sphere = make_sphere(1.0, 24, 12);
|
||
auto mesh = build_display_mesh(sphere, tiny_options(8));
|
||
ASSERT_FALSE(mesh.indices.empty());
|
||
EXPECT_TRUE(mesh.diagnostic.empty());
|
||
|
||
const double diag = (sphere.bounds().max() - sphere.bounds().min()).norm();
|
||
const double defl = 0.002 * diag;
|
||
double max_centroid_dev = 0.0;
|
||
for (size_t ti = 0; ti < mesh.num_triangles(); ++ti) {
|
||
const Point3D a = mesh.positions[mesh.indices[ti][0]];
|
||
const Point3D b = mesh.positions[mesh.indices[ti][1]];
|
||
const Point3D c = mesh.positions[mesh.indices[ti][2]];
|
||
const Point3D centroid = (a + b + c) / 3.0;
|
||
max_centroid_dev = std::max(max_centroid_dev,
|
||
std::abs(centroid.norm() - 1.0));
|
||
}
|
||
EXPECT_LE(max_centroid_dev, 2.0 * defl) << "sphere dev=" << max_centroid_dev;
|
||
for (size_t vi = 0; vi < mesh.num_vertices(); ++vi) {
|
||
const auto& p = mesh.positions[vi];
|
||
EXPECT_NEAR(p.norm(), 1.0, 1e-9);
|
||
EXPECT_GT(mesh.normals[vi].dot(p.normalized()), 0.99);
|
||
}
|
||
}
|
||
|
||
TEST(DisplayMeshTest, TorusDisplaySmooth) {
|
||
auto torus = make_torus_exact(3.0, 1.0);
|
||
auto mesh = build_display_mesh(torus, tiny_options(8));
|
||
ASSERT_FALSE(mesh.indices.empty());
|
||
EXPECT_TRUE(mesh.diagnostic.empty());
|
||
const double R = 3.0, r = 1.0;
|
||
for (size_t vi = 0; vi < mesh.num_vertices(); ++vi) {
|
||
const auto& p = mesh.positions[vi];
|
||
const double rho = std::hypot(p.x(), p.y());
|
||
const double d = std::hypot(rho - R, p.z());
|
||
EXPECT_NEAR(d, r, 1e-9);
|
||
EXPECT_NEAR(mesh.normals[vi].norm(), 1.0, 1e-9);
|
||
}
|
||
}
|
||
|
||
TEST(DisplayMeshTest, TrimmedHoleFaceIdsAndNormals) {
|
||
BrepModel box = make_box(4.0, 4.0, 2.0);
|
||
HoleSpec spec;
|
||
spec.diameter = 1.0;
|
||
spec.depth = 0.0;
|
||
BrepModel holed = make_hole(box, Point3D(0, 0, 1), Vector3D(0, 0, -1), spec);
|
||
ASSERT_GT(holed.num_faces(), 0u);
|
||
auto mesh = build_display_mesh(holed, tiny_options(4));
|
||
ASSERT_FALSE(mesh.indices.empty());
|
||
EXPECT_TRUE(mesh.diagnostic.empty());
|
||
const auto per_face = triangles_per_face(mesh);
|
||
EXPECT_EQ(per_face.size(), holed.num_faces());
|
||
bool saw_holed_face = false;
|
||
for (const auto& [fid, cnt] : per_face) {
|
||
if (holed.face_type(fid) == FaceType::Plane && cnt > 2) saw_holed_face = true;
|
||
}
|
||
EXPECT_TRUE(saw_holed_face);
|
||
for (size_t ti = 0; ti < mesh.num_triangles(); ++ti) {
|
||
if (mesh.triangle_surface_kind[ti] != static_cast<int32_t>(FaceType::Plane))
|
||
continue;
|
||
const int fid = mesh.triangle_face_id[ti];
|
||
const Vector3D fn = holed.face_normal(fid).normalized();
|
||
for (int k = 0; k < 3; ++k) {
|
||
const Vector3D n = mesh.normals[mesh.indices[ti][static_cast<size_t>(k)]];
|
||
EXPECT_NEAR(n.norm(), 1.0, 1e-12);
|
||
EXPECT_GT(n.dot(fn), 0.999999);
|
||
}
|
||
}
|
||
// 通孔上下盖(z=±1)法线必须严格沿 Z
|
||
const auto caps = [&] {
|
||
int n = 0;
|
||
for (size_t fi = 0; fi < holed.num_faces(); ++fi)
|
||
if (holed.face_type(static_cast<int>(fi)) == FaceType::Plane &&
|
||
std::abs(std::abs(holed.face_normal(static_cast<int>(fi)).z()) - 1.0) < 1e-9)
|
||
++n;
|
||
return n;
|
||
}();
|
||
EXPECT_EQ(caps, 2);
|
||
}
|
||
|
||
TEST(DisplayMeshTest, VisibilityRespectedAndEmptyModel) {
|
||
auto box = make_box(1.0, 1.0, 1.0);
|
||
box.set_face_visibility(3, false);
|
||
DisplayTessOptions respect;
|
||
respect.num_threads = 2;
|
||
auto vis_mesh = build_display_mesh(box, respect);
|
||
EXPECT_FALSE(vis_mesh.indices.empty());
|
||
const auto per_face = triangles_per_face(vis_mesh);
|
||
EXPECT_EQ(per_face.count(3), 0u);
|
||
|
||
DisplayTessOptions ignore = respect;
|
||
ignore.respect_face_visibility = false;
|
||
auto all_mesh = build_display_mesh(box, ignore);
|
||
const auto all_per_face = triangles_per_face(all_mesh);
|
||
EXPECT_EQ(all_per_face.count(3), 1u);
|
||
EXPECT_GT(all_mesh.num_triangles(), vis_mesh.num_triangles());
|
||
|
||
BrepModel empty;
|
||
auto empty_mesh = build_display_mesh(empty, respect);
|
||
EXPECT_TRUE(empty_mesh.indices.empty());
|
||
EXPECT_FALSE(empty_mesh.diagnostic.empty());
|
||
auto empty_wf = build_wireframe(empty, 0.01, false);
|
||
EXPECT_TRUE(empty_wf.segments.empty());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Wireframe / edge info
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(DisplayWireframeTest, BoxTwelveEdgesAndLineInfo) {
|
||
auto box = make_box(2.0, 1.0, 3.0);
|
||
auto wf = build_wireframe(box, 0.01, true);
|
||
EXPECT_EQ(wf.num_segments(), 12u); // 12 条 B-Rep 直线边,每边 1 段
|
||
EXPECT_EQ(wf.positions.size(), 24u);
|
||
EXPECT_EQ(wf.segment_edge_id.size(), wf.segments.size());
|
||
std::set<int32_t> edge_ids(wf.segment_edge_id.begin(), wf.segment_edge_id.end());
|
||
EXPECT_EQ(edge_ids.size(), 12u);
|
||
for (int32_t eid : edge_ids) {
|
||
const auto info = edge_display_info(box, eid);
|
||
EXPECT_TRUE(info.ok);
|
||
EXPECT_EQ(info.kind, DisplayEdgeKind::Line);
|
||
EXPECT_GT((info.start - info.end).norm(), 0.0);
|
||
}
|
||
const auto bad = edge_display_info(box, 9999);
|
||
EXPECT_FALSE(bad.ok);
|
||
EXPECT_FALSE(bad.error.empty());
|
||
}
|
||
|
||
TEST(DisplayWireframeTest, CurveUsesItsNativeParameterDomain) {
|
||
// STEP B-splines need not have a [0, 1] parameter range. Sampling that
|
||
// range instead of domain() collapses this quadratic edge onto its chord.
|
||
const curves::NurbsCurve curve(
|
||
{Point3D(2.0, 0.0, 0.0), Point3D(4.0, 2.0, 0.0),
|
||
Point3D(6.0, 0.0, 0.0)},
|
||
{4.0, 4.0, 4.0, 10.0, 10.0, 10.0}, {1.0, 1.0, 1.0}, 2);
|
||
ASSERT_EQ(curve.domain(), std::make_pair(4.0, 10.0));
|
||
BrepModel body;
|
||
const int v0 = body.add_vertex(curve.evaluate(curve.domain().first));
|
||
const int v1 = body.add_vertex(curve.evaluate(curve.domain().second));
|
||
body.add_edge(v0, v1, curve);
|
||
|
||
const Wireframe wf = build_wireframe(body, 1e-4, true);
|
||
ASSERT_GT(wf.positions.size(), 2u)
|
||
<< "The curve must not be mistaken for a straight line";
|
||
EXPECT_NEAR((wf.positions.front() - Point3D(2.0, 0.0, 0.0)).norm(),
|
||
0.0, 1e-12);
|
||
EXPECT_NEAR((wf.positions.back() - Point3D(6.0, 0.0, 0.0)).norm(),
|
||
0.0, 1e-12);
|
||
const Point3D expected_mid = curve.evaluate(7.0);
|
||
double min_mid_error = 100.0;
|
||
for (const Point3D& p : wf.positions)
|
||
min_mid_error = std::min(min_mid_error, (p - expected_mid).norm());
|
||
EXPECT_LT(min_mid_error, 1e-9);
|
||
}
|
||
|
||
TEST(DisplayWireframeTest, InvalidCurveFallsBackToTopologicalEndpoints) {
|
||
BrepModel body;
|
||
const int v0 = body.add_vertex(Point3D(2.0, 3.0, 4.0));
|
||
const int v1 = body.add_vertex(Point3D(5.0, 6.0, 7.0));
|
||
body.add_edge(v0, v1, curves::NurbsCurve({}, {}, {}, 2));
|
||
|
||
const Wireframe wf = build_wireframe(body, 1e-4, true);
|
||
ASSERT_EQ(wf.positions.size(), 2u);
|
||
EXPECT_EQ(wf.positions[0], Point3D(2.0, 3.0, 4.0));
|
||
EXPECT_EQ(wf.positions[1], Point3D(5.0, 6.0, 7.0));
|
||
ASSERT_EQ(wf.segments.size(), 1u);
|
||
}
|
||
|
||
TEST(DisplayWireframeTest, CircleEdgeInfoAndSmoothWireframe) {
|
||
auto cyl = make_cylinder(1.0, 2.0, 0);
|
||
const auto wf = build_wireframe(cyl, 1e-3, true);
|
||
ASSERT_FALSE(wf.segments.empty());
|
||
|
||
int circle_edges = 0;
|
||
double max_radius_err = 0.0;
|
||
for (size_t ei = 0; ei < cyl.num_edges(); ++ei) {
|
||
const auto info = edge_display_info(cyl, static_cast<int>(ei));
|
||
if (info.kind != DisplayEdgeKind::Circle) continue;
|
||
++circle_edges;
|
||
max_radius_err = std::max(max_radius_err, std::abs(info.radius - 1.0));
|
||
EXPECT_TRUE(info.ok);
|
||
EXPECT_NEAR(info.center.norm(), 1.0, 1e-9);
|
||
EXPECT_NEAR(std::abs(info.axis.normalized().z()), 1.0, 1e-9);
|
||
}
|
||
EXPECT_GE(circle_edges, 2);
|
||
EXPECT_LE(max_radius_err, 1e-9);
|
||
|
||
for (size_t si = 0; si < wf.segments.size(); ++si) {
|
||
const auto info = edge_display_info(cyl, wf.segment_edge_id[si]);
|
||
if (info.kind != DisplayEdgeKind::Circle) continue;
|
||
const double r = info.radius;
|
||
const Point3D a = wf.positions[wf.segments[si][0]];
|
||
const Point3D b = wf.positions[wf.segments[si][1]];
|
||
const Point3D mid = 0.5 * (a + b);
|
||
const double sag = std::abs(r - std::hypot(mid.x(), mid.y()));
|
||
EXPECT_LE(sag, 1.1e-3) << "segment " << si;
|
||
}
|
||
|
||
BrepModel box = make_box(4.0, 4.0, 2.0);
|
||
HoleSpec spec;
|
||
spec.diameter = 1.0;
|
||
spec.depth = 0.0;
|
||
BrepModel holed = make_hole(box, Point3D(0, 0, 1), Vector3D(0, 0, -1), spec);
|
||
int hole_circles = 0;
|
||
for (size_t ei = 0; ei < holed.num_edges(); ++ei) {
|
||
const auto info = edge_display_info(holed, static_cast<int>(ei));
|
||
if (info.kind == DisplayEdgeKind::Circle &&
|
||
std::abs(info.radius - 0.5) < 1e-9) ++hole_circles;
|
||
}
|
||
EXPECT_GE(hole_circles, 2);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// face_display_info
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(DisplayFaceInfoTest, PlaneCylinderSphereTorusParams) {
|
||
auto box = make_box(2.0, 4.0, 6.0);
|
||
bool saw_plane = false;
|
||
for (size_t fi = 0; fi < box.num_faces(); ++fi) {
|
||
const auto info = face_display_info(box, static_cast<int>(fi));
|
||
ASSERT_TRUE(info.ok);
|
||
EXPECT_EQ(info.type, FaceType::Plane);
|
||
EXPECT_NEAR(info.normal.norm(), 1.0, 1e-12);
|
||
const double ax = std::abs(info.normal.x()), ay = std::abs(info.normal.y()),
|
||
az = std::abs(info.normal.z());
|
||
EXPECT_NEAR(ax + ay + az, 1.0, 1e-12);
|
||
EXPECT_GT((info.bbox.max() - info.bbox.min()).norm(), 0.0);
|
||
saw_plane = true;
|
||
}
|
||
EXPECT_TRUE(saw_plane);
|
||
|
||
auto cyl = make_cylinder(1.0, 2.0, 0);
|
||
int cyl_faces = 0, cap_faces = 0;
|
||
for (size_t fi = 0; fi < cyl.num_faces(); ++fi) {
|
||
const auto info = face_display_info(cyl, static_cast<int>(fi));
|
||
ASSERT_TRUE(info.ok);
|
||
if (info.type == FaceType::Cylinder) {
|
||
++cyl_faces;
|
||
EXPECT_NEAR(info.radius, 1.0, 1e-9);
|
||
EXPECT_NEAR(std::abs(info.axis.normalized().z()), 1.0, 1e-9);
|
||
EXPECT_GT((info.bbox.max() - info.bbox.min()).norm(), 1.9);
|
||
} else if (info.type == FaceType::Plane) {
|
||
++cap_faces;
|
||
EXPECT_NEAR(std::abs(info.normal.z()), 1.0, 1e-12);
|
||
}
|
||
}
|
||
EXPECT_GE(cyl_faces, 1); // 精确圆柱可能按 4 个 90° 侧 patch 表达
|
||
EXPECT_EQ(cap_faces, 2);
|
||
|
||
auto sphere = make_sphere(2.0, 16, 8);
|
||
bool saw_sphere = false;
|
||
for (size_t fi = 0; fi < sphere.num_faces(); ++fi) {
|
||
const auto info = face_display_info(sphere, static_cast<int>(fi));
|
||
if (info.type != FaceType::Sphere) continue;
|
||
saw_sphere = true;
|
||
EXPECT_NEAR(info.radius, 2.0, 1e-9);
|
||
EXPECT_NEAR(info.center.norm(), 0.0, 1e-9);
|
||
break;
|
||
}
|
||
EXPECT_TRUE(saw_sphere);
|
||
|
||
auto torus = make_torus_exact(3.0, 1.0);
|
||
bool saw_torus_face = false;
|
||
for (size_t fi = 0; fi < torus.num_faces(); ++fi) {
|
||
const auto info = face_display_info(torus, static_cast<int>(fi));
|
||
ASSERT_TRUE(info.ok);
|
||
if (info.type == FaceType::Plane) continue;
|
||
saw_torus_face = true;
|
||
if (info.type == FaceType::Torus) {
|
||
EXPECT_NEAR(info.major_radius, 3.0, 0.05);
|
||
EXPECT_NEAR(info.minor_radius, 1.0, 0.05);
|
||
}
|
||
break;
|
||
}
|
||
EXPECT_TRUE(saw_torus_face);
|
||
|
||
const auto bad = face_display_info(box, 9999);
|
||
EXPECT_FALSE(bad.ok);
|
||
EXPECT_FALSE(bad.error.empty());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// LOD 预设 / 三角预算
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(DisplayQualityTest, PresetOptionsAndPlanarExact) {
|
||
const auto bbox = make_box(2.0, 2.0, 2.0).bounds();
|
||
const double diag = (bbox.max() - bbox.min()).norm();
|
||
const auto draft = make_display_options(DisplayQuality::Draft, diag, 5120);
|
||
const auto normal = make_display_options(DisplayQuality::Normal, diag, 5120);
|
||
const auto high = make_display_options(DisplayQuality::High, diag, 5120);
|
||
EXPECT_EQ(draft.quality, DisplayQuality::Draft);
|
||
EXPECT_EQ(normal.quality, DisplayQuality::Normal);
|
||
EXPECT_EQ(high.quality, DisplayQuality::High);
|
||
EXPECT_GT(draft.chord_tolerance, normal.chord_tolerance);
|
||
EXPECT_GT(normal.chord_tolerance, high.chord_tolerance);
|
||
EXPECT_GT(draft.angle_tolerance_deg, high.angle_tolerance_deg);
|
||
EXPECT_GT(draft.max_total_triangles, 0u);
|
||
EXPECT_LE(draft.max_total_triangles, 1000000u);
|
||
EXPECT_LE(normal.max_total_triangles, 4000000u);
|
||
|
||
auto box = make_box(2.0, 2.0, 2.0);
|
||
for (const auto* o : {&draft, &normal, &high}) {
|
||
const auto mesh = build_display_mesh(box, *o);
|
||
ASSERT_FALSE(mesh.indices.empty());
|
||
EXPECT_EQ(mesh.num_triangles(), 12u); // 平面 2 三角/面,不受 LOD 影响
|
||
for (size_t ti = 0; ti < mesh.num_triangles(); ++ti) {
|
||
for (int k = 0; k < 3; ++k) {
|
||
const auto& n = mesh.normals[mesh.indices[ti][static_cast<size_t>(k)]];
|
||
EXPECT_NEAR(n.norm(), 1.0, 1e-12);
|
||
EXPECT_NEAR(std::abs(n.x()) + std::abs(n.y()) + std::abs(n.z()), 1.0,
|
||
1e-12);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
TEST(DisplayQualityTest, PresetChordToleranceOnSphere) {
|
||
auto sphere = make_sphere(1.0, 16, 8);
|
||
const double diag = (sphere.bounds().max() - sphere.bounds().min()).norm();
|
||
for (auto quality : {DisplayQuality::Draft, DisplayQuality::Normal,
|
||
DisplayQuality::High}) {
|
||
const auto opts = make_display_options(quality, diag, sphere.num_faces());
|
||
const double abs_tol = opts.chord_tolerance * diag;
|
||
const auto mesh = build_display_mesh(sphere, opts);
|
||
ASSERT_FALSE(mesh.indices.empty()) << static_cast<int>(quality);
|
||
double max_dev = 0.0;
|
||
for (size_t ti = 0; ti < mesh.num_triangles(); ++ti) {
|
||
const Point3D a = mesh.positions[mesh.indices[ti][0]];
|
||
const Point3D b = mesh.positions[mesh.indices[ti][1]];
|
||
const Point3D c = mesh.positions[mesh.indices[ti][2]];
|
||
const Point3D centroid = (a + b + c) / 3.0;
|
||
max_dev = std::max(max_dev, std::abs(centroid.norm() - 1.0));
|
||
}
|
||
EXPECT_LE(max_dev, 2.5 * abs_tol)
|
||
<< "quality=" << static_cast<int>(quality) << " dev=" << max_dev
|
||
<< " tol=" << abs_tol;
|
||
}
|
||
}
|
||
|
||
TEST(DisplayQualityTest, TotalTriangleBudgetDowngrades) {
|
||
auto sphere = make_sphere(1.0, 16, 8);
|
||
DisplayTessOptions opts; // 默认细容差
|
||
opts.num_threads = 2;
|
||
const auto unbounded = build_display_mesh(sphere, opts);
|
||
ASSERT_GT(unbounded.num_triangles(), 2000u);
|
||
|
||
DisplayTessOptions budgeted = opts;
|
||
budgeted.max_total_triangles = 2000;
|
||
const auto bounded = build_display_mesh(sphere, budgeted);
|
||
EXPECT_LE(bounded.num_triangles(), 2000u);
|
||
EXPECT_FALSE(bounded.diagnostic.empty());
|
||
EXPECT_NE(bounded.diagnostic.find("budget"), std::string::npos);
|
||
}
|
||
|
||
|
||
TEST(DisplayMeshTest, StitchTjunctionsHandlesSpherePoles) {
|
||
// 球体两极存在开口边但边内部没有候选顶点;开启/关闭缝合都必须稳定,
|
||
// 且不能改变闭合区域的三角形数量。
|
||
auto sphere = make_sphere(1.0, 16, 8);
|
||
DisplayTessOptions on;
|
||
on.stitch_tjunctions = true;
|
||
DisplayTessOptions off;
|
||
off.stitch_tjunctions = false;
|
||
const auto a = build_display_mesh(sphere, on);
|
||
const auto b = build_display_mesh(sphere, off);
|
||
ASSERT_FALSE(a.indices.empty());
|
||
EXPECT_EQ(a.indices.size(), b.indices.size());
|
||
EXPECT_EQ(a.open_edge_count, b.open_edge_count);
|
||
}
|
||
|
||
TEST(DisplayQualityTest, PerFaceTriangleBudget) {
|
||
auto sphere = make_sphere(1.0, 16, 8);
|
||
DisplayTessOptions opts = make_display_options(DisplayQuality::High, 3.46,
|
||
sphere.num_faces());
|
||
opts.max_total_triangles = 0;
|
||
opts.max_tris_per_face = 40;
|
||
opts.num_threads = 2;
|
||
const auto mesh = build_display_mesh(sphere, opts);
|
||
ASSERT_FALSE(mesh.indices.empty());
|
||
std::map<int32_t, size_t> per_face = triangles_per_face(mesh);
|
||
for (const auto& [fid, cnt] : per_face) {
|
||
EXPECT_LE(cnt, 40u) << "face " << fid << " tris=" << cnt;
|
||
}
|
||
} |