BREP_render 0.12.0

BREP Rust rendering engine: kernel-fed scene store + wgpu renderer (headless artifact, desktop window, and wasm canvas shells).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! Manufacturing instructions out of the app: the printed PDF, each step's
//! picture as PNG and SVG, and the interactive player's data and offline
//! `.html`.
//!
//! The PDF and the SVG are sheets ([`crate::sheets::print`]): each step's view
//! is projected as a placed view, hidden lines removed, exactly as a drawing
//! sheet draws a PMI view — by the exact pass over the solids' B-reps, which
//! the export asks the runner for first ([`EngineState::instructions_topology_ready`]),
//! as a sheet export does. A B-rep that has not arrived leaves its step
//! pictures to the mesh approximation, and the file says so
//! (`data-hidden-line` on the SVG's view, `/HiddenLine` in the PDF's Info).
//! The PNG is the thumbnail rasterizer from the step's camera, the parts
//! earlier steps placed drawn grey. None of them reads what the viewport
//! shows: a step shown in the player, its ghosts and an insertion half way
//! home do not change a file.
//!
//! The player data carries one mesh per part, each node with its placement,
//! each step's camera, picture and view PMI (laid out as the 3D PDF lays it
//! out, [`crate::sheets::pdf3d::view_annotations`]), and the assembly's tree
//! with a thumbnail per part (the page's Model and Parts tabs).

use super::{component_move::now_ms, EngineState};
use crate::scene::SolidDisplay;
use brep_instructions::wizard;
use crate::sheets::print::{self, PictureAt};
use crate::sheets::project::{poses_of, project_view_through, Deferred, LinesMode, Model, Unposed, ViewDrawing, ViewFrame};
use crate::sheets::PlacedView;
use crate::thumbnail;
use brep_instructions::edit;
use brep_instructions::layout::{self, Rect};
use brep_instructions::player::{self, Mode, PlayerData, PlayerMesh, PlayerNode};
use brep_kernel::PmiProjection;
use brep_kernel::PmiView;
use serde_json::json;
use std::collections::HashMap;

/// A per-step PNG's edge, in pixels.
pub const IMAGE_PX: u32 = 800;

/// The colour a PNG draws a part an earlier step placed in (sRGB 0..1).
const PLACED_RGB: [f32; 3] = [0.80, 0.81, 0.83];

/// A step picture's edge in the player data, in pixels: the picture the
/// player shows without WebGL.
const PLAYER_IMAGE_PX: u32 = 480;

/// A part thumbnail's edge in the player data, in pixels.
const PLAYER_THUMBNAIL_PX: u32 = 96;

const IDENTITY: [f64; 16] = [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0];

/// How long an export waits for the runner's topology reply before it draws
/// what it has ([`EngineState::instructions_topology_ready`]).
pub const TOPOLOGY_WAIT: std::time::Duration = std::time::Duration::from_secs(120);

/// A PDF prepared over successive frames. Heavy exact passes run on the history runner.
/// Dropping this job cancels delivery; the document stamp prevents mixed-revision output.
pub struct InstructionsPdfExport {
    block: brep_instructions::model::Instructions,
    layout: Vec<layout::Page>,
    todo: Vec<(PictureAt, String, Rect)>,
    pictures: HashMap<PictureAt, ViewDrawing>,
    next: usize,
    stamp: (u64, u64),
    started: f64,
    unposed: Option<Unposed>,
    views: Vec<PmiView>,
}

impl InstructionsPdfExport {
    pub fn progress(&self) -> (usize, usize) { (self.next, self.todo.len()) }
}

impl EngineState {
    /// Begin without waiting for topology or projecting any picture.
    pub fn begin_instructions_pdf(&mut self) -> Result<InstructionsPdfExport, String> {
        if self.run_pending() { return Err("wait for the model to finish rebuilding before exporting".into()); }
        let block = self.instructions_required()?;
        let layout = layout::pages(&block)?;
        let todo = print::picture_rects(&block, &layout);
        let names: Vec<String> = self.scene.solids().iter().filter(|solid| !solid.is_sketch).map(|solid| solid.name.clone()).collect();
        self.request_exact_solids(names.iter().map(String::as_str));
        Ok(InstructionsPdfExport {
            block, layout, todo, pictures: HashMap::new(), next: 0,
            stamp: (self.applied_generation(), self.history_revision()),
            started: now_ms(), unposed: None, views: self.pmi_state().views,
        })
    }

