brepkit-operations 3.4.19

CAD modeling operations (booleans, fillets, extrusions) for brepkit
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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use brepkit_math::mat::Mat4;
use brepkit_math::tolerance::Tolerance;
use brepkit_topology::Topology;
use brepkit_topology::edge::EdgeId;
use brepkit_topology::face::FaceSurface;
use brepkit_topology::test_utils::make_unit_cube_non_manifold;

use super::*;

#[test]
fn translate_cube() {
    let mut topo = Topology::new();
    let solid = make_unit_cube_non_manifold(&mut topo);
    let matrix = Mat4::translation(1.0, 0.0, 0.0);

    transform_solid(&mut topo, solid, &matrix).unwrap();

    // All vertices should have x shifted by 1.0.
    let tol = Tolerance::new();
    for (_id, v) in topo.vertices().iter() {
        let x = v.point().x();
        assert!(
            tol.approx_eq(x, 1.0) || tol.approx_eq(x, 2.0),
            "unexpected x = {x}"
        );
    }
}

#[test]
fn identity_transform_no_change() {
    let mut topo = Topology::new();
    let solid = make_unit_cube_non_manifold(&mut topo);

    let before: Vec<_> = topo.vertices().iter().map(|(_, v)| v.point()).collect();

    transform_solid(&mut topo, solid, &Mat4::identity()).unwrap();

    let tol = Tolerance::new();
    for (i, (_, v)) in topo.vertices().iter().enumerate() {
        assert!(tol.approx_eq(v.point().x(), before[i].x()));
        assert!(tol.approx_eq(v.point().y(), before[i].y()));
        assert!(tol.approx_eq(v.point().z(), before[i].z()));
    }
}

#[test]
fn degenerate_matrix_error() {
    let mut topo = Topology::new();
    let solid = make_unit_cube_non_manifold(&mut topo);
    let matrix = Mat4::scale(0.0, 1.0, 1.0);

    let result = transform_solid(&mut topo, solid, &matrix);
    assert!(result.is_err());
}

/// Rotating a cube 90 degrees around the Z axis should update face normals.
#[test]
fn rotation_updates_face_normals() {
    let mut topo = Topology::new();
    let solid = make_unit_cube_non_manifold(&mut topo);

    // 90-degree rotation around Z: +X face normal → +Y, -X → -Y, etc.
    let matrix = Mat4::rotation_z(std::f64::consts::FRAC_PI_2);
    transform_solid(&mut topo, solid, &matrix).unwrap();

    let tol = Tolerance::loose();
    let solid_data = topo.solid(solid).unwrap();
    let shell = topo.shell(solid_data.outer_shell()).unwrap();

    // Collect all plane normals.
    let mut normals: Vec<Vec3> = Vec::new();
    for &fid in shell.faces() {
        let f = topo.face(fid).unwrap();
        if let FaceSurface::Plane { normal, .. } = f.surface() {
            normals.push(*normal);
        }
    }

    // Original cube had normals along ±X, ±Y, ±Z.
    // After 90° Z-rotation: ±X → ±Y, ±Y → ∓X, ±Z unchanged.
    // So we should still have 6 normals, each approximately axis-aligned.
    assert_eq!(normals.len(), 6);

    // Check that we still have a +Z and -Z normal (unchanged by Z rotation).
    let has_pos_z = normals
        .iter()
        .any(|n| tol.approx_eq(n.z(), 1.0) && tol.approx_eq(n.x(), 0.0));
    let has_neg_z = normals
        .iter()
        .any(|n| tol.approx_eq(n.z(), -1.0) && tol.approx_eq(n.x(), 0.0));
    assert!(has_pos_z, "should have +Z normal after Z rotation");
    assert!(has_neg_z, "should have -Z normal after Z rotation");
}

/// Build a minimal solid containing a single face with the given surface.
///
/// The wire is a unit square in XY; only the face surface type varies.
fn make_single_face_solid(
    topo: &mut Topology,
    surface: FaceSurface,
) -> brepkit_topology::solid::SolidId {
    use brepkit_math::vec::Point3;
    use brepkit_topology::edge::{Edge, EdgeCurve};
    use brepkit_topology::face::Face;
    use brepkit_topology::shell::Shell;
    use brepkit_topology::solid::Solid;
    use brepkit_topology::vertex::Vertex;
    use brepkit_topology::wire::{OrientedEdge, Wire};

    let tol = 1e-7;
    let v0 = topo.add_vertex(Vertex::new(Point3::new(0.0, 0.0, 0.0), tol));
    let v1 = topo.add_vertex(Vertex::new(Point3::new(1.0, 0.0, 0.0), tol));
    let v2 = topo.add_vertex(Vertex::new(Point3::new(1.0, 1.0, 0.0), tol));
    let v3 = topo.add_vertex(Vertex::new(Point3::new(0.0, 1.0, 0.0), tol));

    let e0 = topo.add_edge(Edge::new(v0, v1, EdgeCurve::Line));
    let e1 = topo.add_edge(Edge::new(v1, v2, EdgeCurve::Line));
    let e2 = topo.add_edge(Edge::new(v2, v3, EdgeCurve::Line));
    let e3 = topo.add_edge(Edge::new(v3, v0, EdgeCurve::Line));

    let wire = Wire::new(
        vec![
            OrientedEdge::new(e0, true),
            OrientedEdge::new(e1, true),
            OrientedEdge::new(e2, true),
            OrientedEdge::new(e3, true),
        ],
        true,
    )
    .unwrap();
    let wid = topo.add_wire(wire);
    let fid = topo.add_face(Face::new(wid, vec![], surface));
    let shell = Shell::new(vec![fid]).unwrap();
    let shell_id = topo.add_shell(shell);
    topo.add_solid(Solid::new(shell_id, vec![]))
}

