Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
# 自然 B-Rep 面合并
|
||||
|
||||
> 日期:2026-11-12
|
||||
> 目标:布尔/打孔结果不再以四分之一圆柱片交付,宿主可直接选完整面。
|
||||
|
||||
## 面数变化
|
||||
|
||||
```text
|
||||
box−cylinder union:
|
||||
16 → 10
|
||||
4 侧 + 2 环 + 2 完整圆柱带 + 2 盖
|
||||
|
||||
difference:
|
||||
10 → 7
|
||||
4 侧 + 2 环 + 1 完整孔壁
|
||||
|
||||
intersection:
|
||||
6 → 3
|
||||
1 完整圆柱带 + 2 盖
|
||||
```
|
||||
|
||||
## 实现
|
||||
|
||||
### 1. 同圆柱分组
|
||||
|
||||
按:
|
||||
|
||||
```text
|
||||
axis 平行
|
||||
axis 线重合
|
||||
radius 相等
|
||||
共享边连通
|
||||
```
|
||||
|
||||
把四分之一圆柱片分组。
|
||||
|
||||
### 2. 完整圆柱基面
|
||||
|
||||
每组重新构造 360° 圆柱面:
|
||||
|
||||
```cpp
|
||||
make_cylinder_surface(radius, height, base, ex, ey, axis)
|
||||
```
|
||||
|
||||
ex 取 seam 顶点径向。
|
||||
|
||||
### 3. 周期 seam loop
|
||||
|
||||
合并后的面使用一个 outer loop:
|
||||
|
||||
```text
|
||||
上圆环
|
||||
→ seam 下
|
||||
→ 下圆环反向
|
||||
→ seam 上
|
||||
```
|
||||
|
||||
p-curve 域为:
|
||||
|
||||
```text
|
||||
u ∈ [0,1]
|
||||
v ∈ [v_bottom, v_top]
|
||||
```
|
||||
|
||||
seam 边在 loop 中出现两次,validate 按周期 seam 规则计为闭合。
|
||||
|
||||
### 4. 内外法向
|
||||
|
||||
按源面法向与径向点积判断:
|
||||
|
||||
```text
|
||||
外层圆柱带:向外
|
||||
孔壁:向内
|
||||
```
|
||||
|
||||
并反转 p-curve loop 方向。
|
||||
|
||||
### 5. 体积护栏
|
||||
|
||||
合并后立即比较合并前后体积:
|
||||
|
||||
```text
|
||||
|V_after − V_before| <= 1e-6·max(1,V)
|
||||
```
|
||||
|
||||
不满足则放弃合并,保留原碎片拓扑,绝不交付错误自然面。
|
||||
|
||||
## 验收
|
||||
|
||||
```text
|
||||
union:
|
||||
faces=10
|
||||
trim=2.78e-15
|
||||
vol=70.283185
|
||||
|
||||
difference:
|
||||
faces=7
|
||||
trim=8.16e-16
|
||||
vol=51.433629
|
||||
|
||||
intersection:
|
||||
faces=3
|
||||
trim=8.46e-16
|
||||
vol=12.566371
|
||||
```
|
||||
|
||||
旋转 fuzz:
|
||||
|
||||
```text
|
||||
3/3:
|
||||
faces=10
|
||||
trim<=4.3e-14
|
||||
vol=70.283185
|
||||
```
|
||||
|
||||
随机尺寸:
|
||||
|
||||
```text
|
||||
8/8 通过(体积护栏下自动回退)
|
||||
```
|
||||
|
||||
## 限制
|
||||
|
||||
```text
|
||||
1. 目前只合并圆柱面。
|
||||
平面/圆锥/球/环面尚未合并。
|
||||
2. 体积护栏不通过时保留碎片拓扑,
|
||||
面数可能仍高于自然目标。
|
||||
3. 斜交圆柱 union 仍失败(独立问题)。
|
||||
```
|
||||
|
||||
## 文件
|
||||
|
||||
```text
|
||||
include/vde/brep/face_merge.h
|
||||
src/brep/face_merge.cpp
|
||||
```
|
||||
|
||||
|
||||
## 适用范围修正
|
||||
|
||||
本阶段只对**布尔结果**启用合并。
|
||||
|
||||
未启用:
|
||||
|
||||
~~~text
|
||||
make_cylinder_exact 原始体
|
||||
make_hole_on_face 直接打孔结果
|
||||
~~~
|
||||
|
||||
原因:
|
||||
|
||||
~~~text
|
||||
直接合并基础圆柱/直接孔壁后,
|
||||
BrepOffset 的 4 个孔偏置用例体积偏差 1047.2,
|
||||
CylinderOffsetBeyondRadiusFails 也由失败变为成功。
|
||||
~~~
|
||||
|
||||
因此保留:
|
||||
|
||||
~~~text
|
||||
布尔 union/difference/intersection → 自然圆柱面
|
||||
原始圆柱 / 直接孔 → 仍为原拓扑
|
||||
~~~
|
||||
|
||||
后续需要让 offset 支持周期 seam 面后再放开。
|
||||
|
||||
|
||||
## 平面片合并(第二阶段)
|
||||
|
||||
新增:
|
||||
|
||||
~~~cpp
|
||||
merge_coplanar_faces(body, tolerance);
|
||||
~~~
|
||||
|
||||
规则:
|
||||
|
||||
~~~text
|
||||
同法向、同平面偏移
|
||||
共享边连通
|
||||
组边界全部为直线
|
||||
~~~
|
||||
|
||||
重建:
|
||||
|
||||
~~~text
|
||||
最大环 = outer
|
||||
其余环 = holes
|
||||
outer CCW / holes CW
|
||||
直线 p-curve
|
||||
~~~
|
||||
|
||||
体积护栏同圆柱合并:
|
||||
|
||||
~~~text
|
||||
体积变化超过 1e-6 则回退
|
||||
~~~
|
||||
|
||||
测试:
|
||||
|
||||
~~~text
|
||||
FaceMergeTest.CoplanarQuadsMergeToOneFace
|
||||
2 个共面四边形 → 1 个面
|
||||
area=8
|
||||
~~~
|
||||
|
||||
限制:
|
||||
|
||||
~~~text
|
||||
含曲线边界的平面组暂不合并
|
||||
(例如带圆孔的环面,保持原精确 trim)。
|
||||
~~~
|
||||
|
||||
应用到布尔结果:
|
||||
|
||||
~~~text
|
||||
先 merge_coaxial_cylinder_faces
|
||||
再 merge_coplanar_faces
|
||||
~~~
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
/**
|
||||
* @file face_merge.h
|
||||
* @brief 将布尔/打孔结果的同轴圆柱碎片合并为带周期 seam 的自然面。
|
||||
*/
|
||||
#include <cstddef>
|
||||
#include "vde/brep/brep.h"
|
||||
|
||||
namespace vde::brep {
|
||||
|
||||
/// 合并同轴同半径、覆盖完整 360° 的圆柱壁面片。
|
||||
std::size_t merge_coaxial_cylinder_faces(BrepModel& body,
|
||||
double tolerance = 1e-7);
|
||||
|
||||
/// 合并同平面、共享边、边界全为直线的面片。
|
||||
std::size_t merge_coplanar_faces(BrepModel& body, double tolerance = 1e-7);
|
||||
|
||||
} // namespace vde::brep
|
||||
@@ -324,6 +324,7 @@ add_library(vde_brep ${_vde_kernel_lib_type}
|
||||
brep/boolean_quality.cpp
|
||||
brep/ssi_boolean.cpp
|
||||
brep/pcurve_boolean.cpp
|
||||
brep/face_merge.cpp
|
||||
brep/analytic_ssi.cpp
|
||||
# VDE-1319: 精确弧 NURBS + 弧边 T 型分裂(VDE-1315 曲面裁剪管线基础设施)
|
||||
brep/analytic_trim.cpp
|
||||
|
||||
@@ -0,0 +1,517 @@
|
||||
#include "vde/brep/face_merge.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include "vde/curves/generative_surfaces.h"
|
||||
#include "vde/curves/nurbs_curve.h"
|
||||
#include "vde/curves/curve_surface_projection.h"
|
||||
|
||||
namespace vde::brep {
|
||||
namespace {
|
||||
|
||||
struct CylKey {
|
||||
Point3D p;
|
||||
Vector3D axis;
|
||||
double radius = 0.0;
|
||||
bool valid = false;
|
||||
};
|
||||
|
||||
bool same_cylinder(const CylKey& a, const CylKey& b, double tol) {
|
||||
if (!a.valid || !b.valid) return false;
|
||||
if (std::abs(a.radius - b.radius) > tol) return false;
|
||||
if (std::abs(a.axis.dot(b.axis)) < 1.0 - 1e-9) return false;
|
||||
const Vector3D d = b.p - a.p;
|
||||
const Vector3D perp = d - a.axis * d.dot(a.axis);
|
||||
return perp.norm() <= tol;
|
||||
}
|
||||
|
||||
struct Maps {
|
||||
std::map<int,int> v, e, s, t;
|
||||
};
|
||||
|
||||
int cv(BrepModel& out, const BrepModel& src, int v, Maps& m) {
|
||||
auto it=m.v.find(v); if (it!=m.v.end()) return it->second;
|
||||
int nv=out.add_vertex(src.vertex(v).point); m.v[v]=nv; return nv;
|
||||
}
|
||||
int ce(BrepModel& out, const BrepModel& src, int ei, Maps& m) {
|
||||
auto it=m.e.find(ei); if (it!=m.e.end()) return it->second;
|
||||
const auto& e=src.edge(ei);
|
||||
int v0=cv(out,src,e.v_start,m), v1=cv(out,src,e.v_end,m);
|
||||
int ne=e.curve?out.add_edge(v0,v1,*e.curve):out.add_edge(v0,v1);
|
||||
m.e[ei]=ne; return ne;
|
||||
}
|
||||
int cf(BrepModel& out, const BrepModel& src, int fi, Maps& m) {
|
||||
const auto& f=src.face(fi);
|
||||
int sid;
|
||||
auto sit=m.s.find(f.surface_id);
|
||||
if (sit==m.s.end()) { sid=out.add_surface(src.surface(f.surface_id)); m.s[f.surface_id]=sid; }
|
||||
else sid=sit->second;
|
||||
std::vector<int> loops;
|
||||
for (int lid: f.loops) {
|
||||
const auto& lp=src.loop_by_id(lid);
|
||||
std::vector<int> edges;
|
||||
for (int ei: lp.edges) edges.push_back(ce(out,src,ei,m));
|
||||
loops.push_back(out.add_loop(edges,lp.is_outer));
|
||||
}
|
||||
int nf=out.add_face(sid,loops);
|
||||
out.set_face_reversed(nf,f.reversed);
|
||||
if (f.trimmed_surface_id>=0) {
|
||||
auto tit=m.t.find(f.trimmed_surface_id);
|
||||
int nid;
|
||||
if (tit==m.t.end()) { nid=out.add_trimmed_surface(src.trimmed_surface(f.trimmed_surface_id)); m.t[f.trimmed_surface_id]=nid; }
|
||||
else nid=tit->second;
|
||||
out.set_face_trimmed_surface(nf,nid);
|
||||
}
|
||||
return nf;
|
||||
}
|
||||
|
||||
bool order_cycle(const BrepModel& body, const std::vector<int>& edges,
|
||||
int start_v, std::vector<int>& ordered,
|
||||
std::vector<int>& verts) {
|
||||
if (edges.empty()) return false;
|
||||
std::map<int,std::vector<int>> inc;
|
||||
std::set<int> remain(edges.begin(),edges.end());
|
||||
for (int ei: edges) {
|
||||
const auto& e=body.edge(ei);
|
||||
inc[e.v_start].push_back(ei);
|
||||
inc[e.v_end].push_back(ei);
|
||||
}
|
||||
ordered.clear(); verts.clear();
|
||||
int cur=start_v;
|
||||
verts.push_back(cur);
|
||||
while (!remain.empty()) {
|
||||
int next=-1;
|
||||
for (int ei: inc[cur]) if (remain.count(ei)) { next=ei; break; }
|
||||
if (next<0) return false;
|
||||
remain.erase(next);
|
||||
ordered.push_back(next);
|
||||
const auto& e=body.edge(next);
|
||||
cur=(e.v_start==cur)?e.v_end:e.v_start;
|
||||
verts.push_back(cur);
|
||||
}
|
||||
return cur==start_v;
|
||||
}
|
||||
|
||||
struct UV { double u=0.0, v=0.0; };
|
||||
|
||||
UV project_cyl(const BrepModel& body, const curves::NurbsSurface& surf,
|
||||
const Point3D& p) {
|
||||
const auto uv=curves::project_point_on_surface(p,surf);
|
||||
return {uv.first,uv.second};
|
||||
}
|
||||
|
||||
TrimLoop make_seam_loop(const BrepModel& body, const curves::NurbsSurface& surf,
|
||||
const std::vector<int>& top_edges,
|
||||
const std::vector<int>& top_verts,
|
||||
const std::vector<int>& bottom_edges,
|
||||
const std::vector<int>& bottom_verts,
|
||||
double v_top, double v_bottom, bool reverse) {
|
||||
TrimLoop tl; tl.is_outer=true;
|
||||
auto top_e=top_edges, top_v=top_verts;
|
||||
auto bot_e=bottom_edges, bot_v=bottom_verts;
|
||||
auto span=[&](const std::vector<int>& es, const std::vector<int>& vs){
|
||||
double s=0.0;
|
||||
for (size_t k=0;k<es.size();++k) {
|
||||
const auto a=project_cyl(body,surf,body.vertex(vs[k]).point).u;
|
||||
const auto b=project_cyl(body,surf,body.vertex(vs[k+1]).point).u;
|
||||
double d=b-a; while(d>0.5)d-=1.0; while(d<-0.5)d+=1.0;
|
||||
s+=d;
|
||||
}
|
||||
return s;
|
||||
};
|
||||
if (span(top_e,top_v)<0) { std::reverse(top_e.begin(),top_e.end()); std::reverse(top_v.begin(),top_v.end()); }
|
||||
if (span(bot_e,bot_v)<0) { std::reverse(bot_e.begin(),bot_e.end()); std::reverse(bot_v.begin(),bot_v.end()); }
|
||||
const auto push_line=[&](double u0,double v0,double u1,double v1){
|
||||
std::vector<Point3D> cps={Point3D(u0,v0,0),Point3D(u1,v1,0)};
|
||||
tl.p_curves.emplace_back(cps,std::vector<double>{0,0,1,1},
|
||||
std::vector<double>{1,1},1);
|
||||
};
|
||||
// top:从 seam 处绕一圈,累计 u+1
|
||||
double u0=project_cyl(body,surf,body.vertex(top_v.front()).point).u;
|
||||
double cur=u0;
|
||||
for (size_t k=0;k<top_e.size();++k) {
|
||||
const auto a=project_cyl(body,surf,body.vertex(top_v[k]).point).u;
|
||||
const auto b=project_cyl(body,surf,body.vertex(top_v[k+1]).point).u;
|
||||
double d=b-a; while(d>0.5)d-=1.0; while(d<-0.5)d+=1.0;
|
||||
if (std::abs(d)<1e-9) d=1.0/static_cast<double>(top_e.size());
|
||||
push_line(cur,v_top,cur+d,v_top);
|
||||
cur+=d;
|
||||
}
|
||||
if (std::getenv("VDE_MERGE_DIAG"))
|
||||
std::fprintf(stderr,"[MERGE] top span=%.6f vtop=%.4f vbot=%.4f\n",
|
||||
cur-u0,v_top,v_bottom);
|
||||
// seam down at u0+1
|
||||
push_line(cur,v_top,cur,v_bottom);
|
||||
// bottom 反向:从 seam 回到 u0
|
||||
double cur_b=cur;
|
||||
for (size_t k=bot_e.size();k-- >0;) {
|
||||
const auto a=project_cyl(body,surf,body.vertex(bot_v[k+1]).point).u;
|
||||
const auto b=project_cyl(body,surf,body.vertex(bot_v[k]).point).u;
|
||||
double d=b-a; while(d>0.5)d-=1.0; while(d<-0.5)d+=1.0;
|
||||
if (std::abs(d)<1e-9) d=-1.0/static_cast<double>(bot_e.size());
|
||||
push_line(cur_b,v_bottom,cur_b+d,v_bottom);
|
||||
cur_b+=d;
|
||||
}
|
||||
if (std::getenv("VDE_MERGE_DIAG"))
|
||||
std::fprintf(stderr,"[MERGE] bottom end=%.6f\n",cur_b);
|
||||
// seam up at u0
|
||||
push_line(cur_b,v_bottom,cur_b,v_top);
|
||||
if (reverse) {
|
||||
std::reverse(tl.p_curves.begin(), tl.p_curves.end());
|
||||
for (auto& pc : tl.p_curves)
|
||||
pc = curves::reverse_curve(pc);
|
||||
}
|
||||
return tl;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::size_t merge_coaxial_cylinder_faces(BrepModel& body, double tolerance) {
|
||||
const int nf=static_cast<int>(body.num_faces());
|
||||
if (nf<2) return 0;
|
||||
std::vector<CylKey> keys(nf);
|
||||
std::vector<int> cyl;
|
||||
for (int fi=0;fi<nf;++fi) {
|
||||
if (body.face_type(fi)!=FaceType::Cylinder) continue;
|
||||
auto g=body.face_cylinder_geometry(fi);
|
||||
if (!g) continue;
|
||||
keys[fi]={g->axis_point,g->axis.normalized(),g->radius,true};
|
||||
cyl.push_back(fi);
|
||||
}
|
||||
if (cyl.size()<2) return 0;
|
||||
|
||||
std::vector<int> parent(nf);
|
||||
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: cyl) for (int ei: body.face_edges(fi)) edge_faces[ei].push_back(fi);
|
||||
for (int fi: cyl) for (int ei: body.face_edges(fi))
|
||||
for (int fj: edge_faces[ei])
|
||||
if (fj!=fi && same_cylinder(keys[fi],keys[fj],tolerance)) unite(fi,fj);
|
||||
std::map<int,std::vector<int>> groups;
|
||||
for (int fi: cyl) groups[find(fi)].push_back(fi);
|
||||
|
||||
std::map<int,int> face_group;
|
||||
std::map<int,std::vector<int>> group_members;
|
||||
for (auto& [root,faces]: groups)
|
||||
if (faces.size()>1) {
|
||||
group_members[root]=faces;
|
||||
for (int fi: faces) face_group[fi]=root;
|
||||
}
|
||||
if (group_members.empty()) return 0;
|
||||
|
||||
BrepModel out;
|
||||
Maps maps;
|
||||
std::vector<int> out_faces;
|
||||
std::map<int,int> merged_face;
|
||||
|
||||
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::vector<int> bedges;
|
||||
for (auto& [ei,n]: use) if (n==1) bedges.push_back(ei);
|
||||
// 边界必须正好两条闭合环(上下圆)
|
||||
std::set<int> remain(bedges.begin(),bedges.end());
|
||||
std::vector<std::vector<int>> loops;
|
||||
bool ok=true;
|
||||
while(!remain.empty()) {
|
||||
int seed=*remain.begin();
|
||||
std::vector<int> loop;
|
||||
int start=body.edge(seed).v_start;
|
||||
int cur=start; int ei=seed;
|
||||
while(true) {
|
||||
if(!remain.erase(ei)) { ok=false; break; }
|
||||
loop.push_back(ei);
|
||||
const auto& e=body.edge(ei);
|
||||
cur=(e.v_start==cur)?e.v_end:e.v_start;
|
||||
if(cur==start) break;
|
||||
int next=-1;
|
||||
// 找 remain 中 incident cur 的边
|
||||
for(int cand: bedges) if(remain.count(cand)) {
|
||||
const auto& ce=body.edge(cand);
|
||||
if(ce.v_start==cur||ce.v_end==cur){next=cand;break;}
|
||||
}
|
||||
if(next<0){ok=false;break;}
|
||||
ei=next;
|
||||
}
|
||||
if(!ok) break;
|
||||
loops.push_back(loop);
|
||||
}
|
||||
if(!ok||loops.size()!=2) { for(int fi:faces) out_faces.push_back(cf(out,body,fi,maps)); continue; }
|
||||
|
||||
// 轴向范围、上下环
|
||||
const auto& g=body.face_cylinder_geometry(faces.front());
|
||||
if(!g) { for(int fi:faces) out_faces.push_back(cf(out,body,fi,maps)); continue; }
|
||||
const Vector3D axis=g->axis.normalized();
|
||||
double tmin=1e300,tmax=-1e300;
|
||||
for(int ei: bedges) for(int v: {body.edge(ei).v_start,body.edge(ei).v_end}) {
|
||||
double t=body.vertex(v).point.dot(axis);
|
||||
tmin=std::min(tmin,t); tmax=std::max(tmax,t);
|
||||
}
|
||||
std::vector<double> lavg(2,0.0);
|
||||
for(int li=0;li<2;++li) {
|
||||
double s=0; int n=0;
|
||||
for(int ei: loops[li]) for(int v: {body.edge(ei).v_start,body.edge(ei).v_end}) {
|
||||
s+=body.vertex(v).point.dot(axis); ++n;
|
||||
}
|
||||
lavg[li]=n?s/n:0.0;
|
||||
}
|
||||
int top_i=(lavg[0]>lavg[1])?0:1;
|
||||
int bot_i=1-top_i;
|
||||
auto top_loop=loops[top_i], bot_loop=loops[bot_i];
|
||||
|
||||
// 选 seam:上下环中径向角相同的顶点
|
||||
int stop=-1, sbot=-1;
|
||||
for(int ei: top_loop) {
|
||||
const auto& e=body.edge(ei);
|
||||
for(int tv: {e.v_start,e.v_end}) {
|
||||
const Point3D p=body.vertex(tv).point;
|
||||
for(int ej: bot_loop) {
|
||||
const auto& e2=body.edge(ej);
|
||||
for(int bv: {e2.v_start,e2.v_end}) {
|
||||
const Point3D q=body.vertex(bv).point;
|
||||
const Vector3D rp=p-g->axis_point-axis*(p-g->axis_point).dot(axis);
|
||||
const Vector3D rq=q-g->axis_point-axis*(q-g->axis_point).dot(axis);
|
||||
if(std::abs(rp.dot(rq)/(rp.norm()*rq.norm())-1.0)<1e-9) {
|
||||
stop=tv; sbot=bv; break;
|
||||
}
|
||||
}
|
||||
if(stop>=0) break;
|
||||
}
|
||||
if(stop>=0) break;
|
||||
}
|
||||
if(stop>=0) break;
|
||||
}
|
||||
if(stop<0||sbot<0) { for(int fi:faces) out_faces.push_back(cf(out,body,fi,maps)); continue; }
|
||||
|
||||
std::vector<int> top_edges,top_verts,bot_edges,bot_verts;
|
||||
if(!order_cycle(body,top_loop,stop,top_edges,top_verts) ||
|
||||
!order_cycle(body,bot_loop,sbot,bot_edges,bot_verts)) {
|
||||
for(int fi:faces) out_faces.push_back(cf(out,body,fi,maps)); continue;
|
||||
}
|
||||
|
||||
// 完整圆柱基面:ex 取 seam 径向
|
||||
const Point3D sp=body.vertex(stop).point;
|
||||
Vector3D ex=sp-g->axis_point-axis*(sp-g->axis_point).dot(axis);
|
||||
if(ex.norm()<1e-12) { for(int fi:faces) out_faces.push_back(cf(out,body,fi,maps)); continue; }
|
||||
ex.normalize();
|
||||
const Vector3D ey=axis.cross(ex);
|
||||
const Point3D base=g->axis_point+axis*(tmin-g->axis_point.dot(axis));
|
||||
const double height=tmax-tmin;
|
||||
auto surf=curves::make_cylinder_surface(g->radius,height,base,ex,ey,axis);
|
||||
|
||||
// 边界边复制 + seam 边
|
||||
std::vector<int> loop_edges;
|
||||
for(int ei: top_edges) loop_edges.push_back(ce(out,body,ei,maps));
|
||||
int vstop=cv(out,body,stop,maps), vsbot=cv(out,body,sbot,maps);
|
||||
int seam=out.add_edge(vstop,vsbot);
|
||||
loop_edges.push_back(seam);
|
||||
for(size_t k=bot_edges.size();k-->0;) loop_edges.push_back(ce(out,body,bot_edges[k],maps));
|
||||
loop_edges.push_back(seam);
|
||||
|
||||
int sid=out.add_surface(surf);
|
||||
int lp=out.add_loop(loop_edges,true);
|
||||
int nfid=out.add_face(sid,{lp});
|
||||
const Point3D fc=body.face_center(faces.front());
|
||||
const Vector3D frad=fc-g->axis_point-axis*(fc-g->axis_point).dot(axis);
|
||||
const bool inward=
|
||||
body.face_normal(faces.front()).dot(frad)<0.0;
|
||||
out.set_face_reversed(nfid,inward);
|
||||
TrimmedSurface ts; ts.base_surface=surf;
|
||||
const auto uvtop=project_cyl(body,surf,body.vertex(stop).point).v;
|
||||
const auto uvbot=project_cyl(body,surf,body.vertex(sbot).point).v;
|
||||
ts.loops.push_back(make_seam_loop(body,surf,top_edges,top_verts,bot_edges,bot_verts,
|
||||
uvtop,uvbot, inward));
|
||||
out.set_face_trimmed_surface(nfid,out.add_trimmed_surface(ts));
|
||||
merged_face[root]=nfid;
|
||||
out_faces.push_back(nfid);
|
||||
}
|
||||
|
||||
for(int fi=0;fi<nf;++fi) if(!face_group.count(fi)) out_faces.push_back(cf(out,body,fi,maps));
|
||||
if(out_faces.empty()) return 0;
|
||||
int sh=out.add_shell(out_faces,true);
|
||||
out.add_body({sh},"merged");
|
||||
// 合并必须以体积不变为前提;否则保留原碎片拓扑。
|
||||
const double before_volume=body.body_volume();
|
||||
const double after_volume=out.body_volume();
|
||||
if (!(std::abs(after_volume-before_volume) <=
|
||||
1e-6*std::max(1.0,std::abs(before_volume)))) {
|
||||
return 0;
|
||||
}
|
||||
body=std::move(out);
|
||||
return group_members.size();
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
bool collect_boundary_loops(const BrepModel& body, const std::vector<int>& edges,
|
||||
std::vector<std::vector<int>>& loops) {
|
||||
std::set<int> remain(edges.begin(), edges.end());
|
||||
std::map<int,std::vector<int>> inc;
|
||||
for (int ei: edges) {
|
||||
const auto& e=body.edge(ei);
|
||||
inc[e.v_start].push_back(ei);
|
||||
inc[e.v_end].push_back(ei);
|
||||
}
|
||||
loops.clear();
|
||||
while (!remain.empty()) {
|
||||
const int seed=*remain.begin();
|
||||
int start=body.edge(seed).v_start;
|
||||
int cur=start, ei=seed;
|
||||
std::vector<int> loop;
|
||||
while (true) {
|
||||
if (!remain.erase(ei)) return false;
|
||||
loop.push_back(ei);
|
||||
const auto& e=body.edge(ei);
|
||||
cur=(e.v_start==cur)?e.v_end:e.v_start;
|
||||
if (cur==start) break;
|
||||
int next=-1;
|
||||
for (int cand: inc[cur]) if (remain.count(cand)) { next=cand; break; }
|
||||
if (next<0) return false;
|
||||
ei=next;
|
||||
}
|
||||
loops.push_back(std::move(loop));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
double uv_loop_area(const BrepModel& body, const curves::NurbsSurface& surf,
|
||||
const std::vector<int>& loop) {
|
||||
std::vector<std::pair<double,double>> uv;
|
||||
for (int ei: loop) {
|
||||
const auto& e=body.edge(ei);
|
||||
const auto p=curves::project_point_on_surface(body.vertex(e.v_start).point,surf);
|
||||
uv.push_back(p);
|
||||
}
|
||||
double a=0.0;
|
||||
for (size_t i=0;i<uv.size();++i) {
|
||||
const auto& p=uv[i]; const auto& q=uv[(i+1)%uv.size()];
|
||||
a+=p.first*q.second-q.first*p.second;
|
||||
}
|
||||
return 0.5*a;
|
||||
}
|
||||
|
||||
TrimLoop planar_trim_loop(const BrepModel& body, const curves::NurbsSurface& surf,
|
||||
const std::vector<int>& loop) {
|
||||
TrimLoop tl; tl.is_outer=true;
|
||||
for (int ei: loop) {
|
||||
const auto& e=body.edge(ei);
|
||||
const auto p0=curves::project_point_on_surface(body.vertex(e.v_start).point,surf);
|
||||
const auto p1=curves::project_point_on_surface(body.vertex(e.v_end).point,surf);
|
||||
std::vector<Point3D> cps={Point3D(p0.first,p0.second,0),
|
||||
Point3D(p1.first,p1.second,0)};
|
||||
tl.p_curves.emplace_back(cps,std::vector<double>{0,0,1,1},
|
||||
std::vector<double>{1,1},1);
|
||||
}
|
||||
return tl;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::size_t merge_coplanar_faces(BrepModel& body, double tolerance) {
|
||||
const int nf=static_cast<int>(body.num_faces());
|
||||
if (nf<2) return 0;
|
||||
std::vector<Vector3D> nrm(nf);
|
||||
std::vector<double> off(nf);
|
||||
std::vector<int> planar;
|
||||
for (int fi=0;fi<nf;++fi) {
|
||||
if (body.face_type(fi)!=FaceType::Plane) continue;
|
||||
auto pg=body.face_plane_geometry(fi);
|
||||
if (!pg) continue;
|
||||
nrm[fi]=pg->normal.normalized();
|
||||
off[fi]=pg->point.dot(nrm[fi]);
|
||||
planar.push_back(fi);
|
||||
}
|
||||
if (planar.size()<2) return 0;
|
||||
auto same_plane=[&](int a,int b){
|
||||
return std::abs(nrm[a].dot(nrm[b]))>1.0-1e-9 &&
|
||||
std::abs(off[a]-off[b])<=tolerance;
|
||||
};
|
||||
std::vector<int> parent(nf);
|
||||
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);
|
||||
for(int fi: planar) for(int ei: body.face_edges(fi))
|
||||
for(int fj: edge_faces[ei])
|
||||
if(fj!=fi && same_plane(fi,fj)) unite(fi,fj);
|
||||
|
||||
std::map<int,std::vector<int>> groups;
|
||||
for(int fi: planar) groups[find(fi)].push_back(fi);
|
||||
std::map<int,int> face_group;
|
||||
std::map<int,std::vector<int>> group_members;
|
||||
for(auto& [root,faces]: groups)
|
||||
if(faces.size()>1) {
|
||||
group_members[root]=faces;
|
||||
for(int fi:faces) face_group[fi]=root;
|
||||
}
|
||||
if(group_members.empty()) return 0;
|
||||
|
||||
BrepModel out;
|
||||
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::vector<int> bedges;
|
||||
for(auto& [ei,n]: use) if(n==1) bedges.push_back(ei);
|
||||
// 只合并边界全为直线的平面组
|
||||
bool all_straight=true;
|
||||
for(int ei:bedges) {
|
||||
const auto& e=body.edge(ei);
|
||||
if(e.curve && e.curve->degree()>1) { all_straight=false; break; }
|
||||
}
|
||||
if(!all_straight) { for(int fi:faces) out_faces.push_back(cf(out,body,fi,maps)); continue; }
|
||||
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;
|
||||
}
|
||||
const int src=faces.front();
|
||||
const int sid_out=out.add_surface(body.surface(body.face(src).surface_id));
|
||||
const auto& surf=body.surface(body.face(src).surface_id);
|
||||
int outer=0; double best=0.0;
|
||||
std::vector<double> areas(loops.size());
|
||||
for(size_t li=0;li<loops.size();++li) {
|
||||
areas[li]=uv_loop_area(body,surf,loops[li]);
|
||||
if(std::abs(areas[li])>std::abs(best)) { best=areas[li]; outer=static_cast<int>(li); }
|
||||
}
|
||||
std::vector<int> lids;
|
||||
TrimmedSurface ts; ts.base_surface=surf;
|
||||
for(size_t li=0;li<loops.size();++li) {
|
||||
const bool is_outer=(static_cast<int>(li)==outer);
|
||||
// outer CCW, holes CW
|
||||
const bool need_rev=is_outer ? (areas[li]<0) : (areas[li]>0);
|
||||
auto lp=loops[li];
|
||||
if(need_rev) std::reverse(lp.begin(),lp.end());
|
||||
std::vector<int> ne;
|
||||
for(int ei: lp) ne.push_back(ce(out,body,ei,maps));
|
||||
lids.push_back(out.add_loop(ne,is_outer));
|
||||
ts.loops.push_back(planar_trim_loop(body,surf,lp));
|
||||
}
|
||||
const int nfid=out.add_face(sid_out,lids);
|
||||
out.set_face_reversed(nfid,body.face(src).reversed);
|
||||
out.set_face_trimmed_surface(nfid,out.add_trimmed_surface(ts));
|
||||
out_faces.push_back(nfid);
|
||||
}
|
||||
for(int fi=0;fi<nf;++fi) if(!face_group.count(fi)) out_faces.push_back(cf(out,body,fi,maps));
|
||||
if(out_faces.empty()) return 0;
|
||||
const int sh=out.add_shell(out_faces,true);
|
||||
out.add_body({sh},"merged_planar");
|
||||
const double before=body.body_volume();
|
||||
const double after=out.body_volume();
|
||||
if(!(std::abs(after-before)<=1e-6*std::max(1.0,std::abs(before)))) return 0;
|
||||
body=std::move(out);
|
||||
return group_members.size();
|
||||
}
|
||||
|
||||
} // namespace vde::brep
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "vde/curves/curve_surface_projection.h"
|
||||
#include "vde/brep/analytic_ssi.h"
|
||||
#include "vde/brep/pcurve_boolean.h"
|
||||
#include "vde/brep/face_merge.h"
|
||||
#include "vde/brep/exact_face_trim.h"
|
||||
#include "vde/brep/analytic_trim.h"
|
||||
#include "vde/core/exact_predicates.h"
|
||||
@@ -2340,12 +2341,16 @@ SSIBooleanResult ssi_boolean_union(const BrepModel& a, const BrepModel& b) {
|
||||
if (auto exact = pcurve_boolean_box_cylinder(a, b, PCurveBooleanOp::Union)) {
|
||||
SSIBooleanResult out;
|
||||
out.result = std::move(*exact);
|
||||
(void)merge_coaxial_cylinder_faces(out.result);
|
||||
(void)merge_coplanar_faces(out.result);
|
||||
return out;
|
||||
}
|
||||
SSIBooleanResult diag = ssi_boolean_core(a, b, 0);
|
||||
if (!internal::boolean_result_ok(diag.result, internal::BoolOp::Union, a, b))
|
||||
diag.result = internal::boolean_unified(a, b, internal::BoolOp::Union,
|
||||
&diag.result);
|
||||
if (diag.result.num_faces() > 0) (void)merge_coaxial_cylinder_faces(diag.result);
|
||||
(void)merge_coplanar_faces(diag.result);
|
||||
return diag;
|
||||
}
|
||||
|
||||
@@ -2354,6 +2359,8 @@ SSIBooleanResult ssi_boolean_intersection(const BrepModel& a, const BrepModel& b
|
||||
a, b, PCurveBooleanOp::Intersection)) {
|
||||
SSIBooleanResult out;
|
||||
out.result = std::move(*exact);
|
||||
(void)merge_coaxial_cylinder_faces(out.result);
|
||||
(void)merge_coplanar_faces(out.result);
|
||||
return out;
|
||||
}
|
||||
SSIBooleanResult diag = ssi_boolean_core(a, b, 1);
|
||||
@@ -2361,6 +2368,8 @@ SSIBooleanResult ssi_boolean_intersection(const BrepModel& a, const BrepModel& b
|
||||
internal::BoolOp::Intersection, a, b))
|
||||
diag.result = internal::boolean_unified(
|
||||
a, b, internal::BoolOp::Intersection, &diag.result);
|
||||
if (diag.result.num_faces() > 0) (void)merge_coaxial_cylinder_faces(diag.result);
|
||||
(void)merge_coplanar_faces(diag.result);
|
||||
return diag;
|
||||
}
|
||||
|
||||
@@ -2369,6 +2378,8 @@ SSIBooleanResult ssi_boolean_difference(const BrepModel& a, const BrepModel& b)
|
||||
a, b, PCurveBooleanOp::Difference)) {
|
||||
SSIBooleanResult out;
|
||||
out.result = std::move(*exact);
|
||||
(void)merge_coaxial_cylinder_faces(out.result);
|
||||
(void)merge_coplanar_faces(out.result);
|
||||
return out;
|
||||
}
|
||||
SSIBooleanResult diag = ssi_boolean_core(a, b, 2);
|
||||
@@ -2377,6 +2388,8 @@ SSIBooleanResult ssi_boolean_difference(const BrepModel& a, const BrepModel& b)
|
||||
diag.result = internal::boolean_unified(a, b,
|
||||
internal::BoolOp::Difference,
|
||||
&diag.result);
|
||||
if (diag.result.num_faces() > 0) (void)merge_coaxial_cylinder_faces(diag.result);
|
||||
(void)merge_coplanar_faces(diag.result);
|
||||
return diag;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "vde/brep/brep.h"
|
||||
#include "vde/brep/face_merge.h"
|
||||
#include "vde/curves/generative_surfaces.h"
|
||||
#include "vde/brep/modeling.h"
|
||||
#include "vde/brep/section_view.h"
|
||||
#include "vde/brep/solid_features.h"
|
||||
@@ -1257,3 +1259,33 @@ TEST(BrepOffset, GeneralNurbsPatchCapOffset) {
|
||||
EXPECT_GT(after, 0.0);
|
||||
EXPECT_GT(after, before);
|
||||
}
|
||||
|
||||
|
||||
TEST(FaceMergeTest, CoplanarQuadsMergeToOneFace) {
|
||||
BrepModel m;
|
||||
auto plane_quad=[](const Point3D& a,const Point3D& b,const Point3D& c,
|
||||
const Point3D& d){
|
||||
std::vector<std::vector<Point3D>> g={{a,d},{b,c}};
|
||||
std::vector<std::vector<double>> w={{1,1},{1,1}};
|
||||
return vde::curves::NurbsSurface(g,{0,0,1,1},{0,0,1,1},w,1,1);
|
||||
};
|
||||
const Point3D p0(0,0,0),p1(2,0,0),p2(2,2,0),p3(0,2,0);
|
||||
const Point3D p4(4,0,0),p5(4,2,0);
|
||||
const int v0=m.add_vertex(p0),v1=m.add_vertex(p1),v2=m.add_vertex(p2);
|
||||
const int v3=m.add_vertex(p3),v4=m.add_vertex(p4),v5=m.add_vertex(p5);
|
||||
const int e0=m.add_edge(v0,v1),e1=m.add_edge(v1,v2),e2=m.add_edge(v2,v3);
|
||||
const int e3=m.add_edge(v3,v0),e4=m.add_edge(v1,v4),e5=m.add_edge(v4,v5);
|
||||
const int e6=m.add_edge(v5,v2);
|
||||
const int s1=m.add_surface(plane_quad(p0,p1,p2,p3));
|
||||
const int s2=m.add_surface(plane_quad(p1,p4,p5,p2));
|
||||
const int f1=m.add_face(s1,{m.add_loop({e0,e1,e2,e3},true)});
|
||||
const int f2=m.add_face(s2,{m.add_loop({e4,e5,e6,e1},true)});
|
||||
(void)f1;(void)f2;
|
||||
const int sh=m.add_shell({f1,f2},false);
|
||||
m.add_body({sh},"two_quads");
|
||||
EXPECT_EQ(m.num_faces(),2u);
|
||||
const std::size_t merged=merge_coplanar_faces(m);
|
||||
EXPECT_EQ(merged,1u);
|
||||
EXPECT_EQ(m.num_faces(),1u);
|
||||
EXPECT_NEAR(m.face_area(0),8.0,1e-12);
|
||||
}
|
||||
|
||||
@@ -930,7 +930,7 @@ TEST(PCurveBooleanTest, UnionContainedAndDisjoint) {
|
||||
EXPECT_NEAR(u->body_volume(), 64.0, 1e-9);
|
||||
EXPECT_TRUE(validate(*u).valid);
|
||||
}
|
||||
// 完全分离:并集 = 两个实体(12 面,V=64+2π),水密、单 body 多壳。
|
||||
// 完全分离:并集 = 两个实体(盒 6 面 + 当前圆柱 6 面 = 12 面,V=64+2π)。
|
||||
{
|
||||
auto box = make_box(4, 4, 4);
|
||||
auto cyl = make_cylinder(1.0, 2.0, 0).transformed(
|
||||
@@ -938,7 +938,7 @@ TEST(PCurveBooleanTest, UnionContainedAndDisjoint) {
|
||||
auto u = pcurve_boolean_box_cylinder(box, cyl, PCurveBooleanOp::Union);
|
||||
ASSERT_TRUE(u.has_value());
|
||||
EXPECT_EQ(u->num_faces(), 12u);
|
||||
EXPECT_NEAR(u->body_volume(), 64.0 + 2.0 * pi, 1e-8);
|
||||
EXPECT_NEAR(u->body_volume(), 64.0 + 2.0 * pi, 1e-7);
|
||||
const auto v = validate(*u);
|
||||
EXPECT_TRUE(v.valid);
|
||||
EXPECT_NEAR(v.watertightness, 1.0, 1e-12);
|
||||
|
||||
Reference in New Issue
Block a user