cadkit-core 0.5.2

CAD document model and operations, bounded byte readers, versioned JSON and SVG export for cadkit.
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
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
//! Geometry helpers: vector math, affine [`Transform`], circular/elliptic arcs, polyline
//! bulges and NURBS evaluation.
//!
//! All angles are radians. Nothing here panics; degenerate input yields `None` or an
//! identity-like result.

use std::f64::consts::{PI, TAU};

use crate::geom::{BBox, Point3, Vec3};

// ---------------------------------------------------------------------------------------------
// Vector helpers
// ---------------------------------------------------------------------------------------------

impl Vec3 {
    /// Dot product.
    pub fn dot(self, o: Vec3) -> f64 {
        self.x * o.x + self.y * o.y + self.z * o.z
    }

    /// Cross product `self x o`.
    pub fn cross(self, o: Vec3) -> Vec3 {
        Vec3::new(
            self.y * o.z - self.z * o.y,
            self.z * o.x - self.x * o.z,
            self.x * o.y - self.y * o.x,
        )
    }

    /// Euclidean length.
    pub fn length(self) -> f64 {
        self.dot(self).sqrt()
    }

    /// Unit vector, or `None` when the length is zero or not finite.
    pub fn normalized(self) -> Option<Vec3> {
        let l = self.length();
        if l.is_finite() && l > 1e-300 {
            Some(self.scaled(1.0 / l))
        } else {
            None
        }
    }

    /// Multiplies every component by `k`.
    pub fn scaled(self, k: f64) -> Vec3 {
        Vec3::new(self.x * k, self.y * k, self.z * k)
    }

    /// Component-wise sum.
    pub fn plus(self, o: Vec3) -> Vec3 {
        Vec3::new(self.x + o.x, self.y + o.y, self.z + o.z)
    }

    /// Component-wise difference.
    pub fn minus(self, o: Vec3) -> Vec3 {
        Vec3::new(self.x - o.x, self.y - o.y, self.z - o.z)
    }

    /// True when all components are finite.
    pub fn is_finite(self) -> bool {
        self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
    }
}

impl Point3 {
    /// Vector from `self` to `other`.
    pub fn vector_to(self, other: Point3) -> Vec3 {
        Vec3::new(other.x - self.x, other.y - self.y, other.z - self.z)
    }

    /// Euclidean distance between `self` and `other`.
    pub fn distance_to(self, other: Point3) -> f64 {
        self.vector_to(other).length()
    }

    /// Linearly interpolates between `self` (`t = 0`) and `other` (`t = 1`).
    pub fn lerp(self, other: Point3, t: f64) -> Point3 {
        let s = 1.0 - t;
        Point3::new(
            self.x * s + other.x * t,
            self.y * s + other.y * t,
            self.z * s + other.z * t,
        )
    }

    /// Midpoint halfway between `self` and `other`.
    pub fn midpoint(self, other: Point3) -> Point3 {
        self.lerp(other, 0.5)
    }

    /// The point moved by `v`.
    pub fn translated(self, v: Vec3) -> Point3 {
        Point3::new(self.x + v.x, self.y + v.y, self.z + v.z)
    }

    /// True when all coordinates are finite.
    pub fn is_finite(self) -> bool {
        self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
    }
}

impl BBox {
    /// A degenerate box containing exactly `p`.
    pub fn from_point(p: Point3) -> Self {
        Self { min: p, max: p }
    }

    /// Smallest box containing all finite points in `points`, or `None` when empty.
    pub fn from_points(points: impl IntoIterator<Item = Point3>) -> Option<Self> {
        let mut bb = None;
        for p in points {
            extend_bbox(&mut bb, p);
        }
        bb
    }

    /// Grows the box to contain `p`. Non-finite points are ignored.
    pub fn extend(&mut self, p: Point3) {
        if !p.is_finite() {
            return;
        }
        self.min = Point3::new(
            self.min.x.min(p.x),
            self.min.y.min(p.y),
            self.min.z.min(p.z),
        );
        self.max = Point3::new(
            self.max.x.max(p.x),
            self.max.y.max(p.y),
            self.max.z.max(p.z),
        );
    }

    /// Grows the box to contain `other`.
    pub fn union(&mut self, other: &BBox) {
        self.extend(other.min);
        self.extend(other.max);
    }

    /// Returns a new box containing both `self` and `other`.
    pub fn merged(&self, other: &BBox) -> BBox {
        let mut out = *self;
        out.union(other);
        out
    }

    /// Extent along X.
    pub fn width(&self) -> f64 {
        self.max.x - self.min.x
    }

    /// Extent along Y.
    pub fn height(&self) -> f64 {
        self.max.y - self.min.y
    }

    /// Extent along Z.
    pub fn depth(&self) -> f64 {
        self.max.z - self.min.z
    }

    /// Extents `(width, height, depth)` as a [`Vec3`].
    pub fn size(&self) -> Vec3 {
        Vec3::new(self.width(), self.height(), self.depth())
    }

    /// Geometric center of the bounding box.
    pub fn center(&self) -> Point3 {
        self.min.midpoint(self.max)
    }

    /// True when the box lies on `z = 0` within `1e-9`.
    pub fn is_2d(&self) -> bool {
        self.min.z.abs() <= 1e-9 && self.max.z.abs() <= 1e-9
    }

    /// True when `p` lies inside or on the boundary of `self`.
    pub fn contains(&self, p: Point3) -> bool {
        p.is_finite()
            && p.x >= self.min.x
            && p.x <= self.max.x
            && p.y >= self.min.y
            && p.y <= self.max.y
            && p.z >= self.min.z
            && p.z <= self.max.z
    }

    /// True when `self` and `other` overlap or touch on every axis.
    pub fn intersects(&self, other: &BBox) -> bool {
        self.min.x <= other.max.x
            && self.max.x >= other.min.x
            && self.min.y <= other.max.y
            && self.max.y >= other.min.y
            && self.min.z <= other.max.z
            && self.max.z >= other.min.z
    }

    /// Returns the box padded uniformly by `margin` along each axis.
    pub fn expanded(&self, margin: f64) -> BBox {
        if !margin.is_finite() {
            return *self;
        }
        let d = Vec3::new(margin, margin, margin);
        let a = self.min - d;
        let b = self.max + d;
        BBox {
            min: Point3::new(a.x.min(b.x), a.y.min(b.y), a.z.min(b.z)),
            max: Point3::new(a.x.max(b.x), a.y.max(b.y), a.z.max(b.z)),
        }
    }
}

