BREP_kernel 0.3.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
use super::*;
use crate::topology::{FaceRecord, ShellRecord};
use crate::{
    interpolate_curve, make_arc, make_box_brep, make_cone_brep, make_cylinder_brep, make_line,
    make_revolution, make_sphere_brep, make_torus_brep, NurbsSurface, Vec4,
};

fn assert_close(actual: f64, expected: f64, label: &str) {
    assert!(
        (actual - expected).abs() < 1e-9,
        "{label}: expected {expected}, got {actual}"
    );
}

fn assert_vec3(actual: Vec3, expected: Vec3, label: &str) {
    assert_close(actual.x, expected.x, &format!("{label}.x"));
    assert_close(actual.y, expected.y, &format!("{label}.y"));
    assert_close(actual.z, expected.z, &format!("{label}.z"));
}

/// |direction| must equal ±axis (unit vectors).
fn assert_parallel(direction: Vec3, axis: Vec3, label: &str) {
    assert_close(direction.dot(axis).abs(), 1.0, label);
}

fn face_ids(solid: &BrepSolid) -> Vec<u64> {
    solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .map(|face| face.id)
        .collect()
}

/// A minimal one-face solid for hand-built surfaces (no loops/edges needed by
/// the non-planar face lanes).
fn face_only_solid(surface: NurbsSurface) -> BrepSolid {
    BrepSolid {
        id: 1,
        vertices: vec![],
        edges: vec![],
        shells: vec![ShellRecord {
            id: 1,
            faces: vec![FaceRecord {
                id: 1,
                surface,
                same_sense: true,
                loops: vec![],
                name: None,
            }],
        }],
        genus: 0,
    }
}

/// A minimal one-edge solid for hand-built curves.
fn edge_only_solid(curve: NurbsCurve, t0: f64, t1: f64, degenerate: bool) -> BrepSolid {
    BrepSolid {
        id: 1,
        vertices: vec![],
        edges: vec![crate::topology::EdgeRecord {
            id: 1,
            curve,
            t0,
            t1,
            start_vertex_id: 0,
            end_vertex_id: 0,
            degenerate,
            name: Some("E".into()),
        }],
        shells: vec![],
        genus: 0,
    }
}

// ---------------------------------------------------------------------------
// Faces
// ---------------------------------------------------------------------------

#[test]
fn box_faces_resolve_to_six_outward_planes() {
    let solid = make_box_brep(Vec3::new(0.0, 0.0, 0.0), 2.0, 3.0, 4.0).unwrap();
    let center = Vec3::new(1.0, 1.5, 2.0);
    let mut seen = [0usize; 6];
    for id in face_ids(&solid) {
        let SelectionGeometry::Plane { origin, normal } =
            resolve_face_selection(&solid, id).unwrap()
        else {
            panic!("box face {id} did not resolve to a plane");
        };
        assert_close(normal.length(), 1.0, "unit normal");
        // Outward: the normal points away from the box center.
        assert!(
            origin.sub(center).dot(normal) > 0.0,
            "face {id} normal points inward"
        );
        // Origin sits on the face plane at its boundary AABB center.
        let axes = [
            (Vec3::new(1.0, 0.0, 0.0), 2.0_f64),
            (Vec3::new(0.0, 1.0, 0.0), 3.0),
            (Vec3::new(0.0, 0.0, 1.0), 4.0),
        ];
        let slot = axes
            .iter()
            .position(|(axis, _)| normal.dot(*axis).abs() > 1.0 - 1e-9)
            .expect("axis-aligned normal");
        let (axis, size) = axes[slot];
        let positive = normal.dot(axis) > 0.0;
        seen[slot * 2 + positive as usize] += 1;
        let expected = center.add(axis.scale((size / 2.0) * if positive { 1.0 } else { -1.0 }));
        assert_vec3(origin, expected, "face origin");
    }
    assert_eq!(seen, [1; 6], "one face per box side");
}

