//! Per-sub-mesh vertex/index/coverage dump for one resource.
//! Usage: submesh_dump <container.xpr> <resource>...
use sylpheed_formats::mesh::{debug_resource_params, Xbg7Model};
use std::collections::HashSet;
fn main() {
    let a: Vec<String> = std::env::args().collect();
    let bytes = std::fs::read(&a[1]).expect("container");
    let want: HashSet<String> = a[2..].iter().cloned().collect();
    for m in Xbg7Model::models_named(&bytes, &want, &|| false) {
        let markers = debug_resource_params(&bytes, &m.name).map(|(mk, _)| mk).unwrap_or_default();
        println!("{} — {} sub-meshes decoded, {} markers declared", m.name, m.meshes.len(), markers.len());
        for (i, s) in m.meshes.iter().enumerate() {
            let max_idx = s.indices.iter().max().copied().unwrap_or(0) as usize;
            let (mut lo, mut hi) = ([f32::MAX; 3], [f32::MIN; 3]);
            for q in &s.positions {
                for k in 0..3 {
                    lo[k] = lo[k].min(q[k]);
                    hi[k] = hi[k].max(q[k]);
                }
            }
            println!(
                "  #{i:<2} at 0x{:<9x} verts {:<6} idx {:<6} max_idx {:<6} slack {} span [{:.0} {:.0} {:.0}]",
                s.vbuf_offset.unwrap_or(0),
                s.positions.len(),
                s.indices.len(),
                max_idx,
                s.positions.len() as i64 - 1 - max_idx as i64,
                hi[0] - lo[0],
                hi[1] - lo[1],
                hi[2] - lo[2]
            );
        }
    }
}