#[test]
fn translate_cylinder_face_updates_origin() {
    use brepkit_math::surfaces::CylindricalSurface;
    use brepkit_math::vec::Point3;

    let mut topo = Topology::new();
    let cyl =
        CylindricalSurface::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 2.0).unwrap();
    let solid = make_single_face_solid(&mut topo, FaceSurface::Cylinder(cyl));

    let matrix = Mat4::translation(5.0, 3.0, 1.0);
    transform_solid(&mut topo, solid, &matrix).unwrap();

    // Find the (now-transformed) cylinder face.
    let tol = Tolerance::new();
    let solid_data = topo.solid(solid).unwrap();
    let shell = topo.shell(solid_data.outer_shell()).unwrap();
    let mut found = false;
    for &fid in shell.faces() {
        if let FaceSurface::Cylinder(c) = topo.face(fid).unwrap().surface() {
            assert!(
                tol.approx_eq(c.origin().x(), 5.0),
                "cylinder origin x should be 5.0, got {}",
                c.origin().x()
            );
            assert!(
                tol.approx_eq(c.origin().y(), 3.0),
                "cylinder origin y should be 3.0, got {}",
                c.origin().y()
            );
            assert!(
                tol.approx_eq(c.origin().z(), 1.0),
                "cylinder origin z should be 1.0, got {}",
                c.origin().z()
            );
            // The axis (0,0,1) should be unchanged by a pure translation.
            assert!(
                tol.approx_eq(c.axis().z(), 1.0),
                "cylinder axis z should still be 1.0"
            );
            assert!(
                tol.approx_eq(c.radius(), 2.0),
                "cylinder radius should be unchanged"
            );
            found = true;
        }
    }
    assert!(found, "cylinder face not found after transform");
}

#[test]
fn rotate_cylinder_face_updates_axis() {
    use brepkit_math::surfaces::CylindricalSurface;
    use brepkit_math::vec::Point3;

    let mut topo = Topology::new();
    // Cylinder with axis along +Z.
    let cyl =
        CylindricalSurface::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 1.0).unwrap();
    let solid = make_single_face_solid(&mut topo, FaceSurface::Cylinder(cyl));

    // 90° rotation around Y: Z-axis → X-axis
    let matrix = Mat4::rotation_y(std::f64::consts::FRAC_PI_2);
    transform_solid(&mut topo, solid, &matrix).unwrap();

    let tol = Tolerance::loose();
    let solid_data = topo.solid(solid).unwrap();
    let shell = topo.shell(solid_data.outer_shell()).unwrap();
    let mut found = false;
    for &fid in shell.faces() {
        if let FaceSurface::Cylinder(c) = topo.face(fid).unwrap().surface() {
            // After 90° Y rotation, original Z-axis should point along +X.
            assert!(
                tol.approx_eq(c.axis().x().abs(), 1.0),
                "cylinder axis should be along X after Y rotation, got {:?}",
                c.axis()
            );
            found = true;
        }
    }
    assert!(found, "cylinder face not found after rotation");
}

#[test]
fn translate_cone_face_updates_apex() {
    use brepkit_math::surfaces::ConicalSurface;
    use brepkit_math::vec::Point3;

    let mut topo = Topology::new();
    let cone = ConicalSurface::new(
        Point3::new(0.0, 0.0, 0.0),
        Vec3::new(0.0, 0.0, 1.0),
        std::f64::consts::FRAC_PI_4,
    )
    .unwrap();
    let solid = make_single_face_solid(&mut topo, FaceSurface::Cone(cone));

    let matrix = Mat4::translation(2.0, 4.0, 6.0);
    transform_solid(&mut topo, solid, &matrix).unwrap();

    let tol = Tolerance::new();
    let solid_data = topo.solid(solid).unwrap();
    let shell = topo.shell(solid_data.outer_shell()).unwrap();
    let mut found = false;
    for &fid in shell.faces() {
        if let FaceSurface::Cone(c) = topo.face(fid).unwrap().surface() {
            assert!(
                tol.approx_eq(c.apex().x(), 2.0),
                "cone apex x should be 2.0, got {}",
                c.apex().x()
            );
            assert!(
                tol.approx_eq(c.apex().y(), 4.0),
                "cone apex y should be 4.0"
            );
            assert!(
                tol.approx_eq(c.apex().z(), 6.0),
                "cone apex z should be 6.0"
            );
            // Axis should be unchanged by a translation.
            assert!(
                tol.approx_eq(c.axis().z(), 1.0),
                "cone axis z should still be 1.0"
            );
            found = true;
        }
    }
    assert!(found, "cone face not found after transform");
}

#[test]
fn translate_sphere_face_updates_center() {
    use brepkit_math::surfaces::SphericalSurface;
    use brepkit_math::vec::Point3;

    let mut topo = Topology::new();
    let sphere = SphericalSurface::new(Point3::new(0.0, 0.0, 0.0), 3.0).unwrap();
    let solid = make_single_face_solid(&mut topo, FaceSurface::Sphere(sphere));

    let matrix = Mat4::translation(-1.0, 2.0, 5.0);
    transform_solid(&mut topo, solid, &matrix).unwrap();

    let tol = Tolerance::new();
    let solid_data = topo.solid(solid).unwrap();
    let shell = topo.shell(solid_data.outer_shell()).unwrap();
    let mut found = false;
    for &fid in shell.faces() {
        if let FaceSurface::Sphere(s) = topo.face(fid).unwrap().surface() {
            assert!(
                tol.approx_eq(s.center().x(), -1.0),
                "sphere center x should be -1.0"
            );
            assert!(
                tol.approx_eq(s.center().y(), 2.0),
                "sphere center y should be 2.0"
            );
            assert!(
                tol.approx_eq(s.center().z(), 5.0),
                "sphere center z should be 5.0"
            );
            assert!(
                tol.approx_eq(s.radius(), 3.0),
                "sphere radius should be unchanged"
            );
            found = true;
        }
    }
    assert!(found, "sphere face not found after transform");
}