#[test]
fn box_face_resolves_by_name() {
    let mut solid = make_box_brep(Vec3::new(0.0, 0.0, 0.0), 2.0, 2.0, 2.0).unwrap();
    solid.shells[0].faces[0].name = Some("lid".into());
    let by_id = resolve_face_selection(&solid, solid.shells[0].faces[0].id).unwrap();
    let by_name = resolve_named_selection(&solid, "lid").unwrap();
    assert_vec3(
        by_name.representative_point(),
        by_id.representative_point(),
        "name/id parity",
    );
    let missing = resolve_named_selection(&solid, "nope").unwrap_err();
    assert!(matches!(missing, ResolveError::NotFound { .. }));
    assert_eq!(missing.status(), "invalid-selection");
}

#[test]
fn cylinder_side_face_resolves_to_axis_with_radius() {
    let base = Vec3::new(1.0, 2.0, 3.0);
    let axis = Vec3::new(0.0, 0.0, 1.0);
    let solid = make_cylinder_brep(base, axis, 2.0, 5.0).unwrap();
    let mut side = None;
    let mut caps = 0usize;
    for id in face_ids(&solid) {
        match resolve_face_selection(&solid, id).unwrap() {
            SelectionGeometry::Axis {
                origin,
                direction,
                radius,
            } => {
                assert_parallel(direction, axis, "cylinder axis direction");
                // The axis origin lies on the construction axis line.
                assert_close(
                    origin.sub(base).cross(axis).length(),
                    0.0,
                    "axis origin off the construction axis",
                );
                assert_close(radius.expect("cylinder radius"), 2.0, "cylinder radius");
                side = Some(id);
            }
            SelectionGeometry::Plane { normal, .. } => {
                assert_parallel(normal, axis, "cap normal");
                caps += 1;
            }
            other => panic!("unexpected cylinder face resolution: {other:?}"),
        }
    }
    assert!(side.is_some(), "no side face resolved to an axis");
    assert_eq!(caps, 2, "two planar caps");
}

#[test]
fn cylinder_cap_edges_resolve_to_circles_and_seam_to_line() {
    let base = Vec3::new(1.0, 2.0, 3.0);
    let axis = Vec3::new(0.0, 0.0, 1.0);
    let solid = make_cylinder_brep(base, axis, 2.0, 5.0).unwrap();
    let mut circles = Vec::new();
    let mut lines = 0usize;
    for edge in &solid.edges {
        match resolve_edge_selection(&solid, edge.id).unwrap() {
            SelectionGeometry::Circle {
                center,
                axis: circle_axis,
                radius,
            } => {
                assert_parallel(circle_axis, axis, "cap circle axis");
                assert_close(radius, 2.0, "cap circle radius");
                circles.push(center);
            }
            SelectionGeometry::Line { origin, direction } => {
                // The cylinder seam is an axial straight edge.
                assert_parallel(direction, axis, "seam direction");
                assert_close(
                    origin.sub(base).dot(axis),
                    2.5,
                    "seam midpoint at half height",
                );
                lines += 1;
            }
            other => panic!("unexpected cylinder edge resolution: {other:?}"),
        }
    }
    assert_eq!(lines, 1, "one seam line");
    circles.sort_by(|a, b| a.z.total_cmp(&b.z));
    assert_eq!(circles.len(), 2, "two cap circles");
    assert_vec3(circles[0], base, "bottom cap center");
    assert_vec3(circles[1], base.add(axis.scale(5.0)), "top cap center");
}

#[test]
fn cone_side_face_resolves_to_axis_without_radius() {
    let base = Vec3::new(0.0, 0.0, 0.0);
    let axis = Vec3::new(0.0, 0.0, 1.0);
    let solid = make_cone_brep(base, axis, 3.0, 1.0, 4.0).unwrap();
    let cone_axes: Vec<_> = face_ids(&solid)
        .into_iter()
        .filter_map(|id| match resolve_face_selection(&solid, id).unwrap() {
            SelectionGeometry::Axis {
                origin,
                direction,
                radius,
            } => Some((origin, direction, radius)),
            _ => None,
        })
        .collect();
    let [(origin, direction, radius)] = cone_axes[..] else {
        panic!("expected exactly one axis-bearing cone face, got {cone_axes:?}");
    };
    assert_parallel(direction, axis, "cone axis direction");
    assert_close(origin.sub(base).cross(axis).length(), 0.0, "origin on axis");
    // Invariant: only constant-radius cylinders carry a radius.
    assert_eq!(radius, None, "cone must not report a radius");
}

