BREP_kernel 0.29.0

Rust B-rep CAD kernel (next.BREP.io): a boundary representation (B-rep) geometry kernel for building CAD applications, native and WebAssembly.
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
//! XT geometry read into kernel terms (Parasolid XT Format Reference §5.2):
//! analytic carriers by their frames, B-curves and B-surfaces as clamped
//! NURBS, the evaluators the translator needs for the geometry STEP has no
//! word for (SP-curves on their surfaces), and each surface's signed
//! distance, which refines intersection curves.
//!
//! Units: XT is in metres (§4.2.1); everything here is read into
//! millimetres, positions and radii alike. Parameters keep XT's meaning: a
//! parameter that is a length in XT (a plane's u and v, a cylinder's or
//! cone's axial v, a line's t) is still in metres, and the evaluators scale
//! it, so an SP-curve's 2D curve maps through its surface unchanged.

use crate::{NurbsCurve, NurbsSurface, Vec3, Vec4};

use super::reader::{Node, XtFile};

/// XT lengths are metres; the kernel's are millimetres.
pub const MM_PER_METRE: f64 = 1000.0;

pub fn vec3(v: [f64; 3]) -> Vec3 {
    Vec3::new(v[0], v[1], v[2])
}

/// A position in the file, in mm.
pub fn position(v: [f64; 3]) -> Vec3 {
    vec3(v).scale(MM_PER_METRE)
}

fn required_position(node: &Node, field: &str) -> Result<Vec3, String> {
    node.vector(field)
        .map(position)
        .ok_or_else(|| format!("{} {} has no {field}", node.type_name(), node.index))
}

fn required_direction(node: &Node, field: &str) -> Result<Vec3, String> {
    node.vector(field)
        .map(vec3)
        .ok_or_else(|| format!("{} {} has no {field}", node.type_name(), node.index))?
        .normalized()
}

fn required_real(node: &Node, field: &str) -> Result<f64, String> {
    node.real(field)
        .ok_or_else(|| format!("{} {} has no {field}", node.type_name(), node.index))
}

fn required_length(node: &Node, field: &str) -> Result<f64, String> {
    required_real(node, field).map(|r| r * MM_PER_METRE)
}

/// A right-handed frame: origin (mm), z (axis or normal) and x; y = z × x.
#[derive(Clone, Copy, Debug)]
pub struct Frame {
    pub origin: Vec3,
    pub z: Vec3,
    pub x: Vec3,
}

impl Frame {
    pub fn y(&self) -> Vec3 {
        self.z.cross(self.x)
    }

    fn read(node: &Node, origin: &str, z: &str) -> Result<Self, String> {
        let origin = required_position(node, origin)?;
        let z = required_direction(node, z)?;
        let x = match node.vector("x_axis") {
            Some(x) => vec3(x),
            None => z.perpendicular()?,
        };
        // Square x against z: written frames are unit and orthogonal to the
        // file's resolution, which is all the STEP lane needs them to be.
        let x = x.sub(z.scale(x.dot(z))).normalized()?;
        Ok(Self { origin, z, x })
    }

    fn radial(&self, u: f64) -> Vec3 {
        self.x.scale(u.cos()).add(self.y().scale(u.sin()))
    }
}

/// An XT surface, as far as the translator reads it (mm).
#[derive(Clone, Debug)]
pub enum Surface {
    /// R(u,v) = P + uX + vY; natural normal = normal.
    Plane(Frame),
    /// R(u,v) = P + r(X cos u + Y sin u) + vA.
    Cylinder { frame: Frame, radius: f64 },
    /// R(u,v) = P + vA + (X cos u + Y sin u)(r + v tan a). The reference
    /// (§5.2.2.3) writes −vA and says the axis points away from the half in
    /// use; written files have the radius growing along the axis (the
    /// parameterisation above), and the natural normal points outwards.
    Cone { frame: Frame, radius: f64, sin: f64, cos: f64 },
    /// R(u,v) = C + (X cos u + Y sin u) r cos v + r A sin v.
    Sphere { frame: Frame, radius: f64 },
    /// R(u,v) = C + (X cos u + Y sin u)(a + b cos v) + b A sin v.
    Torus { frame: Frame, major: f64, minor: f64 },
    /// A B-surface, clamped.
    Nurbs(NurbsSurface),
    /// An offset of `base` by `distance` along its natural normal (times the
    /// base's sense, §5.2.2.8).
    Offset { base: Box<Surface>, base_sense: bool, distance: f64 },
    /// The profile spun right-handedly about the axis (§5.2.2.11).
    Spun { profile: Curve, axis_point: Vec3, axis: Vec3 },
    /// The section swept along a direction (§5.2.2.10).
    Swept { section: Curve, direction: Vec3 },
}

/// An XT curve, as far as the translator reads it (mm).
#[derive(Clone, Debug)]
pub enum Curve {
    Line { point: Vec3, direction: Vec3 },
    Circle { frame: Frame, radius: f64 },
    Ellipse { frame: Frame, major: f64, minor: f64 },
    /// A B-curve, clamped. A 2D B-curve (an SP-curve's) has z = 0 and is in
    /// its surface's parameters, unscaled.
    Nurbs(NurbsCurve),
}

impl Curve {
    pub fn point(&self, t: f64) -> Result<Vec3, String> {
        Ok(match self {
            Curve::Line { point, direction } => point.add(direction.scale(t * MM_PER_METRE)),
            Curve::Circle { frame, radius } => frame.origin.add(frame.radial(t).scale(*radius)),
            Curve::Ellipse { frame, major, minor } => frame
                .origin
                .add(frame.x.scale(major * t.cos()))
                .add(frame.y().scale(minor * t.sin())),
            Curve::Nurbs(curve) => curve.evaluate(t)?,
        })
    }

    /// The parameter range a whole closed curve, or a bounded one, covers.
    pub fn natural_range(&self) -> Option<[f64; 2]> {
        match self {
            Curve::Line { .. } => None,
            Curve::Circle { .. } | Curve::Ellipse { .. } => Some([0.0, std::f64::consts::TAU]),
            Curve::Nurbs(curve) => curve.domain().ok(),
        }
    }
}

