engeom 0.5.0

Library for working with geometry with a focus on engineering metrology applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
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
//! This module generalizes fitting of boundary geometry to sample points

use crate::common::consensus::weights::{MagsacWeight, estimate_sigma_max};
use crate::common::points::dist;
use crate::common::{PCoords, RefinementHalt, SPCoords, SolveQuality, TerminationReason};
use crate::geom2::{Boundary2, Manifold1Pos2};
use crate::na::{DVector, Dyn, Matrix, Owned, U1, Vector};
use crate::{Point2, Result, SurfacePoint2, VecDot};
use levenberg_marquardt::{LeastSquaresProblem, LevenbergMarquardt};

pub type BndBuildFn = Box<dyn Fn(&DVector<f64>) -> Result<Boundary2>>;
const DELTA: f64 = 1e-6;

/// The residual of a single sample: its distance to the closest position on the boundary, signed
/// by which side of the boundary's normal it fell on.
///
/// The sign carries no extra information for the fit itself. Because the objective squares the
/// residual, and because the jacobian picks up the same sign that the residual does, both `J^T J`
/// and `J^T r` are unchanged by it: a signed and an unsigned fit take identical Levenberg-Marquardt
/// steps and converge to the same parameters.
///
/// It is worth having for two other reasons. The jacobian here is a forward finite difference, and
/// the derivative of an absolute value is wrong for any sample sitting within `DELTA` of the
/// boundary, where the disturbed and undisturbed distances straddle zero. And a robust noise
/// estimate by median absolute deviation assumes residuals that are centered on zero when the fit
/// is good; unsigned distances are all positive, which would have it measure the spread about the
/// typical distance instead and report a scale that is too small.
fn signed_residual(m: &Manifold1Pos2, p: &impl PCoords<2>) -> f64 {
    dist(m, p) * m.scalar_projection(p).signum()
}

/// The residual degrees of freedom for the MAGSAC++ weight function. A boundary residual is a
/// full point-to-manifold distance in the plane, so it follows a chi distribution with two
/// degrees of freedom.
const RESIDUAL_DOF: usize = 2;

/// The default Levenberg-Marquardt evaluation budget, matching the `levenberg-marquardt` crate's
/// own default so that the options struct changes nothing unless a caller asks it to.
const DEFAULT_PATIENCE: usize = 100;

/// Options controlling a boundary fit.
///
/// The default is a plain unweighted least-squares fit with no robust refinement, which is what
/// this module did before the option existed. Robustness is opt-in because it is not free: each
/// refinement round is a whole extra Levenberg-Marquardt solve, and every step of every solve
/// rebuilds the boundary `n_params` times to take a finite-difference jacobian. Call
/// [`BoundaryFitOptions::robust`] when the data is expected to carry outliers and the cost is
/// worth paying.
#[derive(Clone, Copy, Debug)]
pub struct BoundaryFitOptions {
    /// The number of iteratively reweighted refinement rounds to perform after the initial
    /// unweighted solve. Zero, the default, disables robust weighting entirely.
    pub refinement_steps: usize,

    /// The MAGSAC++ upper noise bound, estimated from the initial residuals via the median
    /// absolute deviation when `None`.
    ///
    /// This is in the units of the geometry: a residual is a signed distance from a sample to the
    /// boundary, and nothing normalizes it. Supplying it explicitly is worth doing when the
    /// measurement noise is known, since the estimate is taken from a fit that the outliers have
    /// already influenced.
    pub sigma_max: Option<f64>,

    /// The Levenberg-Marquardt evaluation budget, as a multiplier on the parameter count. Must be
    /// greater than zero.
    pub patience: usize,
}

impl Default for BoundaryFitOptions {
    fn default() -> Self {
        Self {
            refinement_steps: 0,
            sigma_max: None,
            patience: DEFAULT_PATIENCE,
        }
    }
}

impl BoundaryFitOptions {
    /// Options with robust refinement switched on, using the default number of rounds and a noise
    /// bound estimated from the data.
    pub fn robust() -> Self {
        Self {
            refinement_steps: 4,
            ..Default::default()
        }
    }

    fn validate(&self) -> Result<()> {
        if self.patience == 0 {
            return Err("patience must be greater than zero".into());
        }
        if let Some(s) = self.sigma_max
            && (!s.is_finite() || s <= 0.0)
        {
            return Err(
                format!("sigma_max is {s}, but must be finite and strictly positive").into(),
            );
        }
        Ok(())
    }
}

#[derive(Debug)]
pub struct BoundaryFitResult {
    pub params: DVector<f64>,

    /// The geometric residuals of the fit, signed distances in the units of the model. These are
    /// the plain residuals, not the weighted values the solver minimized, so they describe the
    /// geometry rather than the fitting machinery.
    pub residuals: DVector<f64>,

    solves: Vec<TerminationReason>,
    halt: Option<RefinementHalt>,
}