#[test]
fn sphere_face_resolves_to_center_and_radius() {
    let center = Vec3::new(3.0, -1.0, 2.0);
    let solid = make_sphere_brep(center, 1.5, Vec3::new(0.0, 0.0, 1.0)).unwrap();
    let ids = face_ids(&solid);
    let SelectionGeometry::Sphere {
        center: resolved,
        radius,
    } = resolve_face_selection(&solid, ids[0]).unwrap()
    else {
        panic!("sphere face did not resolve to a sphere");
    };
    assert_vec3(resolved, center, "sphere center");
    assert_close(radius, 1.5, "sphere radius");
}

#[test]
fn torus_face_resolves_to_axis_without_radius() {
    let center = Vec3::new(0.0, 1.0, -2.0);
    let axis = Vec3::new(0.0, 0.0, 1.0);
    let solid = make_torus_brep(center, axis, 4.0, 1.0).unwrap();
    let ids = face_ids(&solid);
    let SelectionGeometry::Axis {
        origin,
        direction,
        radius,
    } = resolve_face_selection(&solid, ids[0]).unwrap()
    else {
        panic!("torus face did not resolve to an axis");
    };
    assert_parallel(direction, axis, "torus axis direction");
    assert_close(origin.sub(center).cross(axis).length(), 0.0, "origin on axis");
    assert_eq!(radius, None, "torus must not report a cylinder radius");
}

#[test]
fn partial_cylinder_revolution_resolves_with_radius() {
    // A 180° revolve of an axis-parallel line: a genuine half-cylinder.
    let generatrix = make_line(Vec3::new(2.0, 0.0, 0.0), Vec3::new(2.0, 0.0, 3.0)).unwrap();
    let surface = make_revolution(
        Vec3::new(0.0, 0.0, 0.0),
        Vec3::new(0.0, 0.0, 1.0),
        &generatrix,
        std::f64::consts::PI,
    )
    .unwrap();
    let solid = face_only_solid(surface);
    let SelectionGeometry::Axis {
        direction, radius, ..
    } = resolve_face_selection(&solid, 1).unwrap()
    else {
        panic!("half-cylinder did not resolve to an axis");
    };
    assert_parallel(direction, Vec3::new(0.0, 0.0, 1.0), "half-cylinder axis");
    assert_close(radius.expect("half-cylinder radius"), 2.0, "half-cylinder radius");
}

#[test]
fn hyperboloid_revolution_resolves_without_radius() {
    // A line SKEW to the axis with equal end radii revolves into a
    // hyperboloid: the radius dips between the ends, so no cylinder radius.
    let generatrix = make_line(Vec3::new(2.0, 0.0, 0.0), Vec3::new(0.0, 2.0, 3.0)).unwrap();
    let surface = make_revolution(
        Vec3::new(0.0, 0.0, 0.0),
        Vec3::new(0.0, 0.0, 1.0),
        &generatrix,
        std::f64::consts::PI,
    )
    .unwrap();
    let solid = face_only_solid(surface);
    let SelectionGeometry::Axis { radius, .. } = resolve_face_selection(&solid, 1).unwrap() else {
        panic!("hyperboloid did not resolve to an axis");
    };
    assert_eq!(radius, None, "hyperboloid must not report a radius");
}

#[test]
fn freeform_face_reports_unsupported() {
    // Bicubic patch with an interior bump: not planar, not a revolution.
    let mut rows = Vec::new();
    for i in 0..4 {
        let mut row = Vec::new();
        for j in 0..4 {
            let bump = if (1..=2).contains(&i) && (1..=2).contains(&j) {
                1.0
            } else {
                0.0
            };
            row.push(Vec4::from_point(
                Vec3::new(i as f64, j as f64, bump),
                1.0,
            ));
        }
        rows.push(row);
    }
    let knots = vec![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0];
    let surface = NurbsSurface::new(3, 3, knots.clone(), knots, rows).unwrap();
    let solid = face_only_solid(surface);
    let error = resolve_face_selection(&solid, 1).unwrap_err();
    assert!(matches!(error, ResolveError::Unsupported { .. }), "{error:?}");
    assert_eq!(error.status(), "unsupported-selection");
}

