draftwright 0.2.15

Automated technical drawings from STEP B-rep: a Rust port of draftwright.
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
//! Recognisable shapes among the faces quiddity leaves unexplained (#213): a protrusion, a cut,
//! a chamfer on a hole or shaft edge, a turned step or land, or a periodic tooth pattern. Read
//! from the part's topology when the Analysis is made; the plan warns of each one the Spec does
//! not state (`shape_not_recognised`), beside `unclaimed_geometry`'s count, congruent shapes in
//! one warning.

use std::collections::BTreeSet;

use haecceity::brep::{Arc, Part};
use haecceity::geom::{self, Surface, V3};
use serde::{Deserialize, Serialize};

use crate::model::{Analysis, FaceGroup, Problem, Spec};

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Kind {
    /// Faces standing out of the part: met by the rest along concave edges only.
    Protrusion,
    /// Faces of a recess: their own faces meet along concave edges.
    Cut,
    /// A cone on the edge of a coaxial cylinder, which no hole or turned feature states.
    Chamfer,
    /// Six or more congruent curved faces evenly spaced about one axis.
    Teeth,
    /// A shoulder plane with coaxial curved faces about its centre: a turned step or land.
    Step,
}

impl Kind {
    pub fn name(self) -> &'static str {
        match self {
            Kind::Protrusion => "protrusion",
            Kind::Cut => "cut",
            Kind::Chamfer => "chamfer",
            Kind::Teeth => "teeth",
            Kind::Step => "step",
        }
    }
}

/// A chamfer's axial length and angle to the axis (degrees), on a cylinder of *diameter*.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Chamfer {
    pub diameter: f64,
    pub length: f64,
    pub angle: f64,
}

/// A periodic pattern's count and axis, in the part's frame.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Teeth {
    pub count: usize,
    pub centre: [f64; 3],
    pub axis: [f64; 3],
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Shape {
    pub kind: Kind,
    /// Ascending file face indices.
    pub faces: Vec<usize>,
    /// The faces' box, mm, in the part's frame.
    pub bounds: [[f64; 3]; 2],
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub chamfer: Option<Chamfer>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub teeth: Option<Teeth>,
    /// A step's shoulder diameters, the outer edge of each of its planes, ascending.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub shoulders: Vec<f64>,
}

const TOL: f64 = 1e-3;

fn parallel(a: V3, b: V3) -> bool {
    geom::norm(geom::cross(a, b)) <= TOL
}

/// The distance of *p* from the line through *o* along unit *d*.
fn off_axis(p: V3, o: V3, d: V3) -> f64 {
    let v = geom::sub(p, o);
    geom::norm(geom::sub(v, geom::scale(d, geom::dot(v, d))))
}

fn axis_of(s: &Surface) -> Option<(V3, V3)> {
    match s {
        Surface::Cylinder { frame, .. }
        | Surface::Cone { frame, .. }
        | Surface::Torus { frame, .. } => Some((frame.origin, frame.z)),
        _ => None,
    }
}

fn bounds(part: &Part, faces: &[usize]) -> [[f64; 3]; 2] {
    let mut b = geom::Bounds::empty();
    for &f in faces {
        b.merge(&part.face_bounds(f));
    }
    [b.min, b.max]
}

