tinyspline_sys/
lib.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4
5use std::env;
6
7type size_t = usize;
8
9include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
10
11#[cfg(test)]
12mod tests {
13    use crate::*;
14    use std::ptr;
15    use std::slice;
16    const EPSILON: tsReal = 0.0001;
17    #[test]
18    fn interpolation() {
19        // adapted from https://github.com/msteinbeck/tinyspline/blob/master/test/c/interpolation.c
20        unsafe {
21            let points: [tsReal; 10] = [
22                 1.0,
23                -1.0,
24                -1.0,
25                 2.0,
26                 1.0,
27                 4.0,
28                 4.0,
29                 3.0,
30                 7.0,
31                 5.0,
32            ];
33            let mut ctrlp: *mut tsReal = ptr::null_mut();
34            let mut knots: *mut tsReal = ptr::null_mut();
35            let mut spline = ts_bspline_init();
36            let mut status = tsStatus {
37                code: tsError_TS_SUCCESS,
38                message: [0; 100]
39            };
40            ts_bspline_interpolate_cubic_natural(
41                &points as *const tsReal, 5, 2,
42                &mut spline as *mut tsBSpline,
43                &mut status as *mut tsStatus
44            );
45            if status.code != tsError_TS_SUCCESS {
46                panic!("Error on ts_bspline_interpolate_cubic_natural");
47            }
48            ts_bspline_control_points(
49                &mut spline as *mut tsBSpline,
50                &mut ctrlp as *mut *mut tsReal,
51                &mut status as *mut tsStatus
52            );
53            if status.code != tsError_TS_SUCCESS {
54                panic!("Error on ts_bspline_control_points");
55            }
56            let numpts = ts_bspline_num_control_points(&mut spline as *mut tsBSpline);
57            if numpts != 16 {
58                panic!("Number of control points does not equal 16 (got {})", numpts);
59            }
60            let target_points = [
61                 1.0,
62                -1.0,
63                 0.0,
64                 0.0,
65                -1.0,
66                 1.0,
67                -1.0,
68                 2.0,
69                -1.0,
70                 2.0,
71                -1.0,
72                 3.0,
73                 0.0,
74                 4.0,
75                 1.0,
76                 4.0,
77                 1.0,
78                 4.0,
79                 2.0,
80                 4.0,
81                 3.0,
82                 3.0,
83                 4.0,
84                 3.0,
85                 4.0,
86                 3.0,
87                 5.0,
88                 3.0,
89                 6.0,
90                 4.0,
91                 7.0,
92                 5.0
93            ];
94            let ctrlp = std::slice::from_raw_parts(ctrlp, 32);
95            for (i, value) in target_points.iter().enumerate() {
96                if (value - ctrlp[i]).abs() > EPSILON {
97                    panic!("Mismatch control point at index {}: {}, {}", i, value, ctrlp[i]);
98                }
99            }
100            ts_bspline_knots(
101                &mut spline as *mut tsBSpline,
102                &mut knots as *mut *mut tsReal,
103                &mut status as *mut tsStatus
104            );
105            if status.code != tsError_TS_SUCCESS {
106                panic!("Error on ts_bspline_knots");
107            }
108            let target_knots = [
109                0.0, 
110                0.0, 
111                0.0, 
112                0.0, 
113                0.25,
114                0.25,
115                0.25,
116                0.25,
117                0.5, 
118                0.5, 
119                0.5, 
120                0.5, 
121                0.75,
122                0.75,
123                0.75,
124                0.75,
125                1.0, 
126                1.0, 
127                1.0, 
128                1.0, 
129            ];
130            let knots = slice::from_raw_parts(knots, 20);
131            for (i, value) in target_knots.iter().enumerate() {
132                if (value - knots[i]).abs() > EPSILON {
133                    panic!("Mismatch knot at index {}: {}, {}", i, value, ctrlp[i]);
134                }
135            }
136            ts_bspline_free(&mut spline as *mut tsBSpline);
137        }
138    }
139}