#[test]
fn unknown_face_id_reports_not_found() {
    let solid = make_box_brep(Vec3::new(0.0, 0.0, 0.0), 1.0, 1.0, 1.0).unwrap();
    let error = resolve_face_selection(&solid, 9999).unwrap_err();
    assert!(matches!(error, ResolveError::NotFound { .. }));
    assert_eq!(error.status(), "invalid-selection");
}

// ---------------------------------------------------------------------------
// Edges
// ---------------------------------------------------------------------------

#[test]
fn box_edges_resolve_to_carrier_lines_through_their_vertices() {
    let solid = make_box_brep(Vec3::new(0.0, 0.0, 0.0), 2.0, 3.0, 4.0).unwrap();
    assert_eq!(solid.edges.len(), 12);
    for edge in &solid.edges {
        let SelectionGeometry::Line { origin, direction } =
            resolve_edge_selection(&solid, edge.id).unwrap()
        else {
            panic!("box edge {} did not resolve to a line", edge.id);
        };
        assert_close(direction.length(), 1.0, "unit line direction");
        // The carrier passes through both endpoint vertices, and the origin
        // is their midpoint.
        let endpoints: Vec<Vec3> = [edge.start_vertex_id, edge.end_vertex_id]
            .iter()
            .map(|id| {
                solid
                    .vertices
                    .iter()
                    .find(|vertex| vertex.id == *id)
                    .unwrap()
                    .point
            })
            .collect();
        for point in &endpoints {
            assert_close(
                point.sub(origin).cross(direction).length(),
                0.0,
                "vertex off the carrier line",
            );
        }
        assert_vec3(
            origin,
            endpoints[0].add(endpoints[1]).scale(0.5),
            "line origin at chord midpoint",
        );
    }
}

#[test]
fn arc_edge_resolves_to_circle() {
    let curve = make_arc(
        Vec3::new(1.0, 1.0, 0.0),
        Vec3::new(1.0, 0.0, 0.0),
        Vec3::new(0.0, 1.0, 0.0),
        2.0,
        0.0,
        std::f64::consts::FRAC_PI_2,
    )
    .unwrap();
    let [t0, t1] = curve.domain().unwrap();
    let solid = edge_only_solid(curve, t0, t1, false);
    let SelectionGeometry::Circle {
        center,
        axis,
        radius,
    } = resolve_edge_selection(&solid, 1).unwrap()
    else {
        panic!("arc edge did not resolve to a circle");
    };
    assert_vec3(center, Vec3::new(1.0, 1.0, 0.0), "arc center");
    assert_close(radius, 2.0, "arc radius");
    // CCW from +x toward +y: right-hand rule gives +z.
    assert_vec3(axis, Vec3::new(0.0, 0.0, 1.0), "arc axis");
}

#[test]
fn spline_edge_reports_unsupported() {
    let points = [
        Vec3::new(0.0, 0.0, 0.0),
        Vec3::new(1.0, 0.4, 0.0),
        Vec3::new(2.0, -0.3, 0.2),
        Vec3::new(3.0, 0.0, 0.0),
    ];
    let parameters = [0.0, 1.0 / 3.0, 2.0 / 3.0, 1.0];
    let curve = interpolate_curve(&points, 3, &parameters).unwrap();
    let [t0, t1] = curve.domain().unwrap();
    let solid = edge_only_solid(curve, t0, t1, false);
    let error = resolve_edge_selection(&solid, 1).unwrap_err();
    assert!(matches!(error, ResolveError::Unsupported { .. }), "{error:?}");
    assert_eq!(error.status(), "unsupported-selection");
}

#[test]
fn degenerate_edge_reports_unsupported() {
    let point = Vec3::new(1.0, 1.0, 1.0);
    let curve = make_line(point, point.add(Vec3::new(0.0, 0.0, 1e-15))).unwrap();
    let solid = edge_only_solid(curve, 0.0, 1.0, true);
    let error = resolve_edge_selection(&solid, 1).unwrap_err();
    assert!(matches!(error, ResolveError::Unsupported { .. }));
    assert_eq!(error.status(), "unsupported-selection");
}