#[test]
fn translate_torus_face_updates_center() {
    use brepkit_math::surfaces::ToroidalSurface;
    use brepkit_math::vec::Point3;

    let mut topo = Topology::new();
    let torus = ToroidalSurface::new(Point3::new(0.0, 0.0, 0.0), 5.0, 1.5).unwrap();
    let solid = make_single_face_solid(&mut topo, FaceSurface::Torus(torus));

    let matrix = Mat4::translation(10.0, -3.0, 0.5);
    transform_solid(&mut topo, solid, &matrix).unwrap();

    let tol = Tolerance::new();
    let solid_data = topo.solid(solid).unwrap();
    let shell = topo.shell(solid_data.outer_shell()).unwrap();
    let mut found = false;
    for &fid in shell.faces() {
        if let FaceSurface::Torus(t) = topo.face(fid).unwrap().surface() {
            assert!(
                tol.approx_eq(t.center().x(), 10.0),
                "torus center x should be 10.0"
            );
            assert!(
                tol.approx_eq(t.center().y(), -3.0),
                "torus center y should be -3.0"
            );
            assert!(
                tol.approx_eq(t.center().z(), 0.5),
                "torus center z should be 0.5"
            );
            assert!(
                tol.approx_eq(t.major_radius(), 5.0),
                "torus major radius should be unchanged"
            );
            assert!(
                tol.approx_eq(t.minor_radius(), 1.5),
                "torus minor radius should be unchanged"
            );
            found = true;
        }
    }
    assert!(found, "torus face not found after transform");
}

/// Revolving a face produces NURBS surfaces; translating the result
/// should move both vertices and NURBS control points.
#[test]
fn transform_nurbs_solid() {
    use brepkit_math::vec::Point3;
    use brepkit_topology::edge::{Edge, EdgeCurve};
    use brepkit_topology::face::Face;
    use brepkit_topology::vertex::Vertex;
    use brepkit_topology::wire::{OrientedEdge, Wire};

    let mut topo = Topology::new();

    // Build a NURBS-faced solid by lofting two offset squares: a smooth loft
    // produces genuine NURBS side surfaces (revolve of a polygonal profile is now
    // recognised as analytic cone/cylinder/plane bands, so it no longer yields
    // NURBS walls — see the revolve analytic-surface recognition).
    let square = |topo: &mut Topology, half: f64, z: f64| -> FaceId {
        let tol_val = 1e-10;
        let a = topo.add_vertex(Vertex::new(Point3::new(-half, -half, z), tol_val));
        let b = topo.add_vertex(Vertex::new(Point3::new(half, -half, z), tol_val));
        let c = topo.add_vertex(Vertex::new(Point3::new(half, half, z), tol_val));
        let d = topo.add_vertex(Vertex::new(Point3::new(-half, half, z), tol_val));
        let e0 = topo.add_edge(Edge::new(a, b, EdgeCurve::Line));
        let e1 = topo.add_edge(Edge::new(b, c, EdgeCurve::Line));
        let e2 = topo.add_edge(Edge::new(c, d, EdgeCurve::Line));
        let e3 = topo.add_edge(Edge::new(d, a, EdgeCurve::Line));
        let wire = Wire::new(
            vec![
                OrientedEdge::new(e0, true),
                OrientedEdge::new(e1, true),
                OrientedEdge::new(e2, true),
                OrientedEdge::new(e3, true),
            ],
            true,
        )
        .unwrap();
        let wid = topo.add_wire(wire);
        topo.add_face(Face::new(
            wid,
            vec![],
            FaceSurface::Plane {
                normal: brepkit_math::vec::Vec3::new(0.0, 0.0, 1.0),
                d: z,
            },
        ))
    };
    let p0 = square(&mut topo, 3.0, 0.0);
    let p1 = square(&mut topo, 2.0, 2.0);
    let p2 = square(&mut topo, 3.0, 4.0);
    let solid = crate::loft::loft_smooth(&mut topo, &[p0, p1, p2]).unwrap();

    // Record a NURBS surface control point before the transform.
    let solid_data = topo.solid(solid).unwrap();
    let shell = topo.shell(solid_data.outer_shell()).unwrap();
    let mut original_nurbs_cp = None;
    for &fid in shell.faces() {
        let f = topo.face(fid).unwrap();
        if let FaceSurface::Nurbs(s) = f.surface() {
            original_nurbs_cp = Some(s.control_points()[0][0]);
            break;
        }
    }
    let original_cp = original_nurbs_cp.unwrap();

    // Translate by (10, 0, 0).
    let matrix = Mat4::translation(10.0, 0.0, 0.0);
    transform_solid(&mut topo, solid, &matrix).unwrap();

    // Verify NURBS control points have shifted.
    let tol = Tolerance::new();
    let solid_data = topo.solid(solid).unwrap();
    let shell = topo.shell(solid_data.outer_shell()).unwrap();
    let mut found = false;
    for &fid in shell.faces() {
        let f = topo.face(fid).unwrap();
        if let FaceSurface::Nurbs(s) = f.surface() {
            let cp = s.control_points()[0][0];
            assert!(
                tol.approx_eq(cp.x(), original_cp.x() + 10.0),
                "NURBS control point x should shift by 10, got {} (was {})",
                cp.x(),
                original_cp.x()
            );
            assert!(
                tol.approx_eq(cp.y(), original_cp.y()),
                "NURBS control point y should be unchanged"
            );
            assert!(
                tol.approx_eq(cp.z(), original_cp.z()),
                "NURBS control point z should be unchanged"
            );
            found = true;
            break;
        }
    }
    assert!(found, "should still have NURBS faces after transform");
}