/// The shapes among *accounting*'s unexplained faces of *part*. Planar faces on the part's box
/// are its stock, never a shape's.
pub fn shapes(part: &Part, accounting: &[FaceGroup]) -> Vec<Shape> {
    let n = part.faces.len().min(accounting.len());
    let whole = part.bounds();
    let scale = (0..3).fold(1.0f64, |m, k| m.max(whole.max[k] - whole.min[k]));
    // On a side of the part's box, and spanning half of it one way: a tongue's end face on
    // the box is not stock.
    let stock = |i: usize| {
        matches!(part.faces[i].surface, Surface::Plane { .. }) && {
            let b = part.face_bounds(i);
            (0..3).any(|k| {
                b.max[k] - b.min[k] <= TOL
                    && ((b.min[k] - whole.min[k]).abs() <= TOL
                        || (b.max[k] - whole.max[k]).abs() <= TOL)
                    && (0..3).any(|j| {
                        j != k && b.max[j] - b.min[j] >= 0.5 * (whole.max[j] - whole.min[j])
                    })
            })
        }
    };
    let mut free: BTreeSet<usize> = (0..n)
        .filter(|&i| accounting[i] == FaceGroup::Unexplained && !stock(i))
        .collect();
    if free.is_empty() {
        return Vec::new();
    }
    let mut out = Vec::new();
    // Chamfers: a cone meeting a coaxial cylinder at a convex edge, with the cone's other
    // coaxial halves.
    for &i in &free.clone() {
        let Surface::Cone { semi_angle, .. } = part.faces[i].surface else {
            continue;
        };
        if !free.contains(&i) {
            continue;
        }
        let (o, d) = axis_of(&part.faces[i].surface).expect("a cone has an axis");
        let coaxial = |j: usize| {
            axis_of(&part.faces[j].surface)
                .is_some_and(|(p, e)| parallel(d, e) && off_axis(p, o, d) <= TOL * scale)
        };
        let Some(radius) =
            part.neighbours(i)
                .into_iter()
                .find_map(|j| match part.faces[j].surface {
                    Surface::Cylinder { radius, .. }
                        if coaxial(j) && part.arc(i, j) == Some(Arc::Convex) =>
                    {
                        Some(radius)
                    }
                    _ => None,
                })
        else {
            continue;
        };
        let mut faces = vec![i];
        let mut k = 0;
        while k < faces.len() {
            for j in part.neighbours(faces[k]) {
                if free.contains(&j)
                    && !faces.contains(&j)
                    && matches!(part.faces[j].surface, Surface::Cone { semi_angle: s, .. } if (s - semi_angle).abs() <= TOL)
                    && coaxial(j)
                {
                    faces.push(j);
                }
            }
            k += 1;
        }
        let (lo, hi) = part.extent_along(&faces, d);
        let length = hi - lo;
        if !(length > TOL && length <= 2.0 * radius) {
            continue;
        }
        for f in &faces {
            free.remove(f);
        }
        faces.sort_unstable();
        out.push(Shape {
            kind: Kind::Chamfer,
            bounds: bounds(part, &faces),
            faces,
            chamfer: Some(Chamfer {
                diameter: 2.0 * radius,
                length,
                angle: semi_angle.abs().to_degrees(),
            }),
            teeth: None,
            shoulders: Vec::new(),
        });
    }
    out.extend(teeth(part, &mut free, scale));
    // Cuts: faces joined by concave edges.
    let concave = |a: usize, b: usize| part.arc(a, b) == Some(Arc::Concave);
    let convex = |a: usize, b: usize| part.arc(a, b) == Some(Arc::Convex);
    let mut protrusions: Vec<Vec<usize>> = Vec::new();
    for kind in [Kind::Cut, Kind::Protrusion] {
        if kind == Kind::Protrusion {
            out.extend(steps(part, &mut free, scale));
        }
        let mut seen = BTreeSet::new();
        for &seed in &free.clone() {
            if seen.contains(&seed) || !free.contains(&seed) {
                continue;
            }
            let mut faces = vec![seed];
            seen.insert(seed);
            let mut k = 0;
            while k < faces.len() {
                let a = faces[k];
                for b in part.neighbours(a) {
                    if free.contains(&b)
                        && !seen.contains(&b)
                        && (kind == Kind::Protrusion || concave(a, b))
                    {
                        seen.insert(b);
                        faces.push(b);
                    }
                }
                k += 1;
            }
            // Spanning most of the part two ways, it is the part's body, not a shape on it.
            let [lo, hi] = bounds(part, &faces);
            let spans = (0..3)
                .filter(|&k| hi[k] - lo[k] >= 0.75 * (whole.max[k] - whole.min[k]))
                .count();
            if faces.len() < 2 || spans >= 2 {
                continue;
            }
            // A protrusion stands on the rest of the part, met by it at a concave edge; its own
            // faces meet at none, as concave pairs are cuts by now.
            if kind == Kind::Protrusion
                && !faces.iter().any(|&a| {
                    part.neighbours(a)
                        .into_iter()
                        .any(|b| !faces.contains(&b) && concave(a, b))
                })
            {
                continue;
            }
            for f in &faces {
                free.remove(f);
            }
            if kind == Kind::Protrusion {
                protrusions.push(faces);
                continue;
            }
            faces.sort_unstable();
            out.push(Shape {
                kind,
                bounds: bounds(part, &faces),
                faces,
                chamfer: None,
                teeth: None,
                shoulders: Vec::new(),
            });
        }
    }
    // Protrusions that meet one claimed face at convex edges are one: a tab's two sides, either
    // side of its recognised wall. The face a protrusion stands on meets it at concave edges.
    let walls = |faces: &[usize]| -> BTreeSet<usize> {
        faces
            .iter()
            .flat_map(|&a| {
                part.neighbours(a).into_iter().filter(move |&c| {
                    accounting.get(c) == Some(&FaceGroup::Feature) && convex(a, c)
                })
            })
            .collect()
    };
    let mut k = 0;
    while k < protrusions.len() {
        let mine = walls(&protrusions[k]);
        match (k + 1..protrusions.len()).find(|&j| !mine.is_disjoint(&walls(&protrusions[j]))) {
            Some(j) => {
                let other = protrusions.remove(j);
                protrusions[k].extend(other);
            }
            None => k += 1,
        }
    }
    for mut faces in protrusions {
        faces.sort_unstable();
        out.push(Shape {
            kind: Kind::Protrusion,
            bounds: bounds(part, &faces),
            faces,
            chamfer: None,
            teeth: None,
            shoulders: Vec::new(),
        });
    }
    out.sort_by(|a, b| a.faces.cmp(&b.faces));
    out
}