// ---------------------------------------------------------------------------
// Vertices and components
// ---------------------------------------------------------------------------

#[test]
fn vertex_resolves_to_exact_point_nearest_wins() {
    let solid = make_box_brep(Vec3::new(0.0, 0.0, 0.0), 2.0, 3.0, 4.0).unwrap();
    // A perturbed query snaps to the EXACT corner.
    let SelectionGeometry::Point { position } =
        resolve_vertex_selection(&solid, Vec3::new(2.0 + 1e-8, 3.0 - 1e-8, 4.0)).unwrap()
    else {
        panic!("vertex did not resolve to a point");
    };
    assert_vec3(position, Vec3::new(2.0, 3.0, 4.0), "snapped corner");
    // Nearest wins between two corners.
    let SelectionGeometry::Point { position } =
        resolve_vertex_selection(&solid, Vec3::new(0.9e-6, 0.0, 0.0)).unwrap()
    else {
        panic!("vertex did not resolve to a point");
    };
    assert_vec3(position, Vec3::new(0.0, 0.0, 0.0), "nearest corner");
    // Far from every vertex: not found.
    let error = resolve_vertex_selection(&solid, Vec3::new(10.0, 10.0, 10.0)).unwrap_err();
    assert!(matches!(error, ResolveError::NotFound { .. }));
    assert_eq!(error.status(), "invalid-selection");
}

#[test]
fn component_point_is_aggregate_bbox_center() {
    let a = make_box_brep(Vec3::new(0.0, 0.0, 0.0), 2.0, 2.0, 2.0).unwrap();
    let b = make_box_brep(Vec3::new(4.0, 0.0, 0.0), 2.0, 2.0, 2.0).unwrap();
    let SelectionGeometry::Point { position } = resolve_component_point(&[&a, &b]).unwrap() else {
        panic!("component did not resolve to a point");
    };
    assert_vec3(position, Vec3::new(3.0, 1.0, 1.0), "two-solid bbox center");
    let SelectionGeometry::Point { position } = resolve_component_point(&[&a]).unwrap() else {
        panic!("component did not resolve to a point");
    };
    assert_vec3(position, Vec3::new(1.0, 1.0, 1.0), "one-solid bbox center");
    assert!(resolve_component_point(&[]).is_err(), "empty solid set");
}

// ---------------------------------------------------------------------------
// Namespacing and localization
// ---------------------------------------------------------------------------

#[test]
fn component_namespace_parsing() {
    let (chain, local) = split_component_namespace("ACOMP2:Extrude1|Extrude1_top[0]");
    assert_eq!(chain, vec!["ACOMP2"]);
    assert_eq!(local, "Extrude1|Extrude1_top[0]");

    let (chain, local) = split_component_namespace("ACOMP3:ACOMP1:S1:G20");
    assert_eq!(chain, vec!["ACOMP3", "ACOMP1"]);
    assert_eq!(local, "S1:G20");

    // A bare component id stays in the LOCAL position (whole-component ref).
    let (chain, local) = split_component_namespace("ACOMP2");
    assert!(chain.is_empty());
    assert_eq!(local, "ACOMP2");
    assert!(is_component_reference(local));

    let (chain, local) = split_component_namespace("ACOMP3:ACOMP1");
    assert_eq!(chain, vec!["ACOMP3"]);
    assert_eq!(local, "ACOMP1");
    assert!(is_component_reference(local));

    // Sketch-child names never namespace.
    let (chain, local) = split_component_namespace("S1:G20");
    assert!(chain.is_empty());
    assert_eq!(local, "S1:G20");

    assert!(is_component_reference("ACOMP12"));
    assert!(!is_component_reference("ACOMP"));
    assert!(!is_component_reference("ACOMP1x"));
    assert!(!is_component_reference("TALN3"));
}