#[test]
fn translate_wire() {
    use brepkit_math::vec::Point3;
    use brepkit_topology::builder::make_polygon_wire;

    let mut topo = Topology::new();
    let wire = make_polygon_wire(
        &mut topo,
        &[
            Point3::new(0.0, 0.0, 0.0),
            Point3::new(1.0, 0.0, 0.0),
            Point3::new(1.0, 1.0, 0.0),
        ],
        1e-7,
    )
    .unwrap();

    transform_wire(&mut topo, wire, &Mat4::translation(5.0, 0.0, 0.0)).unwrap();

    // All vertices should have x shifted by 5 (original x values were 0, 1, 1).
    let tol = Tolerance::new();
    let w = topo.wire(wire).unwrap();
    for oe in w.edges() {
        let edge = topo.edge(oe.edge()).unwrap();
        let x = topo.vertex(edge.start()).unwrap().point().x();
        assert!(
            tol.approx_eq(x, 5.0) || tol.approx_eq(x, 6.0),
            "vertex x should be 5.0 or 6.0 after translation, got {x}"
        );
    }
}

#[test]
fn degenerate_matrix_errors_for_wire() {
    use brepkit_math::vec::Point3;
    use brepkit_topology::builder::make_polygon_wire;

    let mut topo = Topology::new();
    let wire = make_polygon_wire(
        &mut topo,
        &[
            Point3::new(0.0, 0.0, 0.0),
            Point3::new(1.0, 0.0, 0.0),
            Point3::new(1.0, 1.0, 0.0),
        ],
        1e-7,
    )
    .unwrap();

    let result = transform_wire(&mut topo, wire, &Mat4::scale(0.0, 1.0, 1.0));
    assert!(result.is_err());
}

#[test]
fn translate_wire_with_circle_edge() {
    use brepkit_math::curves::Circle3D;
    use brepkit_math::vec::{Point3, Vec3};
    use brepkit_topology::edge::{Edge, EdgeCurve};
    use brepkit_topology::vertex::Vertex;
    use brepkit_topology::wire::{OrientedEdge, Wire};

    let mut topo = Topology::new();
    let v = topo.add_vertex(Vertex::new(Point3::new(1.0, 0.0, 0.0), 1e-7));
    let circle = Circle3D::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 1.0).unwrap();
    let edge = topo.add_edge(Edge::new(v, v, EdgeCurve::Circle(circle)));
    let wire = Wire::new(vec![OrientedEdge::new(edge, true)], true).unwrap();
    let wid = topo.add_wire(wire);

    transform_wire(&mut topo, wid, &Mat4::translation(5.0, 0.0, 0.0)).unwrap();

    // Vertex should be shifted.
    let tol = Tolerance::new();
    let pos = topo.vertex(v).unwrap().point();
    assert!(
        tol.approx_eq(pos.x(), 6.0),
        "vertex should be at x=6 after +5 translation, got {}",
        pos.x()
    );

    // Circle center should also be shifted.
    let w = topo.wire(wid).unwrap();
    let e = topo.edge(w.edges()[0].edge()).unwrap();
    assert!(
        matches!(e.curve(), EdgeCurve::Circle(_)),
        "expected Circle edge after transform"
    );
    if let EdgeCurve::Circle(c) = e.curve() {
        assert!(
            tol.approx_eq(c.center().x(), 5.0),
            "circle center should be at x=5, got {}",
            c.center().x()
        );
    }
}

/// A box whose faces and edges are all exact NURBS.
fn nurbs_box(topo: &mut Topology) -> SolidId {
    let solid = crate::primitives::make_box(topo, 2.0, 3.0, 4.0).unwrap();
    crate::heal::convert_to_bspline(topo, solid).unwrap();
    solid
}

/// Worst distance from any vertex or edge sample of `solid` to the surface
/// of each face it bounds.
fn worst_boundary_offset(topo: &Topology, solid: SolidId) -> f64 {
    let mut worst: f64 = 0.0;
    for fid in brepkit_topology::explorer::solid_faces(topo, solid).unwrap() {
        let face = topo.face(fid).unwrap();
        let on_surface = |p: brepkit_math::vec::Point3| -> f64 {
            match face.surface() {
                FaceSurface::Plane { normal, d } => {
                    (normal.dot(p - brepkit_math::vec::Point3::new(0.0, 0.0, 0.0)) - d).abs()
                }
                surface => {
                    let (u, v) = surface.project_point(p).unwrap();
                    (surface.evaluate(u, v).unwrap() - p).length()
                }
            }
        };
        let wires = std::iter::once(face.outer_wire()).chain(face.inner_wires().iter().copied());
        for wid in wires {
            for oe in topo.wire(wid).unwrap().edges() {
                let edge = topo.edge(oe.edge()).unwrap();
                let p0 = topo.vertex(edge.start()).unwrap().point();
                let p1 = topo.vertex(edge.end()).unwrap().point();
                let (t0, t1) = edge.curve().domain_with_endpoints(p0, p1);
                for k in 0..=8 {
                    let t = t0 + (t1 - t0) * f64::from(k) / 8.0;
                    worst = worst.max(on_surface(edge.curve().evaluate_with_endpoints(t, p0, p1)));
                }
            }
        }
    }
    worst
}

