Skip to content

CAD surface export: bisect self-intersecting UV trim chords at tangent cusps - #376

Merged
10-X-eng merged 4 commits into
stevecad:mainfrom
merchantg-cmyk:swarm/360-cusp
Nov 22, 2026
Merged

10-X-eng merged 4 commits into
stevecad:mainfrom
merchantg-cmyk:swarm/360-cusp

Conversation

@merchantg-cmyk

Copy link
Copy Markdown
Contributor

Closes #360

Root cause

OpenCascade discretises adjacent curved edges independently. At a tangent cusp the arc-side node falls below the spline chord, so the arc chord crosses the spline chord in UV. BRepMesh_ModelHealer leaves the wire alone, and FaceDiscret builds only 2 triangles, which omit the cusp tip. _face_data correctly refuses the trim/boundary mismatch ("CAD trim chains do not cover mesh boundary exactly once"). The crossing condition is scale independent, so finer deflection and tighter tolerances hit it again on every retry.

What changed

python/camlib/_cad_surface.py: 354 changed lines against upstream/main b636cc4, and no Rust changes.

  • Exact 2D orientation and segment-intersection predicates, with a float filter and an exact Fraction fallback.
  • _find_uv_trim_conflict finds crossing chords, endpoint touches, 180-degree foldbacks, zero-length chords and inverted wires.
  • _bisect_model_edge evaluates the real CAD edge, and the 2D pcurve in every face of that edge, at the exact mid parameter. It inserts that shared node into all of them, so shared edges stay welded.
  • _mesh_solid_with_cusp_repair runs only after the normal mesher hits a mismatch, so solids that already mesh are untouched. It runs BRepMesh with InParallel = False so the repair path is deterministic; the normal upstream meshing path is unchanged. The count is reported as cusp_trim_bisections in SurfaceMesh.source.
  • A header comment names the assumption: OCP 7.9.3.1 / OpenCascade 7.9.x BRepMesh IMeshTools stages. Only two IMeshData status flags are cleared on repaired faces: IMeshData_Failure on the face and IMeshData_SelfIntersectingWire on its wires. A probe showed these are the only two that block FaceDiscret.
  • docs/ARCHITECTURE.md and docs/library-capabilities.md are updated.

Error accounting (refuse rather than approximate)

  • Bisection is at the mid parameter on the CAD edge, in every face of the edge, capped at 64 per face (_MAX_CUSP_BISECTIONS_PER_FACE).
  • Every refusal states its real cause:
    • the cap was reached;
    • only straight edges self-intersect;
    • bisection reached the edge's parameter resolution;
    • the edge is degenerated;
    • the edge is not SameParameter/SameRange;
    • a discretisation slot is missing;
    • a BRepMesh stage failed (BuildModel, DiscretizeEdges, HealModel, PreProcessModel, DiscretizeFaces or PostProcessModel, named in the message);
    • the OCP build lacks a needed call ("tangent cusp repair unavailable on this OCP version").
  • The refusal names the face only when that face is matched in the solid. Otherwise it says "the solid" and never names a wrong face.
  • There is no custom triangulator and no relaxed error bound or weld tolerance. The existing conforming refinement, welding, error budget and edge closure checks run on the result.

Tests added

python/tests/test_cad_cusps.py is new: 477 lines, 12 test methods.

  • Exact predicates, plus _bisect_model_edge failure reasons checked with a fake edge.
  • The repro at several max_error_mm values, with watertight, volume and facet-inside-solid checks. It also exports 3 times and asserts identical triangles.
  • A tilted 250 mm case, a 2-edge tangent lens, and a cusp with a hole.
  • Refusals: the cap is reached; SameParameter=False reports the true reason; an unmatched face does not name face 0; a failed BRepMesh stage is named; a missing OCP attribute refuses cleanly.
  • The native material verifier.
  • Before the change (on 643c648), TangentCuspExportTest: "Ran 5 tests ... FAILED (errors=5)".

Verification (rebased onto upstream/main b636cc4, HEAD e640311, local run 2026-11-21, log D:\camlib-swarm\final-360c.log)

  • cargo build --release --workspace --all-features: exit 0.
  • ruff check python: All checks passed. ruff format --check python: 230 files already formatted.
  • Full suite, python -m unittest discover -s python/tests: Ran 1098 tests in 930.279s, OK (skipped=21), with 0 failures and 0 errors.
  • Repro: "BRep valid: True" and "meshed OK".
  • Repair path timing with InParallel = False vs True: the warm repro median was 0.0245 s vs 0.0258 s, and test_cad_cusps was unchanged within noise.

Platforms run: Windows 11 x64, Python 3.12.14, CadQuery 2.8.0, OCP 7.9.3.1. Linux and macOS were not run. Rust checks (cargo test/clippy/fmt) were not run because no Rust files changed.

Left alone

While sweeping 250 random cusp prisms I found a separate refusal in the spline-spline sliver weld check. Six spline_spline cases that mesh without cusp repair were later refused by the conforming refinement weld check. It is pre-existing and independent of #360, and I did not weaken check_weld. It should get its own issue.

Merge note: this PR and #361 both touch the same trim check in export_curved_solid, so whichever merges second needs a small rebase.

Review notes

  • The library change is 354 lines.
  • The repair still drives BRepMesh's internal IMeshTools/IMeshData stages. This is narrowed to two status flags and refuses cleanly on a missing call, but it may still need attention on a future OCP major version.
  • The (ImportError, AttributeError) guard wraps the whole repair, so an AttributeError from a bug in the repair code itself would also be reported as "unavailable on this OCP version". It is still a refusal, never an approximation.

IndustryIQ78 and others added 4 commits November 21, 2026 16:07
…t cusps (stevecad#360)

When OpenCascade's BRepMesh fails trim matching (such as a zero-angle tangent cusp where independent edge chords cross in UV regardless of deflection), inspect each face's UV trim polygon with exact 2D predicates (IEEE-754 filter with exact Fraction fallback), bisect offending curved chords at their mid parameter on the real CAD edge across every incident face in IMeshData_Model, and let BRepMesh triangulate with those shared nodes.

Refuses cleanly with a message naming the tangent cusp if the per-face bisection cap is hit, if straight edges self-intersect, or if bisection reaches parameter resolution. Adds regression tests in python/tests/test_cad_cusps.py with independent watertight, trim-to-mesh, facet-to-CAD, cap-refusal, and native verifier checks, and updates ARCHITECTURE.md and library-capabilities.md.

Closes stevecad#360
@10-X-eng
10-X-eng enabled auto-merge November 22, 2026 21:53
@10-X-eng
10-X-eng added this pull request to the merge queue Nov 22, 2026
Merged via the queue into stevecad:main with commit 1b45d60 Nov 22, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CAD surface export refuses a valid planar face with a zero-angle (tangent) cusp

3 participants