    /// Poll at most one picture per frame. Never wait for the runner or fall back
    /// to computing hidden lines on the UI thread if the runner refuses a job.
    pub fn poll_instructions_pdf(&mut self, job: &mut InstructionsPdfExport) -> Result<Option<(Vec<u8>, usize)>, String> {
        if job.stamp != (self.applied_generation(), self.history_revision()) {
            return Err("the document changed during PDF export; export again".into());
        }
        self.pump_topology();
        self.pump_sheet_lines();
        if self.topology_pending() {
            if now_ms() - job.started >= TOPOLOGY_WAIT.as_secs_f64() * 1000.0 { return Err("timed out loading exact geometry for PDF export".into()); }
            return Ok(None);
        }
        if self.sheet_lines_pending() { return Ok(None); }
        if job.unposed.is_none() { job.unposed = Some(self.instructions_export_displays()); }
        if let Some((at, step_id, rect)) = job.todo.get(job.next) {
            if let Some(view) = job.block.step(step_id).and_then(|step| step.view.as_deref()) {
                let placed = self.instructions_placed_names(&job.block, step_id);
                let pending = std::cell::Cell::new(false);
                // Discover at most one cache miss, then submit it after the scene borrow ends.
                let mut requests = Vec::new();
                let mut defer = |request| { requests.push(request); pending.set(true); Deferred::Pending };
                let drawing = self.instructions_project_with(job.unposed.as_ref().unwrap(), &job.views, view, *rect, &placed,
                    &mut LinesMode::Export(&mut defer), &pending);
                for request in requests {
                    if matches!(self.sheet_lines.defer(self.runner.as_mut(), request), Deferred::Here) {
                        return Err("the background runner could not draw the PDF picture; export again".into());
                    }
                }
                let Some(drawing) = drawing else { return Ok(None) };
                if !drawing.error.is_empty() { return Err(drawing.error); }
                job.pictures.insert(*at, drawing);
            }
            job.next += 1;
            return Ok(None);
        }
        let pages = print::pages(&job.block, &job.layout, &job.pictures);
        let refs = pages.iter().collect::<Vec<_>>();
        Ok(Some((crate::sheets::pdf::write_pdf(&print::title(&job.block), &refs), pages.len())))
    }

    /// Ask the runner for the B-rep of every solid the step pictures draw
    /// (see [`Self::request_exact_solids`]) and wait for the reply, up to
    /// [`TOPOLOGY_WAIT`], on a runner that can be waited on (the Inline runner
    /// answers before the request returns; the thread runner is waited on; a
    /// worker cannot be, and the caller holds the export until
    /// [`Self::topology_pending`] clears). Returns whether every drawn solid's
    /// B-rep is in the scene now: the exact pass draws the pictures. Otherwise
    /// the mesh approximation does, and the file says so.
    /// Ask for the exact solids the step pictures draw WITHOUT waiting:
    /// returns whether they are all here already. The frame's `pump` lands the
    /// replies; a caller that may not block (automation) checks
    /// [`Self::topology_pending`] on later frames.
    pub fn instructions_request_topology(&mut self) -> bool {
        let names: Vec<String> = self.scene.solids().iter().filter(|solid| !solid.is_sketch).map(|solid| solid.name.clone()).collect();
        self.request_exact_solids(names.iter().map(String::as_str));
        self.pump_topology();
        !self.topology_pending()
    }

    pub fn instructions_topology_ready(&mut self) -> bool {
        let names: Vec<String> = self.scene.solids().iter().filter(|solid| !solid.is_sketch).map(|solid| solid.name.clone()).collect();
        self.request_exact_solids(names.iter().map(String::as_str));
        let deadline = now_ms() + TOPOLOGY_WAIT.as_secs_f64() * 1000.0;
        while self.topology_pending() {
            let now = now_ms();
            if now >= deadline || !self.runner.wait_topology(std::time::Duration::from_secs_f64((deadline - now) / 1000.0)) {
                break;
            }
            self.pump_topology();
        }
        !self.topology_pending()
    }

