fix(step): trim PLANE faces to outer bound polygon + planar NURBS trimmed surface support
This commit is contained in:
+231
-1
@@ -1667,6 +1667,7 @@ private:
|
||||
|
||||
// Resolve bounds
|
||||
std::vector<int> loop_ids;
|
||||
int outer_bound_id = -1;
|
||||
size_t bounds_idx = 1;
|
||||
if (face_ent.args[bounds_idx].type == StepValue::Type::List) {
|
||||
for (const auto& bv : face_ent.args[bounds_idx].list) {
|
||||
@@ -1686,6 +1687,10 @@ private:
|
||||
}
|
||||
}
|
||||
}
|
||||
if (outer_bound_id < 0 && entities_.count(bound_ref) &&
|
||||
entities_.at(bound_ref).type == "FACE_OUTER_BOUND") {
|
||||
outer_bound_id = bound_ref;
|
||||
}
|
||||
int loop_id = convert_face_bound(bound_ref, model);
|
||||
if (loop_id >= 0) loop_ids.push_back(loop_id);
|
||||
}
|
||||
@@ -1693,7 +1698,232 @@ private:
|
||||
}
|
||||
|
||||
if (loop_ids.empty()) return -1;
|
||||
return model.add_face(surf_id, loop_ids);
|
||||
int added_face_id = model.add_face(surf_id, loop_ids);
|
||||
|
||||
// Step 1: trim PLANE faces to their outer boundary only (no holes yet).
|
||||
if (added_face_id >= 0 && is_plane_surface(surf_ref) && outer_bound_id > 0) {
|
||||
std::vector<Point3D> poly;
|
||||
if (collect_bound_polygon(outer_bound_id, poly) && poly.size() >= 3) {
|
||||
TrimmedSurface ts;
|
||||
if (build_planar_trimmed_surface(surf_ref, poly, ts)) {
|
||||
int ts_id = model.add_trimmed_surface(ts);
|
||||
model.set_face_trimmed_surface(added_face_id, ts_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return added_face_id;
|
||||
}
|
||||
|
||||
// ── PLANE 外环裁剪辅助(Step 1) ──
|
||||
|
||||
bool is_plane_surface(int surf_ref) {
|
||||
return surf_ref > 0 && entities_.count(surf_ref) &&
|
||||
entities_.at(surf_ref).type == "PLANE";
|
||||
}
|
||||
|
||||
Point3D vertex_point_3d(int vp_id) {
|
||||
if (vp_id <= 0 || !entities_.count(vp_id)) return Point3D(0, 0, 0);
|
||||
const auto& ent = entities_.at(vp_id);
|
||||
for (const auto& arg : ent.args) {
|
||||
int pr = to_ref(arg);
|
||||
if (pr > 0 && points_.count(pr)) return points_.at(pr);
|
||||
}
|
||||
return Point3D(0, 0, 0);
|
||||
}
|
||||
|
||||
// Sample an edge's 3D geometry between its effective endpoints.
|
||||
// LINE edges are exact with two endpoints; other curve types currently
|
||||
// fall back to the endpoint segment (circular arcs are handled in Step 2).
|
||||
bool densify_edge(int curve_ref, const Point3D& a, const Point3D& b,
|
||||
std::vector<Point3D>& seg) {
|
||||
seg.clear();
|
||||
if (curve_ref <= 0 || !entities_.count(curve_ref)) {
|
||||
seg = {a, b};
|
||||
return false;
|
||||
}
|
||||
const auto& cent = entities_.at(curve_ref);
|
||||
if (cent.type == "LINE") {
|
||||
seg = {a, b};
|
||||
return true;
|
||||
}
|
||||
seg = {a, b};
|
||||
return false;
|
||||
}
|
||||
|
||||
// Collect the ordered 3D polygon of a face bound's loop.
|
||||
// LINE edges contribute their two endpoints; other curves are sampled.
|
||||
bool collect_bound_polygon(int bound_id, std::vector<Point3D>& poly) {
|
||||
poly.clear();
|
||||
if (!entities_.count(bound_id)) return false;
|
||||
|
||||
int loop_ref = -1;
|
||||
const auto& bent = entities_.at(bound_id);
|
||||
for (const auto& arg : bent.args) {
|
||||
int r = to_ref(arg);
|
||||
if (r > 0 && entities_.count(r)) {
|
||||
const std::string& t = entities_.at(r).type;
|
||||
if (t == "EDGE_LOOP" || t == "POLY_LOOP") { loop_ref = r; break; }
|
||||
}
|
||||
}
|
||||
if (loop_ref < 0) return false;
|
||||
|
||||
const auto& lent = entities_.at(loop_ref);
|
||||
|
||||
// POLY_LOOP: direct list of CARTESIAN_POINT refs.
|
||||
if (lent.type == "POLY_LOOP") {
|
||||
for (const auto& arg : lent.args) {
|
||||
if (arg.type == StepValue::Type::List) {
|
||||
for (const auto& v : arg.list) {
|
||||
int pr = to_ref(v);
|
||||
if (pr > 0 && points_.count(pr)) poly.push_back(points_.at(pr));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return poly.size() >= 3;
|
||||
}
|
||||
|
||||
// EDGE_LOOP: build directed edges (effective start/end in 3D).
|
||||
struct DirEdge {
|
||||
int curve_ref = -1;
|
||||
Point3D a, b;
|
||||
};
|
||||
std::vector<DirEdge> edges;
|
||||
for (const auto& arg : lent.args) {
|
||||
if (arg.type != StepValue::Type::List) continue;
|
||||
for (const auto& ev : arg.list) {
|
||||
int oe = to_ref(ev);
|
||||
if (oe <= 0 || !entities_.count(oe)) continue;
|
||||
const auto& oeent = entities_.at(oe);
|
||||
|
||||
int ec = -1;
|
||||
for (const auto& oarg : oeent.args) {
|
||||
int r = to_ref(oarg);
|
||||
if (r > 0 && entities_.count(r) &&
|
||||
(entities_.at(r).type == "EDGE_CURVE" ||
|
||||
entities_.at(r).type == "EDGE_CURVE_IMPLICIT")) { ec = r; }
|
||||
}
|
||||
if (ec <= 0 || !entities_.count(ec)) continue;
|
||||
|
||||
bool rev = false;
|
||||
if (oeent.args.size() >= 3 && oeent.args[2].type == StepValue::Type::Enum)
|
||||
rev = (oeent.args[2].sval == ".F.");
|
||||
|
||||
const auto& ecent = entities_.at(ec);
|
||||
if (ecent.args.size() < 3) continue;
|
||||
Point3D vs = vertex_point_3d(to_ref(ecent.args[1]));
|
||||
Point3D ve = vertex_point_3d(to_ref(ecent.args[2]));
|
||||
|
||||
DirEdge de;
|
||||
de.curve_ref = to_ref(ecent.args[3]);
|
||||
de.a = rev ? ve : vs;
|
||||
de.b = rev ? vs : ve;
|
||||
edges.push_back(de);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (edges.empty()) return false;
|
||||
|
||||
// Chain directed edges into a closed loop.
|
||||
std::vector<size_t> order;
|
||||
std::vector<bool> used(edges.size(), false);
|
||||
order.push_back(0);
|
||||
used[0] = true;
|
||||
Point3D cur = edges[0].b;
|
||||
const double tol = 1e-6;
|
||||
while (order.size() < edges.size()) {
|
||||
bool found = false;
|
||||
for (size_t i = 1; i < edges.size(); ++i) {
|
||||
if (used[i]) continue;
|
||||
double da = (edges[i].a - cur).norm();
|
||||
double db = (edges[i].b - cur).norm();
|
||||
if (da < tol || db < tol) {
|
||||
used[i] = true;
|
||||
order.push_back(i);
|
||||
cur = (da < tol) ? edges[i].b : edges[i].a;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) break;
|
||||
}
|
||||
|
||||
// Densify each edge and append (skip the duplicate join point).
|
||||
for (size_t k = 0; k < order.size(); ++k) {
|
||||
const DirEdge& e = edges[order[k]];
|
||||
std::vector<Point3D> seg;
|
||||
densify_edge(e.curve_ref, e.a, e.b, seg);
|
||||
for (size_t i = 0; i < seg.size(); ++i) {
|
||||
if (i == 0 && !poly.empty()) continue;
|
||||
poly.push_back(seg[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (poly.size() >= 2) {
|
||||
double d = (poly.back() - poly.front()).norm();
|
||||
if (d > tol) poly.push_back(poly.front());
|
||||
}
|
||||
return poly.size() >= 3;
|
||||
}
|
||||
|
||||
// Build a trimmed PLANE surface whose only loop is the projected outer
|
||||
// boundary polygon. The finite base surface is fitted to the polygon bbox
|
||||
// so the parameter domain [0,1]x[0,1] keeps tessellation resolution sane.
|
||||
bool build_planar_trimmed_surface(int surf_ref,
|
||||
const std::vector<Point3D>& poly,
|
||||
TrimmedSurface& out) {
|
||||
if (!is_plane_surface(surf_ref)) return false;
|
||||
const auto& pent = entities_.at(surf_ref);
|
||||
if (pent.args.size() < 2) return false;
|
||||
int pl_ref = to_ref(pent.args[1]);
|
||||
const auto* pl = get_placement(pl_ref);
|
||||
const Vector3D& X = pl->x_axis;
|
||||
const Vector3D& Y = pl->y_axis;
|
||||
|
||||
std::vector<double> lu, lv;
|
||||
lu.reserve(poly.size());
|
||||
lv.reserve(poly.size());
|
||||
for (const auto& P : poly) {
|
||||
Vector3D d = P - pl->origin;
|
||||
lu.push_back(d.dot(X));
|
||||
lv.push_back(d.dot(Y));
|
||||
}
|
||||
|
||||
double umin = lu[0], umax = lu[0], vmin = lv[0], vmax = lv[0];
|
||||
for (size_t i = 1; i < lu.size(); ++i) {
|
||||
umin = std::min(umin, lu[i]); umax = std::max(umax, lu[i]);
|
||||
vmin = std::min(vmin, lv[i]); vmax = std::max(vmax, lv[i]);
|
||||
}
|
||||
double w = umax - umin, h = vmax - vmin;
|
||||
if (w < 1e-9 || h < 1e-9) return false;
|
||||
|
||||
curves::NurbsSurface base = curves::NurbsSurface::plane(
|
||||
pl->origin + umin * X + vmin * Y, X, Y, w, h);
|
||||
|
||||
TrimLoop outer;
|
||||
outer.is_outer = true;
|
||||
auto make_pc = [](double ua, double va, double ub, double vb) {
|
||||
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 };
|
||||
return PCurve(cps, knots, weights, 1);
|
||||
};
|
||||
|
||||
size_t n = poly.size();
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
size_t j = (i + 1) % n;
|
||||
double ua = (lu[i] - umin) / w, va = (lv[i] - vmin) / h;
|
||||
double ub = (lu[j] - umin) / w, vb = (lv[j] - vmin) / h;
|
||||
if (std::abs(ua - ub) < 1e-12 && std::abs(va - vb) < 1e-12) continue;
|
||||
outer.p_curves.push_back(make_pc(ua, va, ub, vb));
|
||||
}
|
||||
if (outer.p_curves.empty()) return false;
|
||||
|
||||
out.base_surface = base;
|
||||
out.loops.clear();
|
||||
out.loops.push_back(std::move(outer));
|
||||
return true;
|
||||
}
|
||||
|
||||
int convert_face_bound(int bound_id, BrepModel& model) {
|
||||
|
||||
@@ -224,6 +224,101 @@ std::vector<std::pair<double, double>> rect_to_polygon(double u0, double u1,
|
||||
return {{u0, v0}, {u1, v0}, {u1, v1}, {u0, v1}};
|
||||
}
|
||||
|
||||
/// Returns true when all control points of a NURBS surface lie in one plane.
|
||||
bool is_planar_surface(const curves::NurbsSurface& surf, double tol = 1e-9) {
|
||||
const auto& cp = surf.control_points();
|
||||
if (cp.empty() || cp[0].empty()) return true;
|
||||
|
||||
const Point3D& p0 = cp[0][0];
|
||||
Vector3D n(0.0, 0.0, 0.0);
|
||||
bool have = false;
|
||||
|
||||
// Find a normal from two non-collinear vectors among the control points.
|
||||
for (const auto& row : cp) {
|
||||
for (const auto& p : row) {
|
||||
Vector3D du = p - p0;
|
||||
if (du.norm() <= tol) continue;
|
||||
for (const auto& row2 : cp) {
|
||||
for (const auto& q : row2) {
|
||||
Vector3D c = du.cross(q - p0);
|
||||
if (c.norm() > tol) {
|
||||
n = c.normalized();
|
||||
have = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (have) break;
|
||||
}
|
||||
if (have) break;
|
||||
}
|
||||
if (have) break;
|
||||
}
|
||||
if (!have) return true; // degenerate (point/line) — treat as planar
|
||||
|
||||
for (const auto& row : cp) {
|
||||
for (const auto& p : row) {
|
||||
if (std::abs((p - p0).dot(n)) > tol) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Remove consecutive duplicates and collinear vertices from a sampled polygon,
|
||||
/// leaving only the true corner points.
|
||||
std::vector<std::pair<double, double>> simplify_polygon(
|
||||
const std::vector<std::pair<double, double>>& poly, double tol) {
|
||||
std::vector<std::pair<double, double>> out;
|
||||
for (const auto& p : poly) {
|
||||
if (out.empty()) {
|
||||
out.push_back(p);
|
||||
continue;
|
||||
}
|
||||
const auto& a = out.back();
|
||||
double dx = p.first - a.first, dy = p.second - a.second;
|
||||
if (dx * dx + dy * dy <= tol * tol) continue; // duplicate
|
||||
out.push_back(p);
|
||||
}
|
||||
|
||||
// Remove collinear vertices (wrap-around). Rebuild until stable.
|
||||
bool changed = true;
|
||||
while (changed && out.size() >= 3) {
|
||||
changed = false;
|
||||
size_t n = out.size();
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const auto& prev = out[(i + n - 1) % n];
|
||||
const auto& cur = out[i];
|
||||
const auto& next = out[(i + 1) % n];
|
||||
double cross = (cur.first - prev.first) * (next.second - prev.second) -
|
||||
(cur.second - prev.second) * (next.first - prev.first);
|
||||
if (std::abs(cross) <= tol) {
|
||||
out.erase(out.begin() + static_cast<long>(i));
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Returns true when the polygon vertices are in strictly convex order.
|
||||
bool is_convex_polygon(const std::vector<std::pair<double, double>>& poly) {
|
||||
const size_t n = poly.size();
|
||||
if (n < 3) return false;
|
||||
int sign = 0;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const auto& a = poly[i];
|
||||
const auto& b = poly[(i + 1) % n];
|
||||
const auto& c = poly[(i + 2) % n];
|
||||
double cross = (b.first - a.first) * (c.second - a.second) -
|
||||
(b.second - a.second) * (c.first - a.first);
|
||||
if (std::abs(cross) < 1e-12) continue;
|
||||
int s = cross > 0 ? 1 : -1;
|
||||
if (sign == 0) sign = s;
|
||||
else if (sign != s) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
// ─────────────────────────────────────────────────
|
||||
@@ -450,6 +545,29 @@ mesh::HalfedgeMesh TrimmedSurface::to_mesh(double deflection) const {
|
||||
v_min = std::min(v_min, vv); v_max = std::max(v_max, vv);
|
||||
}
|
||||
|
||||
// Fast path: a single outer loop on a planar base surface whose boundary is
|
||||
// a convex polygon. Triangulate the simplified polygon directly (fan from
|
||||
// corner 0) so a rectangular face yields exactly 4 vertices / 2 triangles,
|
||||
// matching the appearance of the prebuilt box instead of a dense
|
||||
// parameter-space grid with many internal vertices and diagonals.
|
||||
if (loops.size() == 1 && is_planar_surface(base_surface)) {
|
||||
auto corners = simplify_polygon(outer_poly, 1e-9);
|
||||
if (corners.size() >= 3 && is_convex_polygon(corners)) {
|
||||
std::vector<core::Point3D> fast_verts;
|
||||
fast_verts.reserve(corners.size());
|
||||
for (const auto& [uu, vv] : corners) {
|
||||
fast_verts.push_back(base_surface.evaluate(uu, vv));
|
||||
}
|
||||
std::vector<std::array<int, 3>> fast_tris;
|
||||
fast_tris.reserve(corners.size() - 2);
|
||||
for (size_t i = 1; i + 1 < corners.size(); ++i) {
|
||||
fast_tris.push_back({0, static_cast<int>(i), static_cast<int>(i + 1)});
|
||||
}
|
||||
result.build_from_triangles(fast_verts, fast_tris);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Adaptive resolution based on deflection
|
||||
double span_u = u_max - u_min;
|
||||
double span_v = v_max - v_min;
|
||||
|
||||
Reference in New Issue
Block a user