apex-solver 1.5.0

High-performance nonlinear least squares optimization with Lie group support for SLAM and bundle adjustment
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
//! GPS measurement factor for pose-graph optimization.
//!
//! Implements a synchronous GPS error term connecting a robot pose `T_WS`
//! and a GPS-to-world frame transform `T_GW` to a 3D GPS position measurement.
//!
//! # Mathematical Formulation
//!
//! The GPS antenna is displaced from the sensor origin by a known lever arm
//! `r_SA` (sensor-to-antenna in sensor frame). The predicted antenna position
//! in the GPS frame is:
//!
//! ```text
//! t_antenna_W = t_WS + C_WS · r_SA
//! predicted   = C_GW · t_antenna_W + t_GW
//! e           = z_GPS − predicted
//! r           = sqrt_info · e
//! ```
//!
//! # Parameter Layout (2 blocks, 12 DOF total)
//!
//! ```text
//! params[0]: T_WS — robot pose in world frame (7D [tx,ty,tz,qw,qx,qy,qz], 6 DOF)
//! params[1]: T_GW — GPS-to-world frame transform (7D, 6 DOF)
//! ```
//!
//! # Jacobians (3×12)
//!
//! Using **right** SE3 perturbation `T ⊕ [δρ,δθ] → t + C·δρ`, `C → C·Exp(δθ)`:
//!
//! ```text
//! ∂e/∂δρ_WS = −C_GW · C_WS
//! ∂e/∂δθ_WS =  C_GW · C_WS · [r_SA]×
//! ∂e/∂δρ_GW = −C_GW
//! ∂e/∂δθ_GW =  C_GW · [t_antenna_W]×
//! ```

use apex_manifolds::LieGroup;
use apex_manifolds::se3::SE3;
use faer::prelude::ReborrowMut;
use nalgebra::{Matrix3, SMatrix, Vector3};

use crate::core::variable::ManifoldVariable;
use crate::factors::Factor;
use crate::factors::common::math::skew;
use crate::factors::common::validate::expect_block_sizes;
use crate::factors::imu::preintegration::ImuPreintegration;
use crate::factors::imu::types::{SpeedAndBias, SpeedAndBiasExt};

/// GPS synchronous factor: connects `T_WS` (robot pose) and `T_GW`
/// (GPS-to-world) to a 3D GPS position measurement.
pub struct GpsFactor {
    /// GPS measurement in the GPS frame [m].
    measurement: Vector3<f64>,
    /// Sensor-to-antenna lever arm in the sensor frame [m].
    antenna_offset: Vector3<f64>,
    /// Square-root information matrix (3×3 upper-triangular, `W` s.t. `Wᵀ W = Σ⁻¹`).
    sqrt_information: SMatrix<f64, 3, 3>,
}

impl GpsFactor {
    /// Create a GPS factor.
    ///
    /// # Arguments
    /// * `measurement` — GPS position in GPS frame [m]
    /// * `antenna_offset` — sensor-to-antenna lever arm in sensor frame [m]
    /// * `sqrt_information` — 3×3 square-root information matrix `W` so that
    ///   the noise covariance satisfies `W^T W = Σ⁻¹`
    pub fn new(
        measurement: Vector3<f64>,
        antenna_offset: Vector3<f64>,
        sqrt_information: SMatrix<f64, 3, 3>,
    ) -> Self {
        Self {
            measurement,
            antenna_offset,
            sqrt_information,
        }
    }

    /// Create a GPS factor with isotropic noise (scalar standard deviation).
    ///
    /// The information matrix is `(1/σ²)·I` and the square-root is `(1/σ)·I`.
    pub fn new_isotropic(
        measurement: Vector3<f64>,
        antenna_offset: Vector3<f64>,
        sigma: f64,
    ) -> Self {
        let sqrt_info = SMatrix::<f64, 3, 3>::identity() * (1.0 / sigma);
        Self::new(measurement, antenna_offset, sqrt_info)
    }
}

