feat(brep): VDE-1181 面替换便捷 API — replace_face_with_plane / replace_face_from_face
- replace_face_with_plane:把选中面替换为过给定点/法向的平面,法向自动与目标面外法向同侧 - replace_face_from_face:源面为平面走平面路径,非平面复制支撑曲面(警告近似) - 平面补丁带投影 p-curve 裁剪环(目标面环顶点投影到补丁参数域),网格化严格裁剪在环边界内, 修复 10% 外扩 AABB + from_rect 全域矩形裁剪导致的共面替换体积虚增(+107.7) - 拓扑重建抽为共享 apply_face_replacement(replace_face 全域矩形 / with_plane 投影环共用) - 法向约定统一:(cp[1][0]-p0)x(cp[0][1]-p0)(u 沿 cp[1][0]-p0、v 沿 cp[0][1]-p0) - test_direct_modeling 43→49 全过;回归 feature_edit/blend/geometry_completion 全绿
This commit is contained in:
@@ -22,6 +22,11 @@
|
||||
- 修复 `face_plane`:法向由支撑面控制点直接推导(`face_edges` 无序导致 Newell 法向为零 → 误报"仅支持平面邻面")
|
||||
- 孔差集工具经 `transformed()` 同时平移拓扑顶点与 NURBS 控制点
|
||||
- 新增测试 `tests/brep/test_feature_edit.cpp` 16 用例全过;回归 test_direct_modeling 43/43 全过
|
||||
- **VDE-1181 面替换便捷 API(Replace Face 闭环)**:新增 `replace_face_with_plane`(把选中面替换为过给定点/法向的平面)与 `replace_face_from_face`(用另一面几何替换当前面)两个 `vde::brep` 入口
|
||||
- 平面补丁带投影 p-curve 裁剪环(目标面环顶点 → 补丁参数域多边形),网格化严格裁剪在环边界内,修复共面替换体积虚增(10% 外扩 AABB + 全域矩形裁剪导致 +107.7)
|
||||
- 拓扑重建抽为共享 `apply_face_replacement`(`replace_face` 全域矩形 / `replace_face_with_plane` 投影环共用)
|
||||
- 法向参数化约定统一为 `(cp[1][0]-p0)×(cp[0][1]-p0)`(u 沿 cp[1][0]-p0、v 沿 cp[0][1]-p0)
|
||||
- 新增测试 6 用例(test_direct_modeling 43→49 全过);回归 test_feature_edit 16/16、test_brep_blend 12/12、test_advanced_blend 32/32、test_geometry_completion 25/25 全过
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
**Source**: ViewDesign DirectEditPanel integration
|
||||
**Module**: `vde/brep/direct_modeling.h` / `vde/brep/solid_features.h`
|
||||
|
||||
**Status**: open
|
||||
**Status**: done(2026-08-31 落地,见下)
|
||||
|
||||
## Current state
|
||||
|
||||
@@ -53,3 +53,19 @@ ViewDesign 的 DirectEditPanel「Replace Face」按钮当前不提供任何目
|
||||
## 影响
|
||||
|
||||
- ViewDesign「Replace Face」按钮当前不可用,需要上述便捷 API 或明确的面选择契约才能达到 SolidWorks 面替换体验。
|
||||
|
||||
## 落地记录(2026-08-31)
|
||||
|
||||
`replace_face_with_plane`(High)与 `replace_face_from_face`(Medium)两项便捷 API 全部实现,ViewDesign 可直接以「面选择 + 过点平面/源面」闭环 Replace Face 命令。
|
||||
|
||||
### 新增 API(`include/vde/brep/direct_modeling.h`)
|
||||
|
||||
- `replace_face_with_plane(body, face_id, plane_point, plane_normal)` — 把选中面替换为过给定点、给定法向的平面(法向自动与目标面外法向同侧对齐)
|
||||
- `replace_face_from_face(body, target_face_id, source_face_id)` — 用另一面的几何替换当前面(源面为平面 → 取源面平面 + 面上一点走平面路径;非平面 → 复制源面支撑曲面 + 全域近似裁剪警告)
|
||||
|
||||
### 实现(`src/brep/direct_modeling.cpp`,`tests/brep/test_direct_modeling.cpp` 49 项测试全绿)
|
||||
|
||||
- 平面补丁裁剪:目标面边界环顶点有序化 → 投影到补丁 (u,v) 参数域 → 生成 p-curve 线性裁剪环(参照 `add_boss_direct` 顶盖模式)。网格化时严格裁剪在环边界内,**不会溢出到补丁外缘**(最初实现用 10% 外扩 AABB + `from_rect` 全域矩形裁剪,共面替换体积虚增 107.7)。
|
||||
- 拓扑重建抽为共享辅助 `apply_face_replacement`:`replace_face`(全域矩形裁剪)与 `replace_face_with_plane`(投影环裁剪)共用,其余面曲面/裁剪面原样复制。
|
||||
- 法向参数化约定修正:`make_plane_surface` 的 u 沿 `cp[1][0]-p0`、v 沿 `cp[0][1]-p0`,法向 = `(cp[1][0]-p0)×(cp[0][1]-p0)`——`replace_face_from_face` 源面法向推导与测试辅助 `support_face_normal` 均按此约定(叉积顺序相反会得到法向取反)。
|
||||
- 回归:test_feature_edit 16/16、test_brep_blend 12/12、test_advanced_blend 32/32、test_geometry_completion 25/25 全过。
|
||||
|
||||
@@ -123,7 +123,7 @@ F0 布尔内核回归(当前,本轮)── 阻塞 F1/F2 回归
|
||||
|
||||
## 五、VD 工程反馈新需求(2026-08-31 同步 integration/vde-adapt)
|
||||
|
||||
> 同步 VDE-1180~1191(12 项,commit e0d039e1)。高优大项按序实施:VDE-1191 / VDE-1190 / VDE-1189 / VDE-1180 已完成,后续 VDE-1181 / VDE-1182 / VDE-1186 / VDE-1187 / VDE-1188。
|
||||
> 同步 VDE-1180~1191(12 项,commit e0d039e1)。高优大项按序实施:VDE-1191 / VDE-1190 / VDE-1189 / VDE-1180 / VDE-1181 已完成,后续 VDE-1182 / VDE-1186 / VDE-1187 / VDE-1188。
|
||||
|
||||
### VDE-1191:Boolean Split / Direct Modeling 面操作 / Shape 值语义与稳定拓扑 ID(High,当前实施)
|
||||
|
||||
@@ -197,3 +197,21 @@ F0 布尔内核回归(当前,本轮)── 阻塞 F1/F2 回归
|
||||
- 顺带修复 `rebuild_with_offset` 挤出模式:偏移目标面改为复制原曲面平移控制点 + 保留裁剪环(裁剪面 `base_surface` 同步替换),修复非四边形/裁剪顶盖求积失真。
|
||||
- `face_plane` 法向改由支撑面控制点推导,规避 `face_edges` 无序导致 Newell 法向为零。
|
||||
- [2026-08-31] **测试**:`tests/brep/test_feature_edit.cpp` 16 用例全过(参数查询/孔半径·深度编辑/凸台高度编辑/圆角编辑/复制孔/复制凸台/非法输入);回归 test_direct_modeling 43/43 全过。
|
||||
|
||||
### VDE-1181:Face replacement needs target surface selection / convenience API(High,已实施)
|
||||
|
||||
来源 ViewDesign DirectEditPanel「Replace Face」按钮需调用方显式构造替换 NURBS 曲面才能闭环(详见 docs/feedback/VDE-1181.md):
|
||||
|
||||
| 增强 | 内容 | 落地范围 |
|
||||
|------|------|---------|
|
||||
| 1 | 平面替换便捷 API | `replace_face_with_plane`:把选中面替换为过给定点、给定法向的平面(法向自动与目标面外法向同侧) |
|
||||
| 2 | 用现有面作为替换源 | `replace_face_from_face`:源面为平面走平面路径,非平面复制支撑曲面(警告近似) |
|
||||
| 3 | 明确 UI 契约 | 便捷 API 已落地,ViewDesign 无需再构造平面 NURBS 曲面 |
|
||||
|
||||
实施记录:
|
||||
|
||||
- [2026-08-31] **新增 `vde::brep` API**:`include/vde/brep/direct_modeling.h` 两个便捷入口,实现于 `src/brep/direct_modeling.cpp`(详见 VDE-1181.md 落地记录)。
|
||||
- 平面补丁带投影 p-curve 裁剪环(目标面环顶点有序化 → 投影补丁参数域 → 线性裁剪环),网格化严格裁剪在环边界内;初始 10% 外扩 AABB + `from_rect` 全域矩形裁剪会导致共面替换体积虚增 107.7。
|
||||
- 拓扑重建抽为共享 `apply_face_replacement`(`replace_face` 与 `replace_face_with_plane` 共用),其余面曲面/裁剪面原样复制。
|
||||
- 法向约定:`(cp[1][0]-p0)×(cp[0][1]-p0)`(u 沿 cp[1][0]-p0、v 沿 cp[0][1]-p0)。
|
||||
- [2026-08-31] **测试**:`tests/brep/test_direct_modeling.cpp` 49 用例全过(新增 6 项:共面替换体积不变/拓扑与环保持/倾斜平面/非法输入/平面源面替换/非法输入);回归 test_feature_edit 16/16、test_brep_blend 12/12、test_advanced_blend 32/32、test_geometry_completion 25/25 全过。
|
||||
|
||||
@@ -135,6 +135,44 @@ struct DirectModelingResult {
|
||||
int face_id,
|
||||
const curves::NurbsSurface& new_surface);
|
||||
|
||||
/**
|
||||
* @brief 平面替换便捷 API:将目标面替换为过给定点的平面(VDE-1181)
|
||||
*
|
||||
* 无需调用方构造 NURBS 曲面。平面法向自动调整为与目标面外法向同侧,
|
||||
* 平面补丁自动覆盖目标面边界(含外扩余量),边界拓扑保持不变。
|
||||
*
|
||||
* @param body 输入 B-Rep 实体
|
||||
* @param face_id 目标面索引
|
||||
* @param plane_point 平面通过点
|
||||
* @param plane_normal 平面法向(方向无关,将按目标面外法向对齐)
|
||||
* @return DirectModelingResult 含新模型和诊断
|
||||
*
|
||||
* @code{.cpp}
|
||||
* auto box = make_box(10, 10, 10);
|
||||
* // 顶面替换为过 (0,0,6) 的水平平面 → 盒高 11
|
||||
* auto r = replace_face_with_plane(box, 1, {0,0,6}, {0,0,1});
|
||||
* @endcode
|
||||
*/
|
||||
[[nodiscard]] DirectModelingResult replace_face_with_plane(
|
||||
const BrepModel& body, int face_id,
|
||||
const core::Point3D& plane_point,
|
||||
const core::Vector3D& plane_normal);
|
||||
|
||||
/**
|
||||
* @brief 用另一面的几何替换目标面(VDE-1181)
|
||||
*
|
||||
* - 源面为平面:取源面平面(法向 + 面上一点)替换目标面(覆盖目标边界)。
|
||||
* - 源面为非平面:复制源面支撑曲面替换目标面,目标边界裁剪为全域近似
|
||||
* (与原 replace_face 语义一致,附 warning)。
|
||||
*
|
||||
* @param body 输入 B-Rep 实体
|
||||
* @param target_face_id 目标面索引
|
||||
* @param source_face_id 源面索引(提供替换几何)
|
||||
* @return DirectModelingResult 含新模型和诊断
|
||||
*/
|
||||
[[nodiscard]] DirectModelingResult replace_face_from_face(
|
||||
const BrepModel& body, int target_face_id, int source_face_id);
|
||||
|
||||
/**
|
||||
* @brief 推拉操作:沿法线挤出(正值)或切除(负值)
|
||||
*
|
||||
|
||||
+188
-29
@@ -567,33 +567,15 @@ DirectModelingResult rotate_face(const BrepModel& body, int face_id,
|
||||
return result;
|
||||
}
|
||||
|
||||
DirectModelingResult replace_face(const BrepModel& body, int face_id,
|
||||
const curves::NurbsSurface& new_surface) {
|
||||
/// 内部:复制体拓扑,将 face_id 面的曲面替换为 trimmed(其余面曲面不变)。
|
||||
/// 支持自定义裁剪环:replace_face 用全域矩形,replace_face_with_plane 用
|
||||
/// 目标面边界投影的 p-curve 环——网格化严格裁剪在环边界内,不会溢出到
|
||||
/// 补丁外缘(否则体积/面积虚高)。
|
||||
static DirectModelingResult apply_face_replacement(const BrepModel& body, int face_id,
|
||||
const TrimmedSurface& trimmed) {
|
||||
DirectModelingResult result;
|
||||
result.operation = "replace_face";
|
||||
result.affected_faces = 1;
|
||||
|
||||
if (face_id < 0 || face_id >= static_cast<int>(body.num_faces())) {
|
||||
result.errors.push_back("replace_face: face_id " + std::to_string(face_id) +
|
||||
" out of range");
|
||||
return result;
|
||||
}
|
||||
|
||||
// Check boundary compatibility
|
||||
auto edges = body.face_edges(face_id);
|
||||
if (!edges.empty()) {
|
||||
const auto& e0 = body.edge(edges[0]);
|
||||
Point3D p_corner = body.vertex(e0.v_start).point;
|
||||
Point3D s00 = new_surface.evaluate(0.0, 0.0);
|
||||
(void)new_surface.evaluate(1.0, 1.0); // verify surface is evaluable
|
||||
|
||||
double dist_corner = (s00 - p_corner).norm();
|
||||
if (dist_corner > 10.0) {
|
||||
result.warnings.push_back("replace_face: new surface corner (0,0) is far "
|
||||
"from face corner, boundary may not match");
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild model with replaced surface
|
||||
BrepModel dst;
|
||||
|
||||
// Copy vertices
|
||||
@@ -624,9 +606,8 @@ DirectModelingResult replace_face(const BrepModel& body, int face_id,
|
||||
auto loop_idx = remap_loop_ids(loop_id_map, src_face);
|
||||
|
||||
if (cur_fi == face_id) {
|
||||
int new_sid = dst.add_surface(new_surface);
|
||||
auto ts = TrimmedSurface::from_rect(new_surface, 0.0, 1.0, 0.0, 1.0);
|
||||
int new_tid = dst.add_trimmed_surface(ts);
|
||||
int new_sid = dst.add_surface(trimmed.base_surface);
|
||||
int new_tid = dst.add_trimmed_surface(trimmed);
|
||||
int fid = dst.add_face(new_sid, loop_idx);
|
||||
dst.set_face_trimmed_surface(fid, new_tid);
|
||||
new_face_ids.push_back(fid);
|
||||
@@ -641,7 +622,6 @@ DirectModelingResult replace_face(const BrepModel& body, int face_id,
|
||||
auto val_result = validate(dst);
|
||||
result.success = val_result.valid;
|
||||
result.body = std::move(dst);
|
||||
result.affected_faces = 1;
|
||||
if (!val_result.valid) {
|
||||
for (const auto& e : val_result.errors)
|
||||
result.warnings.push_back("Validation: " + e);
|
||||
@@ -649,6 +629,185 @@ DirectModelingResult replace_face(const BrepModel& body, int face_id,
|
||||
return result;
|
||||
}
|
||||
|
||||
DirectModelingResult replace_face(const BrepModel& body, int face_id,
|
||||
const curves::NurbsSurface& new_surface) {
|
||||
DirectModelingResult result;
|
||||
result.operation = "replace_face";
|
||||
|
||||
if (face_id < 0 || face_id >= static_cast<int>(body.num_faces())) {
|
||||
result.errors.push_back("replace_face: face_id " + std::to_string(face_id) +
|
||||
" out of range");
|
||||
return result;
|
||||
}
|
||||
|
||||
// Check boundary compatibility
|
||||
auto edges = body.face_edges(face_id);
|
||||
if (!edges.empty()) {
|
||||
const auto& e0 = body.edge(edges[0]);
|
||||
Point3D p_corner = body.vertex(e0.v_start).point;
|
||||
Point3D s00 = new_surface.evaluate(0.0, 0.0);
|
||||
(void)new_surface.evaluate(1.0, 1.0); // verify surface is evaluable
|
||||
|
||||
double dist_corner = (s00 - p_corner).norm();
|
||||
if (dist_corner > 10.0) {
|
||||
result.warnings.push_back("replace_face: new surface corner (0,0) is far "
|
||||
"from face corner, boundary may not match");
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild model with replaced surface
|
||||
auto trimmed = TrimmedSurface::from_rect(new_surface, 0.0, 1.0, 0.0, 1.0);
|
||||
return apply_face_replacement(body, face_id, trimmed);
|
||||
}
|
||||
|
||||
/// 构造"覆盖 face_id 边界的平面补丁"裁剪面。
|
||||
/// 平面过 base、法向 n(单位化);基面为 2×2 平面补丁(略大于环 AABB,
|
||||
/// 保证环投影落在参数域内部),裁剪环 = 目标面各环顶点投影到补丁参数域的
|
||||
/// 多边形(p-curve 线性段)。网格化时环裁剪严格限制在目标边界内,不会
|
||||
/// 溢出到补丁外缘导致体积/面积虚高。返回曲面法向 = n 方向。
|
||||
static TrimmedSurface plane_patch_trimmed_surface(const BrepModel& body, int face_id,
|
||||
const core::Point3D& base,
|
||||
const core::Vector3D& n_plane) {
|
||||
const Vector3D n = n_plane.normalized();
|
||||
const Vector3D ref = (std::abs(n.x()) < 0.9) ? Vector3D(1, 0, 0) : Vector3D(0, 1, 0);
|
||||
const Vector3D u = ref.cross(n).normalized(); // u ⊥ n
|
||||
const Vector3D v = n.cross(u); // u × v = n
|
||||
|
||||
double umin = 1e30, umax = -1e30, vmin = 1e30, vmax = -1e30;
|
||||
for (int vid : collect_face_vertices(body, face_id)) {
|
||||
const Vector3D d = body.vertex(vid).point - base;
|
||||
umin = std::min(umin, d.dot(u));
|
||||
umax = std::max(umax, d.dot(u));
|
||||
vmin = std::min(vmin, d.dot(v));
|
||||
vmax = std::max(vmax, d.dot(v));
|
||||
}
|
||||
const double du = umax - umin, dv = vmax - vmin;
|
||||
const double margin = 0.05 * std::sqrt(du * du + dv * dv) + 1e-3;
|
||||
|
||||
const Point3D p0 = base + u * (umin - margin) + v * (vmin - margin);
|
||||
const Point3D p1 = base + u * (umax + margin) + v * (vmin - margin);
|
||||
const Point3D p2 = base + u * (umax + margin) + v * (vmax + margin);
|
||||
const Point3D p3 = base + u * (umin - margin) + v * (vmax + margin);
|
||||
curves::NurbsSurface surf = make_plane_surface(p0, p1, p2, p3);
|
||||
|
||||
TrimmedSurface ts = TrimmedSurface::from_surface(surf);
|
||||
const double du_p = du + 2.0 * margin, dv_p = dv + 2.0 * margin;
|
||||
|
||||
// 目标面各环顶点有序化 → 投影到补丁参数域 → p-curve 线性裁剪环
|
||||
for (int li : body.face(face_id).loops) {
|
||||
const auto& loop = body.loop_by_id(li);
|
||||
if (loop.edges.empty()) continue;
|
||||
std::vector<Point3D> pts;
|
||||
int cur = body.edge(loop.edges[0]).v_start;
|
||||
pts.push_back(body.vertex_by_id(cur).point);
|
||||
std::vector<char> used(loop.edges.size(), 0);
|
||||
for (size_t step = 0; step < loop.edges.size() && pts.size() <= loop.edges.size();
|
||||
++step) {
|
||||
bool found = false;
|
||||
for (size_t k = 0; k < loop.edges.size(); ++k) {
|
||||
if (used[k]) continue;
|
||||
const auto& e = body.edge(loop.edges[k]);
|
||||
if (e.v_start == cur) cur = e.v_end;
|
||||
else if (e.v_end == cur) cur = e.v_start;
|
||||
else continue;
|
||||
used[k] = 1; found = true; break;
|
||||
}
|
||||
if (!found) break;
|
||||
pts.push_back(body.vertex_by_id(cur).point);
|
||||
}
|
||||
if (pts.size() >= 2 && (pts.front() - pts.back()).norm() < 1e-9)
|
||||
pts.pop_back();
|
||||
if (pts.size() < 3) continue;
|
||||
|
||||
TrimLoop trim;
|
||||
trim.is_outer = loop.is_outer;
|
||||
for (size_t k = 0; k < pts.size(); ++k) {
|
||||
const auto& pa = pts[k];
|
||||
const auto& pb = pts[(k + 1) % pts.size()];
|
||||
const double ua = ((pa - base).dot(u) - (umin - margin)) / du_p;
|
||||
const double va = ((pa - base).dot(v) - (vmin - margin)) / dv_p;
|
||||
const double ub = ((pb - base).dot(u) - (umin - margin)) / du_p;
|
||||
const double vb = ((pb - base).dot(v) - (vmin - margin)) / dv_p;
|
||||
std::vector<Point3D> cps = {Point3D(ua, va, 0.0), Point3D(ub, vb, 0.0)};
|
||||
std::vector<double> knots = {0.0, 0.0, 1.0, 1.0};
|
||||
std::vector<double> weights = {1.0, 1.0};
|
||||
trim.p_curves.push_back(PCurve(cps, knots, weights, 1));
|
||||
}
|
||||
ts.loops.push_back(std::move(trim));
|
||||
}
|
||||
return ts;
|
||||
}
|
||||
|
||||
DirectModelingResult replace_face_with_plane(const BrepModel& body, int face_id,
|
||||
const core::Point3D& plane_point,
|
||||
const core::Vector3D& plane_normal) {
|
||||
DirectModelingResult result;
|
||||
result.operation = "replace_face_with_plane";
|
||||
if (face_id < 0 || face_id >= static_cast<int>(body.num_faces())) {
|
||||
result.errors.push_back("replace_face_with_plane: face_id " +
|
||||
std::to_string(face_id) + " 越界");
|
||||
return result;
|
||||
}
|
||||
if (plane_normal.norm() < 1e-12) {
|
||||
result.errors.push_back("replace_face_with_plane: 平面法向为零");
|
||||
return result;
|
||||
}
|
||||
// 法向与目标面外法向同侧(避免替换后面朝向翻转)
|
||||
Vector3D n = plane_normal.normalized();
|
||||
const Vector3D old_n = compute_face_normal(body, face_id);
|
||||
if (old_n.dot(n) < 0) n = -n;
|
||||
|
||||
const auto ts = plane_patch_trimmed_surface(body, face_id, plane_point, n);
|
||||
auto r = apply_face_replacement(body, face_id, ts);
|
||||
r.operation = "replace_face_with_plane";
|
||||
return r;
|
||||
}
|
||||
|
||||
DirectModelingResult replace_face_from_face(const BrepModel& body, int target_face_id,
|
||||
int source_face_id) {
|
||||
DirectModelingResult result;
|
||||
result.operation = "replace_face_from_face";
|
||||
const int nf = static_cast<int>(body.num_faces());
|
||||
if (target_face_id < 0 || target_face_id >= nf || source_face_id < 0 ||
|
||||
source_face_id >= nf) {
|
||||
result.errors.push_back("replace_face_from_face: face_id 越界");
|
||||
return result;
|
||||
}
|
||||
if (target_face_id == source_face_id) {
|
||||
result.errors.push_back("replace_face_from_face: 源面与目标面相同");
|
||||
return result;
|
||||
}
|
||||
|
||||
// 源面为平面 → 取源面平面(法向 + 面上一点)替换目标面
|
||||
const auto& src_surf = body.surface(body.face(source_face_id).surface_id);
|
||||
const auto& cp = src_surf.control_points();
|
||||
if (cp.size() >= 2 && cp[0].size() >= 2) {
|
||||
const Point3D p0 = cp[0][0];
|
||||
// make_plane_surface 参数化:u 沿 cp[1][0]-p0、v 沿 cp[0][1]-p0 → 法向 = u×v
|
||||
const Vector3D n0 = (cp[1][0] - p0).cross(cp[0][1] - p0);
|
||||
const double len = n0.norm();
|
||||
if (len > 1e-12) {
|
||||
const Vector3D n0u = n0 / len;
|
||||
const double tol = body.bounds().extent().norm() * 1e-6 + 1e-9;
|
||||
bool planar = true;
|
||||
for (const auto& row : cp)
|
||||
for (const auto& p : row)
|
||||
if (std::abs((p - p0).dot(n0u)) > tol) { planar = false; break; }
|
||||
if (planar) {
|
||||
auto r = replace_face_with_plane(body, target_face_id, p0, n0u);
|
||||
r.operation = "replace_face_from_face";
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 非平面源面:复制源面支撑曲面,目标边界裁剪为全域(与原 replace_face 一致)
|
||||
auto r = replace_face(body, target_face_id, src_surf);
|
||||
if (r.success)
|
||||
r.warnings.push_back("非平面源面:已复制源面支撑曲面,目标边界裁剪为全域近似");
|
||||
r.operation = "replace_face_from_face";
|
||||
return r;
|
||||
}
|
||||
|
||||
DirectModelingResult push_pull(const BrepModel& body, int face_id, double distance) {
|
||||
DirectModelingResult result;
|
||||
result.operation = "push_pull";
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "vde/brep/modeling.h"
|
||||
#include "vde/brep/direct_modeling.h"
|
||||
#include "vde/brep/brep_validate.h"
|
||||
#include "vde/brep/measure.h"
|
||||
#include "vde/core/transform.h"
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
@@ -166,6 +167,102 @@ TEST(DirectModelingTest, ReplaceFace_InvalidFaceId) {
|
||||
EXPECT_FALSE(result.success);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
// VDE-1181 — 面替换便捷 API(replace_face_with_plane / from_face)
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
|
||||
namespace {
|
||||
/// 返回目标面的支撑面法向(控制点叉积,未对齐朝向)
|
||||
Vector3D support_face_normal(const BrepModel& b, int fid) {
|
||||
const auto& s = b.face_surface(fid);
|
||||
const auto& cp = s.control_points();
|
||||
if (cp.size() < 2 || cp[0].size() < 2) return Vector3D::Zero();
|
||||
// make_plane_surface 参数化:u 沿 cp[1][0]-p0、v 沿 cp[0][1]-p0 → 法向 = u×v
|
||||
Vector3D n = (cp[1][0] - cp[0][0]).cross(cp[0][1] - cp[0][0]);
|
||||
return n.norm() > 1e-12 ? n.normalized() : Vector3D::Zero();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(DirectModelingTest, ReplaceFaceWithPlane_BoundaryMatched) {
|
||||
auto box = make_box(10, 10, 10); // z ∈ [-5, 5]
|
||||
// 顶面(f1,z=5)替换为过 (0,0,5) 的水平平面 → 与原面共面,体积不变
|
||||
auto r = replace_face_with_plane(box, 1, {0, 0, 5}, {0, 0, 1});
|
||||
ASSERT_TRUE(r.success) << (r.errors.empty() ? "no error" : r.errors[0]);
|
||||
EXPECT_TRUE(validate(r.body).valid);
|
||||
EXPECT_EQ(r.body.num_faces(), box.num_faces());
|
||||
EXPECT_NEAR(volume(r.body), volume(box), 1e-3);
|
||||
}
|
||||
|
||||
TEST(DirectModelingTest, ReplaceFaceWithPlane_ShiftTopologyKept) {
|
||||
auto box = make_box(10, 10, 10);
|
||||
// 顶面替换为 z=6 平面(平面高于原环)→ 拓扑保持、边界不变
|
||||
auto r = replace_face_with_plane(box, 1, {0, 0, 6}, {0, 0, 1});
|
||||
ASSERT_TRUE(r.success) << (r.errors.empty() ? "no error" : r.errors[0]);
|
||||
EXPECT_TRUE(validate(r.body).valid);
|
||||
EXPECT_EQ(r.body.num_faces(), box.num_faces());
|
||||
// 目标面支撑面为 z=6 平面,法向 +Z
|
||||
Vector3D n = support_face_normal(r.body, 1);
|
||||
EXPECT_NEAR(n.dot(Vector3D::UnitZ()), 1.0, 1e-6);
|
||||
// 边界环顶点保持 z=5
|
||||
const auto& f = r.body.face(1);
|
||||
bool corner_kept = false;
|
||||
for (int li : f.loops) {
|
||||
const auto& lp = r.body.loop_by_id(li);
|
||||
for (int ei : lp.edges) {
|
||||
const auto& e = r.body.edge(ei);
|
||||
if (std::abs(r.body.vertex_by_id(e.v_start).point.z() - 5.0) < 1e-9)
|
||||
corner_kept = true;
|
||||
}
|
||||
}
|
||||
EXPECT_TRUE(corner_kept);
|
||||
}
|
||||
|
||||
TEST(DirectModelingTest, ReplaceFaceWithPlane_Inclined) {
|
||||
auto box = make_box(10, 10, 10);
|
||||
// 顶面替换为法向 (0,1,1) 的倾斜平面(过原点附近的面上点)
|
||||
auto r = replace_face_with_plane(box, 1, {0, 0, 5}, {0, 1, 1});
|
||||
ASSERT_TRUE(r.success) << (r.errors.empty() ? "no error" : r.errors[0]);
|
||||
EXPECT_TRUE(validate(r.body).valid);
|
||||
EXPECT_EQ(r.body.num_faces(), box.num_faces());
|
||||
Vector3D n = support_face_normal(r.body, 1);
|
||||
const double cos_a = std::abs(n.dot(Vector3D(0, 1, 1).normalized()));
|
||||
EXPECT_NEAR(cos_a, 1.0, 1e-6);
|
||||
}
|
||||
|
||||
TEST(DirectModelingTest, ReplaceFaceWithPlane_InvalidInputs) {
|
||||
auto box = make_box(10, 10, 10);
|
||||
auto r1 = replace_face_with_plane(box, 99, {0, 0, 5}, {0, 0, 1});
|
||||
EXPECT_FALSE(r1.success);
|
||||
EXPECT_FALSE(r1.errors.empty());
|
||||
auto r2 = replace_face_with_plane(box, 1, {0, 0, 5}, {0, 0, 0});
|
||||
EXPECT_FALSE(r2.success);
|
||||
EXPECT_FALSE(r2.errors.empty());
|
||||
}
|
||||
|
||||
TEST(DirectModelingTest, ReplaceFaceFromFace_PlaneSource) {
|
||||
auto box = make_box(10, 10, 10);
|
||||
// 顶面 f1(z=5)用底面 f0(z=-5 平面)替换 → 目标面变为源面平面
|
||||
auto r = replace_face_from_face(box, 1, 0);
|
||||
ASSERT_TRUE(r.success) << (r.errors.empty() ? "no error" : r.errors[0]);
|
||||
EXPECT_TRUE(validate(r.body).valid);
|
||||
EXPECT_EQ(r.body.num_faces(), box.num_faces());
|
||||
Vector3D n = support_face_normal(r.body, 1);
|
||||
EXPECT_NEAR(std::abs(n.dot(Vector3D::UnitZ())), 1.0, 1e-6);
|
||||
// 源面平面位置 z=-5:目标面支撑面过 z=-5
|
||||
const auto& cp = r.body.face_surface(1).control_points();
|
||||
EXPECT_NEAR(cp[0][0].z(), -5.0, 1e-6);
|
||||
}
|
||||
|
||||
TEST(DirectModelingTest, ReplaceFaceFromFace_InvalidInputs) {
|
||||
auto box = make_box(10, 10, 10);
|
||||
auto r1 = replace_face_from_face(box, 1, 1); // 源=目标
|
||||
EXPECT_FALSE(r1.success);
|
||||
EXPECT_FALSE(r1.errors.empty());
|
||||
auto r2 = replace_face_from_face(box, 99, 0); // 越界
|
||||
EXPECT_FALSE(r2.success);
|
||||
EXPECT_FALSE(r2.errors.empty());
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
// push_pull — 推拉测试
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
|
||||
Reference in New Issue
Block a user