BREP_kernel 0.21.0

A boundary representation (BREP) geometry kernel for building CAD applications.
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
//! Joint-face fitting for fillet-weld beads along a seam of straight runs and
//! circular arcs.
//!
//! A fillet weld sits in the corner two faces make along the seam. The faces
//! may belong to one body (a concave edge) or to two bodies that touch along
//! the seam (a plate standing on another, in one part or across two placed
//! components; a tube standing on a plate). Each leg runs along its face,
//! perpendicular to the seam, on the side of the OTHER face's outward normal,
//! so the bead section is the triangle `P, P + l1·d1, P + l2·d2` inside the
//! free wedge and never enters either body.
//!
//! The section is fitted at the seam's start and carried along it: unchanged
//! along a straight run (the bead is extruded), turned about the axis along an
//! arc (the bead is revolved). A face qualifies only where the carried section
//! stays exact: its normal is constant along a straight run, and turns with
//! the arc about its axis (a plane square to the axis, a coaxial cylinder or
//! cone).
//!
//! A picked face may also stand a small root gap off a straight seam, parallel
//! to it (a sheet-metal closed corner leaves one between the flange edge and
//! the other flange's face). The section's root then moves across the gap onto
//! the picked face's plane, along the other joint face's plane, and the leg
//! that crosses the gap is backed only where its face begins.
use crate::feature_pipeline::SceneMap;
use crate::{FaceRecord, NurbsCurve, PointClass, PolygonClass, Vec2, Vec3};

/// Seam fractions sampled to decide that a face carries the seam.
const STATIONS: [f64; 5] = [0.0, 0.25, 0.5, 0.75, 1.0];
/// Faces closer than this to parallel make no corner a fillet weld can fill.
const MIN_CORNER_SIN: f64 = 0.035; // about 2 degrees

#[derive(Clone, Debug)]
pub(super) struct JointFace {
    pub name: String,
    pub solid: String,
    handle: u32,
    face_id: u64,
    /// The outward normal at the seam's start.
    normal: Vec3,
    /// For a picked face across a root gap from a straight seam: the offset
    /// from the seam to its foot on the face (constant along the seam).
    gap: Option<Vec3>,
}

impl JointFace {
    pub(super) fn same(&self, other: &JointFace) -> bool {
        same(self, other)
    }
}

/// The fitted section at the seam's start: unit leg directions along each
/// joint face and the faces they lie on, leg 1 first.
#[derive(Clone, Debug)]
pub(super) struct Fit {
    pub faces: [JointFace; 2],
    pub legs: [Vec3; 2],
    /// The offset from the seam to the section's root: zero, or across a
    /// root gap onto the gapped face's plane.
    pub root: Vec3,
    /// How far along each leg from the root its face begins (the gap a leg
    /// crosses; zero for a leg that starts on its face).
    pub skip: [f64; 2],
}

/// Why a seam has no fitted section. AUTO placement falls back to the manual
/// orientation on any of these; FITTED placement reports them.
pub(super) type NoFit = String;

/// How a seam segment runs, and so how a section is carried along it.
#[derive(Clone, Copy, Debug)]
pub(super) enum Run {
    /// Straight: the section is extruded along the tangent.
    Line,
    /// A circular arc of `angle` radians, right-handed about `axis` through
    /// `centre`: the section is revolved.
    Arc { centre: Vec3, axis: Vec3, angle: f64 },
}

pub(super) struct Seam {
    pub start: Vec3,
    /// The unit tangent at the start.
    pub tangent: Vec3,
    pub length: f64,
    pub run: Run,
    /// The solid holding the seam edge, when the seam is a resident edge.
    pub owner: Option<String>,
}

/// `v` turned by `angle` radians right-handed about the unit `axis`.
fn rotate(v: Vec3, axis: Vec3, angle: f64) -> Vec3 {
    let (sin, cos) = angle.sin_cos();
    v.scale(cos).add(axis.cross(v).scale(sin)).add(axis.scale(axis.dot(v) * (1.0 - cos)))
}

impl Seam {
    pub(super) fn line(start: Vec3, tangent: Vec3, length: f64, owner: Option<String>) -> Seam {
        Seam { start, tangent, length, run: Run::Line, owner }
    }

    /// The seam a curve runs along: a straight line or a circular arc (a full
    /// circle included). `None` for any other curve.
    pub(super) fn from_curve(curve: &NurbsCurve, owner: Option<String>) -> Result<Option<Seam>, String> {
        let [t0, t1] = curve.domain()?;
        let start = curve.evaluate(t0)?;
        let end = curve.evaluate(t1)?;
        if curve.degree == 1 && curve.control_points.len() == 2 {
            let delta = end.sub(start);
            return Ok(Some(Seam::line(start, delta.normalized()?, delta.length(), owner)));
        }
        // Sampled recognition at a tolerance relative to the curve's size: an
        // arc a sketch or a primitive built is exact to rounding.
        let scale = curve.control_points.iter()
            .map(|pole| if pole.w.abs() > 1e-300 { Vec3::new(pole.x / pole.w, pole.y / pole.w, pole.z / pole.w) } else { Vec3::ZERO })
            .map(|point| point.sub(start).length()).fold(1.0_f64, f64::max);
        let Some(circle) = crate::recognize_circle(curve, t0, t1, 1e-9 * scale) else { return Ok(None) };
        // The axis turns with the curve: its start tangent is `axis × radius`.
        let radial = start.sub(circle.centre);
        let tangent = curve.unit_tangent(t0, t0, t1)?;
        let axis = if circle.axis.cross(radial).dot(tangent) >= 0.0 { circle.axis } else { circle.axis.scale(-1.0) };
        let to = end.sub(circle.centre);
        let mut angle = axis.dot(radial.cross(to)).atan2(radial.dot(to));
        if angle <= 1e-12 {
            angle += std::f64::consts::TAU;
        }
        let tangent = axis.cross(radial).normalized()?;
        Ok(Some(Seam { start, tangent, length: circle.radius * angle, run: Run::Arc { centre: circle.centre, axis, angle }, owner }))
    }