impl Surface {
    /// Point (mm) at XT parameters (u, v).
    pub fn point(&self, u: f64, v: f64) -> Result<Vec3, String> {
        const M: f64 = MM_PER_METRE;
        Ok(match self {
            Surface::Plane(f) => f.origin.add(f.x.scale(u * M)).add(f.y().scale(v * M)),
            Surface::Cylinder { frame, radius } => frame.origin.add(frame.radial(u).scale(*radius)).add(frame.z.scale(v * M)),
            Surface::Cone { frame, radius, sin, cos } => frame
                .origin
                .add(frame.z.scale(v * M))
                .add(frame.radial(u).scale(radius + v * M * sin / cos)),
            Surface::Sphere { frame, radius } => frame
                .origin
                .add(frame.radial(u).scale(radius * v.cos()))
                .add(frame.z.scale(radius * v.sin())),
            Surface::Torus { frame, major, minor } => frame
                .origin
                .add(frame.radial(u).scale(major + minor * v.cos()))
                .add(frame.z.scale(minor * v.sin())),
            Surface::Nurbs(surface) => surface.evaluate(u, v)?,
            Surface::Offset { base, base_sense, distance } => {
                let sign = if *base_sense { 1.0 } else { -1.0 };
                // A B-surface base runs on past its domain along its boundary
                // tangent planes, as Parasolid extends it: an SP-curve on the
                // offset can run there (a keycap dish's apex stands 0.3 mm
                // past its base's last row, which the domain's edge stood in
                // for).
                match base.as_ref() {
                    Surface::Nurbs(surface) if outside_domain(surface, u, v) => {
                        let d = surface.derivatives_extended(u, v, 1)?;
                        d[0][0].add(d[1][0].cross(d[0][1]).normalized()?.scale(sign * distance))
                    }
                    _ => base.point(u, v)?.add(base.natural_normal(u, v)?.scale(sign * distance)),
                }
            }
            Surface::Spun { profile, axis_point, axis } => {
                let c = profile.point(u)?;
                let z = axis_point.add(axis.scale(c.sub(*axis_point).dot(*axis)));
                let r = c.sub(z);
                z.add(r.scale(v.cos())).add(axis.cross(r).scale(v.sin()))
            }
            Surface::Swept { section, direction } => section.point(u)?.add(direction.scale(v * M)),
        })
    }

    /// The unit natural normal, dP/du × dP/dv, by central differences on the
    /// exact evaluator: used only to offset and to orient, never as geometry.
    pub fn natural_normal(&self, u: f64, v: f64) -> Result<Vec3, String> {
        if let Surface::Nurbs(surface) = self {
            return surface.normal(u, v);
        }
        let h = 1e-7;
        let du = self.point(u + h, v)?.sub(self.point(u - h, v)?);
        let dv = self.point(u, v + h)?.sub(self.point(u, v - h)?);
        // Unit tangents first: on a small surface the two differences are
        // ~1e-7 mm and their product falls under the zero-length floor.
        du.normalized()?.cross(dv.normalized()?).normalized()
    }

    /// Whether the surface is a plane, cylinder, cone, sphere or torus.
    pub fn is_analytic(&self) -> bool {
        matches!(
            self,
            Surface::Plane(_) | Surface::Cylinder { .. } | Surface::Cone { .. } | Surface::Sphere { .. } | Surface::Torus { .. }
        )
    }