/// Turned steps and lands: a free round shoulder plane about an axis, with the free coaxial
/// cylinders, cones and tori joined to it, whatever their edges. Read after the cuts and before
/// the protrusions, as a land's faces meet along tangent edges and may span the whole part.
fn steps(part: &Part, free: &mut BTreeSet<usize>, scale: f64) -> Vec<Shape> {
    let mut out = Vec::new();
    for &seed in &free.clone() {
        if !free.contains(&seed) {
            continue;
        }
        let Some((o, d)) = axis_of(&part.faces[seed].surface) else {
            continue;
        };
        let Some(d) = geom::unit(d) else { continue };
        let centre = |f: usize| {
            let b = part.face_bounds(f);
            geom::scale(geom::add(b.min, b.max), 0.5)
        };
        // A round plane's box is square across the axis, like the circle bounding it.
        let round = |j: usize| {
            let b = part.face_bounds(j);
            let w: Vec<f64> = (0..3)
                .filter(|&k| d[k].abs() < 1.0 - TOL)
                .map(|k| b.max[k] - b.min[k])
                .collect();
            w.len() < 2 || (w[0] - w[1]).abs() <= 0.01 * w[0].max(w[1])
        };
        let member = |j: usize| match &part.faces[j].surface {
            Surface::Plane { frame } => {
                parallel(frame.z, d) && off_axis(centre(j), o, d) <= TOL * scale && round(j)
            }
            s => {
                axis_of(s).is_some_and(|(p, e)| parallel(e, d) && off_axis(p, o, d) <= TOL * scale)
            }
        };
        let mut faces = vec![seed];
        let mut k = 0;
        while k < faces.len() {
            for j in part.neighbours(faces[k]) {
                if free.contains(&j) && !faces.contains(&j) && member(j) {
                    faces.push(j);
                }
            }
            k += 1;
        }
        // The coordinate direction most across the axis, to read a plane's outer radius.
        let across = (0..3)
            .min_by(|&a, &b| d[a].abs().total_cmp(&d[b].abs()))
            .unwrap_or(0);
        let mut shoulders: Vec<f64> = faces
            .iter()
            .filter(|&&f| matches!(part.faces[f].surface, Surface::Plane { .. }))
            .map(|&f| {
                let b = part.face_bounds(f);
                b.max[across] - b.min[across]
            })
            .collect();
        if faces.len() < 2 || shoulders.is_empty() {
            continue;
        }
        shoulders.sort_by(f64::total_cmp);
        for f in &faces {
            free.remove(f);
        }
        faces.sort_unstable();
        out.push(Shape {
            kind: Kind::Step,
            bounds: bounds(part, &faces),
            faces,
            chamfer: None,
            teeth: None,
            shoulders,
        });
    }
    out
}