    pub(super) fn at(&self, fraction: f64) -> Vec3 {
        match self.run {
            Run::Line => self.start.add(self.tangent.scale(self.length * fraction)),
            Run::Arc { centre, axis, angle } => centre.add(rotate(self.start.sub(centre), axis, angle * fraction)),
        }
    }

    /// A direction fixed in the section at the start, carried to `fraction`.
    pub(super) fn carry(&self, fraction: f64, direction: Vec3) -> Vec3 {
        match self.run {
            Run::Line => direction,
            Run::Arc { axis, angle, .. } => rotate(direction, axis, angle * fraction),
        }
    }

    pub(super) fn end_tangent(&self) -> Vec3 {
        self.carry(1.0, self.tangent)
    }

    pub(super) fn is_line(&self) -> bool {
        matches!(self.run, Run::Line)
    }

    fn tolerance(&self) -> f64 {
        1e-6_f64.max(self.length * 1e-12)
    }
}

/// Where a straight run's bead is cut at a cornered join: the mitre plane
/// (a point and its normal, pointing along the seam) at each end, and how far
/// the run is extended past that end so the cut has material to trim.
#[derive(Clone, Copy, Debug, Default)]
pub(super) struct Trim {
    pub ends: [Option<(Vec3, Vec3, f64)>; 2],
}

/// The section `points` (a closed polygon at the seam's start, square to it)
/// carried along the seam: extruded along a straight run (and mitred where
/// `trim` says), revolved along an arc.
pub(super) fn sweep(seam: &Seam, points: &[Vec3], trim: &Trim) -> Result<crate::BrepSolid, String> {
    match seam.run {
        Run::Line => {
            let before = trim.ends[0].map_or(0.0, |(_, _, extend)| extend);
            let after = trim.ends[1].map_or(0.0, |(_, _, extend)| extend);
            let shift = seam.tangent.scale(-before);
            let moved: Vec<Vec3> = points.iter().map(|point| point.add(shift)).collect();
            let profile = (0..moved.len()).map(|i| crate::make_line(moved[i], moved[(i + 1) % moved.len()]))
                .collect::<Result<Vec<_>, _>>()?;
            let mut solid = crate::extrude_profile_brep(&profile, seam.tangent, seam.length + before + after)
                .map_err(|error| error.to_string())?;
            for (end, cut) in trim.ends.iter().enumerate() {
                let Some((point, normal, _)) = cut else { continue };
                let (below, above) = crate::split_solid_by_plane(&solid, *point, *normal)
                    .map_err(|error| format!("could not mitre the bead: {error}"))?;
                // The start cut keeps what lies ahead of it, the end cut what lies behind.
                solid = if end == 0 { above } else { below };
            }
            Ok(solid)
        }
        Run::Arc { centre, axis, angle } => {
            if trim.ends.iter().any(Option::is_some) {
                return Err("an arc is never mitred".into());
            }
            let profile = (0..points.len()).map(|i| crate::make_line(points[i], points[(i + 1) % points.len()]))
                .collect::<Result<Vec<_>, _>>()?;
            crate::revolve_profile_brep(&profile, centre, axis, angle)
        }
    }
}

fn with_face<T>(handle: u32, face_id: u64, f: impl FnOnce(&FaceRecord) -> Result<T, String>) -> Result<T, String> {
    crate::with_registered_solid_str(handle, |solid| {
        let face = solid.shells.iter().flat_map(|shell| &shell.faces).find(|face| face.id == face_id)
            .ok_or_else(|| format!("face {face_id} is no longer resident"))?;
        f(face)
    })
}

fn face_class(face: &FaceRecord, point: Vec3, tolerance: f64) -> Result<Option<PolygonClass>, String> {
    let projection = crate::project_point_to_surface(&face.surface, point)?;
    if projection.distance > tolerance {
        return Ok(None);
    }
    crate::parameter_point_in_face(face, Vec2 { x: projection.u, y: projection.v }, tolerance).map(Some)
}

/// The face's outward normal at the seam's start if the seam lies on it along
/// its whole length, inside or on its trim, and the normal is carried with the
/// section: constant along a straight run, turning about an arc's axis.
fn carries_seam(face: &FaceRecord, seam: &Seam) -> Result<Option<Vec3>, String> {
    let tolerance = seam.tolerance();
    let mut normal: Option<Vec3> = None;
    for fraction in STATIONS {
        let point = seam.at(fraction);
        if !matches!(face_class(face, point, tolerance)?, Some(PolygonClass::Inside | PolygonClass::Boundary)) {
            return Ok(None);
        }
        let (_, outward) = crate::fillet::face_plane_normal(face, point).map_err(|refusal| refusal.to_string())?;
        match normal {
            None => normal = Some(outward),
            // A face that turns against the section is not a joint face.
            Some(first) => {
                let carried = seam.carry(fraction, first);
                if carried.cross(outward).length() > 1e-9 || carried.dot(outward) < 0.0 {
                    return Ok(None);
                }
            }
        }
    }
    Ok(normal)
}

