fn boxclip_min2(a: f64, b: f64) -> f64 { if a < b { return a; } b } fn boxclip_max2(a: f64, b: f64) -> f64 { if a > b { return a; } b } fn boxclip_finite(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value - value == 0.0) { return false; } i += 1; } true } fn boxclip_compute(box: vector, width: f64, height: f64) -> vector { if len(box) != 4 { return []; } let left = boxclip_max2(0.0, at(box, 0)); let top = boxclip_max2(0.0, at(box, 1)); let right = boxclip_min2(width, at(box, 2)); let bottom = boxclip_min2(height, at(box, 3)); if right <= left || bottom <= top { return []; } [left, top, right, bottom] } // Clip a detection rectangle to the visible image. // Returns the visible [left, top, right, bottom] in continuous pixel-edge coordinates. An empty intersection is invalid. pub fn boxclip_inside(box: vector, width: f64, height: f64) -> vector { if !boxclip_finite(box) { return []; } if !(width > 0.0 && width - width == 0.0) { return []; } if !(height > 0.0 && height - height == 0.0) { return []; } let result = boxclip_compute(box, width, height); if !boxclip_finite(result) { return []; } result }