fix(brep): rebuild split face shell and match coplanar edges geometrically
This commit is contained in:
@@ -204,7 +204,7 @@ display_conforming_patch 只对全外环简单 patch 生效,
|
||||
未合入失败测试,避免固定错误行为。
|
||||
|
||||
|
||||
## face_splitting 简单盒体分裂体积异常
|
||||
## face_splitting 简单盒体分裂体积异常(已修复)
|
||||
|
||||
合成:
|
||||
|
||||
@@ -235,3 +235,40 @@ face_splitting 在简单平面盒顶面也会破坏体积;
|
||||
~~~
|
||||
|
||||
未合入失败测试。
|
||||
|
||||
|
||||
修复:
|
||||
|
||||
~~~text
|
||||
face_splitting:
|
||||
复用原面承载 loop1;
|
||||
loop2 新建面;
|
||||
rebuild_single_shell 重建单一 shell/body,
|
||||
不再保留旧壳/旧体。
|
||||
|
||||
merge_coplanar_faces:
|
||||
几何边 key 增加曲线中点,
|
||||
区分同端点的不同弧;
|
||||
反向重复边按几何 key 归并。
|
||||
~~~
|
||||
|
||||
结果:
|
||||
|
||||
~~~text
|
||||
box 10×10×10 顶面分裂
|
||||
faces: 6 → 7
|
||||
body_volume: 1000 不变
|
||||
合并后 faces: 7 → 6
|
||||
body_volume: 1000 不变
|
||||
|
||||
半圆盘:
|
||||
2 面 → 1 面
|
||||
面积 πr²
|
||||
~~~
|
||||
|
||||
回归:
|
||||
|
||||
~~~text
|
||||
FaceMergeTest.SplitBoxTopThenMergeRestoresFace
|
||||
FaceMergeTest.CurvedBoundaryHalfDisksMergeToOneDisk
|
||||
~~~
|
||||
|
||||
@@ -575,59 +575,20 @@ std::vector<int> face_splitting(
|
||||
loop2_edges.push_back(path_edge);
|
||||
int loop2_id = body.add_loop(loop2_edges, true);
|
||||
|
||||
// add_face 会扩容 faces_,先从原面拷贝标量,避免悬垂引用。
|
||||
// 复用原面承载 loop1,原面 ID 不变;loop2 新建一个面。
|
||||
// 旧实现 add_shell/add_body 只追加不删除,会留下旧壳/旧体,
|
||||
// 导致 body_volume 重复计数(10×10×10 盒分裂后 1000→1166.67)。
|
||||
const int face_surface_id = face.surface_id;
|
||||
const bool face_reversed = face.reversed;
|
||||
int new_face1 = body.add_face(face_surface_id, {loop1_id});
|
||||
int new_face2 = body.add_face(face_surface_id, {loop2_id});
|
||||
// 分裂不改变曲面参数朝向,两个子面须保留原面朝向标志。
|
||||
body.set_face_reversed(new_face1, face_reversed);
|
||||
body.set_face_reversed(new_face2, face_reversed);
|
||||
body.set_face_loops(face_id, {loop1_id});
|
||||
int new_face = body.add_face(face_surface_id, {loop2_id});
|
||||
body.set_face_reversed(new_face, face_reversed);
|
||||
|
||||
// ── Step 6: 更新壳与体引用,替换原面 ──
|
||||
// add_shell 会扩容 shells_;先拷贝快照,循环中不再持有旧 vector 引用。
|
||||
const std::vector<TopoShell> shells = body.all_shells();
|
||||
std::map<int, int> shell_replacement; // 旧壳索引 → 新壳索引
|
||||
size_t old_shell_count = shells.size();
|
||||
for (size_t si = 0; si < old_shell_count; ++si) {
|
||||
auto& shell = shells[si];
|
||||
std::vector<int> new_shell_faces;
|
||||
bool replaced = false;
|
||||
for (int fi : shell.faces) {
|
||||
if (fi == face_id) {
|
||||
new_shell_faces.push_back(new_face1);
|
||||
new_shell_faces.push_back(new_face2);
|
||||
replaced = true;
|
||||
} else {
|
||||
new_shell_faces.push_back(fi);
|
||||
}
|
||||
}
|
||||
if (replaced) {
|
||||
int new_shell = body.add_shell(new_shell_faces, shell.closed);
|
||||
shell_replacement[static_cast<int>(si)] = new_shell;
|
||||
}
|
||||
}
|
||||
// add_body 同样会扩容 bodies_;使用快照避免旧引用失效。
|
||||
const std::vector<TopoBody> bodies = body.all_bodies();
|
||||
size_t old_body_count = bodies.size();
|
||||
for (size_t bi = 0; bi < old_body_count; ++bi) {
|
||||
auto& bd = bodies[bi];
|
||||
std::vector<int> new_body_shells;
|
||||
bool changed = false;
|
||||
for (int sid : bd.shells) {
|
||||
auto it = shell_replacement.find(sid);
|
||||
if (it != shell_replacement.end()) {
|
||||
new_body_shells.push_back(it->second);
|
||||
changed = true;
|
||||
} else {
|
||||
new_body_shells.push_back(sid);
|
||||
}
|
||||
}
|
||||
if (changed) body.add_body(new_body_shells, bd.name);
|
||||
}
|
||||
// 用全部面重建单一 shell/body,避免旧壳引用残留。
|
||||
body.rebuild_single_shell(true);
|
||||
|
||||
result.push_back(new_face1);
|
||||
result.push_back(new_face2);
|
||||
result.push_back(face_id);
|
||||
result.push_back(new_face);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
+30
-6
@@ -464,10 +464,33 @@ std::size_t merge_coplanar_faces(BrepModel& body, double tolerance) {
|
||||
for(int i=0;i<nf;++i) parent[i]=i;
|
||||
auto find=[&](int x){ while(parent[x]!=x){parent[x]=parent[parent[x]];x=parent[x];} return x; };
|
||||
auto unite=[&](int a,int b){parent[find(a)]=find(b);};
|
||||
std::map<int,std::vector<int>> edge_faces;
|
||||
for(int fi: planar) for(int ei: body.face_edges(fi)) edge_faces[ei].push_back(fi);
|
||||
// 按几何端点匹配边:分裂/布尔结果常在公共边上留下方向相反、
|
||||
// id 不同的两条边;只用 edge id 会把同平面碎片拆成互不相邻的组。
|
||||
auto edge_key=[&](int ei) {
|
||||
const auto& e=body.edge(ei);
|
||||
const auto& a=body.vertex(e.v_start).point;
|
||||
const auto& b=body.vertex(e.v_end).point;
|
||||
auto q=[&](const Point3D& p) {
|
||||
return std::array<long long,3>{std::llround(p.x()*1e7),
|
||||
std::llround(p.y()*1e7),
|
||||
std::llround(p.z()*1e7)};
|
||||
};
|
||||
Point3D mid = (a + b) * 0.5;
|
||||
if (const auto& ec = body.edge_curve(ei)) {
|
||||
const auto dom = ec->domain();
|
||||
if (dom.second > dom.first)
|
||||
mid = ec->evaluate(0.5 * (dom.first + dom.second));
|
||||
}
|
||||
auto ka=q(a), kb=q(b), km=q(mid);
|
||||
if (kb<ka) std::swap(ka,kb);
|
||||
return std::array<long long,9>{ka[0],ka[1],ka[2],kb[0],kb[1],kb[2],
|
||||
km[0],km[1],km[2]};
|
||||
};
|
||||
std::map<std::array<long long,9>,std::vector<int>> edge_geo_faces;
|
||||
for(int fi: planar) for(int ei: body.face_edges(fi))
|
||||
for(int fj: edge_faces[ei])
|
||||
edge_geo_faces[edge_key(ei)].push_back(fi);
|
||||
for(int fi: planar) for(int ei: body.face_edges(fi))
|
||||
for(int fj: edge_geo_faces[edge_key(ei)])
|
||||
if(fj!=fi && same_plane(fi,fj)) unite(fi,fj);
|
||||
|
||||
std::map<int,std::vector<int>> groups;
|
||||
@@ -485,10 +508,11 @@ std::size_t merge_coplanar_faces(BrepModel& body, double tolerance) {
|
||||
Maps maps;
|
||||
std::vector<int> out_faces;
|
||||
for(auto& [root,faces]: group_members) {
|
||||
std::map<int,int> use;
|
||||
for(int fi:faces) for(int ei: body.face_edges(fi)) ++use[ei];
|
||||
std::map<std::array<long long,9>,std::vector<int>> use;
|
||||
for(int fi:faces) for(int ei: body.face_edges(fi))
|
||||
use[edge_key(ei)].push_back(ei);
|
||||
std::vector<int> bedges;
|
||||
for(auto& [ei,n]: use) if(n==1) bedges.push_back(ei);
|
||||
for(auto& [key,ids]: use) if(ids.size()==1) bedges.push_back(ids.front());
|
||||
std::vector<std::vector<int>> loops;
|
||||
if(!collect_boundary_loops(body,bedges,loops) || loops.empty()) {
|
||||
for(int fi:faces) out_faces.push_back(cf(out,body,fi,maps)); continue;
|
||||
|
||||
@@ -1546,3 +1546,31 @@ TEST(DisplayMeshTest, TwoHolePlateBoundaryIsClosed) {
|
||||
ASSERT_FALSE(mesh.indices.empty()) << mesh.diagnostic;
|
||||
EXPECT_EQ(mesh.open_edge_count, 0u) << mesh.diagnostic;
|
||||
}
|
||||
|
||||
|
||||
TEST(FaceMergeTest, SplitBoxTopThenMergeRestoresFace) {
|
||||
auto box = make_box(10, 10, 10);
|
||||
const int before_faces = static_cast<int>(box.num_faces());
|
||||
int top = -1;
|
||||
for (int fi = 0; fi < before_faces; ++fi) {
|
||||
if (box.face_type(fi) != FaceType::Plane) continue;
|
||||
auto g = box.face_plane_geometry(fi);
|
||||
if (g && g->normal.z() > 0.9 && std::abs(g->point.z() - 5.0) < 1e-6) {
|
||||
top = fi;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ASSERT_GE(top, 0);
|
||||
const double before_volume = box.body_volume();
|
||||
const auto parts = face_splitting(
|
||||
box, top,
|
||||
vde::curves::NurbsCurve::line(Point3D(0.5, 0.0, 0.0),
|
||||
Point3D(0.5, 1.0, 0.0)));
|
||||
ASSERT_EQ(parts.size(), 2u);
|
||||
EXPECT_EQ(box.num_faces(), static_cast<size_t>(before_faces + 1));
|
||||
EXPECT_NEAR(box.body_volume(), before_volume, 1e-9);
|
||||
const auto merged = merge_coplanar_faces(box);
|
||||
EXPECT_GT(merged, 0);
|
||||
EXPECT_EQ(box.num_faces(), static_cast<size_t>(before_faces));
|
||||
EXPECT_NEAR(box.body_volume(), before_volume, 1e-9);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user