impl BoundaryFitResult {
    pub(crate) fn new(
        params: DVector<f64>,
        residuals: DVector<f64>,
        solves: Vec<TerminationReason>,
        halt: Option<RefinementHalt>,
    ) -> Self {
        Self {
            params,
            residuals,
            solves,
            halt,
        }
    }

    /// How every solve which contributed to this fit terminated, the initial one first.
    pub fn solves(&self) -> &[TerminationReason] {
        &self.solves
    }

    /// The number of robust refinement rounds which completed, which is zero for a plain fit.
    pub fn refinement_rounds(&self) -> usize {
        self.solves.len().saturating_sub(1)
    }

    /// The worst quality among the solves which contributed to this fit.
    pub fn quality(&self) -> SolveQuality {
        self.solves
            .iter()
            .map(SolveQuality::from_termination)
            .fold(SolveQuality::Converged, SolveQuality::worse_of)
    }

    /// Whether every solve which contributed to this fit converged.
    pub fn converged(&self) -> bool {
        self.quality() == SolveQuality::Converged
    }

    /// Why robust refinement stopped early, if it did.
    pub fn halt(&self) -> Option<&RefinementHalt> {
        self.halt.as_ref()
    }
}

// =============================================================================================
// Fitting a boundary to discrete points
// =============================================================================================

/// Given a set of points, a function which takes a parameter vector and produces a [`Boundary2`],
/// and an initial guess, this function will attempt to perform a Levenberg-Marquardt best fit of
/// the parameters to produce a `Boundary2` that minimizes the residuals of the points projected
/// onto the boundary.
///
/// You will need to provide a `DVector` with a reasonable initial guess, and a [`BndBuildFn`] that
/// accepts a `DVector` of that size and produces a boundary. The function is allowed to return a
/// `Err(Box<dyn Error>)` if the parameters produce invalid geometry, but the initial guess values
/// provided may not fail or this function will exit before attempting the minimization.
///
/// Because the residuals are the values of the points projected onto the nearest point on the
/// boundary, be aware that there is no inherent pressure preventing the boundary geometry from
/// growing beyond the sample points _unless_ it exists within the nature of the geometry. For
/// example, if the points lie on a line and the boundary geometry is a segment, there is nothing
/// that will constrain the segment from growing beyond the ends of the points.
///
/// For this reason, it is best to write the builder function so that any constraints are inherently
/// contained within it. For example, write the builder function so that the segment ends are
/// only parameterized in one direction.
///
/// Refer to the examples for more information.
///
/// # Arguments
///
/// * `points`: A slice of `Point2` that the boundary will be fit to. The residuals are calculated
///   as the distance from each point to the closest point on the boundary, so the points will
///   "pull" the closest area of the boundary towards themselves, but won't have any effect on
///   the boundary farther away.
/// * `builder`: A function that takes a `DVector` of parameters and produces a `Boundary2`.
/// * `initial`: An initial guess for the parameters. The `builder` function must return an
///   `Ok(Boundary2)` with this input for the fitting algorithm to initialize. The algorithm will
///   use the size of the initial guess to determine the number of parameters, so make sure it
///   matches what the `builder` function is expecting.
/// * `ignore_ends`: if `true`, points that project onto the ends of an open boundary will have
///   residuals of zero
///
/// returns: Result<BoundaryFitResult>
///
/// # Examples
///
/// ```
/// // This example will fit a boundary consisting of three line segments to a bunch of points
/// // arranged in a triangle.
/// // ------------------------------------------------------------------------------------------
/// use engeom::{Point2, DVector};
/// use engeom::common::{to_points, fill_gaps};
/// use engeom::geom2::{
///     BndBuildFn, BoundaryData2, BoundaryEditor, BoundaryFitOptions, fit_boundary_to_points,
/// };
/// use approx::assert_relative_eq;
///
/// // We'll create the initial corners and then use the `fill_gaps` helper to generate points
/// // between them. Normally this would be your observed data.
/// let corners = to_points(&[[1.0, 1.0], [3.0, 2.0], [2.0, 4.0], [1.0, 1.0]]);
/// let points = fill_gaps(&corners, 0.1);
///
/// // Here we define the function which creates the boundary from six parameters. This is an
/// // extremely simple parameterization that just encodes the three corners as x,y pairs
/// let builder: BndBuildFn = Box::new(|params: &DVector| {
///    let mut bdata = BoundaryData2::new_open(Point2::new(params[0], params[1]));
///    bdata.add_seg_xy(params[2], params[3]);
///    bdata.add_seg_xy(params[4], params[5]);
///    bdata.add_seg_xy(params[0], params[1]);
///    bdata.try_to_boundary()
/// });
///
/// // We'll provide an initial guess which has the corners in roughly the correct areas, just way
/// // too large for the actual data and not aligned with the edges. Then we'll run the fitting
/// // algorithm.
/// let initial = DVector::from(vec![0.0, 0.0, 4.0, 0.0, 1.0, 7.0]);
/// let result = fit_boundary_to_points(
///     &points, &builder, initial, false, &BoundaryFitOptions::default(),
/// )
/// .unwrap();
///
/// // Finally we'll verify that the corners match the ones we originally provided.
/// let expected = DVector::from(vec![1.0, 1.0, 3.0, 2.0, 2.0, 4.0]);
/// assert_relative_eq!(result.params, expected, epsilon = 1.0e-6);
///
/// ```
pub fn fit_boundary_to_points(
    points: &[Point2],
    builder: &BndBuildFn,
    initial: DVector<f64>,
    ignore_ends: bool,
    opts: &BoundaryFitOptions,
) -> Result<BoundaryFitResult> {
    let fitting = BoundaryToPoints::new(points, ignore_ends);
    solve(&fitting, builder, initial, opts)
}