/// Adds `p` to an optional box, creating it on first use. Non-finite points are ignored.
pub fn extend_bbox(bb: &mut Option<BBox>, p: Point3) {
    if !p.is_finite() {
        return;
    }
    match bb {
        Some(b) => b.extend(p),
        None => *bb = Some(BBox::from_point(p)),
    }
}

// ---------------------------------------------------------------------------------------------
// Transform
// ---------------------------------------------------------------------------------------------

/// DXF arbitrary-axis algorithm: the plane axes `(Ax, Ay, Az)` for an entity `normal`
/// (Ax is the zero-angle direction of the plane, Az the normal).
///
/// If both |Nx| and |Ny| are below 1/64 the world Y axis is crossed with N, otherwise the
/// world Z axis; `Ay = N x Ax`. A zero or non-finite normal yields the world axes.
pub fn arbitrary_axes(normal: Vec3) -> (Vec3, Vec3, Vec3) {
    let Some(n) = normal.normalized() else {
        return (Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 1.0, 0.0), Vec3::Z);
    };
    let wz = Vec3::Z;
    let wy = Vec3::new(0.0, 1.0, 0.0);
    let ax = if n.x.abs() < 1.0 / 64.0 && n.y.abs() < 1.0 / 64.0 {
        wy.cross(n)
    } else {
        wz.cross(n)
    };
    let ax = ax.normalized().unwrap_or(Vec3::new(1.0, 0.0, 0.0));
    let ay = n.cross(ax).normalized().unwrap_or(Vec3::new(0.0, 1.0, 0.0));
    (ax, ay, n)
}

/// A 3x4 affine transform: `p' = x*p.x + y*p.y + z*p.z + t`.
///
/// `x`, `y`, `z` are the images of the unit axes (the matrix columns) and `t` is the
/// translation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Transform {
    /// Image of the X axis.
    pub x: Vec3,
    /// Image of the Y axis.
    pub y: Vec3,
    /// Image of the Z axis.
    pub z: Vec3,
    /// Translation.
    pub t: Vec3,
}

impl Default for Transform {
    fn default() -> Self {
        Self::identity()
    }
}

impl Transform {
    /// The identity transform.
    pub fn identity() -> Self {
        Self {
            x: Vec3::new(1.0, 0.0, 0.0),
            y: Vec3::new(0.0, 1.0, 0.0),
            z: Vec3::new(0.0, 0.0, 1.0),
            t: Vec3::new(0.0, 0.0, 0.0),
        }
    }

    /// Pure translation.
    pub fn translation(v: Vec3) -> Self {
        Self {
            t: v,
            ..Self::identity()
        }
    }

    /// Axis-aligned scaling about the origin.
    pub fn scaling(sx: f64, sy: f64, sz: f64) -> Self {
        Self {
            x: Vec3::new(sx, 0.0, 0.0),
            y: Vec3::new(0.0, sy, 0.0),
            z: Vec3::new(0.0, 0.0, sz),
            t: Vec3::new(0.0, 0.0, 0.0),
        }
    }

    /// Uniform scaling about the origin along all three axes.
    pub fn uniform_scale(factor: f64) -> Self {
        Self::scaling(factor, factor, factor)
    }

    /// Constructs a transform from a row-major 3x4 affine matrix
    /// `[[xx, yx, zx, tx], [xy, yy, zy, ty], [xz, yz, zz, tz]]`.
    pub const fn from_matrix(rows: [[f64; 4]; 3]) -> Self {
        let [r0, r1, r2] = rows;
        let [xx, yx, zx, tx] = r0;
        let [xy, yy, zy, ty] = r1;
        let [xz, yz, zz, tz] = r2;
        Self {
            x: Vec3::new(xx, xy, xz),
            y: Vec3::new(yx, yy, yz),
            z: Vec3::new(zx, zy, zz),
            t: Vec3::new(tx, ty, tz),
        }
    }

    /// Exports the transform as a row-major 3x4 affine matrix.
    pub const fn to_matrix(&self) -> [[f64; 4]; 3] {
        [
            [self.x.x, self.y.x, self.z.x, self.t.x],
            [self.x.y, self.y.y, self.z.y, self.t.y],
            [self.x.z, self.y.z, self.z.z, self.t.z],
        ]
    }

    /// Rotation about the Z axis, counter-clockwise by `angle` radians.
    pub fn rotation_z(angle: f64) -> Self {
        let (s, c) = angle.sin_cos();
        Self {
            x: Vec3::new(c, s, 0.0),
            y: Vec3::new(-s, c, 0.0),
            z: Vec3::new(0.0, 0.0, 1.0),
            t: Vec3::new(0.0, 0.0, 0.0),
        }
    }

    /// Transform whose columns are the given axes, with `origin` as translation.
    pub fn from_axes(x: Vec3, y: Vec3, z: Vec3, origin: Vec3) -> Self {
        Self { x, y, z, t: origin }
    }

    /// Maps plane-local coordinates to world coordinates: local `(x, y)` lie in the plane
    /// through `origin` spanned by [`arbitrary_axes`]`(normal)`, local `z` runs along the normal.
    pub fn plane(origin: Point3, normal: Vec3) -> Self {
        let (ax, ay, az) = arbitrary_axes(normal);
        Self::from_axes(ax, ay, az, Vec3::new(origin.x, origin.y, origin.z))
    }

    /// Applies the linear part only (directions, extents).
    pub fn apply_vector(&self, v: Vec3) -> Vec3 {
        self.x
            .scaled(v.x)
            .plus(self.y.scaled(v.y))
            .plus(self.z.scaled(v.z))
    }

    /// Applies the full transform to a point.
    pub fn apply_point(&self, p: Point3) -> Point3 {
        let v = self.apply_vector(Vec3::new(p.x, p.y, p.z));
        Point3::new(v.x + self.t.x, v.y + self.t.y, v.z + self.t.z)
    }

    /// Composition: the result applies `inner` first, then `self`
    /// (`self.compose(inner).apply_point(p) == self.apply_point(inner.apply_point(p))`).
    pub fn compose(&self, inner: &Transform) -> Transform {
        let t = self.apply_point(Point3::new(inner.t.x, inner.t.y, inner.t.z));
        Transform {
            x: self.apply_vector(inner.x),
            y: self.apply_vector(inner.y),
            z: self.apply_vector(inner.z),
            t: Vec3::new(t.x, t.y, t.z),
        }
    }

    /// Determinant of the linear part; negative when the transform mirrors.
    pub fn determinant(&self) -> f64 {
        self.x.dot(self.y.cross(self.z))
    }

