use packed_spatial_index::{Index3D, Index3DView, Point3D, Ray3D, Triangle3D};
fn main() {
let mut tris = Vec::new();
for i in 0..10 {
for j in 0..10 {
let (x, y) = (i as f64, j as f64);
let z = if (i + j) % 7 == 0 { 3.0 } else { 0.0 };
tris.push(Triangle3D::new([x, y, z], [x + 1.0, y, z], [x, y + 1.0, z]));
}
}
let index = Index3D::from_triangles(&tris).unwrap();
let bytes = index.serialize().triangles(&tris).to_bytes().unwrap();
println!(
"{} triangles serialized to {} bytes",
tris.len(),
bytes.len()
);
let view = Index3DView::from_bytes(&bytes).unwrap();
let ray = Ray3D::new(Point3D::new(4.5, 4.5, 10.0), 0.0, 0.0, -1.0, 100.0);
let candidate_ids = index.raycast(ray);
println!("broad phase: {} candidate triangles", candidate_ids.len());
let candidates: Vec<Triangle3D> = candidate_ids
.iter()
.map(|&id| view.triangle(id).expect("triangle payload"))
.collect();
match ray.closest_triangle(&candidates) {
Some(hit) => {
let id = candidate_ids[hit.index];
println!(
"hit triangle #{id} at t = {:.3} (z = {:.2})",
hit.t,
10.0 - hit.t
);
}
None => println!("ray missed the mesh"),
}
}