    /// Every display as the files draw it: unposed (the explode originals of
    /// the view the viewport shows) and shown, so only each step view's own
    /// hidden list and poses decide what a picture holds.
    fn instructions_export_displays(&self) -> Unposed {
        self.scene
            .solids()
            .iter()
            .map(|solid| {
                let mut display = self.pmi_explode_originals.get(&solid.name).unwrap_or(solid).clone();
                display.visible = true;
                (solid.name.clone(), display)
            })
            .collect()
    }

    /// The PMI view `view_id` projected into `rect`: projected once at scale
    /// 1 to measure it, then placed at the scale and position that fit it.
    /// The parts `placed` by earlier steps are drawn in the model stroke and
    /// the rest, the parts the step adds, in the highlight stroke
    /// ([`ViewDrawing::highlighted`]). Visibility is decided once over every
    /// part; a second pass at the same placement with the placed parts hidden
    /// says which visible runs lie on the added parts' own edges.
    fn instructions_project(&self, unposed: &Unposed, views: &[PmiView], view_id: &str, rect: Rect, placed: &[String]) -> ViewDrawing {
        self.instructions_project_with(unposed, views, view_id, rect, placed, &mut LinesMode::Compute, &std::cell::Cell::new(false)).expect("synchronous projection")
    }

    fn instructions_project_with(&self, unposed: &Unposed, views: &[PmiView], view_id: &str, rect: Rect, placed: &[String], mode: &mut LinesMode, pending: &std::cell::Cell<bool>) -> Option<ViewDrawing> {
        let env = self.sheet_env();
        let model = Model { scene: &self.scene, unposed, env: &env };
        let view = views.iter().find(|view| view.id == view_id);
        let report = self.pmi_report.as_ref().and_then(|report| report.view(view_id));
        let placement = |position: [f64; 2], scale: f64| -> PlacedView {
            serde_json::from_value(json!({ "id": format!("IV-{view_id}"), "view": view_id, "position": position, "scale": scale }))
                .expect("a placement from its id, view, position and scale")
        };
        let measured = project_view_through(&model, &placement([0.0, 0.0], 1.0), view, report, None, None, mode);
        if pending.get() { return None; }
        if !measured.error.is_empty() {
            return Some(measured);
        }
        let (position, scale) = print::fit(measured.bounds, rect);
        let mut drawing = project_view_through(&model, &placement(position, scale), view, report, None, None, mode);
        if pending.get() { return None; }
        let Some(view) = view else { return Some(drawing) };
        if placed.is_empty() || drawing.edges.is_empty() {
            // Nothing placed yet: everything drawn is being added.
            drawing.highlighted = std::mem::take(&mut drawing.edges);
            return Some(drawing);
        }
        // The same view with what earlier steps placed hidden too (a
        // transient hide step on a copy, never stored).
        let mut added_only = view.clone();
        let extra: Vec<String> = placed.iter().filter(|name| !added_only.hides(name)).cloned().collect();
        added_only.annotations.push(brep_kernel::PmiAnnotation::hide_step("HID-placed", &extra, true));
        let alone = project_view_through(&model, &placement(position, scale), Some(&added_only), report, None, None, mode);
        if pending.get() { return None; }
        if !alone.error.is_empty() { return Some(alone); }
        let (edges, highlighted) = split_highlighted(std::mem::take(&mut drawing.edges), &alone.edges);
        drawing.edges = edges;
        drawing.highlighted = highlighted;
        Some(drawing)
    }

    /// The parts earlier steps placed, by solid name, for `step_id`'s picture.
    fn instructions_placed_names(&self, block: &brep_instructions::model::Instructions, step_id: &str) -> Vec<String> {
        let names: Vec<String> = self.scene.solids().iter().map(|solid| solid.name.clone()).collect();
        edit::presentation(block, step_id, &names).map(|presentation| presentation.placed).unwrap_or_default()
    }

