use crate::component::Component;
pub fn classify_by_geometry(comps: &mut [Component], img_shape: (u32, u32)) {
let (img_h, img_w) = (img_shape.0 as f64, img_shape.1 as f64);
let img_area = img_h * img_w;
for comp in comps.iter_mut() {
if comp.category != "Compo" {
continue;
}
let w = comp.bbox.width() as f64;
let h = comp.bbox.height() as f64;
let area = comp.bbox.area() as f64;
let ratio = w / h;
let area_ratio = area / img_area;
if area_ratio > 0.08 && (0.3..=5.0).contains(&ratio) {
comp.category = "Image".to_string();
continue;
}
if (w / img_w > 0.5 && h / img_h > 0.03)
|| (h / img_h > 0.5 && w / img_w > 0.03)
{
comp.category = "Block".to_string();
continue;
}
if (0.7..=1.4).contains(&ratio) && h <= 48.0 && w <= 48.0 {
comp.category = "Icon".to_string();
continue;
}
if (0.5..=3.0).contains(&ratio)
&& (20.0..=80.0).contains(&h)
&& (20.0..=200.0).contains(&w)
&& area / (w * h) > 0.8
{
comp.category = "Button".to_string();
continue;
}
if h / img_h < 0.025 && ratio > 3.0 {
comp.category = "Text".to_string();
continue;
}
}
}