Add diagnostic tracing and fix ot_analyze_shape O(N^4) wire matching#3
Merged
Conversation
Two changes that go together — the tracing exposed the analyze hang
the user reported on real shapes:
1. Trace infrastructure (src/occt_templot_trace.{h,cpp}). Off by
default; enable via OCCT_TEMPLOT_TRACE=1 env var or the new
ot_set_trace(true) public C function. When on, OT_TRACE(...) lines
from each ot_* call are flushed to stderr immediately so hangs are
still visible, and OCCT's Message::DefaultMessenger() is wired to
stderr so ShapeFix / Sewing / BRepCheck internal output surfaces.
2. ot_analyze_shape no longer scans every face inside a per-wire loop.
The previous code was nested explorer iteration — O(W * F * W_per_F),
roughly N^4 on dense meshes — and would hang for tens of minutes on
anything with thousands of faces. Replaced with a single
TopExp::MapShapesAndAncestors(WIRE, FACE) build (O(F)) and a flat
walk over the unique wires. As a side effect, the duplicate-wire
double-counting bug in the old loop is also gone.
Wired tracing into every heal-module entry point (analyze, heal,
heal_detailed, sew, upgrade, make_solid) with phase-level granularity
in analyze so the log says exactly which scan a stuck call is in.
Validated on bearing.stl from the OCCT test data (24,680 faces /
74,040 edges / 24,680 wires) — analyze now finishes in milliseconds;
on the old code it would run ~10^13 inner-loop iterations.
ctest 4/4 green on macOS arm64 with tracing both off (default) and on.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on top of #2.
Two changes that go together — the tracing exposed a real bug in
ot_analyze_shapethat was almost certainly the cause of the "stuck with no output" symptom reported in the templot-restructured viewer.1. Diagnostic tracing infrastructure
New
src/occt_templot_trace.{h,cpp}. Off by default — enable either way:OCCT_TEMPLOT_TRACE=1 ./your-app # env varot_set_trace(true); // public C API, also bound in pascal/occt_templot.pasWhen on:
ot_*function in the heal module emits[ot] ...entry / phase / exit lines to stderr with immediate flush, so a hung call is still visible.Message::DefaultMessenger()is wired to stderr as[occt:trace|info|warn|alarm|fail] ..., surfacing internalShapeFix/Sewing/BRepCheckoutput that was previously swallowed.Sample on
bearing.stl:The phase granularity inside
ot_analyze_shapewas deliberate — see (2).2. Fix
ot_analyze_shapeO(N⁴) wire matchingThe wire-gap scan was a nested-explorer search:
That is
O(W × F × W_per_F). On a typical solid whereW ≈ F, this is O(N⁴) in the face count. Onbearing.stl(24,680 faces / 24,680 wires) the inner-loop count is roughly1.5 × 10¹³— easily a multi-minute hang on real models. Anything dense enough to look "stuck with no output" hits this.Replaced with a single
TopExp::MapShapesAndAncestors(shape, TopAbs_WIRE, TopAbs_FACE, ...)build (O(F)) and a flat walk over the unique wires:As a side effect this also fixes a duplicate-wire double-counting bug in the old loop (a wire shared between two faces was counted twice, inflating
gap_count).Test plan
ctest4/4 green with tracing off (default) on macOS arm64.OCCT_TEMPLOT_TRACE=1 ./test_heal bearing.stl— 16/16 pass; analyze finishes in ms (was unbounded).test_healoutput.How a downstream caller uses this
From the templot-restructured Pascal app:
Or just launch the app with
OCCT_TEMPLOT_TRACE=1set in the environment.🤖 Generated with Claude Code