impl Factor for GpsFactor {
    /// Compute the weighted 3D residual and optional 3×12 Jacobian.
    fn linearize(
        &self,
        params: &[&[f64]],
        residual: &mut [f64],
        jacobian: Option<faer::mat::MatMut<'_, f64>>,
    ) {
        debug_assert_eq!(params.len(), 2, "GpsFactor expects 2 parameter blocks");
        debug_assert_eq!(params[0].len(), 7, "params[0] must be SE3 (7D)");
        debug_assert_eq!(params[1].len(), 7, "params[1] must be SE3 (7D)");

        let t_ws = SE3::from_param_slice(params[0]);
        let t_gw = SE3::from_param_slice(params[1]);

        let c_ws: Matrix3<f64> = t_ws.rotation_so3().rotation_matrix();
        let c_gw: Matrix3<f64> = t_gw.rotation_so3().rotation_matrix();
        let pos_ws = t_ws.translation();
        let pos_gw = t_gw.translation();

        // Antenna position in world frame
        let t_antenna_w: Vector3<f64> = pos_ws + c_ws * self.antenna_offset;

        // Predicted GPS measurement
        let predicted: Vector3<f64> = c_gw * t_antenna_w + pos_gw;

        // Unweighted residual
        let raw_err: Vector3<f64> = self.measurement - predicted;

        // Weighted residual (apply sqrt_info)
        let weighted_err = self.sqrt_information * raw_err;
        for i in 0..3 {
            residual[i] = weighted_err[i];
        }

        let Some(mut jac) = jacobian else {
            return;
        };

        // ── Jacobians ─────────────────────────────────────────────────────────
        //
        // Right SE3 perturbation:  T ⊕ [δρ, δθ]
        //   t → t + C·δρ  (to first order)
        //   C → C·(I + [δθ]×)
        //
        // For T_WS:
        //   t_antenna_W → (t_WS + C_WS·δρ_WS) + C_WS·(I+[δθ_WS]×)·r_SA
        //                = t_antenna_W + C_WS·δρ_WS - C_WS·[r_SA]×·δθ_WS
        //   predicted → predicted + C_GW·(C_WS·δρ_WS - C_WS·[r_SA]×·δθ_WS)
        //   ∂e/∂δρ_WS = −C_GW·C_WS
        //   ∂e/∂δθ_WS =  C_GW·C_WS·[r_SA]×
        //
        // For T_GW:
        //   predicted → C_GW·(I+[δθ_GW]×)·t_antenna_W + t_GW + C_GW·δρ_GW
        //             = predicted − C_GW·[t_antenna_W]×·δθ_GW + C_GW·δρ_GW
        //   ∂e/∂δρ_GW = −C_GW
        //   ∂e/∂δθ_GW =  C_GW·[t_antenna_W]×
        //
        // Each block is 3×3; full unweighted Jacobian is 3×12.
        // After weighting: J_weighted = sqrt_info · J_raw.

        let r_sa_hat = skew(&self.antenna_offset);
        let t_ant_hat = skew(&t_antenna_w);

        let de_drho_ws = -(c_gw * c_ws);
        let de_dtheta_ws = c_gw * c_ws * r_sa_hat;
        let de_drho_gw = -c_gw;
        let de_dtheta_gw = c_gw * t_ant_hat;

        let mut j_raw = SMatrix::<f64, 3, 12>::zeros();
        j_raw.fixed_view_mut::<3, 3>(0, 0).copy_from(&de_drho_ws);
        j_raw.fixed_view_mut::<3, 3>(0, 3).copy_from(&de_dtheta_ws);
        j_raw.fixed_view_mut::<3, 3>(0, 6).copy_from(&de_drho_gw);
        j_raw.fixed_view_mut::<3, 3>(0, 9).copy_from(&de_dtheta_gw);

        let j_weighted = self.sqrt_information * j_raw;
        for row in 0..3 {
            for col in 0..12 {
                *jac.rb_mut().get_mut(row, col) = j_weighted[(row, col)];
            }
        }
    }

    fn residual_dim(&self) -> usize {
        3
    }

    fn jacobian_shape(&self) -> (usize, usize) {
        (3, 12)
    }

    fn validate_variables(&self, variables: &[&dyn ManifoldVariable]) -> Result<(), String> {
        expect_block_sizes(
            variables,
            &[SE3::REP_SIZE, SE3::REP_SIZE],
            "GpsFactor expects [SE3 T_WS, SE3 T_GW]",
        )
    }

    /// This factor whitens with the 3×3 sqrt-information supplied at construction,
    /// so it must be registered with `NoiseModel::null()`.
    fn whitens_internally(&self) -> bool {
        true
    }
}

// ── GpsAsyncFactor ────────────────────────────────────────────────────────────