struct BoundaryToPoints<'a> {
    points: &'a [Point2],
    ignore_ends: bool,
}

impl<'a> BoundaryToPoints<'a> {
    fn new(points: &'a [Point2], ignore_ends: bool) -> Self {
        BoundaryToPoints {
            points,
            ignore_ends,
        }
    }
}

impl BoundaryFittable for BoundaryToPoints<'_> {
    fn residuals_and_weights(&self, boundary: &Boundary2) -> (DVector<f64>, DVector<f64>) {
        let bounds = f64::EPSILON..(boundary.length() - f64::EPSILON);
        let mut res = DVector::zeros(self.points.len());
        let mut weights = DVector::zeros(self.points.len());
        weights.fill(1.0);

        for i in 0..self.points.len() {
            let (_, m) = boundary.at_closest_to_point(&self.points[i]);
            if self.ignore_ends {
                weights[i] = if bounds.contains(&m.l) { 1.0 } else { 0.0 };
            }
            res[i] = signed_residual(&m, &self.points[i]);
        }

        (res, weights)
    }

    fn residual_only(&self, sample_i: usize, boundary: &Boundary2) -> f64 {
        let (_, m) = boundary.at_closest_to_point(&self.points[sample_i]);
        signed_residual(&m, &self.points[sample_i])
    }
}

// =============================================================================================
// Fitting a boundary to discrete surface points
// =============================================================================================

/// Given a set of surface points, a function which takes a parameter vector and produces a
/// [`Boundary2`], and an initial guess, this function will attempt to perform a Levenberg-Marquardt
/// best fit of the parameters to produce a `Boundary2` that minimizes the residuals of the surface
/// points projected onto the boundary and weighted by their dot product with the boundary normal
/// at the projection site.
///
/// You will need to provide a `DVector` with a reasonable initial guess, and a [`BndBuildFn`] that
/// accepts a `DVector` of that size and produces a boundary. The function is allowed to return a
/// `Err(Box<dyn Error>)` if the parameters produce invalid geometry, but the initial guess values
/// provided may not fail or this function will exit before attempting the minimization.
///
/// Refer to the examples for more information.
///
/// # Arguments
///
/// * `points`: a slice of `SurfacePoint2` entities, representing points and their surface normals
/// * `builder`: A function that takes a `DVector` of parameters and produces a `Boundary2`.
/// * `initial`: An initial guess for the parameters. The `builder` function must return an
///   `Ok(Boundary2)` with this input for the fitting algorithm to initialize. The algorithm will
///   use the size of the initial guess to determine the number of parameters, so make sure it
///   matches what the `builder` function is expecting.
/// * `weight_mode`: the mode by which the surface normal dot products produce a weight. If you
///   use `AsIs`, a point can have a negative weight if the normals face in opposite direction. If
///   you use `Abs`, it will de-weight surfaces pointing orthogonally to each other, but won't care
///   about the direction. If you use `ClampPos`, it will only consider points with normals facing
///   in the same direction. I recommend using `ClampPos` if you know the boundary and sample
///   normals should be facing the same direction, and `Abs` if you just want to de-weight
///   orthogonal normals but aren't sure if they're facing the same way.
/// * `ignore_ends`: if `true`, points that project onto the ends of an open boundary will have
///   residuals of zero
///
/// returns: Result<BoundaryFitResult>
///
/// # Examples
///
/// ```
/// // This example shows how a very simple boundary fitting can use the surface normals to reject
/// // samples facing the wrong direction
/// // ------------------------------------------------------------------------------------------
/// use engeom::{VecDot, Vector2, SurfacePoint2, DVector, Point2};
/// use engeom::common::{to_points, fill_gaps, linear_space};
/// use engeom::geom2::{
///     BndBuildFn, BoundaryData2, BoundaryEditor, BoundaryFitOptions,
///     fit_boundary_to_surface_points,
/// };
/// use approx::assert_relative_eq;
///
/// // We'll create ten points facing in +Y at Y=0
/// let good = linear_space(0.0, 10.0, 10)
///     .iter()
///     .map(|x| SurfacePoint2::new(Point2::new(*x, 0.0), Vector2::y_axis()))
///     .collect::<Vec<_>>();
///
/// // We'll create ten bad points facing in +x at Y=1
/// let bad = linear_space(0.0, 10.0, 10)
///     .iter()
///     .map(|x| SurfacePoint2::new(Point2::new(*x, 1.0), Vector2::x_axis()))
///     .collect::<Vec<_>>();
///
/// // We'll combine the good and the bad points together
/// let samples = [good, bad].concat();
///
/// // Our builder function will create a single line segment from X=0 to X=10. Because `engeom`'s
/// // boundaries follows the anti-clockwise winding order convention, the segment's surface normal
/// // will be facing in -Y. If we reversed the order it would face in +Y.
/// let builder: BndBuildFn = Box::new(|params: &DVector| {
///     let mut bdata = BoundaryData2::new_open([0.0, params[0]].into());
///     bdata.add_seg_xy(10.0, params[1]);
///     bdata.try_to_boundary()
/// });
///
/// // We'll create our initial guess right near the bad points.
/// let initial = DVector::from(vec![1.0, 1.0]);
///
/// // When we do the fitting, we'll use the `VecDot::Abs` mode, which will not care that the
/// // boundary normal is facing in the opposite direction of the good points, but will de-weight
/// // the bad points because they are orthogonal.
/// let result =
///     fit_boundary_to_surface_points(
///         &samples, &builder, initial, VecDot::Abs, false, &BoundaryFitOptions::default(),
///     )
///     .unwrap();
///
/// // As we can see, the fit ended at the position of the good points.
/// let expected = DVector::from(vec![0.0, 0.0]);
/// assert_relative_eq!(result.params, expected, epsilon = 1.0e-6);
/// ```
pub fn fit_boundary_to_surface_points(
    points: &[SurfacePoint2],
    builder: &BndBuildFn,
    initial: DVector<f64>,
    weight_mode: VecDot,
    ignore_ends: bool,
    opts: &BoundaryFitOptions,
) -> Result<BoundaryFitResult> {
    let fitting = BoundaryToSurfacePoints::new(points, weight_mode, ignore_ends);
    solve(&fitting, builder, initial, opts)
}