    /// The signed distance (mm) from `x` to the surface and its gradient (a
    /// unit vector), where the surface has one the reader can compute; the
    /// sign is arbitrary but consistent with the gradient.
    pub fn distance(&self, x: Vec3) -> Option<(f64, Vec3)> {
        let unit = |v: Vec3| v.normalized().ok();
        match self {
            Surface::Plane(f) => Some((x.sub(f.origin).dot(f.z), f.z)),
            Surface::Cylinder { frame, radius } => {
                let d = x.sub(frame.origin);
                let radial = d.sub(frame.z.scale(d.dot(frame.z)));
                let e = unit(radial)?;
                Some((radial.length() - radius, e))
            }
            Surface::Cone { frame, radius, sin, cos } => {
                // In the half-plane through the axis, the cone is the line
                // rho = r + a tan(angle); its unit normal is (cos, -sin).
                let d = x.sub(frame.origin);
                let a = d.dot(frame.z);
                let radial = d.sub(frame.z.scale(a));
                let e = unit(radial)?;
                let rho = radial.length();
                Some(((rho - radius) * cos - a * sin, e.scale(*cos).sub(frame.z.scale(*sin))))
            }
            Surface::Sphere { frame, radius } => {
                let d = x.sub(frame.origin);
                Some((d.length() - radius, unit(d)?))
            }
            Surface::Torus { frame, major, minor } => {
                let d = x.sub(frame.origin);
                let radial = d.sub(frame.z.scale(d.dot(frame.z)));
                let tube = frame.origin.add(unit(radial)?.scale(*major));
                let to = x.sub(tube);
                Some((to.length() - minor, unit(to)?))
            }
            Surface::Nurbs(surface) => {
                let foot = crate::project_point_to_surface(surface, x).ok()?;
                let normal = surface.normal(foot.u, foot.v).ok()?;
                Some((x.sub(foot.point).dot(normal), normal))
            }
            Surface::Offset { base, base_sense, distance } => {
                // The offset lies where the base's distance, measured along
                // the base's natural normal, is the offset.
                // A B-surface base is continued past its domain along its
                // boundary tangent planes: an offset's intersection can run
                // there, and the domain's edge would stand in for it (chart
                // points 0.04 mm off a clamped base).
                let normal_side = if *base_sense { *distance } else { -distance };
                let (f, g) = match base.as_ref() {
                    Surface::Nurbs(surface) => {
                        let (foot, normal) = extended_foot(surface, x)?;
                        (x.sub(foot).dot(normal), normal)
                    }
                    // An offset of a swept surface is swept too, by the
                    // section's offset across the sweep: measured to that
                    // curve. Where the section bends tighter than the offset
                    // on its concave side, the base's nearest point is not
                    // the one whose offset passes through x (the offset of a
                    // section's sharp start read 0.015 mm off its own points
                    // through its base's nearest point, on the extension).
                    // Only a point the base reads off the offset is looked
                    // for on the offset itself: one it reads on is.
                    Surface::Swept { section, direction } => {
                        let (f, g) = base.distance(x)?;
                        if (f - normal_side).abs() <= 10.0 * 1e-5 {
                            (f, g)
                        } else {
                            let d = unit(*direction)?;
                            let across = |p: Vec3| p.sub(d.scale(p.dot(d)));
                            let normal_at = |u: f64| unit(curve_tangent(section, u)?.cross(d));
                            let offset_at = |u: f64| -> Option<Vec3> { Some(extended_point(section, u).ok()?.add(normal_at(u)?.scale(normal_side))) };
                            let u = nearest_parameter_by(section, [true, true], |u| offset_at(u).map_or(f64::INFINITY, |o| across(o.sub(x)).length()))?;
                            let (o, normal) = (offset_at(u)?, normal_at(u)?);
                            // Through the offset itself: its distance is f - side.
                            (x.sub(o).dot(normal) + normal_side, normal)
                        }
                    }
                    // A spun base's foot is the one the offset stands
                    // out from, where its turn is tighter than the offset.
                    // Any other foot is looked for beyond the resolution:
                    // within the blend's 5% the wrong side's foot moved a
                    // spine point already on the offset (cross review).
                    Surface::Spun { .. } => base.distance_near(x, normal_side, true, 1e-5)?,
                    other => other.distance(x)?,
                };
                // Orient the base distance with the natural normal: the
                // analytic distances above grow outwards, as their natural
                // normals point; a B-surface's is measured along its normal.
                Some((f - normal_side, g))
            }
            // A swept surface is its section's cylinder: the nearest point
            // is the section's nearest across the sweep, then slid along it.
            // Measured along the natural normal, dC/du × sweep.
            Surface::Swept { section, direction } => {
                let d = unit(*direction)?;
                let across = |p: Vec3| p.sub(d.scale(p.dot(d)));
                let u = nearest_parameter(section, [true, true], |c| across(c.sub(x)).length())?;
                let c = extended_point(section, u).ok()?;
                let foot = c.add(d.scale(x.sub(c).dot(d)));
                let normal = unit(curve_tangent(section, u)?.cross(d))?;
                Some((x.sub(foot).dot(normal), normal))
            }
            // A spun surface in the half-plane through the axis and `x` is
            // its profile's (height, radius) trace: the nearest point is that
            // trace's nearest to x's (height, radius), turned to x's side.
            // Measured along the natural normal, dP/du × dP/dv.
            Surface::Spun { profile, axis_point, axis } => {
                let split = spun_split(*axis_point, *axis)?;
                let (h, radial) = split(x);
                // A profile is not continued past an end on the axis (a
                // dome's pole): past it the trace folds back on itself.
                let off_axis = |t: f64| profile.point(t).is_ok_and(|c| split(c).1.length() > 1e-6);
                let extend = profile.natural_range().map_or([true, true], |[a, b]| [off_axis(a), off_axis(b)]);
                let u = nearest_parameter(profile, extend, |c| {
                    let (hc, rc) = split(c);
                    (hc - h).hypot(rc.length() - radial.length())
                })?;
                spun_distance_at(profile, *axis_point, *axis, x, u)
            }
        }
    }

    /// [`Self::distance`], but where the nearest point does not stand
    /// `want` (mm) from `x` within `band` (signed when `signed`, else either side), the
    /// foot among the surface's local nearest points that comes nearest to
    /// standing `want` from it. Inside a spun profile's turn tighter than
    /// `want`, a point `want` from one side of the turn can be nearer its
    /// other side, beyond the face: a rolling ball's spine, refined as the
    /// meeting of its supports' offsets, was read through that side and
    /// fell to 0.07 mm from the support it rests on at 0.8. Only a spun
    /// surface has more than one foot looked for; the others read as
    /// [`Self::distance`] does.
    pub fn distance_near(&self, x: Vec3, want: f64, signed: bool, band: f64) -> Option<(f64, Vec3)> {
        let miss = |d: f64| if signed { (d - want).abs() } else { (d.abs() - want).abs() };
        let nearest = self.distance(x);
        if nearest.is_some_and(|(d, _)| miss(d) <= band) {
            return nearest;
        }
        let Surface::Spun { profile, axis_point, axis } = self else { return nearest };
        let split = spun_split(*axis_point, *axis)?;
        let (h, radial) = split(x);
        let trace = |u: f64| {
            extended_point(profile, u).ok().map_or(f64::INFINITY, |c| {
                let (hc, rc) = split(c);
                (hc - h).hypot(rc.length() - radial.length())
            })
        };
        // The profile's own range only, sampled finely enough to part two
        // feet either side of a tight turn. A profile with no range of its
        // own (a line) has no other foot (cross review: it read none).
        let range = match profile {
            Curve::Nurbs(nurbs) => nurbs.domain().ok(),
            _ => profile.natural_range(),
        };
        let Some([a, b]) = range else { return nearest };
        let closed = matches!(profile, Curve::Circle { .. } | Curve::Ellipse { .. }) || closed_range(profile).is_some();
        let n = 2048;
        let samples: Vec<f64> = (0..=n).map(|k| a + (b - a) * k as f64 / n as f64).collect();
        let values: Vec<f64> = samples.iter().map(|&u| trace(u)).collect();
        let tolerance = 1e-12 * (b - a).abs().max(1e-300);
        let mut best = nearest;
        for i in 0..=n {
            let (prev, next) = match (i, closed) {
                (0, true) => (values[n - 1], values[1]),
                (0, false) => (f64::INFINITY, values[1]),
                (i, _) if i == n => {
                    if closed {
                        continue;
                    }
                    (values[n - 1], f64::INFINITY)
                }
                (i, _) => (values[i - 1], values[i + 1]),
            };
            // Every foot, the distance's least and greatest along the
            // profile: beyond a turn's centre of curvature the foot a point
            // stands out from is the greatest of its neighbours (cross
            // review), an end of an open profile only the least.
            let least = values[i] <= prev && values[i] <= next;
            let greatest = values[i] >= prev && values[i] >= next && prev.is_finite() && next.is_finite();
            if !((least || greatest) && values[i].is_finite()) {
                continue;
            }
            let step = (b - a) / n as f64;
            let (lo, hi) = (samples[i] - step, samples[i] + step);
            let (lo, hi) = if closed { (lo, hi) } else { (lo.max(a), hi.min(b)) };
            let u = if least { brent_minimum(&trace, lo, hi, tolerance) } else { brent_minimum(&|u: f64| -trace(u), lo, hi, tolerance) };
            let Some(candidate) = spun_distance_at(profile, *axis_point, *axis, x, u) else { continue };
            if best.is_none_or(|(d, _)| miss(candidate.0) < miss(d)) {
                best = Some(candidate);
            }
        }
        best
    }
}