/// GPS asynchronous factor: extends [`GpsFactor`] for GPS measurements that
/// arrive at a time `t_g ≠ t_k` from the keyframe.
///
/// An IMU preintegration from `t_k` to `t_g` is used to propagate the robot
/// pose forward before evaluating the same 3D position error.
///
/// # Mathematical Formulation
///
/// ```text
/// Δt           = t_g − t_k
/// p_propagated = p_k + v_k · Δt − ½·g·Δt² + C_WS_k · Δp_body
/// C_WS_g       = C_WS_k · ΔR                          (preintegrated rotation)
/// t_antenna_W  = p_propagated + C_WS_g · r_SA
/// predicted    = C_GW · t_antenna_W + t_GW
/// e            = z_GPS − predicted
/// r            = sqrt_info · e
/// ```
///
/// Velocity `v_k` is in **world frame** (SpeedAndBias layout [0..2]).
/// `Δp_body` = `preintegration.acc_doubleintegral()` (body-frame position
/// increment). It is **gravity-free**, like every preintegrated quantity, so
/// the `−½·g·Δt²` term above reinstates gravity — exactly as the IMU factors do
/// when forming their gravity-corrected state.
///
/// # Parameter Layout (3 blocks, 21 DOF)
///
/// ```text
/// params[0]: T_WS  — SE3 at keyframe t_k (7D, 6 DOF)
/// params[1]: sb_k  — SpeedAndBias at t_k  (9D, 9 DOF)
/// params[2]: T_GW  — GPS-to-world         (7D, 6 DOF)
/// ```
///
/// # Jacobians (3×21)
///
/// ```text
/// ∂e/∂δρ_WS = −C_GW · C_WS_k
/// ∂e/∂δθ_WS =  C_GW · C_WS_k · [Δp_body + ΔR · r_SA]×
/// ∂e/∂v_k   = −C_GW · Δt · I₃
/// ∂e/∂b_g   = −C_GW · C_WS_k · dp_db_g
/// ∂e/∂b_a   = −C_GW · C_WS_k · c_doubleintegral
/// ∂e/∂δρ_GW = −C_GW
/// ∂e/∂δθ_GW =  C_GW · [t_antenna_W]×
/// ```
pub struct GpsAsyncFactor {
    /// GPS measurement in the GPS frame [m].
    measurement: Vector3<f64>,
    /// Sensor-to-antenna lever arm in the sensor frame [m].
    antenna_offset: Vector3<f64>,
    /// Square-root information matrix (3×3, `W` s.t. `Wᵀ W = Σ⁻¹`).
    sqrt_information: SMatrix<f64, 3, 3>,
    /// IMU preintegration from keyframe time `t_k` to GPS timestamp `t_g`.
    preintegration: ImuPreintegration,
}

impl GpsAsyncFactor {
    /// Create an asynchronous GPS factor.
    ///
    /// # Arguments
    /// * `measurement`      — GPS position in GPS frame [m]
    /// * `antenna_offset`   — sensor-to-antenna lever arm in sensor frame [m]
    /// * `sqrt_information` — 3×3 square-root information matrix
    /// * `preintegration`   — IMU preintegration from `t_k` to `t_g`
    pub fn new(
        measurement: Vector3<f64>,
        antenna_offset: Vector3<f64>,
        sqrt_information: SMatrix<f64, 3, 3>,
        preintegration: ImuPreintegration,
    ) -> Self {
        Self {
            measurement,
            antenna_offset,
            sqrt_information,
            preintegration,
        }
    }

    /// Create with isotropic noise (scalar standard deviation).
    pub fn new_isotropic(
        measurement: Vector3<f64>,
        antenna_offset: Vector3<f64>,
        sigma: f64,
        preintegration: ImuPreintegration,
    ) -> Self {
        let sqrt_info = SMatrix::<f64, 3, 3>::identity() * (1.0 / sigma);
        Self::new(measurement, antenna_offset, sqrt_info, preintegration)
    }
}

