Files
ViewDesignEngine/src/vde_ui/selection_manager.cpp
T
hm a1ed835804
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
fix(brep/curves/ui/pdm): 落实曲面缝合·N边补面·交互拾取·焊道定位·BOM 真实体积
继续对 "Status: Fixed" 反馈文档做真实现终审,本轮落实 5 处伪实现/失效实现。

曲线曲面(VDE-125):
- sew_surfaces:源码注释自述"不检测重合顶点/边",仅把各面按索引偏移拼进同一模型,
  相邻曲面公共边界各留一份顶点与边 ⇒ 壳必然漏水、validate 按破壳处理。改为容差
  空间哈希焊接顶点(3×3×3 邻域、map<tuple> 避免大坐标位打包溢出)+ add_edge_unique
  共享边界边 + 按"每条边恰 2 邻接面"判定壳体闭合
- boundary_surface / fill_surface:N 边(n≠4)分支控制网格建成 (nu+1)×(nv+1),
  而 knots_u 隐含 u 向控制点数 = nu ⇒ 三角形/五边形输入 100% 抛
  "u-direction size mismatch"。修正网格维度与闭合环参数化

交互拾取(VDE-405,vde_ui::SelectionManager):
- interactive_select 原完全忽略窗口角点、无条件返回全部面(框选永远全选)
- pick_select 原为 (void)全部入参; return {}
- 现实现:新增 ViewProjection/set_view(world→clip + 视口);pick_select 屏幕点
  反投影成世界射线 → rayIntersectAll 取最近命中面,并按 pick_radius 像素做屏幕
  空间回退;interactive_select 投影面 AABB/边/顶点,Window 要求完全包围、
  Crossing 相交即选。未设视图时明确返回空

结构型材(VDE-124):
- make_weld_bead 原把 body_b/edge_b 用 (void) 掉,焊道只按单边定位会埋进板体。
  改为取两边中点连线的中点作接缝位置;越界边返回空体而非 UB

BOM(VDE-408):
- bom_generation 质量累计用 bounds().volume()(包围盒体积,圆柱偏高 27%)⇒
  改用 body_volume()(解析真实体积)

回归测试:新增 tests/ui/test_selection_manager.cpp(8 例);test_surface_tools 新增
缝合焊接断言(6 顶点/7 边/公共边 2 邻接);test_structural_profiles 新增焊道接缝定位断言。

验证:test_surface_tools 16/16、test_structural_profiles 26/26、test_selection_manager 8/8、
test_measure 33/33、test_draft_analysis 17/17、test_brep_validate 18/18、test_hlr_api 11/11、
test_brep_modeling 73/73、test_brep 43/43。

文档:VDE-125/124/405/408 补真实实现审计并校正状态;audit-implementation.md 增第二轮章节。
2026-10-03 23:46:33 +08:00