/// An axis-aligned box around the body. A NURBS curve or surface lies in the
/// convex hull of its poles, so the poles of every edge and every face carrier
/// bound the body, curved face interiors included.
fn bounds(solid: &crate::BrepSolid) -> (Vec3, Vec3) {
    let mut low = Vec3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY);
    let mut high = Vec3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
    let edge_poles = solid.edges.iter().flat_map(|edge| edge.curve.control_points.iter());
    let face_poles = solid.shells.iter().flat_map(|shell| &shell.faces)
        .flat_map(|face| face.surface.control_points.iter().flatten());
    for pole in edge_poles.chain(face_poles) {
        let w = if pole.w.abs() > 1e-300 { pole.w } else { 1.0 };
        let p = Vec3::new(pole.x / w, pole.y / w, pole.z / w);
        low = Vec3::new(low.x.min(p.x), low.y.min(p.y), low.z.min(p.z));
        high = Vec3::new(high.x.max(p.x), high.y.max(p.y), high.z.max(p.z));
    }
    (low, high)
}

fn solid_bounds_contain(solid: &crate::BrepSolid, points: &[Vec3], tolerance: f64) -> bool {
    let (low, high) = bounds(solid);
    points.iter().all(|p| {
        p.x >= low.x - tolerance && p.y >= low.y - tolerance && p.z >= low.z - tolerance
            && p.x <= high.x + tolerance && p.y <= high.y + tolerance && p.z <= high.z + tolerance
    })
}

/// A bead built by a weld feature (every face a bead face) offers no joint
/// faces to another weld; a body a bead was merged into still does. Every
/// body, beads included, is an obstacle to the bead.
fn is_weld_bead(solid: &crate::BrepSolid) -> bool {
    solid.shells.iter().flat_map(|shell| &shell.faces)
        .all(|face| face.name.as_deref().is_some_and(|name| name.contains(":BeadFace")))
}

/// Every resident face that carries the seam, in scene-name order.
fn candidates(scene: &SceneMap, seam: &Seam, exclude: &str) -> Result<Vec<JointFace>, String> {
    let ends = [seam.at(0.0), seam.at(1.0)];
    let mut names: Vec<(&String, &u32)> = scene.solids.iter().filter(|(name, _)| name.as_str() != exclude).collect();
    names.sort();
    let mut found = Vec::new();
    for (solid_name, &handle) in names {
        let faces = crate::with_registered_solid_str(handle, |solid| {
            if is_weld_bead(solid) || !solid_bounds_contain(solid, &ends, seam.tolerance()) {
                return Ok(Vec::new());
            }
            let mut faces = Vec::new();
            for face in solid.shells.iter().flat_map(|shell| &shell.faces) {
                // A face this check cannot read (a degenerate normal at a pole,
                // a failed projection) is not a joint face; it is never fatal.
                if let Ok(Some(normal)) = carries_seam(face, seam) {
                    faces.push((face.id, face.name.clone(), normal));
                }
            }
            Ok(faces)
        })?;
        for (face_id, name, normal) in faces {
            let name = name.unwrap_or_else(|| format!("{solid_name} face {face_id}"));
            found.push(JointFace { name, solid: solid_name.clone(), handle, face_id, normal, gap: None });
        }
    }
    Ok(found)
}

/// A picked flat face standing a root gap off a straight seam: parallel to it,
/// the seam on its free side at one distance along the whole seam, and the gap
/// narrower than `widest`. `None` for any other face. Where on the face the
/// root lands depends on the other joint face (an oblique one moves it off the
/// seam's perpendicular foot), so the fit's corner probe, toe checks and the
/// support check decide that the face holds the root and its leg.
fn across_gap(scene: &SceneMap, seam: &Seam, name: &str, widest: f64) -> Result<Option<JointFace>, String> {
    if !seam.is_line() {
        return Ok(None);
    }
    let tolerance = seam.tolerance();
    for face in scene.resolve_face_group(name) {
        let found = with_face(face.handle, face.face_id, |record| {
            let mut first: Option<(Vec3, Vec3)> = None;
            for fraction in STATIONS {
                let point = seam.at(fraction);
                // The foot on the face's plane, not on its bounded carrier
                // (a projection there clamps to the patch's edge).
                let Ok((on, outward)) = crate::fillet::face_plane_normal(record, point) else { return Ok(None) };
                // Parallel to the seam, the seam outside the face.
                let gap = point.sub(on).dot(outward);
                if outward.dot(seam.tangent).abs() > 1e-9 || gap <= tolerance || gap >= widest {
                    return Ok(None);
                }
                // Flat: every pole of the carrier lies on that plane.
                let flat = record.surface.control_points.iter().flatten().all(|pole| {
                    let w = if pole.w.abs() > 1e-300 { pole.w } else { 1.0 };
                    Vec3::new(pole.x / w, pole.y / w, pole.z / w).sub(on).dot(outward).abs() <= tolerance
                });
                if !flat {
                    return Ok(None);
                }
                let offset = outward.scale(-gap);
                match first {
                    None => first = Some((offset, outward)),
                    Some((offset0, outward0)) => {
                        if offset.sub(offset0).length() > tolerance || outward.cross(outward0).length() > 1e-9 {
                            return Ok(None);
                        }
                    }
                }
            }
            Ok(first)
        })?;
        if let Some((offset, normal)) = found {
            let solid = scene.solids.iter().filter(|(_, handle)| **handle == face.handle)
                .map(|(name, _)| name.clone()).min().unwrap_or_default();
            return Ok(Some(JointFace { name: name.to_string(), solid, handle: face.handle, face_id: face.face_id, normal, gap: Some(offset) }));
        }
    }
    Ok(None)
}

/// Whether the named face stands a bridgeable root gap off a straight seam.
pub(super) fn bridges(scene: &SceneMap, seam: &Seam, name: &str, legs: [f64; 2]) -> Result<bool, String> {
    Ok(across_gap(scene, seam, name, legs[0].max(legs[1]))?.is_some())
}