/// A point's height along a spun surface's axis and its offset across it.
fn spun_split(axis_point: Vec3, axis: Vec3) -> Option<impl Fn(Vec3) -> (f64, Vec3)> {
    let k = axis.normalized().ok()?;
    Some(move |p: Vec3| {
        let d = p.sub(axis_point);
        let h = d.dot(k);
        (h, d.sub(k.scale(h)))
    })
}

/// A spun surface's signed distance from `x` through the profile's point
/// at `u`, turned onto x's side of the axis, and its gradient: measured
/// along the natural normal, dP/du × dP/dv.
fn spun_distance_at(profile: &Curve, axis_point: Vec3, axis: Vec3, x: Vec3, u: f64) -> Option<(f64, Vec3)> {
    let unit = |v: Vec3| v.normalized().ok();
    let k = unit(axis)?;
    let split = spun_split(axis_point, axis)?;
    let e = unit(split(x).1)?;
    let (hc, rc) = split(extended_point(profile, u).ok()?);
    let foot = axis_point.add(k.scale(hc)).add(e.scale(rc.length()));
    let t = curve_tangent(profile, u)?;
    // The profile's tangent, turned with its point onto x's side.
    let along = k.scale(t.dot(k)).add(e.scale(unit(rc).map_or(0.0, |r| t.dot(r))));
    let normal = unit(along.cross(k.cross(e)))?;
    Some((x.sub(foot).dot(normal), normal))
}

/// Whether (u, v) lies past an open side of a B-surface's domain (a closed
/// direction wraps; it has no past).
fn outside_domain(surface: &NurbsSurface, u: f64, v: f64) -> bool {
    let (Ok([u0, u1]), Ok([v0, v1]), Ok((closed_u, closed_v))) = (surface.domain_u(), surface.domain_v(), surface.closed_directions()) else { return false };
    (!closed_u && (u < u0 || u > u1)) || (!closed_v && (v < v0 || v > v1))
}

/// The nearest point of `surface` to `x` and its unit natural normal, the
/// surface continued past an open edge of its domain along the boundary's
/// tangent plane ([`NurbsSurface::derivatives_extended`]): the domain's
/// projection, refined by Gauss-Newton on the extension when it stops at the
/// domain's edge.
fn extended_foot(surface: &NurbsSurface, x: Vec3) -> Option<(Vec3, Vec3)> {
    let foot = crate::project_point_to_surface(surface, x).ok()?;
    let [u0, u1] = surface.domain_u().ok()?;
    let [v0, v1] = surface.domain_v().ok()?;
    let at_edge = |t: f64, a: f64, b: f64| (t - a).abs() <= 1e-9 * (b - a) || (b - t).abs() <= 1e-9 * (b - a);
    if !at_edge(foot.u, u0, u1) && !at_edge(foot.v, v0, v1) {
        return Some((foot.point, surface.normal(foot.u, foot.v).ok()?));
    }
    let (mut u, mut v) = (foot.u, foot.v);
    for _ in 0..50 {
        let d = surface.derivatives_extended(u, v, 1).ok()?;
        let (su, sv) = (d[1][0], d[0][1]);
        let r = x.sub(d[0][0]);
        let (a, b, c) = (su.dot(su), su.dot(sv), sv.dot(sv));
        let det = a * c - b * b;
        if !(det > 0.0) {
            break;
        }
        let (g1, g2) = (r.dot(su), r.dot(sv));
        let (du, dv) = ((c * g1 - b * g2) / det, (a * g2 - b * g1) / det);
        u += du;
        v += dv;
        if du.abs() <= 1e-15 * (u1 - u0) && dv.abs() <= 1e-15 * (v1 - v0) {
            break;
        }
    }
    let d = surface.derivatives_extended(u, v, 1).ok()?;
    Some((d[0][0], d[1][0].cross(d[0][1]).normalized().ok()?))
}

/// A point of `curve` at `t`, a bounded B-curve continued past its ends
/// along its end tangents at its end speeds: Parasolid extends the sections
/// of swept and spun surfaces so (a SolidWorks part's blend spine runs
/// 123 mm along a swept section's straight extension).
/// A closed B-curve is not extended: it wraps round its period.
pub fn extended_point(curve: &Curve, t: f64) -> Result<Vec3, String> {
    let Curve::Nurbs(nurbs) = curve else { return curve.point(t) };
    if let Some([a, b]) = closed_range(curve) {
        return nurbs.evaluate(a + (t - a).rem_euclid(b - a));
    }
    let [a, b] = nurbs.domain()?;
    let h = 1e-6 * (b - a);
    if t < a {
        let p = nurbs.evaluate(a)?;
        let velocity = nurbs.evaluate(a + h)?.sub(p).scale(1.0 / h);
        Ok(p.add(velocity.scale(t - a)))
    } else if t > b {
        let p = nurbs.evaluate(b)?;
        let velocity = p.sub(nurbs.evaluate(b - h)?).scale(1.0 / h);
        Ok(p.add(velocity.scale(t - b)))
    } else {
        nurbs.evaluate(t)
    }
}

/// A B-curve's range when its ends meet (to 1e-6 mm): a closed section or
/// profile, which is searched round its period and never extended.
pub fn closed_range(curve: &Curve) -> Option<[f64; 2]> {
    let Curve::Nurbs(nurbs) = curve else { return None };
    let [a, b] = nurbs.domain().ok()?;
    (b > a && nurbs.evaluate(a).ok()?.sub(nurbs.evaluate(b).ok()?).length() <= 1e-6).then_some([a, b])
}