impl Factor for GpsAsyncFactor {
    /// Compute the weighted 3D residual and optional 3×21 Jacobian.
    fn linearize(
        &self,
        params: &[&[f64]],
        residual: &mut [f64],
        jacobian: Option<faer::mat::MatMut<'_, f64>>,
    ) {
        debug_assert_eq!(params.len(), 3, "GpsAsyncFactor expects 3 parameter blocks");
        debug_assert_eq!(params[0].len(), 7, "params[0] must be SE3 (7D)");
        debug_assert_eq!(params[1].len(), 9, "params[1] must be SpeedAndBias (9D)");
        debug_assert_eq!(params[2].len(), 7, "params[2] must be SE3 (7D)");

        let preint = &self.preintegration;

        let t_ws_k = SE3::from_param_slice(params[0]);
        let sb_k = SpeedAndBias::from_column_slice(params[1]);
        let t_gw = SE3::from_param_slice(params[2]);

        let c_ws_k: Matrix3<f64> = t_ws_k.rotation_so3().rotation_matrix();
        let c_gw: Matrix3<f64> = t_gw.rotation_so3().rotation_matrix();
        let p_k = t_ws_k.translation();
        let t_gw_pos = t_gw.translation();

        let v_k = sb_k.velocity(); // world frame

        let delta_t = preint.t1() - preint.t0();
        let delta_r: Matrix3<f64> = preint.delta_q().to_rotation_matrix().into_inner();

        // First-order bias correction: Δp_body ≈ Δp_nom + dp_db_g·δb_g + c_dbl·δb_a
        let b_g_ref = preint.speed_and_biases_ref().gyro_bias();
        let b_a_ref = preint.speed_and_biases_ref().accel_bias();
        let db_g = sb_k.gyro_bias() - b_g_ref;
        let db_a = sb_k.accel_bias() - b_a_ref;
        let delta_p_body = preint.acc_doubleintegral()
            + preint.dp_db_g() * db_g
            + preint.c_doubleintegral() * db_a;

        // Propagate position to GPS time.
        //
        // The preintegrated `acc_doubleintegral` is gravity-free — the IMU
        // factors add gravity when they build their gravity-corrected state —
        // so it has to be reinstated here too. Leaving it out biases the
        // predicted antenna position by ½·g·Δt², which is 0.44 m after only
        // 0.3 s and grows quadratically.
        let gravity = Vector3::new(0.0, 0.0, preint.imu_params().g);
        let p_propagated =
            p_k + v_k * delta_t - 0.5 * gravity * delta_t * delta_t + c_ws_k * delta_p_body;

        // Rotation at GPS time (first-order)
        let c_ws_g = c_ws_k * delta_r;

        // Antenna position in world frame
        let t_antenna_w = p_propagated + c_ws_g * self.antenna_offset;

        // Predicted GPS measurement
        let predicted = c_gw * t_antenna_w + t_gw_pos;

        // Unweighted residual
        let raw_err = self.measurement - predicted;

        // Weighted residual
        let weighted_err = self.sqrt_information * raw_err;
        for i in 0..3 {
            residual[i] = weighted_err[i];
        }

        let Some(mut jac) = jacobian else {
            return;
        };

        // ── Jacobians ─────────────────────────────────────────────────────────
        //
        // Right SE3 perturbation for T_WS(t_k): T → T ⊕ [δρ, δθ]
        //   p_k → p_k + C_WS_k · δρ
        //   C_WS_k → C_WS_k · (I + [δθ]×)
        //
        // Under this perturbation:
        //   p_propagated → p_propagated + C_WS_k·δρ − C_WS_k·[Δp_body]×·δθ
        //   C_WS_g·r_SA → C_WS_g·r_SA − C_WS_k·[ΔR·r_SA]×·δθ
        //   δt_antenna_W = C_WS_k·δρ − C_WS_k·([Δp_body]× + [ΔR·r_SA]×)·δθ
        //
        //   ∂e/∂δρ_WS = −C_GW·C_WS_k
        //   ∂e/∂δθ_WS =  C_GW·C_WS_k·[Δp_body + ΔR·r_SA]×
        //
        // For sb_k = [v, b_g, b_a]:
        //   p_propagated += v·Δt + C_WS_k·dp_db_g·δb_g + C_WS_k·c_dbl·δb_a
        //
        //   ∂e/∂v_k  = −C_GW · Δt · I₃
        //   ∂e/∂b_g  = −C_GW · C_WS_k · dp_db_g
        //   ∂e/∂b_a  = −C_GW · C_WS_k · c_doubleintegral
        //
        // For T_GW: same as GpsFactor, evaluated at propagated t_antenna_W:
        //   ∂e/∂δρ_GW = −C_GW
        //   ∂e/∂δθ_GW =  C_GW · [t_antenna_W]×

        let delta_r_r_sa = delta_r * self.antenna_offset;
        let theta_arm = skew(&(delta_p_body + delta_r_r_sa));
        let t_ant_hat = skew(&t_antenna_w);

        // Block 0: T_WS (cols 0–5)
        let de_drho_ws = -(c_gw * c_ws_k);
        let de_dtheta_ws = c_gw * c_ws_k * theta_arm;

        // Block 1: sb_k (cols 6–14)  [v(3) | b_g(3) | b_a(3)]
        let de_dv = -(c_gw * delta_t);
        let de_dbg = -(c_gw * c_ws_k * preint.dp_db_g());
        let de_dba = -(c_gw * c_ws_k * preint.c_doubleintegral());

        // Block 2: T_GW (cols 15–20)
        let de_drho_gw = -c_gw;
        let de_dtheta_gw = c_gw * t_ant_hat;

        let mut j_raw = SMatrix::<f64, 3, 21>::zeros();
        // T_WS block
        j_raw.fixed_view_mut::<3, 3>(0, 0).copy_from(&de_drho_ws);
        j_raw.fixed_view_mut::<3, 3>(0, 3).copy_from(&de_dtheta_ws);
        // sb_k block
        j_raw.fixed_view_mut::<3, 3>(0, 6).copy_from(&de_dv);
        j_raw.fixed_view_mut::<3, 3>(0, 9).copy_from(&de_dbg);
        j_raw.fixed_view_mut::<3, 3>(0, 12).copy_from(&de_dba);
        // T_GW block
        j_raw.fixed_view_mut::<3, 3>(0, 15).copy_from(&de_drho_gw);
        j_raw.fixed_view_mut::<3, 3>(0, 18).copy_from(&de_dtheta_gw);

        let j_weighted = self.sqrt_information * j_raw;
        for row in 0..3 {
            for col in 0..21 {
                *jac.rb_mut().get_mut(row, col) = j_weighted[(row, col)];
            }
        }
    }