/// The section's root for a pair: the offset from the seam to it and how far
/// each leg runs before its face begins. Across a root gap the root moves
/// along the face carrying the seam onto the gapped face's plane, backwards
/// along that face's leg; `None` when it cannot (both faces gapped, or the
/// faces too near parallel to meet).
fn root_of(pair: [&JointFace; 2], tangent: Vec3) -> Result<Option<(Vec3, [f64; 2])>, String> {
    let (gapped, carrier) = match (pair[0].gap, pair[1].gap) {
        (None, None) => return Ok(Some((Vec3::ZERO, [0.0, 0.0]))),
        (Some(_), None) => (0, 1),
        (None, Some(_)) => (1, 0),
        (Some(_), Some(_)) => return Ok(None),
    };
    let offset = pair[gapped].gap.expect("the gapped face");
    let normal = pair[gapped].normal;
    let leg = leg_direction(pair[carrier], pair[gapped], tangent)?;
    let across = leg.dot(normal);
    if across.abs() < MIN_CORNER_SIN {
        return Ok(None);
    }
    let shift = leg.scale(offset.dot(normal) / across);
    // The carrying face's leg runs from the root back across the gap.
    if shift.dot(leg) >= 0.0 {
        return Ok(None);
    }
    let mut skip = [0.0; 2];
    skip[carrier] = shift.length();
    Ok(Some((shift, skip)))
}

/// The leg direction along `face`, on the free side of `other`.
fn leg_direction(face: &JointFace, other: &JointFace, tangent: Vec3) -> Result<Vec3, String> {
    let direction = face.normal.cross(tangent).normalized()?;
    Ok(if direction.dot(other.normal) < 0.0 { direction.scale(-1.0) } else { direction })
}

fn runs_onto(face: &JointFace, point: Vec3, tolerance: f64) -> Result<bool, String> {
    with_face(face.handle, face.face_id, |record| {
        Ok(matches!(face_class(record, point, tolerance)?, Some(PolygonClass::Inside | PolygonClass::Boundary)))
    })
}

fn outside_solid(scene: &SceneMap, solid: &str, point: Vec3, tolerance: f64) -> Result<bool, String> {
    let handle = scene.solids.get(solid).copied().ok_or_else(|| format!("solid '{solid}' is no longer resident"))?;
    crate::with_registered_solid_str(handle, |body| {
        Ok(crate::classify_point(point, body, tolerance)?.class == PointClass::Out)
    })
}

/// Check the pair makes a corner with free space between the legs: both short
/// legs land on their faces and the wedge between them is outside every body
/// that offered a joint face.
fn corner(scene: &SceneMap, seam: &Seam, pair: [&JointFace; 2], solids: &[String], probe: f64) -> Result<Option<[Vec3; 2]>, String> {
    if pair[0].normal.cross(pair[1].normal).length() < MIN_CORNER_SIN {
        return Ok(None);
    }
    let legs = [leg_direction(pair[0], pair[1], seam.tangent)?, leg_direction(pair[1], pair[0], seam.tangent)?];
    let Some((root, skip)) = root_of(pair, seam.tangent)? else { return Ok(None) };
    let tolerance = seam.tolerance();
    let middle = seam.at(0.5).add(seam.carry(0.5, root));
    let carried = [seam.carry(0.5, legs[0]), seam.carry(0.5, legs[1])];
    for ((face, leg), skip) in pair.iter().zip(&carried).zip(skip) {
        if !runs_onto(face, middle.add(leg.scale(skip + probe)), tolerance)? {
            return Ok(None);
        }
    }
    let wedge = middle.add(carried[0].add(carried[1]).scale(probe / 3.0));
    for solid in solids {
        if !outside_solid(scene, solid, wedge, tolerance * 0.1)? {
            return Ok(None);
        }
    }
    Ok(Some(legs))
}