#[test]
fn transform_localizes_frames() {
    // World→local: rotate -90° about z after translating by (-5, 0, 0) —
    // the inverse of a component posed at translation (5,0,0), rotation +90°.
    // Column-major-free: row-major elements [r00 r01 r02 tx; ...].
    let world_to_local = AffineTransform::new([
        0.0, 1.0, 0.0, 0.0, //
        -1.0, 0.0, 0.0, 5.0, //
        0.0, 0.0, 1.0, 0.0, //
        0.0, 0.0, 0.0, 1.0,
    ])
    .unwrap();
    let plane = SelectionGeometry::Plane {
        origin: Vec3::new(1.0, 2.0, 3.0),
        normal: Vec3::new(1.0, 0.0, 0.0),
    };
    let SelectionGeometry::Plane { origin, normal } =
        plane.transformed(&world_to_local).unwrap()
    else {
        panic!("plane transformed into a different kind");
    };
    assert_vec3(origin, Vec3::new(2.0, 4.0, 3.0), "localized plane origin");
    assert_vec3(normal, Vec3::new(0.0, -1.0, 0.0), "localized plane normal");

    // Radii are invariant under rigid transforms…
    let circle = SelectionGeometry::Circle {
        center: Vec3::new(0.0, 0.0, 0.0),
        axis: Vec3::new(0.0, 0.0, 1.0),
        radius: 2.0,
    };
    let SelectionGeometry::Circle { radius, .. } = circle.transformed(&world_to_local).unwrap()
    else {
        panic!("circle transformed into a different kind");
    };
    assert_close(radius, 2.0, "rigid transform keeps radius");

    // …scale by the factor of a uniform-scale matrix…
    let double = AffineTransform::new([
        2.0, 0.0, 0.0, 0.0, //
        0.0, 2.0, 0.0, 0.0, //
        0.0, 0.0, 2.0, 0.0, //
        0.0, 0.0, 0.0, 1.0,
    ])
    .unwrap();
    let SelectionGeometry::Circle { radius, .. } = circle.transformed(&double).unwrap() else {
        panic!("circle transformed into a different kind");
    };
    assert_close(radius, 4.0, "uniform scale scales radius");

    // …and a non-uniform scale is refused.
    let squash = AffineTransform::new([
        1.0, 0.0, 0.0, 0.0, //
        0.0, 2.0, 0.0, 0.0, //
        0.0, 0.0, 1.0, 0.0, //
        0.0, 0.0, 0.0, 1.0,
    ])
    .unwrap();
    let error = circle.transformed(&squash).unwrap_err();
    assert!(matches!(error, ResolveError::Unsupported { .. }));
    assert_eq!(error.status(), "unsupported-selection");
}

#[test]
fn mate_marshaling_helpers() {
    let plane = SelectionGeometry::Plane {
        origin: Vec3::new(1.0, 2.0, 3.0),
        normal: Vec3::new(0.0, 0.0, 1.0),
    };
    let mate_plane = plane.mate_plane().unwrap();
    assert_eq!(mate_plane.origin, [1.0, 2.0, 3.0]);
    assert_eq!(mate_plane.normal, [0.0, 0.0, 1.0]);
    assert!(plane.mate_axis().is_none());

    let circle = SelectionGeometry::Circle {
        center: Vec3::new(4.0, 5.0, 6.0),
        axis: Vec3::new(0.0, 1.0, 0.0),
        radius: 2.0,
    };
    let mate_axis = circle.mate_axis().unwrap();
    assert_eq!(mate_axis.origin, [4.0, 5.0, 6.0]);
    assert_eq!(mate_axis.direction, [0.0, 1.0, 0.0]);
    assert!(circle.mate_plane().is_none());

    let line = SelectionGeometry::Line {
        origin: Vec3::new(7.0, 8.0, 9.0),
        direction: Vec3::new(1.0, 0.0, 0.0),
    };
    assert!(line.mate_axis().is_some());

    let sphere = SelectionGeometry::Sphere {
        center: Vec3::new(1.0, 1.0, 1.0),
        radius: 3.0,
    };
    assert!(sphere.mate_axis().is_none());
    assert!(sphere.mate_plane().is_none());
    assert_vec3(
        sphere.representative_point(),
        Vec3::new(1.0, 1.0, 1.0),
        "sphere anchor",
    );
}