/// Signed area vector of a face's outer wire, traversed in wire order.
fn outer_wire_area_vector(topo: &Topology, fid: FaceId) -> Vec3 {
    let face = topo.face(fid).unwrap();
    let wire = topo.wire(face.outer_wire()).unwrap();
    let pts: Vec<brepkit_math::vec::Point3> = wire
        .edges()
        .iter()
        .map(|oe| {
            let edge = topo.edge(oe.edge()).unwrap();
            let v = if oe.is_forward() {
                edge.start()
            } else {
                edge.end()
            };
            topo.vertex(v).unwrap().point()
        })
        .collect();
    let origin = pts[0];
    let mut area = Vec3::new(0.0, 0.0, 0.0);
    for w in pts.windows(2).skip(1) {
        area += (w[0] - origin).cross(w[1] - origin) * 0.5;
    }
    area
}

fn mesh_is_watertight_with_volume(topo: &Topology, solid: SolidId) -> f64 {
    let mesh = crate::tessellate::tessellate_solid(topo, solid, 0.01).unwrap();
    assert_eq!(
        crate::tessellate::boundary_edge_count(&mesh),
        0,
        "mesh must be watertight"
    );
    crate::measure::oriented_solid_volume(topo, solid, 0.01).unwrap()
}

/// A rigid motion must move every curved surface WITH its frame: a torus or
/// sphere rebuilt around the world z axis leaves its boundary edges off the
/// surface. The pose also spins each primitive about its own axis, which
/// puts a rim sample on the cylinder's u seam, where the band mesher must
/// still pair the two rims in phase.
#[test]
fn rigid_motion_keeps_curved_surfaces_on_their_boundaries() {
    use crate::primitives::{make_cone, make_cylinder, make_sphere, make_torus};

    type Make = fn(&mut Topology) -> SolidId;
    let pose = Mat4::translation(1.0, 2.0, 3.0)
        * Mat4::rotation_x(0.9)
        * Mat4::rotation_y(-0.4)
        * Mat4::rotation_z(1.3);
    let shapes: [(&str, Make); 4] = [
        ("cylinder", |t| make_cylinder(t, 1.5, 4.0).unwrap()),
        ("cone", |t| make_cone(t, 2.0, 1.0, 3.0).unwrap()),
        ("sphere", |t| make_sphere(t, 2.0, 16).unwrap()),
        ("torus", |t| make_torus(t, 6.0, 1.5, 16).unwrap()),
    ];
    for (name, make) in shapes {
        let mut topo = Topology::new();
        let solid = make(&mut topo);
        let before = mesh_is_watertight_with_volume(&topo, solid);
        // `make_sphere` bounds its hemispheres with a chordal equator, so the
        // sphere starts off its own boundary by the sagitta.
        let offset_before = worst_boundary_offset(&topo, solid);
        transform_solid(&mut topo, solid, &pose).unwrap();
        let offset = worst_boundary_offset(&topo, solid);
        assert!(
            (offset - offset_before).abs() < 1e-9,
            "{name}: boundary moved {offset_before} -> {offset} off its surfaces"
        );
        let after = mesh_is_watertight_with_volume(&topo, solid);
        assert!(
            (after - before).abs() < 1e-9 * before,
            "{name}: mesh volume {before} became {after}"
        );
        assert!(
            crate::validate::validate_solid(&topo, solid)
                .unwrap()
                .is_valid()
        );
    }
}

/// A mirror keeps every outer wire counter-clockwise around its face's
/// outward normal. Without the wire reversal the mirrored solid validates
/// clean but winds every face backwards, which later booleans read as
/// same-direction shared edges.
#[test]
fn mirror_keeps_outer_wires_counter_clockwise() {
    let mut topo = Topology::new();
    let solid = crate::primitives::make_box(&mut topo, 2.0, 3.0, 4.0).unwrap();
    let mirrored = crate::mirror::mirror(
        &mut topo,
        solid,
        brepkit_math::vec::Point3::new(0.0, 0.0, 0.0),
        Vec3::new(1.0, 0.0, 0.0),
    )
    .unwrap();
    for fid in brepkit_topology::explorer::solid_faces(&topo, mirrored).unwrap() {
        let face = topo.face(fid).unwrap();
        let FaceSurface::Plane { normal, .. } = face.surface() else {
            panic!("box faces are planar");
        };
        let outward = if face.is_reversed() {
            -*normal
        } else {
            *normal
        };
        assert!(
            outer_wire_area_vector(&topo, fid).dot(outward) > 0.0,
            "face {fid:?} winds clockwise around its outward normal"
        );
    }

    // Fuse the mirror image back onto an overlapping box: the shared walls
    // must meet with opposite edge senses.
    let other = crate::primitives::make_box(&mut topo, 2.0, 2.0, 2.0).unwrap();
    transform_solid(&mut topo, other, &Mat4::translation(-1.0, 1.0, 1.0)).unwrap();
    let fused =
        crate::boolean::boolean(&mut topo, crate::boolean::BooleanOp::Fuse, mirrored, other)
            .unwrap();
    let report = crate::validate::validate_solid(&topo, fused).unwrap();
    assert!(
        report.is_valid(),
        "fuse of a mirrored box: {:?}",
        report.issues
    );
}

/// A NURBS face's normal is the cross product of its partials, which a
/// mirror reverses; the face flag must flip so the solid stays outward.
#[test]
fn mirror_keeps_nurbs_faces_outward() {
    let mut topo = Topology::new();
    let solid = nurbs_box(&mut topo);
    let mirrored = crate::mirror::mirror(
        &mut topo,
        solid,
        brepkit_math::vec::Point3::new(5.0, 0.0, 0.0),
        Vec3::new(1.0, 1.0, 0.0),
    )
    .unwrap();
    let volume = mesh_is_watertight_with_volume(&topo, mirrored);
    assert!(
        (volume - 24.0).abs() < 1e-9,
        "mirrored NURBS box volume {volume}"
    );
    assert!(
        crate::validate::validate_solid(&topo, mirrored)
            .unwrap()
            .is_valid()
    );
}

