Drawing W06a: parallel hidden lines and shared work limits (#2455) (#3013)

* geom/hlr: per-curve work on worker threads, shared work limits

Silhouette tracing, screen sampling, shadow cuts, classification, coverage and
the crossing search (by grid rows) with crossing refinement run on worker
threads through one ordered map: results go back in item order, so drawings
are the same bit for bit on any number of threads. Options::threads picks the
workers (0 = one per core, small views stay on the calling thread); the
browser build stays serial. Placements sharing a body and a turn share one
silhouette tracing.

Cancellation is polled before every item and inside long ones from every
worker; the first error stops the others; a panicking worker becomes
HlrError::Internal instead of escaping. Ray, curve, crossing, shadow and
coverage budgets are totals over all threads (shared counters; crossing pairs
are counted before the search).

Limits: placements and the default max_tracks are hlr::MAX_ITEMS, which is
section::MAX_ITEMS (also the brep record limit); trim_samples must be 1..=4096.

* docs/drawing-hlr: link the parallel-work PR

* geom/hlr: clearer limits and parallel-work docs, faster thread tests

docs/drawing-hlr.md now separates input limits (with the error each returns:
body records fail with Invalid, not Budget) from the work budgets, names every
budget's error, and states how errors and panics behave on worker threads.
Code comments say the same in plainer terms. The budget and cancellation
tests drop the oblique torus, and the freeform pass of the identity test
compares one parallel run, which keeps the HLR suite under 4 s.

* geom/hlr: poll cancellation inside long items, find spline spans by bisection

Review found two items a worker could run for seconds without seeing a
cancellation: tracing a body with many curved surfaces, and sampling one long
spline (up to 4096 control points). Silhouette tracing now asks before each
surface, screen sampling at every refinement pass, the cuts at left-out faces
every 4096 chords and coverage every 65536 checks.

The long spline was slow because every point and every part hull scanned all
of its knot spans. de Boor evaluation now finds its span by bisection over the
ordered knots, and a part's hull starts at its first piece and stops after
its last: the same spans, so the same bits (checked on the fixture scenes),
and a 4096-point spline edge prepares in 0.9 s instead of 2.2 s and samples
in 0.23 s instead of 1.47 s.

* geom/hlr: poll cancellation while forming and trimming silhouette sets

Review found that forming a body's silhouette sets scanned each face's whole
surface group before the first poll, quadratic in the faces on one surface
(about 4e8 steps for 20 000 faces). The group scan now stops at its first
supported face (groups list faces in order, so that is the same face), the
loop polls every 256 faces, and trimming a traced curve asks before each
face. A cancellation seen while trimming drops the partial curves, even if
the callback answers false later. Output unchanged on the fixture scenes.

* geom/hlr: poll cancellation per boxes met when cutting at left-out faces

Review found that one chord crossing the screen boxes of very many left-out
faces bisects every side without a poll (polls came only every 4096 chords).
It now also polls every 1024 boxes met. The docs state the cancellation
contract precisely: polls are where listed, and the work between two polls is
bounded by the input limits rather than by a time.

* docs/drawing-hlr: say where coverage polls cancellation

* geom/hlr: body records have brep's own, larger limit now

Main raised brep::MAX_ITEMS to 1 000 000 (#3093, which stores whole evaluated
models), so it no longer equals section::MAX_ITEMS. hlr::MAX_ITEMS stays
section's 100 000 for placements and the default curve budget; the docs and
the limits test now say that a body's records are bounded separately, by
brep's limit.

* geom/hlr: a panicking worker stops the others; the ray budget is HLR's own

A worker that panics now sets the shared stop flag while it unwinds, so the
other workers end the phase early instead of finishing work the call throws
away. The docs no longer compare the ray budget with the occlusion work
budget of file.export_view_dxf: the two count different work.

* geom/hlr: an ignored timing test, prepare and drawing timed apart

hlr_timing prepares four bodies, then draws 100 differently turned placements
on one thread, two workers and one worker per core, printing the total, the
phase timings and the ray count. SOLIDCRAFT_BENCH_HLR sets the placement count.

* geom/hlr: timing scenes for sharing and crossings; placements keep their lines when permuted

hlr_timing now starts its prepare clock after the fixtures are built, adds the
same grid at one turn (shared definitions against one per placement) and
sixteen thin bars crossing 8x8 from above, rotates the thread order over three
rounds and checks the drawings match across thread counts outside the timed
part. A new test permutes three placements of one prepared box and removes one:
each line keeps its own placement index, and the boxes' lines stay exact.

* docs/drawing-hlr: the review's limits and fixtures in the parallel tables

* geom/hlr: follow the B-rep record into solidcore-record

The record, its version and its item limit now come from solidcore-record
(#11496): the new tests build their bodies at `solidcore_record::brep::VERSION`
and make a spline's curve with `C3::bounded`, as the other fixtures do, and
the limits table gives the record's own limit, 4 000 000 items, checked by
`brep::check::validate`.

---------

Co-authored-by: MateuszSychXpl <151755914+MateuszSychXpl@users.noreply.github.com>
This commit is contained in:
Mateusz SychandMateuszSychXpl authored and GitHub committed 2026-11-04 16:56:07 -05:00
1 parent 96bb41c480
commit e81a201ff8
5 files changed
+1125 -343

No files matched your search

+102 -18
View File
@@ -1,7 +1,8 @@
# Hidden lines (W06a)
This is the geometry core of Drawing package W06 from the [published plan][plan] in
[#860][rfc]: [#2455][issue] / [PR #2457][pr]. It turns the subset that the W05a
[#860][rfc]: [#2455][issue] / [PR #2457][pr], with its parallel work and shared limits in
[PR #3013][pr2]. It turns the subset that the W05a
investigation ([#2105][w05-issue], [draft #2106][w05-pr], [findings][findings])
showed to be ready into code: exact orthographic hidden lines for placed bodies whose
faces are planes, cylinders, cones, spheres or tori.
@@ -30,9 +31,12 @@ hidden_lines(&[Prepared], &[Placement], &View, &Options, &cancel) -> Result<Draw
- `View::new(direction_of_sight, up)` is an orthographic view. Screen coordinates are
`(p . x, p . y)` in millimetres, with `x = direction x up` normalised and `y = x x
direction`.
- `cancel` is polled during the work; when it returns true the call ends with
`HlrError::Cancelled`. `prepare_cancellable(&Brep, &Options, &cancel)` is `prepare` with
such a callback, polled between edges, faces and surface comparisons.
- `cancel` is polled during the work, from every worker thread, so it must be `Sync`; when
it returns true the call ends with `HlrError::Cancelled`. `prepare_cancellable(&Brep,
&Options, &cancel)` is `prepare` with such a callback, polled between edges, faces and
surface comparisons.
- `Options::threads` sets the number of worker threads (0, the default, means one per core);
see [Parallel work](#parallel-work).
The `Drawing` holds one `Line` per drawable edge and per silhouette curve of each
placement, in order of placement. A line names its `placement` and `source` (`Edge { edge, tangent }`
@@ -95,6 +99,44 @@ large scenes keep their detail.
the scene and shifted back at the end, so coordinates stay small: a part 1e7 mm from the
origin, whether placed there or modelled there, is classified as at the origin.
## Parallel work
`hidden_lines` spreads its per-curve work over worker threads: silhouette tracing, screen
sampling, the cuts at the boxes of left-out faces, the classification of pieces, coverage,
and the crossing search (by rows of its screen grid) with the refinement of each crossing.
Each phase hands its items to the workers one at a time and stores every result at its
item's index, so the drawing is identical, bit for bit, for any number of threads: the same
pieces in the same order and the same statistics apart from the phase timings.
- **Thread count.** With `Options::threads` at 0 (the default) a phase uses up to one worker
per core, but at most one per distinct silhouette tracing, 16 curves, 4 grid rows or 256
crossings, so the per-curve phases of a small view stay on the calling thread. 1 runs
everything on the calling thread; a larger number caps the workers. The browser build
always runs on the calling thread.
- **Shared silhouettes.** Silhouettes depend on the body and the direction of sight in its
own frame, not on its position, so placements that share both (repeated parts turned
alike) share one tracing. This speeds up the browser build too.
- **Cancellation.** `cancel` is polled before every item, on whichever thread runs it, and
inside long items: every 256 faces and before every face a silhouette is trimmed to, at
every sampling pass, every 4096 chords and every 1024 boxes met when cutting at left-out
faces, every 1024 rays, every 65 536 crossing candidates, and between the chords of a piece
once 65 536 coverage checks have passed since the last poll.
The work between two polls is bounded by the input limits, not by a time: in ordinary
scenes the longest step is tracing one surface's silhouette curves, a few tenths of a second
for a torus, but a scene built at the limits can make one step take longer.
- **Errors.** The first error, cancellation included, stops the other workers and is the one
returned. When two different failures coincide on different threads (a cancellation while
a budget is exceeded), the error returned may differ from a run on one thread; a successful
result never does.
- **Panics.** Every parallel item runs on a spawned worker that the calling thread joins. A
worker that panics loses its item and stops the other workers: the call returns
`HlrError::Internal` and no drawing, and the panic does not reach the caller. Work on the calling thread (one thread, or the browser
build) lets a panic propagate to the caller as before.
Measured on a 32-thread desktop (release build, isometric view of boxes, cylinders, spheres
and tori): 100 placements, each turned differently, take 0.8 s, against 9.9 s on one thread;
300 placements turned alike take 0.8 s, against 33 to 55 s before silhouettes were shared.
## Freeform faces
Faces that are not planes, cylinders, cones, spheres or tori are freeform. By default
@@ -110,14 +152,45 @@ in the W05a findings.
## Limits
- `Options::max_tracks` (curves) and `Options::max_rays` bound the work, as do internal
caps: crossing grid entries (50 million), crossing candidate pairs (500 million, counted
before the search, so no set of pairs met grows with them), crossings (50 million) and
the surface comparisons that group a body's faces (50 million). Exceeding one returns
`HlrError::Budget`.
- `Options::trim_samples` is 1 to `MAX_TRIM_SAMPLES` (4096), and every coordinate of a
placed body at most `MAX_COORDINATE` (1e12 mm, the bound `geom::section` and
`geom::brep` put on stored numbers); otherwise the call returns `HlrError::Invalid`.
Inputs are checked before any work:
| Input | Limit | Otherwise |
| --- | --- | --- |
| Records of one body | `solidcore_record::brep::MAX_ITEMS` (4 000 000), checked by `brep::check::validate` in `prepare` | `Invalid("portable B-rep")` |
| Placements in one scene | `hlr::MAX_ITEMS` (100 000) | `Budget("placement")` |
| `Options::trim_samples` | 1 to `MAX_TRIM_SAMPLES` (4096) | `Invalid("trim samples")` |
| Coordinates of a placed body's box | at most `MAX_COORDINATE` (1e12 mm, the bound `geom::section` and `geom::brep` put on stored numbers) | `Invalid("scene extent")` |
| `Options::threads` | at most `MAX_THREADS` (64) | capped |
Work budgets are totals over the whole call and all worker threads. Whether one is exceeded
does not depend on the number of threads or on their scheduling:
| Work | Budget | Error |
| --- | --- | --- |
| Surface comparisons grouping a body's faces, in `prepare` | 50 million | `Budget("surface group")` |
| Curves: drawable edges and silhouette pieces | `Options::max_tracks`, default `hlr::MAX_ITEMS` | `Budget("curve")` |
| Visibility rays | `Options::max_rays`, default 20 000 000 | `Budget("ray")` |
| Crossing grid entries | 50 million | `Budget("crossing grid")` |
| Crossing candidate pairs, counted before the search | 500 million | `Budget("crossing pair")` |
| Crossings | 50 million | `Budget("crossing")` |
| Left-out face grid entries | 50 million | `Budget("shadow grid")` |
| Left-out face box checks | 500 million | `Budget("shadow")` |
| Coverage point checks | 200 million | `Budget("coverage")` |
`hlr::MAX_ITEMS` is `section::MAX_ITEMS`, the aggregate item limit that `geom::section`
applies, and the body limit of `file.export_view_dxf`. It bounds the placements a caller
supplies and, by default, the number of curves, which is the number of lines in the drawing;
a caller drawing a large assembly may raise `max_tracks`. A body's own records have the
larger limit of the B-rep record (`solidcore_record::brep`), which stores whole evaluated
models.
The ray budget is HLR's own: it counts visibility classifications, one per ray. The 200 million
work steps of `file.export_view_dxf` count other work (placements, tree nodes and triangles of
its occlusion test), so the two numbers are not comparable. Every piece costs at least one
ray, so the ray budget also bounds the pieces. The remaining caps bound
the steps in between.
Other limits:
- `Options::tangent_edges` (default on) draws edges between tangent faces, flagged
`tangent: true`; drafting policy for them is W17.
- Tolerances are relative to the faces and the scene. They are meant for faces up to
@@ -127,14 +200,15 @@ in the W05a findings.
recognition takes for one (closer than 1e-9 of their size or position, a nearly flat
cone's far apex included) share the own-face margin of 1e-6 of the face's size when a
ray leaves one of them.
- Not in this slice: parallel per-curve work and the shared work limits of
`geom::section`, the kernel adapter (after W02), perspective, section regions (W03),
- Not in this slice: the kernel adapter (after W02), perspective, section regions (W03),
views and commands (W07), and mirrored placements. Of the records the kernel now saves,
planes through three points (`PlaneThrough`) are drawn as planes; a procedural
`SurfaceIntersection` curve is drawn as its leader, within its tolerance, when the leader is
a spline; one with a polyline leader, or a `PlacedRevolution` surface whose placement is not
rigid (it may scale it unevenly), is refused by `prepare` with `HlrError::Unsupported` until
the adapter handles them.
`SurfaceIntersection` curve is drawn as its leader, within its tolerance, when the leader
is a spline; one with a polyline leader, or a `PlacedRevolution` surface whose placement
is not rigid (it may scale it unevenly), is refused by `prepare` with
`HlrError::Unsupported` until the adapter handles them. A single surface's silhouette is
traced on one thread, so one torus seen obliquely still takes about 0.3 s whatever the
thread count.
## Fixtures
@@ -187,6 +261,15 @@ and intervals:
- a box stored with planes through three points draws the same bits as one with
axis-stored planes; rigidly placed revolutions draw as what they are, others are refused
as not supported yet, as are intersection curves with no spline to follow;
- a mixed scene (touching boxes, cylinders, a sphere, an oblique torus and a left-out patch,
with and without freeform faces) draws the same bits on 1, 2, 3, 8 and automatic threads;
placements sharing a body and a turn draw as if each had its own copy of the body; the ray
and curve budgets pass at the serial run's counts and fail one below them on 1 and 4
threads; cancellation asked only from worker threads ends a parallel run, and a failing or
panicking item ends the work with its error; scenes over `MAX_ITEMS` placements and trim
sample counts out of range are refused; sampling one long spline and tracing a body's
silhouettes ask for cancellation inside the item; spline points and part hulls come from
their own knot spans;
- torus ray roots for thin, large and nearly tangent cases, and torus silhouettes near
the axis direction.
@@ -195,5 +278,6 @@ and intervals:
[findings]: https://github.com/tangency-labs/solidcraft/issues/860#issuecomment-6243691483
[issue]: https://github.com/tangency-labs/solidcraft/issues/2455
[pr]: https://github.com/tangency-labs/solidcraft/pull/2457
[pr2]: https://github.com/tangency-labs/solidcraft/pull/3013
[w05-issue]: https://github.com/tangency-labs/solidcraft/issues/2105
[w05-pr]: https://github.com/tangency-labs/solidcraft/pull/2106