/// The parameter of `curve` (extended, [`extended_point`]) where `distance`
/// (from some fixed point) is least: the best of eight samples in every knot
/// span (fewer, but at least two, when spans are many), refined by Brent's
/// method between its neighbouring samples. A closed curve is searched over
/// its period, a bounded B-curve over its range and as far again past each
/// end `extend` names, a line over ±1 km about its point.
pub fn nearest_parameter(curve: &Curve, extend: [bool; 2], distance: impl Fn(Vec3) -> f64) -> Option<f64> {
    nearest_parameter_by(curve, extend, |t| extended_point(curve, t).ok().map_or(f64::INFINITY, &distance))
}

/// [`nearest_parameter`] with the distance given as a function of the
/// curve's parameter, searched over the same samples and bounds.
pub fn nearest_parameter_by(curve: &Curve, extend: [bool; 2], f: impl Fn(f64) -> f64) -> Option<f64> {
    // The samples, and the bounds the search may not pass: a B-curve's ends
    // it is not extended past. A closed curve's search is unbounded (its
    // foot just below the seam lies before the first sample) and wraps.
    let (mut floor, mut ceiling) = (f64::NEG_INFINITY, f64::INFINITY);
    let closed = closed_range(curve);
    let samples: Vec<f64> = match curve {
        Curve::Nurbs(nurbs) => {
            let [a, b] = nurbs.domain().ok()?;
            let extend = if closed.is_some() { [false, false] } else { extend };
            if closed.is_none() {
                if !extend[0] {
                    floor = a;
                }
                if !extend[1] {
                    ceiling = b;
                }
            }
            let mut knots: Vec<f64> = nurbs.knots.iter().copied().filter(|k| (a..=b).contains(k)).collect();
            knots.dedup();
            let spans = knots.len().saturating_sub(1).max(1);
            let per_span = (1024 / spans).clamp(2, 8);
            let mut samples = Vec::with_capacity(spans * per_span + 2 * 8 * spans + 1);
            // Each extension is as long as the range, sampled as densely as
            // the range is on average.
            let outside = (8 * spans).min(256);
            if extend[0] {
                samples.extend((0..outside).map(|k| a - (b - a) + (b - a) * k as f64 / outside as f64));
            }
            for pair in knots.windows(2) {
                samples.extend((0..per_span).map(|k| pair[0] + (pair[1] - pair[0]) * k as f64 / per_span as f64));
            }
            samples.push(b);
            if extend[1] {
                samples.extend((1..=outside).map(|k| b + (b - a) * k as f64 / outside as f64));
            }
            samples
        }
        _ => {
            // Thirty-two samples round a circle or ellipse, as many along a
            // line's ±1 km.
            let [a, b] = curve.natural_range().unwrap_or([-1000.0, 1000.0]);
            (0..=32).map(|k| a + (b - a) * k as f64 / 32.0).collect()
        }
    };
    let best = (0..samples.len()).min_by(|&i, &j| f(samples[i]).total_cmp(&f(samples[j])))?;
    // The neighbouring samples bracket the minimum; at the first or last
    // sample, the bracket runs as far again outwards (a closed curve's foot
    // just below its seam, a B-curve's foot past its last sample on its
    // extension), within the bounds.
    let t = samples[best];
    let below = if best > 0 { samples[best - 1] } else { t - (samples[1.min(samples.len() - 1)] - t) };
    let above = if best + 1 < samples.len() { samples[best + 1] } else { t + (t - samples[best.saturating_sub(1)]) };
    let (lo, hi) = (below.max(floor), above.min(ceiling));
    let tolerance = 1e-12 * (samples[samples.len() - 1] - samples[0]).abs().max(1e-300);
    let t = brent_minimum(&f, lo, hi, tolerance);
    let t = match closed {
        Some([a, b]) => a + (t - a).rem_euclid(b - a),
        None => t,
    };
    f(t).is_finite().then_some(t)
}

/// Brent's minimisation of `f` over [lo, hi] (golden sections and
/// parabolic steps), to an absolute parameter tolerance `tolerance`.
fn brent_minimum(f: &impl Fn(f64) -> f64, mut lo: f64, mut hi: f64, tolerance: f64) -> f64 {
    let golden = 0.5 * (3.0 - 5f64.sqrt());
    let mut x = lo + golden * (hi - lo);
    let (mut w, mut v) = (x, x);
    let mut fx = f(x);
    let (mut fw, mut fv) = (fx, fx);
    let (mut d, mut e) = (0.0f64, 0.0f64);
    for _ in 0..100 {
        let mid = 0.5 * (lo + hi);
        let tol1 = tolerance + 1e-10 * x.abs();
        let tol2 = 2.0 * tol1;
        if (x - mid).abs() <= tol2 - 0.5 * (hi - lo) {
            break;
        }
        let mut golden_step = true;
        if e.abs() > tol1 {
            // A parabola through x, w and v.
            let r = (x - w) * (fx - fv);
            let mut q = (x - v) * (fx - fw);
            let mut p = (x - v) * q - (x - w) * r;
            q = 2.0 * (q - r);
            if q > 0.0 {
                p = -p;
            }
            q = q.abs();
            if p.abs() < (0.5 * q * e).abs() && p > q * (lo - x) && p < q * (hi - x) {
                e = d;
                d = p / q;
                let u = x + d;
                if u - lo < tol2 || hi - u < tol2 {
                    d = if mid >= x { tol1 } else { -tol1 };
                }
                golden_step = false;
            }
        }
        if golden_step {
            e = if x >= mid { lo - x } else { hi - x };
            d = golden * e;
        }
        let u = if d.abs() >= tol1 { x + d } else if d > 0.0 { x + tol1 } else { x - tol1 };
        let fu = f(u);
        if fu <= fx {
            if u >= x {
                lo = x;
            } else {
                hi = x;
            }
            (v, fv, w, fw, x, fx) = (w, fw, x, fx, u, fu);
        } else {
            if u < x {
                lo = u;
            } else {
                hi = u;
            }
            if fu <= fw || w == x {
                (v, fv, w, fw) = (w, fw, u, fu);
            } else if fu <= fv || v == x || v == w {
                (v, fv) = (u, fu);
            }
        }
    }
    x
}