/// Find the joint along a straight or circular seam. `explicit` holds the faces the user
/// selected (in selection order, at most two). `legs` are the requested leg
/// sizes; the toe of each leg must stay on its face along the whole seam.
/// `previous`, the fit of the run before on a chain, keeps leg 1 on the face
/// leg 1 ran along there (checked before the leg sizes are).
pub(super) fn fit(scene: &SceneMap, seam: &Seam, explicit: &[String], exclude: &str, legs: [f64; 2], previous: Option<&Fit>) -> Result<Result<Fit, NoFit>, String> {
    if explicit.len() > 2 {
        return Err("select at most two joint faces".into());
    }
    let all = candidates(scene, seam, exclude)?;
    let mut chosen = Vec::new();
    for name in explicit {
        // A container name picks the member that carries the seam.
        let group = scene.resolve_face_group(name);
        if let Some(joint) = all.iter().find(|joint| group.iter().any(|face| joint.handle == face.handle && joint.face_id == face.face_id)) {
            chosen.push(JointFace { name: name.clone(), ..joint.clone() });
            continue;
        }
        // A flat face a root gap off a straight seam, narrower than the legs.
        if let Some(joint) = across_gap(scene, seam, name, legs[0].max(legs[1]))? {
            chosen.push(joint);
            continue;
        }
        return Ok(Err(format!("joint face '{name}' does not carry the seam along its whole length {}",
            if seam.is_line() { "as a flat face" } else { "square to the arc's axis or coaxial with it" })));
    }
    let mut solids: Vec<String> = all.iter().chain(chosen.iter()).map(|joint| joint.solid.clone()).collect();
    solids.sort();
    solids.dedup();
    // A picked face across a root gap pairs with the faces carrying the seam.
    let pool: Vec<JointFace> = all.iter().cloned().chain(chosen.iter().filter(|joint| joint.gap.is_some()).cloned()).collect();
    let probe = (legs[0].min(legs[1]) * 0.05).min(seam.length * 0.05).max(seam.tolerance() * 10.0);
    let mut pairs: Vec<([JointFace; 2], [Vec3; 2])> = Vec::new();
    match chosen.as_slice() {
        [first, second] => {
            if let Some(legs) = corner(scene, seam, [first, second], &solids, probe)? {
                pairs.push(([first.clone(), second.clone()], legs));
            }
        }
        _ => {
            for (i, a) in pool.iter().enumerate() {
                for b in &pool[i + 1..] {
                    if let [only] = chosen.as_slice() {
                        if !(same(only, a) || same(only, b)) {
                            continue;
                        }
                    }
                    if corner(scene, seam, [a, b], &solids, probe)?.is_some() {
                        let [f0, f1] = order(seam, chosen.first(), [a, b]);
                        let legs = [leg_direction(&f0, &f1, seam.tangent)?, leg_direction(&f1, &f0, seam.tangent)?];
                        pairs.push(([f0, f1], legs));
                    }
                }
            }
        }
    }
    let ([mut first, mut second], mut legs_dir) = match pairs.len() {
        0 if all.is_empty() => return Ok(Err(if seam.is_line() {
            "no face carries the seam as a flat face".into()
        } else {
            "no face carries the arc square to its axis or coaxial with it".into()
        })),
        0 => return Ok(Err(format!(
            "no concave corner along the seam between faces {}", list(&all)))),
        1 => pairs.pop().expect("one pair"),
        _ => return Ok(Err(format!(
            "several corners meet at the seam ({}); select the two joint faces",
            pairs.iter().map(|(faces, _)| format!("{} + {}", faces[0].name, faces[1].name)).collect::<Vec<_>>().join(", ")))),
    };
    if let Some(previous) = previous {
        if first.same(&previous.faces[1]) || second.same(&previous.faces[0]) {
            std::mem::swap(&mut first, &mut second);
            legs_dir.swap(0, 1);
        }
    }
    let tolerance = seam.tolerance();
    let Some((root, skip)) = root_of([&first, &second], seam.tangent)? else {
        return Ok(Err(format!("no root across the gap between faces {} and {}", first.name, second.name)));
    };
    for (((face, leg), size), skip) in [&first, &second].iter().zip(&legs_dir).zip(legs).zip(skip) {
        if size <= skip + tolerance {
            return Ok(Err(format!("leg {size} does not reach across the {skip} root gap onto joint face {}", face.name)));
        }
        for fraction in STATIONS {
            let toe = seam.at(fraction).add(seam.carry(fraction, root.add(leg.scale(size))));
            if !runs_onto(face, toe, tolerance)? {
                return Ok(Err(format!("leg {size} runs off joint face {}", face.name)));
            }
        }
    }
    Ok(Ok(Fit { faces: [first, second], legs: legs_dir, root, skip }))
}

/// Does the named face carry the whole seam (on or inside its trim)?
pub(super) fn carries(scene: &SceneMap, seam: &Seam, name: &str) -> Result<bool, String> {
    for face in scene.resolve_face_group(name) {
        if with_face(face.handle, face.face_id, |record| carries_any(record, seam))? {
            return Ok(true);
        }
    }
    Ok(false)
}

fn carries_any(face: &FaceRecord, seam: &Seam) -> Result<bool, String> {
    let tolerance = seam.tolerance();
    for fraction in STATIONS {
        if !matches!(face_class(face, seam.at(fraction), tolerance)?, Some(PolygonClass::Inside | PolygonClass::Boundary)) {
            return Ok(false);
        }
    }
    Ok(true)
}

fn same(a: &JointFace, b: &JointFace) -> bool {
    a.handle == b.handle && a.face_id == b.face_id
}

fn list(faces: &[JointFace]) -> String {
    faces.iter().map(|face| face.name.as_str()).collect::<Vec<_>>().join(", ")
}

/// Leg 1 goes on the selected face, else on the seam owner's face, else on
/// the face whose name sorts first, so unequal legs never swap on rebuild.
fn order(seam: &Seam, selected: Option<&JointFace>, pair: [&JointFace; 2]) -> [JointFace; 2] {
    let first_wins = match selected {
        Some(selected) => same(selected, pair[0]) || !same(selected, pair[1]),
        None => {
            let owned = |face: &JointFace| seam.owner.as_deref() == Some(face.solid.as_str());
            match (owned(pair[0]), owned(pair[1])) {
                (true, false) => true,
                (false, true) => false,
                _ => (&pair[0].name, &pair[0].solid) <= (&pair[1].name, &pair[1].solid),
            }
        }
    };
    if first_wins { [pair[0].clone(), pair[1].clone()] } else { [pair[1].clone(), pair[0].clone()] }
}

/// Joins closer to straight than this (1 − cos of the turn) are tangent: the
/// two segments share the section there.
const TANGENT_JOIN: f64 = 1e-9;
/// A mitre plane must cut the runs it trims at least this squarely (cosine of
/// half the turn): past it the seam folds back on itself (about 168.5°, the
/// sweep's own mitre limit).
const MITRE_FLOOR: f64 = 0.1;

/// A seam of straight runs and circular arcs, head to tail, with the mitre
/// plane at each cornered join.
pub(super) struct Chain {
    pub runs: Vec<Seam>,
    /// `corners[k]`: the join after run `k` (the closing one last on a closed
    /// chain) as the mitre plane's point and forward normal, or `None` where
    /// the runs meet tangent.
    corners: Vec<(usize, usize, Option<(Vec3, Vec3)>)>,
}