    /// The instructions as one PDF: the cover, the parts list, the tools list
    /// and the steps on the document's page template, the pictures drawn by
    /// the exact pass ([`Self::instructions_topology_ready`]). Returns the
    /// bytes and the page count.
    pub fn instructions_pdf(&mut self) -> Result<(Vec<u8>, usize), String> {
        let block = self.instructions_required()?;
        self.instructions_topology_ready();
        let layout = layout::pages(&block)?;
        let pmi = self.pmi_state();
        let unposed = self.instructions_export_displays();
        let mut pictures: HashMap<PictureAt, ViewDrawing> = HashMap::new();
        for (at, step_id, rect) in print::picture_rects(&block, &layout) {
            if let Some(view) = block.step(&step_id).and_then(|step| step.view.clone()) {
                let placed = self.instructions_placed_names(&block, &step_id);
                pictures.insert(at, self.instructions_project(&unposed, &pmi.views, &view, rect, &placed));
            }
        }
        let pages = print::pages(&block, &layout, &pictures);
        let refs: Vec<&crate::sheets::project::SheetDrawing> = pages.iter().collect();
        Ok((crate::sheets::pdf::write_pdf(&print::title(&block), &refs), pages.len()))
    }

    /// One step's picture as SVG: its number and title over its view, hidden
    /// lines removed by the exact pass ([`Self::instructions_topology_ready`]).
    pub fn instructions_step_svg(&mut self, step_id: &str) -> Result<String, String> {
        let block = self.instructions_required()?;
        let step = block.step(step_id).ok_or_else(|| format!("no step '{step_id}'"))?;
        self.instructions_topology_ready();
        let placed = self.instructions_placed_names(&block, step_id);
        let picture = step.view.as_deref().map(|view| {
            self.instructions_project(&self.instructions_export_displays(), &self.pmi_state().views, view, print::image_picture_rect(), &placed)
        });
        Ok(crate::sheets::svg::to_svg(&print::step_image(&block, step_id, picture)?))
    }

    /// One step's picture as a `size`-pixel square PNG from its view's camera:
    /// what the step adds in its colours with its edges, what earlier steps
    /// placed in grey, what comes later left out, each part where the view's
    /// explode rows put it.
    pub fn instructions_step_png(&self, step_id: &str, size: u32) -> Result<Vec<u8>, String> {
        let block = self.instructions_required()?;
        let step = block.step(step_id).ok_or_else(|| format!("no step '{step_id}'"))?;
        let view_id = step.view.as_deref().ok_or_else(|| format!("step '{step_id}' has no view: run the wizard or pick one"))?;
        let pmi = self.pmi_state();
        let view = pmi.views.iter().find(|view| view.id == view_id).ok_or_else(|| format!("no PMI view '{view_id}'"))?;
        let camera = view.camera.as_ref().ok_or_else(|| format!("PMI view '{view_id}' has no captured camera"))?;
        let frame = ViewFrame::of(camera);
        let back = frame.forward.map(|c| -c);
        let report = self.pmi_report.as_ref().and_then(|report| report.view(view_id));
        let names: Vec<String> = self.scene.solids().iter().map(|solid| solid.name.clone()).collect();
        let presentation = edit::presentation(&block, step_id, &names)?;
        let unposed = self.instructions_export_displays();
        let mut capture = thumbnail::Capture::default();
        for name in &names {
            if view.hides(name) || presentation.hidden.contains(name) {
                continue;
            }
            let Some(display) = unposed.get(name).filter(|display| !display.is_sketch) else { continue };
            let Some(body) = posed_body(display, &poses_of(report, name), presentation.placed.contains(name)) else { continue };
            capture.bodies.push(body);
        }
        thumbnail::render_png_from(&capture, size, (frame.right, frame.up, back))
            .ok_or_else(|| format!("step '{step_id}' shows nothing to draw"))
    }