struct BoundaryToSurfacePoints<'a> {
    points: &'a [SurfacePoint2],
    weight_mode: VecDot,
    ignore_ends: bool,
}

impl<'a> BoundaryToSurfacePoints<'a> {
    fn new(points: &'a [SurfacePoint2], weight_mode: VecDot, ignore_ends: bool) -> Self {
        BoundaryToSurfacePoints {
            points,
            weight_mode,
            ignore_ends,
        }
    }
}

impl BoundaryFittable for BoundaryToSurfacePoints<'_> {
    fn residuals_and_weights(&self, boundary: &Boundary2) -> (DVector<f64>, DVector<f64>) {
        let bounds = f64::EPSILON..(boundary.length() - f64::EPSILON);
        let mut res = DVector::zeros(self.points.len());
        let mut weights = DVector::zeros(self.points.len());
        weights.fill(1.0);

        for i in 0..self.points.len() {
            let (_, m) = boundary.at_closest_to_point(&self.points[i]);
            let dot = self.points[i].normal.dot(&m.normal);
            weights[i] = match self.weight_mode {
                VecDot::AsIs => dot,
                VecDot::Abs => dot.abs(),
                VecDot::ClampPos => dot.max(0.0),
            };

            if self.ignore_ends && !bounds.contains(&m.l) {
                weights[i] = 0.0;
            }

            res[i] = signed_residual(&m, &self.points[i]);
        }

        (res, weights)
    }

    fn residual_only(&self, sample_i: usize, boundary: &Boundary2) -> f64 {
        let (_, m) = boundary.at_closest_to_point(&self.points[sample_i]);
        signed_residual(&m, &self.points[sample_i])
    }
}

// =============================================================================================
// `BoundaryFit` and `BoundaryFittable` together offer a generic mechanism for performing the
// fitting of boundaries to a set of samples of a finite count.
// =============================================================================================

// =============================================================================================
// The solve
// =============================================================================================