    /// Transform of a block reference: block coordinates to the coordinates of the space
    /// holding the insert (WCS).
    ///
    /// `p_world = position + Plane(normal) * Rz(rotation) * (offset + scale * (p - base_point))`,
    /// where `Plane(normal)` is the linear map of [`Transform::plane`]. `position` is a world
    /// point; `offset` is the array offset of one cell in the rotated, unscaled insert frame.
    pub fn from_insert_offset(
        position: Point3,
        scale: Vec3,
        rotation: f64,
        normal: Vec3,
        base_point: Point3,
        offset: Vec3,
    ) -> Transform {
        let minus_base =
            Transform::translation(Vec3::new(-base_point.x, -base_point.y, -base_point.z));
        let s = Transform::scaling(scale.x, scale.y, scale.z);
        let off = Transform::translation(offset);
        let r = Transform::rotation_z(rotation);
        Transform::plane(position, normal)
            .compose(&r)
            .compose(&off)
            .compose(&s)
            .compose(&minus_base)
    }

    /// [`Transform::from_insert_offset`] without an array offset.
    pub fn from_insert(
        position: Point3,
        scale: Vec3,
        rotation: f64,
        normal: Vec3,
        base_point: Point3,
    ) -> Transform {
        Self::from_insert_offset(
            position,
            scale,
            rotation,
            normal,
            base_point,
            Vec3::new(0.0, 0.0, 0.0),
        )
    }
}

// ---------------------------------------------------------------------------------------------
// Arcs and ellipses
// ---------------------------------------------------------------------------------------------

/// A (possibly partial) ellipse in parametric form
/// `P(t) = center + u*cos(t) + v*sin(t)`, travelled from `t0` to `t1`.
///
/// `u` and `v` are conjugate semi-diameters (perpendicular for a true ellipse, but any pair
/// is valid, which keeps the form closed under affine transforms). `t1 < t0` means the
/// curve is travelled clockwise.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EllipseArc {
    /// Center.
    pub center: Point3,
    /// First semi-diameter (point at `t = 0` is `center + u`).
    pub u: Vec3,
    /// Second semi-diameter (point at `t = pi/2` is `center + v`).
    pub v: Vec3,
    /// Start parameter in radians.
    pub t0: f64,
    /// End parameter in radians.
    pub t1: f64,
}

/// Counter-clockwise sweep from `start` to `end`, in (0, 2*pi]. Equal angles mean a full turn.
pub fn ccw_sweep(start: f64, end: f64) -> f64 {
    let d = (end - start).rem_euclid(TAU);
    if d < 1e-12 { TAU } else { d }
}

impl EllipseArc {
    /// Circular arc in the XY plane of the local frame: center `c`, radius `r`, CCW from
    /// `start` to `end` (equal angles give a full circle).
    pub fn circle(c: Point3, r: f64, start: f64, end: f64) -> Self {
        Self {
            center: c,
            u: Vec3::new(r, 0.0, 0.0),
            v: Vec3::new(0.0, r, 0.0),
            t0: start,
            t1: start + ccw_sweep(start, end),
        }
    }

    /// Point at parameter `t`.
    pub fn point(&self, t: f64) -> Point3 {
        let (s, c) = t.sin_cos();
        Point3::new(
            self.center.x + self.u.x * c + self.v.x * s,
            self.center.y + self.u.y * c + self.v.y * s,
            self.center.z + self.u.z * c + self.v.z * s,
        )
    }

    /// Signed parameter sweep `t1 - t0`.
    pub fn sweep(&self) -> f64 {
        self.t1 - self.t0
    }

    /// The arc after applying an affine transform (parameters are preserved).
    pub fn transformed(&self, tf: &Transform) -> Self {
        Self {
            center: tf.apply_point(self.center),
            u: tf.apply_vector(self.u),
            v: tf.apply_vector(self.v),
            t0: self.t0,
            t1: self.t1,
        }
    }

    /// Exact bounding box of the traveled curve, merged into `bb`.
    pub fn extend_bbox(&self, bb: &mut Option<BBox>) {
        extend_bbox(bb, self.point(self.t0));
        extend_bbox(bb, self.point(self.t1));
        let (lo, hi) = if self.t0 <= self.t1 {
            (self.t0, self.t1)
        } else {
            (self.t1, self.t0)
        };
        let hi = hi.min(lo + TAU);
        let axes = [
            (self.u.x, self.v.x),
            (self.u.y, self.v.y),
            (self.u.z, self.v.z),
        ];
        for (a, b) in axes {
            if !(a.is_finite() && b.is_finite()) {
                continue;
            }
            // d/dt (a cos t + b sin t) = 0 at t = atan2(b, a) + k*pi.
            let te = b.atan2(a);
            let mut k = ((lo - te) / PI).ceil();
            let mut guard = 0;
            while guard < 4 {
                let t = te + k * PI;
                if t > hi {
                    break;
                }
                extend_bbox(bb, self.point(t));
                k += 1.0;
                guard += 1;
            }
        }
    }
}

/// Principal axes of the ellipse spanned by conjugate semi-diameters `u`, `v` (2D).
/// Returns `(semi_major, semi_minor, rotation_of_major_axis)`.
pub fn conjugate_to_axes(u: (f64, f64), v: (f64, f64)) -> (f64, f64, f64) {
    let uu = u.0 * u.0 + u.1 * u.1;
    let vv = v.0 * v.0 + v.1 * v.1;
    let uv = u.0 * v.0 + u.1 * v.1;
    // |P(t)|^2 is extremal where tan(2t) = 2 u.v / (u.u - v.v).
    let t0 = 0.5 * (2.0 * uv).atan2(uu - vv);
    let (s, c) = t0.sin_cos();
    let a = (u.0 * c + v.0 * s, u.1 * c + v.1 * s);
    let b = (-u.0 * s + v.0 * c, -u.1 * s + v.1 * c);
    let ra = a.0.hypot(a.1);
    let rb = b.0.hypot(b.1);
    if ra >= rb {
        (ra, rb, a.1.atan2(a.0))
    } else {
        (rb, ra, b.1.atan2(b.0))
    }
}

/// Ellipse (or elliptical arc) from DXF-style data: `major_axis` is the vector from the
/// center to the end of the major axis, `ratio` = minor / major, parameters in radians.
/// The minor axis direction is `normal x major_axis`.
pub fn ellipse_arc(
    center: Point3,
    major_axis: Vec3,
    ratio: f64,
    normal: Vec3,
    start: f64,
    end: f64,
) -> EllipseArc {
    let n = normal.normalized().unwrap_or(Vec3::Z);
    let minor_dir = n
        .cross(major_axis)
        .scaled(if ratio.is_finite() { ratio } else { 1.0 });
    EllipseArc {
        center,
        u: major_axis,
        v: minor_dir,
        t0: start,
        t1: start + ccw_sweep(start, end),
    }
}