    fn residual_dim(&self) -> usize {
        3
    }

    fn jacobian_shape(&self) -> (usize, usize) {
        (3, 21)
    }

    fn validate_variables(&self, variables: &[&dyn ManifoldVariable]) -> Result<(), String> {
        expect_block_sizes(
            variables,
            &[SE3::REP_SIZE, 9, SE3::REP_SIZE],
            "GpsAsyncFactor expects [SE3 T_WS, speed-and-bias (9D), SE3 T_GW]",
        )
    }

    /// This factor whitens with the 3×3 sqrt-information supplied at construction,
    /// so it must be registered with `NoiseModel::null()`.
    fn whitens_internally(&self) -> bool {
        true
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use apex_manifolds::LieGroup;
    use apex_manifolds::Tangent;
    use apex_manifolds::se3::{SE3, SE3Tangent};
    use nalgebra::{DMatrix, DVector, UnitQuaternion, Vector3};

    use crate::factors::imu::preintegration::ImuPreintegration;
    use crate::factors::imu::types::{
        ImuMeasurement, ImuParameters, ImuSensorReadings, SpeedAndBias,
    };

    fn identity_pose() -> DVector<f64> {
        DVector::from_vec(vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0])
    }

    fn make_pose(tx: f64, ty: f64, tz: f64, q: UnitQuaternion<f64>) -> DVector<f64> {
        let q = q.quaternion();
        DVector::from_vec(vec![tx, ty, tz, q.w, q.i, q.j, q.k])
    }

    fn perturb_se3(pose: &[f64], tangent: &[f64; 6]) -> DVector<f64> {
        let se3 = SE3::from_param_slice(pose);
        let tan = SE3Tangent::from_slice(tangent);
        DVector::from_column_slice(se3.right_plus(&tan, None, None).as_param_slice())
    }

    fn compute_residual_gps(factor: &GpsFactor, pose_ws: &[f64], pose_gw: &[f64]) -> Vec<f64> {
        let mut residual = vec![0.0f64; factor.residual_dim()];
        factor.linearize(&[pose_ws, pose_gw], &mut residual, None);
        residual
    }

    fn compute_with_jacobian_gps(
        factor: &GpsFactor,
        pose_ws: &[f64],
        pose_gw: &[f64],
    ) -> (Vec<f64>, DMatrix<f64>) {
        let (rows, cols) = factor.jacobian_shape();
        let mut residual = vec![0.0f64; rows];
        let mut jac_buf = vec![0.0f64; rows * cols];
        let jac_mut = faer::mat::MatMut::from_column_major_slice_mut(&mut jac_buf, rows, cols);
        factor.linearize(&[pose_ws, pose_gw], &mut residual, Some(jac_mut));
        let jacobian = DMatrix::from_column_slice(rows, cols, &jac_buf);
        (residual, jacobian)
    }

    fn compute_residual_async(
        factor: &GpsAsyncFactor,
        pose_ws: &[f64],
        sb: &[f64],
        pose_gw: &[f64],
    ) -> Vec<f64> {
        let mut residual = vec![0.0f64; factor.residual_dim()];
        factor.linearize(&[pose_ws, sb, pose_gw], &mut residual, None);
        residual
    }

    fn compute_with_jacobian_async(
        factor: &GpsAsyncFactor,
        pose_ws: &[f64],
        sb: &[f64],
        pose_gw: &[f64],
    ) -> (Vec<f64>, DMatrix<f64>) {
        let (rows, cols) = factor.jacobian_shape();
        let mut residual = vec![0.0f64; rows];
        let mut jac_buf = vec![0.0f64; rows * cols];
        let jac_mut = faer::mat::MatMut::from_column_major_slice_mut(&mut jac_buf, rows, cols);
        factor.linearize(&[pose_ws, sb, pose_gw], &mut residual, Some(jac_mut));
        let jacobian = DMatrix::from_column_slice(rows, cols, &jac_buf);
        (residual, jacobian)
    }

    // ── Test 1: zero residual when measurement equals prediction (no offset) ──