/// Runs the fit: one unweighted Levenberg-Marquardt solve, optionally followed by rounds of
/// iteratively reweighted least squares using MAGSAC++ weights.
///
/// The structure mirrors `geom2::align2::points_to_surface2`, for the same reasons. An initial
/// unweighted solve comes first because the noise scale has to be estimated from residuals, and
/// there are none until something has been fitted. Within each refinement round the weights are
/// held fixed, so that the jacobian stays consistent with the residual it differentiates.
///
/// An `Err` is reserved for having no answer at all: rejected options, a builder which fails on
/// the initial parameters, or an initial solve which broke down. A refinement round which breaks
/// down is rolled back to the previous round's parameters and reported on the result.
fn solve(
    fitting: &dyn BoundaryFittable,
    builder: &BndBuildFn,
    initial: DVector<f64>,
    opts: &BoundaryFitOptions,
) -> Result<BoundaryFitResult> {
    opts.validate()?;

    let lm = LevenbergMarquardt::new().with_patience(opts.patience);
    let n_params = initial.len();

    let problem = BoundaryFit::try_new(fitting, builder, initial)?;
    let (mut problem, termination) = run(&lm, problem);
    if !SolveQuality::from_termination(&termination).is_usable() {
        return Err(format!("Fitting failed: {termination:?}").into());
    }

    let mut solves = vec![termination];
    let mut halt = None;

    if opts.refinement_steps > 0 {
        match resolve_sigma_max(opts, &problem) {
            None => halt = Some(RefinementHalt::NoNoiseEstimate),
            Some(sigma_max) => {
                let weighting = MagsacWeight::new(sigma_max, RESIDUAL_DOF);

                for _ in 0..opts.refinement_steps {
                    let weighted = problem.count_if_reweighted(&weighting);
                    if weighted < n_params {
                        halt = Some(RefinementHalt::Underdetermined {
                            weighted,
                            params: n_params,
                        });
                        break;
                    }

                    let last_good = problem.params.clone();
                    problem.apply_magsac_weights(&weighting);

                    let (next, termination) = run(&lm, problem);
                    problem = next;

                    if !SolveQuality::from_termination(&termination).is_usable() {
                        problem.restore(&last_good);
                        halt = Some(RefinementHalt::SolveFailed(termination));
                        break;
                    }
                    solves.push(termination);
                }
            }
        }
    }

    let residuals = problem
        .residuals
        .clone()
        .ok_or("the fitted parameters do not produce a valid boundary")?;

    Ok(BoundaryFitResult::new(
        problem.params,
        residuals,
        solves,
        halt,
    ))
}

fn run<'a>(
    lm: &LevenbergMarquardt<f64>,
    problem: BoundaryFit<'a>,
) -> (BoundaryFit<'a>, TerminationReason) {
    let (result, report) = lm.minimize(problem);
    (result, report.termination)
}

fn resolve_sigma_max(opts: &BoundaryFitOptions, problem: &BoundaryFit<'_>) -> Option<f64> {
    match opts.sigma_max {
        Some(s) => Some(s),
        None => estimate_sigma_max(problem.residuals.as_ref()?.as_slice()),
    }
}

pub trait BoundaryFittable {
    /// Given a boundary, this should return two equally sized `DVector`s of the residuals and
    /// the weights. The weights are not calculated independently during the jabcobian, but simply
    /// are reused from this step. Return the _un-weighted_ residuals, and the weights that will
    /// be used to scale them. For any element `i` in the residual vector, the value of
    /// `residual_only(i, ...)` should be identical.
    ///
    /// Return in the order: `(residuals, weights)`
    fn residuals_and_weights(&self, boundary: &Boundary2) -> (DVector<f64>, DVector<f64>);

    /// Given a boundary, this should return the residual for sample `sample_i`. This step is used
    /// for the numerical jacobians. It should produce values identical to the ones from
    /// `residuals_and_weights`
    fn residual_only(&self, sample_i: usize, boundary: &Boundary2) -> f64;
}

struct BoundaryFit<'a> {
    fitting: &'a dyn BoundaryFittable,
    params: DVector<f64>,
    builder: &'a BndBuildFn,
    current: Option<Boundary2>,
    residuals: Option<DVector<f64>>,

    /// The geometric weight of each sample, which depends on where the sample projects and so is
    /// recomputed whenever the parameters move.
    weights: Option<DVector<f64>>,

    /// The robust weight of each sample, held fixed for the whole of a solve and refreshed only
    /// between them. Reweighting inside a solve would leave the jacobian differentiating a
    /// different objective than the residual reports.
    magsac_weights: DVector<f64>,
}

impl<'a> BoundaryFit<'a> {
    fn try_new(
        fitting: &'a dyn BoundaryFittable,
        builder: &'a BndBuildFn,
        initial: DVector<f64>,
    ) -> Result<Self> {
        // Check to make sure that the initial value doesn't fail
        let _ = builder(&initial)?;

        let mut problem = BoundaryFit {
            fitting,
            params: DVector::zeros(initial.len()),
            builder,
            current: None,
            residuals: None,
            weights: None,
            magsac_weights: DVector::zeros(0),
        };

        problem.set_params(&initial);

        // Sized once the first evaluation has told us how many samples there are, and left at one
        // apiece so that the opening solve is unweighted.
        let n = problem.residuals.as_ref().map_or(0, |r| r.len());
        problem.magsac_weights = DVector::from_element(n, 1.0);

        Ok(problem)
    }

    /// The factor applied to both the residual and the jacobian row of a sample.
    ///
    /// The geometric weight enters linearly and the robust weight as a square root, which is not
    /// an oversight. A residual scaled by `w` contributes `w^2 r^2` to the objective, so the
    /// square root is what makes a MAGSAC weight mean what it says. The geometric weight predates
    /// that and is left alone: it is a caller-facing knob whose established behavior is a scale on
    /// the residual, and quietly changing it would move every existing fit.
    fn scale(&self, weights: &DVector<f64>, i: usize) -> f64 {
        weights[i] * self.magsac_weights[i].sqrt()
    }

