Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / camera / src / main / rust / tests / math_edges.rs
math_edges.rs78 lines · 3.1 KiBRaw
| 1 | //! Host-side math edges for the panorama stitcher. |
| 2 | //! |
| 3 | //! The crate's inline suite in `src/lib.rs` covers the full stitch and estimate paths against |
| 4 | //! `testdata/*.jpg`. These tests cover the pure math reachable through the public re-exports |
| 5 | //! (`camera`, `geometry`, `linalg`, `imgbuf`) with no JPEGs and no Android: Rodrigues round |
| 6 | //! trips, the intrinsic matrix, a translation homography, and image sampling behaviour. |
| 7 | |
| 8 | use camera_stitch::camera::{mat_to_rodrigues, rodrigues_to_mat, CameraParams}; |
| 9 | use camera_stitch::geometry::{find_homography_ransac, transfer_inliers}; |
| 10 | use camera_stitch::imgbuf::{to_gray, Rgba}; |
| 11 | use camera_stitch::linalg::{Matrix3, Vector3}; |
| 12 | |
| 13 | fn approx(a: f64, b: f64, tol: f64) -> bool { |
| 14 | (a - b).abs() <= tol |
| 15 | } |
| 16 | |
| 17 | #[test] |
| 18 | fn rodrigues_round_trips_small_rotation() { |
| 19 | let rvec = Vector3::new(0.1, 0.2, 0.3); |
| 20 | let mat = rodrigues_to_mat(rvec); |
| 21 | let back = mat_to_rodrigues(&mat); |
| 22 | assert!(approx(back.x, rvec.x, 1e-9), "x: {}", back.x); |
| 23 | assert!(approx(back.y, rvec.y, 1e-9), "y: {}", back.y); |
| 24 | assert!(approx(back.z, rvec.z, 1e-9), "z: {}", back.z); |
| 25 | } |
| 26 | |
| 27 | #[test] |
| 28 | fn rodrigues_identity_stays_identity() { |
| 29 | let mat = rodrigues_to_mat(Vector3::zeros()); |
| 30 | for r in 0..3 { |
| 31 | for c in 0..3 { |
| 32 | let want = if r == c { 1.0 } else { 0.0 }; |
| 33 | assert!(approx(mat[(r, c)], want, 1e-12), "m[{r}{c}]={}", mat[(r, c)]); |
| 34 | } |
| 35 | } |
| 36 | let back = mat_to_rodrigues(&Matrix3::identity()); |
| 37 | assert!(back.norm() < 1e-9, "norm {}", back.norm()); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn camera_k_matrix_has_expected_shape() { |
| 42 | let params = |
| 43 | CameraParams { focal: 800.0, aspect: 1.0, ppx: 320.0, ppy: 240.0, r: Matrix3::identity() }; |
| 44 | let k = params.k(); |
| 45 | assert!(approx(k[(0, 0)], 800.0, 1e-9)); |
| 46 | assert!(approx(k[(1, 1)], 800.0, 1e-9)); |
| 47 | assert!(approx(k[(0, 2)], 320.0, 1e-9)); |
| 48 | assert!(approx(k[(1, 2)], 240.0, 1e-9)); |
| 49 | assert!(approx(k[(2, 2)], 1.0, 1e-12)); |
| 50 | assert!(approx(k[(0, 1)], 0.0, 1e-12)); |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn homography_recovers_translation() { |
| 55 | let a = vec![(0.0f32, 0.0f32), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0), (5.0, 5.0), (15.0, 5.0)]; |
| 56 | let b: Vec<(f32, f32)> = a.iter().map(|(x, y)| (x + 3.0, y + 4.0)).collect(); |
| 57 | let (h, inliers) = find_homography_ransac(&a, &b, 200, 2.0).expect("homography"); |
| 58 | assert!(inliers >= 4, "inliers {inliers}"); |
| 59 | assert!(approx(h[(0, 2)], 3.0, 0.5), "tx {}", h[(0, 2)]); |
| 60 | assert!(approx(h[(1, 2)], 4.0, 0.5), "ty {}", h[(1, 2)]); |
| 61 | assert_eq!(transfer_inliers(&h, &a, &b, 2.0).len(), inliers); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn rgba_sample_resize_and_gray_behave() { |
| 66 | let mut img = Rgba::new(4, 4); |
| 67 | img.set(1, 1, [255, 0, 0, 255]); |
| 68 | assert_eq!(img.get(1, 1), [255, 0, 0, 255]); |
| 69 | assert!(img.sample(-1.0, 0.0).is_none()); |
| 70 | assert!(img.sample(99.0, 99.0).is_none()); |
| 71 | let clear = Rgba::new(2, 2); |
| 72 | assert!(clear.sample(0.5, 0.5).is_none(), "fully transparent has no data"); |
| 73 | let small = img.resized(2, 2); |
| 74 | assert_eq!((small.w, small.h), (2, 2)); |
| 75 | let gray = to_gray(&img); |
| 76 | assert_eq!((gray.w, gray.h), (4, 4)); |
| 77 | assert_eq!(gray.px[4 + 1], ((255u32 * 77) >> 8) as u8); |
| 78 | } |