/// A curve's unit tangent at `t` (extended, [`extended_point`]), from its
/// exact derivative: a difference step either crosses a tiny parameter
/// range whole or rounds away beside a large knot (cross review, re-review).
pub fn curve_tangent(curve: &Curve, t: f64) -> Option<Vec3> {
    match curve {
        Curve::Line { direction, .. } => direction.normalized().ok(),
        Curve::Circle { frame, .. } => frame.x.scale(-t.sin()).add(frame.y().scale(t.cos())).normalized().ok(),
        Curve::Ellipse { frame, major, minor } => frame.x.scale(-major * t.sin()).add(frame.y().scale(minor * t.cos())).normalized().ok(),
        Curve::Nurbs(nurbs) => {
            let [a, b] = nurbs.domain().ok()?;
            // Round a closed curve as [`extended_point`] goes, not along an
            // end's tangent line.
            let t = if closed_range(curve).is_some() { a + (t - a).rem_euclid(b - a) } else { t };
            nurbs.point_and_unit_tangent_extended(t, a, b).ok().map(|(_, tangent)| tangent)
        }
    }
}

/// A lemon torus (major radius `major` < 0, `|major|` < `minor`, mm) as the
/// surface the writer gives it: the arc of its generating circle (centre
/// `major` along the frame's x, radius `minor`, in the x-z half-plane) where
/// the torus's radial factor `major + minor·cos v` is positive, spun about
/// the axis. The arc runs pole to pole, `v` from `θ0` down to `-θ0` with
/// `cos θ0 = -major / minor`: two exact rational quadratics, each sweeping
/// under π/2, so no weight falls under cos(π/4) however near a sphere the
/// lemon is (re-review). Running down the axis makes the spun surface's
/// natural normal `dP/du × dP/dv` the torus's: at v = 0 the profile's
/// tangent is `-z` and the turn's is `+y`, so the normal is `+x`, away from
/// the circle's centre, as `(cos v)·radial + (sin v)·axis` is (decided, not
/// sampled: a thin lemon has no length to difference across, re-review).
pub fn lemon(frame: Frame, major: f64, minor: f64) -> Result<Surface, String> {
    if !(minor > -major && major < 0.0) {
        return Err(format!("major radius {major} mm and minor radius {minor} mm are not a lemon"));
    }
    let centre = frame.origin.add(frame.x.scale(major));
    let top = (-major / minor).acos();
    let direction = |v: f64| frame.x.scale(v.cos()).add(frame.z.scale(v.sin()));
    let on = |v: f64| centre.add(direction(v).scale(minor));
    let half = top / 2.0;
    let apex = |mid: f64| (centre.add(direction(mid).scale(minor / half.cos())), half.cos());
    let points = [(on(top), 1.0), apex(top / 2.0), (on(0.0), 1.0), apex(-top / 2.0), (on(-top), 1.0)];
    let net = points.iter().map(|&(p, w)| Vec4 { x: p.x * w, y: p.y * w, z: p.z * w, w }).collect();
    let arc = NurbsCurve::new(2, vec![0.0, 0.0, 0.0, 0.5, 0.5, 1.0, 1.0, 1.0], net)?;
    Ok(Surface::Spun { profile: Curve::Nurbs(arc), axis_point: frame.origin, axis: frame.z })
}

/// Read the surface node `node`.
pub fn surface(file: &XtFile, node: &Node, depth: usize) -> Result<Surface, String> {
    if depth > 8 {
        return Err("surfaces nest more than 8 deep".into());
    }
    Ok(match node.type_name() {
        "PLANE" => Surface::Plane(Frame::read(node, "pvec", "normal")?),
        "CYLINDER" => Surface::Cylinder { frame: Frame::read(node, "pvec", "axis")?, radius: required_length(node, "radius")? },
        "CONE" => Surface::Cone {
            frame: Frame::read(node, "pvec", "axis")?,
            radius: required_length(node, "radius")?,
            sin: required_real(node, "sin_half_angle")?,
            cos: required_real(node, "cos_half_angle")?,
        },
        "SPHERE" => Surface::Sphere { frame: Frame::read(node, "centre", "axis")?, radius: required_length(node, "radius")? },
        "TORUS" => {
            let major = required_length(node, "major_radius")?;
            let minor = required_length(node, "minor_radius")?;
            let frame = Frame::read(node, "centre", "axis")?;
            if major < 0.0 && minor <= -major {
                return Err(format!("TORUS {} has major radius {major} mm and minor {minor} mm, not a lemon", node.index));
            }
            // A lemon (§5.2.2.5: a < 0, |a| < b) stays a torus here: the
            // torus formula is its XT evaluator too, the one its SP-curves,
            // blends and offsets are parameterised by. Only the writer says
            // it otherwise (STEP has no lemon): see [`lemon`].
            if major == 0.0 {
                return Err(format!("TORUS {} has major radius 0, which the format says is a sphere", node.index));
            }
            Surface::Torus { frame, major, minor }
        }
        "B_SURFACE" => {
            let nurbs = file
                .follow(node, "nurbs")
                .ok_or_else(|| format!("B_SURFACE {} has no NURBS_SURF", node.index))?;
            Surface::Nurbs(nurbs_surface(file, nurbs)?)
        }
        "OFFSET_SURF" => {
            let base_node = file
                .follow(node, "surface")
                .ok_or_else(|| format!("OFFSET_SURF {} has no underlying surface", node.index))?;
            Surface::Offset {
                base: Box::new(surface(file, base_node, depth + 1)?),
                base_sense: base_node.char("sense") != Some(b'-'),
                distance: required_length(node, "offset")?,
            }
        }
        "SPUN_SURF" => {
            let profile = file
                .follow(node, "profile")
                .ok_or_else(|| format!("SPUN_SURF {} has no profile", node.index))?;
            Surface::Spun {
                profile: curve(file, profile)?,
                axis_point: required_position(node, "base")?,
                axis: required_direction(node, "axis")?,
            }
        }
        "SWEPT_SURF" => {
            let section = file
                .follow(node, "section")
                .ok_or_else(|| format!("SWEPT_SURF {} has no section", node.index))?;
            Surface::Swept { section: curve(file, section)?, direction: required_direction(node, "sweep")? }
        }
        other => {
            return Err(format!(
                "surface {} is a {other}, which is not read yet",
                node.int("node_id").unwrap_or(node.index as i64)
            ))
        }
    })
}