    #[test]
    fn zero_residual_when_antenna_at_origin() {
        // Identity poses, antenna at origin, measure exactly t_WS
        let t_ws_pos = Vector3::new(1.0, 2.0, 3.0);
        let pose_ws = make_pose(
            t_ws_pos.x,
            t_ws_pos.y,
            t_ws_pos.z,
            UnitQuaternion::identity(),
        );
        let pose_gw = identity_pose(); // C_GW=I, t_GW=0 → GPS frame == world frame

        let measurement = t_ws_pos; // No offset, no T_GW rotation
        let factor = GpsFactor::new_isotropic(measurement, Vector3::zeros(), 1.0);
        let r = compute_residual_gps(&factor, pose_ws.as_slice(), pose_gw.as_slice());

        for (i, ri) in r.iter().enumerate().take(3) {
            assert!(ri.abs() < 1e-12, "residual[{i}] = {} should be zero", ri);
        }
    }

    // ── Test 2: zero residual with nonzero antenna offset ─────────────────────

    #[test]
    fn zero_residual_with_antenna_offset() {
        let t_ws = Vector3::new(0.5, -1.0, 2.0);
        let r_sa = Vector3::new(0.1, 0.0, 0.05);

        // Build a small yaw rotation for T_WS
        let q_ws = UnitQuaternion::from_axis_angle(
            &nalgebra::Unit::new_normalize(Vector3::new(0.0, 0.0, 1.0)),
            0.3,
        );
        let c_ws = q_ws.to_rotation_matrix().into_inner();

        // T_GW: small rotation + translation
        let q_gw = UnitQuaternion::from_axis_angle(
            &nalgebra::Unit::new_normalize(Vector3::new(1.0, 0.0, 0.0)),
            0.1,
        );
        let t_gw_pos = Vector3::new(100.0, 200.0, 0.0);
        let c_gw = q_gw.to_rotation_matrix().into_inner();

        let t_antenna_w = t_ws + c_ws * r_sa;
        let measurement = c_gw * t_antenna_w + t_gw_pos;

        let pose_ws = make_pose(t_ws.x, t_ws.y, t_ws.z, q_ws);
        let pose_gw = make_pose(t_gw_pos.x, t_gw_pos.y, t_gw_pos.z, q_gw);

        let factor = GpsFactor::new_isotropic(measurement, r_sa, 1.0);
        let r = compute_residual_gps(&factor, pose_ws.as_slice(), pose_gw.as_slice());

        for (i, ri) in r.iter().enumerate().take(3) {
            assert!(ri.abs() < 1e-10, "residual[{i}] = {} should be zero", ri);
        }
    }

    // ── Test 3: finite-difference Jacobian check ──────────────────────────────

    #[test]
    fn finite_difference_jacobians() {
        let t_ws = Vector3::new(1.0, -0.5, 0.3);
        let r_sa = Vector3::new(0.15, 0.05, -0.02);

        let q_ws = UnitQuaternion::from_axis_angle(
            &nalgebra::Unit::new_normalize(Vector3::new(0.0, 1.0, 0.0)),
            0.25,
        );
        let c_ws = q_ws.to_rotation_matrix().into_inner();

        let q_gw = UnitQuaternion::from_axis_angle(
            &nalgebra::Unit::new_normalize(Vector3::new(0.0, 0.0, 1.0)),
            -0.15,
        );
        let t_gw_pos = Vector3::new(50.0, 30.0, -5.0);
        let c_gw = q_gw.to_rotation_matrix().into_inner();

        let t_antenna_w = t_ws + c_ws * r_sa;
        let measurement = c_gw * t_antenna_w + t_gw_pos;

        let pose_ws = make_pose(t_ws.x, t_ws.y, t_ws.z, q_ws);
        let pose_gw = make_pose(t_gw_pos.x, t_gw_pos.y, t_gw_pos.z, q_gw);

        let factor = GpsFactor::new_isotropic(measurement, r_sa, 0.5);
        let (r0, jac) = compute_with_jacobian_gps(&factor, pose_ws.as_slice(), pose_gw.as_slice());

        const EPS: f64 = 1e-6;
        const TOL: f64 = 1e-4;

        // Block 0: T_WS (6 DOF)
        for col in 0..6 {
            let mut tan = [0.0f64; 6];
            tan[col] = EPS;
            let pose_ws_p = perturb_se3(pose_ws.as_slice(), &tan);
            let r_pert = compute_residual_gps(&factor, pose_ws_p.as_slice(), pose_gw.as_slice());
            for row in 0..3 {
                let fd = (r_pert[row] - r0[row]) / EPS;
                let err = (fd - jac[(row, col)]).abs();
                assert!(
                    err < TOL,
                    "J_T_WS[{row},{col}]: analytical={:.6} fd={:.6} err={err:.2e}",
                    jac[(row, col)],
                    fd
                );
            }
        }

        // Block 1: T_GW (6 DOF)
        for col in 0..6 {
            let mut tan = [0.0f64; 6];
            tan[col] = EPS;
            let pose_gw_p = perturb_se3(pose_gw.as_slice(), &tan);
            let r_pert = compute_residual_gps(&factor, pose_ws.as_slice(), pose_gw_p.as_slice());
            for row in 0..3 {
                let fd = (r_pert[row] - r0[row]) / EPS;
                let err = (fd - jac[(row, 6 + col)]).abs();
                assert!(
                    err < TOL,
                    "J_T_GW[{row},{col}]: analytical={:.6} fd={:.6} err={err:.2e}",
                    jac[(row, 6 + col)],
                    fd
                );
            }
        }
    }

