Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / camera / src / main / rust / tests / pure_logic.rs

pure_logic.rs295 lines · 10.7 KiBRaw
1//! Synthetic-input tests for the crate's pure, deterministic logic.
2//!
3//! Everything here builds tiny in-memory images / matrices — no JPEG fixtures —
4//! and exercises the public surface that `src/lib.rs` re-exports or declares.
5
6use camera_stitch::camera::{rodrigues_to_mat, CameraParams};
7use camera_stitch::features::{Features, KeyPoint};
8use camera_stitch::imgbuf::Rgba;
9use camera_stitch::linalg::{Matrix3, Vector3};
10
11// The stitch pipeline modules are crate-private, so integration tests reach
12// the pure helpers through the crate root's internal paths only where they
13// are `pub`. That means most pipeline coverage lives in sibling
14// `#[cfg(test)]` modules next to the code; this file covers what the crate
15// publicly exposes: `camera`, `imgbuf`, `linalg`, `geometry`, `features`.
16
17fn approx(a: f64, b: f64, tol: f64) -> bool {
18 (a - b).abs() <= tol
19}
20
21fn solid(w: usize, h: usize, c: [u8; 4]) -> Rgba {
22 let mut img = Rgba::new(w, h);
23 for y in 0..h {
24 for x in 0..w {
25 img.set(x, y, c);
26 }
27 }
28 img
29}
30
31// ---------------------------------------------------------------- camera ---
32
33#[test]
34fn camera_k_uses_focal_times_aspect_for_y() {
35 let p =
36 CameraParams { focal: 600.0, aspect: 0.5, ppx: 10.0, ppy: 20.0, r: Matrix3::identity() };
37 let k = p.k();
38 assert!(approx(k[(0, 0)], 600.0, 1e-12));
39 assert!(approx(k[(1, 1)], 300.0, 1e-12), "fy = focal*aspect");
40 assert!(approx(k[(0, 2)], 10.0, 1e-12));
41 assert!(approx(k[(1, 2)], 20.0, 1e-12));
42}
43
44#[test]
45fn rodrigues_quarter_turn_about_z_maps_x_to_y() {
46 let r = rodrigues_to_mat(Vector3::new(0.0, 0.0, std::f64::consts::FRAC_PI_2));
47 let v = r * Vector3::new(1.0, 0.0, 0.0);
48 assert!(approx(v.x, 0.0, 1e-12), "x {}", v.x);
49 assert!(approx(v.y, 1.0, 1e-12), "y {}", v.y);
50 assert!(approx(v.z, 0.0, 1e-12));
51 // A rotation matrix is orthonormal with determinant +1.
52 assert!(approx(r.determinant(), 1.0, 1e-12));
53 let should_be_i = r * r.transpose();
54 for i in 0..3 {
55 for j in 0..3 {
56 let want = if i == j { 1.0 } else { 0.0 };
57 assert!(approx(should_be_i[(i, j)], want, 1e-12), "[{i}{j}]");
58 }
59 }
60}
61
62#[test]
63fn rodrigues_large_angle_round_trips_through_matrix() {
64 // 180° about X: mat_to_rodrigues takes the trace<=0 branch.
65 let rvec = Vector3::new(std::f64::consts::PI, 0.0, 0.0);
66 let m = rodrigues_to_mat(rvec);
67 assert!(approx(m[(1, 1)], -1.0, 1e-12));
68 assert!(approx(m[(2, 2)], -1.0, 1e-12));
69 let back = camera_stitch::camera::mat_to_rodrigues(&m);
70 assert!(approx(back.norm(), std::f64::consts::PI, 1e-9), "norm {}", back.norm());
71}
72
73// ---------------------------------------------------------------- imgbuf ---
74
75#[test]
76fn rgba_from_bytes_with_exact_size_round_trips() {
77 // from_bytes requires exactly w*h*4 bytes (enforced by debug_assert).
78 let img = Rgba::from_bytes(2, 1, vec![1, 2, 3, 4, 5, 6, 7, 8]);
79 assert_eq!(img.get(0, 0), [1, 2, 3, 4]);
80 assert_eq!(img.get(1, 0), [5, 6, 7, 8]);
81 let zero = Rgba::from_bytes(0, 0, vec![]);
82 assert_eq!((zero.w, zero.h, zero.px.len()), (0, 0, 0));
83}
84
85#[test]
86fn rgba_get_reads_written_pixels() {
87 let img = solid(2, 2, [7, 8, 9, 255]);
88 assert_eq!(img.get(0, 0), [7, 8, 9, 255]);
89 assert_eq!(img.get(1, 1), [7, 8, 9, 255]);
90}
91
92#[test]
93#[should_panic]
94fn rgba_get_out_of_bounds_panics() {
95 // Documented current behaviour: get() indexes unchecked, so callers
96 // must guard coordinates (all in-crate callers do).
97 let img = solid(2, 2, [7, 8, 9, 255]);
98 let _ = img.get(5, 0);
99}
100
101#[test]
102fn rgba_sample_bilinear_midpoint_averages_neighbours() {
103 let mut img = Rgba::new(2, 1);
104 img.set(0, 0, [0, 0, 0, 255]);
105 img.set(1, 0, [100, 200, 40, 255]);
106 let mid = img.sample(0.5, 0.0).expect("opaque sample");
107 assert!((mid[0] - 50.0).abs() < 1e-4, "r {}", mid[0]);
108 assert!((mid[1] - 100.0).abs() < 1e-4, "g {}", mid[1]);
109 assert!((mid[2] - 20.0).abs() < 1e-4, "b {}", mid[2]);
110 // Exact pixel centre returns that pixel.
111 let c = img.sample(1.0, 0.0).expect("centre");
112 assert!((c[0] - 100.0).abs() < 1e-4);
113}
114
115#[test]
116fn rgba_sample_transparency_rules() {
117 let mut img = Rgba::new(3, 3);
118 img.set(1, 1, [255, 255, 255, 255]);
119 assert!(img.sample(1.0, 1.0).is_some());
120 // At the corner the bilinear neighbourhood *includes* the opaque centre
121 // as one corner, so sample returns Some even though the weighted value
122 // there is fully transparent black.
123 let corner = img.sample(0.0, 0.0).expect("neighbourhood has one opaque corner");
124 assert_eq!(corner, [0.0, 0.0, 0.0, 0.0]);
125 // A fully transparent image samples to None everywhere in bounds.
126 let clear = Rgba::new(3, 3);
127 assert!(clear.sample(1.0, 1.0).is_none());
128 assert!(clear.sample(0.0, 0.0).is_none());
129}
130
131#[test]
132fn rgba_resized_to_same_size_is_identity_and_downscale_averages() {
133 let img = solid(4, 4, [80, 40, 20, 255]);
134 let same = img.resized(4, 4);
135 assert_eq!(same.px, img.px);
136 let small = img.resized(2, 2);
137 assert!(small.px.chunks_exact(4).all(|p| p == [80, 40, 20, 255]));
138 let one = img.resized(1, 1);
139 assert_eq!(one.get(0, 0), [80, 40, 20, 255]);
140}
141
142#[test]
143fn to_gray_uses_integer_luma_weights() {
144 let mut img = Rgba::new(3, 1);
145 img.set(0, 0, [255, 255, 255, 255]);
146 img.set(1, 0, [255, 0, 0, 255]);
147 img.set(2, 0, [0, 0, 255, 255]);
148 let g = camera_stitch::imgbuf::to_gray(&img);
149 assert_eq!(g.px[0], 255);
150 assert_eq!(g.px[1], ((255u32 * 77) >> 8) as u8);
151 assert_eq!(g.px[2], ((255u32 * 29) >> 8) as u8);
152}
153
154#[test]
155fn gray_resize_and_blur_keep_constant_images_constant() {
156 let mut g = camera_stitch::imgbuf::Gray::new(6, 6);
157 for v in g.px.iter_mut() {
158 *v = 90;
159 }
160 let blurred = g.gaussian_blur_3x3();
161 assert!(blurred.px.iter().all(|&v| v == 90));
162 let small = g.resized(3, 3);
163 assert_eq!((small.w, small.h), (3, 3));
164 assert!(small.px.iter().all(|&v| v == 90));
165}
166
167#[test]
168#[should_panic]
169fn gray_at_out_of_bounds_panics() {
170 // Like Rgba::get, Gray::at indexes unchecked.
171 let g = camera_stitch::imgbuf::Gray::new(4, 4);
172 let _ = g.at(100, 100);
173}
174
175// -------------------------------------------------------------- features ---
176
177fn feats_with_desc(descs: Vec<[u8; 32]>) -> Features {
178 let n = descs.len();
179 Features {
180 kps: (0..n)
181 .map(|i| KeyPoint { x: i as f32 * 10.0, y: 5.0, angle: 0.0, score: 1.0 })
182 .collect(),
183 desc: descs,
184 w: 100,
185 h: 50,
186 }
187}
188
189#[test]
190fn match_features_empty_inputs_match_nothing() {
191 let empty = feats_with_desc(vec![]);
192 let one = feats_with_desc(vec![[0u8; 32]]);
193 assert!(camera_stitch::features::match_features(&empty, &one, 0.8).is_empty());
194 assert!(camera_stitch::features::match_features(&one, &empty, 0.8).is_empty());
195}
196
197#[test]
198fn match_features_identical_descriptor_wins_over_distant_ones() {
199 let target = [0xAAu8; 32];
200 let mut far = [0u8; 32];
201 far[0] = 0xFF; // some bits differ from both
202 let a = feats_with_desc(vec![target]);
203 // best = identical (dist 0), second = far (dist > 0): ratio test passes.
204 let b = feats_with_desc(vec![far, target, [0x55u8; 32]]);
205 let m = camera_stitch::features::match_features(&a, &b, 0.8);
206 assert_eq!(m, vec![(0, 1)]);
207}
208
209#[test]
210fn match_features_ratio_rejects_ambiguous_equidistant_candidates() {
211 // Two identical candidates: best == second, so best < ratio*second fails.
212 let d = [3u8; 32];
213 let a = feats_with_desc(vec![d]);
214 let b = feats_with_desc(vec![d, d]);
215 assert!(camera_stitch::features::match_features(&a, &b, 0.8).is_empty());
216}
217
218#[test]
219fn detect_and_describe_on_blank_and_tiny_images_is_empty_but_safe() {
220 let blank = camera_stitch::imgbuf::Gray::new(64, 64);
221 let f = camera_stitch::features::detect_and_describe(&blank, 100, 20);
222 assert!(f.kps.is_empty() && f.desc.is_empty());
223 assert_eq!((f.w, f.h), (64, 64));
224 let tiny = camera_stitch::imgbuf::Gray::new(2, 2);
225 let f2 = camera_stitch::features::detect_and_describe(&tiny, 100, 20);
226 assert!(f2.kps.len() <= 100);
227 assert_eq!(f2.kps.len(), f2.desc.len(), "one descriptor per keypoint");
228}
229
230// -------------------------------------------------------------- geometry ---
231
232#[test]
233fn transfer_inliers_with_identity_maps_points_to_themselves() {
234 let pts: Vec<(f32, f32)> = vec![(0.0, 0.0), (10.0, 3.0), (-5.0, 7.0)];
235 let inl = camera_stitch::geometry::transfer_inliers(&Matrix3::identity(), &pts, &pts, 1.0);
236 assert_eq!(inl, vec![0, 1, 2]);
237 // A destination 100px away is not an inlier of the identity.
238 let far: Vec<(f32, f32)> = pts.iter().map(|(x, y)| (x + 100.0, *y)).collect();
239 assert!(
240 camera_stitch::geometry::transfer_inliers(&Matrix3::identity(), &pts, &far, 1.0).is_empty()
241 );
242}
243
244#[test]
245fn homography_needs_at_least_four_points() {
246 let a = vec![(0.0f32, 0.0f32), (1.0, 0.0), (0.0, 1.0)];
247 assert!(camera_stitch::geometry::find_homography_ransac(&a, &a, 50, 1.0).is_none());
248 assert!(camera_stitch::geometry::find_homography_ransac(&[], &[], 50, 1.0).is_none());
249}
250
251#[test]
252fn homography_recovers_pure_scale_about_origin() {
253 let a: Vec<(f32, f32)> =
254 vec![(1.0, 1.0), (4.0, 1.0), (4.0, 4.0), (1.0, 4.0), (2.0, 3.0), (7.0, 2.0), (3.0, 8.0)];
255 let b: Vec<(f32, f32)> = a.iter().map(|(x, y)| (2.0 * x, 2.0 * y)).collect();
256 let (h, inl) = camera_stitch::geometry::find_homography_ransac(&a, &b, 200, 1.0).expect("h");
257 assert!(inl >= 4, "inliers {inl}");
258 assert!(approx(h[(0, 0)], 2.0, 0.05), "h00 {}", h[(0, 0)]);
259 assert!(approx(h[(1, 1)], 2.0, 0.05), "h11 {}", h[(1, 1)]);
260 assert!(approx(h[(0, 2)], 0.0, 0.05), "h02 {}", h[(0, 2)]);
261}
262
263// ---------------------------------------------------------------- linalg ---
264
265#[test]
266fn matrix3_inverse_undoes_multiplication() {
267 let m = Matrix3::new(2.0, 0.5, 1.0, 0.0, 3.0, 0.25, 1.0, 0.0, 2.0);
268 let inv = m.try_inverse().expect("invertible");
269 let prod = m * inv;
270 for i in 0..3 {
271 for j in 0..3 {
272 let want = if i == j { 1.0 } else { 0.0 };
273 assert!(approx(prod[(i, j)], want, 1e-9), "[{i}{j}] {}", prod[(i, j)]);
274 }
275 }
276 assert!(approx(m.determinant() * inv.determinant(), 1.0, 1e-9));
277}
278
279#[test]
280fn singular_matrix_has_no_inverse_and_zero_determinant() {
281 let m = Matrix3::new(1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 0.0, 1.0, 0.0); // row1 = 2*row0
282 assert!(approx(m.determinant(), 0.0, 1e-12));
283 assert!(m.try_inverse().is_none());
284}
285
286#[test]
287fn vector3_cross_dot_and_norm_are_consistent() {
288 let x = Vector3::new(1.0, 0.0, 0.0);
289 let y = Vector3::new(0.0, 1.0, 0.0);
290 let z = x.cross(&y);
291 assert!(approx(z.z, 1.0, 1e-12) && approx(z.x, 0.0, 1e-12));
292 assert!(approx(x.dot(&y), 0.0, 1e-12));
293 assert!(approx(Vector3::new(3.0, 4.0, 0.0).norm(), 5.0, 1e-12));
294 assert!(approx(x.cross(&y).norm(), 1.0, 1e-12), "unit axes give unit cross");
295}