impl Chain {
    /// Classify the joins of `runs` (ordered head to tail). A corner is
    /// mitred between two straight runs; a corner at an arc, or a turn that
    /// folds the seam back on itself, has no bead.
    pub(super) fn new(runs: Vec<Seam>) -> Result<Chain, NoFit> {
        let count = runs.len();
        let tolerance = runs.iter().map(Seam::tolerance).fold(0.0, f64::max);
        let closed = count > 1 && runs[count - 1].at(1.0).sub(runs[0].start).length() <= tolerance * 10.0;
        let mut corners = Vec::new();
        let joins: Vec<(usize, usize)> = (0..count.saturating_sub(1)).map(|k| (k, k + 1))
            .chain(closed.then(|| (count - 1, 0))).collect();
        let name = |k: usize| k + 1;
        for (a, b) in joins {
            let (out, into) = (runs[a].end_tangent(), runs[b].tangent);
            if out.dot(into) >= 1.0 - TANGENT_JOIN {
                corners.push((a, b, None));
                continue;
            }
            if !(runs[a].is_line() && runs[b].is_line()) {
                return Err(format!("segments {} and {} meet at a corner on an arc; a bead is mitred only between straight segments", name(a), name(b)));
            }
            let normal = out.add(into).normalized().map_err(|_| format!("the seam doubles back where segments {} and {} meet", name(a), name(b)))?;
            if out.dot(normal) < MITRE_FLOOR {
                return Err(format!("the seam turns back too sharply where segments {} and {} meet for a mitred bead", name(a), name(b)));
            }
            corners.push((a, b, Some((runs[b].start, normal))));
        }
        Ok(Chain { runs, corners })
    }

    pub(super) fn is_single_line(&self) -> bool {
        self.runs.len() == 1 && self.runs[0].is_line()
    }

    fn label(&self, k: usize) -> String {
        if self.runs.len() == 1 { String::new() } else { format!("segment {}: ", k + 1) }
    }

    /// The mitre cut at each end of each run, for a section reaching `reach`
    /// from the seam. A run too short for its two mitres has no bead.
    pub(super) fn trims(&self, reach: f64) -> Result<Vec<Trim>, NoFit> {
        let mut trims = vec![Trim::default(); self.runs.len()];
        let mut taken = vec![0.0_f64; self.runs.len()];
        for (a, b, corner) in &self.corners {
            let Some((point, normal)) = corner else { continue };
            let cos_half = self.runs[*b].tangent.dot(*normal);
            // A section point `w` off the seam meets the plane `w·tan(θ/2)`
            // from the join at most; extend past that before cutting.
            let tan_half = (1.0 - cos_half * cos_half).max(0.0).sqrt() / cos_half;
            let extend = reach * tan_half * 2.0 + reach * 0.01;
            trims[*a].ends[1] = Some((*point, *normal, extend));
            trims[*b].ends[0] = Some((*point, *normal, extend));
            taken[*a] += reach * tan_half;
            taken[*b] += reach * tan_half;
        }
        for (k, run) in self.runs.iter().enumerate() {
            if taken[k] >= run.length {
                return Err(format!("{}the segment is too short for the bead's mitred corners", self.label(k)));
            }
        }
        Ok(trims)
    }

    /// Fit every run. Leg 1 stays on the same face from run to run (fitting
    /// starts at the run carrying the first picked face and works outward
    /// both ways, each run ordered by its fitted neighbour), and where runs
    /// meet tangent their sections must agree.
    pub(super) fn fit(&self, scene: &SceneMap, explicit: &[String], exclude: &str, legs: [f64; 2]) -> Result<Result<Vec<Fit>, NoFit>, String> {
        let count = self.runs.len();
        // On a chain a picked face joins the runs it carries.
        let mut picked: Vec<Vec<String>> = Vec::with_capacity(count);
        for run in &self.runs {
            if count == 1 {
                picked.push(explicit.to_vec());
                continue;
            }
            let mut names = Vec::new();
            for name in explicit {
                if carries(scene, run, name)? {
                    names.push(name.clone());
                }
            }
            picked.push(names);
        }
        let start = explicit.first()
            .and_then(|first| picked.iter().position(|names| names.first() == Some(first)))
            .unwrap_or(0);
        let order: Vec<(usize, Option<usize>)> = std::iter::once((start, None))
            .chain((start + 1..count).map(|k| (k, Some(k - 1))))
            .chain((0..start).rev().map(|k| (k, Some(k + 1))))
            .collect();
        let mut fitted: Vec<Option<Fit>> = vec![None; count];
        for (k, neighbour) in order {
            let next = neighbour.and_then(|n| fitted[n].as_ref());
            match fit(scene, &self.runs[k], &picked[k], exclude, legs, next)? {
                Ok(joint) => fitted[k] = Some(joint),
                Err(reason) => return Ok(Err(format!("{}{reason}", self.label(k)))),
            }
        }
        let fits: Vec<Fit> = fitted.into_iter().map(|fit| fit.expect("every run fitted")).collect();
        for (a, b, corner) in &self.corners {
            if corner.is_some() {
                continue;
            }
            let tolerance = self.runs[*a].tolerance().max(self.runs[*b].tolerance()) * 10.0;
            let roots = [self.runs[*a].carry(1.0, fits[*a].root), fits[*b].root];
            if roots[0].sub(roots[1]).length() > tolerance {
                return Ok(Err(format!("the joint changes where segments {} and {} meet", a + 1, b + 1)));
            }
            for (i, size) in legs.iter().enumerate() {
                let end = self.runs[*a].carry(1.0, fits[*a].legs[i]).scale(*size);
                let start = fits[*b].legs[i].scale(*size);
                if end.sub(start).length() > tolerance {
                    return Ok(Err(format!("the joint changes where segments {} and {} meet", a + 1, b + 1)));
                }
            }
        }
        Ok(Ok(fits))
    }