/// Read the curve node `node` (not INTERSECTION, SP_CURVE or TRIMMED_CURVE,
/// which the translator handles through their parts), in mm.
pub fn curve(file: &XtFile, node: &Node) -> Result<Curve, String> {
    Ok(match node.type_name() {
        "LINE" => Curve::Line { point: required_position(node, "pvec")?, direction: required_direction(node, "direction")? },
        "CIRCLE" => Curve::Circle { frame: Frame::read(node, "centre", "normal")?, radius: required_length(node, "radius")? },
        "ELLIPSE" => Curve::Ellipse {
            frame: Frame::read(node, "centre", "normal")?,
            major: required_length(node, "major_radius")?,
            minor: required_length(node, "minor_radius")?,
        },
        "B_CURVE" => {
            let nurbs = file
                .follow(node, "nurbs")
                .ok_or_else(|| format!("B_CURVE {} has no NURBS_CURVE", node.index))?;
            Curve::Nurbs(nurbs_curve(file, nurbs, MM_PER_METRE)?)
        }
        other => return Err(format!("curve {} is a {other}, which is not read as a basis curve", node.index)),
    })
}

/// The 2D B-curve of an SP-curve, in its surface's parameters (unscaled).
pub fn parameter_curve(file: &XtFile, node: &Node) -> Result<NurbsCurve, String> {
    if node.type_name() != "B_CURVE" {
        return Err(format!("an SP-curve's 2D curve is a {}, not a B-curve", node.type_name()));
    }
    let nurbs = file
        .follow(node, "nurbs")
        .ok_or_else(|| format!("B_CURVE {} has no NURBS_CURVE", node.index))?;
    nurbs_curve(file, nurbs, 1.0)
}

/// Distinct knots and multiplicities, expanded. The knot and multiplicity
/// nodes may be longer than the `count` the curve states, with the rest
/// unset; only the first `count` are the knot vector.
fn expanded_knots(file: &XtFile, node: &Node, count: &str, knots: &str, mult: &str) -> Result<Vec<f64>, String> {
    let mut values = file
        .follow(node, knots)
        .ok_or_else(|| format!("{} {} has no {knots}", node.type_name(), node.index))?
        .reals("knots");
    let mut mults = file
        .follow(node, mult)
        .ok_or_else(|| format!("{} {} has no {mult}", node.type_name(), node.index))?
        .ints("mult");
    if let Some(n) = node.int(count).and_then(|n| usize::try_from(n).ok()) {
        if n > values.len() || n > mults.len() {
            return Err(format!("{} {} states {n} knots and has {}", node.type_name(), node.index, values.len().min(mults.len())));
        }
        values.truncate(n);
        mults.truncate(n);
    }
    if values.len() != mults.len() || values.iter().any(|v| !v.is_finite()) {
        return Err(format!("{} {} has {} knots and {} multiplicities", node.type_name(), node.index, values.len(), mults.len()));
    }
    // A multiplicity is at most degree + 1, and no degree XT writes nears
    // this bound; the vertex arrays bound the total long before it.
    const MAX_MULTIPLICITY: i64 = 64;
    let mut out = Vec::new();
    for (value, mult) in values.iter().zip(mults) {
        if !(1..=MAX_MULTIPLICITY).contains(&mult) {
            return Err(format!("{} {} has a knot of multiplicity {mult}", node.type_name(), node.index));
        }
        out.extend(std::iter::repeat(*value).take(mult as usize));
    }
    Ok(out)
}

/// Homogeneous control points from a vertex array, positions scaled by
/// `scale`: rational vertices are stored premultiplied by their weight, with
/// the weight last (§5.2.1.4).
fn control_points(values: &[f64], dim: usize, rational: bool, scale: f64) -> Result<Vec<Vec4>, String> {
    if dim == 0 || values.len() % dim != 0 || values.iter().any(|v| !v.is_finite()) {
        return Err(format!("a B-spline vertex array of {} values does not divide into {dim}-vectors", values.len()));
    }
    let space = if rational { dim - 1 } else { dim };
    if !(2..=3).contains(&space) {
        return Err(format!("B-spline vertices of dimension {dim} are not read"));
    }
    Ok(values
        .chunks(dim)
        .map(|c| {
            let w = if rational { c[dim - 1] } else { 1.0 };
            let z = if space == 3 { c[2] } else { 0.0 };
            // A rational vertex is already x·w, y·w, z·w.
            Vec4 { x: c[0] * scale, y: c[1] * scale, z: z * scale, w }
        })
        .collect())
}

fn nurbs_curve(file: &XtFile, node: &Node, scale: f64) -> Result<NurbsCurve, String> {
    let degree = node.int("degree").unwrap_or(0);
    let degree = usize::try_from(degree).ok().filter(|&d| d >= 1).ok_or_else(|| format!("NURBS_CURVE {} has degree {degree}", node.index))?;
    let dim = node.int("vertex_dim").unwrap_or(0).max(0) as usize;
    let rational = node.logical("rational").unwrap_or(false);
    let vertices = file
        .follow(node, "bspline_vertices")
        .ok_or_else(|| format!("NURBS_CURVE {} has no vertices", node.index))?
        .reals("vertices");
    let count = node.int("n_vertices").and_then(|n| usize::try_from(n).ok()).unwrap_or(0) * dim;
    let vertices = if count > 0 && count <= vertices.len() { &vertices[..count] } else { &vertices[..] };
    let points = control_points(vertices, dim, rational, scale)?;
    let knots = expanded_knots(file, node, "n_knots", "knots", "knot_mult")?;
    let (knots, points) = clamp(degree, knots, points)?;
    NurbsCurve::new(degree, knots, points)
}