/// Point of an ellipse at parameter `t` (see [`ellipse_arc`] for the conventions).
pub fn ellipse_point(center: Point3, major_axis: Vec3, ratio: f64, normal: Vec3, t: f64) -> Point3 {
    ellipse_arc(center, major_axis, ratio, normal, 0.0, TAU).point(t)
}

/// A circular arc recovered from a polyline segment with a bulge.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BulgeArc {
    /// Center in the plane of the segment.
    pub center: (f64, f64),
    /// Radius.
    pub radius: f64,
    /// Angle of the segment start as seen from the center.
    pub start_angle: f64,
    /// Signed sweep: positive counter-clockwise, `4 * atan(bulge)`.
    pub sweep: f64,
}

impl BulgeArc {
    /// Angle of the segment end (`start_angle + sweep`).
    pub fn end_angle(&self) -> f64 {
        self.start_angle + self.sweep
    }

    /// True when the arc runs counter-clockwise.
    pub fn is_ccw(&self) -> bool {
        self.sweep > 0.0
    }
}

/// Converts the segment `p0 -> p1` with the given `bulge` (tan of a quarter of the included
/// angle, positive = counter-clockwise) into a circular arc. `None` for a straight segment
/// (|bulge| ~ 0), coincident end points or non-finite input.
pub fn bulge_to_arc(p0: (f64, f64), p1: (f64, f64), bulge: f64) -> Option<BulgeArc> {
    if !bulge.is_finite() || bulge.abs() < 1e-12 {
        return None;
    }
    let (dx, dy) = (p1.0 - p0.0, p1.1 - p0.1);
    let d = dx.hypot(dy);
    if !d.is_finite() || d < 1e-300 {
        return None;
    }
    let radius = d * (1.0 + bulge * bulge) / (4.0 * bulge.abs());
    // Distance from the chord midpoint to the center, signed to the left of p0 -> p1:
    // (d/2) / tan(theta/2) with tan(2*atan(b)) = 2b / (1 - b^2).
    let off = (d / 2.0) * (1.0 - bulge * bulge) / (2.0 * bulge);
    let mid = ((p0.0 + p1.0) / 2.0, (p0.1 + p1.1) / 2.0);
    let center = (mid.0 - dy / d * off, mid.1 + dx / d * off);
    let start_angle = (p0.1 - center.1).atan2(p0.0 - center.0);
    Some(BulgeArc {
        center,
        radius,
        start_angle,
        sweep: 4.0 * bulge.atan(),
    })
}

// ---------------------------------------------------------------------------------------------
// NURBS
// ---------------------------------------------------------------------------------------------

/// Clamped uniform knot vector for `n` control points of the given degree
/// (`n + degree + 1` knots, domain `[0, n - degree]`).
pub fn clamped_uniform_knots(n: usize, degree: usize) -> Vec<f64> {
    let spans = n.saturating_sub(degree);
    let mut k = Vec::with_capacity(n + degree + 1);
    for i in 0..(n + degree + 1) {
        let v = if i <= degree {
            0.0
        } else if i >= n {
            spans as f64
        } else {
            (i - degree) as f64
        };
        k.push(v);
    }
    k
}

/// Highest B-spline degree that is evaluated; anything above is drawn as its control polygon.
pub const MAX_NURBS_DEGREE: usize = 25;

/// Upper bound on de Boor work (samples x degree^2) per flattened spline.
const MAX_NURBS_FLOP: usize = 5_000_000;

#[derive(Clone, Copy)]
struct H {
    x: f64,
    y: f64,
    z: f64,
    w: f64,
}

impl H {
    const ZERO: H = H {
        x: 0.0,
        y: 0.0,
        z: 0.0,
        w: 0.0,
    };

    fn lerp(a: H, b: H, alpha: f64) -> H {
        let m = 1.0 - alpha;
        H {
            x: a.x * m + b.x * alpha,
            y: a.y * m + b.y * alpha,
            z: a.z * m + b.z * alpha,
            w: a.w * m + b.w * alpha,
        }
    }
}

/// Index of the knot span containing `t`: the largest `k` in `[degree, n - 1]` with
/// `knots[k] <= t`.
fn find_span(knots: &[f64], n: usize, degree: usize, t: f64) -> Option<usize> {
    let s = knots.get(degree..n)?;
    let count = s.partition_point(|&x| x <= t);
    Some(degree + count.saturating_sub(1))
}

fn nurbs_eval_h(degree: usize, knots: &[f64], pts: &[H], t: f64) -> Option<Point3> {
    let n = pts.len();
    if degree == 0 || degree > MAX_NURBS_DEGREE || degree >= n || knots.len() != n + degree + 1 {
        return None;
    }
    let k = find_span(knots, n, degree, t)?;
    // Fixed scratch space: evaluation allocates nothing.
    let mut d = [H::ZERO; MAX_NURBS_DEGREE + 1];
    for (slot, p) in d.iter_mut().zip(pts.get(k.checked_sub(degree)?..=k)?) {
        *slot = *p;
    }
    for r in 1..=degree {
        for j in (r..=degree).rev() {
            let i = j + k - degree;
            let lo = *knots.get(i)?;
            let hi = *knots.get(i + degree + 1 - r)?;
            let den = hi - lo;
            let alpha = if den.abs() < 1e-300 {
                0.0
            } else {
                (t - lo) / den
            };
            let a = *d.get(j - 1)?;
            let b = *d.get(j)?;
            *d.get_mut(j)? = H::lerp(a, b, alpha);
        }
    }
    let r = *d.get(degree)?;
    if r.w.abs() < 1e-300 {
        return None;
    }
    Some(Point3::new(r.x / r.w, r.y / r.w, r.z / r.w))
}

fn homogeneous(control: &[Point3], weights: &[f64]) -> Vec<H> {
    let rational =
        weights.len() == control.len() && weights.iter().all(|w| w.is_finite() && *w > 0.0);
    control
        .iter()
        .enumerate()
        .map(|(i, p)| {
            let w = if rational {
                weights.get(i).copied().unwrap_or(1.0)
            } else {
                1.0
            };
            H {
                x: p.x * w,
                y: p.y * w,
                z: p.z * w,
                w,
            }
        })
        .collect()
}