278 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file selection_manager.cpp
* @brief 交互式选择管理器实现 — 支持过滤与高亮的点选/框选拾取
*
* VDE-405:屏幕空间拾取需要宿主提供视图投影(ViewProjection)。没有相机就
* 无法把屏幕像素还原成世界射线,因此未 set_view 时 pick/interactive 明确返回
* 空结果。此前 interactive_select 忽略窗口角点、无条件返回全部面,pick_select
* 直接 `return {}` —— 两者都不是真实的拾取。
*/
#include "vde_ui/selection_manager.h"
#include <cmath>
#include <limits>
namespace vde::ui {
using brep::BrepModel;
using core::Point2D;
using core::Point3D;
using core::Vector3D;
namespace {
/// 世界点 → 屏幕像素(左上原点,y 向下)。w≈0(相机后方/无穷远)返回 false。
bool project_to_screen(const ViewProjection& vp, const Point3D& p, Point2D& out) {
if (!vp.valid) return false;
const core::Matrix4D& m = vp.world_to_clip;
const double w = m.at(3, 0) * p.x() + m.at(3, 1) * p.y() +
m.at(3, 2) * p.z() + m.at(3, 3);
if (std::abs(w) < 1e-12) return false;
const double nx = (m.at(0, 0) * p.x() + m.at(0, 1) * p.y() +
m.at(0, 2) * p.z() + m.at(0, 3)) / w;
const double ny = (m.at(1, 0) * p.x() + m.at(1, 1) * p.y() +
m.at(1, 2) * p.z() + m.at(1, 3)) / w;
out = Point2D((nx * 0.5 + 0.5) * vp.viewport_width,
(1.0 - (ny * 0.5 + 0.5)) * vp.viewport_height);
return true;
}
/// 屏幕像素 → clip 空间的 x,y(NDC,[-1,1])
Point2D screen_to_ndc(const ViewProjection& vp, const Point2D& s) {
return Point2D((s.x() / vp.viewport_width) * 2.0 - 1.0,
1.0 - (s.y() / vp.viewport_height) * 2.0);
}
/// NDC(sx,sy,z) → 世界点(手工 4 维齐次变换,Matrix4D::transform_point 只接受 3 维点)
bool unproject_ndc(const core::Matrix4D& inv, double sx, double sy, double z,
Point3D& out) {
const double w = inv.at(3, 0) * sx + inv.at(3, 1) * sy +
inv.at(3, 2) * z + inv.at(3, 3);
if (std::abs(w) < 1e-12) return false;
out = Point3D((inv.at(0, 0) * sx + inv.at(0, 1) * sy + inv.at(0, 2) * z +
inv.at(0, 3)) / w,
(inv.at(1, 0) * sx + inv.at(1, 1) * sy + inv.at(1, 2) * z +
inv.at(1, 3)) / w,
(inv.at(2, 0) * sx + inv.at(2, 1) * sy + inv.at(2, 2) * z +
inv.at(2, 3)) / w);
return true;
}
} // namespace
SelectionResult SelectionManager::interactive_select(
const BrepModel& body,
const core::Point2D& corner1,
const core::Point2D& corner2,
SelectionMode mode,
const SelectionFilter& filter) {
SelectionResult result;
result.mode = mode;
if (!view_.valid) return result;
const double x_min = std::min(corner1.x(), corner2.x());
const double x_max = std::max(corner1.x(), corner2.x());
const double y_min = std::min(corner1.y(), corner2.y());
const double y_max = std::max(corner1.y(), corner2.y());
auto inside = [&](const Point2D& s) {
return s.x() >= x_min && s.x() <= x_max &&
s.y() >= y_min && s.y() <= y_max;
};
// 窗口(Window)模式:实体完全落在窗口内才选中;交叉(Crossing)模式:任一点落入即选中。
const bool require_all = (mode == SelectionMode::Window);
auto test_points = [&](const std::vector<Point3D>& pts, bool& any, bool& all) {
any = false;
all = true;
int projected = 0;
for (const auto& p : pts) {
Point2D s;
if (!project_to_screen(view_, p, s)) { all = false; continue; }
++projected;
if (inside(s)) any = true;
else all = false;
}
if (projected == 0) { any = false; all = false; }
};
if (filter.accepts(SelectionFilterType::Face)) {
for (int fi = 0; fi < static_cast<int>(body.num_faces()); ++fi) {
const auto bb = body.face_bounds(fi);
std::vector<Point3D> pts = {
bb.center(), bb.min(), bb.max(),
Point3D(bb.min().x(), bb.min().y(), bb.max().z()),
Point3D(bb.min().x(), bb.max().y(), bb.min().z()),
Point3D(bb.max().x(), bb.min().y(), bb.min().z()),
Point3D(bb.min().x(), bb.max().y(), bb.max().z()),
Point3D(bb.max().x(), bb.min().y(), bb.max().z()),
Point3D(bb.max().x(), bb.max().y(), bb.min().z()),
};
bool any = false, all = false;
test_points(pts, any, all);
const bool hit = require_all ? all : any;
if (!hit) continue;
SelectionItem item;
item.entity_id = fi;
item.type = SelectionFilterType::Face;
item.world_position = bb.center();
item.is_selected = true;
result.items.push_back(item);
}
}
if (filter.accepts(SelectionFilterType::Vertex)) {
for (int vi = 0; vi < static_cast<int>(body.num_vertices()); ++vi) {
Point2D s;
if (!project_to_screen(view_, body.vertex(vi).point, s)) continue;
if (!inside(s)) continue;
SelectionItem item;
item.entity_id = vi;
item.type = SelectionFilterType::Vertex;
item.world_position = body.vertex(vi).point;
item.is_selected = true;
result.items.push_back(item);
}
}
if (filter.accepts(SelectionFilterType::Edge)) {
for (int ei = 0; ei < static_cast<int>(body.num_edges()); ++ei) {
const auto& e = body.edge(ei);
const Point3D p0 = body.vertex(e.v_start).point;
const Point3D p1 = body.vertex(e.v_end).point;
const Point3D mid((p0.x() + p1.x()) * 0.5, (p0.y() + p1.y()) * 0.5,
(p0.z() + p1.z()) * 0.5);
std::vector<Point3D> pts;
Point2D s0, s1, sm;
if (project_to_screen(view_, p0, s0)) pts.push_back(p0);
if (project_to_screen(view_, p1, s1)) pts.push_back(p1);
if (project_to_screen(view_, mid, sm)) pts.push_back(mid);
bool any = false, all = false;
test_points(pts, any, all);
const bool hit = require_all ? all : any;
if (!hit) continue;
SelectionItem item;
item.entity_id = ei;
item.type = SelectionFilterType::Edge;
item.world_position = mid;
item.is_selected = true;
result.items.push_back(item);
}
}
return result;
}
SelectionResult SelectionManager::pick_select(
const BrepModel& body,
const core::Point2D& screen_point,
double pick_radius,
const SelectionFilter& filter) {
SelectionResult result;
result.mode = SelectionMode::Single;
if (!view_.valid) return result;
// ── 1. 射线拾取:屏幕点反投影成世界射线,取最近命中的面 ──
if (filter.accepts(SelectionFilterType::Face)) {
const Point2D ndc = screen_to_ndc(view_, screen_point);
const core::Matrix4D inv = view_.world_to_clip.inverse();
Point3D near_p, far_p;
if (unproject_ndc(inv, ndc.x(), ndc.y(), -1.0, near_p) &&
unproject_ndc(inv, ndc.x(), ndc.y(), 1.0, far_p)) {
Vector3D dir = far_p - near_p;
if (dir.norm() > 1e-12) {
dir = dir.normalized();
auto hits = body.rayIntersectAll(
static_cast<float>(near_p.x()), static_cast<float>(near_p.y()),
static_cast<float>(near_p.z()), static_cast<float>(dir.x()),
static_cast<float>(dir.y()), static_cast<float>(dir.z()));
const BrepModel::FaceHit* best = nullptr;
for (const auto& h : hits) {
if (!best || h.distance < best->distance) best = &h;
}
if (best) {
SelectionItem item;
item.entity_id = best->faceIndex;
item.type = SelectionFilterType::Face;
item.world_position = Point3D(best->worldX, best->worldY,
best->worldZ);
item.is_selected = true;
result.items.push_back(item);
return result;
}
}
}
}
// ── 2. 屏幕空间近邻回退:射线未命中(点在实体外)时,按投影距离在
// pick_radius 像素内取最近的面/边/顶点。pick_radius 由此生效。──
if (pick_radius <= 0.0) return result;
double best_dist = pick_radius;
SelectionItem best_item;
bool found = false;
auto consider = [&](const Point3D& world, int id, SelectionFilterType type) {
Point2D s;
if (!project_to_screen(view_, world, s)) return;
const double dx = s.x() - screen_point.x();
const double dy = s.y() - screen_point.y();
const double d = std::sqrt(dx * dx + dy * dy);
if (d <= best_dist) {
best_dist = d;
best_item = SelectionItem{};
best_item.entity_id = id;
best_item.type = type;
best_item.world_position = world;
best_item.is_selected = true;
found = true;
}
};
if (filter.accepts(SelectionFilterType::Face)) {
for (int fi = 0; fi < static_cast<int>(body.num_faces()); ++fi)
consider(body.face_bounds(fi).center(), fi, SelectionFilterType::Face);
}
if (filter.accepts(SelectionFilterType::Vertex)) {
for (int vi = 0; vi < static_cast<int>(body.num_vertices()); ++vi)
consider(body.vertex(vi).point, vi, SelectionFilterType::Vertex);
}
if (filter.accepts(SelectionFilterType::Edge)) {
for (int ei = 0; ei < static_cast<int>(body.num_edges()); ++ei) {
const auto& e = body.edge(ei);
const Point3D p0 = body.vertex(e.v_start).point;
const Point3D p1 = body.vertex(e.v_end).point;
consider(Point3D((p0.x() + p1.x()) * 0.5, (p0.y() + p1.y()) * 0.5,
(p0.z() + p1.z()) * 0.5),
ei, SelectionFilterType::Edge);
}
}
if (found) result.items.push_back(best_item);
return result;
}
SelectionResult SelectionManager::selection_filter(
const SelectionResult& input,
const SelectionFilter& filter) {
SelectionResult result;
result.mode = input.mode;
for (auto& item : input.items) {
if (filter.accepts(item.type)) result.items.push_back(item);
}
return result;
}
void SelectionManager::selection_highlight(
const SelectionResult& selection,
const SelectionHighlight& style) {
current_ = selection;
highlight_style_ = style;
}
void SelectionManager::clear_selection() {
current_.clear();
}
} // namespace vde::ui