/// *d* in one sense: its largest component positive.
fn sense(d: V3) -> V3 {
    let k = (0..3)
        .max_by(|&a, &b| d[a].abs().total_cmp(&d[b].abs()))
        .unwrap_or(2);
    if d[k] < 0.0 { geom::scale(d, -1.0) } else { d }
}

/// Classes of six or more congruent unexplained cylinders evenly spaced about one axis, each
/// with the free faces that touch them: a gear, sprocket or pulley's teeth.
fn teeth(part: &Part, free: &mut BTreeSet<usize>, scale: f64) -> Vec<Shape> {
    let key = |x: f64, step: f64| (x / step).round() as i64;
    let mut classes = std::collections::BTreeMap::<_, Vec<usize>>::new();
    for &i in free.iter() {
        let Surface::Cylinder { frame, radius } = &part.faces[i].surface else {
            continue;
        };
        let d = sense(frame.z);
        let (lo, hi) = part.extent_along(&[i], d);
        classes
            .entry((
                key(*radius, 0.01),
                d.map(|c| key(c, 0.01)),
                key(hi - lo, 0.05),
            ))
            .or_default()
            .push(i);
    }
    let mut out: Vec<Shape> = Vec::new();
    for ((_, _, _), faces) in classes {
        if faces.len() < 6 || faces.iter().any(|f| !free.contains(f)) {
            continue;
        }
        let Surface::Cylinder { frame, .. } = &part.faces[faces[0]].surface else {
            continue;
        };
        let Some(d) = geom::unit(sense(frame.z)) else {
            continue;
        };
        let flat = |p: V3| geom::sub(p, geom::scale(d, geom::dot(p, d)));
        let points: Vec<V3> = faces
            .iter()
            .map(|&f| match &part.faces[f].surface {
                Surface::Cylinder { frame, .. } => flat(frame.origin),
                _ => unreachable!("the class holds cylinders"),
            })
            .collect();
        let m = points.len() as f64;
        let centre = points
            .iter()
            .fold([0.0; 3], |c, p| geom::add(c, geom::scale(*p, 1.0 / m)));
        let radii: Vec<f64> = points
            .iter()
            .map(|p| geom::norm(geom::sub(*p, centre)))
            .collect();
        let r = radii.iter().sum::<f64>() / m;
        if r <= TOL * scale || radii.iter().any(|x| (x - r).abs() > 0.02 * r) {
            continue;
        }
        let (u, v) = {
            let a = geom::scale(geom::sub(points[0], centre), 1.0 / radii[0]);
            (a, geom::cross(d, a))
        };
        let mut angles: Vec<f64> = points
            .iter()
            .map(|p| {
                let q = geom::sub(*p, centre);
                geom::dot(q, v).atan2(geom::dot(q, u))
            })
            .collect();
        angles.sort_by(f64::total_cmp);
        let gaps: Vec<f64> = (0..angles.len())
            .map(|i| {
                let next = angles
                    .get(i + 1)
                    .copied()
                    .unwrap_or(angles[0] + std::f64::consts::TAU);
                next - angles[i]
            })
            .collect();
        let even = |step: usize| {
            (0..gaps.len()).all(|i| (gaps[i] - gaps[(i + step) % gaps.len()]).abs() <= 0.02)
        };
        let count = if even(1) {
            faces.len()
        } else if faces.len() % 2 == 0 && even(2) {
            faces.len() / 2
        } else {
            continue;
        };
        // The same axis as a pattern already found: its teeth's other faces.
        let axis_point = geom::add(centre, geom::scale(d, geom::dot(frame.origin, d)));
        if let Some(s) = out.iter_mut().find(|s| {
            s.teeth.as_ref().is_some_and(|t| {
                parallel(t.axis, d) && off_axis(axis_point, t.centre, t.axis) <= TOL * scale
            })
        }) {
            s.faces.extend(&faces);
            if let Some(t) = &mut s.teeth {
                t.count = t.count.max(count);
            }
        } else {
            out.push(Shape {
                kind: Kind::Teeth,
                faces,
                // Read below, once the pattern's faces are all gathered.
                bounds: [[0.0; 3]; 2],
                chamfer: None,
                teeth: Some(Teeth {
                    count,
                    centre: axis_point,
                    axis: d,
                }),
                shoulders: Vec::new(),
            });
        }
    }
    // The teeth's other free faces: those touching them, out from the axis as far as their
    // cylinders' centres nearly are (never a web or face across the axis).
    let centre_off = |f: usize, t: &Teeth| {
        let b = part.face_bounds(f);
        off_axis(geom::scale(geom::add(b.min, b.max), 0.5), t.centre, t.axis)
    };
    for s in &mut out {
        for f in &s.faces {
            free.remove(f);
        }
        let Some(t) = s.teeth.clone() else { continue };
        let root = s
            .faces
            .iter()
            .map(|&f| centre_off(f, &t))
            .fold(f64::INFINITY, f64::min);
        let mut k = 0;
        while k < s.faces.len() {
            for j in part.neighbours(s.faces[k]) {
                if free.contains(&j) && centre_off(j, &t) >= 0.8 * root {
                    free.remove(&j);
                    s.faces.push(j);
                }
            }
            k += 1;
        }
        s.faces.sort_unstable();
        s.bounds = bounds(part, &s.faces);
    }
    out
}

