// Copyright (c) 2026 LlamaIndex, Inc. All rights reserved. // Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository). // This is not open-source software: see the license for permitted uses. use std::slice; fn root(parents: &mut [usize], mut i: usize) -> usize { while parents[i] != i { parents[i] = parents[parents[i]]; i = parents[i]; } i } // The Python boundary provides finite N x 4 boxes in pixel-edge coordinates. // Output indices retain transcript order; offsets delimit at most N paths. #[no_mangle] pub unsafe extern "C" fn paragraph_paths( n: usize, boxes: *const f64, output: *mut i64, offsets: *mut i64, ) -> usize { if n < 3 { return 0; } let flat = slice::from_raw_parts(boxes, n * 4); let b: Vec<&[f64]> = flat.chunks_exact(4).collect(); let mut heights: Vec = b.iter().map(|a| a[3] - a[1]).collect(); heights.sort_by(|a, b| a.total_cmp(b)); let h = if n % 2 == 1 { heights[n / 2] } else { (heights[n / 2 - 1] + heights[n / 2]) / 2.0 }; let h = h.max(1.0); let mut parents: Vec = (0..n).collect(); for i in 0..n { for j in 0..i { let a = b[i]; let z = b[j]; let dx = (a[0] - z[2]).max(z[0] - a[2]).max(0.0); let dy = (a[1] - z[3]).max(z[1] - a[3]).max(0.0); if dx <= 4.0 * h && dy <= 2.5 * h { let r = root(&mut parents, i); let q = root(&mut parents, j); parents[r] = q; } } } let mut keys = Vec::new(); let mut groups: Vec> = Vec::new(); for i in 0..n { let r = root(&mut parents, i); if let Some(k) = keys.iter().position(|&x| x == r) { groups[k].push(i); } else { keys.push(r); groups.push(vec![i]); } } let mut paths: Vec> = Vec::new(); for ids in groups { let mut lengths = vec![1; n]; let mut prev = vec![usize::MAX; n]; for (pos, &i) in ids.iter().enumerate() { for &j in &ids[..pos] { let a = b[j]; let z = b[i]; let overlap = a[3].min(z[3]) - a[1].max(z[1]); let same = overlap >= 0.5 * (a[3] - a[1]).min(z[3] - z[1]); let follows = if same { z[0] >= a[0] + 0.2 * h && z[0] - a[2] >= -0.5 * h && z[0] - a[2] <= 4.0 * h } else { z[1] >= a[1] + 0.5 * h && z[1] - a[3] <= 2.5 * h && z[0] < a[2] }; if follows && lengths[j] + 1 > lengths[i] { lengths[i] = lengths[j] + 1; prev[i] = j; } } } let mut end = ids[0]; for &i in &ids { if lengths[i] > lengths[end] { end = i; } } let mut path = vec![end]; while prev[end] != usize::MAX { end = prev[end]; path.push(end); } path.reverse(); if path.len() >= 3 { paths.push(path); } } if paths.is_empty() { return 0; } paths.sort_by_key(|p| p[0]); if (paths.iter().map(|p| p.len()).sum::() as f64) < 0.6 * n as f64 { return 0; } let mut ambiguous = false; for pair in paths.windows(2) { let left = &pair[0]; let right = &pair[1]; let gutter = right.iter().map(|&i| b[i][0]).fold(f64::INFINITY, f64::min) - left .iter() .map(|&i| b[i][2]) .fold(f64::NEG_INFINITY, f64::max); if left[left.len() - 1] >= right[0] || gutter < h || b[right[0]][1] > b[left[left.len() - 1]][1] { ambiguous = true; break; } } if ambiguous { let mut best = 0; for i in 1..paths.len() { if paths[i].len() > paths[best].len() { best = i; } } if (paths[best].len() as f64) < 0.6 * n as f64 { return 0; } paths = vec![paths[best].clone()]; } let mut count = 0; *offsets = 0; for (k, path) in paths.iter().enumerate() { for &i in path { *output.add(count) = i as i64; count += 1; } *offsets.add(k + 1) = count as i64; } paths.len() }