use super::*;
#[must_use]
pub fn compute_scene_topo_order(nodes: &[SceneNode3D]) -> (Vec<u32>, Vec<u16>) {
let count: usize = nodes.len();
let mut depths: Vec<u16> = vec![0u16; count];
for node_idx in 0..count {
let mut depth: u16 = 0;
let mut cursor: Option<u32> = nodes[node_idx].try_get_parent();
let mut guard: usize = 0;
while let Some(parent_idx) = cursor {
let parent_usize: usize = parent_idx as usize;
if parent_usize >= count || guard >= count {
break;
}
depth = depth.saturating_add(1);
cursor = nodes[parent_usize].try_get_parent();
guard += 1;
}
depths[node_idx] = depth;
}
let mut order: Vec<u32> = (0..(count as u32)).collect();
order.sort_by(|left_id: &u32, right_id: &u32| {
depths[*left_id as usize]
.cmp(&depths[*right_id as usize])
.then_with(|| left_id.cmp(right_id))
});
(order, depths)
}