Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / measure / src / main / rust / tests / vio_math.rs
vio_math.rs160 lines · 6.1 KiBRaw
| 1 | //! Host-side integration tests for the `measure_vio` pure math pipeline. |
| 2 | //! |
| 3 | //! The per-module unit tests live inline next to the code; these tests pin the |
| 4 | //! public API end to end on the host. Everything here avoids the Android-gated |
| 5 | //! JNI session map in `lib.rs`: a known 3D point is projected into synthetic |
| 6 | //! views and must triangulate back within a tight epsilon, epipolar residuals |
| 7 | //! must vanish for exact correspondences, and the `VioSession` query helpers |
| 8 | //! are checked for centring, clipping and initialising-state gating. |
| 9 | |
| 10 | use measure_vio::align::{align_visual_inertial, refine_gravity}; |
| 11 | use measure_vio::epipolar::{essential_eight_point, recover_pose, sampson_distance, NPt}; |
| 12 | use measure_vio::plane::fit_plane_ransac; |
| 13 | use measure_vio::preintegrate::{preintegrate, ImuSample, GRAVITY}; |
| 14 | use measure_vio::triangulate::{triangulate, CameraPose}; |
| 15 | use measure_vio::vio::{Intrinsics, Quality, VioSession}; |
| 16 | use vision_core::camera::rodrigues_to_mat; |
| 17 | use vision_core::imgbuf::Gray; |
| 18 | use vision_core::linalg::Vector3; |
| 19 | |
| 20 | fn intr() -> Intrinsics { |
| 21 | Intrinsics { fx: 500.0, fy: 500.0, cx: 320.0, cy: 240.0 } |
| 22 | } |
| 23 | |
| 24 | fn project(pose: &CameraPose, p: Vector3<f64>) -> NPt { |
| 25 | let c = pose.r * p + pose.t; |
| 26 | (c[0] / c[2], c[1] / c[2]) |
| 27 | } |
| 28 | |
| 29 | /// Twenty non-coplanar bearings from a sideways step plus a small yaw, with |
| 30 | /// the true translation returned for direction checks. |
| 31 | fn grid_scene() -> (Vec<NPt>, Vec<NPt>, Vector3<f64>) { |
| 32 | let a = CameraPose::identity(); |
| 33 | let t_true = Vector3::new(-0.25, 0.03, 0.01); |
| 34 | let b = CameraPose { r: rodrigues_to_mat(Vector3::new(0.0, 0.06, 0.0)), t: t_true }; |
| 35 | let mut aa = Vec::new(); |
| 36 | let mut bb = Vec::new(); |
| 37 | for ix in 0..5 { |
| 38 | for iy in 0..4 { |
| 39 | let depth = 3.0 + (ix + iy) as f64 * 0.25; |
| 40 | let p = Vector3::new(ix as f64 * 0.5 - 1.0, iy as f64 * 0.5 - 0.75, depth); |
| 41 | aa.push(project(&a, p)); |
| 42 | bb.push(project(&b, p)); |
| 43 | } |
| 44 | } |
| 45 | (aa, bb, t_true) |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn triangulate_known_point_with_yawed_second_view() { |
| 50 | let a = CameraPose::identity(); |
| 51 | let b = CameraPose { |
| 52 | r: rodrigues_to_mat(Vector3::new(0.0, 0.2, 0.0)), |
| 53 | t: Vector3::new(-0.3, 0.05, 0.0), |
| 54 | }; |
| 55 | let truth = Vector3::new(0.4, -0.2, 5.0); |
| 56 | let views = vec![(a.clone(), project(&a, truth)), (b.clone(), project(&b, truth))]; |
| 57 | let got = triangulate(&views).expect("well-posed pair must triangulate"); |
| 58 | assert!((got - truth).norm() < 1e-6, "error was {}", (got - truth).norm()); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn epipolar_residuals_are_tiny_for_exact_correspondences() { |
| 63 | let (a, b, _) = grid_scene(); |
| 64 | let e = essential_eight_point(&a, &b).expect("twenty points must yield a matrix"); |
| 65 | for i in 0..a.len() { |
| 66 | assert!(sampson_distance(&e, a[i], b[i]) < 1e-10, "pair {i} must agree with E"); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn epipolar_pose_recovery_finds_the_translation_direction() { |
| 72 | let (a, b, t_true) = grid_scene(); |
| 73 | let (pose, inliers) = recover_pose(&a, &b, 200, 1e-6).expect("pose must recover"); |
| 74 | assert!(inliers.len() >= 18, "inliers: {}", inliers.len()); |
| 75 | let est = pose.t / pose.t.norm(); |
| 76 | let want = t_true / t_true.norm(); |
| 77 | assert!(est.dot(&want).abs() > 0.98, "recovered direction must match the step"); |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn plane_fit_recovers_a_noisy_floor() { |
| 82 | let mut seed = 4242u64; |
| 83 | let mut next = || { |
| 84 | seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1); |
| 85 | ((seed >> 33) as f64 / (1u64 << 31) as f64) - 0.5 |
| 86 | }; |
| 87 | let pts: Vec<Vector3<f64>> = (0..80) |
| 88 | .map(|_| Vector3::new(next() * 4.0, next() * 4.0, -1.5 + next() * 0.004)) |
| 89 | .collect(); |
| 90 | let plane = fit_plane_ransac(&pts, 200, 0.02, None, 0.3).expect("floor must fit"); |
| 91 | assert!(plane.normal[2].abs() > 0.99, "normal: {:?}", plane.normal); |
| 92 | assert!(plane.distance(&Vector3::new(0.0, 0.0, -1.5)).abs() < 0.02); |
| 93 | let up = Vector3::new(0.0, 0.0, 1.0); |
| 94 | assert!(plane.tilt_from(&up) < 0.05); |
| 95 | let q = plane.project(&Vector3::new(1.0, 2.0, 5.0)); |
| 96 | assert!(plane.distance(&q).abs() < 1e-9); |
| 97 | } |
| 98 | |
| 99 | fn imu_series(n: usize, accel: Vector3<f64>) -> Vec<ImuSample> { |
| 100 | (0..n) |
| 101 | .map(|i| ImuSample { |
| 102 | t_ns: i as i64 * 2_500_000, |
| 103 | gyro: Vector3::zeros(), |
| 104 | accel, |
| 105 | }) |
| 106 | .collect() |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn preintegration_matches_constant_acceleration_kinematics() { |
| 111 | let samples = imu_series(401, Vector3::new(2.0, 0.0, 0.0)); |
| 112 | let pre = preintegrate(&samples, Vector3::zeros(), Vector3::zeros()); |
| 113 | assert!((pre.dt - 1.0).abs() < 1e-6, "dt: {}", pre.dt); |
| 114 | assert!((pre.delta_v[0] - 2.0).abs() < 1e-6); |
| 115 | assert!((pre.delta_p[0] - 1.0).abs() < 1e-3); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn preintegration_identity_and_bias_cases() { |
| 120 | let empty = preintegrate(&[], Vector3::zeros(), Vector3::zeros()); |
| 121 | assert_eq!(empty.sample_count, 0); |
| 122 | assert!(empty.dt.abs() < 1e-12); |
| 123 | let bias = Vector3::new(0.5, -0.2, 0.1); |
| 124 | let samples = imu_series(401, bias); |
| 125 | let cancelled = preintegrate(&samples, Vector3::zeros(), bias); |
| 126 | assert!(cancelled.delta_v.norm() < 1e-9); |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | fn align_helpers_reject_degenerate_input_and_normalise_gravity() { |
| 131 | assert!(align_visual_inertial(&[], &[]).is_none()); |
| 132 | let g = refine_gravity(Vector3::new(1.0, 2.0, -9.0)); |
| 133 | assert!((g.norm() - GRAVITY).abs() < 1e-9); |
| 134 | assert!(g[2] < 0.0); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn project_to_screen_centers_clips_and_rejects_behind() { |
| 139 | let s = VioSession::new(intr()); |
| 140 | let centre = s.project_to_screen(&[Vector3::new(0.0, 0.0, 2.0)], 640.0, 480.0); |
| 141 | assert_eq!(centre.len(), 1); |
| 142 | assert!((centre[0].0 - 0.5).abs() < 1e-6); |
| 143 | assert!((centre[0].1 - 0.5).abs() < 1e-6); |
| 144 | assert!(centre[0].2); |
| 145 | let behind = s.project_to_screen(&[Vector3::new(0.0, 0.0, -1.0)], 640.0, 480.0); |
| 146 | assert!(!behind[0].2); |
| 147 | let outside = s.project_to_screen(&[Vector3::new(10.0, 0.0, 1.0)], 640.0, 480.0); |
| 148 | assert!(!outside[0].2); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn blank_frames_keep_a_new_session_initialising_and_unmeasurable() { |
| 153 | let mut s = VioSession::new(intr()); |
| 154 | let gray = Gray::new(320, 240); |
| 155 | assert_eq!(s.push_frame(&gray, 0), Quality::Initialising); |
| 156 | assert_eq!(s.push_frame(&gray, 33_000_000), Quality::Initialising); |
| 157 | assert_eq!(s.landmark_count(), 0); |
| 158 | assert!(!s.has_plane()); |
| 159 | assert!(s.ray_to_world(160.0, 120.0).is_none()); |
| 160 | } |