/// Evaluates a (rational) B-spline at parameter `t` with de Boor's algorithm.
///
/// `weights` may be empty (non-rational). `t` is clamped to the curve domain
/// `[knots[degree], knots[n]]`. Returns `None` when the arguments are inconsistent
/// (`knots.len() != control.len() + degree + 1`, degree 0, too few points).
pub fn nurbs_eval(
    degree: usize,
    knots: &[f64],
    control: &[Point3],
    weights: &[f64],
    t: f64,
) -> Option<Point3> {
    let n = control.len();
    let lo = *knots.get(degree)?;
    let hi = *knots.get(n)?;
    let t = if hi >= lo { t.clamp(lo, hi) } else { t };
    nurbs_eval_h(degree, knots, &homogeneous(control, weights), t)
}

/// Samples a NURBS curve into a polyline.
///
/// Robust against messy input: a missing or inconsistent knot vector is replaced by a
/// clamped uniform one, invalid weights are ignored, and a degree that is too high for the
/// number of control points is lowered. With fewer than 2 control points the control points
/// are returned unchanged, and when the domain is empty the control polygon is returned.
/// `samples_per_span` points are generated per non-empty knot span (total capped at 100 000).
pub fn nurbs_flatten(
    degree: u32,
    knots: &[f64],
    control: &[Point3],
    weights: &[f64],
    samples_per_span: usize,
) -> Vec<Point3> {
    let n = control.len();
    if n < 2 {
        return control.to_vec();
    }
    let p = (degree as usize).clamp(1, n - 1);
    if p > MAX_NURBS_DEGREE {
        return control.to_vec();
    }
    let knots_ok = knots.len() == n + p + 1
        && knots.iter().all(|k| k.is_finite())
        && knots.windows(2).all(|w| w.first() <= w.get(1));
    let knots: Vec<f64> = if knots_ok {
        knots.to_vec()
    } else {
        clamped_uniform_knots(n, p)
    };
    let (Some(&lo), Some(&hi)) = (knots.get(p), knots.get(n)) else {
        return control.to_vec();
    };
    if hi <= lo {
        return control.to_vec();
    }
    let pts = homogeneous(control, weights);
    let spans: Vec<(f64, f64)> = knots
        .get(p..=n)
        .map(|s| {
            s.windows(2)
                .filter_map(|w| Some((*w.first()?, *w.get(1)?)))
                .filter(|(a, b)| b > a)
                .collect()
        })
        .unwrap_or_default();
    let max_samples = (MAX_NURBS_FLOP / (p * p)).clamp(2, 100_000);
    let per = samples_per_span.clamp(1, (max_samples / spans.len().max(1)).max(1));
    let mut out = Vec::with_capacity(spans.len() * per + 1);
    for (a, b) in &spans {
        for i in 0..per {
            let t = a + (b - a) * (i as f64) / (per as f64);
            if let Some(pt) = nurbs_eval_h(p, &knots, &pts, t) {
                out.push(pt);
            }
        }
    }
    if let Some(pt) = nurbs_eval_h(p, &knots, &pts, hi) {
        out.push(pt);
    }
    out
}

