From a1092183f6b2bb3cbfa04afa9b42196fdfd95dbb Mon Sep 17 00:00:00 2001 From: hm-thinkbook14p Date: Sun, 22 Nov 2026 19:07:36 +0800 Subject: [PATCH] feat(brep/foundation): Wave19 IGES placeholder cleanup, canonical export and UTF-8 leftovers - A43: IGES fallback only builds implicit untrimmed faces for true surface entities (is_surface_entity_type); validate 510/140 surface references; return empty model with UnsupportedEntity diagnostic when the file has no surface entities. corpus/real/igs wireframe files: 29 files, 17,511 placeholder faces -> 0 bodies; trimmed 24 samples unchanged (1,050 faces / 4,443 edges); 6 untrimmed-surface files 347 -> 170 faces; 5 no-surface files 60 -> 0. - A44: IGES 5.3 canonical P-section layout: type-number prefix on every P entity, 502/504/508/510/514/186/144 canonical field layouts, 128 five-attribute header, 126 POLYNOMIAL flag semantics; importer keeps accepting both layouts. Round-trip: box volume rel 0 / bounds <=1e-9, sphere <=1.5e-2, cylinder <=1e-5, 3-body model, real STEP through IGES (PWB 3 bodies/424 faces/852 edges rel 0). - A46: format_detect and step_index reuse include/vde/foundation/utf8_path.h; remove file-local fs_path_from_native / path_from_utf8; add UTF-8 format-detect and parallel step-index round-trip tests. - header doc: iges_export.h describes canonical default output and 144/406 limits. Tests: test_iges_import 49 (48+1 env skip), test_iges_export 34, test_format_write_roundtrip 13, test_format_detect 35, test_step_import 111, test_format_io 21+3 skip. 23 suites 916 passed / 0 failed; PR gate 1065/1065. --- include/vde/brep/iges_export.h | 16 +- src/brep/iges_export.cpp | 349 ++++++++++++++--------- src/brep/iges_import.cpp | 23 ++ src/brep/step_index.cpp | 15 +- src/foundation/format_detect.cpp | 39 +-- tests/brep/test_iges_export.cpp | 355 ++++++++++++++++++++++-- tests/brep/test_iges_import.cpp | 206 ++++++++++++-- tests/brep/test_step_import.cpp | 69 +++++ tests/foundation/test_format_detect.cpp | 107 +++++++ 9 files changed, 963 insertions(+), 216 deletions(-) diff --git a/include/vde/brep/iges_export.h b/include/vde/brep/iges_export.h index d2cf7ce9..d74fac83 100644 --- a/include/vde/brep/iges_export.h +++ b/include/vde/brep/iges_export.h @@ -55,19 +55,23 @@ struct IgesExportDiagnostics { /** * @brief IGES 导出选项(FMT-04) * - * 旧 API export_iges(bodies) / export_iges_file(path, bodies) 保持默认选项, - * 默认输出与历史版本逐字节一致。 + * 旧 API export_iges(bodies) / export_iges_file(path, bodies) 保持默认选项; + * 自 Wave19(A44,合流于 1.0.490 之后)起默认输出改为 IGES 5.3 规范 P 段布局(首 token 为实体 + * 类型号,502/504/508/510/514/186/144 按规范字段),不再与 1.0.490 及更早版本的 + * 默认输出逐字节相同;本内核导入端(A36/A38)同时接受规范布局与 legacy 布局。 + * 完整拓扑布局与 reader 锁步,改一处必须同步 `src/brep/iges_import.cpp`。 * * 144 限制:TRIMMED_SURFACE 引用既有 108/128 基面与 508 loops(边界来自既有 * edge curves),不额外生成 p-curve/模型曲线;外部 CAD 的裁剪精度以这些几何为准。 - * 本项目导入端当前不解析 144,写出后请以目标 CAD 的兼容性为准。 + * 本项目导入端(A38)在无 186/510 时会解析 144/143/142;`406` 图层名负载仍为 + * 仓库私有 `[target_de,nHname]`,外部 CAD 不保证可读。 */ struct IgesExportOptions { /// 开启后为有裁剪环的面写 144 TRIMMED_SURFACE(引用基面 108/128 + 508 loops)。 - /// 默认 false:保持旧输出不变。 + /// 默认 false:不写 144;拓扑实体仍按规范布局写 502/504/508/510/514/186。 bool write_trimmed_surfaces = false; /// 仅对闭合壳写 186 Manifold Solid B-Rep Object;开放壳/非水密体跳过并诊断。 - /// 默认 true(闭合体输出与旧版本一致)。 + /// 默认 true(1.0.490 及更早版本同样只对闭合体写 186;布局改为规范 P 段)。 bool write_186_only_when_closed = true; /// FMT-05:写出颜色(314 Color Definition + DE color number 引用)。 /// 默认 false;body 未提供 body_colors 时取 body_material().diffuse。 @@ -137,7 +141,7 @@ struct IgesExportOptions { * 开放壳跳过并计入 diagnostics->open_shell_bodies_skipped。 * * @param bodies B-Rep 体列表 - * @param options 导出选项(默认构造 = 历史行为) + * @param options 导出选项(默认构造 = 规范 P 段布局;选项语义与历史版本一致) * @return IGES 5.3 格式字符串 */ [[nodiscard]] std::string export_iges(const std::vector& bodies, diff --git a/src/brep/iges_export.cpp b/src/brep/iges_export.cpp index 672a1980..49092860 100644 --- a/src/brep/iges_export.cpp +++ b/src/brep/iges_export.cpp @@ -14,6 +14,7 @@ #include "vde/brep/modeling.h" #include "vde/foundation/format_utils.h" #include +#include #include #include #include @@ -304,9 +305,18 @@ private: // ── Geometry entity writers ────────────────────────── - /// Type 116: Point - int write_point(const Point3D& p) { + /// IGES 5.3:Parameter Data 记录的首个字段必须是实体类型号(P 段首 + /// token)。这里写入 ",",调用方随后直接追加第一个参数;后续 + /// 参数仍由调用方补逗号。 + void begin_params(int entity_type) { params_.clear(); + append_int(params_, entity_type); + params_.push_back(','); + } + + /// Type 116: Point — P 段:116, X, Y, Z + int write_point(const Point3D& p) { + begin_params(116); append_real(params_, p.x()); params_.push_back(','); append_real(params_, p.y()); @@ -316,9 +326,9 @@ private: return append_de(116, pd_start, pd_seq_ - pd_start + 1); } - /// Type 110: Line + /// Type 110: Line — P 段:110, X1, Y1, Z1, X2, Y2, Z2 int write_line(const Point3D& p1, const Point3D& p2) { - params_.clear(); + begin_params(110); append_real(params_, p1.x()); params_.push_back(','); append_real(params_, p1.y()); @@ -338,7 +348,7 @@ private: /// IGES places arc in Z=ZT plane; ZT → Z offset, then 6 params for X,Y of center/start/end int write_arc(double zt, const Point3D& center_2d, const Point3D& start_2d, const Point3D& end_2d) { - params_.clear(); + begin_params(100); append_real(params_, zt); params_.push_back(','); append_real(params_, center_2d.x()); @@ -365,19 +375,19 @@ private: int M = curve.degree(); int N = K + 1; // number of CPs (= order for clamped) - // Properties: planar, open, rational/polynomial, nonperiodic - int prop1 = 0; // nonplanar (conservative) - int prop2 = 0; // open + // Properties(IGES 5.3 Type 126 头字段:PLANAR, CLOSED, POLYNOMIAL, PERIODIC) + int prop1 = 0; // PLANAR:保守写 0(非平面) + int prop2 = 0; // CLOSED:0=开曲线 int prop3 = (!w.empty() && std::any_of(w.begin(), w.end(), [](double wt){ return std::abs(wt - 1.0) > 1e-10; })) - ? 1 : 0; // 0=polynomial, 1=rational - int prop4 = 0; // nonperiodic + ? 0 : 1; // POLYNOMIAL:1=多项式,0=有理 + int prop4 = 0; // PERIODIC:非周期 // Domain double tmin = knots.empty() ? 0.0 : knots.front(); double tmax = knots.empty() ? 1.0 : knots.back(); - params_.clear(); + begin_params(126); append_int(params_, K); params_.push_back(','); append_int(params_, M); @@ -444,7 +454,7 @@ private: double A = n.x(), B = n.y(), C = n.z(); double D = -(A * p00.x() + B * p00.y() + C * p00.z()); - params_.clear(); + begin_params(108); append_real(params_, A); params_.push_back(','); append_real(params_, B); @@ -470,21 +480,25 @@ private: int K1 = nu - 1, K2 = nv - 1; int M1 = du, M2 = dv; - // Properties - int prop1 = 0, prop2 = 0; // open in both u and v - int prop3 = (!w.empty() && - std::any_of(w.begin(), w.end(), [](const auto& row) { - return std::any_of(row.begin(), row.end(), - [](double wt){ return std::abs(wt - 1.0) > 1e-10; }); - })) ? 1 : 0; - int prop4 = 0; + // IGES 5.3 Type 128 参数头为 K1, K2, M1, M2, PROP1(closed u), PROP2(closed v), + // PROP3(polynomial), PROP4(periodic u), PROP5(periodic v) 共 9 个字段。 + // 仓库读取端 Spec 分支固定跳过 9 个头字段再读节点/权重/控制点(旧私有布局只有 + // 4 个属性位,整体会错位 1 个 token),所以这里必须按规范写满 5 个属性位。 + const bool rational = + !w.empty() && + std::any_of(w.begin(), w.end(), [](const auto& row) { + return std::any_of(row.begin(), row.end(), + [](double wt){ return std::abs(wt - 1.0) > 1e-10; }); + }); + const int closed_u = 0, closed_v = 0, polynomial = rational ? 0 : 1; + const int periodic_u = 0, periodic_v = 0; double umin = ku.empty() ? 0.0 : ku.front(); double umax = ku.empty() ? 1.0 : ku.back(); double vmin = kv.empty() ? 0.0 : kv.front(); double vmax = kv.empty() ? 1.0 : kv.back(); - params_.clear(); + begin_params(128); append_int(params_, K1); params_.push_back(','); append_int(params_, K2); @@ -493,13 +507,15 @@ private: params_.push_back(','); append_int(params_, M2); params_.push_back(','); - append_int(params_, prop1); + append_int(params_, closed_u); params_.push_back(','); - append_int(params_, prop2); + append_int(params_, closed_v); params_.push_back(','); - append_int(params_, prop3); + append_int(params_, polynomial); params_.push_back(','); - append_int(params_, prop4); + append_int(params_, periodic_u); + params_.push_back(','); + append_int(params_, periodic_v); params_.push_back(','); // U knots: M1 + K1 + 1 @@ -624,72 +640,88 @@ private: // ── Topology entity writers ────────────────────────── // - // IGES 5.3 leaves the B-Rep topology entities (502/504/508/510) as - // non-normative "untested" entities: the standard does not pin down their - // parameter layout, so interoperability depends on an agreed contract. - // The parameter layouts emitted below are exactly the ones consumed by - // this module's sibling reader (IgesToBrep in iges_import.cpp); keeping the - // two in lockstep is what makes an export→import round-trip reconstruct the - // same topology. Any change here must be mirrored in the reader. + // IGES 5.3 把 B-Rep 拓扑实体(502/504/508/510/514/186)列为非强制的 + // "untested" 实体,但下面写出的参数布局与仓库导入端(IgesToBrep, + // iges_import.cpp 的 Spec 布局分支,A36/A38)严格对齐:P 段首字段为实体 + // 类型号,502/504 为按 1 基行号引用的共享表,508 条目指向 504 表, + // 510/514/186 逐级向上引用。任何布局改动都必须同步镜像到读取端。 + // + // 残余限制:406(Property Form 15)仅加类型号前缀,载荷仍为仓库私有的 + // [target_de, nHname];IGES 规范 406 为 [NP, N, (target, values)…] 的 + // 属性表形式。导入端不解析 406(仅 DE 层可读),故不影响往返。 - /// Type 502: Vertex — parameters are the vertex coordinates X, Y, Z. - /// The reader (convert_vertex) reads p[0..2] as the point directly, so the - /// vertex carries its own coordinates instead of a Type 116 point pointer. - int write_vertex(const Point3D& p) { - params_.clear(); - append_real(params_, p.x()); - params_.push_back(','); - append_real(params_, p.y()); - params_.push_back(','); - append_real(params_, p.z()); + /// Type 502: Vertex List — 502, N, (X,Y,Z)×N + /// 顶点坐标表;边表按 1 基行号(1..N)引用。同一 body 内坐标完全相同的 + /// 顶点由调用方去重后共用一行(与导入端 (DE,index)→顶点 缓存一致)。 + int write_vertex_list(const std::vector& coords) { + if (coords.empty()) return 0; + begin_params(502); + append_size(params_, coords.size()); + for (const auto& p : coords) { + params_.push_back(','); + append_real(params_, p.x()); + params_.push_back(','); + append_real(params_, p.y()); + params_.push_back(','); + append_real(params_, p.z()); + } int pd_start = append_pd(de_seq_ + 1); return append_de(502, pd_start, pd_seq_ - pd_start + 1); } - /// Type 504: Edge - /// Reader contract (convert_edge): CURType, CURDEF, SVDE, SVINDEX, EVDE, - /// EVINDEX, CSDE, SP, EP — it uses p[1]=curve DE, p[2]=start vertex DE and - /// p[4]=end vertex DE, and rejects the entity unless at least 5 fields are - /// present. Emitting only 3 fields made every edge fail to convert. - int write_edge(int curve_de, int v_start_de, int v_end_de) { - params_.clear(); - params_ += "0,"; - append_int(params_, curve_de); - params_.push_back(','); - append_int(params_, v_start_de); - params_ += ",1,"; - append_int(params_, v_end_de); - params_ += ",1,0,0.0,0.0"; + /// Type 504: Edge List — 504, M, (CURVE, SV_LIST, SV_IDX, EV_LIST, EV_IDX)×M + /// 每条边一行:曲线 DE + 起/终点所在 502 顶点表 DE 与 1 基行号。 + int write_edge_list(int vertex_list_de, const std::vector& curve_des, + const std::vector& start_rows, + const std::vector& end_rows) { + const std::size_t m = curve_des.size(); + if (m == 0 || start_rows.size() != m || end_rows.size() != m) return 0; + const int list_de = ref_ok(vertex_list_de, 502) ? vertex_list_de : 0; + begin_params(504); + append_size(params_, m); + for (std::size_t i = 0; i < m; ++i) { + params_.push_back(','); + append_int(params_, curve_des[i]); // 0 = no curve entity + params_.push_back(','); + append_int(params_, list_de); + params_.push_back(','); + append_int(params_, start_rows[i]); + params_.push_back(','); + append_int(params_, list_de); + params_.push_back(','); + append_int(params_, end_rows[i]); + } int pd_start = append_pd(de_seq_ + 1); return append_de(504, pd_start, pd_seq_ - pd_start + 1); } - /// Type 508: Loop - /// Reader contract (convert_loop): N, then per edge entry - /// TYPE, EDGE_DE, COUNT, VX1..VX_COUNT, CS_DE, SP, EP for a total of - /// 6+COUNT fields (the reader advances by exactly that stride). We emit the - /// edge-only form (COUNT=0) → "0, EDGE_DE, 0, 0, SP, EP". - int write_loop(const std::vector& edge_des, const std::vector& orientations) { - (void)orientations; // reader derives edge sense from geometry, not the loop - std::vector valid_edges; - valid_edges.reserve(edge_des.size()); - for (int ed : edge_des) - if (ref_ok(ed, 504)) valid_edges.push_back(ed); - if (valid_edges.empty()) return 0; - params_.clear(); - append_size(params_, valid_edges.size()); - for (int ed : valid_edges) { + /// Type 508: Loop — 508, N, (TYPE=0, EDGE_LIST_DE, EDGE_IDX, ORIENT, NPARAM=0)×N + /// TYPE=0 表示条目引用 504 边表;EDGE_IDX 为该表 1 基行号;ORIENT 记录 + /// 拓扑边的反向标志(导入端目前按几何推导方向,字段保留以保真)。 + int write_loop(int edge_list_de, const std::vector& edge_rows, + const std::vector& orientations) { + const std::size_t n = edge_rows.size(); + if (n == 0 || orientations.size() != n) return 0; + if (!ref_ok(edge_list_de, 504)) return 0; + begin_params(508); + append_size(params_, n); + for (std::size_t i = 0; i < n; ++i) { params_ += ",0,"; - append_int(params_, ed); - params_ += ",0,0,0.0,0.0"; + append_int(params_, edge_list_de); + params_.push_back(','); + append_int(params_, edge_rows[i]); + params_.push_back(','); + append_int(params_, orientations[i]); + params_ += ",0"; } int pd_start = append_pd(de_seq_ + 1); return append_de(508, pd_start, pd_seq_ - pd_start + 1); } /// Type 144: Trimmed Surface(FMT-04) - /// 参数:PTS(基面 DE), N1(外环数), N2(内环数), 外环 DE..., 内环 DE... - /// 基面只接受 108 Plane / 128 B-Spline Surface;loop 必须是 508。 + /// 参数:144, PTS(基面 DE), N1(外环数), N2(内环数), 外环 DE..., 内环 DE... + /// 基面只接受 108 Plane / 128 B-Spline Surface;loop 必须是 508。导入端 + /// 在 p[3] 为 508 时按 loop 布局解析(convert_144 的 loop_layout 分支)。 int write_trimmed_surface(int surface_de, const std::vector& outer_loop_des, const std::vector& inner_loop_des) { if (!ref_ok_surface(surface_de)) return 0; @@ -697,7 +729,7 @@ private: if (!ref_ok(ld, 508)) return 0; for (int ld : inner_loop_des) if (!ref_ok(ld, 508)) return 0; - params_.clear(); + begin_params(144); append_int(params_, surface_de); params_.push_back(','); append_size(params_, outer_loop_des.size()); @@ -715,47 +747,49 @@ private: return append_de(144, pd_start, pd_seq_ - pd_start + 1); } - /// Type 510: Face - /// Reader contract (convert_face): surface_DE, N_loops, then N pairs of - /// (loop_flag, loop_DE) — loop_flag 0 = outer boundary, 1 = inner hole. + /// Type 510: Face — 510, SURF, NLOOPS, OUTERLOOPFLAG, LOOP×N + /// 外环固定为列表第一个:OUTERLOOPFLAG=0 表示「外环是第一个环」,其余 + /// 环为内孔(读取端忽略该 flag,仍按第一个环为外环处理)。 int write_face(int surface_de, const std::vector& loop_des) { if (!ref_ok_surface(surface_de)) return 0; for (int ld : loop_des) if (!ref_ok(ld, 508)) return 0; - params_.clear(); + begin_params(510); append_int(params_, surface_de); params_.push_back(','); append_size(params_, loop_des.size()); - for (size_t i = 0; i < loop_des.size(); ++i) { + params_ += ",0"; + for (int ld : loop_des) { params_.push_back(','); - append_int(params_, i == 0 ? 0 : 1); - params_.push_back(','); - append_int(params_, loop_des[i]); + append_int(params_, ld); } int pd_start = append_pd(de_seq_ + 1); return append_de(510, pd_start, pd_seq_ - pd_start + 1); } - /// Type 514: Shell + /// Type 514: Shell — 514, NFACES, (FACE, ORIENTATION=0)×NFACES + /// 导入端 convert_shell 逐面读取 p[1+2i] 的面 DE,方向字段忽略。 int write_shell(const std::vector& face_des) { for (int fd : face_des) if (!ref_ok(fd, 510)) return 0; - params_.clear(); + begin_params(514); append_size(params_, face_des.size()); for (int fd : face_des) { params_.push_back(','); append_int(params_, fd); + params_ += ",0"; } int pd_start = append_pd(de_seq_ + 1); return append_de(514, pd_start, pd_seq_ - pd_start + 1); } - /// Type 186: Manifold Solid B-Rep Object + /// Type 186: Manifold Solid B-Rep Object — 186, SHELL, NSHELLS=1, NVOIDS=0 + /// 导入端 build_manifold_solid 取 p[0]=壳 DE、p[2]=空洞壳数(0)。 int write_manifold_solid_brep(int shell_de) { if (!ref_ok(shell_de, 514)) return 0; - params_.clear(); + begin_params(186); append_int(params_, shell_de); - params_ += ",0HNAME,0"; + params_ += ",1,0"; int pd_start = append_pd(de_seq_ + 1); return append_de(186, pd_start, pd_seq_ - pd_start + 1); } @@ -805,8 +839,9 @@ private: } /// FMT-05:Type 314 Color Definition(RGB 0..100 百分数)。 + /// P 段:314, R, G, B int write_color_definition(const Color& color) { - params_.clear(); + begin_params(314); append_real(params_, color.r * 100.0); params_.push_back(','); append_real(params_, color.g * 100.0); @@ -819,10 +854,13 @@ private: return de; } - /// FMT-05:Type 406 Property Form 15(Name),参数 target_de,H。 + /// FMT-05:Type 406 Property Form 15(Name)。 + /// P 段:406, target_de, H —— 只加了规范要求的类型号首字段, + /// 载荷仍是仓库私有形式(规范 406 为属性表 [NP, N, (target,value)…]); + /// 导入端不解析 406,残余限制记录在 TestSpecLayout* 测试旁注中。 int write_property_name(int target_de, const std::string& name) { if (target_de <= 0 || !valid_layer_name(name)) return 0; - params_.clear(); + begin_params(406); append_int(params_, target_de); params_.push_back(','); append_size(params_, name.size()); @@ -905,50 +943,111 @@ private: surf_des[static_cast(sid)] = write_surface(body.surface(sid)); } - // Phase 4: Write topology — vertex, edge, loop, face, shell, MSBO + // Phase 4: Write topology — 502 vertex list, 504 edge list, 508 loop, + // 510 face, 514 shell, 186 MSBO(IGES 5.3 规范布局,P 段首字段为 + // 实体类型号,见上方 write_* 注释)。 // - // Reference convention: a TopoEdge stores its endpoints as vertex - // *indices* (add_edge is called with add_vertex() return values), and a - // TopoLoop stores its edges as edge *indices* (add_loop is called with - // find_edge()/add_edge() return values — see step_export.cpp, which - // resolves them via body.edge(lop.edges[i])). The lookup tables below - // must therefore be keyed by index, not by the entities' synthetic - // `id` fields; keying by `id` silently dropped every loop whose edge - // index did not coincide with an unrelated entity's id. - std::vector vert_topo_de(static_cast(num_vertices), 0); - for (int vi = 0; vi < num_vertices; ++vi) - vert_topo_de[static_cast(vi)] = write_vertex(body.vertex(vi).point); + // 引用约定:TopoEdge 用顶点 *索引* 表示端点(add_edge 的返回值即 + // 索引),TopoLoop 用边 *索引* 表示环成员(add_loop 的返回值;见 + // step_export.cpp 对 body.edge(lop.edges[i]) 的解析)。因此各表必须 + // 按索引建映射,而不是按实体的合成 `id` 字段——按 id 建表会静默 + // 丢弃边索引与无关实体 id 不重合的环。 + // + // 502/504 是 body 内共享表:502 按坐标精确去重,504 按(曲线 DE, + // 起点行, 终点行)去重;环条目再以 1 基行号引用 504 表。行号必须 + // 始终落在 1..N 内——导入端 convert_edge_list_entry 对越界索引会 + // 回退到 legacy 单边解析,静默给出错误拓扑。 + std::vector vertex_list; + vertex_list.reserve(static_cast(num_vertices)); + std::vector vertex_rows(static_cast(num_vertices), 1); + { + const auto vertex_hash = [](const std::array& a) { + std::size_t h = std::hash{}(a[0]); + h ^= std::hash{}(a[1]) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2); + h ^= std::hash{}(a[2]) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2); + return h; + }; + std::unordered_map, int, decltype(vertex_hash)> + row_of(0, vertex_hash); + row_of.reserve(static_cast(num_vertices) * 2 + 1); + for (int vi = 0; vi < num_vertices; ++vi) { + const auto& p = body.vertex(vi).point; + const std::array key{p.x(), p.y(), p.z()}; + const auto it = row_of.find(key); + if (it != row_of.end()) { + vertex_rows[static_cast(vi)] = it->second; + } else { + vertex_list.push_back(p); + const int row = static_cast(vertex_list.size()); // 1-based + row_of.emplace(key, row); + vertex_rows[static_cast(vi)] = row; + } + } + } + const int vertex_list_de = write_vertex_list(vertex_list); - // Edge topology entities - std::vector edge_topo_de(static_cast(num_edges), 0); - for (int ei = 0; ei < num_edges; ++ei) { - const auto& e = body.edge(ei); - const int cde = curve_des[static_cast(ei)]; - const int vs = (e.v_start >= 0 && e.v_start < num_vertices) - ? vert_topo_de[static_cast(e.v_start)] - : 0; - const int ve = (e.v_end >= 0 && e.v_end < num_vertices) - ? vert_topo_de[static_cast(e.v_end)] - : 0; - edge_topo_de[static_cast(ei)] = write_edge(cde, vs, ve); + // 504 边表(行号 1..M;曲线 DE 可能为 0 = 无边曲线实体) + std::vector edge_rows(static_cast(num_edges), 0); + int edge_list_de = 0; + if (num_edges > 0) { + const auto edge_hash = [](const std::array& a) { + std::size_t h = std::hash{}(a[0]); + h ^= std::hash{}(a[1]) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2); + h ^= std::hash{}(a[2]) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2); + return h; + }; + std::unordered_map, int, decltype(edge_hash)> + row_of(0, edge_hash); + row_of.reserve(static_cast(num_edges) * 2 + 1); + std::vector row_curve; + std::vector row_sv; + std::vector row_ev; + row_curve.reserve(static_cast(num_edges)); + row_sv.reserve(static_cast(num_edges)); + row_ev.reserve(static_cast(num_edges)); + for (int ei = 0; ei < num_edges; ++ei) { + const auto& e = body.edge(ei); + const int sv = (e.v_start >= 0 && e.v_start < num_vertices) + ? vertex_rows[static_cast(e.v_start)] + : 0; + const int ev = (e.v_end >= 0 && e.v_end < num_vertices) + ? vertex_rows[static_cast(e.v_end)] + : 0; + const std::array key{curve_des[static_cast(ei)], sv, ev}; + const auto it = row_of.find(key); + if (it != row_of.end()) { + edge_rows[static_cast(ei)] = it->second; + } else { + row_curve.push_back(key[0]); + row_sv.push_back(sv); + row_ev.push_back(ev); + const int row = static_cast(row_curve.size()); // 1-based + row_of.emplace(key, row); + edge_rows[static_cast(ei)] = row; + } + } + edge_list_de = write_edge_list(vertex_list_de, row_curve, row_sv, row_ev); } - // Loop entities (keyed by loop id) + // 508 环(按 loop id 索引):条目引用 504 边表行号 + 方向 std::unordered_map loop_topo_de; const auto& loops = body.all_loops(); loop_topo_de.reserve(loops.size() * 2 + 1); - std::vector e_des; - std::vector orientations; + std::vector loop_rows; + std::vector loop_orientations; for (const auto& loop : loops) { - e_des.clear(); - orientations.clear(); + loop_rows.clear(); + loop_orientations.clear(); for (int ei : loop.edges) { if (ei < 0 || ei >= num_edges) continue; - e_des.push_back(edge_topo_de[static_cast(ei)]); - orientations.push_back(body.edge(ei).reversed ? 1 : 0); + const int row = edge_rows[static_cast(ei)]; + if (row <= 0) continue; + loop_rows.push_back(row); + loop_orientations.push_back(body.edge(ei).reversed ? 1 : 0); } - if (!e_des.empty()) { - const int loop_entity_de = write_loop(e_des, orientations); + if (!loop_rows.empty()) { + const int loop_entity_de = + write_loop(edge_list_de, loop_rows, loop_orientations); if (loop_entity_de != 0) loop_topo_de[loop.id] = loop_entity_de; } } diff --git a/src/brep/iges_import.cpp b/src/brep/iges_import.cpp index 56fc4413..2bdbfa90 100644 --- a/src/brep/iges_import.cpp +++ b/src/brep/iges_import.cpp @@ -626,8 +626,14 @@ public: has_any = true; } } else { + // A43(Wave19):无 141-144 裁剪拓扑时只收集真值曲面实体。旧实现 + // 遍历全部实体调用 convert_or_get_surface,124 变换矩阵、100/110/126 + // 曲线、308/408/314 子图实例等非曲面类型会被 get_or_build_surface + // 的默认单位方片(控制点 {0,0}..{1,1})顶替,伪造 faces=实体数、 + // edges=0 的占位面(A41 全语料复扫:29 个纯线框文件 17,511 个假面)。 for (const auto& ent : entities_) { if (!checkpoint()) return result; + if (!is_surface_entity_type(ent.type_number)) continue; int sid = convert_or_get_surface(ent.de_seq, model); if (aborted_) return result; if (sid >= 0) has_any = true; @@ -1147,6 +1153,10 @@ private: if (p.size() < 2) return -1; const int surf_de = IgesParser::parse_int(p[0]); + // A43:510 的面参数只应引用真值曲面实体;引用缺失或非曲面实体时若继续 + // convert_or_get_surface,会由默认单位方片伪造面,这里直接放弃该面。 + const auto* surf_ent = get_entity(surf_de); + if (!surf_ent || !is_surface_entity_type(surf_ent->type_number)) return -1; const int surf_id = convert_or_get_surface(surf_de, model); const int n_loops = IgesParser::parse_int(p[1]); @@ -2486,6 +2496,11 @@ private: int base_de = get_int(p, 0); double offset = get_real(p, 1); + // A43:140 的基面必须引用真值曲面实体,否则 get_or_build_surface 会为非曲面 + // 目标默认构造单位方片面;抛异常交由调用方按退化曲面处理。 + const auto* base = get_entity(base_de); + if (!base || !is_surface_entity_type(base->type_number)) + throw std::runtime_error("Offset surface base is not a surface entity"); // Simple offset: return the base surface with a note — full offset // implementation would require offsetting the NURBS, which is complex. @@ -2817,6 +2832,14 @@ std::vector import_iges_impl(std::string data, IgesBudget& budget) { try { auto bodies = converter.convert(); if (!budget.check()) return {}; // 预算在最后一刻耗尽:不返回半成品 + // A43:转换成功但没有任何面/曲面时(纯线框、100/110/126 曲线、124 矩阵、 + // 308/408/314 子图实例等),旧实现靠默认单位方片伪造 body;现在给出明确 + // 诊断并返回空结果,而不是假 B-rep。仅在没有其他错误码时设置,避免覆盖 + // 超时/取消/解析失败等既有诊断。 + if (bodies.empty() && g_last_iges_error == IgesError::Ok) { + set_error(IgesError::UnsupportedEntity, + "IGES 中没有可用的曲面实体(仅线框/非曲面实体),未生成 B-rep 模型"); + } return bodies; } catch (const std::exception& e) { set_error(IgesError::ParseError, std::string("Conversion error: ") + e.what()); diff --git a/src/brep/step_index.cpp b/src/brep/step_index.cpp index 0cd72ea9..06210e1b 100644 --- a/src/brep/step_index.cpp +++ b/src/brep/step_index.cpp @@ -1,4 +1,5 @@ #include "vde/brep/step_index.h" +#include "vde/foundation/utf8_path.h" #include #include @@ -13,17 +14,10 @@ namespace { constexpr std::uint64_t kChunkBytes = 8ull << 20; constexpr std::uint64_t kOverlapBytes = 1ull << 20; -std::filesystem::path path_from_utf8(const std::string& value) { -#if defined(__cpp_char8_t) - return std::filesystem::path(reinterpret_cast(value.c_str())); -#else - return std::filesystem::u8path(value); -#endif -} - bool read_range(const std::string& path, std::uint64_t start, std::uint64_t len, std::vector& out) { - std::ifstream f(path_from_utf8(path), std::ios::binary); + std::ifstream f = + vde::foundation::open_ifstream_utf8(path, std::ios::binary); if (!f) return false; f.seekg(static_cast(start)); out.resize(static_cast(len)); @@ -93,7 +87,8 @@ StepIndexResult index_step_parallel(const std::string& path, unsigned threads) { StepIndexResult result; const auto t0 = std::chrono::steady_clock::now(); std::error_code ec; - const auto size = std::filesystem::file_size(path_from_utf8(path), ec); + const auto size = std::filesystem::file_size( + vde::foundation::path_from_utf8(path), ec); if (ec || size == 0) { result.error = "无法读取文件大小"; return result; diff --git a/src/foundation/format_detect.cpp b/src/foundation/format_detect.cpp index dfd80a56..a3595708 100644 --- a/src/foundation/format_detect.cpp +++ b/src/foundation/format_detect.cpp @@ -5,6 +5,7 @@ * 只读前缀探测、NUL/二进制安全、无异常(打不开文件/未知格式均返回 Unknown+reason)。 */ #include "vde/foundation/format_detect.h" +#include "vde/foundation/utf8_path.h" #include #include @@ -18,42 +19,12 @@ #include #include #include -#if defined(_WIN32) -# ifndef NOMINMAX -# define NOMINMAX -# endif -# include -#endif namespace vde::io { namespace { using Bytes = std::string_view; -std::filesystem::path fs_path_from_native(const std::string& value) { -#if defined(_WIN32) - const int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, - value.data(), static_cast(value.size()), - nullptr, 0); - if (len > 0) { - std::wstring wide(static_cast(len), L'\0'); - MultiByteToWideChar(CP_UTF8, 0, value.data(), - static_cast(value.size()), wide.data(), len); - return std::filesystem::path(wide); - } - const int ansi_len = MultiByteToWideChar(CP_ACP, 0, value.data(), - static_cast(value.size()), - nullptr, 0); - if (ansi_len <= 0) return {}; - std::wstring wide(static_cast(ansi_len), L'\0'); - MultiByteToWideChar(CP_ACP, 0, value.data(), static_cast(value.size()), - wide.data(), ansi_len); - return std::filesystem::path(wide); -#else - return std::filesystem::path(value); -#endif -} - constexpr size_t kMaxFileProbe = 8u * 1024u * 1024u; // 最多读取 8MB 前缀 Bytes view_of(const std::vector& bytes) { @@ -624,7 +595,10 @@ DetectedFormat detect_format(const std::vector& bytes, const std::strin } DetectedFormat detect_format_file(const std::string& path) { - std::ifstream f(path, std::ios::binary); + // 路径按 UTF-8 解释(Windows 经 path_from_utf8 转宽字符),中文路径可打开; + // ASCII 路径行为与旧窄字符 std::ifstream(path) 一致。 + std::ifstream f = + vde::foundation::open_ifstream_utf8(path, std::ios::binary); if (!f) { return make_result(FileFormat::Unknown, 0.0, "无法打开文件: " + path); } @@ -647,7 +621,8 @@ DetectedFormat detect_format_file(const std::string& path) { (static_cast(prefix[82]) << 16) | (static_cast(prefix[83]) << 24); std::error_code ec; - const auto fsize = std::filesystem::file_size(fs_path_from_native(path), ec); + const auto fsize = std::filesystem::file_size( + vde::foundation::path_from_utf8(path), ec); if (!ec && count > 0 && fsize == 84ull + 50ull * count) return make_result(FileFormat::Stl, 0.99, "binary STL(真实文件尺寸精确匹配)"); diff --git a/tests/brep/test_iges_export.cpp b/tests/brep/test_iges_export.cpp index 10808ea4..11cc4016 100644 --- a/tests/brep/test_iges_export.cpp +++ b/tests/brep/test_iges_export.cpp @@ -3,6 +3,7 @@ #include "vde/brep/modeling.h" #include "vde/brep/iges_export.h" #include "vde/brep/iges_import.h" +#include "vde/brep/measure.h" #include "vde/core/transform.h" #include #include @@ -11,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -388,18 +390,21 @@ TEST(IgesExportTest, TrimmedSurfacesOptionWrites144WithValidReferences) { const auto pd_it = parsed.pd.find(entry.first); ASSERT_NE(pd_it, parsed.pd.end()) << "144 DE 缺少 PD"; const auto fields = split_iges_params(pd_it->second); - ASSERT_GE(fields.size(), 4u) << "144 参数不足: " << pd_it->second; - const int surface_de = std::stoi(fields[0]); - const int outer = std::stoi(fields[1]); - const int inner = std::stoi(fields[2]); + // IGES 5.3 Type 144 参数:144, PTS(基面), N1(外环数), N2(内环数), + // PTO×N1(外环 508 DE), PTI×N2(内环 508 DE)。 + ASSERT_GE(fields.size(), 5u) << "144 参数不足: " << pd_it->second; + EXPECT_EQ(fields[0], "144") << "144 P 段首个参数必须是实体类型号"; + const int surface_de = std::stoi(fields[1]); + const int outer = std::stoi(fields[2]); + const int inner = std::stoi(fields[3]); EXPECT_GE(outer, 1); EXPECT_GE(inner, 0); - ASSERT_EQ(fields.size(), 3u + static_cast(outer + inner)); + ASSERT_EQ(fields.size(), 4u + static_cast(outer + inner)); const auto surface_it = parsed.de_type.find(surface_de); ASSERT_NE(surface_it, parsed.de_type.end()) << "144 基面 DE 悬空"; EXPECT_TRUE(surface_it->second == 108 || surface_it->second == 128) << "144 基面必须是 108/128,实际 " << surface_it->second; - for (std::size_t i = 3; i < fields.size(); ++i) { + for (std::size_t i = 4; i < fields.size(); ++i) { const int loop_de = std::stoi(fields[i]); const auto loop_it = parsed.de_type.find(loop_de); ASSERT_NE(loop_it, parsed.de_type.end()) << "144 loop DE 悬空"; @@ -478,8 +483,9 @@ TEST(IgesExportTest, TrimmedSurfacesOnBsplineSurfaceReferences128) { const auto pd_it = parsed.pd.find(entry.first); ASSERT_NE(pd_it, parsed.pd.end()); const auto fields = split_iges_params(pd_it->second); - ASSERT_GE(fields.size(), 4u); - const auto surface_it = parsed.de_type.find(std::stoi(fields[0])); + ASSERT_GE(fields.size(), 5u); + EXPECT_EQ(fields[0], "144") << "144 P 段首个参数必须是实体类型号"; + const auto surface_it = parsed.de_type.find(std::stoi(fields[1])); ASSERT_NE(surface_it, parsed.de_type.end()); EXPECT_TRUE(surface_it->second == 128 || surface_it->second == 108); } @@ -511,10 +517,12 @@ TEST(IgesExportTest, ColorsWrite314AndDeColorReferences) { const auto pd_it = parsed.pd.find(color_de); ASSERT_NE(pd_it, parsed.pd.end()); const auto fields = split_iges_params(pd_it->second); - ASSERT_EQ(fields.size(), 3u); - EXPECT_NEAR(parse_iges_real(fields[0]), 100.0, 1e-6); - EXPECT_NEAR(parse_iges_real(fields[1]), 0.0, 1e-6); + // 规范 314:314, R, G, B(0~100)。 + ASSERT_EQ(fields.size(), 4u); + EXPECT_EQ(fields[0], "314") << "314 P 段首个参数必须是实体类型号"; + EXPECT_NEAR(parse_iges_real(fields[1]), 100.0, 1e-6); EXPECT_NEAR(parse_iges_real(fields[2]), 0.0, 1e-6); + EXPECT_NEAR(parse_iges_real(fields[3]), 0.0, 1e-6); const int shell_de = first_de_of_type(parsed, 514); const int msbo_de = first_de_of_type(parsed, 186); @@ -532,10 +540,11 @@ TEST(IgesExportTest, ColorsWrite314AndDeColorReferences) { const int override_color = first_de_of_type(override_parsed, 314); ASSERT_GT(override_color, 0); const auto override_fields = split_iges_params(override_parsed.pd.at(override_color)); - ASSERT_EQ(override_fields.size(), 3u); - EXPECT_NEAR(parse_iges_real(override_fields[0]), 0.0, 1e-6); - EXPECT_NEAR(parse_iges_real(override_fields[1]), 100.0, 1e-6); - EXPECT_NEAR(parse_iges_real(override_fields[2]), 0.0, 1e-6); + ASSERT_EQ(override_fields.size(), 4u); + EXPECT_EQ(override_fields[0], "314"); + EXPECT_NEAR(parse_iges_real(override_fields[1]), 0.0, 1e-6); + EXPECT_NEAR(parse_iges_real(override_fields[2]), 100.0, 1e-6); + EXPECT_NEAR(parse_iges_real(override_fields[3]), 0.0, 1e-6); } TEST(IgesExportTest, InvalidColorIsSkippedSafely) { @@ -577,10 +586,13 @@ TEST(IgesExportTest, LayersWriteDeLevelAnd406Name) { ASSERT_GT(property_de, 0) << "缺少 406 图层 property"; EXPECT_EQ(parsed.de_form.at(property_de), 15) << "期望 406 Form 15 (Name)"; const auto fields = split_iges_params(parsed.pd.at(property_de)); - ASSERT_GE(fields.size(), 2u); - EXPECT_EQ(std::stoi(fields[0]), shell_de) << "406 应引用 shell DE"; - EXPECT_NE(fields[1].find("Layer_A"), std::string::npos) - << "406 名称为 " << fields[1]; + // 406 仅加类型号前缀;载荷仍是仓库私有的 [target_de, nHname](残余限制, + // 规范 406 属性表形式见 iges_export.cpp 注释),导入端不解析 406。 + ASSERT_GE(fields.size(), 3u); + EXPECT_EQ(fields[0], "406") << "406 P 段首个参数必须是实体类型号"; + EXPECT_EQ(std::stoi(fields[1]), shell_de) << "406 应引用 shell DE"; + EXPECT_NE(fields[2].find("Layer_A"), std::string::npos) + << "406 名称为 " << fields[2]; } TEST(IgesExportTest, InvalidLayerAndEmptyNameAreSafe) { @@ -764,3 +776,308 @@ TEST(IgesExportTest, RewrittenWriterScalesNearLinearlyWithFaceCount) { << "ms large=" << large_ms << "ms)"; } } + +// ═══════════════════════════════════════════════════════════ +// Wave19 A44:P 段实体类型号前缀 + 规范拓扑参数布局 +// +// 验收覆盖: +// (a) 导出→导入往返恢复拓扑与体积(box 6 面/12 边/8 顶点、 +// 球/圆柱、3 body 文件); +// (b) 每条 P 段记录首参数 == 该 DE 的实体类型号; +// (c) 502/504/508/510/514/186/144/314/406 参数个数与引用符合规范; +// (d) 80 列 / DE 计数 / terminate 计数 / 两次导出逐字节一致由既有用例覆盖。 +// ═══════════════════════════════════════════════════════════ + +namespace { + +double mesh_volume_for_test(const BrepModel& body, double deflection = 0.01) { + const auto m = body.to_mesh(deflection); + double v = 0.0; + for (std::size_t fi = 0; fi < m.num_faces(); ++fi) { + const auto fv = m.face_vertices(static_cast(fi)); + if (fv.size() < 3) continue; + for (std::size_t k = 1; k + 1 < fv.size(); ++k) { + const Point3D& a = m.vertex(fv[0]); + const Point3D& b = m.vertex(fv[k]); + const Point3D& c = m.vertex(fv[k + 1]); + v += a.dot(b.cross(c)); + } + } + return std::abs(v) / 6.0; +} + +void expect_volume_rel_for_test(const BrepModel& expected, const BrepModel& actual, + double rel, const char* label) { + const double want = volume(expected); + const double got = volume(actual); + EXPECT_NEAR(got, want, rel * std::max(1.0, std::abs(want))) << label; +} + +void expect_bounds_extent_for_test(const BrepModel& expected, const BrepModel& actual, + double abs_tol, const char* label) { + const auto want = expected.bounds().extent(); + const auto got = actual.bounds().extent(); + EXPECT_NEAR(got.x(), want.x(), abs_tol) << label; + EXPECT_NEAR(got.y(), want.y(), abs_tol) << label; + EXPECT_NEAR(got.z(), want.z(), abs_tol) << label; +} + +// 逐个 DE 校验 P 段首参数 == 实体类型号,返回出现过的类型集合。 +// 用 EXPECT(非 ASSERT),因为辅助函数有返回值。 +std::set pd_first_token_types(const std::string& iges, const char* label) { + const ParsedIges parsed = parse_iges_for_test(iges); + std::set seen; + EXPECT_FALSE(parsed.de_type.empty()) << label << ":没有解析到 DE"; + for (const auto& entry : parsed.de_type) { + seen.insert(entry.second); + const auto pd_it = parsed.pd.find(entry.first); + if (pd_it == parsed.pd.end()) { + EXPECT_TRUE(false) << label << ":DE " << entry.first << "(类型 " + << entry.second << ")缺少 P 段记录"; + continue; + } + const auto fields = split_iges_params(pd_it->second); + if (fields.empty()) { + EXPECT_TRUE(false) << label << ":DE " << entry.first << " P 段为空"; + continue; + } + EXPECT_EQ(fields[0], std::to_string(entry.second)) + << label << ":DE " << entry.first << "(类型 " << entry.second + << ")P 段首参数不是类型号,实际 PD=" << pd_it->second; + } + return seen; +} + +} // namespace + +// (b) 每个 P 段记录的首参数必须等于该实体的类型号(含 144/314/406)。 +TEST(IgesExportTest, EveryParameterRecordStartsWithEntityTypeNumber) { + auto box = make_box(2, 3, 4); + IgesExportOptions options; + options.write_trimmed_surfaces = true; + options.write_colors = true; + options.write_layers = true; + options.body_colors = {Color(0.25, 0.5, 0.75)}; + options.body_layers = {{3, "Layer_Spec"}}; + const std::set box_types = + pd_first_token_types(export_iges({box}, options), "box(all options)"); + for (int type : {108, 110, 116, 144, 186, 314, 406, 502, 504, 508, 510, 514}) { + EXPECT_EQ(box_types.count(type), 1u) + << "box 导出缺少类型 " << type << "(已出现类型数 " + << box_types.size() << ")"; + } + + auto sphere = make_sphere(3.0, 16, 12); + const std::set sphere_types = + pd_first_token_types(export_iges({sphere}), "sphere"); + for (int type : {128, 186, 502, 504, 508, 510, 514}) { + EXPECT_EQ(sphere_types.count(type), 1u) + << "sphere 导出缺少类型 " << type << "(已出现类型数 " + << sphere_types.size() << ")"; + } +} + +// (c) 拓扑实体参数布局:共享表 + 1 基行号引用 + 规范参数个数。 +TEST(IgesExportTest, TopologyEntitiesUseSpecParameterLayouts) { + auto box = make_box(2, 3, 4); + const ParsedIges parsed = parse_iges_for_test(export_iges({box})); + + EXPECT_EQ(count_de_type(parsed, 502), 1); + EXPECT_EQ(count_de_type(parsed, 504), 1); + EXPECT_EQ(count_de_type(parsed, 508), 6); + EXPECT_EQ(count_de_type(parsed, 510), 6); + EXPECT_EQ(count_de_type(parsed, 514), 1); + EXPECT_EQ(count_de_type(parsed, 186), 1); + EXPECT_EQ(count_de_type(parsed, 108), 6); + EXPECT_EQ(count_de_type(parsed, 110), 12); + EXPECT_EQ(count_de_type(parsed, 116), 8); + EXPECT_EQ(count_de_type(parsed, 144), 0) << "默认不写 144"; + + // 502: 502, N, (X,Y,Z)×N —— 顶点坐标共享表 + const int vertex_list_de = first_de_of_type(parsed, 502); + ASSERT_GT(vertex_list_de, 0); + const auto p502 = split_iges_params(parsed.pd.at(vertex_list_de)); + ASSERT_GE(p502.size(), 4u); + EXPECT_EQ(p502[0], "502"); + const int n_vertices = std::stoi(p502[1]); + EXPECT_GE(n_vertices, 8); + // 含类型号:502, N, (X,Y,Z)×N + EXPECT_EQ(p502.size(), 2u + 3u * static_cast(n_vertices)); + for (int i = 0; i < n_vertices; ++i) { + for (int k = 0; k < 3; ++k) { + EXPECT_TRUE(std::isfinite(parse_iges_real(p502[2 + 3 * i + k]))) + << "502 顶点第 " << i << " 行坐标非法"; + } + } + + // 504: 504, M, (CURVE, 502DE, SV_IDX, 502DE, EV_IDX)×M + const int edge_list_de = first_de_of_type(parsed, 504); + ASSERT_GT(edge_list_de, 0); + const auto p504 = split_iges_params(parsed.pd.at(edge_list_de)); + ASSERT_GE(p504.size(), 6u); + EXPECT_EQ(p504[0], "504"); + const int n_edges = std::stoi(p504[1]); + EXPECT_GE(n_edges, 12); + // 含类型号:504, M, (…)×M + EXPECT_EQ(p504.size(), 2u + 5u * static_cast(n_edges)); + for (int i = 0; i < n_edges; ++i) { + const int curve_de = std::stoi(p504[2 + 5 * i]); + EXPECT_GT(curve_de, 0) << "504 第 " << i + 1 << " 行曲线 DE 非法"; + const auto curve_it = parsed.de_type.find(curve_de); + ASSERT_NE(curve_it, parsed.de_type.end()); + EXPECT_TRUE(curve_it->second == 110 || curve_it->second == 100 || + curve_it->second == 126) + << "504 曲线实体应为 110/100/126,实际 " << curve_it->second; + EXPECT_EQ(std::stoi(p504[3 + 5 * i]), vertex_list_de) + << "504 起点顶点表 DE 必须引用 502"; + EXPECT_EQ(std::stoi(p504[5 + 5 * i]), vertex_list_de) + << "504 终点顶点表 DE 必须引用 502"; + const int sv = std::stoi(p504[4 + 5 * i]); + const int ev = std::stoi(p504[6 + 5 * i]); + EXPECT_GE(sv, 1); + EXPECT_LE(sv, n_vertices); + EXPECT_GE(ev, 1); + EXPECT_LE(ev, n_vertices); + } + + // 508: 508, N, (TYPE=0, 504DE, EDGE_IDX, ORIENT, NPARAM=0)×N + for (const auto& entry : parsed.de_type) { + if (entry.second != 508) continue; + const auto fields = split_iges_params(parsed.pd.at(entry.first)); + ASSERT_GE(fields.size(), 6u) << "508 参数不足"; + EXPECT_EQ(fields[0], "508"); + const int n = std::stoi(fields[1]); + // 含类型号:508, N, (…)×N + EXPECT_EQ(fields.size(), 2u + 5u * static_cast(n)); + for (int i = 0; i < n; ++i) { + EXPECT_EQ(fields[2 + 5 * i], "0") << "508 条目类型应为 0(边表)"; + EXPECT_EQ(std::stoi(fields[3 + 5 * i]), edge_list_de) + << "508 条目必须引用 504 边表"; + const int edge_idx = std::stoi(fields[4 + 5 * i]); + EXPECT_GE(edge_idx, 1); + EXPECT_LE(edge_idx, n_edges); + const int orientation = std::stoi(fields[5 + 5 * i]); + EXPECT_TRUE(orientation == 0 || orientation == 1); + EXPECT_EQ(fields[6 + 5 * i], "0") << "508 条目 NPARAM 应为 0"; + } + } + + // 510: 510, SURF, NLOOPS, OUTERLOOPFLAG=0, LOOP×N + for (const auto& entry : parsed.de_type) { + if (entry.second != 510) continue; + const auto fields = split_iges_params(parsed.pd.at(entry.first)); + ASSERT_GE(fields.size(), 5u) << "510 参数不足"; + EXPECT_EQ(fields[0], "510"); + const int surface_de = std::stoi(fields[1]); + const auto surface_it = parsed.de_type.find(surface_de); + ASSERT_NE(surface_it, parsed.de_type.end()); + EXPECT_TRUE(surface_it->second == 108 || surface_it->second == 128); + const int n_loops = std::stoi(fields[2]); + EXPECT_EQ(fields.size(), 4u + static_cast(n_loops)); + EXPECT_EQ(fields[3], "0") << "第十位 OUTERLOOPFLAG 应为 0(外环为首环)"; + for (int i = 0; i < n_loops; ++i) { + const int loop_de = std::stoi(fields[4 + i]); + const auto loop_it = parsed.de_type.find(loop_de); + ASSERT_NE(loop_it, parsed.de_type.end()); + EXPECT_EQ(loop_it->second, 508) << "510 环必须是 508"; + } + } + + // 514: 514, NFACES, (FACE, 0)×NFACES + const int shell_de = first_de_of_type(parsed, 514); + ASSERT_GT(shell_de, 0); + const auto p514 = split_iges_params(parsed.pd.at(shell_de)); + ASSERT_GE(p514.size(), 4u); + EXPECT_EQ(p514[0], "514"); + const int n_faces = std::stoi(p514[1]); + EXPECT_EQ(n_faces, 6); + EXPECT_EQ(p514.size(), 2u + 2u * static_cast(n_faces)); + for (int i = 0; i < n_faces; ++i) { + const int face_de = std::stoi(p514[2 + 2 * i]); + const auto face_it = parsed.de_type.find(face_de); + ASSERT_NE(face_it, parsed.de_type.end()); + EXPECT_EQ(face_it->second, 510); + EXPECT_EQ(p514[3 + 2 * i], "0") << "514 面方向字段应为 0"; + } + + // 186: 186, SHELL, NSHELLS=1, NVOIDS=0 + const int msbo_de = first_de_of_type(parsed, 186); + ASSERT_GT(msbo_de, 0); + const auto p186 = split_iges_params(parsed.pd.at(msbo_de)); + ASSERT_EQ(p186.size(), 4u); + EXPECT_EQ(p186[0], "186"); + EXPECT_EQ(std::stoi(p186[1]), shell_de); + EXPECT_EQ(p186[2], "1"); + EXPECT_EQ(p186[3], "0"); +} + +// (a) box 往返:6 面 / 12 边 / 8 顶点,体积相对误差 ≤1e-9,边界一致。 +TEST(IgesExportTest, RoundtripBoxRestoresTopologyAndVolume) { + auto box = make_box(2.0, 3.0, 4.0); + const std::string iges = export_iges({box}); + EXPECT_EQ(iges, export_iges({box})) << "同输入两次导出必须逐字节一致"; + + const auto bodies = import_iges_from_string(iges); + EXPECT_EQ(iges_last_error(), IgesError::Ok) << iges_last_error_message(); + ASSERT_EQ(bodies.size(), 1u); + EXPECT_EQ(bodies[0].num_faces(), 6u); + EXPECT_EQ(bodies[0].num_edges(), 12u); + EXPECT_EQ(bodies[0].num_vertices(), 8u); + expect_volume_rel_for_test(box, bodies[0], 1e-9, "box 往返体积"); + expect_bounds_extent_for_test(box, bodies[0], 1e-9, "box 往返边界"); +} + +// (a) 球与圆柱往返:面数保持,网格体积/解析体积与边界在容差内。 +TEST(IgesExportTest, RoundtripSphereAndFacetedCylinderPreserveGeometry) { + auto sphere = make_sphere(3.0, 16, 12); + { + SCOPED_TRACE("sphere"); + const auto bodies = import_iges_from_string(export_iges({sphere})); + EXPECT_EQ(iges_last_error(), IgesError::Ok) << iges_last_error_message(); + ASSERT_EQ(bodies.size(), 1u); + EXPECT_EQ(bodies[0].num_faces(), sphere.num_faces()); + const double want = mesh_volume_for_test(sphere); + const double got = mesh_volume_for_test(bodies[0]); + EXPECT_NEAR(got, want, 1.5e-2 * std::max(1.0, std::abs(want))); + expect_bounds_extent_for_test(sphere, bodies[0], 1e-4, "sphere 往返边界"); + } + auto cylinder = make_cylinder(4.0, 10.0, 32); + { + SCOPED_TRACE("cylinder"); + const auto bodies = import_iges_from_string(export_iges({cylinder})); + EXPECT_EQ(iges_last_error(), IgesError::Ok) << iges_last_error_message(); + ASSERT_EQ(bodies.size(), 1u); + EXPECT_EQ(bodies[0].num_faces(), cylinder.num_faces()); + expect_volume_rel_for_test(cylinder, bodies[0], 1e-5, "cylinder 往返体积"); + expect_bounds_extent_for_test(cylinder, bodies[0], 1e-5, "cylinder 往返边界"); + } +} + +// (a) 3 body 文件往返:每个 186 恢复为一个 body。 +TEST(IgesExportTest, RoundtripThreeBodiesRestoresAllBodies) { + auto make_at = [](double x) { + auto body = make_box(2.0, 2.0, 2.0); + body.transform(vde::core::translate(x, 0.0, 0.0)); + return body; + }; + const std::vector input{make_at(0.0), make_at(100.0), make_at(200.0)}; + const auto bodies = import_iges_from_string(export_iges(input)); + EXPECT_EQ(iges_last_error(), IgesError::Ok) << iges_last_error_message(); + ASSERT_EQ(bodies.size(), 3u); + + std::size_t total_faces = 0; + std::vector min_x; + for (const auto& body : bodies) { + EXPECT_EQ(body.num_faces(), 6u); + EXPECT_NEAR(volume(body), 8.0, 1e-8) << "3-body 往返体积"; + total_faces += body.num_faces(); + min_x.push_back(body.bounds().min().x()); + } + EXPECT_EQ(total_faces, 18u); + std::sort(min_x.begin(), min_x.end()); + ASSERT_EQ(min_x.size(), 3u); + // make_box(2,2,2) 以原点为中心:min_x 基准 -1,平移后 99/199。 + EXPECT_NEAR(min_x[0], -1.0, 1e-6); + EXPECT_NEAR(min_x[1], 99.0, 1e-6); + EXPECT_NEAR(min_x[2], 199.0, 1e-6); +} diff --git a/tests/brep/test_iges_import.cpp b/tests/brep/test_iges_import.cpp index cb162b99..4c413b4c 100644 --- a/tests/brep/test_iges_import.cpp +++ b/tests/brep/test_iges_import.cpp @@ -181,12 +181,11 @@ TEST(IgesImport, MinimalLine) { // Line: X1,Y1,Z1,X2,Y2,Z2 = (0,0,0) to (10,0,0) b.add_pd("0.0,0.0,0.0,10.0,0.0,0.0;", 1); + // A43:纯线框 IGES 不再用默认单位方片伪造 body;返回空结果并给明确诊断。 auto bodies = import_iges_from_string(b.build()); - EXPECT_EQ(iges_last_error(), IgesError::Ok); - - // Should produce at least one body (fallback mode with a surface) - ASSERT_FALSE(bodies.empty()); - EXPECT_GT(bodies[0].num_surfaces(), 0u); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity); + EXPECT_FALSE(std::string(iges_last_error_message()).empty()); } TEST(IgesImport, SingleArc) { @@ -199,9 +198,11 @@ TEST(IgesImport, SingleArc) { b.add_de(100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); b.add_pd("0.0,0.0,0.0,5.0,0.0,5.0,0.0;", 1); + // A43:弧是曲线实体,不再伪造曲面/实体。 auto bodies = import_iges_from_string(b.build()); - EXPECT_EQ(iges_last_error(), IgesError::Ok); - ASSERT_FALSE(bodies.empty()); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity); + EXPECT_FALSE(std::string(iges_last_error_message()).empty()); } TEST(IgesImport, SingleLine110) { @@ -213,8 +214,10 @@ TEST(IgesImport, SingleLine110) { b.add_de(110, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); b.add_pd("1.0,2.0,3.0,4.0,5.0,6.0;", 1); + // A43:只有 110 线 → 空结果 + 无曲面实体诊断。 auto bodies = import_iges_from_string(b.build()); - EXPECT_EQ(iges_last_error(), IgesError::Ok); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity); } TEST(IgesImport, CylinderSurface) { @@ -298,8 +301,10 @@ TEST(IgesImport, BSplineCurve) { b.add_de(126, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); b.add_pd("5,2,0,0,0,3,0.0,0.0,0.0,0.5,1.0,1.0,0.0,0.0,0.0,2.0,3.0,0.0,5.0,1.0,0.0,7.0,4.0,0.0;", 1); + // A43:只有 126 曲线 → 空结果 + 无曲面实体诊断。 auto bodies = import_iges_from_string(b.build()); - EXPECT_EQ(iges_last_error(), IgesError::Ok); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity); } TEST(IgesImport, BSplineSurface) { @@ -386,8 +391,10 @@ TEST(IgesImport, CopiousDataPolyline) { b.add_de(106, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); b.add_pd("1,4,0.0,0.0,0.0,2.0,0.0,0.0,2.0,2.0,0.0,0.0,2.0,0.0;", 1); + // A43:只有 106 折线 → 空结果 + 无曲面实体诊断。 auto bodies = import_iges_from_string(b.build()); - EXPECT_EQ(iges_last_error(), IgesError::Ok); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity); } TEST(IgesImport, ManifoldSolidBox) { @@ -503,8 +510,10 @@ TEST(IgesImport, DEExponentNotation) { b.add_de(110, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); b.add_pd("1.0D+2,2.0D+1,3.5D+0,4.0D-1,5.0D-2,6.0D+3;", 1); + // A43:只有 110 线 → 空结果 + 无曲面实体诊断。 auto bodies = import_iges_from_string(b.build()); - EXPECT_EQ(iges_last_error(), IgesError::Ok); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity); } TEST(IgesImport, MultipleSurfaces) { @@ -547,9 +556,12 @@ TEST(IgesImport, ToleratesOddDirectoryEntries) { b.d_lines.push_back(de_line({110, 1, 0, 0, 0, 0, 0, 0, 0}, 'D', static_cast(b.d_lines.size() + 1))); + // A43:容忍奇数条 D 记录之后仍是纯线框文件 → 空结果 + 无曲面实体诊断。 const auto bodies = import_iges_from_string(b.build()); - EXPECT_EQ(iges_last_error(), IgesError::Ok) << iges_last_error_message(); - ASSERT_FALSE(bodies.empty()); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity) + << iges_last_error_message(); + EXPECT_FALSE(std::string(iges_last_error_message()).empty()); } // VDE-1192: 第 73 列段标识缺失时按内容 + 段顺序推断(旧写出器产物形态) @@ -565,9 +577,12 @@ TEST(IgesImport, InfersMissingSectionLetters) { const std::string data = s_line + "\n" + g_line + "\n" + d1 + "\n" + d2 + "\n" + p_line + "\n" + t_line + "\n"; + // A43:只有一条 110 线 → 空结果 + 无曲面实体诊断。 const auto bodies = import_iges_from_string(data); - EXPECT_EQ(iges_last_error(), IgesError::Ok) << iges_last_error_message(); - ASSERT_FALSE(bodies.empty()); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity) + << iges_last_error_message(); + EXPECT_FALSE(std::string(iges_last_error_message()).empty()); } // VDE-1192: 文件级 round-trip(写入临时文件 → import_iges(path) 读回) @@ -615,9 +630,11 @@ TEST(IgesImport, VerifyArcGeometry) { b.add_de(100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); b.add_pd("0.0,0.0,0.0,5.0,0.0,0.0,5.0;", 1); + // A43:弧是曲线实体,不再伪造曲面/实体。 auto bodies = import_iges_from_string(b.build()); - EXPECT_EQ(iges_last_error(), IgesError::Ok); - ASSERT_FALSE(bodies.empty()); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity); + EXPECT_FALSE(std::string(iges_last_error_message()).empty()); } TEST(IgesImport, FullCircleArc) { @@ -629,9 +646,62 @@ TEST(IgesImport, FullCircleArc) { b.add_de(100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); b.add_pd("0.0,0.0,0.0,10.0,0.0,10.0,0.0;", 1); + // A43:整圆是曲线实体,不再伪造曲面/实体。 auto bodies = import_iges_from_string(b.build()); - EXPECT_EQ(iges_last_error(), IgesError::Ok); - ASSERT_FALSE(bodies.empty()); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity); + EXPECT_FALSE(std::string(iges_last_error_message()).empty()); +} + +// A43(Wave19):纯线框/曲线/点文件不得再靠默认单位方片伪造 body。 +TEST(IgesImport, PureWireframeIgesHasNoPlaceholderFaces) { + IgesBuilder b; + b.add_s_line("A43 pure wireframe file"); + b.add_g_line(standard_global()); + + // 两条 110 线 + 一个 116 点:全部是非曲面实体。 + b.add_de(110, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); + b.add_pd("0.0,0.0,0.0,10.0,0.0,0.0;", 1); + b.add_de(110, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); + b.add_pd("0.0,5.0,0.0,10.0,5.0,0.0;", 2); + b.add_de(116, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); + b.add_pd("1.0,2.0,3.0;", 3); + + const auto bodies = import_iges_from_string(b.build()); + EXPECT_TRUE(bodies.empty()) << "纯线框 IGES 不得伪造 body"; + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity) + << iges_last_error_message(); + EXPECT_FALSE(std::string(iges_last_error_message()).empty()) + << "纯线框文件必须给出明确诊断"; +} + +// A43(Wave19):「1 个 128 曲面 + 若干线框」只能有 1 个真实面(真曲面实体数), +// 不得把每条 110/116 都变成占位面。 +TEST(IgesImport, SingleSurfacePlusWireframeGivesOneRealFace) { + IgesBuilder b; + b.add_s_line("A43 surface plus wireframe"); + b.add_g_line(standard_global()); + + // 1 个 128 双线性曲面(2×2 控制点,1×1 次) + b.add_de(128, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); + b.add_pd("1,1,1,1,0,0,0,0,0,0,0,0.0,1.0,0.0,1.0,0.0,0.0,0.0;", 1); + + // 3 条 110 线 + 1 个 116 点,均不得生成面。 + b.add_de(110, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); + b.add_pd("0.0,0.0,0.0,10.0,0.0,0.0;", 2); + b.add_de(110, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); + b.add_pd("0.0,5.0,0.0,10.0,5.0,0.0;", 3); + b.add_de(110, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); + b.add_pd("0.0,0.0,0.0,0.0,0.0,10.0;", 4); + b.add_de(116, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); + b.add_pd("1.0,2.0,3.0;", 5); + + const auto bodies = import_iges_from_string(b.build()); + ASSERT_EQ(iges_last_error(), IgesError::Ok) << iges_last_error_message(); + ASSERT_EQ(bodies.size(), 1u); + EXPECT_EQ(bodies[0].num_faces(), 1u) << "只应有 128 曲面这 1 个真实面"; + EXPECT_EQ(bodies[0].num_surfaces(), 1u); + EXPECT_EQ(bodies[0].num_edges(), 0u) << "线框实体不得被当作裁剪边"; } // ───────────────────────────────────────────────────────────── @@ -834,8 +904,9 @@ TEST(IgesImport, NormalTimeBudgetStillSucceeds) { IgesBuilder b; b.add_s_line("normal budget test"); b.add_g_line(standard_global()); - b.add_de(110, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); - b.add_pd("0.0,0.0,0.0,10.0,0.0,0.0;", 1); + // A43:预算用例需要一个真值曲面实体;纯线框文件按新语义返回空结果 + 诊断。 + b.add_de(128, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); + b.add_pd("1,1,1,1,0,0,0,0,0,0,0,0.0,1.0,0.0,1.0,0.0,0.0,0.0;", 1); IgesImportOptions options; options.max_seconds = 5.0; @@ -1084,6 +1155,17 @@ std::string a36_corpus_path(const std::string& name) { return {}; } +// A43:真实语料定位(可指定 corpus/real 下子目录),gtest 由构建脚本在仓库根运行。 +std::string a43_corpus_path(const char* subdir, const std::string& name) { + static const char* kPrefixes[] = {"corpus/", "../corpus/", "../../corpus/"}; + for (const char* prefix : kPrefixes) { + const std::string path = std::string(prefix) + subdir + "/" + name; + std::ifstream f(path); + if (f.good()) return path; + } + return {}; +} + } // namespace TEST(IgesImport, SpecAndLegacyLayoutBoxAgree) { @@ -1121,10 +1203,13 @@ TEST(IgesImport, SpecLayoutBadPrefixIsNotStrippedFromLegacyFile) { b.add_g_line(standard_global()); b.add_de(110, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0); b.add_pd("110.0,0.0,0.0,10.0,0.0,0.0;", 1); + // A43:该文件只有一条 110 线(纯线框);无论 X1=110 是否被误当类型前缀, + // 都不得再靠默认单位方片伪造面。这里回归「纯线框 → 空结果 + 诊断」。 const auto bodies = import_iges_from_string(b.build()); - EXPECT_EQ(iges_last_error(), IgesError::Ok); - ASSERT_FALSE(bodies.empty()); - EXPECT_GT(bodies[0].num_surfaces(), 0u); + EXPECT_TRUE(bodies.empty()); + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity) + << iges_last_error_message(); + EXPECT_FALSE(std::string(iges_last_error_message()).empty()); } // 真实 IGES 语料(corpus/real/iges,605 个文件)门控用例:文件缺失时跳过。 @@ -1355,6 +1440,79 @@ TEST(IgesImport, RealCorpusTrimmedSurfacesHaveEdges) { EXPECT_EQ(edges, 0u) << untrimmed << ":文件无 141–144 实体,不应有裁剪边"; } } + +// A43(Wave19)真实语料门控:29 个 corpus/real/igs 纯线框文件 + 5 个 +// corpus/real/iges no_topo_nosurf 文件必须返回空结果 + UnsupportedEntity 诊断, +// 不再伪造占位面(A41 基线上这两组 faces 合计 17,511 / 60)。 +TEST(IgesImport, RealCorpusWireframeFilesProduceNoBodies) { + struct Case { + const char* dir; + const char* name; + }; + static const Case kCases[] = { + {"real/igs", "rotar_1.igs"}, + {"real/igs", "弹簧.igs"}, + {"real/igs", "滑道1下板.igs"}, + {"real/iges", "25.igs"}, + {"real/iges", "83.igs"}, + {"real/iges", "curve_geom_plate.igs"}, + }; + size_t found = 0; + for (const auto& c : kCases) { + const std::string path = a43_corpus_path(c.dir, c.name); + if (path.empty()) continue; + ++found; + SCOPED_TRACE(path); + IgesImportOptions options; + options.max_seconds = 120.0; + const auto bodies = import_iges(path, options); + EXPECT_TRUE(bodies.empty()) << path << ":纯线框/无曲面文件不得产出 body"; + EXPECT_EQ(iges_last_error(), IgesError::UnsupportedEntity) + << path << ": " << iges_last_error_message(); + EXPECT_FALSE(std::string(iges_last_error_message()).empty()) << path; + } + if (found == 0) + GTEST_SKIP() << "真实 IGES 语料缺失(corpus/real/igs、corpus/real/iges)"; +} + +// A43(Wave19)真实语料门控:30 个 no_topo_surf 文件里 6 个无 141–144 的文件走 +// else 兜底分支后,只保留真值曲面实体的整域隐式面(占位面 347→170;24 个含 +// 141–144 的文件走裁剪分支,逐件不变)。期望值 = 各文件真值曲面实体数 +// (108/128/120/122…),不是实体总数。基线:build/a38_probe_final.json。 +TEST(IgesImport, RealCorpusNoTrimSurfFilesDropPlaceholderFaces) { + struct Case { + const char* name; + size_t faces; + }; + static const Case kCases[] = { + {"1__2.igs", 32}, + {"17.igs", 32}, + {"3.igs", 33}, + {"82__2.igs", 32}, + {"97.igs", 37}, + {"occ__OCC17026-1__2.igs", 4}, + }; + size_t found = 0; + for (const auto& c : kCases) { + const std::string path = a36_corpus_path(c.name); + if (path.empty()) continue; + ++found; + SCOPED_TRACE(path); + IgesImportOptions options; + options.max_seconds = 120.0; + const auto bodies = import_iges(path, options); + EXPECT_EQ(iges_last_error(), IgesError::Ok) << iges_last_error_message(); + ASSERT_EQ(bodies.size(), 1u) << path; + size_t faces = 0, edges = 0; + for (const auto& body : bodies) { + faces += body.num_faces(); + edges += body.num_edges(); + } + EXPECT_EQ(faces, c.faces) << path << ":应只保留真值曲面实体的隐式面"; + EXPECT_EQ(edges, 0u) << path; + } + if (found == 0) GTEST_SKIP() << "真实 IGES 语料缺失(corpus/real/iges)"; +} // ───────────────────────────────────────────────────────────── // Wave18(A39):UTF-8 路径回归 // diff --git a/tests/brep/test_step_import.cpp b/tests/brep/test_step_import.cpp index 70b1eb46..816f1636 100644 --- a/tests/brep/test_step_import.cpp +++ b/tests/brep/test_step_import.cpp @@ -6,6 +6,7 @@ #include "vde/brep/measure.h" #include "vde/brep/brep_validate.h" #include "vde/brep/format_io.h" +#include "vde/brep/step_index.h" #include "vde/foundation/utf8_path.h" #include #include @@ -4034,6 +4035,74 @@ bool a39_step_has_non_ascii(const std::string& text) { } // namespace // 临时目录 + UTF-8 中文名(含 ① U+2460)+ 最小 STEP 盒 → import_cad 必须成功。 +// ───────────────────────────────────────────────────────────── +// Wave19(A46):UTF-8 路径回归(STEP 并行实体索引) +// +// step_index.cpp 原自带一份 file-local path_from_utf8(与共享 header 重复); +// 改用 include/vde/foundation/utf8_path.h 后本用例锁定:UTF-8 中文名路径经 +// index_step_parallel 与内存 index_step_buffer 结果逐实体一致,且 +// path_from_utf8/path_to_utf8 往返稳定;不存在的路径返回 ok=false+error 不抛异常。 +// ───────────────────────────────────────────────────────────── +TEST(StepImport, Utf8PathIndexStepParallelMatchesBuffer) { + const std::filesystem::path dir = a39_step_temp_dir("vde_a46_utf8_step_index"); + const std::string name = "索引_模型_①.stp"; + const std::filesystem::path file = dir / vde::foundation::path_from_utf8(name); + const std::string fixture = make_box_step(); + + std::ofstream out(file, std::ios::binary); + ASSERT_TRUE(out.is_open()) + << "无法写入 UTF-8 测试文件:" << vde::foundation::path_to_utf8(file); + out.write(fixture.data(), static_cast(fixture.size())); + out.close(); + ASSERT_TRUE(out.good()) + << "写入 UTF-8 测试文件失败:" << vde::foundation::path_to_utf8(file); + + const std::string utf8_path = vde::foundation::path_to_utf8(file); + ASSERT_TRUE(a39_step_has_non_ascii(utf8_path)) + << "测试前置条件不成立:路径不是非 ASCII:" << utf8_path; + // 共享 header 的往返语义:UTF-8 路径 → path → UTF-8 路径逐字节一致。 + EXPECT_EQ(vde::foundation::path_to_utf8( + vde::foundation::path_from_utf8(utf8_path)), + utf8_path); + + const vde::brep::StepIndexResult on_disk = + vde::brep::index_step_parallel(utf8_path, 2); + ASSERT_TRUE(on_disk.ok) << "UTF-8 路径索引失败:error=" << on_disk.error; + EXPECT_EQ(on_disk.bytes, static_cast(fixture.size())); + ASSERT_FALSE(on_disk.entities.empty()); + + // 与内存索引逐实体比对(id/offset/length/type 全等)。 + const vde::brep::StepIndexResult in_memory = + vde::brep::index_step_buffer(fixture); + ASSERT_TRUE(in_memory.ok); + ASSERT_EQ(on_disk.entities.size(), in_memory.entities.size()); + for (std::size_t i = 0; i < on_disk.entities.size(); ++i) { + EXPECT_EQ(on_disk.entities[i].id, in_memory.entities[i].id) << i; + EXPECT_EQ(on_disk.entities[i].offset, in_memory.entities[i].offset) << i; + EXPECT_EQ(on_disk.entities[i].length, in_memory.entities[i].length) << i; + EXPECT_EQ(on_disk.entities[i].type, in_memory.entities[i].type) << i; + } + bool found_point_1 = false; + for (const auto& entity : on_disk.entities) { + if (entity.id == 1 && entity.type == "CARTESIAN_POINT") found_point_1 = true; + } + EXPECT_TRUE(found_point_1) << utf8_path; + std::cout << "[A46] STEP UTF-8 路径索引成功:path=" << utf8_path + << " bytes=" << on_disk.bytes + << " entities=" << on_disk.entities.size() << std::endl; + + // 不存在的 UTF-8 路径:必须返回 ok=false + error,不得抛异常。 + const std::filesystem::path missing_file = + dir / vde::foundation::path_from_utf8("不存在_索引_②.stp"); + const vde::brep::StepIndexResult missing = vde::brep::index_step_parallel( + vde::foundation::path_to_utf8(missing_file), 2); + EXPECT_FALSE(missing.ok); + EXPECT_FALSE(missing.error.empty()); + + std::error_code cleanup_ec; + std::filesystem::remove_all(dir, cleanup_ec); +} + TEST(StepImport, Utf8PathImportCadSucceeds) { const std::filesystem::path dir = a39_step_temp_dir("vde_a39_utf8_step"); const std::string name = "导出_模型_①.stp"; diff --git a/tests/foundation/test_format_detect.cpp b/tests/foundation/test_format_detect.cpp index 0239f0d8..fcc5866b 100644 --- a/tests/foundation/test_format_detect.cpp +++ b/tests/foundation/test_format_detect.cpp @@ -1,8 +1,10 @@ #include "vde/foundation/format_detect.h" +#include "vde/foundation/utf8_path.h" #include #include +#include #include #include #include @@ -343,6 +345,111 @@ TEST(FormatDetect, FileApi) { EXPECT_FALSE(empty.reason.empty()); } +// ───────────────────────────────────────────────────────────── +// Wave19(A46):UTF-8 路径回归(detect_format_file 文件入口) +// +// detect_format_file 原先用窄字符 std::ifstream(path) 打开路径;Windows 下 +// 窄字符路径按 ANSI 代码页(本机 CP936)解释,UTF-8 中文名文件存在也会 +// 「无法打开文件」。修复后统一走 vde::foundation::path_from_utf8 / +// open_ifstream_utf8,ASCII 路径行为不变。以下用例写 UTF-8 中文名临时文件, +// 覆盖二进制魔数探测、IGES 80 列启发式、binary STL 真实尺寸复核、失败诊断。 +// ───────────────────────────────────────────────────────────── +namespace { + +std::filesystem::path a46_utf8_temp_file(const std::string& name) { + return vde::foundation::path_from_utf8(std::string(VDE_TEST_TEMP_DIR) + name); +} + +bool a46_has_non_ascii(const std::string& text) { + for (unsigned char byte : text) { + if (byte >= 0x80) return true; + } + return false; +} + +bool a46_write_file(const std::filesystem::path& file, const std::string& bytes) { + std::ofstream out(file, std::ios::binary); + if (!out.is_open()) return false; + out.write(bytes.data(), static_cast(bytes.size())); + out.close(); + return out.good(); +} + +std::string a46_iges_fixture() { + std::string data; + for (const char* section : {"S", "G", "D"}) { + std::string line(72, ' '); + line += section; + line += " 1"; + line.resize(80, ' '); + data += line; + data.push_back('\n'); + } + return data; +} + +} // namespace + +TEST(FormatDetect, Utf8PathFileApiStep) { + const std::filesystem::path file = a46_utf8_temp_file("探测_格式_①.stp"); + ASSERT_TRUE(a46_write_file( + file, "ISO-10303-21;\nHEADER;\nFILE_SCHEMA(('AP214'));\nENDSEC;\n")) + << "无法写入 UTF-8 测试文件:" << vde::foundation::path_to_utf8(file); + const std::string utf8_path = vde::foundation::path_to_utf8(file); + ASSERT_TRUE(a46_has_non_ascii(utf8_path)) + << "测试前置条件不成立:路径不是非 ASCII:" << utf8_path; + const DetectedFormat d = detect_format_file(utf8_path); + std::error_code ec; + std::filesystem::remove(file, ec); + EXPECT_EQ(d.format, FileFormat::Step) << "reason=" << d.reason; + EXPECT_GT(d.confidence, 0.9) << d.reason; + EXPECT_FALSE(d.reason.empty()); +} + +TEST(FormatDetect, Utf8PathFileApiIges) { + const std::filesystem::path file = a46_utf8_temp_file("探测_格式_②.igs"); + ASSERT_TRUE(a46_write_file(file, a46_iges_fixture())) + << "无法写入 UTF-8 测试文件:" << vde::foundation::path_to_utf8(file); + const std::string utf8_path = vde::foundation::path_to_utf8(file); + ASSERT_TRUE(a46_has_non_ascii(utf8_path)) + << "测试前置条件不成立:路径不是非 ASCII:" << utf8_path; + const DetectedFormat d = detect_format_file(utf8_path); + std::error_code ec; + std::filesystem::remove(file, ec); + EXPECT_EQ(d.format, FileFormat::Iges) << "reason=" << d.reason; + EXPECT_GT(d.confidence, 0.5) << d.reason; +} + +TEST(FormatDetect, Utf8PathFileApiBinaryStlExactSize) { + // 84 字节头 + 1 个 50 字节三角:detect_format_file 的 STL 分支用真实 + // 文件尺寸(file_size)复核 u32 三角数,此处覆盖该分支的 UTF-8 路径。 + std::string bytes(84 + 50, '\0'); + bytes[80] = 1; + const std::filesystem::path file = a46_utf8_temp_file("探测_STL_③.stl"); + ASSERT_TRUE(a46_write_file(file, bytes)) + << "无法写入 UTF-8 测试文件:" << vde::foundation::path_to_utf8(file); + const std::string utf8_path = vde::foundation::path_to_utf8(file); + ASSERT_TRUE(a46_has_non_ascii(utf8_path)) + << "测试前置条件不成立:路径不是非 ASCII:" << utf8_path; + const DetectedFormat d = detect_format_file(utf8_path); + std::error_code ec; + std::filesystem::remove(file, ec); + EXPECT_EQ(d.format, FileFormat::Stl) << "reason=" << d.reason; + EXPECT_GE(d.confidence, 0.9) << d.reason; + EXPECT_NE(d.reason.find("真实文件尺寸精确匹配"), std::string::npos) << d.reason; +} + +TEST(FormatDetect, Utf8PathMissingFileKeepsDiagnostic) { + const std::filesystem::path file = a46_utf8_temp_file("不存在的_格式_④.stp"); + const std::string utf8_path = vde::foundation::path_to_utf8(file); + ASSERT_TRUE(a46_has_non_ascii(utf8_path)) + << "测试前置条件不成立:路径不是非 ASCII:" << utf8_path; + const DetectedFormat d = detect_format_file(utf8_path); + EXPECT_EQ(d.format, FileFormat::Unknown); + EXPECT_FALSE(d.reason.empty()); + EXPECT_NE(d.reason.find("无法打开文件"), std::string::npos) << d.reason; +} + TEST(FormatDetect, BinaryStlNotIgesOnColumn73Byte) { // 80 字节头 + u32 count + 50 字节三角 = 134 字节;第 73 字节是 'S', // 旧顺序会先命中 IGES 的 80 列弱启发式。