fn nurbs_surface(file: &XtFile, node: &Node) -> Result<NurbsSurface, String> {
    let read_degree = |field: &str| {
        let d = node.int(field).unwrap_or(0);
        usize::try_from(d).ok().filter(|&d| d >= 1).ok_or_else(|| format!("NURBS_SURF {} has {field} {d}", node.index))
    };
    let (degree_u, degree_v) = (read_degree("u_degree")?, read_degree("v_degree")?);
    let nu = node.int("n_u_vertices").unwrap_or(0).max(0) as usize;
    let nv = node.int("n_v_vertices").unwrap_or(0).max(0) as usize;
    let dim = node.int("vertex_dim").unwrap_or(0).max(0) as usize;
    let rational = node.logical("rational").unwrap_or(false);
    let vertices = file
        .follow(node, "bspline_vertices")
        .ok_or_else(|| format!("NURBS_SURF {} has no vertices", node.index))?
        .reals("vertices");
    let count = nu * nv * dim;
    let vertices = if count > 0 && count <= vertices.len() { &vertices[..count] } else { &vertices[..] };
    let points = control_points(vertices, dim, rational, MM_PER_METRE)?;
    if nv == 0 || points.len() != nu * nv {
        return Err(format!("NURBS_SURF {} has {} vertices for a {nu} by {nv} net", node.index, points.len()));
    }
    // The net is stored with v varying fastest: row i holds u index i
    // (checked on corpus faces: the transposed reading misses the faces'
    // vertices by orders of magnitude more).
    let rows: Vec<Vec<Vec4>> = points.chunks(nv).map(<[Vec4]>::to_vec).collect();
    let knots_u = expanded_knots(file, node, "n_u_knots", "u_knots", "u_knot_mult")?;
    let knots_v = expanded_knots(file, node, "n_v_knots", "v_knots", "v_knot_mult")?;
    // Clamp each direction by clamping every row (v) and every column (u).
    let mut clamped_v_knots = None;
    let mut rows_v = Vec::with_capacity(rows.len());
    for row in rows {
        let (k, r) = clamp(degree_v, knots_v.clone(), row)?;
        clamped_v_knots = Some(k);
        rows_v.push(r);
    }
    let knots_v = clamped_v_knots.ok_or_else(|| format!("NURBS_SURF {} has no rows", node.index))?;
    let columns = rows_v.first().map_or(0, Vec::len);
    let mut clamped_u_knots = None;
    let mut columns_u = Vec::with_capacity(columns);
    for j in 0..columns {
        let column: Vec<Vec4> = rows_v.iter().map(|row| row[j]).collect();
        let (k, c) = clamp(degree_u, knots_u.clone(), column)?;
        clamped_u_knots = Some(k);
        columns_u.push(c);
    }
    let knots_u = clamped_u_knots.ok_or_else(|| format!("NURBS_SURF {} has no columns", node.index))?;
    let nu = columns_u.first().map_or(0, Vec::len);
    let net: Vec<Vec<Vec4>> = (0..nu).map(|i| columns_u.iter().map(|column| column[i]).collect()).collect();
    NurbsSurface::new(degree_u, degree_v, knots_u, knots_v, net)
}

/// A B-spline restricted to its valid range [t_p, t_(m-p)] with clamped
/// (Bézier) ends. XT stores periodic B-splines unclamped (§5.2.1.4); the
/// kernel's NURBS are clamped. Knot insertion to full multiplicity at each
/// end of the valid range leaves the curve unchanged on it.
pub fn clamp(degree: usize, knots: Vec<f64>, points: Vec<Vec4>) -> Result<(Vec<f64>, Vec<Vec4>), String> {
    let p = degree;
    if points.len() < p + 1 {
        return Err(format!("a degree {p} B-spline has only {} vertices", points.len()));
    }
    if knots.windows(2).any(|w| !(w[1] >= w[0])) {
        return Err("a B-spline's knots decrease".into());
    }
    if knots.len() != points.len() + p + 1 {
        return Err(format!(
            "a degree {p} B-spline with {} vertices needs {} knots, has {}",
            points.len(),
            points.len() + p + 1,
            knots.len()
        ));
    }
    let (start, end) = (knots[p], knots[knots.len() - 1 - p]);
    if !(end > start) {
        return Err(format!("a B-spline's valid range [{start}, {end}] is empty"));
    }
    let clamped_at = |knots: &[f64], value: f64, from_start: bool| {
        let run = if from_start {
            knots.iter().take_while(|&&k| k == value).count()
        } else {
            knots.iter().rev().take_while(|&&k| k == value).count()
        };
        run >= p + 1
    };
    if clamped_at(&knots, start, true) && clamped_at(&knots, end, false) {
        return Ok((knots, points));
    }
    let (mut knots, mut points) = (knots, points);
    for value in [start, end] {
        let have = knots.iter().filter(|&&k| k == value).count();
        for _ in have..p {
            (knots, points) = insert_knot(p, &knots, &points, value);
        }
    }
    // `first` .. `first + p` are the last p copies of start, and `last` the
    // p-th copy of end from the left; the curve passes through vertex
    // `first - 1` at start and vertex `last - p` at end.
    let first = knots.iter().rposition(|&k| k == start).expect("start was inserted") + 1 - p;
    let last = knots.iter().position(|&k| k == end).expect("end was inserted") + p - 1;
    let lo = first - 1;
    let hi = last - p + 1;
    let mut new_knots = vec![start];
    new_knots.extend_from_slice(&knots[first..=last]);
    new_knots.push(end);
    let new_points = points[lo..hi].to_vec();
    if new_knots.len() != new_points.len() + p + 1 {
        return Err("clamping a periodic B-spline went wrong".into());
    }
    Ok((new_knots, new_points))
}

/// Boehm insertion of one knot.
fn insert_knot(p: usize, knots: &[f64], points: &[Vec4], value: f64) -> (Vec<f64>, Vec<Vec4>) {
    // The span k with knots[k] <= value < knots[k+1], within the valid range.
    let n = points.len();
    let mut k = p;
    while k + 1 < n && knots[k + 1] <= value {
        k += 1;
    }
    let mut new_points = Vec::with_capacity(n + 1);
    for i in 0..=n {
        let point = if i + p <= k {
            points[i]
        } else if i > k {
            points[i - 1]
        } else {
            let denominator = knots[i + p] - knots[i];
            let a = if denominator == 0.0 { 0.0 } else { (value - knots[i]) / denominator };
            points[i - 1].scale(1.0 - a).add(points[i].scale(a))
        };
        new_points.push(point);
    }
    let mut new_knots = knots.to_vec();
    new_knots.insert(k + 1, value);
    (new_knots, new_points)
}