/// Stretching a cylinder along its axis keeps it a cylinder; stretching it
/// across the axis makes an elliptic wall, which only NURBS can carry.
#[test]
fn non_uniform_scale_keeps_cylinders_exact() {
    use crate::primitives::make_cylinder;
    let exact = std::f64::consts::PI * 1.5 * 1.5 * 4.0;

    let mut topo = Topology::new();
    let along = make_cylinder(&mut topo, 1.5, 4.0).unwrap();
    transform_solid(&mut topo, along, &Mat4::scale(1.0, 1.0, 3.0)).unwrap();
    let wall = brepkit_topology::explorer::solid_faces(&topo, along)
        .unwrap()
        .into_iter()
        .find_map(|f| match topo.face(f).unwrap().surface() {
            FaceSurface::Cylinder(c) => Some(c.radius()),
            _ => None,
        })
        .expect("an axial stretch keeps the cylinder analytic");
    assert!((wall - 1.5).abs() < 1e-12);
    let v = crate::measure::solid_volume(&topo, along, 0.001).unwrap();
    assert!(
        (v - 3.0 * exact).abs() < 1e-3 * exact,
        "axial stretch volume {v}"
    );

    let across = make_cylinder(&mut topo, 1.5, 4.0).unwrap();
    let squash = Mat4::rotation_z(0.3) * Mat4::scale(2.0, 1.0, 1.0);
    transform_solid(&mut topo, across, &squash).unwrap();
    assert!(worst_boundary_offset(&topo, across) < 1e-9);
    assert!(
        brepkit_topology::explorer::solid_faces(&topo, across)
            .unwrap()
            .iter()
            .all(|&f| !matches!(topo.face(f).unwrap().surface(), FaceSurface::Cylinder(_))),
        "an elliptic wall cannot stay a circular cylinder"
    );
    // The wall's seam lies a quarter turn from the rims' parametric origin
    // (an ellipse starts at its major vertex); the mesh must still close.
    mesh_is_watertight_with_volume(&topo, across);
    let v = crate::measure::solid_volume(&topo, across, 0.01).unwrap();
    assert!(
        (v - 2.0 * exact).abs() < 5e-3 * exact,
        "elliptic cylinder volume {v}"
    );
}

/// The image of a circle under a non-uniform scale is an ellipse whose axes
/// are the image's principal axes, not the images of the circle's own axes.
#[test]
fn non_uniform_scale_of_a_tilted_circle_is_its_exact_ellipse() {
    use brepkit_math::curves::Circle3D;
    use brepkit_math::vec::Point3;
    use brepkit_topology::edge::{Edge, EdgeCurve};
    use brepkit_topology::vertex::Vertex;
    use brepkit_topology::wire::Wire;

    let mut topo = Topology::new();
    let circle = Circle3D::new(
        Point3::new(1.0, -2.0, 0.5),
        Vec3::new(0.3, 0.5, 0.8).normalize().unwrap(),
        2.0,
    )
    .unwrap();
    let start = circle.evaluate(0.0);
    let v = topo.add_vertex(Vertex::new(start, 1e-7));
    let e = topo.add_edge(Edge::new(v, v, EdgeCurve::Circle(circle.clone())));
    let w = topo.add_wire(Wire::new(vec![OrientedEdge::new(e, true)], true).unwrap());

    let m = Mat4::scale(2.0, 1.0, 0.5);
    transform_wire(&mut topo, w, &m).unwrap();
    let EdgeCurve::Ellipse(ellipse) = topo.edge(e).unwrap().curve().clone() else {
        panic!("a squashed circle is an ellipse");
    };
    assert!(ellipse.u_axis().dot(ellipse.v_axis()).abs() < 1e-12);
    for k in 0..32 {
        let p = m.mul_point(circle.evaluate(f64::from(k) * std::f64::consts::TAU / 32.0));
        let d = p - ellipse.center();
        let (x, y) = (d.dot(ellipse.u_axis()), d.dot(ellipse.v_axis()));
        let implicit = (x / ellipse.semi_major()).powi(2) + (y / ellipse.semi_minor()).powi(2);
        assert!(
            (implicit - 1.0).abs() < 1e-12,
            "sample {k} off the ellipse: {implicit}"
        );
        assert!(
            d.dot(ellipse.normal()).abs() < 1e-12,
            "sample {k} off the ellipse plane"
        );
    }
}

/// Stored pcurves stay only where the face's parameterization is carried
/// over: always for NURBS, never for a plane (it has no stored frame).
#[test]
fn transform_drops_pcurves_that_no_longer_fit_their_surface() {
    use brepkit_math::curves2d::{Curve2D, Line2D};
    use brepkit_math::vec::{Point2, Vec2};
    use brepkit_topology::pcurve::PCurve;

    let mut topo = Topology::new();
    let planar = crate::primitives::make_box(&mut topo, 1.0, 1.0, 1.0).unwrap();
    let curved = nurbs_box(&mut topo);
    let first_face = |s: SolidId| brepkit_topology::explorer::solid_faces(&topo, s).unwrap()[0];
    let (plane, nurbs) = (first_face(planar), first_face(curved));
    let edge_of = |f: FaceId| {
        topo.wire(topo.face(f).unwrap().outer_wire())
            .unwrap()
            .edges()[0]
            .edge()
    };
    let (plane_edge, nurbs_edge) = (edge_of(plane), edge_of(nurbs));
    let line = || {
        PCurve::new(
            Curve2D::Line(Line2D::new(Point2::new(0.0, 0.0), Vec2::new(1.0, 0.0)).unwrap()),
            0.0,
            1.0,
        )
    };
    topo.pcurves_mut().set(plane_edge, plane, line());
    topo.pcurves_mut().set(nurbs_edge, nurbs, line());

    transform_solid(&mut topo, planar, &Mat4::rotation_x(0.4)).unwrap();
    transform_solid(&mut topo, curved, &Mat4::rotation_x(0.4)).unwrap();
    assert!(!topo.pcurves().contains(plane_edge, plane));
    assert!(topo.pcurves().contains(nurbs_edge, nurbs));
}

