|
| 1 | +use ordered_float::NotNan; |
| 2 | +use pathfinder_geometry::rect::RectF; |
| 3 | + |
| 4 | +pub fn gap_list<'a>(boxes: &'a [(RectF, usize)], span: impl Fn(&RectF) -> (f32, f32) + 'a) -> impl Iterator<Item=(f32, f32, usize)> + 'a { |
| 5 | + let mut boxes = boxes.iter(); |
| 6 | + let &(ref r, _) = boxes.next().unwrap(); |
| 7 | + let (_, mut last_max) = span(r); |
| 8 | + boxes.enumerate().filter_map(move |(idx, &(ref r, _))| { |
| 9 | + // top left y, bottom right y |
| 10 | + let (min, max) = span(&r); |
| 11 | + let r = if min > last_max { |
| 12 | + Some((last_max, min, idx+1)) |
| 13 | + } else { |
| 14 | + None |
| 15 | + }; |
| 16 | + last_max = max.max(last_max); |
| 17 | + r |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +pub fn gaps<'a>(threshold: f32, boxes: &'a [(RectF, usize)], span: impl Fn(&RectF) -> (f32, f32) + 'a) -> impl Iterator<Item=f32> + 'a { |
| 22 | + let mut boxes = boxes.iter(); |
| 23 | + let &(ref r, _) = boxes.next().unwrap(); |
| 24 | + let (_, mut last_max) = span(r); |
| 25 | + boxes.filter_map(move |&(ref r, _)| { |
| 26 | + let (min, max) = span(&r); |
| 27 | + let r = if min - last_max >= threshold { |
| 28 | + Some(0.5 * (last_max + min)) |
| 29 | + } else { |
| 30 | + None |
| 31 | + }; |
| 32 | + last_max = max.max(last_max); |
| 33 | + r |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +pub fn max_gap(boxes: &[(RectF, usize)], span: impl Fn(&RectF) -> (f32, f32)) -> Option<(f32, f32)> { |
| 38 | + gap_list(boxes, span) |
| 39 | + .max_by_key(|&(a, b, _)| NotNan::new(b - a).unwrap()) |
| 40 | + .map(|(a, b, _)| (b - a, 0.5 * (a + b))) |
| 41 | +} |
| 42 | + |
| 43 | +pub fn dist_x(boxes: &[(RectF, usize)]) -> Option<(f32, f32)> { |
| 44 | + max_gap(boxes, |r| (r.min_x(), r.max_x())) |
| 45 | +} |
| 46 | +pub fn dist_y(boxes: &[(RectF, usize)]) -> Option<(f32, f32)> { |
| 47 | + max_gap(boxes, |r| (r.min_y(), r.max_y())) |
| 48 | +} |
| 49 | + |
| 50 | +pub fn top_bottom_gap(boxes: &mut [(RectF, usize)], bbox: RectF) -> (Option<usize>, Option<usize>) { |
| 51 | + let num_boxes = boxes.len(); |
| 52 | + if num_boxes < 2 { |
| 53 | + return (None, None); |
| 54 | + } |
| 55 | + |
| 56 | + let mut gaps = gap_list(boxes, |r| ( |
| 57 | + // top left y |
| 58 | + r.min_y(), |
| 59 | + // bottom right y |
| 60 | + r.max_y() |
| 61 | + )); |
| 62 | + let top_limit = bbox.min_y() + bbox.height() * 0.2; |
| 63 | + let bottom_limit = bbox.min_y() + bbox.height() * 0.8; |
| 64 | + |
| 65 | + match gaps.next() { |
| 66 | + Some((y, _, top)) if y < top_limit => { |
| 67 | + match gaps.last() { |
| 68 | + Some((y, _, bottom)) if y > bottom_limit => (Some(top), Some(bottom)), |
| 69 | + _ => (Some(top), None) |
| 70 | + } |
| 71 | + } |
| 72 | + Some((y, _, bottom)) if y > bottom_limit => (None, Some(bottom)), |
| 73 | + _ => (None, None) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +pub fn left_right_gap(boxes: &mut [(RectF, usize)], bbox: RectF) -> (Option<usize>, Option<usize>) { |
| 78 | + let num_boxes = boxes.len(); |
| 79 | + if num_boxes < 2 { |
| 80 | + return (None, None); |
| 81 | + } |
| 82 | + |
| 83 | + let mut gaps = gap_list(boxes, |r| (r.min_x(), r.max_x())); |
| 84 | + let left_limit = bbox.min_x() + bbox.width() * 0.2; |
| 85 | + let right_limit = bbox.min_x() + bbox.width() * 0.8; |
| 86 | + match gaps.next() { |
| 87 | + Some((x, _, left)) if x < left_limit => { |
| 88 | + match gaps.last() { |
| 89 | + Some((x, _, right)) if x > right_limit => (Some(left), Some(right)), |
| 90 | + _ => (Some(left), None) |
| 91 | + } |
| 92 | + } |
| 93 | + Some((x, _, right)) if x > right_limit => (None, Some(right)), |
| 94 | + _ => (None, None) |
| 95 | + } |
| 96 | +} |
0 commit comments