    /// Recomputes the robust weights from the current residuals.
    fn apply_magsac_weights(&mut self, weighting: &MagsacWeight) {
        if let Some(residuals) = &self.residuals {
            for i in 0..residuals.len() {
                self.magsac_weights[i] = weighting.weight(residuals[i].abs());
            }
        }
    }

    /// How many samples would still carry weight after reweighting. A round which would leave
    /// fewer of them than there are parameters is rank-deficient and must not be run.
    ///
    /// The `.abs()` on the geometric weight is not redundant: under `VecDot::AsIs` the weight is
    /// a raw dot product and can be negative, and a negative weight still gives its sample
    /// influence because the sign cancels in the squared objective.
    fn count_if_reweighted(&self, weighting: &MagsacWeight) -> usize {
        let (Some(residuals), Some(weights)) = (&self.residuals, &self.weights) else {
            return 0;
        };
        (0..residuals.len())
            .filter(|&i| weights[i].abs() * weighting.weight(residuals[i].abs()) > 0.0)
            .count()
    }

    /// Puts the parameters back to a previous state and rebuilds the fit to match.
    fn restore(&mut self, params: &DVector<f64>) {
        self.set_params(params);
    }
}

impl LeastSquaresProblem<f64, Dyn, Dyn> for BoundaryFit<'_> {
    type ResidualStorage = Owned<f64, Dyn, U1>;
    type JacobianStorage = Owned<f64, Dyn, Dyn>;
    type ParameterStorage = Owned<f64, Dyn>;

    fn set_params(&mut self, x: &Vector<f64, Dyn, Self::ParameterStorage>) {
        self.params = x.clone();

        if let Ok(boundary) = (self.builder)(&self.params) {
            let (residuals, weights) = self.fitting.residuals_and_weights(&boundary);
            self.residuals = Some(residuals);
            self.weights = Some(weights);
            self.current = Some(boundary);
        } else {
            self.residuals = None;
            self.current = None;
        }
    }

    fn params(&self) -> Vector<f64, Dyn, Self::ParameterStorage> {
        self.params.clone()
    }

    fn residuals(&self) -> Option<Vector<f64, Dyn, Self::ResidualStorage>> {
        let Some(residuals) = &self.residuals else {
            return None;
        };
        let Some(weights) = &self.weights else {
            return None;
        };
        let mut res = DVector::zeros(residuals.len());
        for i in 0..residuals.len() {
            res[i] = residuals[i] * self.scale(weights, i);
        }

        Some(res)
    }

    fn jacobian(&self) -> Option<Matrix<f64, Dyn, Dyn, Self::JacobianStorage>> {
        let Some(residuals) = &self.residuals else {
            return None;
        };
        let Some(weights) = &self.weights else {
            return None;
        };

        let mut jac = Matrix::<f64, Dyn, Dyn, Self::JacobianStorage>::zeros(
            residuals.len(),
            self.params.len(),
        );

        for k in 0..self.params.len() {
            let mut params = self.params.clone();
            params[k] += DELTA;
            let Ok(disturbed) = (self.builder)(&params) else {
                continue;
            };

            for i in 0..residuals.len() {
                let d = self.fitting.residual_only(i, &disturbed);
                jac[(i, k)] = self.scale(weights, i) * (d - residuals[i]) / DELTA;
            }
        }

        Some(jac)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Vector2;
    use crate::common::linear_space;
    use crate::common::points::{fill_gaps, to_points};
    use crate::common::random_geometry::RandomGeometry2;
    use crate::geom2::{BoundaryData2, BoundaryEditor};
    use approx::assert_relative_eq;

    /// A closed counter-clockwise unit square from (0,0) to (1,1). Boundary normals point to the
    /// right of the tangent, which for counter-clockwise winding means outward.
    fn ccw_square() -> Boundary2 {
        let mut bdata = BoundaryData2::new_closed();
        let mut cursor = bdata.get_cursor(None);
        cursor.add_seg_xy(0.0, 0.0);
        cursor.add_seg_xy(1.0, 0.0);
        cursor.add_seg_xy(1.0, 1.0);
        cursor.add_seg_xy(0.0, 1.0);
        bdata.try_to_boundary().unwrap()
    }

    #[test]
    fn the_residual_is_signed_by_which_side_of_the_boundary_a_sample_falls_on() {
        let square = ccw_square();

        let outside = Point2::new(0.5, -0.25);
        let inside = Point2::new(0.5, 0.25);

        let (_, m_out) = square.at_closest_to_point(&outside);
        let (_, m_in) = square.at_closest_to_point(&inside);

        let r_out = signed_residual(&m_out, &outside);
        let r_in = signed_residual(&m_in, &inside);

        assert!(
            r_out > 0.0,
            "a point outside should be positive, got {r_out}"
        );
        assert!(r_in < 0.0, "a point inside should be negative, got {r_in}");

        // The magnitude is still the plain distance to the boundary.
        assert_relative_eq!(r_out.abs(), 0.25, epsilon = 1e-12);
        assert_relative_eq!(r_in.abs(), 0.25, epsilon = 1e-12);
    }

    #[test]
    fn signing_leaves_the_size_of_the_residual_alone() {
        // The objective squares the residual, so signing it cannot move the minimum. This pins
        // that down directly: the magnitude at every sample matches the unsigned distance the
        // residual used to be.
        let square = ccw_square();
        let samples = fill_gaps(
            &to_points(&[
                [-0.4, -0.4],
                [1.4, -0.4],
                [1.4, 1.4],
                [-0.4, 1.4],
                [-0.4, -0.4],
            ]),
            0.1,
        );

        for p in &samples {
            let (_, m) = square.at_closest_to_point(p);
            assert_relative_eq!(signed_residual(&m, p).abs(), dist(&m, p), epsilon = 1e-12);
        }
    }

    // =========================================================================================
    // Robust refinement
    // =========================================================================================

    /// The triangle fixture of `simple_triangle`, as (points, builder, initial).
    fn triangle_case() -> (Vec<Point2>, BndBuildFn, DVector<f64>) {
        let corners = to_points(&[[1.0, 1.0], [3.0, 2.0], [2.0, 4.0], [1.0, 1.0]]);
        let points = fill_gaps(&corners, 0.1);

        let builder: BndBuildFn = Box::new(|params: &DVector<f64>| {
            let mut bdata = BoundaryData2::new_open(Point2::new(params[0], params[1]));
            let mut cursor = bdata.get_cursor(None);
            cursor.add_seg_xy(params[2], params[3]);
            cursor.add_seg_xy(params[4], params[5]);
            cursor.add_seg_xy(params[0], params[1]);
            bdata.try_to_boundary()
        });

        let initial = DVector::from(vec![0.9, 0.9, 3.1, 2.1, 2.1, 4.1]);
        (points, builder, initial)
    }

    #[test]
    fn gross_outliers_are_rejected() {
        // Clean samples carrying a little measurement noise, plus a tenth of them thrown well
        // clear of the triangle. The plain fit has no defense against the strays; the robust one
        // should weight them out and land close to the truth.
        //
        // The noise on the inliers is not decoration. MAGSAC separates outliers from inliers by
        // a noise scale estimated from the residuals, so there has to be a visible inlier scale
        // for it to find. Perfectly clean data plus large strays is the pathological case: the
        // opening unweighted solve is dragged, and the only spread left to measure is the damage
        // itself. That is the same trap `MultiOptions2::max_distance` exists to avoid.
        let (mut points, builder, initial) = triangle_case();

        let mut rg = RandomGeometry2::from_seed(0xb0_11d);
        for p in points.iter_mut() {
            *p += Vector2::new(rg.gaussian_f64(0.0, 0.01), rg.gaussian_f64(0.0, 0.01));
        }
        for (k, p) in points.iter_mut().enumerate() {
            if k.is_multiple_of(10) {
                *p += Vector2::new(0.0, 0.5);
            }
        }

        let truth = DVector::from(vec![1.0, 1.0, 3.0, 2.0, 2.0, 4.0]);
        let err = |r: &BoundaryFitResult| (&r.params - &truth).norm();

        let naive = fit_boundary_to_points(
            &points,
            &builder,
            initial.clone(),
            false,
            &Default::default(),
        )
        .unwrap();
        let robust = fit_boundary_to_points(
            &points,
            &builder,
            initial.clone(),
            false,
            &BoundaryFitOptions::robust(),
        )
        .unwrap();

        assert!(
            err(&robust) < 0.25 * err(&naive),
            "robust weighting should have suppressed the outliers: naive {}, robust {}",
            err(&naive),
            err(&robust)
        );
        assert_eq!(robust.refinement_rounds(), 4);
        assert!(robust.halt().is_none());

        // A noise bound supplied outright does as well, and does not depend on the estimate being
        // taken from a solve the outliers have already influenced.
        let explicit = fit_boundary_to_points(
            &points,
            &builder,
            initial,
            false,
            &BoundaryFitOptions {
                sigma_max: Some(0.02),
                ..BoundaryFitOptions::robust()
            },
        )
        .unwrap();
        assert!(err(&explicit) < 0.25 * err(&naive));
    }

    #[test]
    fn refinement_is_off_by_default() {
        let (points, builder, initial) = triangle_case();
        let result =
            fit_boundary_to_points(&points, &builder, initial, false, &Default::default()).unwrap();

        assert_eq!(result.refinement_rounds(), 0);
        assert_eq!(result.solves().len(), 1);
        assert!(result.halt().is_none());
    }

    #[test]
    fn refinement_on_essentially_exact_data_leaves_the_fit_alone() {
        // The fixture samples sit on the triangle outright, so a converged fit leaves residuals
        // at the level of floating point noise. Reweighting has nothing to find, and the rounds
        // must not disturb an answer that is already right.
        //
        // Worth knowing: the noise estimate does not bail out here. It only rejects a spread that
        // is zero or non-finite, and residuals of order 1e-16 are neither, so the rounds run on
        // numerical noise rather than reporting `RefinementHalt::NoNoiseEstimate`. That is shared
        // behavior with every alignment solver, not something specific to boundary fitting.
        let (points, builder, initial) = triangle_case();
        let result = fit_boundary_to_points(
            &points,
            &builder,
            initial,
            false,
            &BoundaryFitOptions::robust(),
        )
        .unwrap();

        let truth = DVector::from(vec![1.0, 1.0, 3.0, 2.0, 2.0, 4.0]);
        assert_relative_eq!(result.params, truth, epsilon = 1.0e-5);
        assert!(
            result.halt().is_none(),
            "unexpected halt: {:?}",
            result.halt()
        );
    }

    #[test]
    fn a_patience_exhausted_initial_solve_is_reported_rather_than_raised() {
        // Exhausting the evaluation budget leaves behind the best parameters the solver found,
        // which is an answer whose convergence was not demonstrated, not the absence of an
        // answer. The gate here has to match the alignment solvers, which classify
        // `LostPatience` as usable (see `SolveQuality`).
        let (mut points, builder, initial) = triangle_case();

        let mut rg = RandomGeometry2::from_seed(0xb0_11d);
        for p in points.iter_mut() {
            *p += Vector2::new(rg.gaussian_f64(0.0, 0.01), rg.gaussian_f64(0.0, 0.01));
        }

        // A patience of one allows seven evaluations of a six-parameter problem, which cannot
        // reach convergence from an initial guess this far off.
        let opts = BoundaryFitOptions {
            patience: 1,
            ..Default::default()
        };
        let result = fit_boundary_to_points(&points, &builder, initial, false, &opts).unwrap();

        assert_eq!(result.quality(), SolveQuality::Unconverged);
        assert!(!result.converged());
    }

    #[test]
    fn invalid_options_are_rejected() {
        let (points, builder, initial) = triangle_case();

        let bad_patience = BoundaryFitOptions {
            patience: 0,
            ..Default::default()
        };
        assert!(
            fit_boundary_to_points(&points, &builder, initial.clone(), false, &bad_patience)
                .is_err()
        );

        let bad_sigma = BoundaryFitOptions {
            sigma_max: Some(-1.0),
            ..BoundaryFitOptions::robust()
        };
        assert!(fit_boundary_to_points(&points, &builder, initial, false, &bad_sigma).is_err());
    }

    /// This and `surface_normal_line` assert converged parameters to 1e-6, so between them they
    /// are the regression net for the claim that signing the residual does not move a fit.
    #[test]
    fn simple_triangle() {
        let corners = to_points(&[[1.0, 1.0], [3.0, 2.0], [2.0, 4.0], [1.0, 1.0]]);
        let points = fill_gaps(&corners, 0.1);

        let builder: BndBuildFn = Box::new(|params: &DVector<f64>| {
            let mut bdata = BoundaryData2::new_open(Point2::new(params[0], params[1]));
            let mut cursor = bdata.get_cursor(None);
            cursor.add_seg_xy(params[2], params[3]);
            cursor.add_seg_xy(params[4], params[5]);
            cursor.add_seg_xy(params[0], params[1]);
            bdata.try_to_boundary()
        });

        let initial = DVector::from(vec![0.0, 0.0, 4.0, 0.0, 1.0, 7.0]);
        let result =
            fit_boundary_to_points(&points, &builder, initial, false, &Default::default()).unwrap();
        let expected = DVector::from(vec![1.0, 1.0, 3.0, 2.0, 2.0, 4.0]);
        assert_relative_eq!(result.params, expected, epsilon = 1.0e-6);
    }

    #[test]
    fn surface_normal_line() {
        let good = linear_space(0.0, 10.0, 10)
            .iter()
            .map(|x| SurfacePoint2::new(Point2::new(*x, 0.0), Vector2::y_axis()))
            .collect::<Vec<_>>();
        let bad = linear_space(0.0, 10.0, 10)
            .iter()
            .map(|x| SurfacePoint2::new(Point2::new(*x, 1.0), Vector2::x_axis()))
            .collect::<Vec<_>>();

        let samples = [good, bad].concat();

        let builder: BndBuildFn = Box::new(|params: &DVector<f64>| {
            let mut bdata = BoundaryData2::new_open(Point2::new(0.0, params[0]));
            let mut cursor = bdata.get_cursor(None);
            cursor.add_seg_xy(10.0, params[1]);
            bdata.try_to_boundary()
        });
        let initial = DVector::from(vec![1.0, 1.0]);

        let result = fit_boundary_to_surface_points(
            &samples,
            &builder,
            initial,
            VecDot::Abs,
            false,
            &Default::default(),
        )
        .unwrap();
        let expected = DVector::from(vec![0.0, 0.0]);
        assert_relative_eq!(result.params, expected, epsilon = 1.0e-6);
    }
}