    /// The bead: each run's section (its root's offset from the run's start
    /// and the toe offsets of its two legs from the root) carried along the run and mitred at corners, united, its
    /// faces named `{id}:BeadFace{n}`.
    pub(super) fn bead(&self, id: &str, sections: &[[Vec3; 3]], trims: &[Trim]) -> Result<crate::BrepSolid, String> {
        let mut bead: Option<crate::BrepSolid> = None;
        for ((run, [root, a, b]), trim) in self.runs.iter().zip(sections).zip(trims) {
            // Keep the section counter-clockwise about the seam.
            let (a, b) = if a.cross(*b).dot(run.tangent) >= 0.0 { (*a, *b) } else { (*b, *a) };
            let start = run.start.add(*root);
            let piece = sweep(run, &[start, start.add(a), start.add(b)], trim)?;
            bead = Some(match bead {
                None => piece,
                Some(sum) => crate::boolean_operation(&sum, &piece, crate::BooleanOperation::Union, &crate::BooleanOptions::default())
                    .map_err(|refusal| format!("could not join the bead's segments: {refusal}"))?,
            });
        }
        let mut bead = bead.ok_or("the seam is empty")?;
        for (i, face) in bead.shells.iter_mut().flat_map(|shell| &mut shell.faces).enumerate() {
            face.name = Some(format!("{id}:BeadFace{}", i + 1));
        }
        Ok(bead)
    }

    /// The exact checks on a fitted bead: clear of every body, every leg
    /// backed along every run.
    pub(super) fn verify(&self, scene: &SceneMap, fits: &[Fit], legs: [f64; 2], bead: &crate::BrepSolid, trims: &[Trim], exclude: &str) -> Result<Result<(), NoFit>, String> {
        let tolerance = self.runs.iter().map(Seam::tolerance).fold(0.0, f64::max);
        if let Err(reason) = clear(scene, bead, exclude, tolerance)? {
            return Ok(Err(reason));
        }
        for (k, ((run, joint), trim)) in self.runs.iter().zip(fits).zip(trims).enumerate() {
            if let Err(reason) = supported(run, joint, legs, trim)? {
                return Ok(Err(format!("{}{reason}", self.label(k))));
            }
        }
        Ok(Ok(()))
    }

    /// The normal of the plane every run lies in (an arc's axis), for placing
    /// a section by Orientation; `None` when the runs do not share one.
    pub(super) fn plane(&self) -> Option<Vec3> {
        let mut up: Option<Vec3> = None;
        for run in &self.runs {
            if let Run::Arc { axis, .. } = run.run {
                up.get_or_insert(axis);
            }
        }
        if up.is_none() {
            for pair in self.runs.windows(2) {
                if let Ok(normal) = pair[0].tangent.cross(pair[1].tangent).normalized() {
                    up = Some(normal);
                    break;
                }
            }
        }
        // Every run along one line: any normal of it will do.
        let up = match up {
            Some(up) => up,
            None => self.runs[0].tangent.perpendicular().ok()?,
        };
        let origin = self.runs[0].start;
        let flat = self.runs.iter().all(|run| {
            let tolerance = run.tolerance() * 10.0;
            let axis_ok = match run.run {
                Run::Line => run.tangent.dot(up).abs() <= 1e-9,
                Run::Arc { axis, .. } => axis.cross(up).length() <= 1e-9,
            };
            axis_ok && run.start.sub(origin).dot(up).abs() <= tolerance && run.at(1.0).sub(origin).dot(up).abs() <= tolerance
        });
        flat.then_some(up)
    }
}

/// The volume (cubic model units) a clearance or support boolean may leave
/// and still read as empty: fixed, so a large bead never hides a small defect.
const LEFTOVER_VOLUME: f64 = 1e-9;

/// A yes/no boolean: the result is only measured, so coplanar fragments are
/// left unmerged.
fn boolean(a: &crate::BrepSolid, b: &crate::BrepSolid, operation: crate::BooleanOperation) -> Result<crate::BrepSolid, crate::KernelRefusal> {
    let options = crate::BooleanOptions { merge_coplanar_faces: false, ..Default::default() };
    crate::boolean_operation(a, b, operation, &options)
}

/// An empty result where the operands only touch: the boolean refuses it as a
/// conservative empty overlap rather than returning no body.
fn empty(refusal: &crate::KernelRefusal) -> bool {
    matches!(refusal.class, crate::RefusalClass::ConservativeEmptyOverlap)
}

/// The volume two bodies share; a touching-only pair shares none.
fn common_volume(a: &crate::BrepSolid, b: &crate::BrepSolid) -> Result<f64, NoFit> {
    match boolean(a, b, crate::BooleanOperation::Intersect) {
        Ok(common) => crate::solid_mass_properties(&common).map(|m| m.volume.abs())
            .map_err(|error| format!("could not measure the common part: {error}")),
        Err(refusal) if empty(&refusal) => Ok(0.0),
        Err(refusal) => Err(format!("could not check the bead against the base metal: {refusal}")),
    }
}

/// Check the built bead exactly: it shares no volume with any resident body.
pub(super) fn clear(scene: &SceneMap, bead: &crate::BrepSolid, exclude: &str, tolerance: f64) -> Result<Result<(), NoFit>, String> {
    let (low, high) = bounds(bead);
    let mut names: Vec<(&String, &u32)> = scene.solids.iter().filter(|(name, _)| name.as_str() != exclude).collect();
    names.sort();
    for (name, &handle) in names {
        let body = crate::registered_solid_clone(handle)?;
        let (body_low, body_high) = bounds(&body);
        let apart = body_low.x > high.x + tolerance || body_low.y > high.y + tolerance || body_low.z > high.z + tolerance
            || body_high.x < low.x - tolerance || body_high.y < low.y - tolerance || body_high.z < low.z - tolerance;
        if apart {
            continue;
        }
        match common_volume(bead, &body) {
            Ok(shared) if shared > LEFTOVER_VOLUME => return Ok(Err(format!("the bead would overlap {name}"))),
            Ok(_) => {}
            Err(reason) => return Ok(Err(reason)),
        }
    }
    Ok(Ok(()))
}