    /// The interactive player's data: the steps over every drawn solid, each
    /// step's camera from its view, its picture and its view's PMI, and the
    /// assembly's tree with each part's thumbnail. What PLM serves per revision and the offline copy inlines.
    pub fn instructions_player_data(&self) -> Result<PlayerData, String> {
        let block = self.instructions_required()?;
        let unposed = self.instructions_export_displays();
        let mut scene = player::Scene::default();
        // A step's motion is keyed by the assembly's own occurrences; the
        // nodes sit under the wrapper.
        scene.wrapper = self.instructions_wrapper().map(|(id, _)| id).unwrap_or_default();
        let flat = |values: &[[f32; 3]]| values.iter().flatten().copied().collect::<Vec<f32>>();
        for solid in self.scene.solids() {
            let Some(display) = unposed.get(&solid.name).filter(|display| !display.is_sketch && !display.mesh.indices.is_empty()) else {
                continue;
            };
            let color = display.color_override.unwrap_or_else(|| crate::color::solid_color_srgb(&display.name).map(|c| c as f32));
            scene.nodes.push(PlayerNode { occurrence: solid.name.clone(), mesh: scene.meshes.len(), matrix: IDENTITY, color });
            let edges = thumbnail::body_of(display).map(|body| body.edges).unwrap_or_default();
            scene.meshes.push(
                PlayerMesh::new(&solid.name, &flat(&display.mesh.positions), &flat(&display.mesh.normals), &display.mesh.indices)
                    .with_edges(&edges),
            );
        }
        // One mesh per part: a node's key is its part (the PLM id or the
        // name) and the member inside it, from the occurrence that holds it,
        // and its placement is the engine's pose of that occurrence.
        let level = self.instructions_level()?;
        let components = wizard::flatten(&level);
        let mut keys = Vec::with_capacity(scene.nodes.len());
        let mut placements = Vec::with_capacity(scene.nodes.len());
        for node in &scene.nodes {
            let holder = components
                .iter()
                .filter(|(_, c)| node.occurrence.strip_prefix(c.path.as_str()).is_some_and(|rest| rest.starts_with(':')))
                .max_by_key(|(_, c)| c.path.len())
                .map(|(_, c)| *c);
            keys.push(holder.map(|c| format!("{}|{}", c.part_key(), &node.occurrence[c.path.len()..])).unwrap_or_else(|| node.occurrence.clone()));
            placements.push(holder.and_then(|c| self.instructions_occurrence_placement(&c.path)));
        }
        scene.share_meshes(&keys, &placements);
        // A part's thumbnail, once per part: every solid of `occurrence` (a
        // part's, or a sub-assembly's), unposed.
        let thumbnail_of = |scene: &mut player::Scene, part: &str, occurrence: &str| {
            if scene.thumbnails.contains_key(part) {
                return;
            }
            let mut capture = thumbnail::Capture::default();
            for solid in self.scene.solids() {
                let name = solid.name.as_str();
                let inside = name == occurrence || name.strip_prefix(occurrence).is_some_and(|rest| rest.starts_with(':'));
                if let Some(display) = unposed.get(name).filter(|display| inside && !display.is_sketch) {
                    capture.bodies.extend(thumbnail::body_of(display));
                }
            }
            if let Some(png) = thumbnail::render_png(&capture, PLAYER_THUMBNAIL_PX) {
                scene.thumbnails.insert(part.to_string(), player::png_data_url(&png));
            }
        };
        let pmi = self.pmi_state();
        for (_, step) in block.steps() {
            // The step view's explode poses, a matrix per posed node, in the
            // order the pictures apply them.
            if let Some(view_id) = step.view.as_deref() {
                let report = self.pmi_report.as_ref().and_then(|report| report.view(view_id));
                let posed: Vec<player::Pose> = scene
                    .nodes
                    .iter()
                    .enumerate()
                    .filter_map(|(node, n)| {
                        let poses = crate::sheets::project::poses_of(report, &n.occurrence);
                        (!poses.is_empty()).then(|| player::Pose { node, matrix: poses.iter().fold(IDENTITY, |m, pose| mul4(pose.matrix(), m)) })
                    })
                    .collect();
                if !posed.is_empty() {
                    scene.poses.insert(step.id.clone(), posed);
                }
            }
            let view = step.view.as_deref().and_then(|id| pmi.views.iter().find(|view| view.id == id));
            let camera = view.and_then(|view| view.camera.as_ref());
            // The PMI the view shows, laid out as the 3D PDF lays it out.
            if let (Some(view), Some(camera)) = (view, camera) {
                if let Some(rows) = self.pmi_report.as_ref().and_then(|report| report.view(&view.id)) {
                    let drawn = crate::sheets::pdf3d::view_annotations(view, camera, rows);
                    let texts: Vec<String> = drawn.iter().map(|(row, _)| row.text.clone()).filter(|text| !text.trim().is_empty()).collect();
                    let lines: Vec<Vec<[f64; 3]>> = drawn.iter().flat_map(|(_, d)| d.lines.iter().cloned()).collect();
                    let arrows: Vec<[[f64; 3]; 3]> = drawn.iter().flat_map(|(_, d)| d.arrows.iter().copied()).collect();
                    if let Some(drawing) = player::PlayerPmi::new(&lines, &arrows, drawn.len(), texts) {
                        scene.pmi.insert(step.id.clone(), drawing);
                    }
                }
            }
            if let Some(camera) = camera {
                let (fov_deg, half_height) = match camera.projection {
                    PmiProjection::Perspective { fov_y_deg } => (Some(fov_y_deg), None),
                    PmiProjection::Orthographic { half_height } => (None, Some(half_height)),
                };
                scene.cameras.insert(step.id.clone(), player::Camera { eye: camera.eye, target: camera.target, up: camera.up, fov_deg, half_height });
                if let Ok(png) = self.instructions_step_png(&step.id, PLAYER_IMAGE_PX) {
                    scene.step_images.insert(step.id.clone(), player::png_data_url(&png));
                }
            }
            for add in &step.adds {
                // The part's first occurrence: its solid, or a sub-assembly's.
                if let Some(occurrence) = add.occurrences.first() {
                    thumbnail_of(&mut scene, &add.part, occurrence);
                }
            }
        }
        // The assembly's tree, each part with its thumbnail: the page's Model
        // and Parts tabs.
        scene.assembly = player::PlayerComponent::rows(&level);
        let parts: Vec<(String, String)> = scene.assembly.iter().map(|row| (row.part.clone(), row.path.clone())).collect();
        for (part, path) in parts {
            thumbnail_of(&mut scene, &part, &path);
        }
        PlayerData::build(&block, scene)
    }