    // ── Helpers for async tests ───────────────────────────────────────────────

    /// Build a trivial preintegration with near-identity IMU (gravity-only accel,
    /// zero gyro) so the bias Jacobians are non-trivial but the result is known.
    /// The gravity-compensated, non-rotating IMU stream these tests integrate.
    fn make_measurements(dt: f64) -> Vec<ImuMeasurement> {
        let g = ImuParameters::default().g;
        let n = 21usize;
        let dt_step = dt / (n - 1) as f64;
        (0..n)
            .map(|i| ImuMeasurement {
                timestamp: i as f64 * dt_step,
                measurement: ImuSensorReadings {
                    gyroscopes: Vector3::zeros(),
                    accelerometers: Vector3::new(0.0, 0.0, g), // gravity compensation
                },
            })
            .collect()
    }

    fn make_preint(dt: f64, v_world: Vector3<f64>) -> ImuPreintegration {
        let params = ImuParameters::default();
        let measurements = make_measurements(dt);

        // Use sb = [v_world, 0, 0] as linearization point
        let mut sb = SpeedAndBias::zeros();
        sb[0] = v_world.x;
        sb[1] = v_world.y;
        sb[2] = v_world.z;

        ImuPreintegration::new(measurements, params, 0.0, dt, &sb)
    }

    fn perturb_sb(sb: &[f64], idx: usize, eps: f64) -> DVector<f64> {
        let mut out = DVector::from_column_slice(sb);
        out[idx] += eps;
        out
    }

    // ── Test 4: zero residual for async factor with trivial preintegration ────

    #[test]
    fn async_zero_residual() {
        let dt = 0.1;
        let v_k = Vector3::new(0.5, -0.2, 0.0);
        let preint = make_preint(dt, v_k);

        let t_ws_pos = Vector3::new(1.0, 2.0, 0.5);
        let q_ws = UnitQuaternion::identity();
        let r_sa = Vector3::zeros(); // no antenna offset for simplicity

        // Where the antenna actually is at the GPS timestamp, obtained by
        // *forward integrating* the same samples rather than by restating the
        // factor's own expression. Deriving the expectation independently is
        // the point: an earlier version of this test copied the prediction
        // formula, so it could not see that the formula was missing gravity.
        let params = ImuParameters::default();
        let mut pose_at_gps = SE3::new(t_ws_pos, q_ws);
        let mut sb_at_gps = {
            let mut sb = SpeedAndBias::zeros();
            sb[0] = v_k.x;
            sb[1] = v_k.y;
            sb[2] = v_k.z;
            sb
        };
        ImuPreintegration::propagation(
            &make_measurements(dt),
            &params,
            &mut pose_at_gps,
            &mut sb_at_gps,
            0.0,
            dt,
        );
        let t_antenna_w =
            pose_at_gps.translation() + pose_at_gps.rotation_so3().rotation_matrix() * r_sa;

        // GPS frame = world frame (identity T_GW)
        let measurement = t_antenna_w; // perfect measurement

        let pose_ws = make_pose(t_ws_pos.x, t_ws_pos.y, t_ws_pos.z, q_ws);
        let pose_gw = identity_pose();
        let mut sb_vec = DVector::zeros(9);
        sb_vec[0] = v_k.x;
        sb_vec[1] = v_k.y;
        sb_vec[2] = v_k.z;

        let factor = GpsAsyncFactor::new_isotropic(measurement, r_sa, 1.0, preint);
        let r = compute_residual_async(
            &factor,
            pose_ws.as_slice(),
            sb_vec.as_slice(),
            pose_gw.as_slice(),
        );

        for (i, ri) in r.iter().enumerate().take(3) {
            assert!(
                ri.abs() < 1e-6,
                "async residual[{i}] = {} should be near zero",
                ri
            );
        }
    }

    // ── Test 5: finite-difference Jacobian check for async factor ─────────────

