feat(brep): exact spherical octant face for 7-face corner

This commit is contained in:
hm
2026-11-09 05:16:05 +08:00
parent 8b1887746e
commit 848ab93ee5
7 changed files with 89 additions and 60 deletions
+7
View File
@@ -272,4 +272,11 @@ struct LoftConstraint {
[[nodiscard]] NurbsSurface make_sphere_surface(
double radius, const Point3D& center = Point3D(0, 0, 0));
/// 精确球面八分体(三角片):u/v 均为 90° 有理二次弧,v=1 退化为极点。
[[nodiscard]] NurbsSurface make_sphere_octant_surface(
double radius, const Point3D& center = Point3D(0, 0, 0),
const Vector3D& ex = Vector3D(1, 0, 0),
const Vector3D& ey = Vector3D(0, 1, 0),
const Vector3D& ez = Vector3D(0, 0, 1));
} // namespace vde::curves
+2 -2
View File
@@ -4644,7 +4644,7 @@ static void repair_corner_after_boolean(BrepModel& result) {
int spheres = 0;
for (int fi = 0; fi < static_cast<int>(result.num_faces()); ++fi)
if (result.face_type(fi) == FaceType::Sphere) ++spheres;
if (spheres > 1 && std::getenv("VDE_MERGE_SPHERE") != nullptr) {
if (spheres > 1 && std::getenv("VDE_NO_MERGE_SPHERE") == nullptr) {
finalize_boolean_orientation(result);
merge_spherical_patches(result);
}
@@ -4655,7 +4655,7 @@ static void repair_corner_after_boolean(BrepModel& result) {
const auto t1 = std::chrono::steady_clock::now();
if (rep.detected) {
finalize_boolean_orientation(result);
if (std::getenv("VDE_MERGE_SPHERE") != nullptr)
if (std::getenv("VDE_NO_MERGE_SPHERE") == nullptr)
merge_spherical_patches(result);
const auto t2 = std::chrono::steady_clock::now();
if (std::getenv("VDE_BOOLEAN_PROFILE"))
+22 -21
View File
@@ -711,31 +711,32 @@ int merge_spherical_patches(BrepModel& body, double tol) {
if(it==sm.end()){sid=out.add_surface(body.surface(f.surface_id)); sm[f.surface_id]=sid;} else sid=it->second;
int nf=out.add_face(sid,ls); out.set_face_reversed(nf,f.reversed); faces.push_back(nf);
}
const auto full=curves::make_sphere_surface(radius,center);
constexpr double kPi=3.14159265358979323846;
TrimmedSurface ts; ts.base_surface=full; TrimLoop tl; tl.is_outer=true;
int walk=start;
for (int eid:ordered) {
auto& e=body.edge(eid);
const bool fwd=e.v_start==walk;
std::vector<Point3D> eu;
for (int k=0;k<=32;++k) {
double t=k/32.0; if(!fwd) t=1.0-t;
Point3D q=e.curve?e.curve->evaluate(t):body.vertex(e.v_start).point;
auto ab = curves::project_point_on_surface(q, full);
double u=ab.first, v=ab.second;
if(!eu.empty()){ while(u-eu.back().x()>0.5)u-=1.0; while(u-eu.back().x()<-0.5)u+=1.0;
if(v>0.9999)u=eu.back().x(); }
eu.emplace_back(u,v,0.0);
Vector3D axes[3];
bool got_axis[3] = {false, false, false};
const double scale = std::max(1.0, radius);
for (int eid : ordered) {
auto& e = body.edge(eid);
for (int vi : {e.v_start, e.v_end}) {
const Point3D q = body.vertex(vi).point - center;
int k = 0;
if (std::abs(q.y()) > std::abs(q.x())) k = 1;
if (std::abs(q.z()) > (k == 0 ? std::abs(q.x()) : std::abs(q.y()))) k = 2;
const double vals[3] = {q.x(), q.y(), q.z()};
bool dominant = std::abs(vals[k]) > 0.9 * radius;
for (int a = 0; a < 3; ++a)
if (a != k && std::abs(vals[a]) > 1e-4 * scale) dominant = false;
if (dominant) {
axes[k] = Vector3D(vals[0], vals[1], vals[2]) / vals[k];
got_axis[k] = true;
}
}
tl.p_curves.push_back(curves::NurbsCurve::interpolate(eu,2));
walk=fwd?e.v_end:e.v_start;
}
ts.loops.push_back(tl);
if (!got_axis[0] || !got_axis[1] || !got_axis[2]) return 0;
const auto octant = curves::make_sphere_octant_surface(
radius, center, axes[0], axes[1], axes[2]);
std::vector<int> sn; for(int eid:ordered) sn.push_back(me(eid));
int sid=out.add_surface(full), tid=out.add_trimmed_surface(ts);
int sid=out.add_surface(octant);
int lid=out.add_loop(sn,true), nf=out.add_face(sid,{lid});
out.set_face_trimmed_surface(nf,tid);
out.set_face_reversed(nf, body.face(group.front()).reversed);
faces.push_back(nf);
int sh=out.add_shell(faces,true); out.add_body({sh},"merged_sphere");
+28
View File
@@ -1474,4 +1474,32 @@ NurbsSurface make_sphere_surface(double radius, const Point3D& center) {
weights, 2, 2);
}
NurbsSurface make_sphere_octant_surface(double radius, const Point3D& center,
const Vector3D& ex, const Vector3D& ey,
const Vector3D& ez) {
constexpr double kPi = 3.14159265358979323846;
const double w = std::sqrt(0.5);
const double tk[6] = {0, 0, 0, 1, 1, 1};
const double wu[3] = {1.0, w, 1.0};
const double wv[3] = {1.0, w, 1.0};
const double ux[3] = {radius, radius, 0.0};
const double uy[3] = {0.0, radius, radius};
const double rv[3] = {1.0, 1.0, 0.0};
const double zv[3] = {0.0, radius, radius};
std::vector<std::vector<Point3D>> grid(3, std::vector<Point3D>(3));
std::vector<std::vector<double>> weights(3, std::vector<double>(3));
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
grid[i][j] = center + ex * (ux[i] * rv[j]) + ey * (uy[i] * rv[j]) +
ez * zv[j];
weights[i][j] = wu[i] * wv[j];
}
}
(void)kPi;
return NurbsSurface(grid, {tk[0], tk[1], tk[2], tk[3], tk[4], tk[5]},
{tk[0], tk[1], tk[2], tk[3], tk[4], tk[5]},
weights, 2, 2);
}
} // namespace vde::curves
+1 -1
View File
@@ -62,7 +62,7 @@ TEST(BooleanBenchmarkTest, BoxMinusCornerSphere_SMLibComparable) {
body.num_faces(), v.valid ? 1 : 0, v.watertightness,
std::abs(a.volume - ref) / ref, be, tris, ms);
EXPECT_EQ(body.num_faces(), 70u);
EXPECT_EQ(body.num_faces(), 7u);
EXPECT_TRUE(v.valid);
EXPECT_DOUBLE_EQ(v.watertightness, 1.0);
EXPECT_LT(std::abs(a.volume - ref) / ref, 1e-9);
+16 -36
View File
@@ -518,8 +518,7 @@ TEST(WatertightMetricsTest, SphereSeamPcurveArea) {
}
// 7 面合并仍受 p-curve/UV 一致性问题影响,暂不进入生产路径。
TEST(WatertightMetricsTest, DISABLED_OptInMergeSpherePatches) {
TEST(WatertightMetricsTest, OptInMergeSpherePatches) {
#ifdef _WIN32
_putenv_s("VDE_MERGE_SPHERE", "1");
#else
@@ -546,8 +545,7 @@ TEST(WatertightMetricsTest, DISABLED_OptInMergeSpherePatches) {
}
// 同上:7 面合并在部分半径/分辨率下体积失稳,禁用直到 p-curve 修复。
TEST(WatertightMetricsTest, DISABLED_SevenFaceMergeAcrossSphereResolutions) {
TEST(WatertightMetricsTest, SevenFaceMergeAcrossSphereResolutions) {
const int res[][2] = {{16, 8}, {24, 12}, {32, 16}};
for (auto& r : res) {
auto body = brep_difference(box100(), make_sphere(40.0, r[0], r[1]));
@@ -582,26 +580,15 @@ TEST(WatertightMetricsTest, DISABLED_SevenFaceMergeAcrossSphereResolutions) {
}
// 当前 p-curve/3D 边一致性未达生产要求(审计曾出现 5e-2~5e1 级偏差),
// 先禁用,作为精确性审计工具保留,修复后再启用。
TEST(WatertightMetricsTest, DISABLED_SevenFaceTrimExactnessAudit) {
#ifdef _WIN32
_putenv_s("VDE_MERGE_SPHERE", "1");
#else
setenv("VDE_MERGE_SPHERE", "1", 1);
#endif
// 球面八分体改为直接参数化后,7 面体不再依赖 TrimmedSurface p-curve。
TEST(WatertightMetricsTest, SevenFaceExactParametrizationNoPcurve) {
auto body = brep_difference(box100(), make_sphere(40.0, 32, 16));
#ifdef _WIN32
_putenv_s("VDE_MERGE_SPHERE", "");
#else
unsetenv("VDE_MERGE_SPHERE");
#endif
// 已知残差:当前 seam p-curve 与 3D 边最大偏差约 2e-2。
// 生产目标应为 <=1e-6;这里先固化审计能力,防止继续恶化。
const auto rep = validate_exact_trim(body, 1e-1);
EXPECT_EQ(body.num_faces(), 7u);
EXPECT_EQ(body.num_trimmed_surfaces(), 0u);
const auto rep = validate_exact_trim(body, 1e-9);
EXPECT_TRUE(rep.ok) << rep.error;
EXPECT_GT(rep.checked_faces, 0u);
EXPECT_LT(rep.max_pcurve_error, 1e-1);
EXPECT_EQ(rep.checked_faces, 0u);
EXPECT_DOUBLE_EQ(rep.max_pcurve_error, 0.0);
}
@@ -613,14 +600,14 @@ TEST(ExactBrepContract, DefaultBoxSpherePasses) {
EXPECT_DOUBLE_EQ(rep.validation.watertightness, 1.0);
EXPECT_EQ(rep.validation.dangling_edges, 0u);
EXPECT_EQ(rep.validation.non_manifold_edges, 0u);
EXPECT_EQ(body.num_faces(), 70u);
EXPECT_EQ(body.num_faces(), 7u);
}
TEST(ExactBrepContract, StrictDifferenceReturnsExactBody) {
auto r = brep_difference_exact(box100(), make_sphere(40.0, 32, 16));
ASSERT_TRUE(r.has_value());
EXPECT_EQ(r->num_faces(), 70u);
EXPECT_EQ(r->num_faces(), 7u);
EXPECT_TRUE(validate_exact_brep(*r, 1e-3).ok);
}
@@ -633,19 +620,12 @@ TEST(ExactBrepContract, StrictBoxUnionSucceeds) {
}
TEST(ExactBrepContract, RejectsMergedSphereWithBadPcurve) {
#ifdef _WIN32
_putenv_s("VDE_MERGE_SPHERE", "1");
#else
setenv("VDE_MERGE_SPHERE", "1", 1);
#endif
TEST(ExactBrepContract, AcceptsOctantSphereParametrization) {
auto r = brep_difference_exact(box100(), make_sphere(40.0, 32, 16));
#ifdef _WIN32
_putenv_s("VDE_MERGE_SPHERE", "");
#else
unsetenv("VDE_MERGE_SPHERE");
#endif
EXPECT_FALSE(r.has_value());
ASSERT_TRUE(r.has_value());
EXPECT_EQ(r->num_faces(), 7u);
EXPECT_EQ(r->num_trimmed_surfaces(), 0u);
EXPECT_TRUE(validate_exact_brep(*r, 1e-3).ok);
}
TEST(ExactBrepContract, FuzzBoxesNeverReturnInvalidBody) {
+13
View File
@@ -242,3 +242,16 @@ TEST(NurbsSurface, GlobalSphereOctantTrimArea) {
const auto m = ts.mass_integral(0.01);
EXPECT_NEAR(m.area, kPi * 40.0 * 40.0 / 2.0, 1.0);
}
TEST(NurbsSurface, SphereOctantSurfaceLiesOnSphere) {
const vde::core::Point3D center(1, 2, 3);
auto surf = vde::curves::make_sphere_octant_surface(50.0, center);
for (int i = 0; i <= 8; ++i) {
for (int j = 0; j <= 8; ++j) {
const auto p = surf.evaluate(i / 8.0, j / 8.0);
EXPECT_NEAR((p - center).norm(), 50.0, 1e-9)
<< "u=" << i / 8.0 << " v=" << j / 8.0;
}
}
}