/// A rigid motion keeps every circle's parameter origin on the image of the
/// old one: the image axes' lengths differ only by rounding, and that must
/// not trade the circle's axes for a quarter-turn-rotated pair.
#[test]
fn rigid_motion_keeps_circle_parameter_origins() {
    use brepkit_topology::edge::EdgeCurve;

    let poses = [
        Mat4::rotation_x(0.3) * Mat4::rotation_y(0.1),
        Mat4::rotation_z(2.2) * Mat4::rotation_x(-1.1),
        Mat4::translation(4.0, -1.0, 2.0) * Mat4::rotation_y(0.77) * Mat4::rotation_z(0.31),
    ];
    for pose in poses {
        let mut topo = Topology::new();
        let solid = crate::primitives::make_cylinder(&mut topo, 1.5, 4.0).unwrap();
        let circles = |topo: &Topology| -> Vec<(EdgeId, brepkit_math::vec::Point3)> {
            brepkit_topology::explorer::solid_edges(topo, solid)
                .unwrap()
                .into_iter()
                .filter_map(|e| match topo.edge(e).unwrap().curve() {
                    EdgeCurve::Circle(c) => Some((e, c.evaluate(0.0))),
                    _ => None,
                })
                .collect()
        };
        let before = circles(&topo);
        assert!(!before.is_empty());
        transform_solid(&mut topo, solid, &pose).unwrap();
        for ((_, origin), (_, moved)) in before.iter().zip(circles(&topo)) {
            assert!(
                (pose.mul_point(*origin) - moved).length() < 1e-12,
                "a circle's origin moved off its image"
            );
        }
    }
}

/// A NURBS face keeps its parameterization under any map, but a closed
/// circle on it re-expressed on the principal axes of its image starts
/// somewhere new; that edge's pcurve must go.
#[test]
fn closed_circle_whose_origin_moves_loses_its_pcurve() {
    use brepkit_math::curves::Circle3D;
    use brepkit_math::curves2d::{Circle2D, Curve2D};
    use brepkit_math::nurbs::surface::NurbsSurface;
    use brepkit_math::vec::{Point2, Point3};
    use brepkit_topology::edge::{Edge, EdgeCurve};
    use brepkit_topology::face::Face;
    use brepkit_topology::pcurve::PCurve;
    use brepkit_topology::vertex::Vertex;
    use brepkit_topology::wire::Wire;

    let build = |topo: &mut Topology| {
        let patch = NurbsSurface::new(
            1,
            1,
            vec![0.0, 0.0, 1.0, 1.0],
            vec![0.0, 0.0, 1.0, 1.0],
            vec![
                vec![Point3::new(-2.0, -2.0, 0.0), Point3::new(-2.0, 2.0, 0.0)],
                vec![Point3::new(2.0, -2.0, 0.0), Point3::new(2.0, 2.0, 0.0)],
            ],
            vec![vec![1.0, 1.0], vec![1.0, 1.0]],
        )
        .unwrap();
        let circle =
            Circle3D::new(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 1.0).unwrap();
        let v = topo.add_vertex(Vertex::new(circle.evaluate(0.0), 1e-7));
        let e = topo.add_edge(Edge::new(v, v, EdgeCurve::Circle(circle)));
        let w = topo.add_wire(Wire::new(vec![OrientedEdge::new(e, true)], true).unwrap());
        let f = topo.add_face(Face::new(w, vec![], FaceSurface::Nurbs(patch)));
        let pcurve = PCurve::new(
            Curve2D::Circle(Circle2D::new(Point2::new(0.5, 0.5), 0.25).unwrap()),
            0.0,
            std::f64::consts::TAU,
        );
        topo.pcurves_mut().set(e, f, pcurve);
        (e, f)
    };

    let mut topo = Topology::new();
    let (edge, face) = build(&mut topo);
    transform_face(&mut topo, face, &Mat4::scale(2.0, 2.0, 2.0)).unwrap();
    assert!(topo.pcurves().contains(edge, face), "a similarity keeps it");

    let (edge, face) = build(&mut topo);
    transform_face(&mut topo, face, &Mat4::scale(2.0, 1.0, 1.0)).unwrap();
    assert!(
        !topo.pcurves().contains(edge, face),
        "the squashed circle starts at its major vertex now"
    );
}

/// A non-uniform scale turns a sphere into an ellipsoid, which only NURBS can
/// carry; the patch must be the exact image and face outward.
#[test]
fn non_uniform_scale_makes_a_sphere_an_exact_ellipsoid() {
    let mut topo = Topology::new();
    let solid = crate::primitives::make_sphere(&mut topo, 2.0, 16).unwrap();
    transform_solid(&mut topo, solid, &Mat4::scale(2.0, 1.0, 0.5)).unwrap();
    let mut patches = 0;
    for fid in brepkit_topology::explorer::solid_faces(&topo, solid).unwrap() {
        let face = topo.face(fid).unwrap();
        let FaceSurface::Nurbs(n) = face.surface().clone() else {
            continue;
        };
        patches += 1;
        let ((u0, u1), (v0, v1)) = (n.domain_u(), n.domain_v());
        for i in 0..=8 {
            for j in 1..8 {
                let (u, v) = (
                    u0 + (u1 - u0) * f64::from(i) / 8.0,
                    v0 + (v1 - v0) * f64::from(j) / 8.0,
                );
                let p = n.evaluate(u, v);
                let implicit = (p.x() / 4.0).powi(2) + (p.y() / 2.0).powi(2) + p.z().powi(2);
                assert!(
                    (implicit - 1.0).abs() < 1e-9,
                    "off the ellipsoid: {implicit}"
                );
                let normal = brepkit_math::traits::ParametricSurface::normal(&n, u, v);
                let normal = if face.is_reversed() { -normal } else { normal };
                let outward = p - brepkit_math::vec::Point3::new(0.0, 0.0, 0.0);
                assert!(normal.dot(outward) > 0.0, "ellipsoid face points inward");
            }
        }
    }
    assert!(patches > 0, "the ellipsoid faces are NURBS");
}