/// Interpolates `fit_points` into an explicit non-rational clamped B-spline
/// `(degree, knots, control_points)` using chord-length Bessel/Hermite cubic spans.
///
/// Every distinct input fit point lies on the resulting B-spline curve at its chord parameter.
/// Returns `None` if fewer than two distinct finite points are present.
pub fn interpolate_fit_points(
    fit_points: &[Point3],
    closed: bool,
) -> Option<(u32, Vec<f64>, Vec<Point3>)> {
    let mut pts: Vec<Point3> = Vec::with_capacity(fit_points.len() + usize::from(closed));
    for &p in fit_points {
        if !p.is_finite() {
            return None;
        }
        if pts.last().is_none_or(|prev| prev.distance_to(p) > 1e-12) {
            pts.push(p);
        }
    }
    if pts.len() < 2 {
        return None;
    }
    let is_loop = if closed {
        if let (Some(&first), Some(&last)) = (pts.first(), pts.last())
            && first.distance_to(last) > 1e-12
        {
            pts.push(first);
        }
        pts.len() >= 4
    } else {
        false
    };
    if pts.len() == 2 {
        return Some((1, vec![0.0, 0.0, 1.0, 1.0], pts));
    }

    let m = pts.len();
    let spans = m - 1;
    let mut u = Vec::with_capacity(m);
    u.push(0.0);
    for w in pts.windows(2) {
        let (Some(&a), Some(&b)) = (w.first(), w.get(1)) else {
            continue;
        };
        let prev = u.last().copied().unwrap_or(0.0);
        u.push(prev + a.distance_to(b).max(1e-12));
    }
    let total = u.last().copied().unwrap_or(1.0).max(1e-12);
    for v in &mut u {
        *v /= total;
    }

    // Chord-weighted Bessel tangents dP/du at each fit point.
    let secant = |i: usize| -> Option<(f64, Vec3)> {
        let p0 = *pts.get(i)?;
        let p1 = *pts.get(i + 1)?;
        let du = (*u.get(i + 1)? - *u.get(i)?).max(1e-12);
        Some((du, (p1 - p0).scaled(1.0 / du)))
    };

    let mut tangents = Vec::with_capacity(m);
    for i in 0..m {
        let t = if i == 0 {
            if is_loop {
                let (h_prev, d_prev) = secant(spans - 1)?;
                let (h_next, d_next) = secant(0)?;
                d_prev
                    .scaled(h_next)
                    .plus(d_next.scaled(h_prev))
                    .scaled(1.0 / (h_prev + h_next))
            } else {
                let (h0, d0) = secant(0)?;
                let (h1, d1) = secant(1)?;
                d0.scaled((2.0 * h0 + h1) / (h0 + h1))
                    .minus(d1.scaled(h0 / (h0 + h1)))
            }
        } else if i == spans {
            if is_loop {
                tangents.first().copied().unwrap_or(Vec3::ZERO)
            } else {
                let (hm2, dm2) = secant(spans - 2)?;
                let (hm1, dm1) = secant(spans - 1)?;
                dm1.scaled((2.0 * hm1 + hm2) / (hm1 + hm2))
                    .minus(dm2.scaled(hm1 / (hm1 + hm2)))
            }
        } else {
            let (h_prev, d_prev) = secant(i - 1)?;
            let (h_next, d_next) = secant(i)?;
            d_prev
                .scaled(h_next)
                .plus(d_next.scaled(h_prev))
                .scaled(1.0 / (h_prev + h_next))
        };
        tangents.push(t);
    }

    let mut control = Vec::with_capacity(3 * spans + 1);
    control.push(*pts.first()?);
    for i in 0..spans {
        let p0 = *pts.get(i)?;
        let p1 = *pts.get(i + 1)?;
        let t0 = *tangents.get(i)?;
        let t1 = *tangents.get(i + 1)?;
        let du = *u.get(i + 1)? - *u.get(i)?;
        control.push(p0 + t0.scaled(du / 3.0));
        control.push(p1 - t1.scaled(du / 3.0));
        control.push(p1);
    }

    let mut knots = Vec::with_capacity(3 * spans + 5);
    knots.extend([0.0; 4]);
    for i in 1..spans {
        if let Some(&uk) = u.get(i) {
            knots.extend([uk; 3]);
        }
    }
    knots.extend([1.0; 4]);
    Some((3, knots, control))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn close(a: f64, b: f64) -> bool {
        (a - b).abs() < 1e-9
    }
    fn pclose(a: Point3, b: Point3) -> bool {
        close(a.x, b.x) && close(a.y, b.y) && close(a.z, b.z)
    }

    #[test]
    fn vec_ops() {
        let a = Vec3::new(1.0, 0.0, 0.0);
        let b = Vec3::new(0.0, 1.0, 0.0);
        assert_eq!(a.cross(b), Vec3::new(0.0, 0.0, 1.0));
        assert_eq!(a.dot(b), 0.0);
        assert!(Vec3::new(0.0, 0.0, 0.0).normalized().is_none());
        assert!(close(Vec3::new(3.0, 4.0, 0.0).length(), 5.0));
    }

    #[test]
    fn bbox_helpers() {
        let mut bb = None;
        extend_bbox(&mut bb, Point3::new(f64::NAN, 0.0, 0.0));
        assert!(bb.is_none());
        extend_bbox(&mut bb, Point3::xy(1.0, 2.0));
        extend_bbox(&mut bb, Point3::xy(-1.0, 5.0));
        let b = bb.unwrap();
        assert!(close(b.width(), 2.0) && close(b.height(), 3.0));
    }

    #[test]
    fn arbitrary_axis_default_normal() {
        let (ax, ay, az) = arbitrary_axes(Vec3::Z);
        assert_eq!(ax, Vec3::new(1.0, 0.0, 0.0));
        assert_eq!(ay, Vec3::new(0.0, 1.0, 0.0));
        assert_eq!(az, Vec3::Z);
    }

    #[test]
    fn arbitrary_axis_flipped_normal() {
        // N = -Z: Ax = Wy x N = (-1, 0, 0); Ay = N x Ax = (0, 1, 0).
        let (ax, ay, az) = arbitrary_axes(Vec3::new(0.0, 0.0, -1.0));
        assert_eq!(ax, Vec3::new(-1.0, 0.0, 0.0));
        assert_eq!(ay, Vec3::new(0.0, 1.0, 0.0));
        assert_eq!(az, Vec3::new(0.0, 0.0, -1.0));
    }

    #[test]
    fn arbitrary_axis_oblique_normal_is_orthonormal() {
        let n = Vec3::new(1.0, 2.0, 3.0).normalized().unwrap();
        let (ax, ay, az) = arbitrary_axes(n);
        assert!(close(ax.length(), 1.0) && close(ay.length(), 1.0));
        assert!(close(ax.dot(ay), 0.0) && close(ax.dot(az), 0.0) && close(ay.dot(az), 0.0));
        // Ax = Wz x N is horizontal.
        assert!(close(ax.z, 0.0));
        // Right-handed.
        assert!(close(ax.cross(ay).dot(az), 1.0));
    }

    #[test]
    fn arbitrary_axis_near_z_uses_wy() {
        let n = Vec3::new(0.001, 0.001, 1.0).normalized().unwrap();
        let (ax, _, _) = arbitrary_axes(n);
        let expect = Vec3::new(0.0, 1.0, 0.0).cross(n).normalized().unwrap();
        assert!(close(ax.x, expect.x) && close(ax.y, expect.y) && close(ax.z, expect.z));
    }

    #[test]
    fn arbitrary_axis_zero_normal_is_identity() {
        let (ax, ay, az) = arbitrary_axes(Vec3::new(0.0, 0.0, 0.0));
        assert_eq!(
            (ax, ay, az),
            (Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 1.0, 0.0), Vec3::Z)
        );
    }

    #[test]
    fn transform_compose_order() {
        let t = Transform::translation(Vec3::new(10.0, 0.0, 0.0));
        let r = Transform::rotation_z(PI / 2.0);
        // rotate first, then translate
        let c = t.compose(&r);
        let p = c.apply_point(Point3::new(1.0, 0.0, 0.0));
        assert!(pclose(p, Point3::new(10.0, 1.0, 0.0)));
        // translate first, then rotate
        let c = r.compose(&t);
        let p = c.apply_point(Point3::new(1.0, 0.0, 0.0));
        assert!(pclose(p, Point3::new(0.0, 11.0, 0.0)));
    }

    #[test]
    fn transform_vector_ignores_translation() {
        let t = Transform::translation(Vec3::new(5.0, 5.0, 5.0));
        assert_eq!(
            t.apply_vector(Vec3::new(1.0, 2.0, 3.0)),
            Vec3::new(1.0, 2.0, 3.0)
        );
        assert_eq!(Transform::identity().determinant(), 1.0);
        assert!(Transform::scaling(-1.0, 1.0, 1.0).determinant() < 0.0);
    }

    #[test]
    fn insert_transform() {
        // base (1,1), scale 2, rotate 90, position (10,20).
        let tf = Transform::from_insert(
            Point3::new(10.0, 20.0, 0.0),
            Vec3::new(2.0, 2.0, 2.0),
            PI / 2.0,
            Vec3::Z,
            Point3::new(1.0, 1.0, 0.0),
        );
        // block point (2,1): minus base -> (1,0); scale -> (2,0); rotate -> (0,2); + pos.
        assert!(pclose(
            tf.apply_point(Point3::new(2.0, 1.0, 0.0)),
            Point3::new(10.0, 22.0, 0.0)
        ));
    }

    #[test]
    fn insert_transform_with_offset_and_flipped_normal() {
        let tf = Transform::from_insert_offset(
            Point3::new(0.0, 0.0, 0.0),
            Vec3::new(1.0, 1.0, 1.0),
            0.0,
            Vec3::new(0.0, 0.0, -1.0),
            Point3::default(),
            Vec3::new(5.0, 0.0, 0.0),
        );
        // The plane x axis is -X in the world; the position is a plain world point.
        assert!(pclose(
            tf.apply_point(Point3::new(1.0, 2.0, 0.0)),
            Point3::new(-6.0, 2.0, 0.0)
        ));
    }

    #[test]
    fn bulge_semicircle() {
        let a = bulge_to_arc((0.0, 0.0), (2.0, 0.0), 1.0).unwrap();
        assert!(close(a.radius, 1.0));
        assert!(close(a.center.0, 1.0) && close(a.center.1, 0.0));
        assert!(close(a.sweep, PI));
        assert!(a.is_ccw());
        assert!(close(a.start_angle.abs(), PI));
        // CCW from (0,0) to (2,0) passes below the chord: point at start + sweep/2 is (1,-1).
        let mid = a.start_angle + a.sweep / 2.0;
        assert!(close(a.center.0 + a.radius * mid.cos(), 1.0));
        assert!(close(a.center.1 + a.radius * mid.sin(), -1.0));
    }

    #[test]
    fn bulge_negative_and_quarter() {
        let a = bulge_to_arc((0.0, 0.0), (2.0, 0.0), -1.0).unwrap();
        assert!(!a.is_ccw());
        assert!(close(a.sweep, -PI));
        // Quarter circle: bulge = tan(pi/8), chord from (1,0) to (0,1) about the origin, CCW.
        let b = (PI / 8.0).tan();
        let a = bulge_to_arc((1.0, 0.0), (0.0, 1.0), b).unwrap();
        assert!(close(a.radius, 1.0));
        assert!(close(a.center.0, 0.0) && close(a.center.1, 0.0));
        assert!(close(a.start_angle, 0.0));
        assert!(close(a.end_angle(), PI / 2.0));
    }

    #[test]
    fn bulge_large_arc_center_other_side() {
        // bulge 2 > 1: more than a semicircle; the center lies on the bulge side of the chord.
        let a = bulge_to_arc((0.0, 0.0), (2.0, 0.0), 2.0).unwrap();
        assert!(a.sweep > PI);
        assert!(a.center.1 < 0.0);
        let end = a.end_angle();
        assert!(close(a.center.0 + a.radius * end.cos(), 2.0));
        assert!(close(a.center.1 + a.radius * end.sin(), 0.0));
    }

    #[test]
    fn bulge_degenerate() {
        assert!(bulge_to_arc((0.0, 0.0), (1.0, 0.0), 0.0).is_none());
        assert!(bulge_to_arc((1.0, 1.0), (1.0, 1.0), 1.0).is_none());
        assert!(bulge_to_arc((0.0, 0.0), (1.0, 0.0), f64::NAN).is_none());
    }

    #[test]
    fn ellipse_points() {
        let c = Point3::new(1.0, 2.0, 0.0);
        let major = Vec3::new(4.0, 0.0, 0.0);
        let p0 = ellipse_point(c, major, 0.5, Vec3::Z, 0.0);
        assert!(pclose(p0, Point3::new(5.0, 2.0, 0.0)));
        let p1 = ellipse_point(c, major, 0.5, Vec3::Z, PI / 2.0);
        assert!(pclose(p1, Point3::new(1.0, 4.0, 0.0)));
        // Rotated major axis.
        let p = ellipse_point(
            Point3::default(),
            Vec3::new(0.0, 2.0, 0.0),
            0.5,
            Vec3::Z,
            PI / 2.0,
        );
        assert!(pclose(p, Point3::new(-1.0, 0.0, 0.0)));
    }

    #[test]
    fn conjugate_axes_recovers_ellipse() {
        let (a, b, rot) = conjugate_to_axes((4.0, 0.0), (0.0, 2.0));
        assert!(close(a, 4.0) && close(b, 2.0) && close(rot, 0.0));
        // Unit circle sheared by x' = x + y: semi-axes are phi and 1/phi.
        let (a, b, _) = conjugate_to_axes((1.0, 0.0), (1.0, 1.0));
        let phi = (1.0 + 5f64.sqrt()) / 2.0;
        assert!(close(a, phi) && close(b, 1.0 / phi));
    }

    #[test]
    fn ccw_sweep_wraps() {
        assert!(close(ccw_sweep(0.0, PI / 2.0), PI / 2.0));
        assert!(close(ccw_sweep(3.0 * PI / 2.0, PI / 2.0), PI));
        assert!(close(ccw_sweep(1.0, 1.0), TAU));
    }

    #[test]
    fn arc_bbox_includes_extrema() {
        let arc = EllipseArc::circle(Point3::default(), 1.0, -0.1, 0.1);
        let mut bb = None;
        arc.extend_bbox(&mut bb);
        let bb = bb.unwrap();
        assert!(close(bb.max.x, 1.0));
        assert!(bb.min.x > 0.99);
        let full = EllipseArc::circle(Point3::new(1.0, 1.0, 0.0), 2.0, 0.0, 0.0);
        let mut bb = None;
        full.extend_bbox(&mut bb);
        let bb = bb.unwrap();
        assert!(
            close(bb.min.x, -1.0)
                && close(bb.max.x, 3.0)
                && close(bb.min.y, -1.0)
                && close(bb.max.y, 3.0)
        );
    }

    #[test]
    fn nurbs_bezier() {
        let c = [
            Point3::xy(0.0, 0.0),
            Point3::xy(1.0, 2.0),
            Point3::xy(2.0, 0.0),
        ];
        let knots = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0];
        let p = nurbs_eval(2, &knots, &c, &[], 0.5).unwrap();
        // Quadratic Bezier at 0.5: 0.25 P0 + 0.5 P1 + 0.25 P2.
        assert!(pclose(p, Point3::xy(1.0, 1.0)));
        assert!(pclose(nurbs_eval(2, &knots, &c, &[], 0.0).unwrap(), c[0]));
        assert!(pclose(nurbs_eval(2, &knots, &c, &[], 1.0).unwrap(), c[2]));
        // Out-of-domain parameter is clamped.
        assert!(pclose(nurbs_eval(2, &knots, &c, &[], 5.0).unwrap(), c[2]));
    }

    #[test]
    fn nurbs_rational_quarter_circle() {
        let w = 2f64.sqrt() / 2.0;
        let c = [
            Point3::xy(1.0, 0.0),
            Point3::xy(1.0, 1.0),
            Point3::xy(0.0, 1.0),
        ];
        let knots = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0];
        for i in 0..=10 {
            let t = f64::from(i) / 10.0;
            let p = nurbs_eval(2, &knots, &c, &[1.0, w, 1.0], t).unwrap();
            assert!(close(p.x.hypot(p.y), 1.0), "t={t} r={}", p.x.hypot(p.y));
        }
        // Midpoint is at 45 degrees.
        let p = nurbs_eval(2, &knots, &c, &[1.0, w, 1.0], 0.5).unwrap();
        assert!(close(p.x, w) && close(p.y, w));
    }

    #[test]
    fn nurbs_cubic_linear_precision() {
        // Control points at the Greville abscissae reproduce x(t) = t exactly.
        let knots = clamped_uniform_knots(5, 3);
        assert_eq!(knots, vec![0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 2.0]);
        let g = [0.0, 1.0 / 3.0, 1.0, 5.0 / 3.0, 2.0];
        let c: Vec<Point3> = g.iter().map(|&x| Point3::xy(x, x * x)).collect();
        for t in [0.0, 0.25, 0.5, 1.0, 1.7, 2.0] {
            let p = nurbs_eval(3, &knots, &c, &[], t).unwrap();
            assert!(close(p.x, t), "t={t} x={}", p.x);
        }
    }

    #[test]
    fn nurbs_degree_and_work_are_capped() {
        let c: Vec<Point3> = (0..200).map(|i| Point3::xy(f64::from(i), 0.0)).collect();
        // Degree above the cap: control polygon.
        assert_eq!(nurbs_flatten(60, &[], &c, &[], 16).len(), 200);
        assert!(nurbs_eval(60, &clamped_uniform_knots(200, 60), &c, &[], 1.0).is_none());
        // Degree at the cap: bounded number of samples.
        let pts = nurbs_flatten(25, &[], &c, &[], 100_000);
        assert!(pts.len() <= 5_000_000 / (25 * 25) + 1, "{}", pts.len());
        assert!(pts.len() > 10);
    }

    #[test]
    fn nurbs_rejects_inconsistent_input() {
        let c = [Point3::xy(0.0, 0.0), Point3::xy(1.0, 1.0)];
        assert!(nurbs_eval(2, &[0.0, 1.0], &c, &[], 0.5).is_none());
        assert!(nurbs_eval(0, &[0.0, 1.0, 2.0], &c, &[], 0.5).is_none());
    }

    #[test]
    fn flatten_endpoints_and_fallbacks() {
        let c = [
            Point3::xy(0.0, 0.0),
            Point3::xy(1.0, 2.0),
            Point3::xy(2.0, 0.0),
        ];
        let pts = nurbs_flatten(2, &[0.0, 0.0, 0.0, 1.0, 1.0, 1.0], &c, &[], 8);
        assert_eq!(pts.len(), 9);
        assert!(pclose(*pts.first().unwrap(), c[0]));
        assert!(pclose(*pts.last().unwrap(), c[2]));
        // Missing knots and bogus degree still work.
        let pts = nurbs_flatten(9, &[], &c, &[], 4);
        assert!(pts.len() >= 3);
        assert!(pclose(*pts.last().unwrap(), c[2]));
        // Single point.
        assert_eq!(nurbs_flatten(3, &[], &c[..1], &[], 4).len(), 1);
        // Bad weights are ignored.
        let pts = nurbs_flatten(2, &[], &c, &[1.0, -1.0, 1.0], 4);
        assert!(pts.iter().all(|p| p.is_finite()));
    }

    #[test]
    fn point_vec_operators_and_bbox_queries() {
        let p = Point3::from([1.0, 2.0, 3.0]);
        let v = Vec3::from((4.0, -2.0, 1.0));
        assert_eq!(p + v, Point3::new(5.0, 0.0, 4.0));
        assert_eq!((p + v) - p, v);
        assert_eq!(2.0 * v, v * 2.0);
        assert_eq!(-v, Vec3::new(-4.0, 2.0, -1.0));
        assert_eq!(
            p.midpoint(Point3::new(3.0, 4.0, 5.0)),
            Point3::new(2.0, 3.0, 4.0)
        );

        let bb = BBox::from_points([Point3::xy(0.0, 0.0), Point3::xy(10.0, 20.0)]).unwrap();
        assert!(bb.is_2d());
        assert_eq!(bb.center(), Point3::xy(5.0, 10.0));
        assert_eq!(bb.size(), Vec3::new(10.0, 20.0, 0.0));
        assert!(bb.contains(Point3::xy(5.0, 5.0)));
        assert!(!bb.contains(Point3::xy(11.0, 5.0)));
        let padded = bb.expanded(2.0);
        assert_eq!(padded.width(), 14.0);
        assert!(bb.intersects(&padded));

        let tf = Transform::from_matrix([
            [2.0, 0.0, 0.0, 5.0],
            [0.0, 3.0, 0.0, -1.0],
            [0.0, 0.0, 1.0, 7.0],
        ]);
        assert_eq!(
            tf.apply_point(Point3::new(1.0, 2.0, 3.0)),
            Point3::new(7.0, 5.0, 10.0)
        );
        assert_eq!(Transform::from_matrix(tf.to_matrix()), tf);
    }

    #[test]
    fn fit_point_interpolation_passes_through_every_fit_point() {
        let fit = [
            Point3::new(0.0, 0.0, 0.0),
            Point3::new(2.0, 3.0, 1.0),
            Point3::new(5.0, 1.0, 2.0),
            Point3::new(8.0, 4.0, 0.0),
        ];
        let (deg, knots, ctrl) = interpolate_fit_points(&fit, false).unwrap();
        assert_eq!(deg, 3);
        assert_eq!(knots.len(), ctrl.len() + 4);
        assert!(pclose(
            nurbs_eval(3, &knots, &ctrl, &[], 0.0).unwrap(),
            fit[0]
        ));
        assert!(pclose(
            nurbs_eval(3, &knots, &ctrl, &[], knots[4]).unwrap(),
            fit[1]
        ));
        assert!(pclose(
            nurbs_eval(3, &knots, &ctrl, &[], knots[7]).unwrap(),
            fit[2]
        ));
        assert!(pclose(
            nurbs_eval(3, &knots, &ctrl, &[], 1.0).unwrap(),
            fit[3]
        ));

        // Closed fit-point loop wraps smoothly back to fit[0].
        let (cdeg, cknots, cctrl) = interpolate_fit_points(&fit, true).unwrap();
        assert_eq!(cdeg, 3);
        assert!(pclose(
            nurbs_eval(3, &cknots, &cctrl, &[], 0.0).unwrap(),
            fit[0]
        ));
        assert!(pclose(
            nurbs_eval(3, &cknots, &cctrl, &[], 1.0).unwrap(),
            fit[0]
        ));
    }
}