@@ -17,31 +17,6 @@ pub fn as_nat(f: f64) -> Result<u64> {
1717 }
1818}
1919
20- pub fn as_log2 ( f : f64 ) -> Result < u8 > {
21- let i = as_nat ( f) ?;
22-
23- match i & ( i - 1 ) == 0 {
24- true => Ok ( i. trailing_zeros ( ) as u8 ) ,
25- false => Err ( "Input must be a power of two" . to_string ( ) ) ,
26- }
27- }
28-
29- pub fn str_slice_to_mx_class_id ( class_id : & str ) -> Result < MxClassId > {
30- match class_id {
31- "uint8" => Ok ( MxClassId :: Uint8 ) ,
32- "uint16" => Ok ( MxClassId :: Uint16 ) ,
33- "uint32" => Ok ( MxClassId :: Uint32 ) ,
34- "uint64" => Ok ( MxClassId :: Uint64 ) ,
35- "single" => Ok ( MxClassId :: Single ) ,
36- "double" => Ok ( MxClassId :: Double ) ,
37- "int8" => Ok ( MxClassId :: Int8 ) ,
38- "int16" => Ok ( MxClassId :: Int16 ) ,
39- "int32" => Ok ( MxClassId :: Int32 ) ,
40- "int64" => Ok ( MxClassId :: Int64 ) ,
41- _ => Err ( "Unknown MxClassId name" . to_string ( ) ) ,
42- }
43- }
44-
4520pub fn mx_array_to_str < ' a > ( pm : MxArray ) -> Result < & ' a str > {
4621 let pm_ptr = unsafe { mxArrayToUTF8String ( pm) } ;
4722
@@ -76,15 +51,6 @@ pub fn mx_array_to_f64_slice<'a>(pm: MxArray) -> Result<&'a [f64]> {
7651 }
7752}
7853
79- pub fn mx_array_to_f64 ( pm : MxArray ) -> Result < f64 > {
80- let pm_slice = mx_array_to_f64_slice ( pm) ?;
81-
82- match pm_slice. len ( ) {
83- 1 => Ok ( pm_slice[ 0 ] ) ,
84- _ => Err ( "MxArray contains an invalid number of doubles" . to_string ( ) ) ,
85- }
86- }
87-
8854pub fn mx_array_to_u8_slice < ' a > ( pm : MxArray ) -> Result < & ' a [ u8 ] > {
8955 let numel = unsafe { mxGetNumberOfElements ( pm) } ;
9056 let elem_size = unsafe { mxGetElementSize ( pm) } ;
@@ -221,3 +187,57 @@ pub fn copy_as_fortran_order(
221187
222188 Ok ( ( ) )
223189}
190+
191+ fn f64_slice_to_vec ( buf : & [ f64 ] ) -> Result < Vec < u64 > > {
192+ buf. iter ( )
193+ . map ( |x| as_nat ( * x) . or ( Err ( "Invalid value" . to_string ( ) ) ) )
194+ . collect ( )
195+ }
196+
197+ pub fn mx_array_to_bbox ( pm : MxArray , ndim : usize ) -> Result < ( Vec < u64 > , Vec < u64 > ) > {
198+ let buf = mx_array_to_f64_slice ( pm) ?;
199+
200+ // verify shape of array
201+ let input_arg_shape = mx_array_size_to_usize_slice ( pm) ;
202+ if input_arg_shape != & [ ndim, 2 ] {
203+ return Err ( format ! (
204+ "Bounding box has invalid shape. Needs to be [{}, 2]. Got {:?}." ,
205+ ndim, input_arg_shape
206+ ) ) ;
207+ }
208+
209+ let bbox_min_f64 = & buf[ 0 ..ndim] ;
210+ let bbox_max_f64 = & buf[ ndim..( ndim * 2 ) ] ;
211+ let bbox_min = f64_slice_to_vec ( bbox_min_f64)
212+ . or ( Err ( format ! ( "Invalid lower bound. Got {:?}." , bbox_min_f64) ) ) ?;
213+ let bbox_max = f64_slice_to_vec ( bbox_max_f64)
214+ . or ( Err ( format ! ( "Invalid upper bound. Got {:?}." , bbox_max_f64) ) ) ?;
215+
216+ if bbox_min
217+ . iter ( )
218+ . zip ( bbox_max. iter ( ) )
219+ . any ( |( min_x, max_x) | min_x >= max_x)
220+ {
221+ return Err ( format ! (
222+ "Bounding box has invalid shape. Got min={:?}, max={:?}." ,
223+ bbox_min, bbox_max
224+ ) ) ;
225+ }
226+
227+ let bbox_shape: Vec < u64 > = bbox_min
228+ . iter ( )
229+ . zip ( bbox_max. iter ( ) )
230+ . map ( |( min_x, max_x) | max_x - min_x)
231+ . collect ( ) ;
232+
233+ let bbox_min = bbox_min. iter ( ) . map ( |x| x - 1 ) . collect ( ) ;
234+
235+ if bbox_shape. iter ( ) . any ( |x| * x < 1 ) {
236+ return Err ( format ! (
237+ "Bounding box has invalid shape. Got {:?}." ,
238+ bbox_shape
239+ ) ) ;
240+ }
241+
242+ Ok ( ( bbox_min, bbox_shape) )
243+ }
0 commit comments