/// Check each leg's whole strip on its joint face is backed by that face's
/// body (a thin slab under the strip, carried along the seam as the bead is
/// and trimmed as it is, lies entirely inside the body), so a bead never
/// bridges a hole, an edge or a gap in the face.
pub(super) fn supported(seam: &Seam, joint: &Fit, legs: [f64; 2], trim: &Trim) -> Result<Result<(), NoFit>, String> {
    for (i, (face, (direction, size))) in joint.faces.iter().zip(joint.legs.iter().zip(legs)).enumerate() {
        // Shallow enough for sheet-thin joint faces: the support check needs
        // only this much material under the leg.
        let depth = support_depth(seam);
        let start = seam.start.add(joint.root);
        let toe = start.add(direction.scale(size));
        // A leg across a root gap is backed from where its face begins.
        let start = start.add(direction.scale(joint.skip[i]));
        let inward = face.normal.scale(-depth);
        // Where the faces meet at other than a right angle, the slab's inner
        // corner at the seam would reach past the other joint face's plane (out
        // of this body, a cone on a plate); it is moved along the leg onto that
        // plane.
        let other = joint.faces[1 - i].normal;
        let shift = (depth * face.normal.dot(other) / direction.dot(other)).max(0.0);
        let slab = sweep(seam, &[start, toe, toe.add(inward), start.add(direction.scale(shift)).add(inward)], trim)?;
        let body = crate::registered_solid_clone(face.handle)?;
        // Backed means nothing of the slab is left once the body is removed:
        // any hole, edge or gap under the leg leaves a piece.
        match boolean(&slab, &body, crate::BooleanOperation::Subtract) {
            Err(refusal) if empty(&refusal) => {}
            Ok(rest) => {
                let left = crate::solid_mass_properties(&rest).map(|m| m.volume.abs()).unwrap_or(f64::INFINITY);
                if left > LEFTOVER_VOLUME {
                    return Ok(Err(format!("leg {size} is not supported along its whole length by joint face {}", face.name)));
                }
            }
            Err(refusal) => return Ok(Err(format!("could not check the support under leg {size}: {refusal}"))),
        }
    }
    Ok(Ok(()))
}

/// The material depth the support check reads under each leg: a hundred weld
/// floors (0.001 model units), ten times the band under which the boolean
/// reads two parallel planes as coincident, so the slab's floor is always
/// resolvable. Only a joint body thinner than this is refused as unsupported.
pub(super) fn support_depth(_seam: &Seam) -> f64 {
    100.0 * crate::tolerance::WELD_FLOOR
}

/// The seam two picked faces meet along: the one straight or circular edge of either face
/// that the other face carries (a concave edge both faces bound, or a plate's
/// edge standing on the other plate's face). Returned by its scene edge name.
pub(super) fn seam_between(scene: &SceneMap, faces: [&str; 2]) -> Result<Result<String, String>, String> {
    let mut found: Vec<(String, Vec3, Vec3, Vec3)> = Vec::new();
    for (own, other) in [(faces[0], faces[1]), (faces[1], faces[0])] {
        for face in scene.resolve_face_group(own) {
            let runs: Vec<(u64, NurbsCurve)> = crate::with_registered_solid_str(face.handle, |solid| {
                let record = solid.shells.iter().flat_map(|shell| &shell.faces).find(|f| f.id == face.face_id)
                    .ok_or_else(|| format!("face '{own}' is no longer resident"))?;
                let mut runs = Vec::new();
                for coedge in record.loops.iter().flat_map(|l| &l.coedges) {
                    let Some(edge) = solid.edges.iter().find(|e| e.id == coedge.edge_id) else { continue };
                    if edge.degenerate { continue; }
                    runs.push((edge.id, crate::feature_pipeline::features::common::trimmed_curve(&edge.curve, edge.t0, edge.t1)?));
                }
                Ok(runs)
            })?;
            for (edge_id, curve) in runs {
                let Ok(Some(seam)) = Seam::from_curve(&curve, None) else { continue };
                if seam.length <= seam.tolerance() { continue; }
                let (start, end, middle) = (seam.at(0.0), seam.at(1.0), seam.at(0.5));
                if !carries(scene, &seam, other)? { continue; }
                let mut names: Vec<&String> = scene.edges.iter()
                    .filter(|(_, edge)| edge.handle == face.handle && edge.edge_id == edge_id)
                    .map(|(name, _)| name).collect();
                names.sort();
                let Some(name) = names.first() else { continue };
                let tolerance = seam.tolerance();
                // The same edge seen from both faces: same ends and middle (two
                // half circles share their ends, not their middles).
                let same = |(_, a, b, m): &(String, Vec3, Vec3, Vec3)| m.sub(middle).length() < tolerance
                    && ((a.sub(start).length() < tolerance && b.sub(end).length() < tolerance)
                        || (a.sub(end).length() < tolerance && b.sub(start).length() < tolerance));
                if !found.iter().any(same) {
                    found.push(((*name).clone(), start, end, middle));
                }
            }
        }
    }
    Ok(match found.as_slice() {
        [(name, _, _, _)] => Ok(name.clone()),
        [] => Err(format!("faces '{}' and '{}' do not meet along a straight or circular edge; select the seam edge", faces[0], faces[1])),
        _ => Err(format!("faces '{}' and '{}' meet along several edges; select the seam edge", faces[0], faces[1])),
    })
}