fn num(x: f64) -> String {
    let s = format!("{:.2}", x + 0.0);
    let s = s.trim_end_matches('0').trim_end_matches('.');
    if s == "-0" { "0".into() } else { s.into() }
}

/// The faces by index, the first dozen of a longer list (the subjects name them all).
fn faces_text(faces: &[usize]) -> String {
    let list = faces
        .iter()
        .take(12)
        .map(|f| f.to_string())
        .collect::<Vec<_>>();
    match faces.len() {
        1 => format!("Face {}", list[0]),
        n if n > 12 => format!("{n} faces ({}, …)", list.join(", ")),
        _ => format!("Faces {}", list.join(", ")),
    }
}

const UNSIZED: [&str; 2] = [
    "recognition does not give it, so it is neither sized nor located",
    "recognition does not give them, so they are neither sized nor located",
];

/// What a shape is, without its faces or place, and what its warning says is missing, of one
/// shape and of several. Congruent shapes describe alike.
fn describe(s: &Shape) -> (String, [&'static str; 2]) {
    let [lo, hi] = s.bounds;
    let size = format!(
        "{} × {} × {} mm",
        num(hi[0] - lo[0]),
        num(hi[1] - lo[1]),
        num(hi[2] - lo[2])
    );
    match (s.kind, &s.chamfer, &s.teeth) {
        (Kind::Chamfer, Some(c), _) => (
            format!(
                "a {} × {}° chamfer on the edge of a ø{} cylinder",
                num(c.length),
                num(c.angle),
                num(c.diameter)
            ),
            [
                "no hole callout or chamfer dimension states it",
                "no hole callout or chamfer dimension states them",
            ],
        ),
        (Kind::Teeth, _, Some(t)) => (
            format!(
                "a periodic pattern of {} teeth or notches about the axis through ({}, {}, {}) along ({}, {}, {}), {size}",
                t.count,
                num(t.centre[0]),
                num(t.centre[1]),
                num(t.centre[2]),
                num(t.axis[0]),
                num(t.axis[1]),
                num(t.axis[2])
            ),
            ["no tooth form or count is drawn"; 2],
        ),
        (Kind::Step, _, _) => (
            format!(
                "a turned step or land, its {} ø{}, {size}",
                if s.shoulders.len() == 1 {
                    "shoulder"
                } else {
                    "shoulders"
                },
                s.shoulders
                    .iter()
                    .map(|d| num(*d))
                    .collect::<Vec<_>>()
                    .join(", ø")
            ),
            UNSIZED,
        ),
        (kind, _, _) => (format!("a {} of {size}", kind.name()), UNSIZED),
    }
}

/// A warning for each class of congruent shapes of *analysis* that *spec* does not state: none
/// of a shape's faces is *claimed* by a feature (a feature declaration's included) or carries
/// the Spec's leader note or feature control frame (`faces`), and no authored gear lies on a
/// tooth pattern's axis.
pub fn problems(analysis: &Analysis, spec: &Spec, claimed: &BTreeSet<usize>) -> Vec<Problem> {
    let mut stated = claimed.clone();
    for (key, f) in &spec.faces {
        if f.note.is_some() || !f.controls.is_empty() {
            stated.extend(crate::spec_gdt::named_faces(analysis, key).unwrap_or_default());
        }
    }
    let geared = |t: &Teeth| {
        spec.gears.iter().any(|g| {
            let k = ["x", "y", "z"].iter().position(|a| *a == g.frame.axis);
            k.is_some_and(|k| {
                let mut d = [0.0; 3];
                d[k] = 1.0;
                parallel(d, t.axis) && off_axis(t.centre, g.frame.origin, d) <= 0.01
            })
        })
    };
    let mut classes: Vec<((Kind, String), Vec<&Shape>)> = Vec::new();
    for s in analysis.unexplained_shapes.iter().filter(|s| {
        !s.faces.iter().any(|f| stated.contains(f)) && !s.teeth.as_ref().is_some_and(geared)
    }) {
        let key = (s.kind, describe(s).0);
        match classes.iter_mut().find(|(k, _)| *k == key) {
            Some((_, members)) => members.push(s),
            None => classes.push((key, vec![s])),
        }
    }
    classes
        .into_iter()
        .map(|((kind, what), members)| {
            let first = members[0];
            let [lo, hi] = first.bounds;
            let at = format!(
                "({}, {}, {})",
                num((lo[0] + hi[0]) / 2.0),
                num((lo[1] + hi[1]) / 2.0),
                num((lo[2] + hi[2]) / 2.0)
            );
            let all: Vec<usize> = members.iter().flat_map(|s| s.faces.iter().copied()).collect();
            let faces = faces_text(&all);
            let (_, [one, many]) = describe(first);
            let shape = if members.len() == 1 {
                let verb = match (kind, all.len()) {
                    (Kind::Chamfer, 1) => "is",
                    (Kind::Chamfer, _) => "are",
                    _ => "form",
                };
                format!("{faces} {verb} {what} at {at}; {one}")
            } else {
                format!(
                    "{faces} form {} congruent shapes, each {what}, the first at {at}; {many}",
                    members.len()
                )
            };
            let remedy = match kind {
                Kind::Teeth => {
                    "State the gear in the Spec's gears, which withdraws this warning for a gear on this axis, or add a note on one of its faces in the Spec's faces."
                }
                Kind::Chamfer => {
                    "State it with a note on one of its faces in the Spec's faces (its size, as a chamfer callout), which withdraws this warning; otherwise review it manually."
                }
                _ => {
                    "If it is a hole, boss, turned step, slot or pocket, declare it in the Spec's feature_declarations using its face fingerprints; otherwise state it with a note or feature control frame on one of its faces in the Spec's faces, which withdraws this warning."
                }
            };
            Problem {
                id: format!("p:shape_not_recognised:face:{}", first.faces[0]),
                code: "shape_not_recognised".into(),
                severity: "warning".into(),
                message: format!("{shape}. {remedy}"),
                subjects: all.iter().map(|f| format!("face:{f}")).collect(),
                refs: vec![format!("shape:{}", kind.name())],
                ..Default::default()
            }
        })
        .collect()
}