    #[test]
    fn async_finite_difference_jacobians() {
        let dt = 0.05;
        let v_k = Vector3::new(0.3, -0.1, 0.05);
        let r_sa = Vector3::new(0.1, 0.0, 0.05);

        let q_ws = UnitQuaternion::from_axis_angle(
            &nalgebra::Unit::new_normalize(Vector3::new(0.0, 1.0, 0.0)),
            0.2,
        );
        let t_ws_pos = Vector3::new(1.0, -0.5, 0.3);
        let c_ws_k: nalgebra::Matrix3<f64> = q_ws.to_rotation_matrix().into_inner();

        let q_gw = UnitQuaternion::from_axis_angle(
            &nalgebra::Unit::new_normalize(Vector3::new(0.0, 0.0, 1.0)),
            -0.1,
        );
        let t_gw_pos = Vector3::new(50.0, 30.0, -2.0);
        let c_gw: nalgebra::Matrix3<f64> = q_gw.to_rotation_matrix().into_inner();

        let preint = make_preint(dt, v_k);

        // Build measurement from ground truth
        let delta_p_body = *preint.acc_doubleintegral();
        let delta_r: nalgebra::Matrix3<f64> = preint.delta_q().to_rotation_matrix().into_inner();
        let p_propagated = t_ws_pos + v_k * dt + c_ws_k * delta_p_body;
        let c_ws_g = c_ws_k * delta_r;
        let t_antenna_w = p_propagated + c_ws_g * r_sa;
        let measurement = c_gw * t_antenna_w + t_gw_pos;

        let pose_ws = make_pose(t_ws_pos.x, t_ws_pos.y, t_ws_pos.z, q_ws);
        let pose_gw = make_pose(t_gw_pos.x, t_gw_pos.y, t_gw_pos.z, q_gw);
        let mut sb_vec = DVector::zeros(9);
        sb_vec[0] = v_k.x;
        sb_vec[1] = v_k.y;
        sb_vec[2] = v_k.z;

        let make_factor =
            || GpsAsyncFactor::new_isotropic(measurement, r_sa, 0.5, make_preint(dt, v_k));

        let factor = make_factor();
        let (r0, jac) = compute_with_jacobian_async(
            &factor,
            pose_ws.as_slice(),
            sb_vec.as_slice(),
            pose_gw.as_slice(),
        );

        const EPS: f64 = 1e-6;
        const TOL: f64 = 1e-4;

        // Block 0: T_WS (6 DOF, cols 0–5)
        for col in 0..6 {
            let mut tan = [0.0f64; 6];
            tan[col] = EPS;
            let pose_ws_p = perturb_se3(pose_ws.as_slice(), &tan);
            let r_pert = compute_residual_async(
                &make_factor(),
                pose_ws_p.as_slice(),
                sb_vec.as_slice(),
                pose_gw.as_slice(),
            );
            for row in 0..3 {
                let fd = (r_pert[row] - r0[row]) / EPS;
                let err = (fd - jac[(row, col)]).abs();
                assert!(
                    err < TOL,
                    "async J_T_WS[{row},{col}]: analytical={:.6} fd={:.6} err={err:.2e}",
                    jac[(row, col)],
                    fd
                );
            }
        }

        // Block 1: sb_k (9 DOF, cols 6–14)
        for col in 0..9 {
            let sb_p = perturb_sb(sb_vec.as_slice(), col, EPS);
            let r_pert = compute_residual_async(
                &make_factor(),
                pose_ws.as_slice(),
                sb_p.as_slice(),
                pose_gw.as_slice(),
            );
            for row in 0..3 {
                let fd = (r_pert[row] - r0[row]) / EPS;
                let err = (fd - jac[(row, 6 + col)]).abs();
                assert!(
                    err < TOL,
                    "async J_sb[{row},{col}]: analytical={:.6} fd={:.6} err={err:.2e}",
                    jac[(row, 6 + col)],
                    fd
                );
            }
        }

        // Block 2: T_GW (6 DOF, cols 15–20)
        for col in 0..6 {
            let mut tan = [0.0f64; 6];
            tan[col] = EPS;
            let pose_gw_p = perturb_se3(pose_gw.as_slice(), &tan);
            let r_pert = compute_residual_async(
                &make_factor(),
                pose_ws.as_slice(),
                sb_vec.as_slice(),
                pose_gw_p.as_slice(),
            );
            for row in 0..3 {
                let fd = (r_pert[row] - r0[row]) / EPS;
                let err = (fd - jac[(row, 15 + col)]).abs();
                assert!(
                    err < TOL,
                    "async J_T_GW[{row},{col}]: analytical={:.6} fd={:.6} err={err:.2e}",
                    jac[(row, 15 + col)],
                    fd
                );
            }
        }
    }
}