    /// The engine's placement of an occurrence of the wrapper's assembly
    /// (`ACOMP1:ACOMP3`), column-major for the player: the component
    /// features' solved poses composed along the path, the top document's
    /// then each sub-assembly document's, as the kernel places them. `None`
    /// for a path the documents do not place.
    pub fn instructions_occurrence_placement(&self, occurrence: &str) -> Option<[f64; 16]> {
        let mut world: [f64; 16] = [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0];
        let mut features: Vec<serde_json::Value> = self.history.features().to_vec();
        let mut library = self.history.parts_library().clone();
        for id in occurrence.split(':') {
            let feature = features
                .iter()
                .find(|f| super::components::is_acomp_feature_type(f["type"].as_str().unwrap_or_default()) && f["inputParams"]["id"].as_str() == Some(id))?;
            let pose = crate::pipeline::pose_params_to_transform(&feature["inputParams"]["transform"]);
            // Row-major product: the sub-assembly's pose applies first.
            let mut out = [0.0; 16];
            for row in 0..4 {
                for col in 0..4 {
                    out[row * 4 + col] = (0..4).map(|k| world[row * 4 + k] * pose[k * 4 + col]).sum();
                }
            }
            world = out;
            let part = feature["inputParams"]["partName"].as_str()?.to_string();
            let document = library[part.as_str()]["document"].clone();
            features = document["features"].as_array().cloned().unwrap_or_default();
            library = document["partsLibrary"].clone();
        }
        // Column-major for the page.
        Some(std::array::from_fn(|i| world[(i % 4) * 4 + i / 4]))
    }

    /// The offline player: one self-contained `.html` with the player data
    /// inlined, in `mode`. Returns the page and the data it carries.
    pub fn instructions_html(&self, mode: Mode) -> Result<(String, PlayerData), String> {
        let data = self.instructions_player_data()?;
        Ok((player::offline_html(&data, mode), data))
    }
}

