From bddfe427ef73df6043392fdaaab530b91e333c8c Mon Sep 17 00:00:00 2001 From: hm-thinkbook16p Date: Wed, 19 Aug 2026 20:27:55 +0800 Subject: [PATCH] feat(brep): draft_face kernel - tilt coplanar face group around neutral line (F494) DraftResult draft_face(body, face_index, angle_deg, pull_dir) pivots the planar face group (flood-filled through shared vertices in the same plane) about the neutral line through its centroid: each boundary vertex moves h*tan(angle) along the pull-direction projection, topology unchanged. Rebuild every face's plane surface from its post-draft loop vertices so the tessellated mesh actually reflects the tilt (to_mesh samples the surface, ignoring loops - reusing the original plane kept the draft invisible). --- include/vde/brep/modeling.h | 24 ++++++ src/brep/modeling.cpp | 147 ++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) diff --git a/include/vde/brep/modeling.h b/include/vde/brep/modeling.h index c069e7091..829f78ff8 100644 --- a/include/vde/brep/modeling.h +++ b/include/vde/brep/modeling.h @@ -548,6 +548,30 @@ struct FaceThickness { const std::vector& thicknesses, double default_thickness = 0.0); +/// 拔模结果 +struct DraftResult { + bool success = false; ///< 是否成功 + BrepModel body; ///< 拔模后的实体(成功时有效) +}; + +/// 拔模(对标 SolidWorks Face Draft) +/// +/// 将 face_index 所在平面(含与其共面且相连的所有三角面)绕中性线旋转 +/// angle_deg 度。中性线为过面中心的、垂直于 pull_dir 的平面与面平面的交线; +/// 每个边界顶点沿 pull_dir 在面平面内的投影方向移动 h·tan(angle),其中 h +/// 为该顶点相对面中心沿 pull_dir 的高度。共享这些顶点的相邻面随之拉伸, +/// 拓扑保持不变(仅顶点位置变化)。 +/// +/// @param body 输入实体 +/// @param face_index 要拔模的面 ID +/// @param angle_deg 拔模角度(度) +/// @param pull_dir 拔模方向(零向量时取 +Z) +/// @return 拔模结果 +/// +/// @see shell 抽壳 +[[nodiscard]] DraftResult draft_face(const BrepModel& body, int face_index, + double angle_deg, const Vector3D& pull_dir); + // ═══════════════════════════════════════════════ // 拓扑查询(基于几何位置) // ═══════════════════════════════════════════════ diff --git a/src/brep/modeling.cpp b/src/brep/modeling.cpp index bd86687d7..bf9eec2e5 100644 --- a/src/brep/modeling.cpp +++ b/src/brep/modeling.cpp @@ -1681,6 +1681,153 @@ BrepModel shell_multi(const BrepModel& body, int open_face, return result; } +// ── Draft (拔模) ── +DraftResult draft_face(const BrepModel& body, int face_index, double angle_deg, + const Vector3D& pull_dir) { + DraftResult res; + if (face_index < 0 || face_index >= static_cast(body.num_faces())) + return res; + const double ang = angle_deg * M_PI / 180.0; + if (std::abs(ang) < 1e-9) { res.success = true; res.body = body; return res; } + + Vector3D n0 = compute_face_normal(body, face_index); + if (n0.norm() < 1e-9) return res; + n0 = n0.normalized(); + + Vector3D d = pull_dir; + if (d.norm() < 1e-9) d = Vector3D::UnitZ(); + d = d.normalized(); + + // 1. Coplanar face group. Per-triangle imports (shapeMeshToBrep) split one + // box face into two triangles with duplicated edges, so connectivity is + // by shared *vertices*, not shared edges. Flood-fill every face that + // touches the seed through a common vertex AND lies in the same plane, + // so the whole planar face tilts together. + std::vector> vf(body.num_vertices()); // vertex -> incident faces + for (size_t fi = 0; fi < body.num_faces(); ++fi) + for (int ei : body.face_edges(static_cast(fi))) { + const auto& e = body.edge(ei); + vf[e.v_start].push_back(static_cast(fi)); + vf[e.v_end].push_back(static_cast(fi)); + } + std::vector group; + std::vector visited(body.num_faces(), 0); + std::vector stack{face_index}; + visited[face_index] = 1; + while (!stack.empty()) { + const int fi = stack.back(); stack.pop_back(); + group.push_back(fi); + for (int ei : body.face_edges(fi)) { + const auto& e = body.edge(ei); + for (int fj : vf[e.v_start]) { + if (visited[fj]) continue; + Vector3D nj = compute_face_normal(body, fj); + if (nj.norm() > 1e-9 && n0.dot(nj.normalized()) > 0.9995) { + visited[fj] = 1; + stack.push_back(fj); + } + } + for (int fj : vf[e.v_end]) { + if (visited[fj]) continue; + Vector3D nj = compute_face_normal(body, fj); + if (nj.norm() > 1e-9 && n0.dot(nj.normalized()) > 0.9995) { + visited[fj] = 1; + stack.push_back(fj); + } + } + } + } + + // 2. Boundary vertices of the group. + std::vector bvids; + { + std::map in_group; + for (int fi : group) + for (int ei : body.face_edges(fi)) { + const auto& e = body.edge(ei); + in_group[e.v_start] = 1; + in_group[e.v_end] = 1; + } + for (const auto& kv : in_group) bvids.push_back(kv.first); + } + if (bvids.empty()) return res; + + // 3. Centroid of the boundary ring. + Point3D c(0, 0, 0); + for (int vid : bvids) c = c + body.vertex(vid).point; + c = c / static_cast(bvids.size()); + + // 4. Lateral movement direction inside the face plane. + Vector3D m = d - n0 * (d.dot(n0)); + if (m.norm() < 1e-9) { + // Pull direction parallel to the face normal: nothing to draft. + res.success = true; res.body = body; return res; + } + m = m.normalized(); + const double tanA = std::tan(ang); + + // 5. New positions: vertex moves by h*tan(angle) along m, where h is the + // signed height above the ring centroid along the pull direction. + std::map moved; + double maxDisp = 0.0; + for (int vid : bvids) { + const Point3D& p = body.vertex(vid).point; + const double h = (p - c).dot(d); + const Point3D np = p + m * (h * tanA); + moved[vid] = np; + maxDisp = std::max(maxDisp, (np - p).norm()); + } + if (maxDisp < 1e-9) { res.success = true; res.body = body; return res; } + + // 6. Rebuild: identical topology, only boundary-vertex positions change. + BrepModel result; + std::map vid2new; + for (size_t i = 0; i < body.num_vertices(); ++i) { + const int vid = body.vertex(static_cast(i)).id; + auto it = moved.find(vid); + vid2new[vid] = result.add_vertex( + it != moved.end() ? it->second : body.vertex(static_cast(i)).point); + } + std::vector fids; + for (size_t fi = 0; fi < body.num_faces(); ++fi) { + const auto& face = body.face(static_cast(fi)); + // Collect the face-loop's vertex positions AFTER the draft move. + std::vector loop_pts; + for (int ei : body.face_edges(static_cast(fi))) { + const auto& e = body.edge(ei); + auto it = moved.find(e.v_start); + loop_pts.push_back(it != moved.end() ? it->second + : body.vertex(e.v_start).point); + } + // Rebuild planar faces from their moved loop so the tessellated mesh + // reflects the tilt. (to_mesh tessellates the *surface*, ignoring the + // loop — reusing the original plane would keep the draft invisible.) + curves::NurbsSurface surf = body.surface(face.surface_id); + if (loop_pts.size() >= 3) { + Vector3D qn = (loop_pts[1] - loop_pts[0]).cross(loop_pts[2] - loop_pts[0]); + if (qn.norm() > 1e-9) { + if (loop_pts.size() == 3) loop_pts.push_back(loop_pts[0]); + surf = make_plane_surface(loop_pts[0], loop_pts[1], + loop_pts[2], loop_pts[3]); + } + } + std::vector nes; + for (int ei : body.face_edges(static_cast(fi))) { + const auto& e = body.edge(ei); + nes.push_back(result.add_edge(vid2new[e.v_start], vid2new[e.v_end])); + } + int lp = result.add_loop(nes, true); + fids.push_back(result.add_face(result.add_surface(surf), {lp})); + } + if (!fids.empty()) { + int sh = result.add_shell(fids, true); + result.add_body({sh}, "draft"); + } + res.success = true; + res.body = std::move(result); + return res; +} + // ═══════════════════════════════════════════════════════ // Position-based topology queries (VDE-022) // ═══════════════════════════════════════════════════════