/// A primitive sphere's hemispheres are bounded by the equator alone, with no
/// pole vertex; each must still get the patch of its own half.
#[test]
fn ellipsoid_hemispheres_get_their_own_halves() {
    let mut topo = Topology::new();
    let solid = crate::primitives::make_sphere(&mut topo, 1.0, 16).unwrap();
    transform_solid(&mut topo, solid, &Mat4::scale(1.0, 2.0, 3.0)).unwrap();
    let mut sides = Vec::new();
    for fid in brepkit_topology::explorer::solid_faces(&topo, solid).unwrap() {
        let FaceSurface::Nurbs(n) = topo.face(fid).unwrap().surface().clone() else {
            continue;
        };
        let ((u0, u1), (v0, v1)) = (n.domain_u(), n.domain_v());
        let z: Vec<f64> = (0..=8)
            .flat_map(|i| (1..8).map(move |j| (i, j)))
            .map(|(i, j)| {
                n.evaluate(
                    u0 + (u1 - u0) * f64::from(i) / 8.0,
                    v0 + (v1 - v0) * f64::from(j) / 8.0,
                )
                .z()
            })
            .collect();
        assert!(
            z.iter().all(|&z| z > -1e-9) || z.iter().all(|&z| z < 1e-9),
            "a hemisphere patch straddles the equator"
        );
        sides.push(z.iter().sum::<f64>() > 0.0);
    }
    sides.sort_unstable();
    assert_eq!(sides, vec![false, true], "one north and one south patch");
}

/// The fused copy-and-transform maps every surface, curve and wire exactly
/// as copying and then transforming does.
#[test]
fn fused_copy_transform_matches_copy_then_transform() {
    use crate::primitives::{make_cone, make_cylinder, make_sphere, make_torus};

    type Make = fn(&mut Topology) -> SolidId;
    let shapes: [Make; 4] = [
        |t| make_cylinder(t, 1.5, 4.0).unwrap(),
        |t| make_cone(t, 2.0, 1.0, 3.0).unwrap(),
        |t| make_sphere(t, 2.0, 16).unwrap(),
        |t| make_torus(t, 6.0, 1.5, 16).unwrap(),
    ];
    // The squashing pose turns spheres, tori and cones into NURBS faces whose
    // meshing is a separate gap; it is compared by geometry alone.
    let squash = Mat4::rotation_z(0.3) * Mat4::scale(2.0, 1.0, 1.0);
    let poses = [
        Mat4::translation(1.0, 2.0, 3.0) * Mat4::rotation_x(0.9) * Mat4::rotation_y(-0.4),
        Mat4::rotation_x(0.7) * Mat4::scale(1.0, -1.0, 1.0),
        squash,
    ];
    for make in shapes {
        for pose in &poses {
            let mut topo = Topology::new();
            let source = make(&mut topo);
            let fused = crate::copy::copy_and_transform_solid(&mut topo, source, pose).unwrap();
            let staged = crate::copy::copy_solid(&mut topo, source).unwrap();
            transform_solid(&mut topo, staged, pose).unwrap();
            let (a, b) = (
                crate::measure::solid_bounding_box(&topo, fused).unwrap(),
                crate::measure::solid_bounding_box(&topo, staged).unwrap(),
            );
            assert!((a.min - b.min).length() < 1e-9 && (a.max - b.max).length() < 1e-9);
            assert!(
                (worst_boundary_offset(&topo, fused) - worst_boundary_offset(&topo, staged)).abs()
                    < 1e-9
            );
            if *pose == squash {
                continue;
            }
            let (va, vb) = (
                mesh_is_watertight_with_volume(&topo, fused),
                mesh_is_watertight_with_volume(&topo, staged),
            );
            assert!((va - vb).abs() < 1e-9 * vb.abs(), "{va} vs {vb}");
            assert!(va > 0.0);
            assert!(
                crate::validate::validate_solid(&topo, fused)
                    .unwrap()
                    .is_valid()
            );
        }
    }
}

/// A cylinder face whose vertices do not reach the extremes of its edges (a
/// tilted rim, closed at one seam vertex) still sits on its NURBS image
/// after a non-uniform scale.
#[test]
fn cylinder_nurbs_image_covers_its_whole_boundary() {
    let mut topo = Topology::new();
    let tube = crate::primitives::make_cylinder(&mut topo, 1.5, 4.0).unwrap();
    let slab = crate::primitives::make_box(&mut topo, 10.0, 10.0, 10.0).unwrap();
    transform_solid(
        &mut topo,
        slab,
        &(Mat4::translation(0.0, 0.0, 3.0)
            * Mat4::rotation_x(0.4)
            * Mat4::translation(-5.0, -5.0, 0.0)),
    )
    .unwrap();
    let cut =
        crate::boolean::boolean(&mut topo, crate::boolean::BooleanOp::Cut, tube, slab).unwrap();
    transform_solid(&mut topo, cut, &Mat4::scale(2.0, 1.0, 1.0)).unwrap();
    let offset = worst_boundary_offset(&topo, cut);
    assert!(offset < 1e-6, "boundary {offset} off its surfaces");
}