/// Column-major 4×4 product `a · b` (b applied first).
fn mul4(a: [f64; 16], b: [f64; 16]) -> [f64; 16] {
    let mut out = [0.0; 16];
    for col in 0..4 {
        for row in 0..4 {
            out[col * 4 + row] = (0..4).map(|k| a[k * 4 + row] * b[col * 4 + k]).sum();
        }
    }
    out
}

/// `display`'s body for a step picture: posed by `poses` in order (points,
/// and normals through the same map), grey and without edges when an earlier
/// step `placed` it.
fn posed_body(display: &SolidDisplay, poses: &[crate::sheets::project::SolidPose], placed: bool) -> Option<thumbnail::Body> {
    let mut body = thumbnail::body_of(display)?;
    if !poses.is_empty() {
        let pose = |p: [f64; 3]| poses.iter().fold(p, |p, pose| pose.apply(p));
        let f64s = |p: [f32; 3]| [p[0] as f64, p[1] as f64, p[2] as f64];
        let f32s = |p: [f64; 3]| [p[0] as f32, p[1] as f32, p[2] as f32];
        for (i, normal) in body.normals.iter_mut().enumerate() {
            let Some(&at) = body.positions.get(i) else { break };
            let (p, n) = (f64s(at), f64s(*normal));
            let (from, to) = (pose(p), pose([p[0] + n[0], p[1] + n[1], p[2] + n[2]]));
            *normal = f32s([to[0] - from[0], to[1] - from[1], to[2] - from[2]]);
        }
        for position in body.positions.iter_mut() {
            *position = f32s(pose(f64s(*position)));
        }
        for line in body.edges.iter_mut() {
            for point in line.iter_mut() {
                *point = f32s(pose(f64s(*point)));
            }
        }
    }
    if placed {
        body.colors.iter_mut().for_each(|colour| *colour = PLACED_RGB);
        body.edges.clear();
    }
    Some(body)
}

/// How close (paper mm) a visible segment's midpoint must lie to one of the
/// added parts' own runs to be drawn in the highlight stroke.
const HIGHLIGHT_TOLERANCE_MM: f64 = 0.02;

/// `runs` split into the runs to draw in the model stroke and those to draw
/// highlighted: a segment is highlighted when its midpoint lies on a segment
/// of `added` (the same projection with only the added parts drawn, so every
/// run of theirs that survives occlusion by the rest is found there). Each
/// input run is cut where its class changes.
pub(super) fn split_highlighted(runs: Vec<Vec<[f64; 2]>>, added: &[Vec<[f64; 2]>]) -> (Vec<Vec<[f64; 2]>>, Vec<Vec<[f64; 2]>>) {
    let segments: Vec<([f64; 2], [f64; 2])> = added.iter().flat_map(|run| run.windows(2).map(|w| (w[0], w[1]))).collect();
    let on_added = |p: [f64; 2]| segments.iter().any(|(a, b)| point_segment_distance(p, *a, *b) <= HIGHLIGHT_TOLERANCE_MM);
    let (mut plain, mut lit) = (Vec::new(), Vec::new());
    for run in runs {
        let mut current: Vec<[f64; 2]> = Vec::new();
        let mut class: Option<bool> = None;
        for pair in run.windows(2) {
            let mid = [(pair[0][0] + pair[1][0]) * 0.5, (pair[0][1] + pair[1][1]) * 0.5];
            let highlighted = on_added(mid);
            if class.is_some_and(|c| c != highlighted) {
                let done = std::mem::take(&mut current);
                if class == Some(true) { lit.push(done) } else { plain.push(done) }
            }
            if current.is_empty() {
                current.push(pair[0]);
            }
            current.push(pair[1]);
            class = Some(highlighted);
        }
        if current.len() >= 2 {
            if class == Some(true) { lit.push(current) } else { plain.push(current) }
        }
    }
    (plain, lit)
}

fn point_segment_distance(p: [f64; 2], a: [f64; 2], b: [f64; 2]) -> f64 {
    let (dx, dy) = (b[0] - a[0], b[1] - a[1]);
    let len2 = dx * dx + dy * dy;
    let t = if len2 <= 0.0 { 0.0 } else { (((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / len2).clamp(0.0, 1.0) };
    ((p[0] - a[0] - t * dx).powi(2) + (p[1] - a[1] - t * dy).powi(2)).sqrt()
}