Skip to main content

libmv_capi_sys/
lib.rs

1#![allow(warnings)]
2include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
3
4#[cfg(test)]
5mod tests {
6    use super::*;
7
8    #[test]
9    fn it_works() {
10        let tracks = unsafe { libmv_tracksNew() };
11        assert!(!tracks.is_null());
12        unsafe { libmv_tracksDestroy(tracks) };
13    }
14
15    /// Returns a black image with a 5x3 white square at the given position.
16    fn test_image_data(x: usize, y: usize) -> Vec<f32> {
17        let mut data = vec![0.0; 300 * 200];
18
19        for i in 0..15 {
20            let current_x = x + i % 5;
21            let current_y = y + i / 5;
22            data[current_y * 300 + current_x] = 1.0;
23        }
24
25        data
26    }
27
28    /// A test that uses functions requiring Ceres,
29    /// to make sure we successfully link to it.
30    #[test]
31    fn track_region() {
32        let image1_data = test_image_data(100, 100);
33        let image2_data = test_image_data(102, 103);
34
35        let libmv_options = libmv_TrackRegionOptions {
36            direction: libmv_TrackRegionDirection_LIBMV_TRACK_REGION_FORWARD,
37            motion_model: 0, // translation
38            num_iterations: 50,
39            use_brute: 1,
40            use_normalization: 0,
41            minimum_correlation: 0.75,
42            sigma: 0.9,
43            image1_mask: std::ptr::null_mut(),
44        };
45
46        let x1: [f64; 5] = [90.0, 110.0, 110.0, 90.0, 100.0];
47        let y1: [f64; 5] = [90.0, 90.0, 110.0, 110.0, 100.0];
48        let mut x2 = x1.clone();
49        let mut y2 = y1.clone();
50
51        let result = unsafe {
52            libmv_trackRegion(
53                &libmv_options as *const libmv_TrackRegionOptions,
54                image1_data.as_ptr(),
55                300,
56                200,
57                image2_data.as_ptr(),
58                300,
59                200,
60                x1.as_ptr(),
61                y1.as_ptr(),
62                std::ptr::null_mut(), // argument is not used by the C API
63                x2.as_mut_ptr(),
64                y2.as_mut_ptr(),
65            )
66        };
67
68        assert_eq!(result, 1, "tracking should have succeeded");
69
70        // verify that tracking did take place
71        assert!((x2[4] - 102.0).abs() < 0.5);
72        assert!((y2[4] - 103.0).abs() < 0.5);
73    }
74}